blob: 8682abd740b32483edd4c1303cf82ca9af0a0a63 [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
316 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317}
Jaeden Amero041039f2018-02-19 15:28:08 +0000318
319#if !defined(MBEDTLS_DEPRECATED_REMOVED)
320void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
321 const unsigned char data[64] )
322{
323 mbedtls_internal_sha1_process( ctx, data );
324}
325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/*
329 * SHA-1 process buffer
330 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100331int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100332 const unsigned char *input,
333 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000334{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100335 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000336 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000337 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Hanno Becker039ccab2018-12-18 17:52:14 +0000339 SHA1_VALIDATE_RET( ctx != NULL );
340 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000341
Brian White12895d12014-04-11 11:29:42 -0400342 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100343 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 left = ctx->total[0] & 0x3F;
346 fill = 64 - left;
347
Paul Bakker5c2364c2012-10-01 14:41:15 +0000348 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 ctx->total[0] &= 0xFFFFFFFF;
350
Paul Bakker5c2364c2012-10-01 14:41:15 +0000351 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 ctx->total[1]++;
353
354 if( left && ilen >= fill )
355 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200356 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100357
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100358 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100359 return( ret );
360
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 input += fill;
362 ilen -= fill;
363 left = 0;
364 }
365
366 while( ilen >= 64 )
367 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100368 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100369 return( ret );
370
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 input += 64;
372 ilen -= 64;
373 }
374
375 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200376 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100377
378 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379}
380
Jaeden Amero041039f2018-02-19 15:28:08 +0000381#if !defined(MBEDTLS_DEPRECATED_REMOVED)
382void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
383 const unsigned char *input,
384 size_t ilen )
385{
386 mbedtls_sha1_update_ret( ctx, input, ilen );
387}
388#endif
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390/*
391 * SHA-1 final digest
392 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100394 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000395{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100396 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200397 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000398 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Hanno Becker039ccab2018-12-18 17:52:14 +0000400 SHA1_VALIDATE_RET( ctx != NULL );
401 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000402
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200403 /*
404 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
405 */
406 used = ctx->total[0] & 0x3F;
407
408 ctx->buffer[used++] = 0x80;
409
410 if( used <= 56 )
411 {
412 /* Enough room for padding + length in current block */
413 memset( ctx->buffer + used, 0, 56 - used );
414 }
415 else
416 {
417 /* We'll need an extra block */
418 memset( ctx->buffer + used, 0, 64 - used );
419
420 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
421 return( ret );
422
423 memset( ctx->buffer, 0, 56 );
424 }
425
426 /*
427 * Add message length
428 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 high = ( ctx->total[0] >> 29 )
430 | ( ctx->total[1] << 3 );
431 low = ( ctx->total[0] << 3 );
432
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200433 PUT_UINT32_BE( high, ctx->buffer, 56 );
434 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200436 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200439 /*
440 * Output final state
441 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000442 PUT_UINT32_BE( ctx->state[0], output, 0 );
443 PUT_UINT32_BE( ctx->state[1], output, 4 );
444 PUT_UINT32_BE( ctx->state[2], output, 8 );
445 PUT_UINT32_BE( ctx->state[3], output, 12 );
446 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100447
448 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449}
450
Jaeden Amero041039f2018-02-19 15:28:08 +0000451#if !defined(MBEDTLS_DEPRECATED_REMOVED)
452void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
453 unsigned char output[20] )
454{
455 mbedtls_sha1_finish_ret( ctx, output );
456}
457#endif
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200460
Paul Bakker5121ce52009-01-03 21:22:43 +0000461/*
462 * output = SHA-1( input buffer )
463 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465 size_t ilen,
466 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100468 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Hanno Becker039ccab2018-12-18 17:52:14 +0000471 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
472 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100477 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100480 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100481
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100483 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100484
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100485exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100487
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100488 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489}
490
Jaeden Amero041039f2018-02-19 15:28:08 +0000491#if !defined(MBEDTLS_DEPRECATED_REMOVED)
492void mbedtls_sha1( const unsigned char *input,
493 size_t ilen,
494 unsigned char output[20] )
495{
496 mbedtls_sha1_ret( input, ilen, output );
497}
498#endif
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000501/*
502 * FIPS-180-1 test vectors
503 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000504static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000505{
506 { "abc" },
507 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
508 { "" }
509};
510
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100511static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
513 3, 56, 1000
514};
515
516static const unsigned char sha1_test_sum[3][20] =
517{
518 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
519 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
520 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
521 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
522 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
523 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
524};
525
526/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 * Checkup routine
528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530{
Paul Bakker5b4af392014-06-26 12:09:34 +0200531 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 unsigned char buf[1024];
533 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200537
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 /*
539 * SHA-1
540 */
541 for( i = 0; i < 3; i++ )
542 {
543 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100546 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100547 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 if( i == 2 )
550 {
551 memset( buf, 'a', buflen = 1000 );
552
553 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100554 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100555 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100556 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100557 goto fail;
558 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 }
560 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100561 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100562 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100563 sha1_test_buflen[i] );
564 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100565 goto fail;
566 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100568 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100569 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100572 {
573 ret = 1;
574 goto fail;
575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 }
580
581 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100584 goto exit;
585
586fail:
587 if( verbose != 0 )
588 mbedtls_printf( "failed\n" );
589
Paul Bakker5b4af392014-06-26 12:09:34 +0200590exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200592
593 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594}
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#endif /* MBEDTLS_SHA1_C */