2 * FIPS-180-1 compliant SHA-1 implementation
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The SHA-1 standard was published by NIST in 1993.
38 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
41 #include "tropicssl/config.h"
43 #if defined(TROPICSSL_SHA1_C)
45 #include "tropicssl/sha1.h"
51 * 32-bit integer manipulation macros (big endian)
54 #define GET_ULONG_BE(n,b,i) \
56 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
57 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
58 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
59 | ( (unsigned long) (b)[(i) + 3] ); \
64 #define PUT_ULONG_BE(n,b,i) \
66 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
67 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
68 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
69 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76 void sha1_starts(sha1_context
* ctx
)
81 ctx
->state
[0] = 0x67452301;
82 ctx
->state
[1] = 0xEFCDAB89;
83 ctx
->state
[2] = 0x98BADCFE;
84 ctx
->state
[3] = 0x10325476;
85 ctx
->state
[4] = 0xC3D2E1F0;
88 static void sha1_process(sha1_context
* ctx
, const unsigned char data
[64])
90 unsigned long temp
, W
[16], A
, B
, C
, D
, E
;
92 GET_ULONG_BE(W
[0], data
, 0);
93 GET_ULONG_BE(W
[1], data
, 4);
94 GET_ULONG_BE(W
[2], data
, 8);
95 GET_ULONG_BE(W
[3], data
, 12);
96 GET_ULONG_BE(W
[4], data
, 16);
97 GET_ULONG_BE(W
[5], data
, 20);
98 GET_ULONG_BE(W
[6], data
, 24);
99 GET_ULONG_BE(W
[7], data
, 28);
100 GET_ULONG_BE(W
[8], data
, 32);
101 GET_ULONG_BE(W
[9], data
, 36);
102 GET_ULONG_BE(W
[10], data
, 40);
103 GET_ULONG_BE(W
[11], data
, 44);
104 GET_ULONG_BE(W
[12], data
, 48);
105 GET_ULONG_BE(W
[13], data
, 52);
106 GET_ULONG_BE(W
[14], data
, 56);
107 GET_ULONG_BE(W
[15], data
, 60);
109 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
113 temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
114 W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
115 ( W[t & 0x0F] = S(temp,1) ) \
118 #define P(a,b,c,d,e,x) \
120 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
129 #define F(x,y,z) (z ^ (x & (y ^ z)))
132 P(A
, B
, C
, D
, E
, W
[0]);
133 P(E
, A
, B
, C
, D
, W
[1]);
134 P(D
, E
, A
, B
, C
, W
[2]);
135 P(C
, D
, E
, A
, B
, W
[3]);
136 P(B
, C
, D
, E
, A
, W
[4]);
137 P(A
, B
, C
, D
, E
, W
[5]);
138 P(E
, A
, B
, C
, D
, W
[6]);
139 P(D
, E
, A
, B
, C
, W
[7]);
140 P(C
, D
, E
, A
, B
, W
[8]);
141 P(B
, C
, D
, E
, A
, W
[9]);
142 P(A
, B
, C
, D
, E
, W
[10]);
143 P(E
, A
, B
, C
, D
, W
[11]);
144 P(D
, E
, A
, B
, C
, W
[12]);
145 P(C
, D
, E
, A
, B
, W
[13]);
146 P(B
, C
, D
, E
, A
, W
[14]);
147 P(A
, B
, C
, D
, E
, W
[15]);
148 P(E
, A
, B
, C
, D
, R(16));
149 P(D
, E
, A
, B
, C
, R(17));
150 P(C
, D
, E
, A
, B
, R(18));
151 P(B
, C
, D
, E
, A
, R(19));
156 #define F(x,y,z) (x ^ y ^ z)
159 P(A
, B
, C
, D
, E
, R(20));
160 P(E
, A
, B
, C
, D
, R(21));
161 P(D
, E
, A
, B
, C
, R(22));
162 P(C
, D
, E
, A
, B
, R(23));
163 P(B
, C
, D
, E
, A
, R(24));
164 P(A
, B
, C
, D
, E
, R(25));
165 P(E
, A
, B
, C
, D
, R(26));
166 P(D
, E
, A
, B
, C
, R(27));
167 P(C
, D
, E
, A
, B
, R(28));
168 P(B
, C
, D
, E
, A
, R(29));
169 P(A
, B
, C
, D
, E
, R(30));
170 P(E
, A
, B
, C
, D
, R(31));
171 P(D
, E
, A
, B
, C
, R(32));
172 P(C
, D
, E
, A
, B
, R(33));
173 P(B
, C
, D
, E
, A
, R(34));
174 P(A
, B
, C
, D
, E
, R(35));
175 P(E
, A
, B
, C
, D
, R(36));
176 P(D
, E
, A
, B
, C
, R(37));
177 P(C
, D
, E
, A
, B
, R(38));
178 P(B
, C
, D
, E
, A
, R(39));
183 #define F(x,y,z) ((x & y) | (z & (x | y)))
186 P(A
, B
, C
, D
, E
, R(40));
187 P(E
, A
, B
, C
, D
, R(41));
188 P(D
, E
, A
, B
, C
, R(42));
189 P(C
, D
, E
, A
, B
, R(43));
190 P(B
, C
, D
, E
, A
, R(44));
191 P(A
, B
, C
, D
, E
, R(45));
192 P(E
, A
, B
, C
, D
, R(46));
193 P(D
, E
, A
, B
, C
, R(47));
194 P(C
, D
, E
, A
, B
, R(48));
195 P(B
, C
, D
, E
, A
, R(49));
196 P(A
, B
, C
, D
, E
, R(50));
197 P(E
, A
, B
, C
, D
, R(51));
198 P(D
, E
, A
, B
, C
, R(52));
199 P(C
, D
, E
, A
, B
, R(53));
200 P(B
, C
, D
, E
, A
, R(54));
201 P(A
, B
, C
, D
, E
, R(55));
202 P(E
, A
, B
, C
, D
, R(56));
203 P(D
, E
, A
, B
, C
, R(57));
204 P(C
, D
, E
, A
, B
, R(58));
205 P(B
, C
, D
, E
, A
, R(59));
210 #define F(x,y,z) (x ^ y ^ z)
213 P(A
, B
, C
, D
, E
, R(60));
214 P(E
, A
, B
, C
, D
, R(61));
215 P(D
, E
, A
, B
, C
, R(62));
216 P(C
, D
, E
, A
, B
, R(63));
217 P(B
, C
, D
, E
, A
, R(64));
218 P(A
, B
, C
, D
, E
, R(65));
219 P(E
, A
, B
, C
, D
, R(66));
220 P(D
, E
, A
, B
, C
, R(67));
221 P(C
, D
, E
, A
, B
, R(68));
222 P(B
, C
, D
, E
, A
, R(69));
223 P(A
, B
, C
, D
, E
, R(70));
224 P(E
, A
, B
, C
, D
, R(71));
225 P(D
, E
, A
, B
, C
, R(72));
226 P(C
, D
, E
, A
, B
, R(73));
227 P(B
, C
, D
, E
, A
, R(74));
228 P(A
, B
, C
, D
, E
, R(75));
229 P(E
, A
, B
, C
, D
, R(76));
230 P(D
, E
, A
, B
, C
, R(77));
231 P(C
, D
, E
, A
, B
, R(78));
232 P(B
, C
, D
, E
, A
, R(79));
245 * SHA-1 process buffer
247 void sha1_update(sha1_context
* ctx
, const unsigned char *input
, int ilen
)
255 left
= ctx
->total
[0] & 0x3F;
258 ctx
->total
[0] += ilen
;
259 ctx
->total
[0] &= 0xFFFFFFFF;
261 if (ctx
->total
[0] < (unsigned long)ilen
)
264 if (left
&& ilen
>= fill
) {
265 memcpy((void *)(ctx
->buffer
+ left
), (const void *)input
, fill
);
266 sha1_process(ctx
, ctx
->buffer
);
273 sha1_process(ctx
, input
);
279 memcpy((void *)(ctx
->buffer
+ left
), (const void *)input
, ilen
);
283 static const unsigned char sha1_padding
[64] = {
284 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
293 void sha1_finish(sha1_context
* ctx
, unsigned char output
[20])
295 unsigned long last
, padn
;
296 unsigned long high
, low
;
297 unsigned char msglen
[8];
299 high
= (ctx
->total
[0] >> 29)
300 | (ctx
->total
[1] << 3);
301 low
= (ctx
->total
[0] << 3);
303 PUT_ULONG_BE(high
, msglen
, 0);
304 PUT_ULONG_BE(low
, msglen
, 4);
306 last
= ctx
->total
[0] & 0x3F;
307 padn
= (last
< 56) ? (56 - last
) : (120 - last
);
309 sha1_update(ctx
, sha1_padding
, padn
);
310 sha1_update(ctx
, msglen
, 8);
312 PUT_ULONG_BE(ctx
->state
[0], output
, 0);
313 PUT_ULONG_BE(ctx
->state
[1], output
, 4);
314 PUT_ULONG_BE(ctx
->state
[2], output
, 8);
315 PUT_ULONG_BE(ctx
->state
[3], output
, 12);
316 PUT_ULONG_BE(ctx
->state
[4], output
, 16);
320 * output = SHA-1( input buffer )
322 void sha1(const unsigned char *input
, int ilen
, unsigned char output
[20])
327 sha1_update(&ctx
, input
, ilen
);
328 sha1_finish(&ctx
, output
);
330 memset(&ctx
, 0, sizeof(sha1_context
));
334 * output = SHA-1( file contents )
336 int sha1_file(const char *path
, unsigned char output
[20])
341 unsigned char buf
[1024];
343 if ((f
= fopen(path
, "rb")) == NULL
)
348 while ((n
= fread(buf
, 1, sizeof(buf
), f
)) > 0)
349 sha1_update(&ctx
, buf
, (int)n
);
351 sha1_finish(&ctx
, output
);
353 memset(&ctx
, 0, sizeof(sha1_context
));
355 if (ferror(f
) != 0) {
365 * SHA-1 HMAC context setup
367 void sha1_hmac_starts(sha1_context
* ctx
, const unsigned char *key
, int keylen
)
370 unsigned char sum
[20];
373 sha1(key
, keylen
, sum
);
378 memset(ctx
->ipad
, 0x36, 64);
379 memset(ctx
->opad
, 0x5C, 64);
381 for (i
= 0; i
< keylen
; i
++) {
382 ctx
->ipad
[i
] = (unsigned char)(ctx
->ipad
[i
] ^ key
[i
]);
383 ctx
->opad
[i
] = (unsigned char)(ctx
->opad
[i
] ^ key
[i
]);
387 sha1_update(ctx
, ctx
->ipad
, 64);
389 memset(sum
, 0, sizeof(sum
));
393 * SHA-1 HMAC process buffer
395 void sha1_hmac_update(sha1_context
* ctx
, const unsigned char *input
, int ilen
)
397 sha1_update(ctx
, input
, ilen
);
401 * SHA-1 HMAC final digest
403 void sha1_hmac_finish(sha1_context
* ctx
, unsigned char output
[20])
405 unsigned char tmpbuf
[20];
407 sha1_finish(ctx
, tmpbuf
);
409 sha1_update(ctx
, ctx
->opad
, 64);
410 sha1_update(ctx
, tmpbuf
, 20);
411 sha1_finish(ctx
, output
);
413 memset(tmpbuf
, 0, sizeof(tmpbuf
));
417 * output = HMAC-SHA-1( hmac key, input buffer )
419 void sha1_hmac(const unsigned char *key
, int keylen
,
420 const unsigned char *input
, int ilen
,
421 unsigned char output
[20])
425 sha1_hmac_starts(&ctx
, key
, keylen
);
426 sha1_hmac_update(&ctx
, input
, ilen
);
427 sha1_hmac_finish(&ctx
, output
);
429 memset(&ctx
, 0, sizeof(sha1_context
));
432 #if defined(TROPICSSL_SELF_TEST)
434 * FIPS-180-1 test vectors
436 static unsigned char sha1_test_buf
[3][57] = {
438 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
442 static const int sha1_test_buflen
[3] = {
446 static const unsigned char sha1_test_sum
[3][20] = {
448 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
449 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D},
451 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
452 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1},
454 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
455 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F}
459 * RFC 2202 test vectors
461 static unsigned char sha1_hmac_test_key
[7][26] = {
463 "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
467 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
470 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
471 "\x11\x12\x13\x14\x15\x16\x17\x18\x19"},
473 "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
475 {""}, /* 0xAA 80 times */
479 static const int sha1_hmac_test_keylen
[7] = {
480 20, 4, 20, 25, 20, 80, 80
483 static unsigned char sha1_hmac_test_buf
[7][74] = {
485 {"what do ya want for nothing?"},
487 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
488 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
489 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
490 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
491 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"},
493 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
494 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
495 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
496 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
497 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"},
498 {"Test With Truncation"},
499 {"Test Using Larger Than Block-Size Key - Hash Key First"},
501 "Test Using Larger Than Block-Size Key and Larger"
502 " Than One Block-Size Data"}
505 static const int sha1_hmac_test_buflen
[7] = {
506 8, 28, 50, 50, 20, 54, 73
509 static const unsigned char sha1_hmac_test_sum
[7][20] = {
511 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
512 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00},
514 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
515 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79},
517 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
518 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3},
520 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
521 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA},
523 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
526 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
527 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12},
529 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
530 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91}
536 int sha1_self_test(int verbose
)
539 unsigned char buf
[1024];
540 unsigned char sha1sum
[20];
546 for (i
= 0; i
< 3; i
++) {
548 printf(" SHA-1 test #%d: ", i
+ 1);
553 memset(buf
, 'a', buflen
= 1000);
555 for (j
= 0; j
< 1000; j
++)
556 sha1_update(&ctx
, buf
, buflen
);
558 sha1_update(&ctx
, sha1_test_buf
[i
],
559 sha1_test_buflen
[i
]);
561 sha1_finish(&ctx
, sha1sum
);
563 if (memcmp(sha1sum
, sha1_test_sum
[i
], 20) != 0) {
577 for (i
= 0; i
< 7; i
++) {
579 printf(" HMAC-SHA-1 test #%d: ", i
+ 1);
581 if (i
== 5 || i
== 6) {
582 memset(buf
, '\xAA', buflen
= 80);
583 sha1_hmac_starts(&ctx
, buf
, buflen
);
585 sha1_hmac_starts(&ctx
, sha1_hmac_test_key
[i
],
586 sha1_hmac_test_keylen
[i
]);
588 sha1_hmac_update(&ctx
, sha1_hmac_test_buf
[i
],
589 sha1_hmac_test_buflen
[i
]);
591 sha1_hmac_finish(&ctx
, sha1sum
);
593 buflen
= (i
== 4) ? 12 : 20;
595 if (memcmp(sha1sum
, sha1_hmac_test_sum
[i
], buflen
) != 0) {