blob: 79bac6b2442cc81e15dd7368c22f64e868c63e36 [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
288 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289}
Jaeden Amero041039f2018-02-19 15:28:08 +0000290
291#if !defined(MBEDTLS_DEPRECATED_REMOVED)
292void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
293 const unsigned char data[64] )
294{
295 mbedtls_internal_sha1_process( ctx, data );
296}
297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/*
301 * SHA-1 process buffer
302 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100303int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100304 const unsigned char *input,
305 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306{
Janos Follath24eed8d2019-11-22 13:21:35 +0000307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000308 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000309 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Hanno Becker039ccab2018-12-18 17:52:14 +0000311 SHA1_VALIDATE_RET( ctx != NULL );
312 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000313
Brian White12895d12014-04-11 11:29:42 -0400314 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100315 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 left = ctx->total[0] & 0x3F;
318 fill = 64 - left;
319
Paul Bakker5c2364c2012-10-01 14:41:15 +0000320 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 ctx->total[0] &= 0xFFFFFFFF;
322
Paul Bakker5c2364c2012-10-01 14:41:15 +0000323 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 ctx->total[1]++;
325
326 if( left && ilen >= fill )
327 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200328 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100329
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100330 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100331 return( ret );
332
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 input += fill;
334 ilen -= fill;
335 left = 0;
336 }
337
338 while( ilen >= 64 )
339 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100340 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100341 return( ret );
342
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 input += 64;
344 ilen -= 64;
345 }
346
347 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200348 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100349
350 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351}
352
Jaeden Amero041039f2018-02-19 15:28:08 +0000353#if !defined(MBEDTLS_DEPRECATED_REMOVED)
354void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
355 const unsigned char *input,
356 size_t ilen )
357{
358 mbedtls_sha1_update_ret( ctx, input, ilen );
359}
360#endif
361
Paul Bakker5121ce52009-01-03 21:22:43 +0000362/*
363 * SHA-1 final digest
364 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100365int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100366 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000367{
Janos Follath24eed8d2019-11-22 13:21:35 +0000368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200369 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000370 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Hanno Becker039ccab2018-12-18 17:52:14 +0000372 SHA1_VALIDATE_RET( ctx != NULL );
373 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000374
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200375 /*
376 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
377 */
378 used = ctx->total[0] & 0x3F;
379
380 ctx->buffer[used++] = 0x80;
381
382 if( used <= 56 )
383 {
384 /* Enough room for padding + length in current block */
385 memset( ctx->buffer + used, 0, 56 - used );
386 }
387 else
388 {
389 /* We'll need an extra block */
390 memset( ctx->buffer + used, 0, 64 - used );
391
392 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
393 return( ret );
394
395 memset( ctx->buffer, 0, 56 );
396 }
397
398 /*
399 * Add message length
400 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 high = ( ctx->total[0] >> 29 )
402 | ( ctx->total[1] << 3 );
403 low = ( ctx->total[0] << 3 );
404
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200405 PUT_UINT32_BE( high, ctx->buffer, 56 );
406 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200408 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200411 /*
412 * Output final state
413 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000414 PUT_UINT32_BE( ctx->state[0], output, 0 );
415 PUT_UINT32_BE( ctx->state[1], output, 4 );
416 PUT_UINT32_BE( ctx->state[2], output, 8 );
417 PUT_UINT32_BE( ctx->state[3], output, 12 );
418 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100419
420 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421}
422
Jaeden Amero041039f2018-02-19 15:28:08 +0000423#if !defined(MBEDTLS_DEPRECATED_REMOVED)
424void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
425 unsigned char output[20] )
426{
427 mbedtls_sha1_finish_ret( ctx, output );
428}
429#endif
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200432
Paul Bakker5121ce52009-01-03 21:22:43 +0000433/*
434 * output = SHA-1( input buffer )
435 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100436int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100437 size_t ilen,
438 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000439{
Janos Follath24eed8d2019-11-22 13:21:35 +0000440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
Hanno Becker039ccab2018-12-18 17:52:14 +0000443 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
444 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100447
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100448 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100449 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100450
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100451 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100452 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100455 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100456
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100457exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100459
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100460 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461}
462
Jaeden Amero041039f2018-02-19 15:28:08 +0000463#if !defined(MBEDTLS_DEPRECATED_REMOVED)
464void mbedtls_sha1( const unsigned char *input,
465 size_t ilen,
466 unsigned char output[20] )
467{
468 mbedtls_sha1_ret( input, ilen, output );
469}
470#endif
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000473/*
474 * FIPS-180-1 test vectors
475 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000476static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000477{
478 { "abc" },
479 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
480 { "" }
481};
482
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100483static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
485 3, 56, 1000
486};
487
488static const unsigned char sha1_test_sum[3][20] =
489{
490 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
491 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
492 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
493 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
494 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
495 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
496};
497
498/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 * Checkup routine
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000502{
Paul Bakker5b4af392014-06-26 12:09:34 +0200503 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 unsigned char buf[1024];
505 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 /*
511 * SHA-1
512 */
513 for( i = 0; i < 3; i++ )
514 {
515 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100518 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100519 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 if( i == 2 )
522 {
523 memset( buf, 'a', buflen = 1000 );
524
525 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100526 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100527 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100528 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100529 goto fail;
530 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 }
532 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100533 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100534 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100535 sha1_test_buflen[i] );
536 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100537 goto fail;
538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100540 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100541 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100544 {
545 ret = 1;
546 goto fail;
547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 }
552
553 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100556 goto exit;
557
558fail:
559 if( verbose != 0 )
560 mbedtls_printf( "failed\n" );
561
Paul Bakker5b4af392014-06-26 12:09:34 +0200562exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200564
565 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566}
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_SHA1_C */