blob: 83ef9bdbdad844b5ad548372bdc5c378a42a7cda [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <string.h>
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SELF_TEST)
65#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#else
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_printf printf
70#endif /* MBEDTLS_PLATFORM_C */
71#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010072
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020073#if !defined(MBEDTLS_SHA1_ALT)
74
Paul Bakker34617722014-06-13 17:20:13 +020075/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010077 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020078}
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080/*
81 * 32-bit integer manipulation macros (big endian)
82 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000083#ifndef GET_UINT32_BE
84#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000085{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000086 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
87 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
88 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
89 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
91#endif
92
Paul Bakker5c2364c2012-10-01 14:41:15 +000093#ifndef PUT_UINT32_BE
94#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000095{ \
96 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
97 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
98 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
99 (b)[(i) + 3] = (unsigned char) ( (n) ); \
100}
101#endif
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200106}
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200109{
110 if( ctx == NULL )
111 return;
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200114}
115
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200116void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
117 const mbedtls_sha1_context *src )
118{
119 *dst = *src;
120}
121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122/*
123 * SHA-1 context setup
124 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100125int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
127 ctx->total[0] = 0;
128 ctx->total[1] = 0;
129
130 ctx->state[0] = 0x67452301;
131 ctx->state[1] = 0xEFCDAB89;
132 ctx->state[2] = 0x98BADCFE;
133 ctx->state[3] = 0x10325476;
134 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100135
136 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137}
138
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000139#if !defined(MBEDTLS_DEPRECATED_REMOVED)
140void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
141{
142 mbedtls_sha1_starts_ret( ctx );
143}
144#endif
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100147int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
148 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000150 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Paul Bakker5c2364c2012-10-01 14:41:15 +0000152 GET_UINT32_BE( W[ 0], data, 0 );
153 GET_UINT32_BE( W[ 1], data, 4 );
154 GET_UINT32_BE( W[ 2], data, 8 );
155 GET_UINT32_BE( W[ 3], data, 12 );
156 GET_UINT32_BE( W[ 4], data, 16 );
157 GET_UINT32_BE( W[ 5], data, 20 );
158 GET_UINT32_BE( W[ 6], data, 24 );
159 GET_UINT32_BE( W[ 7], data, 28 );
160 GET_UINT32_BE( W[ 8], data, 32 );
161 GET_UINT32_BE( W[ 9], data, 36 );
162 GET_UINT32_BE( W[10], data, 40 );
163 GET_UINT32_BE( W[11], data, 44 );
164 GET_UINT32_BE( W[12], data, 48 );
165 GET_UINT32_BE( W[13], data, 52 );
166 GET_UINT32_BE( W[14], data, 56 );
167 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
170
171#define R(t) \
172( \
Paul Bakker66d5d072014-06-17 16:39:18 +0200173 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
174 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 ( W[t & 0x0F] = S(temp,1) ) \
176)
177
178#define P(a,b,c,d,e,x) \
179{ \
180 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
181}
182
183 A = ctx->state[0];
184 B = ctx->state[1];
185 C = ctx->state[2];
186 D = ctx->state[3];
187 E = ctx->state[4];
188
189#define F(x,y,z) (z ^ (x & (y ^ z)))
190#define K 0x5A827999
191
192 P( A, B, C, D, E, W[0] );
193 P( E, A, B, C, D, W[1] );
194 P( D, E, A, B, C, W[2] );
195 P( C, D, E, A, B, W[3] );
196 P( B, C, D, E, A, W[4] );
197 P( A, B, C, D, E, W[5] );
198 P( E, A, B, C, D, W[6] );
199 P( D, E, A, B, C, W[7] );
200 P( C, D, E, A, B, W[8] );
201 P( B, C, D, E, A, W[9] );
202 P( A, B, C, D, E, W[10] );
203 P( E, A, B, C, D, W[11] );
204 P( D, E, A, B, C, W[12] );
205 P( C, D, E, A, B, W[13] );
206 P( B, C, D, E, A, W[14] );
207 P( A, B, C, D, E, W[15] );
208 P( E, A, B, C, D, R(16) );
209 P( D, E, A, B, C, R(17) );
210 P( C, D, E, A, B, R(18) );
211 P( B, C, D, E, A, R(19) );
212
213#undef K
214#undef F
215
216#define F(x,y,z) (x ^ y ^ z)
217#define K 0x6ED9EBA1
218
219 P( A, B, C, D, E, R(20) );
220 P( E, A, B, C, D, R(21) );
221 P( D, E, A, B, C, R(22) );
222 P( C, D, E, A, B, R(23) );
223 P( B, C, D, E, A, R(24) );
224 P( A, B, C, D, E, R(25) );
225 P( E, A, B, C, D, R(26) );
226 P( D, E, A, B, C, R(27) );
227 P( C, D, E, A, B, R(28) );
228 P( B, C, D, E, A, R(29) );
229 P( A, B, C, D, E, R(30) );
230 P( E, A, B, C, D, R(31) );
231 P( D, E, A, B, C, R(32) );
232 P( C, D, E, A, B, R(33) );
233 P( B, C, D, E, A, R(34) );
234 P( A, B, C, D, E, R(35) );
235 P( E, A, B, C, D, R(36) );
236 P( D, E, A, B, C, R(37) );
237 P( C, D, E, A, B, R(38) );
238 P( B, C, D, E, A, R(39) );
239
240#undef K
241#undef F
242
243#define F(x,y,z) ((x & y) | (z & (x | y)))
244#define K 0x8F1BBCDC
245
246 P( A, B, C, D, E, R(40) );
247 P( E, A, B, C, D, R(41) );
248 P( D, E, A, B, C, R(42) );
249 P( C, D, E, A, B, R(43) );
250 P( B, C, D, E, A, R(44) );
251 P( A, B, C, D, E, R(45) );
252 P( E, A, B, C, D, R(46) );
253 P( D, E, A, B, C, R(47) );
254 P( C, D, E, A, B, R(48) );
255 P( B, C, D, E, A, R(49) );
256 P( A, B, C, D, E, R(50) );
257 P( E, A, B, C, D, R(51) );
258 P( D, E, A, B, C, R(52) );
259 P( C, D, E, A, B, R(53) );
260 P( B, C, D, E, A, R(54) );
261 P( A, B, C, D, E, R(55) );
262 P( E, A, B, C, D, R(56) );
263 P( D, E, A, B, C, R(57) );
264 P( C, D, E, A, B, R(58) );
265 P( B, C, D, E, A, R(59) );
266
267#undef K
268#undef F
269
270#define F(x,y,z) (x ^ y ^ z)
271#define K 0xCA62C1D6
272
273 P( A, B, C, D, E, R(60) );
274 P( E, A, B, C, D, R(61) );
275 P( D, E, A, B, C, R(62) );
276 P( C, D, E, A, B, R(63) );
277 P( B, C, D, E, A, R(64) );
278 P( A, B, C, D, E, R(65) );
279 P( E, A, B, C, D, R(66) );
280 P( D, E, A, B, C, R(67) );
281 P( C, D, E, A, B, R(68) );
282 P( B, C, D, E, A, R(69) );
283 P( A, B, C, D, E, R(70) );
284 P( E, A, B, C, D, R(71) );
285 P( D, E, A, B, C, R(72) );
286 P( C, D, E, A, B, R(73) );
287 P( B, C, D, E, A, R(74) );
288 P( A, B, C, D, E, R(75) );
289 P( E, A, B, C, D, R(76) );
290 P( D, E, A, B, C, R(77) );
291 P( C, D, E, A, B, R(78) );
292 P( B, C, D, E, A, R(79) );
293
294#undef K
295#undef F
296
297 ctx->state[0] += A;
298 ctx->state[1] += B;
299 ctx->state[2] += C;
300 ctx->state[3] += D;
301 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100302
303 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304}
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000305
306#if !defined(MBEDTLS_DEPRECATED_REMOVED)
307void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
308 const unsigned char data[64] )
309{
310 mbedtls_internal_sha1_process( ctx, data );
311}
312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315/*
316 * SHA-1 process buffer
317 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100318int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100319 const unsigned char *input,
320 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100322 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000323 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000324 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Brian White12895d12014-04-11 11:29:42 -0400326 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100327 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 left = ctx->total[0] & 0x3F;
330 fill = 64 - left;
331
Paul Bakker5c2364c2012-10-01 14:41:15 +0000332 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 ctx->total[0] &= 0xFFFFFFFF;
334
Paul Bakker5c2364c2012-10-01 14:41:15 +0000335 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 ctx->total[1]++;
337
338 if( left && ilen >= fill )
339 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200340 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100341
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100342 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100343 return( ret );
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 input += fill;
346 ilen -= fill;
347 left = 0;
348 }
349
350 while( ilen >= 64 )
351 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100352 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100353 return( ret );
354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 input += 64;
356 ilen -= 64;
357 }
358
359 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200360 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100361
362 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363}
364
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000365#if !defined(MBEDTLS_DEPRECATED_REMOVED)
366void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
367 const unsigned char *input,
368 size_t ilen )
369{
370 mbedtls_sha1_update_ret( ctx, input, ilen );
371}
372#endif
373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374/*
375 * SHA-1 final digest
376 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100378 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000379{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100380 int ret;
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200381 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000382 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200384 /*
385 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
386 */
387 used = ctx->total[0] & 0x3F;
388
389 ctx->buffer[used++] = 0x80;
390
391 if( used <= 56 )
392 {
393 /* Enough room for padding + length in current block */
394 memset( ctx->buffer + used, 0, 56 - used );
395 }
396 else
397 {
398 /* We'll need an extra block */
399 memset( ctx->buffer + used, 0, 64 - used );
400
401 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
402 return( ret );
403
404 memset( ctx->buffer, 0, 56 );
405 }
406
407 /*
408 * Add message length
409 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 high = ( ctx->total[0] >> 29 )
411 | ( ctx->total[1] << 3 );
412 low = ( ctx->total[0] << 3 );
413
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200414 PUT_UINT32_BE( high, ctx->buffer, 56 );
415 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200417 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200420 /*
421 * Output final state
422 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000423 PUT_UINT32_BE( ctx->state[0], output, 0 );
424 PUT_UINT32_BE( ctx->state[1], output, 4 );
425 PUT_UINT32_BE( ctx->state[2], output, 8 );
426 PUT_UINT32_BE( ctx->state[3], output, 12 );
427 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100428
429 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430}
431
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000432#if !defined(MBEDTLS_DEPRECATED_REMOVED)
433void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
434 unsigned char output[20] )
435{
436 mbedtls_sha1_finish_ret( ctx, output );
437}
438#endif
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200441
Paul Bakker5121ce52009-01-03 21:22:43 +0000442/*
443 * output = SHA-1( input buffer )
444 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100445int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100446 size_t ilen,
447 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000448{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100449 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100455 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100458 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100461 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100462
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100466 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467}
468
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000469#if !defined(MBEDTLS_DEPRECATED_REMOVED)
470void mbedtls_sha1( const unsigned char *input,
471 size_t ilen,
472 unsigned char output[20] )
473{
474 mbedtls_sha1_ret( input, ilen, output );
475}
476#endif
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000479/*
480 * FIPS-180-1 test vectors
481 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000482static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
484 { "abc" },
485 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
486 { "" }
487};
488
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100489static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000490{
491 3, 56, 1000
492};
493
494static const unsigned char sha1_test_sum[3][20] =
495{
496 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
497 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
498 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
499 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
500 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
501 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
502};
503
504/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * Checkup routine
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508{
Paul Bakker5b4af392014-06-26 12:09:34 +0200509 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 unsigned char buf[1024];
511 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200515
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 /*
517 * SHA-1
518 */
519 for( i = 0; i < 3; i++ )
520 {
521 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100524 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100525 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 if( i == 2 )
528 {
529 memset( buf, 'a', buflen = 1000 );
530
531 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100532 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100533 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100534 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100535 goto fail;
536 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 }
538 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100539 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100540 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100541 sha1_test_buflen[i] );
542 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100543 goto fail;
544 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100546 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100547 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100550 {
551 ret = 1;
552 goto fail;
553 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100562 goto exit;
563
564fail:
565 if( verbose != 0 )
566 mbedtls_printf( "failed\n" );
567
Paul Bakker5b4af392014-06-26 12:09:34 +0200568exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200570
571 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572}
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_SHA1_C */