blob: 1cffc75f8cbf7e0f7fd550af5ddade9c0ffde83a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The SHA-1 standard was published by NIST in 1993.
50 *
51 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
52 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <string.h>
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SELF_TEST)
68#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010070#else
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define mbedtls_printf printf
73#endif /* MBEDTLS_PLATFORM_C */
74#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010075
Hanno Beckerb3c70232018-12-20 10:18:05 +000076#define SHA1_VALIDATE_RET(cond) \
77 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
78
79#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
80
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020081#if !defined(MBEDTLS_SHA1_ALT)
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/*
84 * 32-bit integer manipulation macros (big endian)
85 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000086#ifndef GET_UINT32_BE
87#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000088{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000089 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
90 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
91 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
92 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000093}
94#endif
95
Paul Bakker5c2364c2012-10-01 14:41:15 +000096#ifndef PUT_UINT32_BE
97#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000098{ \
99 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
100 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
101 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
102 (b)[(i) + 3] = (unsigned char) ( (n) ); \
103}
104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200107{
Hanno Becker039ccab2018-12-18 17:52:14 +0000108 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200114{
115 if( ctx == NULL )
116 return;
117
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500118 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200119}
120
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200121void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
122 const mbedtls_sha1_context *src )
123{
Hanno Becker039ccab2018-12-18 17:52:14 +0000124 SHA1_VALIDATE( dst != NULL );
125 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000126
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200127 *dst = *src;
128}
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130/*
131 * SHA-1 context setup
132 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100133int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134{
Hanno Becker039ccab2018-12-18 17:52:14 +0000135 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 ctx->total[0] = 0;
138 ctx->total[1] = 0;
139
140 ctx->state[0] = 0x67452301;
141 ctx->state[1] = 0xEFCDAB89;
142 ctx->state[2] = 0x98BADCFE;
143 ctx->state[3] = 0x10325476;
144 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100145
146 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147}
148
Jaeden Amero041039f2018-02-19 15:28:08 +0000149#if !defined(MBEDTLS_DEPRECATED_REMOVED)
150void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
151{
152 mbedtls_sha1_starts_ret( ctx );
153}
154#endif
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100157int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
158 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000159{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000160 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Hanno Becker039ccab2018-12-18 17:52:14 +0000162 SHA1_VALIDATE_RET( ctx != NULL );
163 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000164
Paul Bakker5c2364c2012-10-01 14:41:15 +0000165 GET_UINT32_BE( W[ 0], data, 0 );
166 GET_UINT32_BE( W[ 1], data, 4 );
167 GET_UINT32_BE( W[ 2], data, 8 );
168 GET_UINT32_BE( W[ 3], data, 12 );
169 GET_UINT32_BE( W[ 4], data, 16 );
170 GET_UINT32_BE( W[ 5], data, 20 );
171 GET_UINT32_BE( W[ 6], data, 24 );
172 GET_UINT32_BE( W[ 7], data, 28 );
173 GET_UINT32_BE( W[ 8], data, 32 );
174 GET_UINT32_BE( W[ 9], data, 36 );
175 GET_UINT32_BE( W[10], data, 40 );
176 GET_UINT32_BE( W[11], data, 44 );
177 GET_UINT32_BE( W[12], data, 48 );
178 GET_UINT32_BE( W[13], data, 52 );
179 GET_UINT32_BE( W[14], data, 56 );
180 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Hanno Beckerd6028a12018-10-15 12:01:35 +0100182#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Hanno Beckerd6028a12018-10-15 12:01:35 +0100184#define R(t) \
185 ( \
186 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
187 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
188 ( W[(t) & 0x0F] = S(temp,1) ) \
189 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Hanno Beckerd6028a12018-10-15 12:01:35 +0100191#define P(a,b,c,d,e,x) \
192 do \
193 { \
Hanno Becker3ac21ac2018-10-26 09:13:26 +0100194 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
195 (b) = S((b),30); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100196 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198 A = ctx->state[0];
199 B = ctx->state[1];
200 C = ctx->state[2];
201 D = ctx->state[3];
202 E = ctx->state[4];
203
Hanno Beckerd6028a12018-10-15 12:01:35 +0100204#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000205#define K 0x5A827999
206
207 P( A, B, C, D, E, W[0] );
208 P( E, A, B, C, D, W[1] );
209 P( D, E, A, B, C, W[2] );
210 P( C, D, E, A, B, W[3] );
211 P( B, C, D, E, A, W[4] );
212 P( A, B, C, D, E, W[5] );
213 P( E, A, B, C, D, W[6] );
214 P( D, E, A, B, C, W[7] );
215 P( C, D, E, A, B, W[8] );
216 P( B, C, D, E, A, W[9] );
217 P( A, B, C, D, E, W[10] );
218 P( E, A, B, C, D, W[11] );
219 P( D, E, A, B, C, W[12] );
220 P( C, D, E, A, B, W[13] );
221 P( B, C, D, E, A, W[14] );
222 P( A, B, C, D, E, W[15] );
223 P( E, A, B, C, D, R(16) );
224 P( D, E, A, B, C, R(17) );
225 P( C, D, E, A, B, R(18) );
226 P( B, C, D, E, A, R(19) );
227
228#undef K
229#undef F
230
Hanno Beckerd6028a12018-10-15 12:01:35 +0100231#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000232#define K 0x6ED9EBA1
233
234 P( A, B, C, D, E, R(20) );
235 P( E, A, B, C, D, R(21) );
236 P( D, E, A, B, C, R(22) );
237 P( C, D, E, A, B, R(23) );
238 P( B, C, D, E, A, R(24) );
239 P( A, B, C, D, E, R(25) );
240 P( E, A, B, C, D, R(26) );
241 P( D, E, A, B, C, R(27) );
242 P( C, D, E, A, B, R(28) );
243 P( B, C, D, E, A, R(29) );
244 P( A, B, C, D, E, R(30) );
245 P( E, A, B, C, D, R(31) );
246 P( D, E, A, B, C, R(32) );
247 P( C, D, E, A, B, R(33) );
248 P( B, C, D, E, A, R(34) );
249 P( A, B, C, D, E, R(35) );
250 P( E, A, B, C, D, R(36) );
251 P( D, E, A, B, C, R(37) );
252 P( C, D, E, A, B, R(38) );
253 P( B, C, D, E, A, R(39) );
254
255#undef K
256#undef F
257
Hanno Beckerd6028a12018-10-15 12:01:35 +0100258#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000259#define K 0x8F1BBCDC
260
261 P( A, B, C, D, E, R(40) );
262 P( E, A, B, C, D, R(41) );
263 P( D, E, A, B, C, R(42) );
264 P( C, D, E, A, B, R(43) );
265 P( B, C, D, E, A, R(44) );
266 P( A, B, C, D, E, R(45) );
267 P( E, A, B, C, D, R(46) );
268 P( D, E, A, B, C, R(47) );
269 P( C, D, E, A, B, R(48) );
270 P( B, C, D, E, A, R(49) );
271 P( A, B, C, D, E, R(50) );
272 P( E, A, B, C, D, R(51) );
273 P( D, E, A, B, C, R(52) );
274 P( C, D, E, A, B, R(53) );
275 P( B, C, D, E, A, R(54) );
276 P( A, B, C, D, E, R(55) );
277 P( E, A, B, C, D, R(56) );
278 P( D, E, A, B, C, R(57) );
279 P( C, D, E, A, B, R(58) );
280 P( B, C, D, E, A, R(59) );
281
282#undef K
283#undef F
284
Hanno Beckerd6028a12018-10-15 12:01:35 +0100285#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000286#define K 0xCA62C1D6
287
288 P( A, B, C, D, E, R(60) );
289 P( E, A, B, C, D, R(61) );
290 P( D, E, A, B, C, R(62) );
291 P( C, D, E, A, B, R(63) );
292 P( B, C, D, E, A, R(64) );
293 P( A, B, C, D, E, R(65) );
294 P( E, A, B, C, D, R(66) );
295 P( D, E, A, B, C, R(67) );
296 P( C, D, E, A, B, R(68) );
297 P( B, C, D, E, A, R(69) );
298 P( A, B, C, D, E, R(70) );
299 P( E, A, B, C, D, R(71) );
300 P( D, E, A, B, C, R(72) );
301 P( C, D, E, A, B, R(73) );
302 P( B, C, D, E, A, R(74) );
303 P( A, B, C, D, E, R(75) );
304 P( E, A, B, C, D, R(76) );
305 P( D, E, A, B, C, R(77) );
306 P( C, D, E, A, B, R(78) );
307 P( B, C, D, E, A, R(79) );
308
309#undef K
310#undef F
311
312 ctx->state[0] += A;
313 ctx->state[1] += B;
314 ctx->state[2] += C;
315 ctx->state[3] += D;
316 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100317
318 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319}
Jaeden Amero041039f2018-02-19 15:28:08 +0000320
321#if !defined(MBEDTLS_DEPRECATED_REMOVED)
322void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
323 const unsigned char data[64] )
324{
325 mbedtls_internal_sha1_process( ctx, data );
326}
327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/*
331 * SHA-1 process buffer
332 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100334 const unsigned char *input,
335 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000336{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100337 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000338 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000339 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Hanno Becker039ccab2018-12-18 17:52:14 +0000341 SHA1_VALIDATE_RET( ctx != NULL );
342 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000343
Brian White12895d12014-04-11 11:29:42 -0400344 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100345 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347 left = ctx->total[0] & 0x3F;
348 fill = 64 - left;
349
Paul Bakker5c2364c2012-10-01 14:41:15 +0000350 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 ctx->total[0] &= 0xFFFFFFFF;
352
Paul Bakker5c2364c2012-10-01 14:41:15 +0000353 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 ctx->total[1]++;
355
356 if( left && ilen >= fill )
357 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200358 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100359
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100360 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100361 return( ret );
362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 input += fill;
364 ilen -= fill;
365 left = 0;
366 }
367
368 while( ilen >= 64 )
369 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100370 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100371 return( ret );
372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 input += 64;
374 ilen -= 64;
375 }
376
377 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200378 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100379
380 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381}
382
Jaeden Amero041039f2018-02-19 15:28:08 +0000383#if !defined(MBEDTLS_DEPRECATED_REMOVED)
384void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
385 const unsigned char *input,
386 size_t ilen )
387{
388 mbedtls_sha1_update_ret( ctx, input, ilen );
389}
390#endif
391
Paul Bakker5121ce52009-01-03 21:22:43 +0000392/*
393 * SHA-1 final digest
394 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100396 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000397{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100398 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200399 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000400 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
Hanno Becker039ccab2018-12-18 17:52:14 +0000402 SHA1_VALIDATE_RET( ctx != NULL );
403 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000404
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200405 /*
406 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
407 */
408 used = ctx->total[0] & 0x3F;
409
410 ctx->buffer[used++] = 0x80;
411
412 if( used <= 56 )
413 {
414 /* Enough room for padding + length in current block */
415 memset( ctx->buffer + used, 0, 56 - used );
416 }
417 else
418 {
419 /* We'll need an extra block */
420 memset( ctx->buffer + used, 0, 64 - used );
421
422 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
423 return( ret );
424
425 memset( ctx->buffer, 0, 56 );
426 }
427
428 /*
429 * Add message length
430 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 high = ( ctx->total[0] >> 29 )
432 | ( ctx->total[1] << 3 );
433 low = ( ctx->total[0] << 3 );
434
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200435 PUT_UINT32_BE( high, ctx->buffer, 56 );
436 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200438 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100439 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200441 /*
442 * Output final state
443 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000444 PUT_UINT32_BE( ctx->state[0], output, 0 );
445 PUT_UINT32_BE( ctx->state[1], output, 4 );
446 PUT_UINT32_BE( ctx->state[2], output, 8 );
447 PUT_UINT32_BE( ctx->state[3], output, 12 );
448 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100449
450 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451}
452
Jaeden Amero041039f2018-02-19 15:28:08 +0000453#if !defined(MBEDTLS_DEPRECATED_REMOVED)
454void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
455 unsigned char output[20] )
456{
457 mbedtls_sha1_finish_ret( ctx, output );
458}
459#endif
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463/*
464 * output = SHA-1( input buffer )
465 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100467 size_t ilen,
468 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100470 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Hanno Becker039ccab2018-12-18 17:52:14 +0000473 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
474 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100477
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100479 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100480
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100482 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100483
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100485 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100486
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100487exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100489
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100490 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491}
492
Jaeden Amero041039f2018-02-19 15:28:08 +0000493#if !defined(MBEDTLS_DEPRECATED_REMOVED)
494void mbedtls_sha1( const unsigned char *input,
495 size_t ilen,
496 unsigned char output[20] )
497{
498 mbedtls_sha1_ret( input, ilen, output );
499}
500#endif
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000503/*
504 * FIPS-180-1 test vectors
505 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000506static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000507{
508 { "abc" },
509 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
510 { "" }
511};
512
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100513static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000514{
515 3, 56, 1000
516};
517
518static const unsigned char sha1_test_sum[3][20] =
519{
520 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
521 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
522 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
523 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
524 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
525 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
526};
527
528/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 * Checkup routine
530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000532{
Paul Bakker5b4af392014-06-26 12:09:34 +0200533 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char buf[1024];
535 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200539
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 /*
541 * SHA-1
542 */
543 for( i = 0; i < 3; i++ )
544 {
545 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100548 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100549 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 if( i == 2 )
552 {
553 memset( buf, 'a', buflen = 1000 );
554
555 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100556 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100557 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100558 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100559 goto fail;
560 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100563 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100564 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100565 sha1_test_buflen[i] );
566 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100567 goto fail;
568 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100570 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100571 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100574 {
575 ret = 1;
576 goto fail;
577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 }
582
583 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100586 goto exit;
587
588fail:
589 if( verbose != 0 )
590 mbedtls_printf( "failed\n" );
591
Paul Bakker5b4af392014-06-26 12:09:34 +0200592exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200594
595 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596}
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_SHA1_C */