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