blob: 4bb51982f92fe4ad2c124878806f212d109d9c26 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SHA-1 standard was published by NIST in 1993.
48 *
49 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
50 */
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <string.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_SELF_TEST)
66#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#else
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_printf printf
71#endif /* MBEDTLS_PLATFORM_C */
72#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010073
Hanno Beckerb3c70232018-12-20 10:18:05 +000074#define SHA1_VALIDATE_RET(cond) \
75 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
76
77#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
78
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020079#if !defined(MBEDTLS_SHA1_ALT)
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
82 * 32-bit integer manipulation macros (big endian)
83 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000084#ifndef GET_UINT32_BE
85#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000086{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000087 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
88 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
89 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
90 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92#endif
93
Paul Bakker5c2364c2012-10-01 14:41:15 +000094#ifndef PUT_UINT32_BE
95#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000096{ \
97 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
98 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
99 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
100 (b)[(i) + 3] = (unsigned char) ( (n) ); \
101}
102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200105{
Hanno Becker039ccab2018-12-18 17:52:14 +0000106 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200112{
113 if( ctx == NULL )
114 return;
115
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500116 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200117}
118
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200119void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
120 const mbedtls_sha1_context *src )
121{
Hanno Becker039ccab2018-12-18 17:52:14 +0000122 SHA1_VALIDATE( dst != NULL );
123 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000124
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200125 *dst = *src;
126}
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128/*
129 * SHA-1 context setup
130 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100131int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
Hanno Becker039ccab2018-12-18 17:52:14 +0000133 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 ctx->total[0] = 0;
136 ctx->total[1] = 0;
137
138 ctx->state[0] = 0x67452301;
139 ctx->state[1] = 0xEFCDAB89;
140 ctx->state[2] = 0x98BADCFE;
141 ctx->state[3] = 0x10325476;
142 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100143
144 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145}
146
Jaeden Amero041039f2018-02-19 15:28:08 +0000147#if !defined(MBEDTLS_DEPRECATED_REMOVED)
148void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
149{
150 mbedtls_sha1_starts_ret( ctx );
151}
152#endif
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100155int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
156 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000158 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Hanno Becker039ccab2018-12-18 17:52:14 +0000160 SHA1_VALIDATE_RET( ctx != NULL );
161 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000162
Paul Bakker5c2364c2012-10-01 14:41:15 +0000163 GET_UINT32_BE( W[ 0], data, 0 );
164 GET_UINT32_BE( W[ 1], data, 4 );
165 GET_UINT32_BE( W[ 2], data, 8 );
166 GET_UINT32_BE( W[ 3], data, 12 );
167 GET_UINT32_BE( W[ 4], data, 16 );
168 GET_UINT32_BE( W[ 5], data, 20 );
169 GET_UINT32_BE( W[ 6], data, 24 );
170 GET_UINT32_BE( W[ 7], data, 28 );
171 GET_UINT32_BE( W[ 8], data, 32 );
172 GET_UINT32_BE( W[ 9], data, 36 );
173 GET_UINT32_BE( W[10], data, 40 );
174 GET_UINT32_BE( W[11], data, 44 );
175 GET_UINT32_BE( W[12], data, 48 );
176 GET_UINT32_BE( W[13], data, 52 );
177 GET_UINT32_BE( W[14], data, 56 );
178 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Hanno Beckerd6028a12018-10-15 12:01:35 +0100180#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Hanno Beckerd6028a12018-10-15 12:01:35 +0100182#define R(t) \
183 ( \
184 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
185 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
186 ( W[(t) & 0x0F] = S(temp,1) ) \
187 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Hanno Beckerd6028a12018-10-15 12:01:35 +0100189#define P(a,b,c,d,e,x) \
190 do \
191 { \
Hanno Becker3ac21ac2018-10-26 09:13:26 +0100192 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
193 (b) = S((b),30); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100194 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 A = ctx->state[0];
197 B = ctx->state[1];
198 C = ctx->state[2];
199 D = ctx->state[3];
200 E = ctx->state[4];
201
Hanno Beckerd6028a12018-10-15 12:01:35 +0100202#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000203#define K 0x5A827999
204
205 P( A, B, C, D, E, W[0] );
206 P( E, A, B, C, D, W[1] );
207 P( D, E, A, B, C, W[2] );
208 P( C, D, E, A, B, W[3] );
209 P( B, C, D, E, A, W[4] );
210 P( A, B, C, D, E, W[5] );
211 P( E, A, B, C, D, W[6] );
212 P( D, E, A, B, C, W[7] );
213 P( C, D, E, A, B, W[8] );
214 P( B, C, D, E, A, W[9] );
215 P( A, B, C, D, E, W[10] );
216 P( E, A, B, C, D, W[11] );
217 P( D, E, A, B, C, W[12] );
218 P( C, D, E, A, B, W[13] );
219 P( B, C, D, E, A, W[14] );
220 P( A, B, C, D, E, W[15] );
221 P( E, A, B, C, D, R(16) );
222 P( D, E, A, B, C, R(17) );
223 P( C, D, E, A, B, R(18) );
224 P( B, C, D, E, A, R(19) );
225
226#undef K
227#undef F
228
Hanno Beckerd6028a12018-10-15 12:01:35 +0100229#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000230#define K 0x6ED9EBA1
231
232 P( A, B, C, D, E, R(20) );
233 P( E, A, B, C, D, R(21) );
234 P( D, E, A, B, C, R(22) );
235 P( C, D, E, A, B, R(23) );
236 P( B, C, D, E, A, R(24) );
237 P( A, B, C, D, E, R(25) );
238 P( E, A, B, C, D, R(26) );
239 P( D, E, A, B, C, R(27) );
240 P( C, D, E, A, B, R(28) );
241 P( B, C, D, E, A, R(29) );
242 P( A, B, C, D, E, R(30) );
243 P( E, A, B, C, D, R(31) );
244 P( D, E, A, B, C, R(32) );
245 P( C, D, E, A, B, R(33) );
246 P( B, C, D, E, A, R(34) );
247 P( A, B, C, D, E, R(35) );
248 P( E, A, B, C, D, R(36) );
249 P( D, E, A, B, C, R(37) );
250 P( C, D, E, A, B, R(38) );
251 P( B, C, D, E, A, R(39) );
252
253#undef K
254#undef F
255
Hanno Beckerd6028a12018-10-15 12:01:35 +0100256#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000257#define K 0x8F1BBCDC
258
259 P( A, B, C, D, E, R(40) );
260 P( E, A, B, C, D, R(41) );
261 P( D, E, A, B, C, R(42) );
262 P( C, D, E, A, B, R(43) );
263 P( B, C, D, E, A, R(44) );
264 P( A, B, C, D, E, R(45) );
265 P( E, A, B, C, D, R(46) );
266 P( D, E, A, B, C, R(47) );
267 P( C, D, E, A, B, R(48) );
268 P( B, C, D, E, A, R(49) );
269 P( A, B, C, D, E, R(50) );
270 P( E, A, B, C, D, R(51) );
271 P( D, E, A, B, C, R(52) );
272 P( C, D, E, A, B, R(53) );
273 P( B, C, D, E, A, R(54) );
274 P( A, B, C, D, E, R(55) );
275 P( E, A, B, C, D, R(56) );
276 P( D, E, A, B, C, R(57) );
277 P( C, D, E, A, B, R(58) );
278 P( B, C, D, E, A, R(59) );
279
280#undef K
281#undef F
282
Hanno Beckerd6028a12018-10-15 12:01:35 +0100283#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000284#define K 0xCA62C1D6
285
286 P( A, B, C, D, E, R(60) );
287 P( E, A, B, C, D, R(61) );
288 P( D, E, A, B, C, R(62) );
289 P( C, D, E, A, B, R(63) );
290 P( B, C, D, E, A, R(64) );
291 P( A, B, C, D, E, R(65) );
292 P( E, A, B, C, D, R(66) );
293 P( D, E, A, B, C, R(67) );
294 P( C, D, E, A, B, R(68) );
295 P( B, C, D, E, A, R(69) );
296 P( A, B, C, D, E, R(70) );
297 P( E, A, B, C, D, R(71) );
298 P( D, E, A, B, C, R(72) );
299 P( C, D, E, A, B, R(73) );
300 P( B, C, D, E, A, R(74) );
301 P( A, B, C, D, E, R(75) );
302 P( E, A, B, C, D, R(76) );
303 P( D, E, A, B, C, R(77) );
304 P( C, D, E, A, B, R(78) );
305 P( B, C, D, E, A, R(79) );
306
307#undef K
308#undef F
309
310 ctx->state[0] += A;
311 ctx->state[1] += B;
312 ctx->state[2] += C;
313 ctx->state[3] += D;
314 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100315
gabor-mezei-armd5253bb2020-07-30 16:41:25 +0200316 /* Zeroise buffers and variables to clear sensitive data from memory. */
317 mbedtls_platform_zeroize( &A, sizeof( A ) );
318 mbedtls_platform_zeroize( &B, sizeof( B ) );
319 mbedtls_platform_zeroize( &C, sizeof( C ) );
320 mbedtls_platform_zeroize( &D, sizeof( D ) );
321 mbedtls_platform_zeroize( &E, sizeof( E ) );
322 mbedtls_platform_zeroize( &W, sizeof( W ) );
323 mbedtls_platform_zeroize( &temp, sizeof( temp ) );
324
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100325 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326}
Jaeden Amero041039f2018-02-19 15:28:08 +0000327
328#if !defined(MBEDTLS_DEPRECATED_REMOVED)
329void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
330 const unsigned char data[64] )
331{
332 mbedtls_internal_sha1_process( ctx, data );
333}
334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337/*
338 * SHA-1 process buffer
339 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100341 const unsigned char *input,
342 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000343{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100344 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000345 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000346 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Hanno Becker039ccab2018-12-18 17:52:14 +0000348 SHA1_VALIDATE_RET( ctx != NULL );
349 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000350
Brian White12895d12014-04-11 11:29:42 -0400351 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100352 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354 left = ctx->total[0] & 0x3F;
355 fill = 64 - left;
356
Paul Bakker5c2364c2012-10-01 14:41:15 +0000357 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 ctx->total[0] &= 0xFFFFFFFF;
359
Paul Bakker5c2364c2012-10-01 14:41:15 +0000360 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 ctx->total[1]++;
362
363 if( left && ilen >= fill )
364 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200365 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100366
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100367 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100368 return( ret );
369
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 input += fill;
371 ilen -= fill;
372 left = 0;
373 }
374
375 while( ilen >= 64 )
376 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100377 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100378 return( ret );
379
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 input += 64;
381 ilen -= 64;
382 }
383
384 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200385 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100386
387 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388}
389
Jaeden Amero041039f2018-02-19 15:28:08 +0000390#if !defined(MBEDTLS_DEPRECATED_REMOVED)
391void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
392 const unsigned char *input,
393 size_t ilen )
394{
395 mbedtls_sha1_update_ret( ctx, input, ilen );
396}
397#endif
398
Paul Bakker5121ce52009-01-03 21:22:43 +0000399/*
400 * SHA-1 final digest
401 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100403 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000404{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100405 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200406 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000407 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Hanno Becker039ccab2018-12-18 17:52:14 +0000409 SHA1_VALIDATE_RET( ctx != NULL );
410 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000411
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200412 /*
413 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
414 */
415 used = ctx->total[0] & 0x3F;
416
417 ctx->buffer[used++] = 0x80;
418
419 if( used <= 56 )
420 {
421 /* Enough room for padding + length in current block */
422 memset( ctx->buffer + used, 0, 56 - used );
423 }
424 else
425 {
426 /* We'll need an extra block */
427 memset( ctx->buffer + used, 0, 64 - used );
428
429 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
430 return( ret );
431
432 memset( ctx->buffer, 0, 56 );
433 }
434
435 /*
436 * Add message length
437 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 high = ( ctx->total[0] >> 29 )
439 | ( ctx->total[1] << 3 );
440 low = ( ctx->total[0] << 3 );
441
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200442 PUT_UINT32_BE( high, ctx->buffer, 56 );
443 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200445 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100446 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200448 /*
449 * Output final state
450 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000451 PUT_UINT32_BE( ctx->state[0], output, 0 );
452 PUT_UINT32_BE( ctx->state[1], output, 4 );
453 PUT_UINT32_BE( ctx->state[2], output, 8 );
454 PUT_UINT32_BE( ctx->state[3], output, 12 );
455 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100456
457 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458}
459
Jaeden Amero041039f2018-02-19 15:28:08 +0000460#if !defined(MBEDTLS_DEPRECATED_REMOVED)
461void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
462 unsigned char output[20] )
463{
464 mbedtls_sha1_finish_ret( ctx, output );
465}
466#endif
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200469
Paul Bakker5121ce52009-01-03 21:22:43 +0000470/*
471 * output = SHA-1( input buffer )
472 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100474 size_t ilen,
475 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000476{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100477 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Hanno Becker039ccab2018-12-18 17:52:14 +0000480 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
481 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100484
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100485 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100486 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100487
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100489 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100490
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100492 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100493
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100494exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100496
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100497 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498}
499
Jaeden Amero041039f2018-02-19 15:28:08 +0000500#if !defined(MBEDTLS_DEPRECATED_REMOVED)
501void mbedtls_sha1( const unsigned char *input,
502 size_t ilen,
503 unsigned char output[20] )
504{
505 mbedtls_sha1_ret( input, ilen, output );
506}
507#endif
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000510/*
511 * FIPS-180-1 test vectors
512 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000513static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000514{
515 { "abc" },
516 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
517 { "" }
518};
519
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100520static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000521{
522 3, 56, 1000
523};
524
525static const unsigned char sha1_test_sum[3][20] =
526{
527 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
528 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
529 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
530 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
531 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
532 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
533};
534
535/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * Checkup routine
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539{
Paul Bakker5b4af392014-06-26 12:09:34 +0200540 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 unsigned char buf[1024];
542 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 /*
548 * SHA-1
549 */
550 for( i = 0; i < 3; i++ )
551 {
552 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100555 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100556 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 if( i == 2 )
559 {
560 memset( buf, 'a', buflen = 1000 );
561
562 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100563 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100564 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100565 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100566 goto fail;
567 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 }
569 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100570 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100571 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100572 sha1_test_buflen[i] );
573 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100574 goto fail;
575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100577 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100578 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100581 {
582 ret = 1;
583 goto fail;
584 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 }
589
590 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100593 goto exit;
594
595fail:
596 if( verbose != 0 )
597 mbedtls_printf( "failed\n" );
598
Paul Bakker5b4af392014-06-26 12:09:34 +0200599exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200601
602 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603}
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#endif /* MBEDTLS_SHA1_C */