Upstream sync.
[shishi.git] / crypto / cipher / sha1.c
bloba6339b384311351825d6a3c353c3534cc1bc1fbc
1 /* sha1.c - SHA1 hash function
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /* Test vectors:
24 * "abc"
25 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
27 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <assert.h>
37 #include "g10lib.h"
38 #include "memory.h"
39 #include "dynload.h"
40 #include "bithelp.h"
43 typedef struct {
44 u32 h0,h1,h2,h3,h4;
45 u32 nblocks;
46 byte buf[64];
47 int count;
48 } SHA1_CONTEXT;
51 static void
52 sha1_init( SHA1_CONTEXT *hd )
54 hd->h0 = 0x67452301;
55 hd->h1 = 0xefcdab89;
56 hd->h2 = 0x98badcfe;
57 hd->h3 = 0x10325476;
58 hd->h4 = 0xc3d2e1f0;
59 hd->nblocks = 0;
60 hd->count = 0;
64 /****************
65 * Transform the message X which consists of 16 32-bit-words
67 static void
68 transform( SHA1_CONTEXT *hd, byte *data )
70 register u32 a,b,c,d,e,tm;
71 u32 x[16];
73 /* get values from the chaining vars */
74 a = hd->h0;
75 b = hd->h1;
76 c = hd->h2;
77 d = hd->h3;
78 e = hd->h4;
80 #ifdef BIG_ENDIAN_HOST
81 memcpy( x, data, 64 );
82 #else
83 { int i;
84 byte *p2;
85 for(i=0, p2=(byte*)x; i < 16; i++, p2 += 4 ) {
86 p2[3] = *data++;
87 p2[2] = *data++;
88 p2[1] = *data++;
89 p2[0] = *data++;
92 #endif
95 #define K1 0x5A827999L
96 #define K2 0x6ED9EBA1L
97 #define K3 0x8F1BBCDCL
98 #define K4 0xCA62C1D6L
99 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
100 #define F2(x,y,z) ( x ^ y ^ z )
101 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
102 #define F4(x,y,z) ( x ^ y ^ z )
105 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
106 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
107 , (x[i&0x0f] = rol(tm, 1)) )
109 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
110 + f( b, c, d ) \
111 + k \
112 + m; \
113 b = rol( b, 30 ); \
114 } while(0)
115 R( a, b, c, d, e, F1, K1, x[ 0] );
116 R( e, a, b, c, d, F1, K1, x[ 1] );
117 R( d, e, a, b, c, F1, K1, x[ 2] );
118 R( c, d, e, a, b, F1, K1, x[ 3] );
119 R( b, c, d, e, a, F1, K1, x[ 4] );
120 R( a, b, c, d, e, F1, K1, x[ 5] );
121 R( e, a, b, c, d, F1, K1, x[ 6] );
122 R( d, e, a, b, c, F1, K1, x[ 7] );
123 R( c, d, e, a, b, F1, K1, x[ 8] );
124 R( b, c, d, e, a, F1, K1, x[ 9] );
125 R( a, b, c, d, e, F1, K1, x[10] );
126 R( e, a, b, c, d, F1, K1, x[11] );
127 R( d, e, a, b, c, F1, K1, x[12] );
128 R( c, d, e, a, b, F1, K1, x[13] );
129 R( b, c, d, e, a, F1, K1, x[14] );
130 R( a, b, c, d, e, F1, K1, x[15] );
131 R( e, a, b, c, d, F1, K1, M(16) );
132 R( d, e, a, b, c, F1, K1, M(17) );
133 R( c, d, e, a, b, F1, K1, M(18) );
134 R( b, c, d, e, a, F1, K1, M(19) );
135 R( a, b, c, d, e, F2, K2, M(20) );
136 R( e, a, b, c, d, F2, K2, M(21) );
137 R( d, e, a, b, c, F2, K2, M(22) );
138 R( c, d, e, a, b, F2, K2, M(23) );
139 R( b, c, d, e, a, F2, K2, M(24) );
140 R( a, b, c, d, e, F2, K2, M(25) );
141 R( e, a, b, c, d, F2, K2, M(26) );
142 R( d, e, a, b, c, F2, K2, M(27) );
143 R( c, d, e, a, b, F2, K2, M(28) );
144 R( b, c, d, e, a, F2, K2, M(29) );
145 R( a, b, c, d, e, F2, K2, M(30) );
146 R( e, a, b, c, d, F2, K2, M(31) );
147 R( d, e, a, b, c, F2, K2, M(32) );
148 R( c, d, e, a, b, F2, K2, M(33) );
149 R( b, c, d, e, a, F2, K2, M(34) );
150 R( a, b, c, d, e, F2, K2, M(35) );
151 R( e, a, b, c, d, F2, K2, M(36) );
152 R( d, e, a, b, c, F2, K2, M(37) );
153 R( c, d, e, a, b, F2, K2, M(38) );
154 R( b, c, d, e, a, F2, K2, M(39) );
155 R( a, b, c, d, e, F3, K3, M(40) );
156 R( e, a, b, c, d, F3, K3, M(41) );
157 R( d, e, a, b, c, F3, K3, M(42) );
158 R( c, d, e, a, b, F3, K3, M(43) );
159 R( b, c, d, e, a, F3, K3, M(44) );
160 R( a, b, c, d, e, F3, K3, M(45) );
161 R( e, a, b, c, d, F3, K3, M(46) );
162 R( d, e, a, b, c, F3, K3, M(47) );
163 R( c, d, e, a, b, F3, K3, M(48) );
164 R( b, c, d, e, a, F3, K3, M(49) );
165 R( a, b, c, d, e, F3, K3, M(50) );
166 R( e, a, b, c, d, F3, K3, M(51) );
167 R( d, e, a, b, c, F3, K3, M(52) );
168 R( c, d, e, a, b, F3, K3, M(53) );
169 R( b, c, d, e, a, F3, K3, M(54) );
170 R( a, b, c, d, e, F3, K3, M(55) );
171 R( e, a, b, c, d, F3, K3, M(56) );
172 R( d, e, a, b, c, F3, K3, M(57) );
173 R( c, d, e, a, b, F3, K3, M(58) );
174 R( b, c, d, e, a, F3, K3, M(59) );
175 R( a, b, c, d, e, F4, K4, M(60) );
176 R( e, a, b, c, d, F4, K4, M(61) );
177 R( d, e, a, b, c, F4, K4, M(62) );
178 R( c, d, e, a, b, F4, K4, M(63) );
179 R( b, c, d, e, a, F4, K4, M(64) );
180 R( a, b, c, d, e, F4, K4, M(65) );
181 R( e, a, b, c, d, F4, K4, M(66) );
182 R( d, e, a, b, c, F4, K4, M(67) );
183 R( c, d, e, a, b, F4, K4, M(68) );
184 R( b, c, d, e, a, F4, K4, M(69) );
185 R( a, b, c, d, e, F4, K4, M(70) );
186 R( e, a, b, c, d, F4, K4, M(71) );
187 R( d, e, a, b, c, F4, K4, M(72) );
188 R( c, d, e, a, b, F4, K4, M(73) );
189 R( b, c, d, e, a, F4, K4, M(74) );
190 R( a, b, c, d, e, F4, K4, M(75) );
191 R( e, a, b, c, d, F4, K4, M(76) );
192 R( d, e, a, b, c, F4, K4, M(77) );
193 R( c, d, e, a, b, F4, K4, M(78) );
194 R( b, c, d, e, a, F4, K4, M(79) );
196 /* update chainig vars */
197 hd->h0 += a;
198 hd->h1 += b;
199 hd->h2 += c;
200 hd->h3 += d;
201 hd->h4 += e;
205 /* Update the message digest with the contents
206 * of INBUF with length INLEN.
208 static void
209 sha1_write( SHA1_CONTEXT *hd, byte *inbuf, size_t inlen)
211 if( hd->count == 64 ) { /* flush the buffer */
212 transform( hd, hd->buf );
213 _gcry_burn_stack (88+4*sizeof(void*));
214 hd->count = 0;
215 hd->nblocks++;
217 if( !inbuf )
218 return;
219 if( hd->count ) {
220 for( ; inlen && hd->count < 64; inlen-- )
221 hd->buf[hd->count++] = *inbuf++;
222 sha1_write( hd, NULL, 0 );
223 if( !inlen )
224 return;
227 while( inlen >= 64 ) {
228 transform( hd, inbuf );
229 hd->count = 0;
230 hd->nblocks++;
231 inlen -= 64;
232 inbuf += 64;
234 _gcry_burn_stack (88+4*sizeof(void*));
235 for( ; inlen && hd->count < 64; inlen-- )
236 hd->buf[hd->count++] = *inbuf++;
240 /* The routine final terminates the computation and
241 * returns the digest.
242 * The handle is prepared for a new cycle, but adding bytes to the
243 * handle will the destroy the returned buffer.
244 * Returns: 20 bytes representing the digest.
247 static void
248 sha1_final(SHA1_CONTEXT *hd)
250 u32 t, msb, lsb;
251 byte *p;
253 sha1_write(hd, NULL, 0); /* flush */;
255 t = hd->nblocks;
256 /* multiply by 64 to make a byte count */
257 lsb = t << 6;
258 msb = t >> 26;
259 /* add the count */
260 t = lsb;
261 if( (lsb += hd->count) < t )
262 msb++;
263 /* multiply by 8 to make a bit count */
264 t = lsb;
265 lsb <<= 3;
266 msb <<= 3;
267 msb |= t >> 29;
269 if( hd->count < 56 ) { /* enough room */
270 hd->buf[hd->count++] = 0x80; /* pad */
271 while( hd->count < 56 )
272 hd->buf[hd->count++] = 0; /* pad */
274 else { /* need one extra block */
275 hd->buf[hd->count++] = 0x80; /* pad character */
276 while( hd->count < 64 )
277 hd->buf[hd->count++] = 0;
278 sha1_write(hd, NULL, 0); /* flush */;
279 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
281 /* append the 64 bit count */
282 hd->buf[56] = msb >> 24;
283 hd->buf[57] = msb >> 16;
284 hd->buf[58] = msb >> 8;
285 hd->buf[59] = msb ;
286 hd->buf[60] = lsb >> 24;
287 hd->buf[61] = lsb >> 16;
288 hd->buf[62] = lsb >> 8;
289 hd->buf[63] = lsb ;
290 transform( hd, hd->buf );
291 _gcry_burn_stack (88+4*sizeof(void*));
293 p = hd->buf;
294 #ifdef BIG_ENDIAN_HOST
295 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
296 #else /* little endian */
297 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
298 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
299 #endif
300 X(0);
301 X(1);
302 X(2);
303 X(3);
304 X(4);
305 #undef X
309 static byte *
310 sha1_read( SHA1_CONTEXT *hd )
312 return hd->buf;
315 /****************
316 * Return some information about the algorithm. We need algo here to
317 * distinguish different flavors of the algorithm.
318 * Returns: A pointer to string describing the algorithm or NULL if
319 * the ALGO is invalid.
321 static const char *
322 sha1_get_info( int algo, size_t *contextsize,
323 byte **r_asnoid, int *r_asnlen, int *r_mdlen,
324 void (**r_init)( void *c ),
325 void (**r_write)( void *c, byte *buf, size_t nbytes ),
326 void (**r_final)( void *c ),
327 byte *(**r_read)( void *c )
330 static byte asn[15] = /* Object ID is 1.3.14.3.2.26 */
331 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
332 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
333 if( algo != 2 )
334 return NULL;
336 *contextsize = sizeof(SHA1_CONTEXT);
337 *r_asnoid = asn;
338 *r_asnlen = DIM(asn);
339 *r_mdlen = 20;
340 *(void (**)(SHA1_CONTEXT *))r_init = sha1_init;
341 *(void (**)(SHA1_CONTEXT *, byte*, size_t))r_write = sha1_write;
342 *(void (**)(SHA1_CONTEXT *))r_final = sha1_final;
343 *(byte *(**)(SHA1_CONTEXT *))r_read = sha1_read;
345 return "SHA1";
350 #ifndef IS_MODULE
351 static
352 #endif
353 const char * const gnupgext_version = "SHA1 ($Revision$)";
355 static struct {
356 int class;
357 int version;
358 int value;
359 void (*func)(void);
360 } func_table[] = {
361 { 10, 1, 0, (void(*)(void))sha1_get_info },
362 { 11, 1, 2 },
366 #ifndef IS_MODULE
367 static
368 #endif
369 void *
370 gnupgext_enum_func( int what, int *sequence, int *class, int *vers )
372 void *ret;
373 int i = *sequence;
375 do {
376 if( i >= DIM(func_table) || i < 0 ) {
377 return NULL;
379 *class = func_table[i].class;
380 *vers = func_table[i].version;
381 switch( *class ) {
382 case 11:
383 case 21:
384 case 31:
385 ret = &func_table[i].value;
386 break;
387 default:
388 ret = func_table[i].func;
389 break;
391 i++;
392 } while( what && what != *class );
394 *sequence = i;
395 return ret;
401 #ifndef IS_MODULE
402 void
403 _gcry_sha1_constructor(void)
405 _gcry_register_internal_cipher_extension( gnupgext_version, gnupgext_enum_func );
407 #endif