blob: 545d093109fd35d1d5e7d6ccea65061567d3c4c5 [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 */
TRodziewicz26371e42021-06-08 16:45:41 +0200101int mbedtls_sha1_starts( 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100118int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
119 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200121 struct
122 {
123 uint32_t temp, W[16], A, B, C, D, E;
124 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Hanno Becker039ccab2018-12-18 17:52:14 +0000126 SHA1_VALIDATE_RET( ctx != NULL );
127 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000128
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200129 GET_UINT32_BE( local.W[ 0], data, 0 );
130 GET_UINT32_BE( local.W[ 1], data, 4 );
131 GET_UINT32_BE( local.W[ 2], data, 8 );
132 GET_UINT32_BE( local.W[ 3], data, 12 );
133 GET_UINT32_BE( local.W[ 4], data, 16 );
134 GET_UINT32_BE( local.W[ 5], data, 20 );
135 GET_UINT32_BE( local.W[ 6], data, 24 );
136 GET_UINT32_BE( local.W[ 7], data, 28 );
137 GET_UINT32_BE( local.W[ 8], data, 32 );
138 GET_UINT32_BE( local.W[ 9], data, 36 );
139 GET_UINT32_BE( local.W[10], data, 40 );
140 GET_UINT32_BE( local.W[11], data, 44 );
141 GET_UINT32_BE( local.W[12], data, 48 );
142 GET_UINT32_BE( local.W[13], data, 52 );
143 GET_UINT32_BE( local.W[14], data, 56 );
144 GET_UINT32_BE( local.W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Hanno Becker1eeca412018-10-15 12:01:35 +0100146#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Hanno Becker1eeca412018-10-15 12:01:35 +0100148#define R(t) \
149 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200150 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
151 local.W[( (t) - 8 ) & 0x0F] ^ \
152 local.W[( (t) - 14 ) & 0x0F] ^ \
153 local.W[ (t) & 0x0F], \
154 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100155 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Hanno Becker1eeca412018-10-15 12:01:35 +0100157#define P(a,b,c,d,e,x) \
158 do \
159 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100160 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
161 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100162 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200164 local.A = ctx->state[0];
165 local.B = ctx->state[1];
166 local.C = ctx->state[2];
167 local.D = ctx->state[3];
168 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Hanno Becker1eeca412018-10-15 12:01:35 +0100170#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000171#define K 0x5A827999
172
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200173 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
174 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
175 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
176 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
177 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
178 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
179 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
180 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
181 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
182 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
183 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
184 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
185 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
186 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
187 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
188 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
189 P( local.E, local.A, local.B, local.C, local.D, R(16) );
190 P( local.D, local.E, local.A, local.B, local.C, R(17) );
191 P( local.C, local.D, local.E, local.A, local.B, R(18) );
192 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194#undef K
195#undef F
196
Hanno Becker1eeca412018-10-15 12:01:35 +0100197#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000198#define K 0x6ED9EBA1
199
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200200 P( local.A, local.B, local.C, local.D, local.E, R(20) );
201 P( local.E, local.A, local.B, local.C, local.D, R(21) );
202 P( local.D, local.E, local.A, local.B, local.C, R(22) );
203 P( local.C, local.D, local.E, local.A, local.B, R(23) );
204 P( local.B, local.C, local.D, local.E, local.A, R(24) );
205 P( local.A, local.B, local.C, local.D, local.E, R(25) );
206 P( local.E, local.A, local.B, local.C, local.D, R(26) );
207 P( local.D, local.E, local.A, local.B, local.C, R(27) );
208 P( local.C, local.D, local.E, local.A, local.B, R(28) );
209 P( local.B, local.C, local.D, local.E, local.A, R(29) );
210 P( local.A, local.B, local.C, local.D, local.E, R(30) );
211 P( local.E, local.A, local.B, local.C, local.D, R(31) );
212 P( local.D, local.E, local.A, local.B, local.C, R(32) );
213 P( local.C, local.D, local.E, local.A, local.B, R(33) );
214 P( local.B, local.C, local.D, local.E, local.A, R(34) );
215 P( local.A, local.B, local.C, local.D, local.E, R(35) );
216 P( local.E, local.A, local.B, local.C, local.D, R(36) );
217 P( local.D, local.E, local.A, local.B, local.C, R(37) );
218 P( local.C, local.D, local.E, local.A, local.B, R(38) );
219 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221#undef K
222#undef F
223
Hanno Becker1eeca412018-10-15 12:01:35 +0100224#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000225#define K 0x8F1BBCDC
226
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200227 P( local.A, local.B, local.C, local.D, local.E, R(40) );
228 P( local.E, local.A, local.B, local.C, local.D, R(41) );
229 P( local.D, local.E, local.A, local.B, local.C, R(42) );
230 P( local.C, local.D, local.E, local.A, local.B, R(43) );
231 P( local.B, local.C, local.D, local.E, local.A, R(44) );
232 P( local.A, local.B, local.C, local.D, local.E, R(45) );
233 P( local.E, local.A, local.B, local.C, local.D, R(46) );
234 P( local.D, local.E, local.A, local.B, local.C, R(47) );
235 P( local.C, local.D, local.E, local.A, local.B, R(48) );
236 P( local.B, local.C, local.D, local.E, local.A, R(49) );
237 P( local.A, local.B, local.C, local.D, local.E, R(50) );
238 P( local.E, local.A, local.B, local.C, local.D, R(51) );
239 P( local.D, local.E, local.A, local.B, local.C, R(52) );
240 P( local.C, local.D, local.E, local.A, local.B, R(53) );
241 P( local.B, local.C, local.D, local.E, local.A, R(54) );
242 P( local.A, local.B, local.C, local.D, local.E, R(55) );
243 P( local.E, local.A, local.B, local.C, local.D, R(56) );
244 P( local.D, local.E, local.A, local.B, local.C, R(57) );
245 P( local.C, local.D, local.E, local.A, local.B, R(58) );
246 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248#undef K
249#undef F
250
Hanno Becker1eeca412018-10-15 12:01:35 +0100251#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000252#define K 0xCA62C1D6
253
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200254 P( local.A, local.B, local.C, local.D, local.E, R(60) );
255 P( local.E, local.A, local.B, local.C, local.D, R(61) );
256 P( local.D, local.E, local.A, local.B, local.C, R(62) );
257 P( local.C, local.D, local.E, local.A, local.B, R(63) );
258 P( local.B, local.C, local.D, local.E, local.A, R(64) );
259 P( local.A, local.B, local.C, local.D, local.E, R(65) );
260 P( local.E, local.A, local.B, local.C, local.D, R(66) );
261 P( local.D, local.E, local.A, local.B, local.C, R(67) );
262 P( local.C, local.D, local.E, local.A, local.B, R(68) );
263 P( local.B, local.C, local.D, local.E, local.A, R(69) );
264 P( local.A, local.B, local.C, local.D, local.E, R(70) );
265 P( local.E, local.A, local.B, local.C, local.D, R(71) );
266 P( local.D, local.E, local.A, local.B, local.C, R(72) );
267 P( local.C, local.D, local.E, local.A, local.B, R(73) );
268 P( local.B, local.C, local.D, local.E, local.A, R(74) );
269 P( local.A, local.B, local.C, local.D, local.E, R(75) );
270 P( local.E, local.A, local.B, local.C, local.D, R(76) );
271 P( local.D, local.E, local.A, local.B, local.C, R(77) );
272 P( local.C, local.D, local.E, local.A, local.B, R(78) );
273 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275#undef K
276#undef F
277
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200278 ctx->state[0] += local.A;
279 ctx->state[1] += local.B;
280 ctx->state[2] += local.C;
281 ctx->state[3] += local.D;
282 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100283
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200284 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200285 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100286
287 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288}
Jaeden Amero041039f2018-02-19 15:28:08 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292/*
293 * SHA-1 process buffer
294 */
TRodziewicz26371e42021-06-08 16:45:41 +0200295int mbedtls_sha1_update( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100296 const unsigned char *input,
297 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298{
Janos Follath24eed8d2019-11-22 13:21:35 +0000299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000300 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000301 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Hanno Becker039ccab2018-12-18 17:52:14 +0000303 SHA1_VALIDATE_RET( ctx != NULL );
304 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000305
Brian White12895d12014-04-11 11:29:42 -0400306 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100307 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 left = ctx->total[0] & 0x3F;
310 fill = 64 - left;
311
Paul Bakker5c2364c2012-10-01 14:41:15 +0000312 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 ctx->total[0] &= 0xFFFFFFFF;
314
Paul Bakker5c2364c2012-10-01 14:41:15 +0000315 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 ctx->total[1]++;
317
318 if( left && ilen >= fill )
319 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200320 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100321
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100322 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100323 return( ret );
324
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 input += fill;
326 ilen -= fill;
327 left = 0;
328 }
329
330 while( ilen >= 64 )
331 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100332 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100333 return( ret );
334
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 input += 64;
336 ilen -= 64;
337 }
338
339 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200340 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100341
342 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343}
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/*
346 * SHA-1 final digest
347 */
TRodziewicz26371e42021-06-08 16:45:41 +0200348int mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100349 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000350{
Janos Follath24eed8d2019-11-22 13:21:35 +0000351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200352 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000353 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Hanno Becker039ccab2018-12-18 17:52:14 +0000355 SHA1_VALIDATE_RET( ctx != NULL );
356 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000357
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200358 /*
359 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
360 */
361 used = ctx->total[0] & 0x3F;
362
363 ctx->buffer[used++] = 0x80;
364
365 if( used <= 56 )
366 {
367 /* Enough room for padding + length in current block */
368 memset( ctx->buffer + used, 0, 56 - used );
369 }
370 else
371 {
372 /* We'll need an extra block */
373 memset( ctx->buffer + used, 0, 64 - used );
374
375 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
376 return( ret );
377
378 memset( ctx->buffer, 0, 56 );
379 }
380
381 /*
382 * Add message length
383 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 high = ( ctx->total[0] >> 29 )
385 | ( ctx->total[1] << 3 );
386 low = ( ctx->total[0] << 3 );
387
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200388 PUT_UINT32_BE( high, ctx->buffer, 56 );
389 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200391 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100392 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200394 /*
395 * Output final state
396 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000397 PUT_UINT32_BE( ctx->state[0], output, 0 );
398 PUT_UINT32_BE( ctx->state[1], output, 4 );
399 PUT_UINT32_BE( ctx->state[2], output, 8 );
400 PUT_UINT32_BE( ctx->state[3], output, 12 );
401 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100402
403 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404}
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408/*
409 * output = SHA-1( input buffer )
410 */
TRodziewicz26371e42021-06-08 16:45:41 +0200411int mbedtls_sha1( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100412 size_t ilen,
413 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000414{
Janos Follath24eed8d2019-11-22 13:21:35 +0000415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Hanno Becker039ccab2018-12-18 17:52:14 +0000418 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
419 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100422
TRodziewicz26371e42021-06-08 16:45:41 +0200423 if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100424 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100425
TRodziewicz26371e42021-06-08 16:45:41 +0200426 if( ( ret = mbedtls_sha1_update( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100427 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100428
TRodziewicz26371e42021-06-08 16:45:41 +0200429 if( ( ret = mbedtls_sha1_finish( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100430 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100431
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100434
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100435 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436}
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000439/*
440 * FIPS-180-1 test vectors
441 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000442static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000443{
444 { "abc" },
445 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
446 { "" }
447};
448
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100449static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000450{
451 3, 56, 1000
452};
453
454static const unsigned char sha1_test_sum[3][20] =
455{
456 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
457 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
458 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
459 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
460 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
461 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
462};
463
464/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 * Checkup routine
466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000468{
Paul Bakker5b4af392014-06-26 12:09:34 +0200469 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 unsigned char buf[1024];
471 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200475
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 /*
477 * SHA-1
478 */
479 for( i = 0; i < 3; i++ )
480 {
481 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
TRodziewicz26371e42021-06-08 16:45:41 +0200484 if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100485 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 if( i == 2 )
488 {
489 memset( buf, 'a', buflen = 1000 );
490
491 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100492 {
TRodziewicz26371e42021-06-08 16:45:41 +0200493 ret = mbedtls_sha1_update( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100494 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100495 goto fail;
496 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 }
498 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100499 {
TRodziewicz26371e42021-06-08 16:45:41 +0200500 ret = mbedtls_sha1_update( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100501 sha1_test_buflen[i] );
502 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100503 goto fail;
504 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
TRodziewicz26371e42021-06-08 16:45:41 +0200506 if( ( ret = mbedtls_sha1_finish( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100507 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100510 {
511 ret = 1;
512 goto fail;
513 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 }
518
519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100522 goto exit;
523
524fail:
525 if( verbose != 0 )
526 mbedtls_printf( "failed\n" );
527
Paul Bakker5b4af392014-06-26 12:09:34 +0200528exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200530
531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532}
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_SHA1_C */