Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8774 rgimad 1
/**
2
 * \file sha1.h
3
 *
4
 * \brief This file contains SHA-1 definitions and functions.
5
 *
6
 * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in
7
 * FIPS 180-4: Secure Hash Standard (SHS).
8
 *
9
 * \warning   SHA-1 is considered a weak message digest and its use constitutes
10
 *            a security risk. We recommend considering stronger message
11
 *            digests instead.
12
 */
13
/*
14
 *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
15
 *  SPDX-License-Identifier: GPL-2.0
16
 *
17
 *  This program is free software; you can redistribute it and/or modify
18
 *  it under the terms of the GNU General Public License as published by
19
 *  the Free Software Foundation; either version 2 of the License, or
20
 *  (at your option) any later version.
21
 *
22
 *  This program is distributed in the hope that it will be useful,
23
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *  GNU General Public License for more details.
26
 *
27
 *  You should have received a copy of the GNU General Public License along
28
 *  with this program; if not, write to the Free Software Foundation, Inc.,
29
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30
 *
31
 *  This file is part of Mbed TLS (https://tls.mbed.org)
32
 */
33
#ifndef MBEDTLS_SHA1_H
34
#define MBEDTLS_SHA1_H
35
 
36
#if !defined(MBEDTLS_CONFIG_FILE)
37
#include "config.h"
38
#else
39
#include MBEDTLS_CONFIG_FILE
40
#endif
41
 
42
#include 
43
#include 
44
 
45
/* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
46
#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED                  -0x0035  /**< SHA-1 hardware accelerator failed */
47
#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA                   -0x0073  /**< SHA-1 input data was malformed. */
48
 
49
#ifdef __cplusplus
50
extern "C" {
51
#endif
52
 
53
#if !defined(MBEDTLS_SHA1_ALT)
54
// Regular implementation
55
//
56
 
57
/**
58
 * \brief          The SHA-1 context structure.
59
 *
60
 * \warning        SHA-1 is considered a weak message digest and its use
61
 *                 constitutes a security risk. We recommend considering
62
 *                 stronger message digests instead.
63
 *
64
 */
65
typedef struct mbedtls_sha1_context
66
{
67
    uint32_t total[2];          /*!< The number of Bytes processed.  */
68
    uint32_t state[5];          /*!< The intermediate digest state.  */
69
    unsigned char buffer[64];   /*!< The data block being processed. */
70
}
71
mbedtls_sha1_context;
72
 
73
#else  /* MBEDTLS_SHA1_ALT */
74
#include "sha1_alt.h"
75
#endif /* MBEDTLS_SHA1_ALT */
76
 
77
/**
78
 * \brief          This function initializes a SHA-1 context.
79
 *
80
 * \warning        SHA-1 is considered a weak message digest and its use
81
 *                 constitutes a security risk. We recommend considering
82
 *                 stronger message digests instead.
83
 *
84
 * \param ctx      The SHA-1 context to initialize.
85
 *                 This must not be \c NULL.
86
 *
87
 */
88
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
89
 
90
/**
91
 * \brief          This function clears a SHA-1 context.
92
 *
93
 * \warning        SHA-1 is considered a weak message digest and its use
94
 *                 constitutes a security risk. We recommend considering
95
 *                 stronger message digests instead.
96
 *
97
 * \param ctx      The SHA-1 context to clear. This may be \c NULL,
98
 *                 in which case this function does nothing. If it is
99
 *                 not \c NULL, it must point to an initialized
100
 *                 SHA-1 context.
101
 *
102
 */
103
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
104
 
105
/**
106
 * \brief          This function clones the state of a SHA-1 context.
107
 *
108
 * \warning        SHA-1 is considered a weak message digest and its use
109
 *                 constitutes a security risk. We recommend considering
110
 *                 stronger message digests instead.
111
 *
112
 * \param dst      The SHA-1 context to clone to. This must be initialized.
113
 * \param src      The SHA-1 context to clone from. This must be initialized.
114
 *
115
 */
116
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
117
                         const mbedtls_sha1_context *src );
118
 
119
/**
120
 * \brief          This function starts a SHA-1 checksum calculation.
121
 *
122
 * \warning        SHA-1 is considered a weak message digest and its use
123
 *                 constitutes a security risk. We recommend considering
124
 *                 stronger message digests instead.
125
 *
126
 * \param ctx      The SHA-1 context to initialize. This must be initialized.
127
 *
128
 * \return         \c 0 on success.
129
 * \return         A negative error code on failure.
130
 *
131
 */
132
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
133
 
134
/**
135
 * \brief          This function feeds an input buffer into an ongoing SHA-1
136
 *                 checksum calculation.
137
 *
138
 * \warning        SHA-1 is considered a weak message digest and its use
139
 *                 constitutes a security risk. We recommend considering
140
 *                 stronger message digests instead.
141
 *
142
 * \param ctx      The SHA-1 context. This must be initialized
143
 *                 and have a hash operation started.
144
 * \param input    The buffer holding the input data.
145
 *                 This must be a readable buffer of length \p ilen Bytes.
146
 * \param ilen     The length of the input data \p input in Bytes.
147
 *
148
 * \return         \c 0 on success.
149
 * \return         A negative error code on failure.
150
 */
151
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
152
                             const unsigned char *input,
153
                             size_t ilen );
154
 
155
/**
156
 * \brief          This function finishes the SHA-1 operation, and writes
157
 *                 the result to the output buffer.
158
 *
159
 * \warning        SHA-1 is considered a weak message digest and its use
160
 *                 constitutes a security risk. We recommend considering
161
 *                 stronger message digests instead.
162
 *
163
 * \param ctx      The SHA-1 context to use. This must be initialized and
164
 *                 have a hash operation started.
165
 * \param output   The SHA-1 checksum result. This must be a writable
166
 *                 buffer of length \c 20 Bytes.
167
 *
168
 * \return         \c 0 on success.
169
 * \return         A negative error code on failure.
170
 */
171
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
172
                             unsigned char output[20] );
173
 
174
/**
175
 * \brief          SHA-1 process data block (internal use only).
176
 *
177
 * \warning        SHA-1 is considered a weak message digest and its use
178
 *                 constitutes a security risk. We recommend considering
179
 *                 stronger message digests instead.
180
 *
181
 * \param ctx      The SHA-1 context to use. This must be initialized.
182
 * \param data     The data block being processed. This must be a
183
 *                 readable buffer of length \c 64 Bytes.
184
 *
185
 * \return         \c 0 on success.
186
 * \return         A negative error code on failure.
187
 *
188
 */
189
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
190
                                   const unsigned char data[64] );
191
 
192
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
193
#if defined(MBEDTLS_DEPRECATED_WARNING)
194
#define MBEDTLS_DEPRECATED      __attribute__((deprecated))
195
#else
196
#define MBEDTLS_DEPRECATED
197
#endif
198
/**
199
 * \brief          This function starts a SHA-1 checksum calculation.
200
 *
201
 * \warning        SHA-1 is considered a weak message digest and its use
202
 *                 constitutes a security risk. We recommend considering
203
 *                 stronger message digests instead.
204
 *
205
 * \deprecated     Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
206
 *
207
 * \param ctx      The SHA-1 context to initialize. This must be initialized.
208
 *
209
 */
210
MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
211
 
212
/**
213
 * \brief          This function feeds an input buffer into an ongoing SHA-1
214
 *                 checksum calculation.
215
 *
216
 * \warning        SHA-1 is considered a weak message digest and its use
217
 *                 constitutes a security risk. We recommend considering
218
 *                 stronger message digests instead.
219
 *
220
 * \deprecated     Superseded by mbedtls_sha1_update_ret() in 2.7.0.
221
 *
222
 * \param ctx      The SHA-1 context. This must be initialized and
223
 *                 have a hash operation started.
224
 * \param input    The buffer holding the input data.
225
 *                 This must be a readable buffer of length \p ilen Bytes.
226
 * \param ilen     The length of the input data \p input in Bytes.
227
 *
228
 */
229
MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
230
                                             const unsigned char *input,
231
                                             size_t ilen );
232
 
233
/**
234
 * \brief          This function finishes the SHA-1 operation, and writes
235
 *                 the result to the output buffer.
236
 *
237
 * \warning        SHA-1 is considered a weak message digest and its use
238
 *                 constitutes a security risk. We recommend considering
239
 *                 stronger message digests instead.
240
 *
241
 * \deprecated     Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
242
 *
243
 * \param ctx      The SHA-1 context. This must be initialized and
244
 *                 have a hash operation started.
245
 * \param output   The SHA-1 checksum result.
246
 *                 This must be a writable buffer of length \c 20 Bytes.
247
 */
248
MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
249
                                             unsigned char output[20] );
250
 
251
/**
252
 * \brief          SHA-1 process data block (internal use only).
253
 *
254
 * \warning        SHA-1 is considered a weak message digest and its use
255
 *                 constitutes a security risk. We recommend considering
256
 *                 stronger message digests instead.
257
 *
258
 * \deprecated     Superseded by mbedtls_internal_sha1_process() in 2.7.0.
259
 *
260
 * \param ctx      The SHA-1 context. This must be initialized.
261
 * \param data     The data block being processed.
262
 *                 This must be a readable buffer of length \c 64 bytes.
263
 *
264
 */
265
MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
266
                                              const unsigned char data[64] );
267
 
268
#undef MBEDTLS_DEPRECATED
269
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
270
 
271
/**
272
 * \brief          This function calculates the SHA-1 checksum of a buffer.
273
 *
274
 *                 The function allocates the context, performs the
275
 *                 calculation, and frees the context.
276
 *
277
 *                 The SHA-1 result is calculated as
278
 *                 output = SHA-1(input buffer).
279
 *
280
 * \warning        SHA-1 is considered a weak message digest and its use
281
 *                 constitutes a security risk. We recommend considering
282
 *                 stronger message digests instead.
283
 *
284
 * \param input    The buffer holding the input data.
285
 *                 This must be a readable buffer of length \p ilen Bytes.
286
 * \param ilen     The length of the input data \p input in Bytes.
287
 * \param output   The SHA-1 checksum result.
288
 *                 This must be a writable buffer of length \c 20 Bytes.
289
 *
290
 * \return         \c 0 on success.
291
 * \return         A negative error code on failure.
292
 *
293
 */
294
int mbedtls_sha1_ret( const unsigned char *input,
295
                      size_t ilen,
296
                      unsigned char output[20] );
297
 
298
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
299
#if defined(MBEDTLS_DEPRECATED_WARNING)
300
#define MBEDTLS_DEPRECATED      __attribute__((deprecated))
301
#else
302
#define MBEDTLS_DEPRECATED
303
#endif
304
/**
305
 * \brief          This function calculates the SHA-1 checksum of a buffer.
306
 *
307
 *                 The function allocates the context, performs the
308
 *                 calculation, and frees the context.
309
 *
310
 *                 The SHA-1 result is calculated as
311
 *                 output = SHA-1(input buffer).
312
 *
313
 * \warning        SHA-1 is considered a weak message digest and its use
314
 *                 constitutes a security risk. We recommend considering
315
 *                 stronger message digests instead.
316
 *
317
 * \deprecated     Superseded by mbedtls_sha1_ret() in 2.7.0
318
 *
319
 * \param input    The buffer holding the input data.
320
 *                 This must be a readable buffer of length \p ilen Bytes.
321
 * \param ilen     The length of the input data \p input in Bytes.
322
 * \param output   The SHA-1 checksum result. This must be a writable
323
 *                 buffer of size \c 20 Bytes.
324
 *
325
 */
326
MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
327
                                      size_t ilen,
328
                                      unsigned char output[20] );
329
 
330
#undef MBEDTLS_DEPRECATED
331
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
332
 
333
#if defined(MBEDTLS_SELF_TEST)
334
 
335
/**
336
 * \brief          The SHA-1 checkup routine.
337
 *
338
 * \warning        SHA-1 is considered a weak message digest and its use
339
 *                 constitutes a security risk. We recommend considering
340
 *                 stronger message digests instead.
341
 *
342
 * \return         \c 0 on success.
343
 * \return         \c 1 on failure.
344
 *
345
 */
346
int mbedtls_sha1_self_test( int verbose );
347
 
348
#endif /* MBEDTLS_SELF_TEST */
349
 
350
#ifdef __cplusplus
351
}
352
#endif
353
 
354
#endif /* mbedtls_sha1.h */