]> git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/sha1.c
'hf iclass loclass': fix error handling (#865)
[proxmark3-svn] / common / mbedtls / sha1.c
1 /*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23 /*
24 * The SHA-1 standard was published by NIST in 1993.
25 *
26 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
27 */
28
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
31 #else
32 #include MBEDTLS_CONFIG_FILE
33 #endif
34
35 #if defined(MBEDTLS_SHA1_C)
36
37 #include "mbedtls/sha1.h"
38 #include "mbedtls/platform_util.h"
39
40 #include <string.h>
41
42 #if defined(MBEDTLS_SELF_TEST)
43 #if defined(MBEDTLS_PLATFORM_C)
44 #include "mbedtls/platform.h"
45 #else
46 #include <stdio.h>
47 #define mbedtls_printf printf
48 #endif /* MBEDTLS_PLATFORM_C */
49 #endif /* MBEDTLS_SELF_TEST */
50
51 #if !defined(MBEDTLS_SHA1_ALT)
52
53 /*
54 * 32-bit integer manipulation macros (big endian)
55 */
56 #ifndef GET_UINT32_BE
57 #define GET_UINT32_BE(n,b,i) \
58 { \
59 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
60 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
61 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
62 | ( (uint32_t) (b)[(i) + 3] ); \
63 }
64 #endif
65
66 #ifndef PUT_UINT32_BE
67 #define PUT_UINT32_BE(n,b,i) \
68 { \
69 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
70 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
71 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
72 (b)[(i) + 3] = (unsigned char) ( (n) ); \
73 }
74 #endif
75
76 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
77 {
78 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
79 }
80
81 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
82 {
83 if( ctx == NULL )
84 return;
85
86 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
87 }
88
89 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
90 const mbedtls_sha1_context *src )
91 {
92 *dst = *src;
93 }
94
95 /*
96 * SHA-1 context setup
97 */
98 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
99 {
100 ctx->total[0] = 0;
101 ctx->total[1] = 0;
102
103 ctx->state[0] = 0x67452301;
104 ctx->state[1] = 0xEFCDAB89;
105 ctx->state[2] = 0x98BADCFE;
106 ctx->state[3] = 0x10325476;
107 ctx->state[4] = 0xC3D2E1F0;
108
109 return( 0 );
110 }
111
112 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
113 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
114 {
115 mbedtls_sha1_starts_ret( ctx );
116 }
117 #endif
118
119 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
120 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
121 const unsigned char data[64] )
122 {
123 uint32_t temp, W[16], A, B, C, D, E;
124
125 GET_UINT32_BE( W[ 0], data, 0 );
126 GET_UINT32_BE( W[ 1], data, 4 );
127 GET_UINT32_BE( W[ 2], data, 8 );
128 GET_UINT32_BE( W[ 3], data, 12 );
129 GET_UINT32_BE( W[ 4], data, 16 );
130 GET_UINT32_BE( W[ 5], data, 20 );
131 GET_UINT32_BE( W[ 6], data, 24 );
132 GET_UINT32_BE( W[ 7], data, 28 );
133 GET_UINT32_BE( W[ 8], data, 32 );
134 GET_UINT32_BE( W[ 9], data, 36 );
135 GET_UINT32_BE( W[10], data, 40 );
136 GET_UINT32_BE( W[11], data, 44 );
137 GET_UINT32_BE( W[12], data, 48 );
138 GET_UINT32_BE( W[13], data, 52 );
139 GET_UINT32_BE( W[14], data, 56 );
140 GET_UINT32_BE( W[15], data, 60 );
141
142 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
143
144 #define R(t) \
145 ( \
146 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
147 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
148 ( W[t & 0x0F] = S(temp,1) ) \
149 )
150
151 #define P(a,b,c,d,e,x) \
152 { \
153 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
154 }
155
156 A = ctx->state[0];
157 B = ctx->state[1];
158 C = ctx->state[2];
159 D = ctx->state[3];
160 E = ctx->state[4];
161
162 #define F(x,y,z) (z ^ (x & (y ^ z)))
163 #define K 0x5A827999
164
165 P( A, B, C, D, E, W[0] );
166 P( E, A, B, C, D, W[1] );
167 P( D, E, A, B, C, W[2] );
168 P( C, D, E, A, B, W[3] );
169 P( B, C, D, E, A, W[4] );
170 P( A, B, C, D, E, W[5] );
171 P( E, A, B, C, D, W[6] );
172 P( D, E, A, B, C, W[7] );
173 P( C, D, E, A, B, W[8] );
174 P( B, C, D, E, A, W[9] );
175 P( A, B, C, D, E, W[10] );
176 P( E, A, B, C, D, W[11] );
177 P( D, E, A, B, C, W[12] );
178 P( C, D, E, A, B, W[13] );
179 P( B, C, D, E, A, W[14] );
180 P( A, B, C, D, E, W[15] );
181 P( E, A, B, C, D, R(16) );
182 P( D, E, A, B, C, R(17) );
183 P( C, D, E, A, B, R(18) );
184 P( B, C, D, E, A, R(19) );
185
186 #undef K
187 #undef F
188
189 #define F(x,y,z) (x ^ y ^ z)
190 #define K 0x6ED9EBA1
191
192 P( A, B, C, D, E, R(20) );
193 P( E, A, B, C, D, R(21) );
194 P( D, E, A, B, C, R(22) );
195 P( C, D, E, A, B, R(23) );
196 P( B, C, D, E, A, R(24) );
197 P( A, B, C, D, E, R(25) );
198 P( E, A, B, C, D, R(26) );
199 P( D, E, A, B, C, R(27) );
200 P( C, D, E, A, B, R(28) );
201 P( B, C, D, E, A, R(29) );
202 P( A, B, C, D, E, R(30) );
203 P( E, A, B, C, D, R(31) );
204 P( D, E, A, B, C, R(32) );
205 P( C, D, E, A, B, R(33) );
206 P( B, C, D, E, A, R(34) );
207 P( A, B, C, D, E, R(35) );
208 P( E, A, B, C, D, R(36) );
209 P( D, E, A, B, C, R(37) );
210 P( C, D, E, A, B, R(38) );
211 P( B, C, D, E, A, R(39) );
212
213 #undef K
214 #undef F
215
216 #define F(x,y,z) ((x & y) | (z & (x | y)))
217 #define K 0x8F1BBCDC
218
219 P( A, B, C, D, E, R(40) );
220 P( E, A, B, C, D, R(41) );
221 P( D, E, A, B, C, R(42) );
222 P( C, D, E, A, B, R(43) );
223 P( B, C, D, E, A, R(44) );
224 P( A, B, C, D, E, R(45) );
225 P( E, A, B, C, D, R(46) );
226 P( D, E, A, B, C, R(47) );
227 P( C, D, E, A, B, R(48) );
228 P( B, C, D, E, A, R(49) );
229 P( A, B, C, D, E, R(50) );
230 P( E, A, B, C, D, R(51) );
231 P( D, E, A, B, C, R(52) );
232 P( C, D, E, A, B, R(53) );
233 P( B, C, D, E, A, R(54) );
234 P( A, B, C, D, E, R(55) );
235 P( E, A, B, C, D, R(56) );
236 P( D, E, A, B, C, R(57) );
237 P( C, D, E, A, B, R(58) );
238 P( B, C, D, E, A, R(59) );
239
240 #undef K
241 #undef F
242
243 #define F(x,y,z) (x ^ y ^ z)
244 #define K 0xCA62C1D6
245
246 P( A, B, C, D, E, R(60) );
247 P( E, A, B, C, D, R(61) );
248 P( D, E, A, B, C, R(62) );
249 P( C, D, E, A, B, R(63) );
250 P( B, C, D, E, A, R(64) );
251 P( A, B, C, D, E, R(65) );
252 P( E, A, B, C, D, R(66) );
253 P( D, E, A, B, C, R(67) );
254 P( C, D, E, A, B, R(68) );
255 P( B, C, D, E, A, R(69) );
256 P( A, B, C, D, E, R(70) );
257 P( E, A, B, C, D, R(71) );
258 P( D, E, A, B, C, R(72) );
259 P( C, D, E, A, B, R(73) );
260 P( B, C, D, E, A, R(74) );
261 P( A, B, C, D, E, R(75) );
262 P( E, A, B, C, D, R(76) );
263 P( D, E, A, B, C, R(77) );
264 P( C, D, E, A, B, R(78) );
265 P( B, C, D, E, A, R(79) );
266
267 #undef K
268 #undef F
269
270 ctx->state[0] += A;
271 ctx->state[1] += B;
272 ctx->state[2] += C;
273 ctx->state[3] += D;
274 ctx->state[4] += E;
275
276 return( 0 );
277 }
278
279 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
280 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
281 const unsigned char data[64] )
282 {
283 mbedtls_internal_sha1_process( ctx, data );
284 }
285 #endif
286 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
287
288 /*
289 * SHA-1 process buffer
290 */
291 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
292 const unsigned char *input,
293 size_t ilen )
294 {
295 int ret;
296 size_t fill;
297 uint32_t left;
298
299 if( ilen == 0 )
300 return( 0 );
301
302 left = ctx->total[0] & 0x3F;
303 fill = 64 - left;
304
305 ctx->total[0] += (uint32_t) ilen;
306 ctx->total[0] &= 0xFFFFFFFF;
307
308 if( ctx->total[0] < (uint32_t) ilen )
309 ctx->total[1]++;
310
311 if( left && ilen >= fill )
312 {
313 memcpy( (void *) (ctx->buffer + left), input, fill );
314
315 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
316 return( ret );
317
318 input += fill;
319 ilen -= fill;
320 left = 0;
321 }
322
323 while( ilen >= 64 )
324 {
325 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
326 return( ret );
327
328 input += 64;
329 ilen -= 64;
330 }
331
332 if( ilen > 0 )
333 memcpy( (void *) (ctx->buffer + left), input, ilen );
334
335 return( 0 );
336 }
337
338 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
339 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
340 const unsigned char *input,
341 size_t ilen )
342 {
343 mbedtls_sha1_update_ret( ctx, input, ilen );
344 }
345 #endif
346
347 /*
348 * SHA-1 final digest
349 */
350 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
351 unsigned char output[20] )
352 {
353 int ret;
354 uint32_t used;
355 uint32_t high, low;
356
357 /*
358 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
359 */
360 used = ctx->total[0] & 0x3F;
361
362 ctx->buffer[used++] = 0x80;
363
364 if( used <= 56 )
365 {
366 /* Enough room for padding + length in current block */
367 memset( ctx->buffer + used, 0, 56 - used );
368 }
369 else
370 {
371 /* We'll need an extra block */
372 memset( ctx->buffer + used, 0, 64 - used );
373
374 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
375 return( ret );
376
377 memset( ctx->buffer, 0, 56 );
378 }
379
380 /*
381 * Add message length
382 */
383 high = ( ctx->total[0] >> 29 )
384 | ( ctx->total[1] << 3 );
385 low = ( ctx->total[0] << 3 );
386
387 PUT_UINT32_BE( high, ctx->buffer, 56 );
388 PUT_UINT32_BE( low, ctx->buffer, 60 );
389
390 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
391 return( ret );
392
393 /*
394 * Output final state
395 */
396 PUT_UINT32_BE( ctx->state[0], output, 0 );
397 PUT_UINT32_BE( ctx->state[1], output, 4 );
398 PUT_UINT32_BE( ctx->state[2], output, 8 );
399 PUT_UINT32_BE( ctx->state[3], output, 12 );
400 PUT_UINT32_BE( ctx->state[4], output, 16 );
401
402 return( 0 );
403 }
404
405 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
406 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
407 unsigned char output[20] )
408 {
409 mbedtls_sha1_finish_ret( ctx, output );
410 }
411 #endif
412
413 #endif /* !MBEDTLS_SHA1_ALT */
414
415 /*
416 * output = SHA-1( input buffer )
417 */
418 int mbedtls_sha1_ret( const unsigned char *input,
419 size_t ilen,
420 unsigned char output[20] )
421 {
422 int ret;
423 mbedtls_sha1_context ctx;
424
425 mbedtls_sha1_init( &ctx );
426
427 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
428 goto exit;
429
430 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
431 goto exit;
432
433 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
434 goto exit;
435
436 exit:
437 mbedtls_sha1_free( &ctx );
438
439 return( ret );
440 }
441
442 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
443 void mbedtls_sha1( const unsigned char *input,
444 size_t ilen,
445 unsigned char output[20] )
446 {
447 mbedtls_sha1_ret( input, ilen, output );
448 }
449 #endif
450
451 #if defined(MBEDTLS_SELF_TEST)
452 /*
453 * FIPS-180-1 test vectors
454 */
455 static const unsigned char sha1_test_buf[3][57] =
456 {
457 { "abc" },
458 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
459 { "" }
460 };
461
462 static const size_t sha1_test_buflen[3] =
463 {
464 3, 56, 1000
465 };
466
467 static const unsigned char sha1_test_sum[3][20] =
468 {
469 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
470 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
471 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
472 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
473 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
474 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
475 };
476
477 /*
478 * Checkup routine
479 */
480 int mbedtls_sha1_self_test( int verbose )
481 {
482 int i, j, buflen, ret = 0;
483 unsigned char buf[1024];
484 unsigned char sha1sum[20];
485 mbedtls_sha1_context ctx;
486
487 mbedtls_sha1_init( &ctx );
488
489 /*
490 * SHA-1
491 */
492 for( i = 0; i < 3; i++ )
493 {
494 if( verbose != 0 )
495 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
496
497 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
498 goto fail;
499
500 if( i == 2 )
501 {
502 memset( buf, 'a', buflen = 1000 );
503
504 for( j = 0; j < 1000; j++ )
505 {
506 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
507 if( ret != 0 )
508 goto fail;
509 }
510 }
511 else
512 {
513 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
514 sha1_test_buflen[i] );
515 if( ret != 0 )
516 goto fail;
517 }
518
519 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
520 goto fail;
521
522 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
523 {
524 ret = 1;
525 goto fail;
526 }
527
528 if( verbose != 0 )
529 mbedtls_printf( "passed\n" );
530 }
531
532 if( verbose != 0 )
533 mbedtls_printf( "\n" );
534
535 goto exit;
536
537 fail:
538 if( verbose != 0 )
539 mbedtls_printf( "failed\n" );
540
541 exit:
542 mbedtls_sha1_free( &ctx );
543
544 return( ret );
545 }
546
547 #endif /* MBEDTLS_SELF_TEST */
548
549 #endif /* MBEDTLS_SHA1_C */
Impressum, Datenschutz