blob: 0a5edafaff91dd21263daeee002b56c6629812c3 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020052{
Hanno Becker039ccab2018-12-18 17:52:14 +000053 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020056}
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020059{
60 if( ctx == NULL )
61 return;
62
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020064}
65
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020066void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
67 const mbedtls_sha1_context *src )
68{
Hanno Becker039ccab2018-12-18 17:52:14 +000069 SHA1_VALIDATE( dst != NULL );
70 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000071
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020072 *dst = *src;
73}
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/*
76 * SHA-1 context setup
77 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010078int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Hanno Becker039ccab2018-12-18 17:52:14 +000080 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000081
Paul Bakker5121ce52009-01-03 21:22:43 +000082 ctx->total[0] = 0;
83 ctx->total[1] = 0;
84
85 ctx->state[0] = 0x67452301;
86 ctx->state[1] = 0xEFCDAB89;
87 ctx->state[2] = 0x98BADCFE;
88 ctx->state[3] = 0x10325476;
89 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010090
91 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
93
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020094#if !defined(MBEDTLS_DEPRECATED_REMOVED)
95void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
96{
97 mbedtls_sha1_starts_ret( ctx );
98}
99#endif
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100102int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
103 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200105 struct
106 {
107 uint32_t temp, W[16], A, B, C, D, E;
108 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Hanno Becker039ccab2018-12-18 17:52:14 +0000110 SHA1_VALIDATE_RET( ctx != NULL );
111 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000112
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100113 local.W[ 0] = MBEDTLS_GET_UINT32_BE( data, 0 );
114 local.W[ 1] = MBEDTLS_GET_UINT32_BE( data, 4 );
115 local.W[ 2] = MBEDTLS_GET_UINT32_BE( data, 8 );
116 local.W[ 3] = MBEDTLS_GET_UINT32_BE( data, 12 );
117 local.W[ 4] = MBEDTLS_GET_UINT32_BE( data, 16 );
118 local.W[ 5] = MBEDTLS_GET_UINT32_BE( data, 20 );
119 local.W[ 6] = MBEDTLS_GET_UINT32_BE( data, 24 );
120 local.W[ 7] = MBEDTLS_GET_UINT32_BE( data, 28 );
121 local.W[ 8] = MBEDTLS_GET_UINT32_BE( data, 32 );
122 local.W[ 9] = MBEDTLS_GET_UINT32_BE( data, 36 );
123 local.W[10] = MBEDTLS_GET_UINT32_BE( data, 40 );
124 local.W[11] = MBEDTLS_GET_UINT32_BE( data, 44 );
125 local.W[12] = MBEDTLS_GET_UINT32_BE( data, 48 );
126 local.W[13] = MBEDTLS_GET_UINT32_BE( data, 52 );
127 local.W[14] = MBEDTLS_GET_UINT32_BE( data, 56 );
128 local.W[15] = MBEDTLS_GET_UINT32_BE( data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Hanno Becker1eeca412018-10-15 12:01:35 +0100130#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Hanno Becker1eeca412018-10-15 12:01:35 +0100132#define R(t) \
133 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200134 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
135 local.W[( (t) - 8 ) & 0x0F] ^ \
136 local.W[( (t) - 14 ) & 0x0F] ^ \
137 local.W[ (t) & 0x0F], \
138 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100139 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Hanno Becker1eeca412018-10-15 12:01:35 +0100141#define P(a,b,c,d,e,x) \
142 do \
143 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100144 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
145 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100146 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200148 local.A = ctx->state[0];
149 local.B = ctx->state[1];
150 local.C = ctx->state[2];
151 local.D = ctx->state[3];
152 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Hanno Becker1eeca412018-10-15 12:01:35 +0100154#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000155#define K 0x5A827999
156
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200157 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
158 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
159 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
160 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
161 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
162 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
163 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
164 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
165 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
166 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
167 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
168 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
169 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
170 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
171 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
172 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
173 P( local.E, local.A, local.B, local.C, local.D, R(16) );
174 P( local.D, local.E, local.A, local.B, local.C, R(17) );
175 P( local.C, local.D, local.E, local.A, local.B, R(18) );
176 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178#undef K
179#undef F
180
Hanno Becker1eeca412018-10-15 12:01:35 +0100181#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000182#define K 0x6ED9EBA1
183
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200184 P( local.A, local.B, local.C, local.D, local.E, R(20) );
185 P( local.E, local.A, local.B, local.C, local.D, R(21) );
186 P( local.D, local.E, local.A, local.B, local.C, R(22) );
187 P( local.C, local.D, local.E, local.A, local.B, R(23) );
188 P( local.B, local.C, local.D, local.E, local.A, R(24) );
189 P( local.A, local.B, local.C, local.D, local.E, R(25) );
190 P( local.E, local.A, local.B, local.C, local.D, R(26) );
191 P( local.D, local.E, local.A, local.B, local.C, R(27) );
192 P( local.C, local.D, local.E, local.A, local.B, R(28) );
193 P( local.B, local.C, local.D, local.E, local.A, R(29) );
194 P( local.A, local.B, local.C, local.D, local.E, R(30) );
195 P( local.E, local.A, local.B, local.C, local.D, R(31) );
196 P( local.D, local.E, local.A, local.B, local.C, R(32) );
197 P( local.C, local.D, local.E, local.A, local.B, R(33) );
198 P( local.B, local.C, local.D, local.E, local.A, R(34) );
199 P( local.A, local.B, local.C, local.D, local.E, R(35) );
200 P( local.E, local.A, local.B, local.C, local.D, R(36) );
201 P( local.D, local.E, local.A, local.B, local.C, R(37) );
202 P( local.C, local.D, local.E, local.A, local.B, R(38) );
203 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205#undef K
206#undef F
207
Hanno Becker1eeca412018-10-15 12:01:35 +0100208#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000209#define K 0x8F1BBCDC
210
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200211 P( local.A, local.B, local.C, local.D, local.E, R(40) );
212 P( local.E, local.A, local.B, local.C, local.D, R(41) );
213 P( local.D, local.E, local.A, local.B, local.C, R(42) );
214 P( local.C, local.D, local.E, local.A, local.B, R(43) );
215 P( local.B, local.C, local.D, local.E, local.A, R(44) );
216 P( local.A, local.B, local.C, local.D, local.E, R(45) );
217 P( local.E, local.A, local.B, local.C, local.D, R(46) );
218 P( local.D, local.E, local.A, local.B, local.C, R(47) );
219 P( local.C, local.D, local.E, local.A, local.B, R(48) );
220 P( local.B, local.C, local.D, local.E, local.A, R(49) );
221 P( local.A, local.B, local.C, local.D, local.E, R(50) );
222 P( local.E, local.A, local.B, local.C, local.D, R(51) );
223 P( local.D, local.E, local.A, local.B, local.C, R(52) );
224 P( local.C, local.D, local.E, local.A, local.B, R(53) );
225 P( local.B, local.C, local.D, local.E, local.A, R(54) );
226 P( local.A, local.B, local.C, local.D, local.E, R(55) );
227 P( local.E, local.A, local.B, local.C, local.D, R(56) );
228 P( local.D, local.E, local.A, local.B, local.C, R(57) );
229 P( local.C, local.D, local.E, local.A, local.B, R(58) );
230 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232#undef K
233#undef F
234
Hanno Becker1eeca412018-10-15 12:01:35 +0100235#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000236#define K 0xCA62C1D6
237
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200238 P( local.A, local.B, local.C, local.D, local.E, R(60) );
239 P( local.E, local.A, local.B, local.C, local.D, R(61) );
240 P( local.D, local.E, local.A, local.B, local.C, R(62) );
241 P( local.C, local.D, local.E, local.A, local.B, R(63) );
242 P( local.B, local.C, local.D, local.E, local.A, R(64) );
243 P( local.A, local.B, local.C, local.D, local.E, R(65) );
244 P( local.E, local.A, local.B, local.C, local.D, R(66) );
245 P( local.D, local.E, local.A, local.B, local.C, R(67) );
246 P( local.C, local.D, local.E, local.A, local.B, R(68) );
247 P( local.B, local.C, local.D, local.E, local.A, R(69) );
248 P( local.A, local.B, local.C, local.D, local.E, R(70) );
249 P( local.E, local.A, local.B, local.C, local.D, R(71) );
250 P( local.D, local.E, local.A, local.B, local.C, R(72) );
251 P( local.C, local.D, local.E, local.A, local.B, R(73) );
252 P( local.B, local.C, local.D, local.E, local.A, R(74) );
253 P( local.A, local.B, local.C, local.D, local.E, R(75) );
254 P( local.E, local.A, local.B, local.C, local.D, R(76) );
255 P( local.D, local.E, local.A, local.B, local.C, R(77) );
256 P( local.C, local.D, local.E, local.A, local.B, R(78) );
257 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
259#undef K
260#undef F
261
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200262 ctx->state[0] += local.A;
263 ctx->state[1] += local.B;
264 ctx->state[2] += local.C;
265 ctx->state[3] += local.D;
266 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100267
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200268 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200269 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100270
271 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272}
Jaeden Amero041039f2018-02-19 15:28:08 +0000273
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200274#if !defined(MBEDTLS_DEPRECATED_REMOVED)
275void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
276 const unsigned char data[64] )
277{
278 mbedtls_internal_sha1_process( ctx, data );
279}
280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283/*
284 * SHA-1 process buffer
285 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100286int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100287 const unsigned char *input,
288 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000289{
Janos Follath24eed8d2019-11-22 13:21:35 +0000290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000291 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000292 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Hanno Becker039ccab2018-12-18 17:52:14 +0000294 SHA1_VALIDATE_RET( ctx != NULL );
295 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000296
Brian White12895d12014-04-11 11:29:42 -0400297 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100298 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 left = ctx->total[0] & 0x3F;
301 fill = 64 - left;
302
Paul Bakker5c2364c2012-10-01 14:41:15 +0000303 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 ctx->total[0] &= 0xFFFFFFFF;
305
Paul Bakker5c2364c2012-10-01 14:41:15 +0000306 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 ctx->total[1]++;
308
309 if( left && ilen >= fill )
310 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200311 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100312
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100313 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100314 return( ret );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 input += fill;
317 ilen -= fill;
318 left = 0;
319 }
320
321 while( ilen >= 64 )
322 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100323 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100324 return( ret );
325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 input += 64;
327 ilen -= 64;
328 }
329
330 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200331 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100332
333 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334}
335
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200336#if !defined(MBEDTLS_DEPRECATED_REMOVED)
337void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
338 const unsigned char *input,
339 size_t ilen )
340{
341 mbedtls_sha1_update_ret( ctx, input, ilen );
342}
343#endif
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/*
346 * SHA-1 final digest
347 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100348int mbedtls_sha1_finish_ret( 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
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100388 MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 );
389 MBEDTLS_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 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100397 MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 );
398 MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 );
399 MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 );
400 MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 );
401 MBEDTLS_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é-Gonnard93c08472021-04-15 12:23:55 +0200406#if !defined(MBEDTLS_DEPRECATED_REMOVED)
407void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
408 unsigned char output[20] )
409{
410 mbedtls_sha1_finish_ret( ctx, output );
411}
412#endif
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200415
Paul Bakker5121ce52009-01-03 21:22:43 +0000416/*
417 * output = SHA-1( input buffer )
418 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100419int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100420 size_t ilen,
421 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422{
Janos Follath24eed8d2019-11-22 13:21:35 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Hanno Becker039ccab2018-12-18 17:52:14 +0000426 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
427 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100431 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100432 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100433
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100434 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100435 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100437 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100438 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100439
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100440exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100442
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100443 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444}
445
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200446#if !defined(MBEDTLS_DEPRECATED_REMOVED)
447void mbedtls_sha1( const unsigned char *input,
448 size_t ilen,
449 unsigned char output[20] )
450{
451 mbedtls_sha1_ret( input, ilen, output );
452}
453#endif
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456/*
457 * FIPS-180-1 test vectors
458 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000459static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000460{
461 { "abc" },
462 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
463 { "" }
464};
465
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100466static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000467{
468 3, 56, 1000
469};
470
471static const unsigned char sha1_test_sum[3][20] =
472{
473 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
474 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
475 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
476 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
477 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
478 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
479};
480
481/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 * Checkup routine
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000485{
Paul Bakker5b4af392014-06-26 12:09:34 +0200486 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 unsigned char buf[1024];
488 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200492
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 /*
494 * SHA-1
495 */
496 for( i = 0; i < 3; i++ )
497 {
498 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100501 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100502 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( i == 2 )
505 {
506 memset( buf, 'a', buflen = 1000 );
507
508 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100509 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100510 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100511 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100512 goto fail;
513 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 }
515 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100516 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100517 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100518 sha1_test_buflen[i] );
519 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100520 goto fail;
521 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100523 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100524 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100527 {
528 ret = 1;
529 goto fail;
530 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 }
535
536 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100539 goto exit;
540
541fail:
542 if( verbose != 0 )
543 mbedtls_printf( "failed\n" );
544
Paul Bakker5b4af392014-06-26 12:09:34 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200547
548 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549}
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_SHA1_C */