blob: 14331b3ac5c7bcbf7941ff90ca3289c5b79a156c [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
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SHA-1 standard was published by NIST in 1993.
24 *
25 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/sha1.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
Paul Bakker34617722014-06-13 17:20:13 +020049/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020051 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
52}
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020055
Paul Bakker5121ce52009-01-03 21:22:43 +000056/*
57 * 32-bit integer manipulation macros (big endian)
58 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000059#ifndef GET_UINT32_BE
60#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000061{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000062 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000066}
67#endif
68
Paul Bakker5c2364c2012-10-01 14:41:15 +000069#ifndef PUT_UINT32_BE
70#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000071{ \
72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76}
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020082}
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020085{
86 if( ctx == NULL )
87 return;
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020090}
91
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020092void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
93 const mbedtls_sha1_context *src )
94{
95 *dst = *src;
96}
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/*
99 * SHA-1 context setup
100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 ctx->total[0] = 0;
104 ctx->total[1] = 0;
105
106 ctx->state[0] = 0x67452301;
107 ctx->state[1] = 0xEFCDAB89;
108 ctx->state[2] = 0x98BADCFE;
109 ctx->state[3] = 0x10325476;
110 ctx->state[4] = 0xC3D2E1F0;
111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
114void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000116 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakker5c2364c2012-10-01 14:41:15 +0000118 GET_UINT32_BE( W[ 0], data, 0 );
119 GET_UINT32_BE( W[ 1], data, 4 );
120 GET_UINT32_BE( W[ 2], data, 8 );
121 GET_UINT32_BE( W[ 3], data, 12 );
122 GET_UINT32_BE( W[ 4], data, 16 );
123 GET_UINT32_BE( W[ 5], data, 20 );
124 GET_UINT32_BE( W[ 6], data, 24 );
125 GET_UINT32_BE( W[ 7], data, 28 );
126 GET_UINT32_BE( W[ 8], data, 32 );
127 GET_UINT32_BE( W[ 9], data, 36 );
128 GET_UINT32_BE( W[10], data, 40 );
129 GET_UINT32_BE( W[11], data, 44 );
130 GET_UINT32_BE( W[12], data, 48 );
131 GET_UINT32_BE( W[13], data, 52 );
132 GET_UINT32_BE( W[14], data, 56 );
133 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
136
137#define R(t) \
138( \
Paul Bakker66d5d072014-06-17 16:39:18 +0200139 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
140 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 ( W[t & 0x0F] = S(temp,1) ) \
142)
143
144#define P(a,b,c,d,e,x) \
145{ \
146 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
147}
148
149 A = ctx->state[0];
150 B = ctx->state[1];
151 C = ctx->state[2];
152 D = ctx->state[3];
153 E = ctx->state[4];
154
155#define F(x,y,z) (z ^ (x & (y ^ z)))
156#define K 0x5A827999
157
158 P( A, B, C, D, E, W[0] );
159 P( E, A, B, C, D, W[1] );
160 P( D, E, A, B, C, W[2] );
161 P( C, D, E, A, B, W[3] );
162 P( B, C, D, E, A, W[4] );
163 P( A, B, C, D, E, W[5] );
164 P( E, A, B, C, D, W[6] );
165 P( D, E, A, B, C, W[7] );
166 P( C, D, E, A, B, W[8] );
167 P( B, C, D, E, A, W[9] );
168 P( A, B, C, D, E, W[10] );
169 P( E, A, B, C, D, W[11] );
170 P( D, E, A, B, C, W[12] );
171 P( C, D, E, A, B, W[13] );
172 P( B, C, D, E, A, W[14] );
173 P( A, B, C, D, E, W[15] );
174 P( E, A, B, C, D, R(16) );
175 P( D, E, A, B, C, R(17) );
176 P( C, D, E, A, B, R(18) );
177 P( B, C, D, E, A, R(19) );
178
179#undef K
180#undef F
181
182#define F(x,y,z) (x ^ y ^ z)
183#define K 0x6ED9EBA1
184
185 P( A, B, C, D, E, R(20) );
186 P( E, A, B, C, D, R(21) );
187 P( D, E, A, B, C, R(22) );
188 P( C, D, E, A, B, R(23) );
189 P( B, C, D, E, A, R(24) );
190 P( A, B, C, D, E, R(25) );
191 P( E, A, B, C, D, R(26) );
192 P( D, E, A, B, C, R(27) );
193 P( C, D, E, A, B, R(28) );
194 P( B, C, D, E, A, R(29) );
195 P( A, B, C, D, E, R(30) );
196 P( E, A, B, C, D, R(31) );
197 P( D, E, A, B, C, R(32) );
198 P( C, D, E, A, B, R(33) );
199 P( B, C, D, E, A, R(34) );
200 P( A, B, C, D, E, R(35) );
201 P( E, A, B, C, D, R(36) );
202 P( D, E, A, B, C, R(37) );
203 P( C, D, E, A, B, R(38) );
204 P( B, C, D, E, A, R(39) );
205
206#undef K
207#undef F
208
209#define F(x,y,z) ((x & y) | (z & (x | y)))
210#define K 0x8F1BBCDC
211
212 P( A, B, C, D, E, R(40) );
213 P( E, A, B, C, D, R(41) );
214 P( D, E, A, B, C, R(42) );
215 P( C, D, E, A, B, R(43) );
216 P( B, C, D, E, A, R(44) );
217 P( A, B, C, D, E, R(45) );
218 P( E, A, B, C, D, R(46) );
219 P( D, E, A, B, C, R(47) );
220 P( C, D, E, A, B, R(48) );
221 P( B, C, D, E, A, R(49) );
222 P( A, B, C, D, E, R(50) );
223 P( E, A, B, C, D, R(51) );
224 P( D, E, A, B, C, R(52) );
225 P( C, D, E, A, B, R(53) );
226 P( B, C, D, E, A, R(54) );
227 P( A, B, C, D, E, R(55) );
228 P( E, A, B, C, D, R(56) );
229 P( D, E, A, B, C, R(57) );
230 P( C, D, E, A, B, R(58) );
231 P( B, C, D, E, A, R(59) );
232
233#undef K
234#undef F
235
236#define F(x,y,z) (x ^ y ^ z)
237#define K 0xCA62C1D6
238
239 P( A, B, C, D, E, R(60) );
240 P( E, A, B, C, D, R(61) );
241 P( D, E, A, B, C, R(62) );
242 P( C, D, E, A, B, R(63) );
243 P( B, C, D, E, A, R(64) );
244 P( A, B, C, D, E, R(65) );
245 P( E, A, B, C, D, R(66) );
246 P( D, E, A, B, C, R(67) );
247 P( C, D, E, A, B, R(68) );
248 P( B, C, D, E, A, R(69) );
249 P( A, B, C, D, E, R(70) );
250 P( E, A, B, C, D, R(71) );
251 P( D, E, A, B, C, R(72) );
252 P( C, D, E, A, B, R(73) );
253 P( B, C, D, E, A, R(74) );
254 P( A, B, C, D, E, R(75) );
255 P( E, A, B, C, D, R(76) );
256 P( D, E, A, B, C, R(77) );
257 P( C, D, E, A, B, R(78) );
258 P( B, C, D, E, A, R(79) );
259
260#undef K
261#undef F
262
263 ctx->state[0] += A;
264 ctx->state[1] += B;
265 ctx->state[2] += C;
266 ctx->state[3] += D;
267 ctx->state[4] += E;
268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
271/*
272 * SHA-1 process buffer
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275{
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000277 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
Brian White12895d12014-04-11 11:29:42 -0400279 if( ilen == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 return;
281
282 left = ctx->total[0] & 0x3F;
283 fill = 64 - left;
284
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 ctx->total[0] &= 0xFFFFFFFF;
287
Paul Bakker5c2364c2012-10-01 14:41:15 +0000288 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 ctx->total[1]++;
290
291 if( left && ilen >= fill )
292 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200293 memcpy( (void *) (ctx->buffer + left), input, fill );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_sha1_process( ctx, ctx->buffer );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 input += fill;
296 ilen -= fill;
297 left = 0;
298 }
299
300 while( ilen >= 64 )
301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_sha1_process( ctx, input );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 input += 64;
304 ilen -= 64;
305 }
306
307 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200308 memcpy( (void *) (ctx->buffer + left), input, ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309}
310
311static const unsigned char sha1_padding[64] =
312{
313 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
315 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
317};
318
319/*
320 * SHA-1 final digest
321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000324 uint32_t last, padn;
325 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 unsigned char msglen[8];
327
328 high = ( ctx->total[0] >> 29 )
329 | ( ctx->total[1] << 3 );
330 low = ( ctx->total[0] << 3 );
331
Paul Bakker5c2364c2012-10-01 14:41:15 +0000332 PUT_UINT32_BE( high, msglen, 0 );
333 PUT_UINT32_BE( low, msglen, 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 last = ctx->total[0] & 0x3F;
336 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_sha1_update( ctx, sha1_padding, padn );
339 mbedtls_sha1_update( ctx, msglen, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Paul Bakker5c2364c2012-10-01 14:41:15 +0000341 PUT_UINT32_BE( ctx->state[0], output, 0 );
342 PUT_UINT32_BE( ctx->state[1], output, 4 );
343 PUT_UINT32_BE( ctx->state[2], output, 8 );
344 PUT_UINT32_BE( ctx->state[3], output, 12 );
345 PUT_UINT32_BE( ctx->state[4], output, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346}
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200349
Paul Bakker5121ce52009-01-03 21:22:43 +0000350/*
351 * output = SHA-1( input buffer )
352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_sha1_init( &ctx );
358 mbedtls_sha1_starts( &ctx );
359 mbedtls_sha1_update( &ctx, input, ilen );
360 mbedtls_sha1_finish( &ctx, output );
361 mbedtls_sha1_free( &ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000365/*
366 * FIPS-180-1 test vectors
367 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000368static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000369{
370 { "abc" },
371 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
372 { "" }
373};
374
375static const int sha1_test_buflen[3] =
376{
377 3, 56, 1000
378};
379
380static const unsigned char sha1_test_sum[3][20] =
381{
382 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
383 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
384 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
385 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
386 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
387 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
388};
389
390/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * Checkup routine
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000394{
Paul Bakker5b4af392014-06-26 12:09:34 +0200395 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 unsigned char buf[1024];
397 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200401
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 /*
403 * SHA-1
404 */
405 for( i = 0; i < 3; i++ )
406 {
407 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_sha1_starts( &ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 if( i == 2 )
413 {
414 memset( buf, 'a', buflen = 1000 );
415
416 for( j = 0; j < 1000; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_sha1_update( &ctx, buf, buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 }
419 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_sha1_update( &ctx, sha1_test_buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 sha1_test_buflen[i] );
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_sha1_finish( &ctx, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
426 {
427 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Paul Bakker5b4af392014-06-26 12:09:34 +0200430 ret = 1;
431 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 }
433
434 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 }
437
438 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Paul Bakker5b4af392014-06-26 12:09:34 +0200441exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200443
444 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445}
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_SHA1_C */