blob: 593f79513af4835f5ba35faab946e943e70fee9c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The SHA-1 standard was published by NIST in 1993.
21 *
22 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_SELF_TEST)
36#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_printf printf
41#endif /* MBEDTLS_PLATFORM_C */
42#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010043
Hanno Beckerb3c70232018-12-20 10:18:05 +000044#define SHA1_VALIDATE_RET(cond) \
45 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
46
47#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
48
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020049#if !defined(MBEDTLS_SHA1_ALT)
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
52 * 32-bit integer manipulation macros (big endian)
53 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000054#ifndef GET_UINT32_BE
55#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000056{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
60 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
62#endif
63
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef PUT_UINT32_BE
65#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000066{ \
67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) ); \
71}
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020075{
Hanno Becker039ccab2018-12-18 17:52:14 +000076 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020079}
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020082{
83 if( ctx == NULL )
84 return;
85
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050086 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020087}
88
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020089void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
90 const mbedtls_sha1_context *src )
91{
Hanno Becker039ccab2018-12-18 17:52:14 +000092 SHA1_VALIDATE( dst != NULL );
93 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000094
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095 *dst = *src;
96}
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/*
99 * SHA-1 context setup
100 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100101int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
Hanno Becker039ccab2018-12-18 17:52:14 +0000103 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000104
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 ctx->total[0] = 0;
106 ctx->total[1] = 0;
107
108 ctx->state[0] = 0x67452301;
109 ctx->state[1] = 0xEFCDAB89;
110 ctx->state[2] = 0x98BADCFE;
111 ctx->state[3] = 0x10325476;
112 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100113
114 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115}
116
Jaeden Amero041039f2018-02-19 15:28:08 +0000117#if !defined(MBEDTLS_DEPRECATED_REMOVED)
118void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
119{
120 mbedtls_sha1_starts_ret( ctx );
121}
122#endif
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100125int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
126 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000128 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Hanno Becker039ccab2018-12-18 17:52:14 +0000130 SHA1_VALIDATE_RET( ctx != NULL );
131 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000132
Paul Bakker5c2364c2012-10-01 14:41:15 +0000133 GET_UINT32_BE( W[ 0], data, 0 );
134 GET_UINT32_BE( W[ 1], data, 4 );
135 GET_UINT32_BE( W[ 2], data, 8 );
136 GET_UINT32_BE( W[ 3], data, 12 );
137 GET_UINT32_BE( W[ 4], data, 16 );
138 GET_UINT32_BE( W[ 5], data, 20 );
139 GET_UINT32_BE( W[ 6], data, 24 );
140 GET_UINT32_BE( W[ 7], data, 28 );
141 GET_UINT32_BE( W[ 8], data, 32 );
142 GET_UINT32_BE( W[ 9], data, 36 );
143 GET_UINT32_BE( W[10], data, 40 );
144 GET_UINT32_BE( W[11], data, 44 );
145 GET_UINT32_BE( W[12], data, 48 );
146 GET_UINT32_BE( W[13], data, 52 );
147 GET_UINT32_BE( W[14], data, 56 );
148 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Hanno Becker1eeca412018-10-15 12:01:35 +0100150#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Hanno Becker1eeca412018-10-15 12:01:35 +0100152#define R(t) \
153 ( \
154 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
155 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
156 ( W[(t) & 0x0F] = S(temp,1) ) \
157 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Hanno Becker1eeca412018-10-15 12:01:35 +0100159#define P(a,b,c,d,e,x) \
160 do \
161 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100162 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
163 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100164 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 A = ctx->state[0];
167 B = ctx->state[1];
168 C = ctx->state[2];
169 D = ctx->state[3];
170 E = ctx->state[4];
171
Hanno Becker1eeca412018-10-15 12:01:35 +0100172#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000173#define K 0x5A827999
174
175 P( A, B, C, D, E, W[0] );
176 P( E, A, B, C, D, W[1] );
177 P( D, E, A, B, C, W[2] );
178 P( C, D, E, A, B, W[3] );
179 P( B, C, D, E, A, W[4] );
180 P( A, B, C, D, E, W[5] );
181 P( E, A, B, C, D, W[6] );
182 P( D, E, A, B, C, W[7] );
183 P( C, D, E, A, B, W[8] );
184 P( B, C, D, E, A, W[9] );
185 P( A, B, C, D, E, W[10] );
186 P( E, A, B, C, D, W[11] );
187 P( D, E, A, B, C, W[12] );
188 P( C, D, E, A, B, W[13] );
189 P( B, C, D, E, A, W[14] );
190 P( A, B, C, D, E, W[15] );
191 P( E, A, B, C, D, R(16) );
192 P( D, E, A, B, C, R(17) );
193 P( C, D, E, A, B, R(18) );
194 P( B, C, D, E, A, R(19) );
195
196#undef K
197#undef F
198
Hanno Becker1eeca412018-10-15 12:01:35 +0100199#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000200#define K 0x6ED9EBA1
201
202 P( A, B, C, D, E, R(20) );
203 P( E, A, B, C, D, R(21) );
204 P( D, E, A, B, C, R(22) );
205 P( C, D, E, A, B, R(23) );
206 P( B, C, D, E, A, R(24) );
207 P( A, B, C, D, E, R(25) );
208 P( E, A, B, C, D, R(26) );
209 P( D, E, A, B, C, R(27) );
210 P( C, D, E, A, B, R(28) );
211 P( B, C, D, E, A, R(29) );
212 P( A, B, C, D, E, R(30) );
213 P( E, A, B, C, D, R(31) );
214 P( D, E, A, B, C, R(32) );
215 P( C, D, E, A, B, R(33) );
216 P( B, C, D, E, A, R(34) );
217 P( A, B, C, D, E, R(35) );
218 P( E, A, B, C, D, R(36) );
219 P( D, E, A, B, C, R(37) );
220 P( C, D, E, A, B, R(38) );
221 P( B, C, D, E, A, R(39) );
222
223#undef K
224#undef F
225
Hanno Becker1eeca412018-10-15 12:01:35 +0100226#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000227#define K 0x8F1BBCDC
228
229 P( A, B, C, D, E, R(40) );
230 P( E, A, B, C, D, R(41) );
231 P( D, E, A, B, C, R(42) );
232 P( C, D, E, A, B, R(43) );
233 P( B, C, D, E, A, R(44) );
234 P( A, B, C, D, E, R(45) );
235 P( E, A, B, C, D, R(46) );
236 P( D, E, A, B, C, R(47) );
237 P( C, D, E, A, B, R(48) );
238 P( B, C, D, E, A, R(49) );
239 P( A, B, C, D, E, R(50) );
240 P( E, A, B, C, D, R(51) );
241 P( D, E, A, B, C, R(52) );
242 P( C, D, E, A, B, R(53) );
243 P( B, C, D, E, A, R(54) );
244 P( A, B, C, D, E, R(55) );
245 P( E, A, B, C, D, R(56) );
246 P( D, E, A, B, C, R(57) );
247 P( C, D, E, A, B, R(58) );
248 P( B, C, D, E, A, R(59) );
249
250#undef K
251#undef F
252
Hanno Becker1eeca412018-10-15 12:01:35 +0100253#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000254#define K 0xCA62C1D6
255
256 P( A, B, C, D, E, R(60) );
257 P( E, A, B, C, D, R(61) );
258 P( D, E, A, B, C, R(62) );
259 P( C, D, E, A, B, R(63) );
260 P( B, C, D, E, A, R(64) );
261 P( A, B, C, D, E, R(65) );
262 P( E, A, B, C, D, R(66) );
263 P( D, E, A, B, C, R(67) );
264 P( C, D, E, A, B, R(68) );
265 P( B, C, D, E, A, R(69) );
266 P( A, B, C, D, E, R(70) );
267 P( E, A, B, C, D, R(71) );
268 P( D, E, A, B, C, R(72) );
269 P( C, D, E, A, B, R(73) );
270 P( B, C, D, E, A, R(74) );
271 P( A, B, C, D, E, R(75) );
272 P( E, A, B, C, D, R(76) );
273 P( D, E, A, B, C, R(77) );
274 P( C, D, E, A, B, R(78) );
275 P( B, C, D, E, A, R(79) );
276
277#undef K
278#undef F
279
280 ctx->state[0] += A;
281 ctx->state[1] += B;
282 ctx->state[2] += C;
283 ctx->state[3] += D;
284 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100285
286 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287}
Jaeden Amero041039f2018-02-19 15:28:08 +0000288
289#if !defined(MBEDTLS_DEPRECATED_REMOVED)
290void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
291 const unsigned char data[64] )
292{
293 mbedtls_internal_sha1_process( ctx, data );
294}
295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298/*
299 * SHA-1 process buffer
300 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100301int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100302 const unsigned char *input,
303 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000304{
Janos Follath24eed8d2019-11-22 13:21:35 +0000305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000306 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000307 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
Hanno Becker039ccab2018-12-18 17:52:14 +0000309 SHA1_VALIDATE_RET( ctx != NULL );
310 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000311
Brian White12895d12014-04-11 11:29:42 -0400312 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100313 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 left = ctx->total[0] & 0x3F;
316 fill = 64 - left;
317
Paul Bakker5c2364c2012-10-01 14:41:15 +0000318 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 ctx->total[0] &= 0xFFFFFFFF;
320
Paul Bakker5c2364c2012-10-01 14:41:15 +0000321 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 ctx->total[1]++;
323
324 if( left && ilen >= fill )
325 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200326 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100327
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100328 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100329 return( ret );
330
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 input += fill;
332 ilen -= fill;
333 left = 0;
334 }
335
336 while( ilen >= 64 )
337 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100338 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100339 return( ret );
340
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 input += 64;
342 ilen -= 64;
343 }
344
345 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200346 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100347
348 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349}
350
Jaeden Amero041039f2018-02-19 15:28:08 +0000351#if !defined(MBEDTLS_DEPRECATED_REMOVED)
352void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
353 const unsigned char *input,
354 size_t ilen )
355{
356 mbedtls_sha1_update_ret( ctx, input, ilen );
357}
358#endif
359
Paul Bakker5121ce52009-01-03 21:22:43 +0000360/*
361 * SHA-1 final digest
362 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100363int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100364 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000365{
Janos Follath24eed8d2019-11-22 13:21:35 +0000366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200367 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000368 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
Hanno Becker039ccab2018-12-18 17:52:14 +0000370 SHA1_VALIDATE_RET( ctx != NULL );
371 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000372
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200373 /*
374 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
375 */
376 used = ctx->total[0] & 0x3F;
377
378 ctx->buffer[used++] = 0x80;
379
380 if( used <= 56 )
381 {
382 /* Enough room for padding + length in current block */
383 memset( ctx->buffer + used, 0, 56 - used );
384 }
385 else
386 {
387 /* We'll need an extra block */
388 memset( ctx->buffer + used, 0, 64 - used );
389
390 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
391 return( ret );
392
393 memset( ctx->buffer, 0, 56 );
394 }
395
396 /*
397 * Add message length
398 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 high = ( ctx->total[0] >> 29 )
400 | ( ctx->total[1] << 3 );
401 low = ( ctx->total[0] << 3 );
402
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200403 PUT_UINT32_BE( high, ctx->buffer, 56 );
404 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200406 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200409 /*
410 * Output final state
411 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000412 PUT_UINT32_BE( ctx->state[0], output, 0 );
413 PUT_UINT32_BE( ctx->state[1], output, 4 );
414 PUT_UINT32_BE( ctx->state[2], output, 8 );
415 PUT_UINT32_BE( ctx->state[3], output, 12 );
416 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100417
418 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419}
420
Jaeden Amero041039f2018-02-19 15:28:08 +0000421#if !defined(MBEDTLS_DEPRECATED_REMOVED)
422void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
423 unsigned char output[20] )
424{
425 mbedtls_sha1_finish_ret( ctx, output );
426}
427#endif
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200430
Paul Bakker5121ce52009-01-03 21:22:43 +0000431/*
432 * output = SHA-1( input buffer )
433 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100434int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100435 size_t ilen,
436 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Hanno Becker039ccab2018-12-18 17:52:14 +0000441 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
442 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100445
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100446 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100447 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100449 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100450 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100451
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100453 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100454
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100455exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100457
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100458 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459}
460
Jaeden Amero041039f2018-02-19 15:28:08 +0000461#if !defined(MBEDTLS_DEPRECATED_REMOVED)
462void mbedtls_sha1( const unsigned char *input,
463 size_t ilen,
464 unsigned char output[20] )
465{
466 mbedtls_sha1_ret( input, ilen, output );
467}
468#endif
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000471/*
472 * FIPS-180-1 test vectors
473 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000474static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000475{
476 { "abc" },
477 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
478 { "" }
479};
480
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100481static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000482{
483 3, 56, 1000
484};
485
486static const unsigned char sha1_test_sum[3][20] =
487{
488 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
489 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
490 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
491 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
492 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
493 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
494};
495
496/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 * Checkup routine
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000500{
Paul Bakker5b4af392014-06-26 12:09:34 +0200501 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 unsigned char buf[1024];
503 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200507
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 /*
509 * SHA-1
510 */
511 for( i = 0; i < 3; i++ )
512 {
513 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100516 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100517 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 if( i == 2 )
520 {
521 memset( buf, 'a', buflen = 1000 );
522
523 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100524 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100525 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100526 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100527 goto fail;
528 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
530 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100531 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100532 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100533 sha1_test_buflen[i] );
534 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100535 goto fail;
536 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100538 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100539 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100542 {
543 ret = 1;
544 goto fail;
545 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 }
550
551 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100554 goto exit;
555
556fail:
557 if( verbose != 0 )
558 mbedtls_printf( "failed\n" );
559
Paul Bakker5b4af392014-06-26 12:09:34 +0200560exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200562
563 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564}
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_SHA1_C */