blob: 9a21bcb2aed8d62294fa16fb636ed4157c2712b2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, 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
Paul Bakker5121ce52009-01-03 21:22:43 +000092/*
93 * SHA-1 context setup
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000096{
97 ctx->total[0] = 0;
98 ctx->total[1] = 0;
99
100 ctx->state[0] = 0x67452301;
101 ctx->state[1] = 0xEFCDAB89;
102 ctx->state[2] = 0x98BADCFE;
103 ctx->state[3] = 0x10325476;
104 ctx->state[4] = 0xC3D2E1F0;
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
108void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000110 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker5c2364c2012-10-01 14:41:15 +0000112 GET_UINT32_BE( W[ 0], data, 0 );
113 GET_UINT32_BE( W[ 1], data, 4 );
114 GET_UINT32_BE( W[ 2], data, 8 );
115 GET_UINT32_BE( W[ 3], data, 12 );
116 GET_UINT32_BE( W[ 4], data, 16 );
117 GET_UINT32_BE( W[ 5], data, 20 );
118 GET_UINT32_BE( W[ 6], data, 24 );
119 GET_UINT32_BE( W[ 7], data, 28 );
120 GET_UINT32_BE( W[ 8], data, 32 );
121 GET_UINT32_BE( W[ 9], data, 36 );
122 GET_UINT32_BE( W[10], data, 40 );
123 GET_UINT32_BE( W[11], data, 44 );
124 GET_UINT32_BE( W[12], data, 48 );
125 GET_UINT32_BE( W[13], data, 52 );
126 GET_UINT32_BE( W[14], data, 56 );
127 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
130
131#define R(t) \
132( \
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
134 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 ( W[t & 0x0F] = S(temp,1) ) \
136)
137
138#define P(a,b,c,d,e,x) \
139{ \
140 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
141}
142
143 A = ctx->state[0];
144 B = ctx->state[1];
145 C = ctx->state[2];
146 D = ctx->state[3];
147 E = ctx->state[4];
148
149#define F(x,y,z) (z ^ (x & (y ^ z)))
150#define K 0x5A827999
151
152 P( A, B, C, D, E, W[0] );
153 P( E, A, B, C, D, W[1] );
154 P( D, E, A, B, C, W[2] );
155 P( C, D, E, A, B, W[3] );
156 P( B, C, D, E, A, W[4] );
157 P( A, B, C, D, E, W[5] );
158 P( E, A, B, C, D, W[6] );
159 P( D, E, A, B, C, W[7] );
160 P( C, D, E, A, B, W[8] );
161 P( B, C, D, E, A, W[9] );
162 P( A, B, C, D, E, W[10] );
163 P( E, A, B, C, D, W[11] );
164 P( D, E, A, B, C, W[12] );
165 P( C, D, E, A, B, W[13] );
166 P( B, C, D, E, A, W[14] );
167 P( A, B, C, D, E, W[15] );
168 P( E, A, B, C, D, R(16) );
169 P( D, E, A, B, C, R(17) );
170 P( C, D, E, A, B, R(18) );
171 P( B, C, D, E, A, R(19) );
172
173#undef K
174#undef F
175
176#define F(x,y,z) (x ^ y ^ z)
177#define K 0x6ED9EBA1
178
179 P( A, B, C, D, E, R(20) );
180 P( E, A, B, C, D, R(21) );
181 P( D, E, A, B, C, R(22) );
182 P( C, D, E, A, B, R(23) );
183 P( B, C, D, E, A, R(24) );
184 P( A, B, C, D, E, R(25) );
185 P( E, A, B, C, D, R(26) );
186 P( D, E, A, B, C, R(27) );
187 P( C, D, E, A, B, R(28) );
188 P( B, C, D, E, A, R(29) );
189 P( A, B, C, D, E, R(30) );
190 P( E, A, B, C, D, R(31) );
191 P( D, E, A, B, C, R(32) );
192 P( C, D, E, A, B, R(33) );
193 P( B, C, D, E, A, R(34) );
194 P( A, B, C, D, E, R(35) );
195 P( E, A, B, C, D, R(36) );
196 P( D, E, A, B, C, R(37) );
197 P( C, D, E, A, B, R(38) );
198 P( B, C, D, E, A, R(39) );
199
200#undef K
201#undef F
202
203#define F(x,y,z) ((x & y) | (z & (x | y)))
204#define K 0x8F1BBCDC
205
206 P( A, B, C, D, E, R(40) );
207 P( E, A, B, C, D, R(41) );
208 P( D, E, A, B, C, R(42) );
209 P( C, D, E, A, B, R(43) );
210 P( B, C, D, E, A, R(44) );
211 P( A, B, C, D, E, R(45) );
212 P( E, A, B, C, D, R(46) );
213 P( D, E, A, B, C, R(47) );
214 P( C, D, E, A, B, R(48) );
215 P( B, C, D, E, A, R(49) );
216 P( A, B, C, D, E, R(50) );
217 P( E, A, B, C, D, R(51) );
218 P( D, E, A, B, C, R(52) );
219 P( C, D, E, A, B, R(53) );
220 P( B, C, D, E, A, R(54) );
221 P( A, B, C, D, E, R(55) );
222 P( E, A, B, C, D, R(56) );
223 P( D, E, A, B, C, R(57) );
224 P( C, D, E, A, B, R(58) );
225 P( B, C, D, E, A, R(59) );
226
227#undef K
228#undef F
229
230#define F(x,y,z) (x ^ y ^ z)
231#define K 0xCA62C1D6
232
233 P( A, B, C, D, E, R(60) );
234 P( E, A, B, C, D, R(61) );
235 P( D, E, A, B, C, R(62) );
236 P( C, D, E, A, B, R(63) );
237 P( B, C, D, E, A, R(64) );
238 P( A, B, C, D, E, R(65) );
239 P( E, A, B, C, D, R(66) );
240 P( D, E, A, B, C, R(67) );
241 P( C, D, E, A, B, R(68) );
242 P( B, C, D, E, A, R(69) );
243 P( A, B, C, D, E, R(70) );
244 P( E, A, B, C, D, R(71) );
245 P( D, E, A, B, C, R(72) );
246 P( C, D, E, A, B, R(73) );
247 P( B, C, D, E, A, R(74) );
248 P( A, B, C, D, E, R(75) );
249 P( E, A, B, C, D, R(76) );
250 P( D, E, A, B, C, R(77) );
251 P( C, D, E, A, B, R(78) );
252 P( B, C, D, E, A, R(79) );
253
254#undef K
255#undef F
256
257 ctx->state[0] += A;
258 ctx->state[1] += B;
259 ctx->state[2] += C;
260 ctx->state[3] += D;
261 ctx->state[4] += E;
262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265/*
266 * SHA-1 process buffer
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000269{
Paul Bakker23986e52011-04-24 08:57:21 +0000270 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000271 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Brian White12895d12014-04-11 11:29:42 -0400273 if( ilen == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 return;
275
276 left = ctx->total[0] & 0x3F;
277 fill = 64 - left;
278
Paul Bakker5c2364c2012-10-01 14:41:15 +0000279 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 ctx->total[0] &= 0xFFFFFFFF;
281
Paul Bakker5c2364c2012-10-01 14:41:15 +0000282 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 ctx->total[1]++;
284
285 if( left && ilen >= fill )
286 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200287 memcpy( (void *) (ctx->buffer + left), input, fill );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_sha1_process( ctx, ctx->buffer );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 input += fill;
290 ilen -= fill;
291 left = 0;
292 }
293
294 while( ilen >= 64 )
295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_sha1_process( ctx, input );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 input += 64;
298 ilen -= 64;
299 }
300
301 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200302 memcpy( (void *) (ctx->buffer + left), input, ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303}
304
305static const unsigned char sha1_padding[64] =
306{
307 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
311};
312
313/*
314 * SHA-1 final digest
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000318 uint32_t last, padn;
319 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 unsigned char msglen[8];
321
322 high = ( ctx->total[0] >> 29 )
323 | ( ctx->total[1] << 3 );
324 low = ( ctx->total[0] << 3 );
325
Paul Bakker5c2364c2012-10-01 14:41:15 +0000326 PUT_UINT32_BE( high, msglen, 0 );
327 PUT_UINT32_BE( low, msglen, 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 last = ctx->total[0] & 0x3F;
330 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_sha1_update( ctx, sha1_padding, padn );
333 mbedtls_sha1_update( ctx, msglen, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Paul Bakker5c2364c2012-10-01 14:41:15 +0000335 PUT_UINT32_BE( ctx->state[0], output, 0 );
336 PUT_UINT32_BE( ctx->state[1], output, 4 );
337 PUT_UINT32_BE( ctx->state[2], output, 8 );
338 PUT_UINT32_BE( ctx->state[3], output, 12 );
339 PUT_UINT32_BE( ctx->state[4], output, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340}
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200343
Paul Bakker5121ce52009-01-03 21:22:43 +0000344/*
345 * output = SHA-1( input buffer )
346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_sha1_init( &ctx );
352 mbedtls_sha1_starts( &ctx );
353 mbedtls_sha1_update( &ctx, input, ilen );
354 mbedtls_sha1_finish( &ctx, output );
355 mbedtls_sha1_free( &ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356}
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000359/*
360 * FIPS-180-1 test vectors
361 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000362static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000363{
364 { "abc" },
365 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
366 { "" }
367};
368
369static const int sha1_test_buflen[3] =
370{
371 3, 56, 1000
372};
373
374static const unsigned char sha1_test_sum[3][20] =
375{
376 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
377 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
378 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
379 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
380 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
381 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
382};
383
384/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * Checkup routine
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000388{
Paul Bakker5b4af392014-06-26 12:09:34 +0200389 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 unsigned char buf[1024];
391 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200395
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 /*
397 * SHA-1
398 */
399 for( i = 0; i < 3; i++ )
400 {
401 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_sha1_starts( &ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 if( i == 2 )
407 {
408 memset( buf, 'a', buflen = 1000 );
409
410 for( j = 0; j < 1000; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_sha1_update( &ctx, buf, buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 }
413 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_sha1_update( &ctx, sha1_test_buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 sha1_test_buflen[i] );
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_sha1_finish( &ctx, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
420 {
421 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
Paul Bakker5b4af392014-06-26 12:09:34 +0200424 ret = 1;
425 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 }
427
428 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 }
431
432 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
Paul Bakker5b4af392014-06-26 12:09:34 +0200435exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200437
438 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439}
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#endif /* MBEDTLS_SHA1_C */