ssl_tls: fix format warning in ssl_parse_certificate() on x86_64
[tropicssl.git] / library / sha1.c
blob57b9184e176000bd30f74d8a92cb0a061483af05
1 /*
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>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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"
47 #include <string.h>
48 #include <stdio.h>
51 * 32-bit integer manipulation macros (big endian)
53 #ifndef GET_ULONG_BE
54 #define GET_ULONG_BE(n,b,i) \
55 { \
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] ); \
61 #endif
63 #ifndef PUT_ULONG_BE
64 #define PUT_ULONG_BE(n,b,i) \
65 { \
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) ); \
71 #endif
74 * SHA-1 context setup
76 void sha1_starts(sha1_context * ctx)
78 ctx->total[0] = 0;
79 ctx->total[1] = 0;
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)))
111 #define R(t) \
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); \
123 A = ctx->state[0];
124 B = ctx->state[1];
125 C = ctx->state[2];
126 D = ctx->state[3];
127 E = ctx->state[4];
129 #define F(x,y,z) (z ^ (x & (y ^ z)))
130 #define K 0x5A827999
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));
153 #undef K
154 #undef F
156 #define F(x,y,z) (x ^ y ^ z)
157 #define K 0x6ED9EBA1
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));
180 #undef K
181 #undef F
183 #define F(x,y,z) ((x & y) | (z & (x | y)))
184 #define K 0x8F1BBCDC
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));
207 #undef K
208 #undef F
210 #define F(x,y,z) (x ^ y ^ z)
211 #define K 0xCA62C1D6
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));
234 #undef K
235 #undef F
237 ctx->state[0] += A;
238 ctx->state[1] += B;
239 ctx->state[2] += C;
240 ctx->state[3] += D;
241 ctx->state[4] += E;
245 * SHA-1 process buffer
247 void sha1_update(sha1_context * ctx, const unsigned char *input, int ilen)
249 int fill;
250 unsigned long left;
252 if (ilen <= 0)
253 return;
255 left = ctx->total[0] & 0x3F;
256 fill = 64 - left;
258 ctx->total[0] += ilen;
259 ctx->total[0] &= 0xFFFFFFFF;
261 if (ctx->total[0] < (unsigned long)ilen)
262 ctx->total[1]++;
264 if (left && ilen >= fill) {
265 memcpy((void *)(ctx->buffer + left), (const void *)input, fill);
266 sha1_process(ctx, ctx->buffer);
267 input += fill;
268 ilen -= fill;
269 left = 0;
272 while (ilen >= 64) {
273 sha1_process(ctx, input);
274 input += 64;
275 ilen -= 64;
278 if (ilen > 0) {
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
291 * SHA-1 final digest
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])
324 sha1_context ctx;
326 sha1_starts(&ctx);
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])
338 FILE *f;
339 size_t n;
340 sha1_context ctx;
341 unsigned char buf[1024];
343 if ((f = fopen(path, "rb")) == NULL)
344 return (1);
346 sha1_starts(&ctx);
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) {
356 fclose(f);
357 return (2);
360 fclose(f);
361 return (0);
365 * SHA-1 HMAC context setup
367 void sha1_hmac_starts(sha1_context * ctx, const unsigned char *key, int keylen)
369 int i;
370 unsigned char sum[20];
372 if (keylen > 64) {
373 sha1(key, keylen, sum);
374 keylen = 20;
375 key = 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]);
386 sha1_starts(ctx);
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);
408 sha1_starts(ctx);
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])
423 sha1_context ctx;
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] = {
437 {"abc"},
438 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
439 {""}
442 static const int sha1_test_buflen[3] = {
443 3, 56, 1000
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"
464 "\x0B\x0B\x0B\x0B"},
465 {"Jefe"},
467 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
468 "\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"
474 "\x0C\x0C\x0C\x0C"},
475 {""}, /* 0xAA 80 times */
476 {""}
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] = {
484 {"Hi There"},
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,
524 0x7B, 0xE1},
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}
534 * Checkup routine
536 int sha1_self_test(int verbose)
538 int i, j, buflen;
539 unsigned char buf[1024];
540 unsigned char sha1sum[20];
541 sha1_context ctx;
544 * SHA-1
546 for (i = 0; i < 3; i++) {
547 if (verbose != 0)
548 printf(" SHA-1 test #%d: ", i + 1);
550 sha1_starts(&ctx);
552 if (i == 2) {
553 memset(buf, 'a', buflen = 1000);
555 for (j = 0; j < 1000; j++)
556 sha1_update(&ctx, buf, buflen);
557 } else
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) {
564 if (verbose != 0)
565 printf("failed\n");
567 return (1);
570 if (verbose != 0)
571 printf("passed\n");
574 if (verbose != 0)
575 printf("\n");
577 for (i = 0; i < 7; i++) {
578 if (verbose != 0)
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);
584 } else
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) {
596 if (verbose != 0)
597 printf("failed\n");
599 return (1);
602 if (verbose != 0)
603 printf("passed\n");
606 if (verbose != 0)
607 printf("\n");
609 return (0);
612 #endif
614 #endif