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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 /* This is a simplified SHA01 versions taken from the libgrypt one.
24 We need this for some tests (e.g. OCSP).
31 #include <sys/types.h>
34 #undef u32 /* maybe there is a macro with this name */
35 #if SIZEOF_UNSIGNED_INT == 4
36 typedef unsigned int u32;
37 #elif SIZEOF_UNSIGNED_LONG == 4
38 typedef unsigned long u32;
40 #error no typedef for u32
49 unsigned char buf[64];
54 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
58 sha1_init (void *context)
60 sha1_context_t *hd = context;
73 * Transform the message X which consists of 16 32-bit-words
76 transform( sha1_context_t *hd, unsigned char *data )
78 register u32 a,b,c,d,e,tm;
81 /* Get values from the chaining vars. */
88 #ifdef WORDS_BIGENDIAN
89 memcpy( x, data, 64 );
94 for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 )
105 #define K1 0x5A827999L
106 #define K2 0x6ED9EBA1L
107 #define K3 0x8F1BBCDCL
108 #define K4 0xCA62C1D6L
109 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
110 #define F2(x,y,z) ( x ^ y ^ z )
111 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
112 #define F4(x,y,z) ( x ^ y ^ z )
115 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
116 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
117 , (x[i&0x0f] = rol(tm, 1)) )
119 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
125 R( a, b, c, d, e, F1, K1, x[ 0] );
126 R( e, a, b, c, d, F1, K1, x[ 1] );
127 R( d, e, a, b, c, F1, K1, x[ 2] );
128 R( c, d, e, a, b, F1, K1, x[ 3] );
129 R( b, c, d, e, a, F1, K1, x[ 4] );
130 R( a, b, c, d, e, F1, K1, x[ 5] );
131 R( e, a, b, c, d, F1, K1, x[ 6] );
132 R( d, e, a, b, c, F1, K1, x[ 7] );
133 R( c, d, e, a, b, F1, K1, x[ 8] );
134 R( b, c, d, e, a, F1, K1, x[ 9] );
135 R( a, b, c, d, e, F1, K1, x[10] );
136 R( e, a, b, c, d, F1, K1, x[11] );
137 R( d, e, a, b, c, F1, K1, x[12] );
138 R( c, d, e, a, b, F1, K1, x[13] );
139 R( b, c, d, e, a, F1, K1, x[14] );
140 R( a, b, c, d, e, F1, K1, x[15] );
141 R( e, a, b, c, d, F1, K1, M(16) );
142 R( d, e, a, b, c, F1, K1, M(17) );
143 R( c, d, e, a, b, F1, K1, M(18) );
144 R( b, c, d, e, a, F1, K1, M(19) );
145 R( a, b, c, d, e, F2, K2, M(20) );
146 R( e, a, b, c, d, F2, K2, M(21) );
147 R( d, e, a, b, c, F2, K2, M(22) );
148 R( c, d, e, a, b, F2, K2, M(23) );
149 R( b, c, d, e, a, F2, K2, M(24) );
150 R( a, b, c, d, e, F2, K2, M(25) );
151 R( e, a, b, c, d, F2, K2, M(26) );
152 R( d, e, a, b, c, F2, K2, M(27) );
153 R( c, d, e, a, b, F2, K2, M(28) );
154 R( b, c, d, e, a, F2, K2, M(29) );
155 R( a, b, c, d, e, F2, K2, M(30) );
156 R( e, a, b, c, d, F2, K2, M(31) );
157 R( d, e, a, b, c, F2, K2, M(32) );
158 R( c, d, e, a, b, F2, K2, M(33) );
159 R( b, c, d, e, a, F2, K2, M(34) );
160 R( a, b, c, d, e, F2, K2, M(35) );
161 R( e, a, b, c, d, F2, K2, M(36) );
162 R( d, e, a, b, c, F2, K2, M(37) );
163 R( c, d, e, a, b, F2, K2, M(38) );
164 R( b, c, d, e, a, F2, K2, M(39) );
165 R( a, b, c, d, e, F3, K3, M(40) );
166 R( e, a, b, c, d, F3, K3, M(41) );
167 R( d, e, a, b, c, F3, K3, M(42) );
168 R( c, d, e, a, b, F3, K3, M(43) );
169 R( b, c, d, e, a, F3, K3, M(44) );
170 R( a, b, c, d, e, F3, K3, M(45) );
171 R( e, a, b, c, d, F3, K3, M(46) );
172 R( d, e, a, b, c, F3, K3, M(47) );
173 R( c, d, e, a, b, F3, K3, M(48) );
174 R( b, c, d, e, a, F3, K3, M(49) );
175 R( a, b, c, d, e, F3, K3, M(50) );
176 R( e, a, b, c, d, F3, K3, M(51) );
177 R( d, e, a, b, c, F3, K3, M(52) );
178 R( c, d, e, a, b, F3, K3, M(53) );
179 R( b, c, d, e, a, F3, K3, M(54) );
180 R( a, b, c, d, e, F3, K3, M(55) );
181 R( e, a, b, c, d, F3, K3, M(56) );
182 R( d, e, a, b, c, F3, K3, M(57) );
183 R( c, d, e, a, b, F3, K3, M(58) );
184 R( b, c, d, e, a, F3, K3, M(59) );
185 R( a, b, c, d, e, F4, K4, M(60) );
186 R( e, a, b, c, d, F4, K4, M(61) );
187 R( d, e, a, b, c, F4, K4, M(62) );
188 R( c, d, e, a, b, F4, K4, M(63) );
189 R( b, c, d, e, a, F4, K4, M(64) );
190 R( a, b, c, d, e, F4, K4, M(65) );
191 R( e, a, b, c, d, F4, K4, M(66) );
192 R( d, e, a, b, c, F4, K4, M(67) );
193 R( c, d, e, a, b, F4, K4, M(68) );
194 R( b, c, d, e, a, F4, K4, M(69) );
195 R( a, b, c, d, e, F4, K4, M(70) );
196 R( e, a, b, c, d, F4, K4, M(71) );
197 R( d, e, a, b, c, F4, K4, M(72) );
198 R( c, d, e, a, b, F4, K4, M(73) );
199 R( b, c, d, e, a, F4, K4, M(74) );
200 R( a, b, c, d, e, F4, K4, M(75) );
201 R( e, a, b, c, d, F4, K4, M(76) );
202 R( d, e, a, b, c, F4, K4, M(77) );
203 R( c, d, e, a, b, F4, K4, M(78) );
204 R( b, c, d, e, a, F4, K4, M(79) );
206 /* Update chaining vars. */
215 /* Update the message digest with the contents
216 * of INBUF with length INLEN.
219 sha1_write( void *context, unsigned char *inbuf, size_t inlen)
221 sha1_context_t *hd = context;
223 if( hd->count == 64 ) /* flush the buffer */
225 transform( hd, hd->buf );
234 for( ; inlen && hd->count < 64; inlen-- )
235 hd->buf[hd->count++] = *inbuf++;
236 sha1_write( hd, NULL, 0 );
243 transform( hd, inbuf );
249 for( ; inlen && hd->count < 64; inlen-- )
250 hd->buf[hd->count++] = *inbuf++;
254 /* The routine final terminates the computation and
255 * returns the digest.
256 * The handle is prepared for a new cycle, but adding bytes to the
257 * handle will the destroy the returned buffer.
258 * Returns: 20 bytes representing the digest.
262 sha1_final(void *context)
264 sha1_context_t *hd = context;
269 sha1_write(hd, NULL, 0); /* flush */;
272 /* multiply by 64 to make a byte count */
277 if( (lsb += hd->count) < t )
279 /* multiply by 8 to make a bit count */
285 if( hd->count < 56 ) /* enough room */
287 hd->buf[hd->count++] = 0x80; /* pad */
288 while( hd->count < 56 )
289 hd->buf[hd->count++] = 0; /* pad */
291 else /* need one extra block */
293 hd->buf[hd->count++] = 0x80; /* pad character */
294 while( hd->count < 64 )
295 hd->buf[hd->count++] = 0;
296 sha1_write(hd, NULL, 0); /* flush */;
297 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
299 /* append the 64 bit count */
300 hd->buf[56] = msb >> 24;
301 hd->buf[57] = msb >> 16;
302 hd->buf[58] = msb >> 8;
304 hd->buf[60] = lsb >> 24;
305 hd->buf[61] = lsb >> 16;
306 hd->buf[62] = lsb >> 8;
308 transform( hd, hd->buf );
311 #ifdef WORDS_BIGENDIAN
312 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
313 #else /* little endian */
314 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
315 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
327 sha1_hash_buffer (char *outbuf, const char *buffer, size_t length)
332 sha1_write (&hd, (unsigned char *)buffer, length);
334 memcpy (outbuf, hd.buf, 20);