2 * FIPS-180-1 compliant SHA-1 implementation
4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
6 * This file is part of mbed TLS (https://tls.mbed.org)
8 * 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.
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.
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.
23 * The SHA-1 standard was published by NIST in 1993.
25 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
28 #if !defined(POLARSSL_CONFIG_FILE)
29 //#include "polarssl/config.h"
30 #define POLARSSL_SHA1_C
33 #include POLARSSL_CONFIG_FILE
36 #if defined(POLARSSL_SHA1_C)
42 #if defined(POLARSSL_FS_IO)
46 #if defined(POLARSSL_SELF_TEST)
47 #if defined(POLARSSL_PLATFORM_C)
48 #include "polarssl/platform.h"
51 #define polarssl_printf printf
52 #endif /* POLARSSL_PLATFORM_C */
53 #endif /* POLARSSL_SELF_TEST */
55 /* Implementation that should never be optimized out by the compiler */
56 static void polarssl_zeroize( void *v
, size_t n
) {
57 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
60 #if !defined(POLARSSL_SHA1_ALT)
63 * 32-bit integer manipulation macros (big endian)
66 #define GET_UINT32_BE(n,b,i) \
68 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
69 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
70 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
71 | ( (uint32_t) (b)[(i) + 3] ); \
76 #define PUT_UINT32_BE(n,b,i) \
78 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
79 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
80 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
81 (b)[(i) + 3] = (unsigned char) ( (n) ); \
85 void sha1_init( sha1_context
*ctx
)
87 memset( ctx
, 0, sizeof( sha1_context
) );
90 void sha1_free( sha1_context
*ctx
)
95 polarssl_zeroize( ctx
, sizeof( sha1_context
) );
101 void sha1_starts( sha1_context
*ctx
)
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;
113 void sha1_process( sha1_context
*ctx
, const unsigned char data
[64] )
115 uint32_t temp
, W
[16], A
, B
, C
, D
, E
;
117 GET_UINT32_BE( W
[ 0], data
, 0 );
118 GET_UINT32_BE( W
[ 1], data
, 4 );
119 GET_UINT32_BE( W
[ 2], data
, 8 );
120 GET_UINT32_BE( W
[ 3], data
, 12 );
121 GET_UINT32_BE( W
[ 4], data
, 16 );
122 GET_UINT32_BE( W
[ 5], data
, 20 );
123 GET_UINT32_BE( W
[ 6], data
, 24 );
124 GET_UINT32_BE( W
[ 7], data
, 28 );
125 GET_UINT32_BE( W
[ 8], data
, 32 );
126 GET_UINT32_BE( W
[ 9], data
, 36 );
127 GET_UINT32_BE( W
[10], data
, 40 );
128 GET_UINT32_BE( W
[11], data
, 44 );
129 GET_UINT32_BE( W
[12], data
, 48 );
130 GET_UINT32_BE( W
[13], data
, 52 );
131 GET_UINT32_BE( W
[14], data
, 56 );
132 GET_UINT32_BE( W
[15], data
, 60 );
134 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
138 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
139 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
140 ( W[t & 0x0F] = S(temp,1) ) \
143 #define P(a,b,c,d,e,x) \
145 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
154 #define F(x,y,z) (z ^ (x & (y ^ z)))
157 P( A
, B
, C
, D
, E
, W
[0] );
158 P( E
, A
, B
, C
, D
, W
[1] );
159 P( D
, E
, A
, B
, C
, W
[2] );
160 P( C
, D
, E
, A
, B
, W
[3] );
161 P( B
, C
, D
, E
, A
, W
[4] );
162 P( A
, B
, C
, D
, E
, W
[5] );
163 P( E
, A
, B
, C
, D
, W
[6] );
164 P( D
, E
, A
, B
, C
, W
[7] );
165 P( C
, D
, E
, A
, B
, W
[8] );
166 P( B
, C
, D
, E
, A
, W
[9] );
167 P( A
, B
, C
, D
, E
, W
[10] );
168 P( E
, A
, B
, C
, D
, W
[11] );
169 P( D
, E
, A
, B
, C
, W
[12] );
170 P( C
, D
, E
, A
, B
, W
[13] );
171 P( B
, C
, D
, E
, A
, W
[14] );
172 P( A
, B
, C
, D
, E
, W
[15] );
173 P( E
, A
, B
, C
, D
, R(16) );
174 P( D
, E
, A
, B
, C
, R(17) );
175 P( C
, D
, E
, A
, B
, R(18) );
176 P( B
, C
, D
, E
, A
, R(19) );
181 #define F(x,y,z) (x ^ y ^ z)
184 P( A
, B
, C
, D
, E
, R(20) );
185 P( E
, A
, B
, C
, D
, R(21) );
186 P( D
, E
, A
, B
, C
, R(22) );
187 P( C
, D
, E
, A
, B
, R(23) );
188 P( B
, C
, D
, E
, A
, R(24) );
189 P( A
, B
, C
, D
, E
, R(25) );
190 P( E
, A
, B
, C
, D
, R(26) );
191 P( D
, E
, A
, B
, C
, R(27) );
192 P( C
, D
, E
, A
, B
, R(28) );
193 P( B
, C
, D
, E
, A
, R(29) );
194 P( A
, B
, C
, D
, E
, R(30) );
195 P( E
, A
, B
, C
, D
, R(31) );
196 P( D
, E
, A
, B
, C
, R(32) );
197 P( C
, D
, E
, A
, B
, R(33) );
198 P( B
, C
, D
, E
, A
, R(34) );
199 P( A
, B
, C
, D
, E
, R(35) );
200 P( E
, A
, B
, C
, D
, R(36) );
201 P( D
, E
, A
, B
, C
, R(37) );
202 P( C
, D
, E
, A
, B
, R(38) );
203 P( B
, C
, D
, E
, A
, R(39) );
208 #define F(x,y,z) ((x & y) | (z & (x | y)))
211 P( A
, B
, C
, D
, E
, R(40) );
212 P( E
, A
, B
, C
, D
, R(41) );
213 P( D
, E
, A
, B
, C
, R(42) );
214 P( C
, D
, E
, A
, B
, R(43) );
215 P( B
, C
, D
, E
, A
, R(44) );
216 P( A
, B
, C
, D
, E
, R(45) );
217 P( E
, A
, B
, C
, D
, R(46) );
218 P( D
, E
, A
, B
, C
, R(47) );
219 P( C
, D
, E
, A
, B
, R(48) );
220 P( B
, C
, D
, E
, A
, R(49) );
221 P( A
, B
, C
, D
, E
, R(50) );
222 P( E
, A
, B
, C
, D
, R(51) );
223 P( D
, E
, A
, B
, C
, R(52) );
224 P( C
, D
, E
, A
, B
, R(53) );
225 P( B
, C
, D
, E
, A
, R(54) );
226 P( A
, B
, C
, D
, E
, R(55) );
227 P( E
, A
, B
, C
, D
, R(56) );
228 P( D
, E
, A
, B
, C
, R(57) );
229 P( C
, D
, E
, A
, B
, R(58) );
230 P( B
, C
, D
, E
, A
, R(59) );
235 #define F(x,y,z) (x ^ y ^ z)
238 P( A
, B
, C
, D
, E
, R(60) );
239 P( E
, A
, B
, C
, D
, R(61) );
240 P( D
, E
, A
, B
, C
, R(62) );
241 P( C
, D
, E
, A
, B
, R(63) );
242 P( B
, C
, D
, E
, A
, R(64) );
243 P( A
, B
, C
, D
, E
, R(65) );
244 P( E
, A
, B
, C
, D
, R(66) );
245 P( D
, E
, A
, B
, C
, R(67) );
246 P( C
, D
, E
, A
, B
, R(68) );
247 P( B
, C
, D
, E
, A
, R(69) );
248 P( A
, B
, C
, D
, E
, R(70) );
249 P( E
, A
, B
, C
, D
, R(71) );
250 P( D
, E
, A
, B
, C
, R(72) );
251 P( C
, D
, E
, A
, B
, R(73) );
252 P( B
, C
, D
, E
, A
, R(74) );
253 P( A
, B
, C
, D
, E
, R(75) );
254 P( E
, A
, B
, C
, D
, R(76) );
255 P( D
, E
, A
, B
, C
, R(77) );
256 P( C
, D
, E
, A
, B
, R(78) );
257 P( B
, C
, D
, E
, A
, R(79) );
270 * SHA-1 process buffer
272 void sha1_update( sha1_context
*ctx
, const unsigned char *input
, size_t ilen
)
280 left
= ctx
->total
[0] & 0x3F;
283 ctx
->total
[0] += (uint32_t) ilen
;
284 ctx
->total
[0] &= 0xFFFFFFFF;
286 if( ctx
->total
[0] < (uint32_t) ilen
)
289 if( left
&& ilen
>= fill
)
291 memcpy( (void *) (ctx
->buffer
+ left
), input
, fill
);
292 sha1_process( ctx
, ctx
->buffer
);
300 sha1_process( ctx
, input
);
306 memcpy( (void *) (ctx
->buffer
+ left
), input
, ilen
);
309 static const unsigned char sha1_padding
[64] =
311 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
313 0, 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
320 void sha1_finish( sha1_context
*ctx
, unsigned char output
[20] )
324 unsigned char msglen
[8];
326 high
= ( ctx
->total
[0] >> 29 )
327 | ( ctx
->total
[1] << 3 );
328 low
= ( ctx
->total
[0] << 3 );
330 PUT_UINT32_BE( high
, msglen
, 0 );
331 PUT_UINT32_BE( low
, msglen
, 4 );
333 last
= ctx
->total
[0] & 0x3F;
334 padn
= ( last
< 56 ) ? ( 56 - last
) : ( 120 - last
);
336 sha1_update( ctx
, sha1_padding
, padn
);
337 sha1_update( ctx
, msglen
, 8 );
339 PUT_UINT32_BE( ctx
->state
[0], output
, 0 );
340 PUT_UINT32_BE( ctx
->state
[1], output
, 4 );
341 PUT_UINT32_BE( ctx
->state
[2], output
, 8 );
342 PUT_UINT32_BE( ctx
->state
[3], output
, 12 );
343 PUT_UINT32_BE( ctx
->state
[4], output
, 16 );
346 #endif /* !POLARSSL_SHA1_ALT */
349 * output = SHA-1( input buffer )
351 void sha1( const unsigned char *input
, size_t ilen
, unsigned char output
[20] )
357 sha1_update( &ctx
, input
, ilen
);
358 sha1_finish( &ctx
, output
);
362 #if defined(POLARSSL_FS_IO)
364 * output = SHA-1( file contents )
366 int sha1_file( const char *path
, unsigned char output
[20] )
371 unsigned char buf
[1024];
373 if( ( f
= fopen( path
, "rb" ) ) == NULL
)
374 return( POLARSSL_ERR_SHA1_FILE_IO_ERROR
);
379 while( ( n
= fread( buf
, 1, sizeof( buf
), f
) ) > 0 )
380 sha1_update( &ctx
, buf
, n
);
382 sha1_finish( &ctx
, output
);
385 if( ferror( f
) != 0 )
388 return( POLARSSL_ERR_SHA1_FILE_IO_ERROR
);
394 #endif /* POLARSSL_FS_IO */
397 * SHA-1 HMAC context setup
399 void sha1_hmac_starts( sha1_context
*ctx
, const unsigned char *key
,
403 unsigned char sum
[20];
407 sha1( key
, keylen
, sum
);
412 memset( ctx
->ipad
, 0x36, 64 );
413 memset( ctx
->opad
, 0x5C, 64 );
415 for( i
= 0; i
< keylen
; i
++ )
417 ctx
->ipad
[i
] = (unsigned char)( ctx
->ipad
[i
] ^ key
[i
] );
418 ctx
->opad
[i
] = (unsigned char)( ctx
->opad
[i
] ^ key
[i
] );
422 sha1_update( ctx
, ctx
->ipad
, 64 );
424 polarssl_zeroize( sum
, sizeof( sum
) );
428 * SHA-1 HMAC process buffer
430 void sha1_hmac_update( sha1_context
*ctx
, const unsigned char *input
,
433 sha1_update( ctx
, input
, ilen
);
437 * SHA-1 HMAC final digest
439 void sha1_hmac_finish( sha1_context
*ctx
, unsigned char output
[20] )
441 unsigned char tmpbuf
[20];
443 sha1_finish( ctx
, tmpbuf
);
445 sha1_update( ctx
, ctx
->opad
, 64 );
446 sha1_update( ctx
, tmpbuf
, 20 );
447 sha1_finish( ctx
, output
);
449 polarssl_zeroize( tmpbuf
, sizeof( tmpbuf
) );
453 * SHA1 HMAC context reset
455 void sha1_hmac_reset( sha1_context
*ctx
)
458 sha1_update( ctx
, ctx
->ipad
, 64 );
462 * output = HMAC-SHA-1( hmac key, input buffer )
464 void sha1_hmac( const unsigned char *key
, size_t keylen
,
465 const unsigned char *input
, size_t ilen
,
466 unsigned char output
[20] )
471 sha1_hmac_starts( &ctx
, key
, keylen
);
472 sha1_hmac_update( &ctx
, input
, ilen
);
473 sha1_hmac_finish( &ctx
, output
);
477 #if defined(POLARSSL_SELF_TEST)
479 * FIPS-180-1 test vectors
481 static const unsigned char sha1_test_buf
[3][57] =
484 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
488 static const int sha1_test_buflen
[3] =
493 static const unsigned char sha1_test_sum
[3][20] =
495 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
496 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
497 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
498 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
499 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
500 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
504 * RFC 2202 test vectors
506 static const unsigned char sha1_hmac_test_key
[7][26] =
508 { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
509 "\x0B\x0B\x0B\x0B" },
511 { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
512 "\xAA\xAA\xAA\xAA" },
513 { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
514 "\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
515 { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
516 "\x0C\x0C\x0C\x0C" },
517 { "" }, /* 0xAA 80 times */
521 static const int sha1_hmac_test_keylen
[7] =
523 20, 4, 20, 25, 20, 80, 80
526 static const unsigned char sha1_hmac_test_buf
[7][74] =
529 { "what do ya want for nothing?" },
530 { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
531 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
532 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
533 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
534 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
535 { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
536 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
537 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
538 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
539 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
540 { "Test With Truncation" },
541 { "Test Using Larger Than Block-Size Key - Hash Key First" },
542 { "Test Using Larger Than Block-Size Key and Larger"
543 " Than One Block-Size Data" }
546 static const int sha1_hmac_test_buflen
[7] =
548 8, 28, 50, 50, 20, 54, 73
551 static const unsigned char sha1_hmac_test_sum
[7][20] =
553 { 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
554 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 },
555 { 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
556 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 },
557 { 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
558 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 },
559 { 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
560 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA },
561 { 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
563 { 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
564 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 },
565 { 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
566 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 }
572 int sha1_self_test( int verbose
)
574 int i
, j
, buflen
, ret
= 0;
575 unsigned char buf
[1024];
576 unsigned char sha1sum
[20];
584 for( i
= 0; i
< 3; i
++ )
587 polarssl_printf( " SHA-1 test #%d: ", i
+ 1 );
593 memset( buf
, 'a', buflen
= 1000 );
595 for( j
= 0; j
< 1000; j
++ )
596 sha1_update( &ctx
, buf
, buflen
);
599 sha1_update( &ctx
, sha1_test_buf
[i
],
600 sha1_test_buflen
[i
] );
602 sha1_finish( &ctx
, sha1sum
);
604 if( memcmp( sha1sum
, sha1_test_sum
[i
], 20 ) != 0 )
607 polarssl_printf( "failed\n" );
614 polarssl_printf( "passed\n" );
618 polarssl_printf( "\n" );
620 for( i
= 0; i
< 7; i
++ )
623 polarssl_printf( " HMAC-SHA-1 test #%d: ", i
+ 1 );
625 if( i
== 5 || i
== 6 )
627 memset( buf
, 0xAA, buflen
= 80 );
628 sha1_hmac_starts( &ctx
, buf
, buflen
);
631 sha1_hmac_starts( &ctx
, sha1_hmac_test_key
[i
],
632 sha1_hmac_test_keylen
[i
] );
634 sha1_hmac_update( &ctx
, sha1_hmac_test_buf
[i
],
635 sha1_hmac_test_buflen
[i
] );
637 sha1_hmac_finish( &ctx
, sha1sum
);
639 buflen
= ( i
== 4 ) ? 12 : 20;
641 if( memcmp( sha1sum
, sha1_hmac_test_sum
[i
], buflen
) != 0 )
644 polarssl_printf( "failed\n" );
651 polarssl_printf( "passed\n" );
655 polarssl_printf( "\n" );
663 #endif /* POLARSSL_SELF_TEST */
665 #endif /* POLARSSL_SHA1_C */