blob: bab6087c4e46c456abb010defd910c6f7af1ed78 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SHA-1 standard was published by NIST in 1993.
23 *
24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_SELF_TEST)
41#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010043#else
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define mbedtls_printf printf
46#endif /* MBEDTLS_PLATFORM_C */
47#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010048
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020049#if !defined(MBEDTLS_SHA1_ALT)
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
52 * 32-bit integer manipulation macros (big endian)
53 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000054#ifndef GET_UINT32_BE
55#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000056{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
60 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
62#endif
63
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef PUT_UINT32_BE
65#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000066{ \
67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) ); \
71}
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020077}
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020080{
81 if( ctx == NULL )
82 return;
83
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050084 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020085}
86
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
88 const mbedtls_sha1_context *src )
89{
90 *dst = *src;
91}
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/*
94 * SHA-1 context setup
95 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010096int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 ctx->total[0] = 0;
99 ctx->total[1] = 0;
100
101 ctx->state[0] = 0x67452301;
102 ctx->state[1] = 0xEFCDAB89;
103 ctx->state[2] = 0x98BADCFE;
104 ctx->state[3] = 0x10325476;
105 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100106
107 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108}
109
Jaeden Amero041039f2018-02-19 15:28:08 +0000110#if !defined(MBEDTLS_DEPRECATED_REMOVED)
111void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
112{
113 mbedtls_sha1_starts_ret( ctx );
114}
115#endif
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100118int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
119 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000121 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Paul Bakker5c2364c2012-10-01 14:41:15 +0000123 GET_UINT32_BE( W[ 0], data, 0 );
124 GET_UINT32_BE( W[ 1], data, 4 );
125 GET_UINT32_BE( W[ 2], data, 8 );
126 GET_UINT32_BE( W[ 3], data, 12 );
127 GET_UINT32_BE( W[ 4], data, 16 );
128 GET_UINT32_BE( W[ 5], data, 20 );
129 GET_UINT32_BE( W[ 6], data, 24 );
130 GET_UINT32_BE( W[ 7], data, 28 );
131 GET_UINT32_BE( W[ 8], data, 32 );
132 GET_UINT32_BE( W[ 9], data, 36 );
133 GET_UINT32_BE( W[10], data, 40 );
134 GET_UINT32_BE( W[11], data, 44 );
135 GET_UINT32_BE( W[12], data, 48 );
136 GET_UINT32_BE( W[13], data, 52 );
137 GET_UINT32_BE( W[14], data, 56 );
138 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
141
142#define R(t) \
143( \
Paul Bakker66d5d072014-06-17 16:39:18 +0200144 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
145 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 ( W[t & 0x0F] = S(temp,1) ) \
147)
148
149#define P(a,b,c,d,e,x) \
150{ \
151 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
152}
153
154 A = ctx->state[0];
155 B = ctx->state[1];
156 C = ctx->state[2];
157 D = ctx->state[3];
158 E = ctx->state[4];
159
160#define F(x,y,z) (z ^ (x & (y ^ z)))
161#define K 0x5A827999
162
163 P( A, B, C, D, E, W[0] );
164 P( E, A, B, C, D, W[1] );
165 P( D, E, A, B, C, W[2] );
166 P( C, D, E, A, B, W[3] );
167 P( B, C, D, E, A, W[4] );
168 P( A, B, C, D, E, W[5] );
169 P( E, A, B, C, D, W[6] );
170 P( D, E, A, B, C, W[7] );
171 P( C, D, E, A, B, W[8] );
172 P( B, C, D, E, A, W[9] );
173 P( A, B, C, D, E, W[10] );
174 P( E, A, B, C, D, W[11] );
175 P( D, E, A, B, C, W[12] );
176 P( C, D, E, A, B, W[13] );
177 P( B, C, D, E, A, W[14] );
178 P( A, B, C, D, E, W[15] );
179 P( E, A, B, C, D, R(16) );
180 P( D, E, A, B, C, R(17) );
181 P( C, D, E, A, B, R(18) );
182 P( B, C, D, E, A, R(19) );
183
184#undef K
185#undef F
186
187#define F(x,y,z) (x ^ y ^ z)
188#define K 0x6ED9EBA1
189
190 P( A, B, C, D, E, R(20) );
191 P( E, A, B, C, D, R(21) );
192 P( D, E, A, B, C, R(22) );
193 P( C, D, E, A, B, R(23) );
194 P( B, C, D, E, A, R(24) );
195 P( A, B, C, D, E, R(25) );
196 P( E, A, B, C, D, R(26) );
197 P( D, E, A, B, C, R(27) );
198 P( C, D, E, A, B, R(28) );
199 P( B, C, D, E, A, R(29) );
200 P( A, B, C, D, E, R(30) );
201 P( E, A, B, C, D, R(31) );
202 P( D, E, A, B, C, R(32) );
203 P( C, D, E, A, B, R(33) );
204 P( B, C, D, E, A, R(34) );
205 P( A, B, C, D, E, R(35) );
206 P( E, A, B, C, D, R(36) );
207 P( D, E, A, B, C, R(37) );
208 P( C, D, E, A, B, R(38) );
209 P( B, C, D, E, A, R(39) );
210
211#undef K
212#undef F
213
214#define F(x,y,z) ((x & y) | (z & (x | y)))
215#define K 0x8F1BBCDC
216
217 P( A, B, C, D, E, R(40) );
218 P( E, A, B, C, D, R(41) );
219 P( D, E, A, B, C, R(42) );
220 P( C, D, E, A, B, R(43) );
221 P( B, C, D, E, A, R(44) );
222 P( A, B, C, D, E, R(45) );
223 P( E, A, B, C, D, R(46) );
224 P( D, E, A, B, C, R(47) );
225 P( C, D, E, A, B, R(48) );
226 P( B, C, D, E, A, R(49) );
227 P( A, B, C, D, E, R(50) );
228 P( E, A, B, C, D, R(51) );
229 P( D, E, A, B, C, R(52) );
230 P( C, D, E, A, B, R(53) );
231 P( B, C, D, E, A, R(54) );
232 P( A, B, C, D, E, R(55) );
233 P( E, A, B, C, D, R(56) );
234 P( D, E, A, B, C, R(57) );
235 P( C, D, E, A, B, R(58) );
236 P( B, C, D, E, A, R(59) );
237
238#undef K
239#undef F
240
241#define F(x,y,z) (x ^ y ^ z)
242#define K 0xCA62C1D6
243
244 P( A, B, C, D, E, R(60) );
245 P( E, A, B, C, D, R(61) );
246 P( D, E, A, B, C, R(62) );
247 P( C, D, E, A, B, R(63) );
248 P( B, C, D, E, A, R(64) );
249 P( A, B, C, D, E, R(65) );
250 P( E, A, B, C, D, R(66) );
251 P( D, E, A, B, C, R(67) );
252 P( C, D, E, A, B, R(68) );
253 P( B, C, D, E, A, R(69) );
254 P( A, B, C, D, E, R(70) );
255 P( E, A, B, C, D, R(71) );
256 P( D, E, A, B, C, R(72) );
257 P( C, D, E, A, B, R(73) );
258 P( B, C, D, E, A, R(74) );
259 P( A, B, C, D, E, R(75) );
260 P( E, A, B, C, D, R(76) );
261 P( D, E, A, B, C, R(77) );
262 P( C, D, E, A, B, R(78) );
263 P( B, C, D, E, A, R(79) );
264
265#undef K
266#undef F
267
268 ctx->state[0] += A;
269 ctx->state[1] += B;
270 ctx->state[2] += C;
271 ctx->state[3] += D;
272 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100273
274 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275}
Jaeden Amero041039f2018-02-19 15:28:08 +0000276
277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
278void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
279 const unsigned char data[64] )
280{
281 mbedtls_internal_sha1_process( ctx, data );
282}
283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/*
287 * SHA-1 process buffer
288 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100290 const unsigned char *input,
291 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100293 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000294 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000295 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Brian White12895d12014-04-11 11:29:42 -0400297 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100298 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 left = ctx->total[0] & 0x3F;
301 fill = 64 - left;
302
Paul Bakker5c2364c2012-10-01 14:41:15 +0000303 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 ctx->total[0] &= 0xFFFFFFFF;
305
Paul Bakker5c2364c2012-10-01 14:41:15 +0000306 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 ctx->total[1]++;
308
309 if( left && ilen >= fill )
310 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200311 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100312
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100313 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100314 return( ret );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 input += fill;
317 ilen -= fill;
318 left = 0;
319 }
320
321 while( ilen >= 64 )
322 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100323 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100324 return( ret );
325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 input += 64;
327 ilen -= 64;
328 }
329
330 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200331 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100332
333 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334}
335
Jaeden Amero041039f2018-02-19 15:28:08 +0000336#if !defined(MBEDTLS_DEPRECATED_REMOVED)
337void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
338 const unsigned char *input,
339 size_t ilen )
340{
341 mbedtls_sha1_update_ret( ctx, input, ilen );
342}
343#endif
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/*
346 * SHA-1 final digest
347 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100348int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100349 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000350{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100351 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200352 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000353 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200355 /*
356 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
357 */
358 used = ctx->total[0] & 0x3F;
359
360 ctx->buffer[used++] = 0x80;
361
362 if( used <= 56 )
363 {
364 /* Enough room for padding + length in current block */
365 memset( ctx->buffer + used, 0, 56 - used );
366 }
367 else
368 {
369 /* We'll need an extra block */
370 memset( ctx->buffer + used, 0, 64 - used );
371
372 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
373 return( ret );
374
375 memset( ctx->buffer, 0, 56 );
376 }
377
378 /*
379 * Add message length
380 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 high = ( ctx->total[0] >> 29 )
382 | ( ctx->total[1] << 3 );
383 low = ( ctx->total[0] << 3 );
384
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200385 PUT_UINT32_BE( high, ctx->buffer, 56 );
386 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200388 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100389 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200391 /*
392 * Output final state
393 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000394 PUT_UINT32_BE( ctx->state[0], output, 0 );
395 PUT_UINT32_BE( ctx->state[1], output, 4 );
396 PUT_UINT32_BE( ctx->state[2], output, 8 );
397 PUT_UINT32_BE( ctx->state[3], output, 12 );
398 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100399
400 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401}
402
Jaeden Amero041039f2018-02-19 15:28:08 +0000403#if !defined(MBEDTLS_DEPRECATED_REMOVED)
404void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
405 unsigned char output[20] )
406{
407 mbedtls_sha1_finish_ret( ctx, output );
408}
409#endif
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413/*
414 * output = SHA-1( input buffer )
415 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100416int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100417 size_t ilen,
418 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000419{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100420 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100425 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100426 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100428 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100429 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100431 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100432 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100433
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100434exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100436
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438}
439
Jaeden Amero041039f2018-02-19 15:28:08 +0000440#if !defined(MBEDTLS_DEPRECATED_REMOVED)
441void mbedtls_sha1( const unsigned char *input,
442 size_t ilen,
443 unsigned char output[20] )
444{
445 mbedtls_sha1_ret( input, ilen, output );
446}
447#endif
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000450/*
451 * FIPS-180-1 test vectors
452 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000453static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000454{
455 { "abc" },
456 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
457 { "" }
458};
459
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100460static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
462 3, 56, 1000
463};
464
465static const unsigned char sha1_test_sum[3][20] =
466{
467 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
468 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
469 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
470 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
471 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
472 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
473};
474
475/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * Checkup routine
477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000479{
Paul Bakker5b4af392014-06-26 12:09:34 +0200480 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 unsigned char buf[1024];
482 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200486
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 /*
488 * SHA-1
489 */
490 for( i = 0; i < 3; i++ )
491 {
492 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100495 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100496 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 if( i == 2 )
499 {
500 memset( buf, 'a', buflen = 1000 );
501
502 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100503 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100505 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100506 goto fail;
507 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 }
509 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100510 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100511 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100512 sha1_test_buflen[i] );
513 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100514 goto fail;
515 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100517 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100518 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100521 {
522 ret = 1;
523 goto fail;
524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 }
529
530 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100533 goto exit;
534
535fail:
536 if( verbose != 0 )
537 mbedtls_printf( "failed\n" );
538
Paul Bakker5b4af392014-06-26 12:09:34 +0200539exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200541
542 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543}
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_SHA1_C */