/[winpt]/trunk/Gnupg/sha1.c
ViewVC logotype

Annotation of /trunk/Gnupg/sha1.c

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26