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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
27 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
52 sha1_init( SHA1_CONTEXT
*hd
)
65 * Transform the message X which consists of 16 32-bit-words
68 transform( SHA1_CONTEXT
*hd
, byte
*data
)
70 register u32 a
,b
,c
,d
,e
,tm
;
73 /* get values from the chaining vars */
80 #ifdef BIG_ENDIAN_HOST
81 memcpy( x
, data
, 64 );
85 for(i
=0, p2
=(byte
*)x
; i
< 16; i
++, p2
+= 4 ) {
95 #define K1 0x5A827999L
96 #define K2 0x6ED9EBA1L
97 #define K3 0x8F1BBCDCL
98 #define K4 0xCA62C1D6L
99 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
100 #define F2(x,y,z) ( x ^ y ^ z )
101 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
102 #define F4(x,y,z) ( x ^ y ^ z )
105 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
106 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
107 , (x[i&0x0f] = rol(tm, 1)) )
109 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
115 R( a
, b
, c
, d
, e
, F1
, K1
, x
[ 0] );
116 R( e
, a
, b
, c
, d
, F1
, K1
, x
[ 1] );
117 R( d
, e
, a
, b
, c
, F1
, K1
, x
[ 2] );
118 R( c
, d
, e
, a
, b
, F1
, K1
, x
[ 3] );
119 R( b
, c
, d
, e
, a
, F1
, K1
, x
[ 4] );
120 R( a
, b
, c
, d
, e
, F1
, K1
, x
[ 5] );
121 R( e
, a
, b
, c
, d
, F1
, K1
, x
[ 6] );
122 R( d
, e
, a
, b
, c
, F1
, K1
, x
[ 7] );
123 R( c
, d
, e
, a
, b
, F1
, K1
, x
[ 8] );
124 R( b
, c
, d
, e
, a
, F1
, K1
, x
[ 9] );
125 R( a
, b
, c
, d
, e
, F1
, K1
, x
[10] );
126 R( e
, a
, b
, c
, d
, F1
, K1
, x
[11] );
127 R( d
, e
, a
, b
, c
, F1
, K1
, x
[12] );
128 R( c
, d
, e
, a
, b
, F1
, K1
, x
[13] );
129 R( b
, c
, d
, e
, a
, F1
, K1
, x
[14] );
130 R( a
, b
, c
, d
, e
, F1
, K1
, x
[15] );
131 R( e
, a
, b
, c
, d
, F1
, K1
, M(16) );
132 R( d
, e
, a
, b
, c
, F1
, K1
, M(17) );
133 R( c
, d
, e
, a
, b
, F1
, K1
, M(18) );
134 R( b
, c
, d
, e
, a
, F1
, K1
, M(19) );
135 R( a
, b
, c
, d
, e
, F2
, K2
, M(20) );
136 R( e
, a
, b
, c
, d
, F2
, K2
, M(21) );
137 R( d
, e
, a
, b
, c
, F2
, K2
, M(22) );
138 R( c
, d
, e
, a
, b
, F2
, K2
, M(23) );
139 R( b
, c
, d
, e
, a
, F2
, K2
, M(24) );
140 R( a
, b
, c
, d
, e
, F2
, K2
, M(25) );
141 R( e
, a
, b
, c
, d
, F2
, K2
, M(26) );
142 R( d
, e
, a
, b
, c
, F2
, K2
, M(27) );
143 R( c
, d
, e
, a
, b
, F2
, K2
, M(28) );
144 R( b
, c
, d
, e
, a
, F2
, K2
, M(29) );
145 R( a
, b
, c
, d
, e
, F2
, K2
, M(30) );
146 R( e
, a
, b
, c
, d
, F2
, K2
, M(31) );
147 R( d
, e
, a
, b
, c
, F2
, K2
, M(32) );
148 R( c
, d
, e
, a
, b
, F2
, K2
, M(33) );
149 R( b
, c
, d
, e
, a
, F2
, K2
, M(34) );
150 R( a
, b
, c
, d
, e
, F2
, K2
, M(35) );
151 R( e
, a
, b
, c
, d
, F2
, K2
, M(36) );
152 R( d
, e
, a
, b
, c
, F2
, K2
, M(37) );
153 R( c
, d
, e
, a
, b
, F2
, K2
, M(38) );
154 R( b
, c
, d
, e
, a
, F2
, K2
, M(39) );
155 R( a
, b
, c
, d
, e
, F3
, K3
, M(40) );
156 R( e
, a
, b
, c
, d
, F3
, K3
, M(41) );
157 R( d
, e
, a
, b
, c
, F3
, K3
, M(42) );
158 R( c
, d
, e
, a
, b
, F3
, K3
, M(43) );
159 R( b
, c
, d
, e
, a
, F3
, K3
, M(44) );
160 R( a
, b
, c
, d
, e
, F3
, K3
, M(45) );
161 R( e
, a
, b
, c
, d
, F3
, K3
, M(46) );
162 R( d
, e
, a
, b
, c
, F3
, K3
, M(47) );
163 R( c
, d
, e
, a
, b
, F3
, K3
, M(48) );
164 R( b
, c
, d
, e
, a
, F3
, K3
, M(49) );
165 R( a
, b
, c
, d
, e
, F3
, K3
, M(50) );
166 R( e
, a
, b
, c
, d
, F3
, K3
, M(51) );
167 R( d
, e
, a
, b
, c
, F3
, K3
, M(52) );
168 R( c
, d
, e
, a
, b
, F3
, K3
, M(53) );
169 R( b
, c
, d
, e
, a
, F3
, K3
, M(54) );
170 R( a
, b
, c
, d
, e
, F3
, K3
, M(55) );
171 R( e
, a
, b
, c
, d
, F3
, K3
, M(56) );
172 R( d
, e
, a
, b
, c
, F3
, K3
, M(57) );
173 R( c
, d
, e
, a
, b
, F3
, K3
, M(58) );
174 R( b
, c
, d
, e
, a
, F3
, K3
, M(59) );
175 R( a
, b
, c
, d
, e
, F4
, K4
, M(60) );
176 R( e
, a
, b
, c
, d
, F4
, K4
, M(61) );
177 R( d
, e
, a
, b
, c
, F4
, K4
, M(62) );
178 R( c
, d
, e
, a
, b
, F4
, K4
, M(63) );
179 R( b
, c
, d
, e
, a
, F4
, K4
, M(64) );
180 R( a
, b
, c
, d
, e
, F4
, K4
, M(65) );
181 R( e
, a
, b
, c
, d
, F4
, K4
, M(66) );
182 R( d
, e
, a
, b
, c
, F4
, K4
, M(67) );
183 R( c
, d
, e
, a
, b
, F4
, K4
, M(68) );
184 R( b
, c
, d
, e
, a
, F4
, K4
, M(69) );
185 R( a
, b
, c
, d
, e
, F4
, K4
, M(70) );
186 R( e
, a
, b
, c
, d
, F4
, K4
, M(71) );
187 R( d
, e
, a
, b
, c
, F4
, K4
, M(72) );
188 R( c
, d
, e
, a
, b
, F4
, K4
, M(73) );
189 R( b
, c
, d
, e
, a
, F4
, K4
, M(74) );
190 R( a
, b
, c
, d
, e
, F4
, K4
, M(75) );
191 R( e
, a
, b
, c
, d
, F4
, K4
, M(76) );
192 R( d
, e
, a
, b
, c
, F4
, K4
, M(77) );
193 R( c
, d
, e
, a
, b
, F4
, K4
, M(78) );
194 R( b
, c
, d
, e
, a
, F4
, K4
, M(79) );
196 /* update chainig vars */
205 /* Update the message digest with the contents
206 * of INBUF with length INLEN.
209 sha1_write( SHA1_CONTEXT
*hd
, byte
*inbuf
, size_t inlen
)
211 if( hd
->count
== 64 ) { /* flush the buffer */
212 transform( hd
, hd
->buf
);
213 _gcry_burn_stack (88+4*sizeof(void*));
220 for( ; inlen
&& hd
->count
< 64; inlen
-- )
221 hd
->buf
[hd
->count
++] = *inbuf
++;
222 sha1_write( hd
, NULL
, 0 );
227 while( inlen
>= 64 ) {
228 transform( hd
, inbuf
);
234 _gcry_burn_stack (88+4*sizeof(void*));
235 for( ; inlen
&& hd
->count
< 64; inlen
-- )
236 hd
->buf
[hd
->count
++] = *inbuf
++;
240 /* The routine final terminates the computation and
241 * returns the digest.
242 * The handle is prepared for a new cycle, but adding bytes to the
243 * handle will the destroy the returned buffer.
244 * Returns: 20 bytes representing the digest.
248 sha1_final(SHA1_CONTEXT
*hd
)
253 sha1_write(hd
, NULL
, 0); /* flush */;
256 /* multiply by 64 to make a byte count */
261 if( (lsb
+= hd
->count
) < t
)
263 /* multiply by 8 to make a bit count */
269 if( hd
->count
< 56 ) { /* enough room */
270 hd
->buf
[hd
->count
++] = 0x80; /* pad */
271 while( hd
->count
< 56 )
272 hd
->buf
[hd
->count
++] = 0; /* pad */
274 else { /* need one extra block */
275 hd
->buf
[hd
->count
++] = 0x80; /* pad character */
276 while( hd
->count
< 64 )
277 hd
->buf
[hd
->count
++] = 0;
278 sha1_write(hd
, NULL
, 0); /* flush */;
279 memset(hd
->buf
, 0, 56 ); /* fill next block with zeroes */
281 /* append the 64 bit count */
282 hd
->buf
[56] = msb
>> 24;
283 hd
->buf
[57] = msb
>> 16;
284 hd
->buf
[58] = msb
>> 8;
286 hd
->buf
[60] = lsb
>> 24;
287 hd
->buf
[61] = lsb
>> 16;
288 hd
->buf
[62] = lsb
>> 8;
290 transform( hd
, hd
->buf
);
291 _gcry_burn_stack (88+4*sizeof(void*));
294 #ifdef BIG_ENDIAN_HOST
295 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
296 #else /* little endian */
297 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
298 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
310 sha1_read( SHA1_CONTEXT
*hd
)
316 * Return some information about the algorithm. We need algo here to
317 * distinguish different flavors of the algorithm.
318 * Returns: A pointer to string describing the algorithm or NULL if
319 * the ALGO is invalid.
322 sha1_get_info( int algo
, size_t *contextsize
,
323 byte
**r_asnoid
, int *r_asnlen
, int *r_mdlen
,
324 void (**r_init
)( void *c
),
325 void (**r_write
)( void *c
, byte
*buf
, size_t nbytes
),
326 void (**r_final
)( void *c
),
327 byte
*(**r_read
)( void *c
)
330 static byte asn
[15] = /* Object ID is 1.3.14.3.2.26 */
331 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
332 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
336 *contextsize
= sizeof(SHA1_CONTEXT
);
338 *r_asnlen
= DIM(asn
);
340 *(void (**)(SHA1_CONTEXT
*))r_init
= sha1_init
;
341 *(void (**)(SHA1_CONTEXT
*, byte
*, size_t))r_write
= sha1_write
;
342 *(void (**)(SHA1_CONTEXT
*))r_final
= sha1_final
;
343 *(byte
*(**)(SHA1_CONTEXT
*))r_read
= sha1_read
;
353 const char * const gnupgext_version
= "SHA1 ($Revision$)";
361 { 10, 1, 0, (void(*)(void))sha1_get_info
},
370 gnupgext_enum_func( int what
, int *sequence
, int *class, int *vers
)
376 if( i
>= DIM(func_table
) || i
< 0 ) {
379 *class = func_table
[i
].class;
380 *vers
= func_table
[i
].version
;
385 ret
= &func_table
[i
].value
;
388 ret
= func_table
[i
].func
;
392 } while( what
&& what
!= *class );
403 _gcry_sha1_constructor(void)
405 _gcry_register_internal_cipher_extension( gnupgext_version
, gnupgext_enum_func
);