blob: 92339434152fae5aa5752eacaa8a5927097a5046 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SHA-1 standard was published by NIST in 1993.
23 *
24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_SELF_TEST)
42#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010044#else
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define mbedtls_printf printf
47#endif /* MBEDTLS_PLATFORM_C */
48#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010049
Hanno Beckerb3c70232018-12-20 10:18:05 +000050#define SHA1_VALIDATE_RET(cond) \
51 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
52
53#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
54
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020055#if !defined(MBEDTLS_SHA1_ALT)
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/*
58 * 32-bit integer manipulation macros (big endian)
59 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000060#ifndef GET_UINT32_BE
61#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000062{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000063 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
64 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
65 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
66 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
68#endif
69
Paul Bakker5c2364c2012-10-01 14:41:15 +000070#ifndef PUT_UINT32_BE
71#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000072{ \
73 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
74 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
75 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
76 (b)[(i) + 3] = (unsigned char) ( (n) ); \
77}
78#endif
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020081{
Hanno Becker039ccab2018-12-18 17:52:14 +000082 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020088{
89 if( ctx == NULL )
90 return;
91
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050092 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020093}
94
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
96 const mbedtls_sha1_context *src )
97{
Hanno Becker039ccab2018-12-18 17:52:14 +000098 SHA1_VALIDATE( dst != NULL );
99 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000100
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200101 *dst = *src;
102}
103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/*
105 * SHA-1 context setup
106 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100107int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
Hanno Becker039ccab2018-12-18 17:52:14 +0000109 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 ctx->total[0] = 0;
112 ctx->total[1] = 0;
113
114 ctx->state[0] = 0x67452301;
115 ctx->state[1] = 0xEFCDAB89;
116 ctx->state[2] = 0x98BADCFE;
117 ctx->state[3] = 0x10325476;
118 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100119
120 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122
Jaeden Amero041039f2018-02-19 15:28:08 +0000123#if !defined(MBEDTLS_DEPRECATED_REMOVED)
124void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
125{
126 mbedtls_sha1_starts_ret( ctx );
127}
128#endif
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100131int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
132 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000134 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Hanno Becker039ccab2018-12-18 17:52:14 +0000136 SHA1_VALIDATE_RET( ctx != NULL );
137 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000138
Paul Bakker5c2364c2012-10-01 14:41:15 +0000139 GET_UINT32_BE( W[ 0], data, 0 );
140 GET_UINT32_BE( W[ 1], data, 4 );
141 GET_UINT32_BE( W[ 2], data, 8 );
142 GET_UINT32_BE( W[ 3], data, 12 );
143 GET_UINT32_BE( W[ 4], data, 16 );
144 GET_UINT32_BE( W[ 5], data, 20 );
145 GET_UINT32_BE( W[ 6], data, 24 );
146 GET_UINT32_BE( W[ 7], data, 28 );
147 GET_UINT32_BE( W[ 8], data, 32 );
148 GET_UINT32_BE( W[ 9], data, 36 );
149 GET_UINT32_BE( W[10], data, 40 );
150 GET_UINT32_BE( W[11], data, 44 );
151 GET_UINT32_BE( W[12], data, 48 );
152 GET_UINT32_BE( W[13], data, 52 );
153 GET_UINT32_BE( W[14], data, 56 );
154 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Hanno Becker1eeca412018-10-15 12:01:35 +0100156#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Hanno Becker1eeca412018-10-15 12:01:35 +0100158#define R(t) \
159 ( \
160 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
161 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
162 ( W[(t) & 0x0F] = S(temp,1) ) \
163 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Hanno Becker1eeca412018-10-15 12:01:35 +0100165#define P(a,b,c,d,e,x) \
166 do \
167 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100168 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
169 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100170 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172 A = ctx->state[0];
173 B = ctx->state[1];
174 C = ctx->state[2];
175 D = ctx->state[3];
176 E = ctx->state[4];
177
Hanno Becker1eeca412018-10-15 12:01:35 +0100178#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000179#define K 0x5A827999
180
181 P( A, B, C, D, E, W[0] );
182 P( E, A, B, C, D, W[1] );
183 P( D, E, A, B, C, W[2] );
184 P( C, D, E, A, B, W[3] );
185 P( B, C, D, E, A, W[4] );
186 P( A, B, C, D, E, W[5] );
187 P( E, A, B, C, D, W[6] );
188 P( D, E, A, B, C, W[7] );
189 P( C, D, E, A, B, W[8] );
190 P( B, C, D, E, A, W[9] );
191 P( A, B, C, D, E, W[10] );
192 P( E, A, B, C, D, W[11] );
193 P( D, E, A, B, C, W[12] );
194 P( C, D, E, A, B, W[13] );
195 P( B, C, D, E, A, W[14] );
196 P( A, B, C, D, E, W[15] );
197 P( E, A, B, C, D, R(16) );
198 P( D, E, A, B, C, R(17) );
199 P( C, D, E, A, B, R(18) );
200 P( B, C, D, E, A, R(19) );
201
202#undef K
203#undef F
204
Hanno Becker1eeca412018-10-15 12:01:35 +0100205#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000206#define K 0x6ED9EBA1
207
208 P( A, B, C, D, E, R(20) );
209 P( E, A, B, C, D, R(21) );
210 P( D, E, A, B, C, R(22) );
211 P( C, D, E, A, B, R(23) );
212 P( B, C, D, E, A, R(24) );
213 P( A, B, C, D, E, R(25) );
214 P( E, A, B, C, D, R(26) );
215 P( D, E, A, B, C, R(27) );
216 P( C, D, E, A, B, R(28) );
217 P( B, C, D, E, A, R(29) );
218 P( A, B, C, D, E, R(30) );
219 P( E, A, B, C, D, R(31) );
220 P( D, E, A, B, C, R(32) );
221 P( C, D, E, A, B, R(33) );
222 P( B, C, D, E, A, R(34) );
223 P( A, B, C, D, E, R(35) );
224 P( E, A, B, C, D, R(36) );
225 P( D, E, A, B, C, R(37) );
226 P( C, D, E, A, B, R(38) );
227 P( B, C, D, E, A, R(39) );
228
229#undef K
230#undef F
231
Hanno Becker1eeca412018-10-15 12:01:35 +0100232#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000233#define K 0x8F1BBCDC
234
235 P( A, B, C, D, E, R(40) );
236 P( E, A, B, C, D, R(41) );
237 P( D, E, A, B, C, R(42) );
238 P( C, D, E, A, B, R(43) );
239 P( B, C, D, E, A, R(44) );
240 P( A, B, C, D, E, R(45) );
241 P( E, A, B, C, D, R(46) );
242 P( D, E, A, B, C, R(47) );
243 P( C, D, E, A, B, R(48) );
244 P( B, C, D, E, A, R(49) );
245 P( A, B, C, D, E, R(50) );
246 P( E, A, B, C, D, R(51) );
247 P( D, E, A, B, C, R(52) );
248 P( C, D, E, A, B, R(53) );
249 P( B, C, D, E, A, R(54) );
250 P( A, B, C, D, E, R(55) );
251 P( E, A, B, C, D, R(56) );
252 P( D, E, A, B, C, R(57) );
253 P( C, D, E, A, B, R(58) );
254 P( B, C, D, E, A, R(59) );
255
256#undef K
257#undef F
258
Hanno Becker1eeca412018-10-15 12:01:35 +0100259#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000260#define K 0xCA62C1D6
261
262 P( A, B, C, D, E, R(60) );
263 P( E, A, B, C, D, R(61) );
264 P( D, E, A, B, C, R(62) );
265 P( C, D, E, A, B, R(63) );
266 P( B, C, D, E, A, R(64) );
267 P( A, B, C, D, E, R(65) );
268 P( E, A, B, C, D, R(66) );
269 P( D, E, A, B, C, R(67) );
270 P( C, D, E, A, B, R(68) );
271 P( B, C, D, E, A, R(69) );
272 P( A, B, C, D, E, R(70) );
273 P( E, A, B, C, D, R(71) );
274 P( D, E, A, B, C, R(72) );
275 P( C, D, E, A, B, R(73) );
276 P( B, C, D, E, A, R(74) );
277 P( A, B, C, D, E, R(75) );
278 P( E, A, B, C, D, R(76) );
279 P( D, E, A, B, C, R(77) );
280 P( C, D, E, A, B, R(78) );
281 P( B, C, D, E, A, R(79) );
282
283#undef K
284#undef F
285
286 ctx->state[0] += A;
287 ctx->state[1] += B;
288 ctx->state[2] += C;
289 ctx->state[3] += D;
290 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100291
292 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293}
Jaeden Amero041039f2018-02-19 15:28:08 +0000294
295#if !defined(MBEDTLS_DEPRECATED_REMOVED)
296void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
297 const unsigned char data[64] )
298{
299 mbedtls_internal_sha1_process( ctx, data );
300}
301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304/*
305 * SHA-1 process buffer
306 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100307int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100308 const unsigned char *input,
309 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000310{
Janos Follath24eed8d2019-11-22 13:21:35 +0000311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000312 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000313 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Hanno Becker039ccab2018-12-18 17:52:14 +0000315 SHA1_VALIDATE_RET( ctx != NULL );
316 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000317
Brian White12895d12014-04-11 11:29:42 -0400318 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100319 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 left = ctx->total[0] & 0x3F;
322 fill = 64 - left;
323
Paul Bakker5c2364c2012-10-01 14:41:15 +0000324 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 ctx->total[0] &= 0xFFFFFFFF;
326
Paul Bakker5c2364c2012-10-01 14:41:15 +0000327 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 ctx->total[1]++;
329
330 if( left && ilen >= fill )
331 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200332 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100333
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100334 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100335 return( ret );
336
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 input += fill;
338 ilen -= fill;
339 left = 0;
340 }
341
342 while( ilen >= 64 )
343 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100344 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100345 return( ret );
346
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 input += 64;
348 ilen -= 64;
349 }
350
351 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200352 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100353
354 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355}
356
Jaeden Amero041039f2018-02-19 15:28:08 +0000357#if !defined(MBEDTLS_DEPRECATED_REMOVED)
358void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
359 const unsigned char *input,
360 size_t ilen )
361{
362 mbedtls_sha1_update_ret( ctx, input, ilen );
363}
364#endif
365
Paul Bakker5121ce52009-01-03 21:22:43 +0000366/*
367 * SHA-1 final digest
368 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100369int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100370 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000371{
Janos Follath24eed8d2019-11-22 13:21:35 +0000372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200373 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000374 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
Hanno Becker039ccab2018-12-18 17:52:14 +0000376 SHA1_VALIDATE_RET( ctx != NULL );
377 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000378
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200379 /*
380 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
381 */
382 used = ctx->total[0] & 0x3F;
383
384 ctx->buffer[used++] = 0x80;
385
386 if( used <= 56 )
387 {
388 /* Enough room for padding + length in current block */
389 memset( ctx->buffer + used, 0, 56 - used );
390 }
391 else
392 {
393 /* We'll need an extra block */
394 memset( ctx->buffer + used, 0, 64 - used );
395
396 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
397 return( ret );
398
399 memset( ctx->buffer, 0, 56 );
400 }
401
402 /*
403 * Add message length
404 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 high = ( ctx->total[0] >> 29 )
406 | ( ctx->total[1] << 3 );
407 low = ( ctx->total[0] << 3 );
408
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200409 PUT_UINT32_BE( high, ctx->buffer, 56 );
410 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200412 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100413 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200415 /*
416 * Output final state
417 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000418 PUT_UINT32_BE( ctx->state[0], output, 0 );
419 PUT_UINT32_BE( ctx->state[1], output, 4 );
420 PUT_UINT32_BE( ctx->state[2], output, 8 );
421 PUT_UINT32_BE( ctx->state[3], output, 12 );
422 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100423
424 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425}
426
Jaeden Amero041039f2018-02-19 15:28:08 +0000427#if !defined(MBEDTLS_DEPRECATED_REMOVED)
428void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
429 unsigned char output[20] )
430{
431 mbedtls_sha1_finish_ret( ctx, output );
432}
433#endif
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200436
Paul Bakker5121ce52009-01-03 21:22:43 +0000437/*
438 * output = SHA-1( input buffer )
439 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100440int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100441 size_t ilen,
442 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000443{
Janos Follath24eed8d2019-11-22 13:21:35 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Hanno Becker039ccab2018-12-18 17:52:14 +0000447 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
448 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100451
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100453 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100456 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100457
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100458 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100459 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100460
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100461exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100463
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100464 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465}
466
Jaeden Amero041039f2018-02-19 15:28:08 +0000467#if !defined(MBEDTLS_DEPRECATED_REMOVED)
468void mbedtls_sha1( const unsigned char *input,
469 size_t ilen,
470 unsigned char output[20] )
471{
472 mbedtls_sha1_ret( input, ilen, output );
473}
474#endif
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000477/*
478 * FIPS-180-1 test vectors
479 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000480static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000481{
482 { "abc" },
483 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
484 { "" }
485};
486
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100487static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000488{
489 3, 56, 1000
490};
491
492static const unsigned char sha1_test_sum[3][20] =
493{
494 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
495 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
496 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
497 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
498 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
499 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
500};
501
502/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 * Checkup routine
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506{
Paul Bakker5b4af392014-06-26 12:09:34 +0200507 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 unsigned char buf[1024];
509 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200513
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 /*
515 * SHA-1
516 */
517 for( i = 0; i < 3; i++ )
518 {
519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100522 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100523 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( i == 2 )
526 {
527 memset( buf, 'a', buflen = 1000 );
528
529 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100530 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100531 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100532 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100533 goto fail;
534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 }
536 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100537 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100538 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100539 sha1_test_buflen[i] );
540 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100541 goto fail;
542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100544 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100545 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100548 {
549 ret = 1;
550 goto fail;
551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 }
556
557 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100560 goto exit;
561
562fail:
563 if( verbose != 0 )
564 mbedtls_printf( "failed\n" );
565
Paul Bakker5b4af392014-06-26 12:09:34 +0200566exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200568
569 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570}
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_SHA1_C */