blob: c973455a72a3ec190b53a881bc5eba557b415622 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_SELF_TEST)
38#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#else
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_printf printf
43#endif /* MBEDTLS_PLATFORM_C */
44#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010045
Hanno Beckerb3c70232018-12-20 10:18:05 +000046#define SHA1_VALIDATE_RET(cond) \
47 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
48
49#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
50
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020051#if !defined(MBEDTLS_SHA1_ALT)
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/*
54 * 32-bit integer manipulation macros (big endian)
55 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000056#ifndef GET_UINT32_BE
57#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000058{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000059 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
60 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
61 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
62 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
64#endif
65
Paul Bakker5c2364c2012-10-01 14:41:15 +000066#ifndef PUT_UINT32_BE
67#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000068{ \
69 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
70 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
71 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
72 (b)[(i) + 3] = (unsigned char) ( (n) ); \
73}
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020077{
Hanno Becker039ccab2018-12-18 17:52:14 +000078 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020081}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020084{
85 if( ctx == NULL )
86 return;
87
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050088 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020089}
90
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020091void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
92 const mbedtls_sha1_context *src )
93{
Hanno Becker039ccab2018-12-18 17:52:14 +000094 SHA1_VALIDATE( dst != NULL );
95 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000096
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020097 *dst = *src;
98}
99
Paul Bakker5121ce52009-01-03 21:22:43 +0000100/*
101 * SHA-1 context setup
102 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100103int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Hanno Becker039ccab2018-12-18 17:52:14 +0000105 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 ctx->total[0] = 0;
108 ctx->total[1] = 0;
109
110 ctx->state[0] = 0x67452301;
111 ctx->state[1] = 0xEFCDAB89;
112 ctx->state[2] = 0x98BADCFE;
113 ctx->state[3] = 0x10325476;
114 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100115
116 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117}
118
Jaeden Amero041039f2018-02-19 15:28:08 +0000119#if !defined(MBEDTLS_DEPRECATED_REMOVED)
120void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
121{
122 mbedtls_sha1_starts_ret( ctx );
123}
124#endif
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100127int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
128 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000130 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Hanno Becker039ccab2018-12-18 17:52:14 +0000132 SHA1_VALIDATE_RET( ctx != NULL );
133 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000134
Paul Bakker5c2364c2012-10-01 14:41:15 +0000135 GET_UINT32_BE( W[ 0], data, 0 );
136 GET_UINT32_BE( W[ 1], data, 4 );
137 GET_UINT32_BE( W[ 2], data, 8 );
138 GET_UINT32_BE( W[ 3], data, 12 );
139 GET_UINT32_BE( W[ 4], data, 16 );
140 GET_UINT32_BE( W[ 5], data, 20 );
141 GET_UINT32_BE( W[ 6], data, 24 );
142 GET_UINT32_BE( W[ 7], data, 28 );
143 GET_UINT32_BE( W[ 8], data, 32 );
144 GET_UINT32_BE( W[ 9], data, 36 );
145 GET_UINT32_BE( W[10], data, 40 );
146 GET_UINT32_BE( W[11], data, 44 );
147 GET_UINT32_BE( W[12], data, 48 );
148 GET_UINT32_BE( W[13], data, 52 );
149 GET_UINT32_BE( W[14], data, 56 );
150 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Hanno Becker1eeca412018-10-15 12:01:35 +0100152#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Hanno Becker1eeca412018-10-15 12:01:35 +0100154#define R(t) \
155 ( \
156 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
157 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
158 ( W[(t) & 0x0F] = S(temp,1) ) \
159 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Hanno Becker1eeca412018-10-15 12:01:35 +0100161#define P(a,b,c,d,e,x) \
162 do \
163 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100164 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
165 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100166 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 A = ctx->state[0];
169 B = ctx->state[1];
170 C = ctx->state[2];
171 D = ctx->state[3];
172 E = ctx->state[4];
173
Hanno Becker1eeca412018-10-15 12:01:35 +0100174#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#define K 0x5A827999
176
177 P( A, B, C, D, E, W[0] );
178 P( E, A, B, C, D, W[1] );
179 P( D, E, A, B, C, W[2] );
180 P( C, D, E, A, B, W[3] );
181 P( B, C, D, E, A, W[4] );
182 P( A, B, C, D, E, W[5] );
183 P( E, A, B, C, D, W[6] );
184 P( D, E, A, B, C, W[7] );
185 P( C, D, E, A, B, W[8] );
186 P( B, C, D, E, A, W[9] );
187 P( A, B, C, D, E, W[10] );
188 P( E, A, B, C, D, W[11] );
189 P( D, E, A, B, C, W[12] );
190 P( C, D, E, A, B, W[13] );
191 P( B, C, D, E, A, W[14] );
192 P( A, B, C, D, E, W[15] );
193 P( E, A, B, C, D, R(16) );
194 P( D, E, A, B, C, R(17) );
195 P( C, D, E, A, B, R(18) );
196 P( B, C, D, E, A, R(19) );
197
198#undef K
199#undef F
200
Hanno Becker1eeca412018-10-15 12:01:35 +0100201#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000202#define K 0x6ED9EBA1
203
204 P( A, B, C, D, E, R(20) );
205 P( E, A, B, C, D, R(21) );
206 P( D, E, A, B, C, R(22) );
207 P( C, D, E, A, B, R(23) );
208 P( B, C, D, E, A, R(24) );
209 P( A, B, C, D, E, R(25) );
210 P( E, A, B, C, D, R(26) );
211 P( D, E, A, B, C, R(27) );
212 P( C, D, E, A, B, R(28) );
213 P( B, C, D, E, A, R(29) );
214 P( A, B, C, D, E, R(30) );
215 P( E, A, B, C, D, R(31) );
216 P( D, E, A, B, C, R(32) );
217 P( C, D, E, A, B, R(33) );
218 P( B, C, D, E, A, R(34) );
219 P( A, B, C, D, E, R(35) );
220 P( E, A, B, C, D, R(36) );
221 P( D, E, A, B, C, R(37) );
222 P( C, D, E, A, B, R(38) );
223 P( B, C, D, E, A, R(39) );
224
225#undef K
226#undef F
227
Hanno Becker1eeca412018-10-15 12:01:35 +0100228#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000229#define K 0x8F1BBCDC
230
231 P( A, B, C, D, E, R(40) );
232 P( E, A, B, C, D, R(41) );
233 P( D, E, A, B, C, R(42) );
234 P( C, D, E, A, B, R(43) );
235 P( B, C, D, E, A, R(44) );
236 P( A, B, C, D, E, R(45) );
237 P( E, A, B, C, D, R(46) );
238 P( D, E, A, B, C, R(47) );
239 P( C, D, E, A, B, R(48) );
240 P( B, C, D, E, A, R(49) );
241 P( A, B, C, D, E, R(50) );
242 P( E, A, B, C, D, R(51) );
243 P( D, E, A, B, C, R(52) );
244 P( C, D, E, A, B, R(53) );
245 P( B, C, D, E, A, R(54) );
246 P( A, B, C, D, E, R(55) );
247 P( E, A, B, C, D, R(56) );
248 P( D, E, A, B, C, R(57) );
249 P( C, D, E, A, B, R(58) );
250 P( B, C, D, E, A, R(59) );
251
252#undef K
253#undef F
254
Hanno Becker1eeca412018-10-15 12:01:35 +0100255#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000256#define K 0xCA62C1D6
257
258 P( A, B, C, D, E, R(60) );
259 P( E, A, B, C, D, R(61) );
260 P( D, E, A, B, C, R(62) );
261 P( C, D, E, A, B, R(63) );
262 P( B, C, D, E, A, R(64) );
263 P( A, B, C, D, E, R(65) );
264 P( E, A, B, C, D, R(66) );
265 P( D, E, A, B, C, R(67) );
266 P( C, D, E, A, B, R(68) );
267 P( B, C, D, E, A, R(69) );
268 P( A, B, C, D, E, R(70) );
269 P( E, A, B, C, D, R(71) );
270 P( D, E, A, B, C, R(72) );
271 P( C, D, E, A, B, R(73) );
272 P( B, C, D, E, A, R(74) );
273 P( A, B, C, D, E, R(75) );
274 P( E, A, B, C, D, R(76) );
275 P( D, E, A, B, C, R(77) );
276 P( C, D, E, A, B, R(78) );
277 P( B, C, D, E, A, R(79) );
278
279#undef K
280#undef F
281
282 ctx->state[0] += A;
283 ctx->state[1] += B;
284 ctx->state[2] += C;
285 ctx->state[3] += D;
286 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100287
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200288 /* Zeroise buffers and variables to clear sensitive data from memory. */
289 mbedtls_platform_zeroize( &A, sizeof( A ) );
290 mbedtls_platform_zeroize( &B, sizeof( B ) );
291 mbedtls_platform_zeroize( &C, sizeof( C ) );
292 mbedtls_platform_zeroize( &D, sizeof( D ) );
293 mbedtls_platform_zeroize( &E, sizeof( E ) );
294 mbedtls_platform_zeroize( &W, sizeof( W ) );
295 mbedtls_platform_zeroize( &temp, sizeof( temp ) );
296
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100297 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298}
Jaeden Amero041039f2018-02-19 15:28:08 +0000299
300#if !defined(MBEDTLS_DEPRECATED_REMOVED)
301void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
302 const unsigned char data[64] )
303{
304 mbedtls_internal_sha1_process( ctx, data );
305}
306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309/*
310 * SHA-1 process buffer
311 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100312int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100313 const unsigned char *input,
314 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000315{
Janos Follath24eed8d2019-11-22 13:21:35 +0000316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000317 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000318 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Hanno Becker039ccab2018-12-18 17:52:14 +0000320 SHA1_VALIDATE_RET( ctx != NULL );
321 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000322
Brian White12895d12014-04-11 11:29:42 -0400323 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100324 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 left = ctx->total[0] & 0x3F;
327 fill = 64 - left;
328
Paul Bakker5c2364c2012-10-01 14:41:15 +0000329 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 ctx->total[0] &= 0xFFFFFFFF;
331
Paul Bakker5c2364c2012-10-01 14:41:15 +0000332 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 ctx->total[1]++;
334
335 if( left && ilen >= fill )
336 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200337 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100338
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100339 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100340 return( ret );
341
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 input += fill;
343 ilen -= fill;
344 left = 0;
345 }
346
347 while( ilen >= 64 )
348 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100349 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100350 return( ret );
351
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 input += 64;
353 ilen -= 64;
354 }
355
356 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200357 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100358
359 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360}
361
Jaeden Amero041039f2018-02-19 15:28:08 +0000362#if !defined(MBEDTLS_DEPRECATED_REMOVED)
363void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
364 const unsigned char *input,
365 size_t ilen )
366{
367 mbedtls_sha1_update_ret( ctx, input, ilen );
368}
369#endif
370
Paul Bakker5121ce52009-01-03 21:22:43 +0000371/*
372 * SHA-1 final digest
373 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100375 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376{
Janos Follath24eed8d2019-11-22 13:21:35 +0000377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200378 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000379 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
Hanno Becker039ccab2018-12-18 17:52:14 +0000381 SHA1_VALIDATE_RET( ctx != NULL );
382 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000383
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200384 /*
385 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
386 */
387 used = ctx->total[0] & 0x3F;
388
389 ctx->buffer[used++] = 0x80;
390
391 if( used <= 56 )
392 {
393 /* Enough room for padding + length in current block */
394 memset( ctx->buffer + used, 0, 56 - used );
395 }
396 else
397 {
398 /* We'll need an extra block */
399 memset( ctx->buffer + used, 0, 64 - used );
400
401 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
402 return( ret );
403
404 memset( ctx->buffer, 0, 56 );
405 }
406
407 /*
408 * Add message length
409 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 high = ( ctx->total[0] >> 29 )
411 | ( ctx->total[1] << 3 );
412 low = ( ctx->total[0] << 3 );
413
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200414 PUT_UINT32_BE( high, ctx->buffer, 56 );
415 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200417 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200420 /*
421 * Output final state
422 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000423 PUT_UINT32_BE( ctx->state[0], output, 0 );
424 PUT_UINT32_BE( ctx->state[1], output, 4 );
425 PUT_UINT32_BE( ctx->state[2], output, 8 );
426 PUT_UINT32_BE( ctx->state[3], output, 12 );
427 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100428
429 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430}
431
Jaeden Amero041039f2018-02-19 15:28:08 +0000432#if !defined(MBEDTLS_DEPRECATED_REMOVED)
433void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
434 unsigned char output[20] )
435{
436 mbedtls_sha1_finish_ret( ctx, output );
437}
438#endif
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200441
Paul Bakker5121ce52009-01-03 21:22:43 +0000442/*
443 * output = SHA-1( input buffer )
444 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100445int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100446 size_t ilen,
447 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000448{
Janos Follath24eed8d2019-11-22 13:21:35 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Hanno Becker039ccab2018-12-18 17:52:14 +0000452 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
453 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100458 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100461 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100462
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100464 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100466exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100468
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100469 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470}
471
Jaeden Amero041039f2018-02-19 15:28:08 +0000472#if !defined(MBEDTLS_DEPRECATED_REMOVED)
473void mbedtls_sha1( const unsigned char *input,
474 size_t ilen,
475 unsigned char output[20] )
476{
477 mbedtls_sha1_ret( input, ilen, output );
478}
479#endif
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000482/*
483 * FIPS-180-1 test vectors
484 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000485static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000486{
487 { "abc" },
488 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
489 { "" }
490};
491
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100492static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000493{
494 3, 56, 1000
495};
496
497static const unsigned char sha1_test_sum[3][20] =
498{
499 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
500 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
501 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
502 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
503 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
504 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
505};
506
507/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 * Checkup routine
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000511{
Paul Bakker5b4af392014-06-26 12:09:34 +0200512 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 unsigned char buf[1024];
514 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200518
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 /*
520 * SHA-1
521 */
522 for( i = 0; i < 3; i++ )
523 {
524 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100527 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100528 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530 if( i == 2 )
531 {
532 memset( buf, 'a', buflen = 1000 );
533
534 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100535 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100536 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100537 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100538 goto fail;
539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 }
541 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100542 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100544 sha1_test_buflen[i] );
545 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100546 goto fail;
547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100550 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100553 {
554 ret = 1;
555 goto fail;
556 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 }
561
562 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100565 goto exit;
566
567fail:
568 if( verbose != 0 )
569 mbedtls_printf( "failed\n" );
570
Paul Bakker5b4af392014-06-26 12:09:34 +0200571exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200573
574 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575}
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_SHA1_C */