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
23 /* This file was copied from GnuPG */
24 /* Some portions Copyright (C) 1998 The Free Software Foundation */
32 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
34 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
35 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
38 void sha1_init( SHA1_CONTEXT *hd )
51 * Transform the message X which consists of 16 32-bit-words
53 static void transform( SHA1_CONTEXT *hd, uint8_t *data )
55 uint32_t a,b,c,d,e,tm;
58 /* get values from the chaining vars */
65 #ifdef BIG_ENDIAN_HOST
66 memcpy( x, data, 64 );
70 for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 ) {
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 )
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)) )
94 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
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) );
181 /* update chainig vars */
190 /* Update the message digest with the contents
191 * of INBUF with length INLEN.
193 void sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
196 if( hd->count == 64 ) { /* flush the buffer */
197 transform( hd, hd->buf );
204 for( ; inlen && hd->count < 64; inlen-- )
205 hd->buf[hd->count++] = *inbuf++;
206 sha1_write( hd, NULL, 0 );
211 while( inlen >= 64 ) {
212 transform( hd, inbuf );
218 for( ; inlen && hd->count < 64; inlen-- )
219 hd->buf[hd->count++] = *inbuf++;
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.
230 void sha1_final(SHA1_CONTEXT *hd)
232 unsigned int t, msb, lsb;
235 sha1_write(hd, NULL, 0); /* flush */;
239 if( (lsb = t << 6) < t ) /* multiply by 64 to make a uint8_t count */
243 if( (lsb = t + hd->count) < t ) /* add the count */
246 if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
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 */
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 */
262 /* append the 64 bit count */
263 hd->buf[56] = msb >> 24;
264 hd->buf[57] = msb >> 16;
265 hd->buf[58] = msb >> 8;
267 hd->buf[60] = lsb >> 24;
268 hd->buf[61] = lsb >> 16;
269 hd->buf[62] = lsb >> 8;
271 transform( hd, 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)
289 uint8_t *sha1_read( SHA1_CONTEXT *hd )