blob: 6b0f58e7b615b1d2698de751fc82638261fe5c7a [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_SELF_TEST)
36#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_printf printf
41#endif /* MBEDTLS_PLATFORM_C */
42#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010043
Hanno Beckerb3c70232018-12-20 10:18:05 +000044#define SHA1_VALIDATE_RET(cond) \
45 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
46
47#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
48
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020049#if !defined(MBEDTLS_SHA1_ALT)
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
52 * 32-bit integer manipulation macros (big endian)
53 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000054#ifndef GET_UINT32_BE
55#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000056{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
60 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
62#endif
63
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef PUT_UINT32_BE
65#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000066{ \
67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) ); \
71}
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020075{
Hanno Becker039ccab2018-12-18 17:52:14 +000076 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020079}
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020082{
83 if( ctx == NULL )
84 return;
85
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050086 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020087}
88
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020089void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
90 const mbedtls_sha1_context *src )
91{
Hanno Becker039ccab2018-12-18 17:52:14 +000092 SHA1_VALIDATE( dst != NULL );
93 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000094
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095 *dst = *src;
96}
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/*
99 * SHA-1 context setup
100 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100101int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
Hanno Becker039ccab2018-12-18 17:52:14 +0000103 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000104
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 ctx->total[0] = 0;
106 ctx->total[1] = 0;
107
108 ctx->state[0] = 0x67452301;
109 ctx->state[1] = 0xEFCDAB89;
110 ctx->state[2] = 0x98BADCFE;
111 ctx->state[3] = 0x10325476;
112 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100113
114 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115}
116
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200117#if !defined(MBEDTLS_DEPRECATED_REMOVED)
118void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
119{
120 mbedtls_sha1_starts_ret( ctx );
121}
122#endif
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100125int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
126 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200128 struct
129 {
130 uint32_t temp, W[16], A, B, C, D, E;
131 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Hanno Becker039ccab2018-12-18 17:52:14 +0000133 SHA1_VALIDATE_RET( ctx != NULL );
134 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000135
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200136 GET_UINT32_BE( local.W[ 0], data, 0 );
137 GET_UINT32_BE( local.W[ 1], data, 4 );
138 GET_UINT32_BE( local.W[ 2], data, 8 );
139 GET_UINT32_BE( local.W[ 3], data, 12 );
140 GET_UINT32_BE( local.W[ 4], data, 16 );
141 GET_UINT32_BE( local.W[ 5], data, 20 );
142 GET_UINT32_BE( local.W[ 6], data, 24 );
143 GET_UINT32_BE( local.W[ 7], data, 28 );
144 GET_UINT32_BE( local.W[ 8], data, 32 );
145 GET_UINT32_BE( local.W[ 9], data, 36 );
146 GET_UINT32_BE( local.W[10], data, 40 );
147 GET_UINT32_BE( local.W[11], data, 44 );
148 GET_UINT32_BE( local.W[12], data, 48 );
149 GET_UINT32_BE( local.W[13], data, 52 );
150 GET_UINT32_BE( local.W[14], data, 56 );
151 GET_UINT32_BE( local.W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Hanno Becker1eeca412018-10-15 12:01:35 +0100153#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Hanno Becker1eeca412018-10-15 12:01:35 +0100155#define R(t) \
156 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200157 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
158 local.W[( (t) - 8 ) & 0x0F] ^ \
159 local.W[( (t) - 14 ) & 0x0F] ^ \
160 local.W[ (t) & 0x0F], \
161 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100162 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Hanno Becker1eeca412018-10-15 12:01:35 +0100164#define P(a,b,c,d,e,x) \
165 do \
166 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100167 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
168 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100169 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200171 local.A = ctx->state[0];
172 local.B = ctx->state[1];
173 local.C = ctx->state[2];
174 local.D = ctx->state[3];
175 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Hanno Becker1eeca412018-10-15 12:01:35 +0100177#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000178#define K 0x5A827999
179
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200180 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
181 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
182 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
183 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
184 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
185 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
186 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
187 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
188 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
189 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
190 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
191 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
192 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
193 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
194 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
195 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
196 P( local.E, local.A, local.B, local.C, local.D, R(16) );
197 P( local.D, local.E, local.A, local.B, local.C, R(17) );
198 P( local.C, local.D, local.E, local.A, local.B, R(18) );
199 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201#undef K
202#undef F
203
Hanno Becker1eeca412018-10-15 12:01:35 +0100204#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000205#define K 0x6ED9EBA1
206
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200207 P( local.A, local.B, local.C, local.D, local.E, R(20) );
208 P( local.E, local.A, local.B, local.C, local.D, R(21) );
209 P( local.D, local.E, local.A, local.B, local.C, R(22) );
210 P( local.C, local.D, local.E, local.A, local.B, R(23) );
211 P( local.B, local.C, local.D, local.E, local.A, R(24) );
212 P( local.A, local.B, local.C, local.D, local.E, R(25) );
213 P( local.E, local.A, local.B, local.C, local.D, R(26) );
214 P( local.D, local.E, local.A, local.B, local.C, R(27) );
215 P( local.C, local.D, local.E, local.A, local.B, R(28) );
216 P( local.B, local.C, local.D, local.E, local.A, R(29) );
217 P( local.A, local.B, local.C, local.D, local.E, R(30) );
218 P( local.E, local.A, local.B, local.C, local.D, R(31) );
219 P( local.D, local.E, local.A, local.B, local.C, R(32) );
220 P( local.C, local.D, local.E, local.A, local.B, R(33) );
221 P( local.B, local.C, local.D, local.E, local.A, R(34) );
222 P( local.A, local.B, local.C, local.D, local.E, R(35) );
223 P( local.E, local.A, local.B, local.C, local.D, R(36) );
224 P( local.D, local.E, local.A, local.B, local.C, R(37) );
225 P( local.C, local.D, local.E, local.A, local.B, R(38) );
226 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228#undef K
229#undef F
230
Hanno Becker1eeca412018-10-15 12:01:35 +0100231#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000232#define K 0x8F1BBCDC
233
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200234 P( local.A, local.B, local.C, local.D, local.E, R(40) );
235 P( local.E, local.A, local.B, local.C, local.D, R(41) );
236 P( local.D, local.E, local.A, local.B, local.C, R(42) );
237 P( local.C, local.D, local.E, local.A, local.B, R(43) );
238 P( local.B, local.C, local.D, local.E, local.A, R(44) );
239 P( local.A, local.B, local.C, local.D, local.E, R(45) );
240 P( local.E, local.A, local.B, local.C, local.D, R(46) );
241 P( local.D, local.E, local.A, local.B, local.C, R(47) );
242 P( local.C, local.D, local.E, local.A, local.B, R(48) );
243 P( local.B, local.C, local.D, local.E, local.A, R(49) );
244 P( local.A, local.B, local.C, local.D, local.E, R(50) );
245 P( local.E, local.A, local.B, local.C, local.D, R(51) );
246 P( local.D, local.E, local.A, local.B, local.C, R(52) );
247 P( local.C, local.D, local.E, local.A, local.B, R(53) );
248 P( local.B, local.C, local.D, local.E, local.A, R(54) );
249 P( local.A, local.B, local.C, local.D, local.E, R(55) );
250 P( local.E, local.A, local.B, local.C, local.D, R(56) );
251 P( local.D, local.E, local.A, local.B, local.C, R(57) );
252 P( local.C, local.D, local.E, local.A, local.B, R(58) );
253 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255#undef K
256#undef F
257
Hanno Becker1eeca412018-10-15 12:01:35 +0100258#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000259#define K 0xCA62C1D6
260
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200261 P( local.A, local.B, local.C, local.D, local.E, R(60) );
262 P( local.E, local.A, local.B, local.C, local.D, R(61) );
263 P( local.D, local.E, local.A, local.B, local.C, R(62) );
264 P( local.C, local.D, local.E, local.A, local.B, R(63) );
265 P( local.B, local.C, local.D, local.E, local.A, R(64) );
266 P( local.A, local.B, local.C, local.D, local.E, R(65) );
267 P( local.E, local.A, local.B, local.C, local.D, R(66) );
268 P( local.D, local.E, local.A, local.B, local.C, R(67) );
269 P( local.C, local.D, local.E, local.A, local.B, R(68) );
270 P( local.B, local.C, local.D, local.E, local.A, R(69) );
271 P( local.A, local.B, local.C, local.D, local.E, R(70) );
272 P( local.E, local.A, local.B, local.C, local.D, R(71) );
273 P( local.D, local.E, local.A, local.B, local.C, R(72) );
274 P( local.C, local.D, local.E, local.A, local.B, R(73) );
275 P( local.B, local.C, local.D, local.E, local.A, R(74) );
276 P( local.A, local.B, local.C, local.D, local.E, R(75) );
277 P( local.E, local.A, local.B, local.C, local.D, R(76) );
278 P( local.D, local.E, local.A, local.B, local.C, R(77) );
279 P( local.C, local.D, local.E, local.A, local.B, R(78) );
280 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282#undef K
283#undef F
284
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200285 ctx->state[0] += local.A;
286 ctx->state[1] += local.B;
287 ctx->state[2] += local.C;
288 ctx->state[3] += local.D;
289 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100290
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200291 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200292 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100293
294 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295}
Jaeden Amero041039f2018-02-19 15:28:08 +0000296
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200297#if !defined(MBEDTLS_DEPRECATED_REMOVED)
298void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
299 const unsigned char data[64] )
300{
301 mbedtls_internal_sha1_process( ctx, data );
302}
303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306/*
307 * SHA-1 process buffer
308 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100309int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100310 const unsigned char *input,
311 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000312{
Janos Follath24eed8d2019-11-22 13:21:35 +0000313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000314 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000315 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Hanno Becker039ccab2018-12-18 17:52:14 +0000317 SHA1_VALIDATE_RET( ctx != NULL );
318 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000319
Brian White12895d12014-04-11 11:29:42 -0400320 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100321 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 left = ctx->total[0] & 0x3F;
324 fill = 64 - left;
325
Paul Bakker5c2364c2012-10-01 14:41:15 +0000326 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 ctx->total[0] &= 0xFFFFFFFF;
328
Paul Bakker5c2364c2012-10-01 14:41:15 +0000329 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 ctx->total[1]++;
331
332 if( left && ilen >= fill )
333 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200334 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100335
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100336 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100337 return( ret );
338
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 input += fill;
340 ilen -= fill;
341 left = 0;
342 }
343
344 while( ilen >= 64 )
345 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100346 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100347 return( ret );
348
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 input += 64;
350 ilen -= 64;
351 }
352
353 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200354 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100355
356 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357}
358
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200359#if !defined(MBEDTLS_DEPRECATED_REMOVED)
360void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
361 const unsigned char *input,
362 size_t ilen )
363{
364 mbedtls_sha1_update_ret( ctx, input, ilen );
365}
366#endif
367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368/*
369 * SHA-1 final digest
370 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100371int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100372 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000373{
Janos Follath24eed8d2019-11-22 13:21:35 +0000374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200375 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000376 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Hanno Becker039ccab2018-12-18 17:52:14 +0000378 SHA1_VALIDATE_RET( ctx != NULL );
379 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000380
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200381 /*
382 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
383 */
384 used = ctx->total[0] & 0x3F;
385
386 ctx->buffer[used++] = 0x80;
387
388 if( used <= 56 )
389 {
390 /* Enough room for padding + length in current block */
391 memset( ctx->buffer + used, 0, 56 - used );
392 }
393 else
394 {
395 /* We'll need an extra block */
396 memset( ctx->buffer + used, 0, 64 - used );
397
398 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
399 return( ret );
400
401 memset( ctx->buffer, 0, 56 );
402 }
403
404 /*
405 * Add message length
406 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 high = ( ctx->total[0] >> 29 )
408 | ( ctx->total[1] << 3 );
409 low = ( ctx->total[0] << 3 );
410
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200411 PUT_UINT32_BE( high, ctx->buffer, 56 );
412 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200414 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100415 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200417 /*
418 * Output final state
419 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000420 PUT_UINT32_BE( ctx->state[0], output, 0 );
421 PUT_UINT32_BE( ctx->state[1], output, 4 );
422 PUT_UINT32_BE( ctx->state[2], output, 8 );
423 PUT_UINT32_BE( ctx->state[3], output, 12 );
424 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100425
426 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427}
428
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200429#if !defined(MBEDTLS_DEPRECATED_REMOVED)
430void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
431 unsigned char output[20] )
432{
433 mbedtls_sha1_finish_ret( ctx, output );
434}
435#endif
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439/*
440 * output = SHA-1( input buffer )
441 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100442int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100443 size_t ilen,
444 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000445{
Janos Follath24eed8d2019-11-22 13:21:35 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Hanno Becker039ccab2018-12-18 17:52:14 +0000449 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
450 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100455 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 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_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100461 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100462
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100466 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467}
468
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200469#if !defined(MBEDTLS_DEPRECATED_REMOVED)
470void mbedtls_sha1( const unsigned char *input,
471 size_t ilen,
472 unsigned char output[20] )
473{
474 mbedtls_sha1_ret( input, ilen, output );
475}
476#endif
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000479/*
480 * FIPS-180-1 test vectors
481 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000482static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
484 { "abc" },
485 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
486 { "" }
487};
488
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100489static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000490{
491 3, 56, 1000
492};
493
494static const unsigned char sha1_test_sum[3][20] =
495{
496 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
497 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
498 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
499 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
500 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
501 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
502};
503
504/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * Checkup routine
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508{
Paul Bakker5b4af392014-06-26 12:09:34 +0200509 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 unsigned char buf[1024];
511 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200515
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 /*
517 * SHA-1
518 */
519 for( i = 0; i < 3; i++ )
520 {
521 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100524 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100525 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 if( i == 2 )
528 {
529 memset( buf, 'a', buflen = 1000 );
530
531 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100532 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100533 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100534 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100535 goto fail;
536 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 }
538 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100539 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100540 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100541 sha1_test_buflen[i] );
542 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100543 goto fail;
544 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100546 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100547 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100550 {
551 ret = 1;
552 goto fail;
553 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100562 goto exit;
563
564fail:
565 if( verbose != 0 )
566 mbedtls_printf( "failed\n" );
567
Paul Bakker5b4af392014-06-26 12:09:34 +0200568exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200570
571 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572}
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_SHA1_C */