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

Annotation of /trunk/Gnupg/md5.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Mon Jan 31 11:02:21 2005 UTC (20 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 9468 byte(s)
WinPT initial checkin.


1 twoaday 2 /* md5.c - MD5 Message-Digest Algorithm
2     * Copyright (C) 1995, 1996, 1998, 1999,
3     * 2000, 2001 Free Software Foundation, Inc.
4     *
5     * This program is free software; you can redistribute it and/or modify it
6     * under the terms of the GNU General Public License as published by the
7     * Free Software Foundation; either version 2, or (at your option) any
8     * later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software Foundation,
17     * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     *
19     * According to the definition of MD5 in RFC 1321 from April 1992.
20     * NOTE: This is *not* the same file as the one from glibc.
21     */
22     /* Written by Ulrich Drepper <[email protected]>, 1995. */
23     /* Heavily modified for GnuPG by <[email protected]> */
24    
25     /* Test values:
26     * "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E
27     * "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61
28     * "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72
29     * "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0
30     */
31    
32     #include <stdio.h>
33     #include <stdlib.h>
34     #include <string.h>
35     #include <assert.h>
36    
37     #include "md.h"
38    
39    
40     void
41     md5_init( MD5_CONTEXT *ctx )
42     {
43     ctx->A = 0x67452301;
44     ctx->B = 0xefcdab89;
45     ctx->C = 0x98badcfe;
46     ctx->D = 0x10325476;
47    
48     ctx->nblocks = 0;
49     ctx->count = 0;
50     }
51    
52    
53    
54    
55     /* These are the four functions used in the four steps of the MD5 algorithm
56     and defined in the RFC 1321. The first function is a little bit optimized
57     (as found in Colin Plumbs public domain implementation). */
58     /* #define FF(b, c, d) ((b & c) | (~b & d)) */
59     #define FF(b, c, d) (d ^ (b & (c ^ d)))
60     #define FG(b, c, d) FF (d, b, c)
61     #define FH(b, c, d) (b ^ c ^ d)
62     #define FI(b, c, d) (c ^ (b | ~d))
63    
64     static void
65     burn_stack (int bytes)
66     {
67     char buf[128];
68    
69     memset(buf, 0, sizeof buf);
70     bytes -= sizeof buf;
71     if (bytes > 0)
72     burn_stack (bytes);
73     }
74    
75    
76    
77     /****************
78     * transform n*64 bytes
79     */
80     static void
81     /*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/
82     transform( MD5_CONTEXT *ctx, unsigned char *data )
83     {
84     unsigned long correct_words[16];
85     unsigned long A = ctx->A;
86     unsigned long B = ctx->B;
87     unsigned long C = ctx->C;
88     unsigned long D = ctx->D;
89     unsigned long *cwp = correct_words;
90    
91     #ifdef BIG_ENDIAN_HOST
92     { int i;
93     unsigned char *p2, *p1;
94     for(i=0, p1=data, p2=(unsigned char*)correct_words; i < 16; i++, p2 += 4 ) {
95     p2[3] = *p1++;
96     p2[2] = *p1++;
97     p2[1] = *p1++;
98     p2[0] = *p1++;
99     }
100     }
101     #else
102     memcpy( correct_words, data, 64 );
103     #endif
104    
105    
106     #define OP(a, b, c, d, s, T) \
107     do \
108     { \
109     a += FF (b, c, d) + (*cwp++) + T; \
110     a = rol(a, s); \
111     a += b; \
112     } \
113     while (0)
114    
115     /* Before we start, one word about the strange constants.
116     They are defined in RFC 1321 as
117    
118     T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
119     */
120    
121     /* Round 1. */
122     OP (A, B, C, D, 7, 0xd76aa478);
123     OP (D, A, B, C, 12, 0xe8c7b756);
124     OP (C, D, A, B, 17, 0x242070db);
125     OP (B, C, D, A, 22, 0xc1bdceee);
126     OP (A, B, C, D, 7, 0xf57c0faf);
127     OP (D, A, B, C, 12, 0x4787c62a);
128     OP (C, D, A, B, 17, 0xa8304613);
129     OP (B, C, D, A, 22, 0xfd469501);
130     OP (A, B, C, D, 7, 0x698098d8);
131     OP (D, A, B, C, 12, 0x8b44f7af);
132     OP (C, D, A, B, 17, 0xffff5bb1);
133     OP (B, C, D, A, 22, 0x895cd7be);
134     OP (A, B, C, D, 7, 0x6b901122);
135     OP (D, A, B, C, 12, 0xfd987193);
136     OP (C, D, A, B, 17, 0xa679438e);
137     OP (B, C, D, A, 22, 0x49b40821);
138    
139     #undef OP
140     #define OP(f, a, b, c, d, k, s, T) \
141     do \
142     { \
143     a += f (b, c, d) + correct_words[k] + T; \
144     a = rol(a, s); \
145     a += b; \
146     } \
147     while (0)
148    
149     /* Round 2. */
150     OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
151     OP (FG, D, A, B, C, 6, 9, 0xc040b340);
152     OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
153     OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
154     OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
155     OP (FG, D, A, B, C, 10, 9, 0x02441453);
156     OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
157     OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
158     OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
159     OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
160     OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
161     OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
162     OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
163     OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
164     OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
165     OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
166    
167     /* Round 3. */
168     OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
169     OP (FH, D, A, B, C, 8, 11, 0x8771f681);
170     OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
171     OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
172     OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
173     OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
174     OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
175     OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
176     OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
177     OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
178     OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
179     OP (FH, B, C, D, A, 6, 23, 0x04881d05);
180     OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
181     OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
182     OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
183     OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
184    
185     /* Round 4. */
186     OP (FI, A, B, C, D, 0, 6, 0xf4292244);
187     OP (FI, D, A, B, C, 7, 10, 0x432aff97);
188     OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
189     OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
190     OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
191     OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
192     OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
193     OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
194     OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
195     OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
196     OP (FI, C, D, A, B, 6, 15, 0xa3014314);
197     OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
198     OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
199     OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
200     OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
201     OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
202    
203     /* Put checksum in context given as argument. */
204     ctx->A += A;
205     ctx->B += B;
206     ctx->C += C;
207     ctx->D += D;
208     }
209    
210    
211    
212     /* The routine updates the message-digest context to
213     * account for the presence of each of the characters inBuf[0..inLen-1]
214     * in the message whose digest is being computed.
215     */
216     void
217     md5_write( MD5_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
218     {
219     if( hd->count == 64 ) { /* flush the buffer */
220     transform( hd, hd->buf );
221     burn_stack (80+6*sizeof(void*));
222     hd->count = 0;
223     hd->nblocks++;
224     }
225     if( !inbuf )
226     return;
227     if( hd->count ) {
228     for( ; inlen && hd->count < 64; inlen-- )
229     hd->buf[hd->count++] = *inbuf++;
230     md5_write( hd, NULL, 0 );
231     if( !inlen )
232     return;
233     }
234    
235     while( inlen >= 64 ) {
236     transform( hd, inbuf );
237     hd->count = 0;
238     hd->nblocks++;
239     inlen -= 64;
240     inbuf += 64;
241     }
242     burn_stack (80+6*sizeof(void*));
243     for( ; inlen && hd->count < 64; inlen-- )
244     hd->buf[hd->count++] = *inbuf++;
245     }
246    
247    
248    
249     /* The routine final terminates the message-digest computation and
250     * ends with the desired message digest in mdContext->digest[0...15].
251     * The handle is prepared for a new MD5 cycle.
252     * Returns 16 bytes representing the digest.
253     */
254    
255     void
256     md5_final( MD5_CONTEXT *hd )
257     {
258     unsigned long t, msb, lsb;
259     unsigned char *p;
260    
261     md5_write(hd, NULL, 0); /* flush */;
262    
263     t = hd->nblocks;
264     /* multiply by 64 to make a unsigned char count */
265     lsb = t << 6;
266     msb = t >> 26;
267     /* add the count */
268     t = lsb;
269     if( (lsb += hd->count) < t )
270     msb++;
271     /* multiply by 8 to make a bit count */
272     t = lsb;
273     lsb <<= 3;
274     msb <<= 3;
275     msb |= t >> 29;
276    
277     if( hd->count < 56 ) { /* enough room */
278     hd->buf[hd->count++] = 0x80; /* pad */
279     while( hd->count < 56 )
280     hd->buf[hd->count++] = 0; /* pad */
281     }
282     else { /* need one extra block */
283     hd->buf[hd->count++] = 0x80; /* pad character */
284     while( hd->count < 64 )
285     hd->buf[hd->count++] = 0;
286     md5_write(hd, NULL, 0); /* flush */;
287     memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
288     }
289     /* append the 64 bit count */
290     hd->buf[56] = lsb ;
291     hd->buf[57] = lsb >> 8;
292     hd->buf[58] = lsb >> 16;
293     hd->buf[59] = lsb >> 24;
294     hd->buf[60] = msb ;
295     hd->buf[61] = msb >> 8;
296     hd->buf[62] = msb >> 16;
297     hd->buf[63] = msb >> 24;
298     transform( hd, hd->buf );
299     burn_stack (80+6*sizeof(void*));
300    
301     p = hd->buf;
302     #ifdef BIG_ENDIAN_HOST
303     #define X(a) do { *p++ = hd-> a ; *p++ = hd-> a >> 8; \
304     *p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0)
305     #else /* little endian */
306     #define X(a) do { *(unsigned long*)p = hd-> a ; p += 4; } while(0)
307     #endif
308     X(A);
309     X(B);
310     X(C);
311     X(D);
312     #undef X
313    
314     }
315    
316     unsigned char *
317     md5_read( MD5_CONTEXT *hd )
318     {
319     return hd->buf;
320     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26