xref: /reactos/dll/3rdparty/mbedtls/sha1.c (revision cbda039f)
1 /*
2  *  FIPS-180-1 compliant SHA-1 implementation
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  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:
12  *
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.
24  *
25  *  **********
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  */
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 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "mbedtls/config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #if defined(MBEDTLS_SHA1_C)
59 
60 #include "mbedtls/sha1.h"
61 #include "mbedtls/platform_util.h"
62 
63 #include <string.h>
64 
65 #if defined(MBEDTLS_SELF_TEST)
66 #if defined(MBEDTLS_PLATFORM_C)
67 #include "mbedtls/platform.h"
68 #else
69 #include <stdio.h>
70 #define mbedtls_printf printf
71 #endif /* MBEDTLS_PLATFORM_C */
72 #endif /* MBEDTLS_SELF_TEST */
73 
74 #define SHA1_VALIDATE_RET(cond)                             \
75     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
76 
77 #define SHA1_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond )
78 
79 #if !defined(MBEDTLS_SHA1_ALT)
80 
81 /*
82  * 32-bit integer manipulation macros (big endian)
83  */
84 #ifndef GET_UINT32_BE
85 #define GET_UINT32_BE(n,b,i)                            \
86 {                                                       \
87     (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
88         | ( (uint32_t) (b)[(i) + 1] << 16 )             \
89         | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
90         | ( (uint32_t) (b)[(i) + 3]       );            \
91 }
92 #endif
93 
94 #ifndef PUT_UINT32_BE
95 #define PUT_UINT32_BE(n,b,i)                            \
96 {                                                       \
97     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
98     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
99     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
100     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
101 }
102 #endif
103 
mbedtls_sha1_init(mbedtls_sha1_context * ctx)104 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
105 {
106     SHA1_VALIDATE( ctx != NULL );
107 
108     memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
109 }
110 
mbedtls_sha1_free(mbedtls_sha1_context * ctx)111 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
112 {
113     if( ctx == NULL )
114         return;
115 
116     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
117 }
118 
mbedtls_sha1_clone(mbedtls_sha1_context * dst,const mbedtls_sha1_context * src)119 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
120                          const mbedtls_sha1_context *src )
121 {
122     SHA1_VALIDATE( dst != NULL );
123     SHA1_VALIDATE( src != NULL );
124 
125     *dst = *src;
126 }
127 
128 /*
129  * SHA-1 context setup
130  */
mbedtls_sha1_starts_ret(mbedtls_sha1_context * ctx)131 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
132 {
133     SHA1_VALIDATE_RET( ctx != NULL );
134 
135     ctx->total[0] = 0;
136     ctx->total[1] = 0;
137 
138     ctx->state[0] = 0x67452301;
139     ctx->state[1] = 0xEFCDAB89;
140     ctx->state[2] = 0x98BADCFE;
141     ctx->state[3] = 0x10325476;
142     ctx->state[4] = 0xC3D2E1F0;
143 
144     return( 0 );
145 }
146 
147 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_starts(mbedtls_sha1_context * ctx)148 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
149 {
150     mbedtls_sha1_starts_ret( ctx );
151 }
152 #endif
153 
154 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
mbedtls_internal_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])155 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
156                                    const unsigned char data[64] )
157 {
158     struct
159     {
160         uint32_t temp, W[16], A, B, C, D, E;
161     } local;
162 
163     SHA1_VALIDATE_RET( ctx != NULL );
164     SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
165 
166     GET_UINT32_BE( local.W[ 0], data,  0 );
167     GET_UINT32_BE( local.W[ 1], data,  4 );
168     GET_UINT32_BE( local.W[ 2], data,  8 );
169     GET_UINT32_BE( local.W[ 3], data, 12 );
170     GET_UINT32_BE( local.W[ 4], data, 16 );
171     GET_UINT32_BE( local.W[ 5], data, 20 );
172     GET_UINT32_BE( local.W[ 6], data, 24 );
173     GET_UINT32_BE( local.W[ 7], data, 28 );
174     GET_UINT32_BE( local.W[ 8], data, 32 );
175     GET_UINT32_BE( local.W[ 9], data, 36 );
176     GET_UINT32_BE( local.W[10], data, 40 );
177     GET_UINT32_BE( local.W[11], data, 44 );
178     GET_UINT32_BE( local.W[12], data, 48 );
179     GET_UINT32_BE( local.W[13], data, 52 );
180     GET_UINT32_BE( local.W[14], data, 56 );
181     GET_UINT32_BE( local.W[15], data, 60 );
182 
183 #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
184 
185 #define R(t)                                                    \
186     (                                                           \
187         local.temp = local.W[( (t) -  3 ) & 0x0F] ^             \
188                      local.W[( (t) -  8 ) & 0x0F] ^             \
189                      local.W[( (t) - 14 ) & 0x0F] ^             \
190                      local.W[  (t)        & 0x0F],              \
191         ( local.W[(t) & 0x0F] = S(local.temp,1) )               \
192     )
193 
194 #define P(a,b,c,d,e,x)                                          \
195     do                                                          \
196     {                                                           \
197         (e) += S((a),5) + F((b),(c),(d)) + K + (x);             \
198         (b) = S((b),30);                                        \
199     } while( 0 )
200 
201     local.A = ctx->state[0];
202     local.B = ctx->state[1];
203     local.C = ctx->state[2];
204     local.D = ctx->state[3];
205     local.E = ctx->state[4];
206 
207 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
208 #define K 0x5A827999
209 
210     P( local.A, local.B, local.C, local.D, local.E, local.W[0]  );
211     P( local.E, local.A, local.B, local.C, local.D, local.W[1]  );
212     P( local.D, local.E, local.A, local.B, local.C, local.W[2]  );
213     P( local.C, local.D, local.E, local.A, local.B, local.W[3]  );
214     P( local.B, local.C, local.D, local.E, local.A, local.W[4]  );
215     P( local.A, local.B, local.C, local.D, local.E, local.W[5]  );
216     P( local.E, local.A, local.B, local.C, local.D, local.W[6]  );
217     P( local.D, local.E, local.A, local.B, local.C, local.W[7]  );
218     P( local.C, local.D, local.E, local.A, local.B, local.W[8]  );
219     P( local.B, local.C, local.D, local.E, local.A, local.W[9]  );
220     P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
221     P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
222     P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
223     P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
224     P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
225     P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
226     P( local.E, local.A, local.B, local.C, local.D, R(16) );
227     P( local.D, local.E, local.A, local.B, local.C, R(17) );
228     P( local.C, local.D, local.E, local.A, local.B, R(18) );
229     P( local.B, local.C, local.D, local.E, local.A, R(19) );
230 
231 #undef K
232 #undef F
233 
234 #define F(x,y,z) ((x) ^ (y) ^ (z))
235 #define K 0x6ED9EBA1
236 
237     P( local.A, local.B, local.C, local.D, local.E, R(20) );
238     P( local.E, local.A, local.B, local.C, local.D, R(21) );
239     P( local.D, local.E, local.A, local.B, local.C, R(22) );
240     P( local.C, local.D, local.E, local.A, local.B, R(23) );
241     P( local.B, local.C, local.D, local.E, local.A, R(24) );
242     P( local.A, local.B, local.C, local.D, local.E, R(25) );
243     P( local.E, local.A, local.B, local.C, local.D, R(26) );
244     P( local.D, local.E, local.A, local.B, local.C, R(27) );
245     P( local.C, local.D, local.E, local.A, local.B, R(28) );
246     P( local.B, local.C, local.D, local.E, local.A, R(29) );
247     P( local.A, local.B, local.C, local.D, local.E, R(30) );
248     P( local.E, local.A, local.B, local.C, local.D, R(31) );
249     P( local.D, local.E, local.A, local.B, local.C, R(32) );
250     P( local.C, local.D, local.E, local.A, local.B, R(33) );
251     P( local.B, local.C, local.D, local.E, local.A, R(34) );
252     P( local.A, local.B, local.C, local.D, local.E, R(35) );
253     P( local.E, local.A, local.B, local.C, local.D, R(36) );
254     P( local.D, local.E, local.A, local.B, local.C, R(37) );
255     P( local.C, local.D, local.E, local.A, local.B, R(38) );
256     P( local.B, local.C, local.D, local.E, local.A, R(39) );
257 
258 #undef K
259 #undef F
260 
261 #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
262 #define K 0x8F1BBCDC
263 
264     P( local.A, local.B, local.C, local.D, local.E, R(40) );
265     P( local.E, local.A, local.B, local.C, local.D, R(41) );
266     P( local.D, local.E, local.A, local.B, local.C, R(42) );
267     P( local.C, local.D, local.E, local.A, local.B, R(43) );
268     P( local.B, local.C, local.D, local.E, local.A, R(44) );
269     P( local.A, local.B, local.C, local.D, local.E, R(45) );
270     P( local.E, local.A, local.B, local.C, local.D, R(46) );
271     P( local.D, local.E, local.A, local.B, local.C, R(47) );
272     P( local.C, local.D, local.E, local.A, local.B, R(48) );
273     P( local.B, local.C, local.D, local.E, local.A, R(49) );
274     P( local.A, local.B, local.C, local.D, local.E, R(50) );
275     P( local.E, local.A, local.B, local.C, local.D, R(51) );
276     P( local.D, local.E, local.A, local.B, local.C, R(52) );
277     P( local.C, local.D, local.E, local.A, local.B, R(53) );
278     P( local.B, local.C, local.D, local.E, local.A, R(54) );
279     P( local.A, local.B, local.C, local.D, local.E, R(55) );
280     P( local.E, local.A, local.B, local.C, local.D, R(56) );
281     P( local.D, local.E, local.A, local.B, local.C, R(57) );
282     P( local.C, local.D, local.E, local.A, local.B, R(58) );
283     P( local.B, local.C, local.D, local.E, local.A, R(59) );
284 
285 #undef K
286 #undef F
287 
288 #define F(x,y,z) ((x) ^ (y) ^ (z))
289 #define K 0xCA62C1D6
290 
291     P( local.A, local.B, local.C, local.D, local.E, R(60) );
292     P( local.E, local.A, local.B, local.C, local.D, R(61) );
293     P( local.D, local.E, local.A, local.B, local.C, R(62) );
294     P( local.C, local.D, local.E, local.A, local.B, R(63) );
295     P( local.B, local.C, local.D, local.E, local.A, R(64) );
296     P( local.A, local.B, local.C, local.D, local.E, R(65) );
297     P( local.E, local.A, local.B, local.C, local.D, R(66) );
298     P( local.D, local.E, local.A, local.B, local.C, R(67) );
299     P( local.C, local.D, local.E, local.A, local.B, R(68) );
300     P( local.B, local.C, local.D, local.E, local.A, R(69) );
301     P( local.A, local.B, local.C, local.D, local.E, R(70) );
302     P( local.E, local.A, local.B, local.C, local.D, R(71) );
303     P( local.D, local.E, local.A, local.B, local.C, R(72) );
304     P( local.C, local.D, local.E, local.A, local.B, R(73) );
305     P( local.B, local.C, local.D, local.E, local.A, R(74) );
306     P( local.A, local.B, local.C, local.D, local.E, R(75) );
307     P( local.E, local.A, local.B, local.C, local.D, R(76) );
308     P( local.D, local.E, local.A, local.B, local.C, R(77) );
309     P( local.C, local.D, local.E, local.A, local.B, R(78) );
310     P( local.B, local.C, local.D, local.E, local.A, R(79) );
311 
312 #undef K
313 #undef F
314 
315     ctx->state[0] += local.A;
316     ctx->state[1] += local.B;
317     ctx->state[2] += local.C;
318     ctx->state[3] += local.D;
319     ctx->state[4] += local.E;
320 
321     /* Zeroise buffers and variables to clear sensitive data from memory. */
322     mbedtls_platform_zeroize( &local, sizeof( local ) );
323 
324     return( 0 );
325 }
326 
327 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])328 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
329                            const unsigned char data[64] )
330 {
331     mbedtls_internal_sha1_process( ctx, data );
332 }
333 #endif
334 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
335 
336 /*
337  * SHA-1 process buffer
338  */
mbedtls_sha1_update_ret(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)339 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
340                              const unsigned char *input,
341                              size_t ilen )
342 {
343     int ret;
344     size_t fill;
345     uint32_t left;
346 
347     SHA1_VALIDATE_RET( ctx != NULL );
348     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
349 
350     if( ilen == 0 )
351         return( 0 );
352 
353     left = ctx->total[0] & 0x3F;
354     fill = 64 - left;
355 
356     ctx->total[0] += (uint32_t) ilen;
357     ctx->total[0] &= 0xFFFFFFFF;
358 
359     if( ctx->total[0] < (uint32_t) ilen )
360         ctx->total[1]++;
361 
362     if( left && ilen >= fill )
363     {
364         memcpy( (void *) (ctx->buffer + left), input, fill );
365 
366         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
367             return( ret );
368 
369         input += fill;
370         ilen  -= fill;
371         left = 0;
372     }
373 
374     while( ilen >= 64 )
375     {
376         if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
377             return( ret );
378 
379         input += 64;
380         ilen  -= 64;
381     }
382 
383     if( ilen > 0 )
384         memcpy( (void *) (ctx->buffer + left), input, ilen );
385 
386     return( 0 );
387 }
388 
389 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_update(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)390 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
391                           const unsigned char *input,
392                           size_t ilen )
393 {
394     mbedtls_sha1_update_ret( ctx, input, ilen );
395 }
396 #endif
397 
398 /*
399  * SHA-1 final digest
400  */
mbedtls_sha1_finish_ret(mbedtls_sha1_context * ctx,unsigned char output[20])401 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
402                              unsigned char output[20] )
403 {
404     int ret;
405     uint32_t used;
406     uint32_t high, low;
407 
408     SHA1_VALIDATE_RET( ctx != NULL );
409     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
410 
411     /*
412      * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
413      */
414     used = ctx->total[0] & 0x3F;
415 
416     ctx->buffer[used++] = 0x80;
417 
418     if( used <= 56 )
419     {
420         /* Enough room for padding + length in current block */
421         memset( ctx->buffer + used, 0, 56 - used );
422     }
423     else
424     {
425         /* We'll need an extra block */
426         memset( ctx->buffer + used, 0, 64 - used );
427 
428         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
429             return( ret );
430 
431         memset( ctx->buffer, 0, 56 );
432     }
433 
434     /*
435      * Add message length
436      */
437     high = ( ctx->total[0] >> 29 )
438          | ( ctx->total[1] <<  3 );
439     low  = ( ctx->total[0] <<  3 );
440 
441     PUT_UINT32_BE( high, ctx->buffer, 56 );
442     PUT_UINT32_BE( low,  ctx->buffer, 60 );
443 
444     if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
445         return( ret );
446 
447     /*
448      * Output final state
449      */
450     PUT_UINT32_BE( ctx->state[0], output,  0 );
451     PUT_UINT32_BE( ctx->state[1], output,  4 );
452     PUT_UINT32_BE( ctx->state[2], output,  8 );
453     PUT_UINT32_BE( ctx->state[3], output, 12 );
454     PUT_UINT32_BE( ctx->state[4], output, 16 );
455 
456     return( 0 );
457 }
458 
459 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_finish(mbedtls_sha1_context * ctx,unsigned char output[20])460 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
461                           unsigned char output[20] )
462 {
463     mbedtls_sha1_finish_ret( ctx, output );
464 }
465 #endif
466 
467 #endif /* !MBEDTLS_SHA1_ALT */
468 
469 /*
470  * output = SHA-1( input buffer )
471  */
mbedtls_sha1_ret(const unsigned char * input,size_t ilen,unsigned char output[20])472 int mbedtls_sha1_ret( const unsigned char *input,
473                       size_t ilen,
474                       unsigned char output[20] )
475 {
476     int ret;
477     mbedtls_sha1_context ctx;
478 
479     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
480     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
481 
482     mbedtls_sha1_init( &ctx );
483 
484     if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
485         goto exit;
486 
487     if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
488         goto exit;
489 
490     if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
491         goto exit;
492 
493 exit:
494     mbedtls_sha1_free( &ctx );
495 
496     return( ret );
497 }
498 
499 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1(const unsigned char * input,size_t ilen,unsigned char output[20])500 void mbedtls_sha1( const unsigned char *input,
501                    size_t ilen,
502                    unsigned char output[20] )
503 {
504     mbedtls_sha1_ret( input, ilen, output );
505 }
506 #endif
507 
508 #if defined(MBEDTLS_SELF_TEST)
509 /*
510  * FIPS-180-1 test vectors
511  */
512 static const unsigned char sha1_test_buf[3][57] =
513 {
514     { "abc" },
515     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
516     { "" }
517 };
518 
519 static const size_t sha1_test_buflen[3] =
520 {
521     3, 56, 1000
522 };
523 
524 static const unsigned char sha1_test_sum[3][20] =
525 {
526     { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
527       0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
528     { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
529       0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
530     { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
531       0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
532 };
533 
534 /*
535  * Checkup routine
536  */
mbedtls_sha1_self_test(int verbose)537 int mbedtls_sha1_self_test( int verbose )
538 {
539     int i, j, buflen, ret = 0;
540     unsigned char buf[1024];
541     unsigned char sha1sum[20];
542     mbedtls_sha1_context ctx;
543 
544     mbedtls_sha1_init( &ctx );
545 
546     /*
547      * SHA-1
548      */
549     for( i = 0; i < 3; i++ )
550     {
551         if( verbose != 0 )
552             mbedtls_printf( "  SHA-1 test #%d: ", i + 1 );
553 
554         if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
555             goto fail;
556 
557         if( i == 2 )
558         {
559             memset( buf, 'a', buflen = 1000 );
560 
561             for( j = 0; j < 1000; j++ )
562             {
563                 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
564                 if( ret != 0 )
565                     goto fail;
566             }
567         }
568         else
569         {
570             ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
571                                            sha1_test_buflen[i] );
572             if( ret != 0 )
573                 goto fail;
574         }
575 
576         if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
577             goto fail;
578 
579         if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
580         {
581             ret = 1;
582             goto fail;
583         }
584 
585         if( verbose != 0 )
586             mbedtls_printf( "passed\n" );
587     }
588 
589     if( verbose != 0 )
590         mbedtls_printf( "\n" );
591 
592     goto exit;
593 
594 fail:
595     if( verbose != 0 )
596         mbedtls_printf( "failed\n" );
597 
598 exit:
599     mbedtls_sha1_free( &ctx );
600 
601     return( ret );
602 }
603 
604 #endif /* MBEDTLS_SELF_TEST */
605 
606 #endif /* MBEDTLS_SHA1_C */
607