cscvs to tla changeset 77
[onak.git] / sha.c
1 /* sha1.c - SHA1 hash function
2  *      Copyright (C) 2001 V. Alex Brennen
3  *
4  * Please see below for more legal information!
5  *
6  * This file is part of the CryptNET openPGP Public Keyserver (CKS).
7  *
8  * CKS 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.
12  *
13  * CKS 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.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21  *
22  * $Id: sha.c,v 1.2 2003/06/04 20:57:12 noodles Exp $
23  */
24
25 /* This file was copied from GnuPG                               */
26 /* Some portions Copyright (C) 1998 The Free Software Foundation */
27
28 #include "sha.h"
29
30
31 /*  Test vectors:
32  *
33  *  "abc"
34  *  A999 3E36 4706 816A BA3E  2571 7850 C26C 9CD0 D89D
35  *
36  *  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
37  *  8498 3E44 1C3B D26E BAAE  4AA1 F951 29E5 E546 70F1
38  */
39
40 void sha1_init( SHA1_CONTEXT *hd )
41 {
42     hd->h0 = 0x67452301;
43     hd->h1 = 0xefcdab89;
44     hd->h2 = 0x98badcfe;
45     hd->h3 = 0x10325476;
46     hd->h4 = 0xc3d2e1f0;
47     hd->nblocks = 0;
48     hd->count = 0;
49 }
50
51
52 /****************
53  * Transform the message X which consists of 16 32-bit-words
54  */
55 static void transform( SHA1_CONTEXT *hd, uint8_t *data )
56 {
57     uint32_t a,b,c,d,e,tm;
58     uint32_t x[16];
59
60     /* get values from the chaining vars */
61     a = hd->h0;
62     b = hd->h1;
63     c = hd->h2;
64     d = hd->h3;
65     e = hd->h4;
66
67   #ifdef BIG_ENDIAN_HOST
68     memcpy( x, data, 64 );
69   #else
70     { int i;
71       uint8_t *p2;
72       for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 ) {
73         p2[3] = *data++;
74         p2[2] = *data++;
75         p2[1] = *data++;
76         p2[0] = *data++;
77       }
78     }
79   #endif
80
81
82 #define K1  0x5A827999L
83 #define K2  0x6ED9EBA1L
84 #define K3  0x8F1BBCDCL
85 #define K4  0xCA62C1D6L
86 #define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
87 #define F2(x,y,z)   ( x ^ y ^ z )
88 #define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
89 #define F4(x,y,z)   ( x ^ y ^ z )
90
91
92 #define M(i) ( tm =   x[i&0x0f] ^ x[(i-14)&0x0f] \
93                     ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
94                , (x[i&0x0f] = rol(tm,1)) )
95
96 #define R(a,b,c,d,e,f,k,m)  do { e += rol( a, 5 )     \
97                                       + f( b, c, d )  \
98                                       + k             \
99                                       + m;            \
100                                  b = rol( b, 30 );    \
101                                } while(0)
102     R( a, b, c, d, e, F1, K1, x[ 0] );
103     R( e, a, b, c, d, F1, K1, x[ 1] );
104     R( d, e, a, b, c, F1, K1, x[ 2] );
105     R( c, d, e, a, b, F1, K1, x[ 3] );
106     R( b, c, d, e, a, F1, K1, x[ 4] );
107     R( a, b, c, d, e, F1, K1, x[ 5] );
108     R( e, a, b, c, d, F1, K1, x[ 6] );
109     R( d, e, a, b, c, F1, K1, x[ 7] );
110     R( c, d, e, a, b, F1, K1, x[ 8] );
111     R( b, c, d, e, a, F1, K1, x[ 9] );
112     R( a, b, c, d, e, F1, K1, x[10] );
113     R( e, a, b, c, d, F1, K1, x[11] );
114     R( d, e, a, b, c, F1, K1, x[12] );
115     R( c, d, e, a, b, F1, K1, x[13] );
116     R( b, c, d, e, a, F1, K1, x[14] );
117     R( a, b, c, d, e, F1, K1, x[15] );
118     R( e, a, b, c, d, F1, K1, M(16) );
119     R( d, e, a, b, c, F1, K1, M(17) );
120     R( c, d, e, a, b, F1, K1, M(18) );
121     R( b, c, d, e, a, F1, K1, M(19) );
122     R( a, b, c, d, e, F2, K2, M(20) );
123     R( e, a, b, c, d, F2, K2, M(21) );
124     R( d, e, a, b, c, F2, K2, M(22) );
125     R( c, d, e, a, b, F2, K2, M(23) );
126     R( b, c, d, e, a, F2, K2, M(24) );
127     R( a, b, c, d, e, F2, K2, M(25) );
128     R( e, a, b, c, d, F2, K2, M(26) );
129     R( d, e, a, b, c, F2, K2, M(27) );
130     R( c, d, e, a, b, F2, K2, M(28) );
131     R( b, c, d, e, a, F2, K2, M(29) );
132     R( a, b, c, d, e, F2, K2, M(30) );
133     R( e, a, b, c, d, F2, K2, M(31) );
134     R( d, e, a, b, c, F2, K2, M(32) );
135     R( c, d, e, a, b, F2, K2, M(33) );
136     R( b, c, d, e, a, F2, K2, M(34) );
137     R( a, b, c, d, e, F2, K2, M(35) );
138     R( e, a, b, c, d, F2, K2, M(36) );
139     R( d, e, a, b, c, F2, K2, M(37) );
140     R( c, d, e, a, b, F2, K2, M(38) );
141     R( b, c, d, e, a, F2, K2, M(39) );
142     R( a, b, c, d, e, F3, K3, M(40) );
143     R( e, a, b, c, d, F3, K3, M(41) );
144     R( d, e, a, b, c, F3, K3, M(42) );
145     R( c, d, e, a, b, F3, K3, M(43) );
146     R( b, c, d, e, a, F3, K3, M(44) );
147     R( a, b, c, d, e, F3, K3, M(45) );
148     R( e, a, b, c, d, F3, K3, M(46) );
149     R( d, e, a, b, c, F3, K3, M(47) );
150     R( c, d, e, a, b, F3, K3, M(48) );
151     R( b, c, d, e, a, F3, K3, M(49) );
152     R( a, b, c, d, e, F3, K3, M(50) );
153     R( e, a, b, c, d, F3, K3, M(51) );
154     R( d, e, a, b, c, F3, K3, M(52) );
155     R( c, d, e, a, b, F3, K3, M(53) );
156     R( b, c, d, e, a, F3, K3, M(54) );
157     R( a, b, c, d, e, F3, K3, M(55) );
158     R( e, a, b, c, d, F3, K3, M(56) );
159     R( d, e, a, b, c, F3, K3, M(57) );
160     R( c, d, e, a, b, F3, K3, M(58) );
161     R( b, c, d, e, a, F3, K3, M(59) );
162     R( a, b, c, d, e, F4, K4, M(60) );
163     R( e, a, b, c, d, F4, K4, M(61) );
164     R( d, e, a, b, c, F4, K4, M(62) );
165     R( c, d, e, a, b, F4, K4, M(63) );
166     R( b, c, d, e, a, F4, K4, M(64) );
167     R( a, b, c, d, e, F4, K4, M(65) );
168     R( e, a, b, c, d, F4, K4, M(66) );
169     R( d, e, a, b, c, F4, K4, M(67) );
170     R( c, d, e, a, b, F4, K4, M(68) );
171     R( b, c, d, e, a, F4, K4, M(69) );
172     R( a, b, c, d, e, F4, K4, M(70) );
173     R( e, a, b, c, d, F4, K4, M(71) );
174     R( d, e, a, b, c, F4, K4, M(72) );
175     R( c, d, e, a, b, F4, K4, M(73) );
176     R( b, c, d, e, a, F4, K4, M(74) );
177     R( a, b, c, d, e, F4, K4, M(75) );
178     R( e, a, b, c, d, F4, K4, M(76) );
179     R( d, e, a, b, c, F4, K4, M(77) );
180     R( c, d, e, a, b, F4, K4, M(78) );
181     R( b, c, d, e, a, F4, K4, M(79) );
182
183     /* update chainig vars */
184     hd->h0 += a;
185     hd->h1 += b;
186     hd->h2 += c;
187     hd->h3 += d;
188     hd->h4 += e;
189 }
190
191
192 /* Update the message digest with the contents
193  * of INBUF with length INLEN.
194  */
195 void sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
196 {
197
198     if( hd->count == 64 ) { /* flush the buffer */
199         transform( hd, hd->buf );
200         hd->count = 0;
201         hd->nblocks++;
202     }
203     if( !inbuf )
204         return;
205     if( hd->count ) {
206         for( ; inlen && hd->count < 64; inlen-- )
207             hd->buf[hd->count++] = *inbuf++;
208         sha1_write( hd, NULL, 0 );
209         if( !inlen )
210             return;
211     }
212
213     while( inlen >= 64 ) {
214         transform( hd, inbuf );
215         hd->count = 0;
216         hd->nblocks++;
217         inlen -= 64;
218         inbuf += 64;
219     }
220     for( ; inlen && hd->count < 64; inlen-- )
221         hd->buf[hd->count++] = *inbuf++;
222 }
223
224
225 /* The routine final terminates the computation and
226  * returns the digest.
227  * The handle is prepared for a new cycle, but adding uint8_ts to the
228  * handle will the destroy the returned buffer.
229  * Returns: 20 uint8_ts representing the digest.
230  */
231
232 void sha1_final(SHA1_CONTEXT *hd)
233 {
234     unsigned int t, msb, lsb;
235     unsigned char *p;
236
237     sha1_write(hd, NULL, 0); /* flush */;
238
239     msb = 0;
240     t = hd->nblocks;
241     if( (lsb = t << 6) < t ) /* multiply by 64 to make a uint8_t count */
242         msb++;
243     msb += t >> 26;
244     t = lsb;
245     if( (lsb = t + hd->count) < t ) /* add the count */
246         msb++;
247     t = lsb;
248     if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
249         msb++;
250     msb += t >> 29;
251
252     if( hd->count < 56 ) { /* enough room */
253         hd->buf[hd->count++] = 0x80; /* pad */
254         while( hd->count < 56 )
255             hd->buf[hd->count++] = 0;  /* pad */
256     }
257     else { /* need one extra block */
258         hd->buf[hd->count++] = 0x80; /* pad character */
259         while( hd->count < 64 )
260             hd->buf[hd->count++] = 0;
261         sha1_write(hd, NULL, 0);  /* flush */;
262         memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
263     }
264     /* append the 64 bit count */
265     hd->buf[56] = msb >> 24;
266     hd->buf[57] = msb >> 16;
267     hd->buf[58] = msb >>  8;
268     hd->buf[59] = msb      ;
269     hd->buf[60] = lsb >> 24;
270     hd->buf[61] = lsb >> 16;
271     hd->buf[62] = lsb >>  8;
272     hd->buf[63] = lsb      ;
273     transform( hd, hd->buf );
274
275     p = hd->buf;
276   #ifdef BIG_ENDIAN_HOST
277     #define X(a) do { *(uint32_t *)p = hd->h##a ; p += 4; } while(0)
278   #else /* little endian */
279     #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16;      \
280                       *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
281   #endif
282     X(0);
283     X(1);
284     X(2);
285     X(3);
286     X(4);
287   #undef X
288
289 }
290
291 uint8_t *sha1_read( SHA1_CONTEXT *hd )
292 {
293     return hd->buf;
294 }