1 /* sha1.c - SHA1 hash function
2 * Copyright (C) 2001 V. Alex Brennen
4 * Please see below for more legal information!
6 * This file is part of the CryptNET openPGP Public Keyserver (CKS).
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.
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.
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
22 * $Id: sha.c,v 1.2 2003/06/04 20:57:12 noodles Exp $
25 /* This file was copied from GnuPG */
26 /* Some portions Copyright (C) 1998 The Free Software Foundation */
34 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
36 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
37 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
40 void sha1_init( SHA1_CONTEXT *hd )
53 * Transform the message X which consists of 16 32-bit-words
55 static void transform( SHA1_CONTEXT *hd, uint8_t *data )
57 uint32_t a,b,c,d,e,tm;
60 /* get values from the chaining vars */
67 #ifdef BIG_ENDIAN_HOST
68 memcpy( x, data, 64 );
72 for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 ) {
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 )
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)) )
96 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
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) );
183 /* update chainig vars */
192 /* Update the message digest with the contents
193 * of INBUF with length INLEN.
195 void sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
198 if( hd->count == 64 ) { /* flush the buffer */
199 transform( hd, hd->buf );
206 for( ; inlen && hd->count < 64; inlen-- )
207 hd->buf[hd->count++] = *inbuf++;
208 sha1_write( hd, NULL, 0 );
213 while( inlen >= 64 ) {
214 transform( hd, inbuf );
220 for( ; inlen && hd->count < 64; inlen-- )
221 hd->buf[hd->count++] = *inbuf++;
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.
232 void sha1_final(SHA1_CONTEXT *hd)
234 unsigned int t, msb, lsb;
237 sha1_write(hd, NULL, 0); /* flush */;
241 if( (lsb = t << 6) < t ) /* multiply by 64 to make a uint8_t count */
245 if( (lsb = t + hd->count) < t ) /* add the count */
248 if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
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 */
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 */
264 /* append the 64 bit count */
265 hd->buf[56] = msb >> 24;
266 hd->buf[57] = msb >> 16;
267 hd->buf[58] = msb >> 8;
269 hd->buf[60] = lsb >> 24;
270 hd->buf[61] = lsb >> 16;
271 hd->buf[62] = lsb >> 8;
273 transform( hd, 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)
291 uint8_t *sha1_read( SHA1_CONTEXT *hd )