blob: 83c984fe0ae46693b0f16f9fca748fb48e3eee49 [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 */
TRodziewicz26371e42021-06-08 16:45:41 +020071int mbedtls_sha1_starts( 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +010088int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
89 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +000090{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020091 struct
92 {
93 uint32_t temp, W[16], A, B, C, D, E;
94 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Hanno Becker039ccab2018-12-18 17:52:14 +000096 SHA1_VALIDATE_RET( ctx != NULL );
97 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000098
Joe Subbiani6a506312021-07-07 16:56:29 +010099 local.W[ 0] = MBEDTLS_GET_UINT32_BE( data, 0 );
100 local.W[ 1] = MBEDTLS_GET_UINT32_BE( data, 4 );
101 local.W[ 2] = MBEDTLS_GET_UINT32_BE( data, 8 );
102 local.W[ 3] = MBEDTLS_GET_UINT32_BE( data, 12 );
103 local.W[ 4] = MBEDTLS_GET_UINT32_BE( data, 16 );
104 local.W[ 5] = MBEDTLS_GET_UINT32_BE( data, 20 );
105 local.W[ 6] = MBEDTLS_GET_UINT32_BE( data, 24 );
106 local.W[ 7] = MBEDTLS_GET_UINT32_BE( data, 28 );
107 local.W[ 8] = MBEDTLS_GET_UINT32_BE( data, 32 );
108 local.W[ 9] = MBEDTLS_GET_UINT32_BE( data, 36 );
109 local.W[10] = MBEDTLS_GET_UINT32_BE( data, 40 );
110 local.W[11] = MBEDTLS_GET_UINT32_BE( data, 44 );
111 local.W[12] = MBEDTLS_GET_UINT32_BE( data, 48 );
112 local.W[13] = MBEDTLS_GET_UINT32_BE( data, 52 );
113 local.W[14] = MBEDTLS_GET_UINT32_BE( data, 56 );
114 local.W[15] = MBEDTLS_GET_UINT32_BE( data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Hanno Becker1eeca412018-10-15 12:01:35 +0100116#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Hanno Becker1eeca412018-10-15 12:01:35 +0100118#define R(t) \
119 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200120 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
121 local.W[( (t) - 8 ) & 0x0F] ^ \
122 local.W[( (t) - 14 ) & 0x0F] ^ \
123 local.W[ (t) & 0x0F], \
124 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100125 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Hanno Becker1eeca412018-10-15 12:01:35 +0100127#define P(a,b,c,d,e,x) \
128 do \
129 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100130 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
131 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100132 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200134 local.A = ctx->state[0];
135 local.B = ctx->state[1];
136 local.C = ctx->state[2];
137 local.D = ctx->state[3];
138 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Hanno Becker1eeca412018-10-15 12:01:35 +0100140#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000141#define K 0x5A827999
142
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200143 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
144 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
145 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
146 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
147 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
148 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
149 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
150 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
151 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
152 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
153 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
154 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
155 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
156 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
157 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
158 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
159 P( local.E, local.A, local.B, local.C, local.D, R(16) );
160 P( local.D, local.E, local.A, local.B, local.C, R(17) );
161 P( local.C, local.D, local.E, local.A, local.B, R(18) );
162 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164#undef K
165#undef F
166
Hanno Becker1eeca412018-10-15 12:01:35 +0100167#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000168#define K 0x6ED9EBA1
169
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200170 P( local.A, local.B, local.C, local.D, local.E, R(20) );
171 P( local.E, local.A, local.B, local.C, local.D, R(21) );
172 P( local.D, local.E, local.A, local.B, local.C, R(22) );
173 P( local.C, local.D, local.E, local.A, local.B, R(23) );
174 P( local.B, local.C, local.D, local.E, local.A, R(24) );
175 P( local.A, local.B, local.C, local.D, local.E, R(25) );
176 P( local.E, local.A, local.B, local.C, local.D, R(26) );
177 P( local.D, local.E, local.A, local.B, local.C, R(27) );
178 P( local.C, local.D, local.E, local.A, local.B, R(28) );
179 P( local.B, local.C, local.D, local.E, local.A, R(29) );
180 P( local.A, local.B, local.C, local.D, local.E, R(30) );
181 P( local.E, local.A, local.B, local.C, local.D, R(31) );
182 P( local.D, local.E, local.A, local.B, local.C, R(32) );
183 P( local.C, local.D, local.E, local.A, local.B, R(33) );
184 P( local.B, local.C, local.D, local.E, local.A, R(34) );
185 P( local.A, local.B, local.C, local.D, local.E, R(35) );
186 P( local.E, local.A, local.B, local.C, local.D, R(36) );
187 P( local.D, local.E, local.A, local.B, local.C, R(37) );
188 P( local.C, local.D, local.E, local.A, local.B, R(38) );
189 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191#undef K
192#undef F
193
Hanno Becker1eeca412018-10-15 12:01:35 +0100194#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000195#define K 0x8F1BBCDC
196
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200197 P( local.A, local.B, local.C, local.D, local.E, R(40) );
198 P( local.E, local.A, local.B, local.C, local.D, R(41) );
199 P( local.D, local.E, local.A, local.B, local.C, R(42) );
200 P( local.C, local.D, local.E, local.A, local.B, R(43) );
201 P( local.B, local.C, local.D, local.E, local.A, R(44) );
202 P( local.A, local.B, local.C, local.D, local.E, R(45) );
203 P( local.E, local.A, local.B, local.C, local.D, R(46) );
204 P( local.D, local.E, local.A, local.B, local.C, R(47) );
205 P( local.C, local.D, local.E, local.A, local.B, R(48) );
206 P( local.B, local.C, local.D, local.E, local.A, R(49) );
207 P( local.A, local.B, local.C, local.D, local.E, R(50) );
208 P( local.E, local.A, local.B, local.C, local.D, R(51) );
209 P( local.D, local.E, local.A, local.B, local.C, R(52) );
210 P( local.C, local.D, local.E, local.A, local.B, R(53) );
211 P( local.B, local.C, local.D, local.E, local.A, R(54) );
212 P( local.A, local.B, local.C, local.D, local.E, R(55) );
213 P( local.E, local.A, local.B, local.C, local.D, R(56) );
214 P( local.D, local.E, local.A, local.B, local.C, R(57) );
215 P( local.C, local.D, local.E, local.A, local.B, R(58) );
216 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218#undef K
219#undef F
220
Hanno Becker1eeca412018-10-15 12:01:35 +0100221#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000222#define K 0xCA62C1D6
223
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200224 P( local.A, local.B, local.C, local.D, local.E, R(60) );
225 P( local.E, local.A, local.B, local.C, local.D, R(61) );
226 P( local.D, local.E, local.A, local.B, local.C, R(62) );
227 P( local.C, local.D, local.E, local.A, local.B, R(63) );
228 P( local.B, local.C, local.D, local.E, local.A, R(64) );
229 P( local.A, local.B, local.C, local.D, local.E, R(65) );
230 P( local.E, local.A, local.B, local.C, local.D, R(66) );
231 P( local.D, local.E, local.A, local.B, local.C, R(67) );
232 P( local.C, local.D, local.E, local.A, local.B, R(68) );
233 P( local.B, local.C, local.D, local.E, local.A, R(69) );
234 P( local.A, local.B, local.C, local.D, local.E, R(70) );
235 P( local.E, local.A, local.B, local.C, local.D, R(71) );
236 P( local.D, local.E, local.A, local.B, local.C, R(72) );
237 P( local.C, local.D, local.E, local.A, local.B, R(73) );
238 P( local.B, local.C, local.D, local.E, local.A, R(74) );
239 P( local.A, local.B, local.C, local.D, local.E, R(75) );
240 P( local.E, local.A, local.B, local.C, local.D, R(76) );
241 P( local.D, local.E, local.A, local.B, local.C, R(77) );
242 P( local.C, local.D, local.E, local.A, local.B, R(78) );
243 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245#undef K
246#undef F
247
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200248 ctx->state[0] += local.A;
249 ctx->state[1] += local.B;
250 ctx->state[2] += local.C;
251 ctx->state[3] += local.D;
252 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100253
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200254 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200255 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100256
257 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258}
Jaeden Amero041039f2018-02-19 15:28:08 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262/*
263 * SHA-1 process buffer
264 */
TRodziewicz26371e42021-06-08 16:45:41 +0200265int mbedtls_sha1_update( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100266 const unsigned char *input,
267 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000268{
Janos Follath24eed8d2019-11-22 13:21:35 +0000269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000270 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000271 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Hanno Becker039ccab2018-12-18 17:52:14 +0000273 SHA1_VALIDATE_RET( ctx != NULL );
274 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000275
Brian White12895d12014-04-11 11:29:42 -0400276 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100277 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
279 left = ctx->total[0] & 0x3F;
280 fill = 64 - left;
281
Paul Bakker5c2364c2012-10-01 14:41:15 +0000282 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 ctx->total[0] &= 0xFFFFFFFF;
284
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 ctx->total[1]++;
287
288 if( left && ilen >= fill )
289 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200290 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100291
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100292 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100293 return( ret );
294
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 input += fill;
296 ilen -= fill;
297 left = 0;
298 }
299
300 while( ilen >= 64 )
301 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100302 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100303 return( ret );
304
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 input += 64;
306 ilen -= 64;
307 }
308
309 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200310 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100311
312 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313}
314
Paul Bakker5121ce52009-01-03 21:22:43 +0000315/*
316 * SHA-1 final digest
317 */
TRodziewicz26371e42021-06-08 16:45:41 +0200318int mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100319 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320{
Janos Follath24eed8d2019-11-22 13:21:35 +0000321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200322 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000323 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Hanno Becker039ccab2018-12-18 17:52:14 +0000325 SHA1_VALIDATE_RET( ctx != NULL );
326 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000327
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200328 /*
329 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
330 */
331 used = ctx->total[0] & 0x3F;
332
333 ctx->buffer[used++] = 0x80;
334
335 if( used <= 56 )
336 {
337 /* Enough room for padding + length in current block */
338 memset( ctx->buffer + used, 0, 56 - used );
339 }
340 else
341 {
342 /* We'll need an extra block */
343 memset( ctx->buffer + used, 0, 64 - used );
344
345 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
346 return( ret );
347
348 memset( ctx->buffer, 0, 56 );
349 }
350
351 /*
352 * Add message length
353 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 high = ( ctx->total[0] >> 29 )
355 | ( ctx->total[1] << 3 );
356 low = ( ctx->total[0] << 3 );
357
Joe Subbiani5ecac212021-06-24 13:00:03 +0100358 MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 );
359 MBEDTLS_PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200361 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100362 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200364 /*
365 * Output final state
366 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100367 MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 );
368 MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 );
369 MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 );
370 MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 );
371 MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100372
373 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374}
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200377
Paul Bakker5121ce52009-01-03 21:22:43 +0000378/*
379 * output = SHA-1( input buffer )
380 */
TRodziewicz26371e42021-06-08 16:45:41 +0200381int mbedtls_sha1( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100382 size_t ilen,
383 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000384{
Janos Follath24eed8d2019-11-22 13:21:35 +0000385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Hanno Becker039ccab2018-12-18 17:52:14 +0000388 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
389 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100392
TRodziewicz26371e42021-06-08 16:45:41 +0200393 if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100394 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100395
TRodziewicz26371e42021-06-08 16:45:41 +0200396 if( ( ret = mbedtls_sha1_update( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100397 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100398
TRodziewicz26371e42021-06-08 16:45:41 +0200399 if( ( ret = mbedtls_sha1_finish( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100400 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100401
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100402exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100404
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100405 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406}
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000409/*
410 * FIPS-180-1 test vectors
411 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000412static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000413{
414 { "abc" },
415 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
416 { "" }
417};
418
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100419static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000420{
421 3, 56, 1000
422};
423
424static const unsigned char sha1_test_sum[3][20] =
425{
426 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
427 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
428 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
429 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
430 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
431 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
432};
433
434/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 * Checkup routine
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000438{
Paul Bakker5b4af392014-06-26 12:09:34 +0200439 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 unsigned char buf[1024];
441 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200445
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 /*
447 * SHA-1
448 */
449 for( i = 0; i < 3; i++ )
450 {
451 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
TRodziewicz26371e42021-06-08 16:45:41 +0200454 if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100455 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 if( i == 2 )
458 {
459 memset( buf, 'a', buflen = 1000 );
460
461 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100462 {
TRodziewicz26371e42021-06-08 16:45:41 +0200463 ret = mbedtls_sha1_update( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100464 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465 goto fail;
466 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 }
468 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100469 {
TRodziewicz26371e42021-06-08 16:45:41 +0200470 ret = mbedtls_sha1_update( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100471 sha1_test_buflen[i] );
472 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100473 goto fail;
474 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
TRodziewicz26371e42021-06-08 16:45:41 +0200476 if( ( ret = mbedtls_sha1_finish( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100477 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100480 {
481 ret = 1;
482 goto fail;
483 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 }
488
489 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100492 goto exit;
493
494fail:
495 if( verbose != 0 )
496 mbedtls_printf( "failed\n" );
497
Paul Bakker5b4af392014-06-26 12:09:34 +0200498exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200500
501 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502}
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SHA1_C */