Debian Codegraph @

tests/sha1.c

raw
1 /* sha1.c - SHA1 and SHA2 hash function
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 * Copyright (C) 2004, 2009 g10 Code GmbH
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt 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 Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* These are simplified SHA-1 and SHA-256 versions taken from the
22 * libgrypt and gpg4win's sha1sum.c. We need them for some tests
23 * (e.g. OCSP).
24 */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #ifndef HAVE_TYPE_U32
33 #undef u32 /* maybe there is a macro with this name */
34 #if SIZEOF_UNSIGNED_INT == 4
35 typedef unsigned int u32;
36 #elif SIZEOF_UNSIGNED_LONG == 4
37 typedef unsigned long u32;
38 #else
39 #error no typedef for u32
40 #endif
41 #define HAVE_TYPE_U32
42 #endif
43
44 typedef struct
45 {
46 u32 h0,h1,h2,h3,h4;
47 u32 nblocks;
48 unsigned char buf[64];
49 int count;
50 } sha1_context_t;
51
52 typedef struct
53 {
54 u32 h0,h1,h2,h3,h4,h5,h6,h7;
55 u32 nblocks;
56 unsigned char buf[64];
57 int count;
58 } sha256_context_t;
59
60
61 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
62 #define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
63
64
65 static void
66 sha1_init (void *context)
67 {
68 sha1_context_t *hd = context;
69
70 hd->h0 = 0x67452301;
71 hd->h1 = 0xefcdab89;
72 hd->h2 = 0x98badcfe;
73 hd->h3 = 0x10325476;
74 hd->h4 = 0xc3d2e1f0;
75 hd->nblocks = 0;
76 hd->count = 0;
77 }
78
79 static void
80 sha256_init (void *context)
81 {
82 sha256_context_t *hd = context;
83
84 hd->h0 = 0x6a09e667;
85 hd->h1 = 0xbb67ae85;
86 hd->h2 = 0x3c6ef372;
87 hd->h3 = 0xa54ff53a;
88 hd->h4 = 0x510e527f;
89 hd->h5 = 0x9b05688c;
90 hd->h6 = 0x1f83d9ab;
91 hd->h7 = 0x5be0cd19;
92
93 hd->nblocks = 0;
94 hd->count = 0;
95 }
96
97
98 /****************
99 * Transform the message X which consists of 16 32-bit-words
100 */
101 static void
102 transform (sha1_context_t *hd, unsigned char *data )
103 {
104 register u32 a,b,c,d,e,tm;
105 u32 x[16];
106
107 /* Get values from the chaining vars. */
108 a = hd->h0;
109 b = hd->h1;
110 c = hd->h2;
111 d = hd->h3;
112 e = hd->h4;
113
114 #ifdef WORDS_BIGENDIAN
115 memcpy( x, data, 64 );
116 #else
117 {
118 int i;
119 unsigned char *p2;
120 for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 )
121 {
122 p2[3] = *data++;
123 p2[2] = *data++;
124 p2[1] = *data++;
125 p2[0] = *data++;
126 }
127 }
128 #endif
129
130
131 #define K1 0x5A827999L
132 #define K2 0x6ED9EBA1L
133 #define K3 0x8F1BBCDCL
134 #define K4 0xCA62C1D6L
135 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
136 #define F2(x,y,z) ( x ^ y ^ z )
137 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
138 #define F4(x,y,z) ( x ^ y ^ z )
139
140
141 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
142 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
143 , (x[i&0x0f] = rol(tm, 1)) )
144
145 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
146 + f( b, c, d ) \
147 + k \
148 + m; \
149 b = rol( b, 30 ); \
150 } while(0)
151 R( a, b, c, d, e, F1, K1, x[ 0] );
152 R( e, a, b, c, d, F1, K1, x[ 1] );
153 R( d, e, a, b, c, F1, K1, x[ 2] );
154 R( c, d, e, a, b, F1, K1, x[ 3] );
155 R( b, c, d, e, a, F1, K1, x[ 4] );
156 R( a, b, c, d, e, F1, K1, x[ 5] );
157 R( e, a, b, c, d, F1, K1, x[ 6] );
158 R( d, e, a, b, c, F1, K1, x[ 7] );
159 R( c, d, e, a, b, F1, K1, x[ 8] );
160 R( b, c, d, e, a, F1, K1, x[ 9] );
161 R( a, b, c, d, e, F1, K1, x[10] );
162 R( e, a, b, c, d, F1, K1, x[11] );
163 R( d, e, a, b, c, F1, K1, x[12] );
164 R( c, d, e, a, b, F1, K1, x[13] );
165 R( b, c, d, e, a, F1, K1, x[14] );
166 R( a, b, c, d, e, F1, K1, x[15] );
167 R( e, a, b, c, d, F1, K1, M(16) );
168 R( d, e, a, b, c, F1, K1, M(17) );
169 R( c, d, e, a, b, F1, K1, M(18) );
170 R( b, c, d, e, a, F1, K1, M(19) );
171 R( a, b, c, d, e, F2, K2, M(20) );
172 R( e, a, b, c, d, F2, K2, M(21) );
173 R( d, e, a, b, c, F2, K2, M(22) );
174 R( c, d, e, a, b, F2, K2, M(23) );
175 R( b, c, d, e, a, F2, K2, M(24) );
176 R( a, b, c, d, e, F2, K2, M(25) );
177 R( e, a, b, c, d, F2, K2, M(26) );
178 R( d, e, a, b, c, F2, K2, M(27) );
179 R( c, d, e, a, b, F2, K2, M(28) );
180 R( b, c, d, e, a, F2, K2, M(29) );
181 R( a, b, c, d, e, F2, K2, M(30) );
182 R( e, a, b, c, d, F2, K2, M(31) );
183 R( d, e, a, b, c, F2, K2, M(32) );
184 R( c, d, e, a, b, F2, K2, M(33) );
185 R( b, c, d, e, a, F2, K2, M(34) );
186 R( a, b, c, d, e, F2, K2, M(35) );
187 R( e, a, b, c, d, F2, K2, M(36) );
188 R( d, e, a, b, c, F2, K2, M(37) );
189 R( c, d, e, a, b, F2, K2, M(38) );
190 R( b, c, d, e, a, F2, K2, M(39) );
191 R( a, b, c, d, e, F3, K3, M(40) );
192 R( e, a, b, c, d, F3, K3, M(41) );
193 R( d, e, a, b, c, F3, K3, M(42) );
194 R( c, d, e, a, b, F3, K3, M(43) );
195 R( b, c, d, e, a, F3, K3, M(44) );
196 R( a, b, c, d, e, F3, K3, M(45) );
197 R( e, a, b, c, d, F3, K3, M(46) );
198 R( d, e, a, b, c, F3, K3, M(47) );
199 R( c, d, e, a, b, F3, K3, M(48) );
200 R( b, c, d, e, a, F3, K3, M(49) );
201 R( a, b, c, d, e, F3, K3, M(50) );
202 R( e, a, b, c, d, F3, K3, M(51) );
203 R( d, e, a, b, c, F3, K3, M(52) );
204 R( c, d, e, a, b, F3, K3, M(53) );
205 R( b, c, d, e, a, F3, K3, M(54) );
206 R( a, b, c, d, e, F3, K3, M(55) );
207 R( e, a, b, c, d, F3, K3, M(56) );
208 R( d, e, a, b, c, F3, K3, M(57) );
209 R( c, d, e, a, b, F3, K3, M(58) );
210 R( b, c, d, e, a, F3, K3, M(59) );
211 R( a, b, c, d, e, F4, K4, M(60) );
212 R( e, a, b, c, d, F4, K4, M(61) );
213 R( d, e, a, b, c, F4, K4, M(62) );
214 R( c, d, e, a, b, F4, K4, M(63) );
215 R( b, c, d, e, a, F4, K4, M(64) );
216 R( a, b, c, d, e, F4, K4, M(65) );
217 R( e, a, b, c, d, F4, K4, M(66) );
218 R( d, e, a, b, c, F4, K4, M(67) );
219 R( c, d, e, a, b, F4, K4, M(68) );
220 R( b, c, d, e, a, F4, K4, M(69) );
221 R( a, b, c, d, e, F4, K4, M(70) );
222 R( e, a, b, c, d, F4, K4, M(71) );
223 R( d, e, a, b, c, F4, K4, M(72) );
224 R( c, d, e, a, b, F4, K4, M(73) );
225 R( b, c, d, e, a, F4, K4, M(74) );
226 R( a, b, c, d, e, F4, K4, M(75) );
227 R( e, a, b, c, d, F4, K4, M(76) );
228 R( d, e, a, b, c, F4, K4, M(77) );
229 R( c, d, e, a, b, F4, K4, M(78) );
230 R( b, c, d, e, a, F4, K4, M(79) );
231
232 /* Update chaining vars. */
233 hd->h0 += a;
234 hd->h1 += b;
235 hd->h2 += c;
236 hd->h3 += d;
237 hd->h4 += e;
238
239 #undef R
240 #undef M
241 #undef F1
242 #undef F2
243 #undef F3
244 #undef F4
245 #undef K1
246 #undef K2
247 #undef K3
248 #undef K4
249 }
250
251
252 # define Cho(x,y,z) (z ^ (x & (y ^ z))) /* (4.2) same as SHA-1's F1 */
253 # define Maj(x,y,z) ((x & y) | (z & (x|y))) /* (4.3) same as SHA-1's F3 */
254 # define Sum0(x) (ror ((x), 2) ^ ror ((x), 13) ^ ror ((x), 22)) /* (4.4) */
255 # define Sum1(x) (ror ((x), 6) ^ ror ((x), 11) ^ ror ((x), 25)) /* (4.5) */
256 # define S0(x) (ror ((x), 7) ^ ror ((x), 18) ^ ((x) >> 3)) /* (4.6) */
257 # define S1(x) (ror ((x), 17) ^ ror ((x), 19) ^ ((x) >> 10)) /* (4.7) */
258 # define R(a,b,c,d,e,f,g,h,k,w) do \
259 { \
260 t1 = (h) + Sum1((e)) + Cho((e),(f),(g)) + (k) + (w); \
261 t2 = Sum0((a)) + Maj((a),(b),(c)); \
262 h = g; \
263 g = f; \
264 f = e; \
265 e = d + t1; \
266 d = c; \
267 c = b; \
268 b = a; \
269 a = t1 + t2; \
270 } while (0)
271
272 static void
273 transform256 (sha256_context_t *hd, unsigned char *data)
274 {
275 static const u32 K[64] = {
276 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
277 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
278 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
279 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
280 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
281 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
282 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
283 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
284 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
285 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
286 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
287 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
288 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
289 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
290 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
291 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
292 };
293 u32 a,b,c,d,e,f,g,h,t1,t2;
294 u32 x[16];
295 u32 w[64];
296 int i;
297 a = hd->h0;
298 b = hd->h1;
299 c = hd->h2;
300 d = hd->h3;
301 e = hd->h4;
302 f = hd->h5;
303 g = hd->h6;
304 h = hd->h7;
305
306 #ifdef WORDS_BIGENDIAN
307 memcpy (x, data, 64);
308 #else
309 {
310 unsigned char *p2;
311
312 for (i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 )
313 {
314 p2[3] = *data++;
315 p2[2] = *data++;
316 p2[1] = *data++;
317 p2[0] = *data++;
318 }
319 }
320 #endif
321
322 for (i=0; i < 16; i++)
323 w[i] = x[i];
324 for (; i < 64; i++)
325 w[i] = S1(w[i-2]) + w[i-7] + S0(w[i-15]) + w[i-16];
326
327 for (i=0; i < 64; i++)
328 R(a,b,c,d,e,f,g,h,K[i],w[i]);
329
330 hd->h0 += a;
331 hd->h1 += b;
332 hd->h2 += c;
333 hd->h3 += d;
334 hd->h4 += e;
335 hd->h5 += f;
336 hd->h6 += g;
337 hd->h7 += h;
338 }
339 # undef Cho
340 # undef Maj
341 # undef Sum0
342 # undef Sum1
343 # undef S0
344 # undef S1
345 # undef R
346
347
348
349 /* Update the message digest with the contents
350 * of INBUF with length INLEN.
351 */
352 static void
353 sha1_write( void *context, unsigned char *inbuf, size_t inlen)
354 {
355 sha1_context_t *hd = context;
356
357 if( hd->count == 64 ) /* flush the buffer */
358 {
359 transform( hd, hd->buf );
360 hd->count = 0;
361 hd->nblocks++;
362 }
363 if( !inbuf )
364 return;
365
366 if( hd->count )
367 {
368 for( ; inlen && hd->count < 64; inlen-- )
369 hd->buf[hd->count++] = *inbuf++;
370 sha1_write( hd, NULL, 0 );
371 if( !inlen )
372 return;
373 }
374
375 while( inlen >= 64 )
376 {
377 transform( hd, inbuf );
378 hd->count = 0;
379 hd->nblocks++;
380 inlen -= 64;
381 inbuf += 64;
382 }
383 for( ; inlen && hd->count < 64; inlen-- )
384 hd->buf[hd->count++] = *inbuf++;
385 }
386
387
388 /* Update the message digest with the contents
389 * of INBUF with length INLEN.
390 */
391 static void
392 sha256_write (void *context, unsigned char *inbuf, size_t inlen)
393 {
394 sha256_context_t *hd = context;
395
396 if( hd->count == 64 ) /* flush the buffer */
397 {
398 transform256( hd, hd->buf );
399 hd->count = 0;
400 hd->nblocks++;
401 }
402 if( !inbuf )
403 return;
404
405 if( hd->count )
406 {
407 for( ; inlen && hd->count < 64; inlen-- )
408 hd->buf[hd->count++] = *inbuf++;
409 sha256_write( hd, NULL, 0 );
410 if( !inlen )
411 return;
412 }
413
414 while( inlen >= 64 )
415 {
416 transform256( hd, inbuf );
417 hd->count = 0;
418 hd->nblocks++;
419 inlen -= 64;
420 inbuf += 64;
421 }
422 for( ; inlen && hd->count < 64; inlen-- )
423 hd->buf[hd->count++] = *inbuf++;
424 }
425
426
427 /* The routine final terminates the computation and
428 * returns the digest.
429 * The handle is prepared for a new cycle, but adding bytes to the
430 * handle will the destroy the returned buffer.
431 * Returns: 20 bytes representing the digest.
432 */
433
434 static void
435 sha1_final(void *context)
436 {
437 sha1_context_t *hd = context;
438
439 u32 t, msb, lsb;
440 unsigned char *p;
441
442 sha1_write(hd, NULL, 0); /* flush */;
443
444 t = hd->nblocks;
445 /* multiply by 64 to make a byte count */
446 lsb = t << 6;
447 msb = t >> 26;
448 /* add the count */
449 t = lsb;
450 if( (lsb += hd->count) < t )
451 msb++;
452 /* multiply by 8 to make a bit count */
453 t = lsb;
454 lsb <<= 3;
455 msb <<= 3;
456 msb |= t >> 29;
457
458 if( hd->count < 56 ) /* enough room */
459 {
460 hd->buf[hd->count++] = 0x80; /* pad */
461 while( hd->count < 56 )
462 hd->buf[hd->count++] = 0; /* pad */
463 }
464 else /* need one extra block */
465 {
466 hd->buf[hd->count++] = 0x80; /* pad character */
467 while( hd->count < 64 )
468 hd->buf[hd->count++] = 0;
469 sha1_write(hd, NULL, 0); /* flush */;
470 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
471 }
472 /* append the 64 bit count */
473 hd->buf[56] = msb >> 24;
474 hd->buf[57] = msb >> 16;
475 hd->buf[58] = msb >> 8;
476 hd->buf[59] = msb ;
477 hd->buf[60] = lsb >> 24;
478 hd->buf[61] = lsb >> 16;
479 hd->buf[62] = lsb >> 8;
480 hd->buf[63] = lsb ;
481 transform( hd, hd->buf );
482
483 p = hd->buf;
484 #ifdef WORDS_BIGENDIAN
485 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
486 #else /* little endian */
487 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
488 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
489 #endif
490 X(0);
491 X(1);
492 X(2);
493 X(3);
494 X(4);
495 #undef X
496
497 }
498
499 static void
500 sha256_final(void *context)
501 {
502 sha256_context_t *hd = context;
503
504 u32 t, msb, lsb;
505 unsigned char *p;
506
507 sha256_write(hd, NULL, 0); /* flush */;
508
509 t = hd->nblocks;
510 /* multiply by 64 to make a byte count */
511 lsb = t << 6;
512 msb = t >> 26;
513 /* add the count */
514 t = lsb;
515 if( (lsb += hd->count) < t )
516 msb++;
517 /* multiply by 8 to make a bit count */
518 t = lsb;
519 lsb <<= 3;
520 msb <<= 3;
521 msb |= t >> 29;
522
523 if( hd->count < 56 ) /* enough room */
524 {
525 hd->buf[hd->count++] = 0x80; /* pad */
526 while( hd->count < 56 )
527 hd->buf[hd->count++] = 0; /* pad */
528 }
529 else /* need one extra block */
530 {
531 hd->buf[hd->count++] = 0x80; /* pad character */
532 while( hd->count < 64 )
533 hd->buf[hd->count++] = 0;
534 sha256_write(hd, NULL, 0); /* flush */;
535 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
536 }
537 /* append the 64 bit count */
538 hd->buf[56] = msb >> 24;
539 hd->buf[57] = msb >> 16;
540 hd->buf[58] = msb >> 8;
541 hd->buf[59] = msb ;
542 hd->buf[60] = lsb >> 24;
543 hd->buf[61] = lsb >> 16;
544 hd->buf[62] = lsb >> 8;
545 hd->buf[63] = lsb ;
546 transform256( hd, hd->buf );
547
548 p = hd->buf;
549 #ifdef WORDS_BIGENDIAN
550 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
551 #else /* little endian */
552 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
553 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
554 #endif
555 X(0);
556 X(1);
557 X(2);
558 X(3);
559 X(4);
560 X(5);
561 X(6);
562 X(7);
563 #undef X
564 }
565
566
567 void
568 sha1_hash_buffer (char *outbuf, const char *buffer, size_t length)
569 {
570 sha1_context_t hd;
571
572 sha1_init (&hd);
573 sha1_write (&hd, (unsigned char *)buffer, length);
574 sha1_final (&hd);
575 memcpy (outbuf, hd.buf, 20);
576 }
577
578
579 void
580 sha256_hash_buffer (char *outbuf, const char *buffer, size_t length)
581 {
582 static int selftest;
583 sha256_context_t hd;
584
585 if (!selftest)
586 {
587 unsigned char result[32];
588 selftest = 1;
589
590 sha256_hash_buffer (result, "abc", 3);
591 if (memcmp
592 (result,
593 "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
594 "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad",
595 32))
596 {
597 fputs ("fatal: internal SHA256 selftest failed\n", stderr);
598 exit (8);
599 }
600 }
601
602 sha256_init (&hd);
603 sha256_write (&hd, (unsigned char *)buffer, length);
604 sha256_final (&hd);
605 memcpy (outbuf, hd.buf, 32);
606 }