blob: 7f0c8757dbe2612613e22cbfe930f148a4d6fdb0 [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é-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010036
Hanno Beckerb3c70232018-12-20 10:18:05 +000037#define SHA1_VALIDATE_RET(cond) \
38 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
39
40#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
41
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020042#if !defined(MBEDTLS_SHA1_ALT)
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020045{
Hanno Becker039ccab2018-12-18 17:52:14 +000046 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020049}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020052{
53 if( ctx == NULL )
54 return;
55
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050056 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020057}
58
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020059void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
60 const mbedtls_sha1_context *src )
61{
Hanno Becker039ccab2018-12-18 17:52:14 +000062 SHA1_VALIDATE( dst != NULL );
63 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000064
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020065 *dst = *src;
66}
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * SHA-1 context setup
70 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010071int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
Hanno Becker039ccab2018-12-18 17:52:14 +000073 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000074
Paul Bakker5121ce52009-01-03 21:22:43 +000075 ctx->total[0] = 0;
76 ctx->total[1] = 0;
77
78 ctx->state[0] = 0x67452301;
79 ctx->state[1] = 0xEFCDAB89;
80 ctx->state[2] = 0x98BADCFE;
81 ctx->state[3] = 0x10325476;
82 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010083
84 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000085}
86
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020087#if !defined(MBEDTLS_DEPRECATED_REMOVED)
88void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
89{
90 mbedtls_sha1_starts_ret( ctx );
91}
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +010095int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
96 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020098 struct
99 {
100 uint32_t temp, W[16], A, B, C, D, E;
101 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Hanno Becker039ccab2018-12-18 17:52:14 +0000103 SHA1_VALIDATE_RET( ctx != NULL );
104 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000105
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100106 local.W[ 0] = MBEDTLS_GET_UINT32_BE( data, 0 );
107 local.W[ 1] = MBEDTLS_GET_UINT32_BE( data, 4 );
108 local.W[ 2] = MBEDTLS_GET_UINT32_BE( data, 8 );
109 local.W[ 3] = MBEDTLS_GET_UINT32_BE( data, 12 );
110 local.W[ 4] = MBEDTLS_GET_UINT32_BE( data, 16 );
111 local.W[ 5] = MBEDTLS_GET_UINT32_BE( data, 20 );
112 local.W[ 6] = MBEDTLS_GET_UINT32_BE( data, 24 );
113 local.W[ 7] = MBEDTLS_GET_UINT32_BE( data, 28 );
114 local.W[ 8] = MBEDTLS_GET_UINT32_BE( data, 32 );
115 local.W[ 9] = MBEDTLS_GET_UINT32_BE( data, 36 );
116 local.W[10] = MBEDTLS_GET_UINT32_BE( data, 40 );
117 local.W[11] = MBEDTLS_GET_UINT32_BE( data, 44 );
118 local.W[12] = MBEDTLS_GET_UINT32_BE( data, 48 );
119 local.W[13] = MBEDTLS_GET_UINT32_BE( data, 52 );
120 local.W[14] = MBEDTLS_GET_UINT32_BE( data, 56 );
121 local.W[15] = MBEDTLS_GET_UINT32_BE( data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Hanno Becker1eeca412018-10-15 12:01:35 +0100123#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Hanno Becker1eeca412018-10-15 12:01:35 +0100125#define R(t) \
126 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200127 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
128 local.W[( (t) - 8 ) & 0x0F] ^ \
129 local.W[( (t) - 14 ) & 0x0F] ^ \
130 local.W[ (t) & 0x0F], \
131 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100132 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Hanno Becker1eeca412018-10-15 12:01:35 +0100134#define P(a,b,c,d,e,x) \
135 do \
136 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100137 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
138 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100139 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200141 local.A = ctx->state[0];
142 local.B = ctx->state[1];
143 local.C = ctx->state[2];
144 local.D = ctx->state[3];
145 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Hanno Becker1eeca412018-10-15 12:01:35 +0100147#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000148#define K 0x5A827999
149
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200150 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
151 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
152 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
153 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
154 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
155 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
156 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
157 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
158 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
159 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
160 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
161 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
162 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
163 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
164 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
165 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
166 P( local.E, local.A, local.B, local.C, local.D, R(16) );
167 P( local.D, local.E, local.A, local.B, local.C, R(17) );
168 P( local.C, local.D, local.E, local.A, local.B, R(18) );
169 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171#undef K
172#undef F
173
Hanno Becker1eeca412018-10-15 12:01:35 +0100174#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#define K 0x6ED9EBA1
176
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200177 P( local.A, local.B, local.C, local.D, local.E, R(20) );
178 P( local.E, local.A, local.B, local.C, local.D, R(21) );
179 P( local.D, local.E, local.A, local.B, local.C, R(22) );
180 P( local.C, local.D, local.E, local.A, local.B, R(23) );
181 P( local.B, local.C, local.D, local.E, local.A, R(24) );
182 P( local.A, local.B, local.C, local.D, local.E, R(25) );
183 P( local.E, local.A, local.B, local.C, local.D, R(26) );
184 P( local.D, local.E, local.A, local.B, local.C, R(27) );
185 P( local.C, local.D, local.E, local.A, local.B, R(28) );
186 P( local.B, local.C, local.D, local.E, local.A, R(29) );
187 P( local.A, local.B, local.C, local.D, local.E, R(30) );
188 P( local.E, local.A, local.B, local.C, local.D, R(31) );
189 P( local.D, local.E, local.A, local.B, local.C, R(32) );
190 P( local.C, local.D, local.E, local.A, local.B, R(33) );
191 P( local.B, local.C, local.D, local.E, local.A, R(34) );
192 P( local.A, local.B, local.C, local.D, local.E, R(35) );
193 P( local.E, local.A, local.B, local.C, local.D, R(36) );
194 P( local.D, local.E, local.A, local.B, local.C, R(37) );
195 P( local.C, local.D, local.E, local.A, local.B, R(38) );
196 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198#undef K
199#undef F
200
Hanno Becker1eeca412018-10-15 12:01:35 +0100201#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000202#define K 0x8F1BBCDC
203
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200204 P( local.A, local.B, local.C, local.D, local.E, R(40) );
205 P( local.E, local.A, local.B, local.C, local.D, R(41) );
206 P( local.D, local.E, local.A, local.B, local.C, R(42) );
207 P( local.C, local.D, local.E, local.A, local.B, R(43) );
208 P( local.B, local.C, local.D, local.E, local.A, R(44) );
209 P( local.A, local.B, local.C, local.D, local.E, R(45) );
210 P( local.E, local.A, local.B, local.C, local.D, R(46) );
211 P( local.D, local.E, local.A, local.B, local.C, R(47) );
212 P( local.C, local.D, local.E, local.A, local.B, R(48) );
213 P( local.B, local.C, local.D, local.E, local.A, R(49) );
214 P( local.A, local.B, local.C, local.D, local.E, R(50) );
215 P( local.E, local.A, local.B, local.C, local.D, R(51) );
216 P( local.D, local.E, local.A, local.B, local.C, R(52) );
217 P( local.C, local.D, local.E, local.A, local.B, R(53) );
218 P( local.B, local.C, local.D, local.E, local.A, R(54) );
219 P( local.A, local.B, local.C, local.D, local.E, R(55) );
220 P( local.E, local.A, local.B, local.C, local.D, R(56) );
221 P( local.D, local.E, local.A, local.B, local.C, R(57) );
222 P( local.C, local.D, local.E, local.A, local.B, R(58) );
223 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225#undef K
226#undef F
227
Hanno Becker1eeca412018-10-15 12:01:35 +0100228#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000229#define K 0xCA62C1D6
230
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200231 P( local.A, local.B, local.C, local.D, local.E, R(60) );
232 P( local.E, local.A, local.B, local.C, local.D, R(61) );
233 P( local.D, local.E, local.A, local.B, local.C, R(62) );
234 P( local.C, local.D, local.E, local.A, local.B, R(63) );
235 P( local.B, local.C, local.D, local.E, local.A, R(64) );
236 P( local.A, local.B, local.C, local.D, local.E, R(65) );
237 P( local.E, local.A, local.B, local.C, local.D, R(66) );
238 P( local.D, local.E, local.A, local.B, local.C, R(67) );
239 P( local.C, local.D, local.E, local.A, local.B, R(68) );
240 P( local.B, local.C, local.D, local.E, local.A, R(69) );
241 P( local.A, local.B, local.C, local.D, local.E, R(70) );
242 P( local.E, local.A, local.B, local.C, local.D, R(71) );
243 P( local.D, local.E, local.A, local.B, local.C, R(72) );
244 P( local.C, local.D, local.E, local.A, local.B, R(73) );
245 P( local.B, local.C, local.D, local.E, local.A, R(74) );
246 P( local.A, local.B, local.C, local.D, local.E, R(75) );
247 P( local.E, local.A, local.B, local.C, local.D, R(76) );
248 P( local.D, local.E, local.A, local.B, local.C, R(77) );
249 P( local.C, local.D, local.E, local.A, local.B, R(78) );
250 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252#undef K
253#undef F
254
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200255 ctx->state[0] += local.A;
256 ctx->state[1] += local.B;
257 ctx->state[2] += local.C;
258 ctx->state[3] += local.D;
259 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100260
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200261 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200262 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100263
264 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265}
Jaeden Amero041039f2018-02-19 15:28:08 +0000266
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200267#if !defined(MBEDTLS_DEPRECATED_REMOVED)
268void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
269 const unsigned char data[64] )
270{
271 mbedtls_internal_sha1_process( ctx, data );
272}
273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276/*
277 * SHA-1 process buffer
278 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100279int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100280 const unsigned char *input,
281 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000282{
Janos Follath24eed8d2019-11-22 13:21:35 +0000283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000284 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Hanno Becker039ccab2018-12-18 17:52:14 +0000287 SHA1_VALIDATE_RET( ctx != NULL );
288 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000289
Brian White12895d12014-04-11 11:29:42 -0400290 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100291 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293 left = ctx->total[0] & 0x3F;
294 fill = 64 - left;
295
Paul Bakker5c2364c2012-10-01 14:41:15 +0000296 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 ctx->total[0] &= 0xFFFFFFFF;
298
Paul Bakker5c2364c2012-10-01 14:41:15 +0000299 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 ctx->total[1]++;
301
302 if( left && ilen >= fill )
303 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200304 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100305
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100306 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100307 return( ret );
308
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 input += fill;
310 ilen -= fill;
311 left = 0;
312 }
313
314 while( ilen >= 64 )
315 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100316 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100317 return( ret );
318
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 input += 64;
320 ilen -= 64;
321 }
322
323 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200324 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100325
326 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327}
328
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200329#if !defined(MBEDTLS_DEPRECATED_REMOVED)
330void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
331 const unsigned char *input,
332 size_t ilen )
333{
334 mbedtls_sha1_update_ret( ctx, input, ilen );
335}
336#endif
337
Paul Bakker5121ce52009-01-03 21:22:43 +0000338/*
339 * SHA-1 final digest
340 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100342 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000343{
Janos Follath24eed8d2019-11-22 13:21:35 +0000344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200345 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000346 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Hanno Becker039ccab2018-12-18 17:52:14 +0000348 SHA1_VALIDATE_RET( ctx != NULL );
349 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000350
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200351 /*
352 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
353 */
354 used = ctx->total[0] & 0x3F;
355
356 ctx->buffer[used++] = 0x80;
357
358 if( used <= 56 )
359 {
360 /* Enough room for padding + length in current block */
361 memset( ctx->buffer + used, 0, 56 - used );
362 }
363 else
364 {
365 /* We'll need an extra block */
366 memset( ctx->buffer + used, 0, 64 - used );
367
368 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
369 return( ret );
370
371 memset( ctx->buffer, 0, 56 );
372 }
373
374 /*
375 * Add message length
376 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 high = ( ctx->total[0] >> 29 )
378 | ( ctx->total[1] << 3 );
379 low = ( ctx->total[0] << 3 );
380
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100381 MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 );
382 MBEDTLS_PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200384 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100385 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200387 /*
388 * Output final state
389 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100390 MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 );
391 MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 );
392 MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 );
393 MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 );
394 MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100395
396 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397}
398
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200399#if !defined(MBEDTLS_DEPRECATED_REMOVED)
400void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
401 unsigned char output[20] )
402{
403 mbedtls_sha1_finish_ret( ctx, output );
404}
405#endif
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409/*
410 * output = SHA-1( input buffer )
411 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100412int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100413 size_t ilen,
414 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000415{
Janos Follath24eed8d2019-11-22 13:21:35 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
Hanno Becker039ccab2018-12-18 17:52:14 +0000419 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
420 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100424 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100425 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100427 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100428 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100430 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100431 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100432
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100433exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100435
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437}
438
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200439#if !defined(MBEDTLS_DEPRECATED_REMOVED)
440void mbedtls_sha1( const unsigned char *input,
441 size_t ilen,
442 unsigned char output[20] )
443{
444 mbedtls_sha1_ret( input, ilen, output );
445}
446#endif
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000449/*
450 * FIPS-180-1 test vectors
451 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000452static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
454 { "abc" },
455 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
456 { "" }
457};
458
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100459static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000460{
461 3, 56, 1000
462};
463
464static const unsigned char sha1_test_sum[3][20] =
465{
466 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
467 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
468 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
469 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
470 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
471 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
472};
473
474/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 * Checkup routine
476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000478{
Paul Bakker5b4af392014-06-26 12:09:34 +0200479 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 unsigned char buf[1024];
481 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200485
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 /*
487 * SHA-1
488 */
489 for( i = 0; i < 3; i++ )
490 {
491 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100495 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497 if( i == 2 )
498 {
499 memset( buf, 'a', buflen = 1000 );
500
501 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100502 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100503 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100504 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100505 goto fail;
506 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 }
508 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100509 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100510 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100511 sha1_test_buflen[i] );
512 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100513 goto fail;
514 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100516 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100517 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100520 {
521 ret = 1;
522 goto fail;
523 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 }
528
529 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100532 goto exit;
533
534fail:
535 if( verbose != 0 )
536 mbedtls_printf( "failed\n" );
537
Paul Bakker5b4af392014-06-26 12:09:34 +0200538exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200540
541 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542}
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_SHA1_C */