]>
git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/sha1.c
2 * FIPS-180-1 compliant SHA-1 implementation
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
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.
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.
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.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 * The SHA-1 standard was published by NIST in 1993.
26 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
32 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_SHA1_C)
37 #include "mbedtls/sha1.h"
38 #include "mbedtls/platform_util.h"
42 #if defined(MBEDTLS_SELF_TEST)
43 #if defined(MBEDTLS_PLATFORM_C)
44 #include "mbedtls/platform.h"
47 #define mbedtls_printf printf
48 #endif /* MBEDTLS_PLATFORM_C */
49 #endif /* MBEDTLS_SELF_TEST */
51 #if !defined(MBEDTLS_SHA1_ALT)
54 * 32-bit integer manipulation macros (big endian)
57 #define GET_UINT32_BE(n,b,i) \
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] ); \
67 #define PUT_UINT32_BE(n,b,i) \
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) ); \
76 void mbedtls_sha1_init( mbedtls_sha1_context
*ctx
)
78 memset( ctx
, 0, sizeof( mbedtls_sha1_context
) );
81 void mbedtls_sha1_free( mbedtls_sha1_context
*ctx
)
86 mbedtls_platform_zeroize( ctx
, sizeof( mbedtls_sha1_context
) );
89 void mbedtls_sha1_clone( mbedtls_sha1_context
*dst
,
90 const mbedtls_sha1_context
*src
)
98 int mbedtls_sha1_starts_ret( mbedtls_sha1_context
*ctx
)
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;
112 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
113 void mbedtls_sha1_starts( mbedtls_sha1_context
*ctx
)
115 mbedtls_sha1_starts_ret( ctx
);
119 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
120 int mbedtls_internal_sha1_process( mbedtls_sha1_context
*ctx
,
121 const unsigned char data
[64] )
123 uint32_t temp
, W
[16], A
, B
, C
, D
, E
;
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 );
142 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
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) ) \
151 #define P(a,b,c,d,e,x) \
153 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
162 #define F(x,y,z) (z ^ (x & (y ^ z)))
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) );
189 #define F(x,y,z) (x ^ y ^ z)
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) );
216 #define F(x,y,z) ((x & y) | (z & (x | y)))
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) );
243 #define F(x,y,z) (x ^ y ^ z)
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) );
279 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
280 void mbedtls_sha1_process( mbedtls_sha1_context
*ctx
,
281 const unsigned char data
[64] )
283 mbedtls_internal_sha1_process( ctx
, data
);
286 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
289 * SHA-1 process buffer
291 int mbedtls_sha1_update_ret( mbedtls_sha1_context
*ctx
,
292 const unsigned char *input
,
302 left
= ctx
->total
[0] & 0x3F;
305 ctx
->total
[0] += (uint32_t) ilen
;
306 ctx
->total
[0] &= 0xFFFFFFFF;
308 if( ctx
->total
[0] < (uint32_t) ilen
)
311 if( left
&& ilen
>= fill
)
313 memcpy( (void *) (ctx
->buffer
+ left
), input
, fill
);
315 if( ( ret
= mbedtls_internal_sha1_process( ctx
, ctx
->buffer
) ) != 0 )
325 if( ( ret
= mbedtls_internal_sha1_process( ctx
, input
) ) != 0 )
333 memcpy( (void *) (ctx
->buffer
+ left
), input
, ilen
);
338 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
339 void mbedtls_sha1_update( mbedtls_sha1_context
*ctx
,
340 const unsigned char *input
,
343 mbedtls_sha1_update_ret( ctx
, input
, ilen
);
350 int mbedtls_sha1_finish_ret( mbedtls_sha1_context
*ctx
,
351 unsigned char output
[20] )
358 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
360 used
= ctx
->total
[0] & 0x3F;
362 ctx
->buffer
[used
++] = 0x80;
366 /* Enough room for padding + length in current block */
367 memset( ctx
->buffer
+ used
, 0, 56 - used
);
371 /* We'll need an extra block */
372 memset( ctx
->buffer
+ used
, 0, 64 - used
);
374 if( ( ret
= mbedtls_internal_sha1_process( ctx
, ctx
->buffer
) ) != 0 )
377 memset( ctx
->buffer
, 0, 56 );
383 high
= ( ctx
->total
[0] >> 29 )
384 | ( ctx
->total
[1] << 3 );
385 low
= ( ctx
->total
[0] << 3 );
387 PUT_UINT32_BE( high
, ctx
->buffer
, 56 );
388 PUT_UINT32_BE( low
, ctx
->buffer
, 60 );
390 if( ( ret
= mbedtls_internal_sha1_process( ctx
, ctx
->buffer
) ) != 0 )
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 );
405 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
406 void mbedtls_sha1_finish( mbedtls_sha1_context
*ctx
,
407 unsigned char output
[20] )
409 mbedtls_sha1_finish_ret( ctx
, output
);
413 #endif /* !MBEDTLS_SHA1_ALT */
416 * output = SHA-1( input buffer )
418 int mbedtls_sha1_ret( const unsigned char *input
,
420 unsigned char output
[20] )
423 mbedtls_sha1_context ctx
;
425 mbedtls_sha1_init( &ctx
);
427 if( ( ret
= mbedtls_sha1_starts_ret( &ctx
) ) != 0 )
430 if( ( ret
= mbedtls_sha1_update_ret( &ctx
, input
, ilen
) ) != 0 )
433 if( ( ret
= mbedtls_sha1_finish_ret( &ctx
, output
) ) != 0 )
437 mbedtls_sha1_free( &ctx
);
442 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
443 void mbedtls_sha1( const unsigned char *input
,
445 unsigned char output
[20] )
447 mbedtls_sha1_ret( input
, ilen
, output
);
451 #if defined(MBEDTLS_SELF_TEST)
453 * FIPS-180-1 test vectors
455 static const unsigned char sha1_test_buf
[3][57] =
458 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
462 static const size_t sha1_test_buflen
[3] =
467 static const unsigned char sha1_test_sum
[3][20] =
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 }
480 int mbedtls_sha1_self_test( int verbose
)
482 int i
, j
, buflen
, ret
= 0;
483 unsigned char buf
[1024];
484 unsigned char sha1sum
[20];
485 mbedtls_sha1_context ctx
;
487 mbedtls_sha1_init( &ctx
);
492 for( i
= 0; i
< 3; i
++ )
495 mbedtls_printf( " SHA-1 test #%d: ", i
+ 1 );
497 if( ( ret
= mbedtls_sha1_starts_ret( &ctx
) ) != 0 )
502 memset( buf
, 'a', buflen
= 1000 );
504 for( j
= 0; j
< 1000; j
++ )
506 ret
= mbedtls_sha1_update_ret( &ctx
, buf
, buflen
);
513 ret
= mbedtls_sha1_update_ret( &ctx
, sha1_test_buf
[i
],
514 sha1_test_buflen
[i
] );
519 if( ( ret
= mbedtls_sha1_finish_ret( &ctx
, sha1sum
) ) != 0 )
522 if( memcmp( sha1sum
, sha1_test_sum
[i
], 20 ) != 0 )
529 mbedtls_printf( "passed\n" );
533 mbedtls_printf( "\n" );
539 mbedtls_printf( "failed\n" );
542 mbedtls_sha1_free( &ctx
);
547 #endif /* MBEDTLS_SELF_TEST */
549 #endif /* MBEDTLS_SHA1_C */