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

Annotation of /trunk/Gnupg/md5.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: 9216 byte(s)
Second set of changes to use autotools for building.
1 werner 46 /* 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     #ifdef HAVE_CONFIG_H
33     #include <config.h>
34     #endif
35    
36     #include <stdio.h>
37     #include <stdio.h>
38     #include <stdlib.h>
39     #include <string.h>
40     #include <assert.h>
41    
42     #include "md.h"
43    
44    
45     void
46     md5_init( MD5_CONTEXT *ctx )
47     {
48     ctx->A = 0x67452301;
49     ctx->B = 0xefcdab89;
50     ctx->C = 0x98badcfe;
51     ctx->D = 0x10325476;
52    
53     ctx->nblocks = 0;
54     ctx->count = 0;
55     }
56    
57    
58    
59    
60     /* These are the four functions used in the four steps of the MD5 algorithm
61     and defined in the RFC 1321. The first function is a little bit optimized
62     (as found in Colin Plumbs public domain implementation). */
63     /* #define FF(b, c, d) ((b & c) | (~b & d)) */
64     #define FF(b, c, d) (d ^ (b & (c ^ d)))
65     #define FG(b, c, d) FF (d, b, c)
66     #define FH(b, c, d) (b ^ c ^ d)
67     #define FI(b, c, d) (c ^ (b | ~d))
68    
69     static void
70     burn_stack (int bytes)
71     {
72     char buf[128];
73    
74     memset(buf, 0, sizeof buf);
75     bytes -= sizeof buf;
76     if (bytes > 0)
77     burn_stack (bytes);
78     }
79    
80    
81    
82     /****************
83     * transform n*64 bytes
84     */
85     static void
86     /*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/
87     transform( MD5_CONTEXT *ctx, unsigned char *data )
88     {
89     unsigned long correct_words[16];
90     unsigned long A = ctx->A;
91     unsigned long B = ctx->B;
92     unsigned long C = ctx->C;
93     unsigned long D = ctx->D;
94     unsigned long *cwp = correct_words;
95    
96     #ifdef BIG_ENDIAN_HOST
97     { int i;
98     unsigned char *p2, *p1;
99     for(i=0, p1=data, p2=(unsigned char*)correct_words; i < 16; i++, p2 += 4 ) {
100     p2[3] = *p1++;
101     p2[2] = *p1++;
102     p2[1] = *p1++;
103     p2[0] = *p1++;
104     }
105     }
106     #else
107     memcpy( correct_words, data, 64 );
108     #endif
109    
110    
111     #define OP(a, b, c, d, s, T) \
112     do \
113     { \
114     a += FF (b, c, d) + (*cwp++) + T; \
115     a = rol(a, s); \
116     a += b; \
117     } \
118     while (0)
119    
120     /* Before we start, one word about the strange constants.
121     They are defined in RFC 1321 as
122    
123     T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
124     */
125    
126     /* Round 1. */
127     OP (A, B, C, D, 7, 0xd76aa478);
128     OP (D, A, B, C, 12, 0xe8c7b756);
129     OP (C, D, A, B, 17, 0x242070db);
130     OP (B, C, D, A, 22, 0xc1bdceee);
131     OP (A, B, C, D, 7, 0xf57c0faf);
132     OP (D, A, B, C, 12, 0x4787c62a);
133     OP (C, D, A, B, 17, 0xa8304613);
134     OP (B, C, D, A, 22, 0xfd469501);
135     OP (A, B, C, D, 7, 0x698098d8);
136     OP (D, A, B, C, 12, 0x8b44f7af);
137     OP (C, D, A, B, 17, 0xffff5bb1);
138     OP (B, C, D, A, 22, 0x895cd7be);
139     OP (A, B, C, D, 7, 0x6b901122);
140     OP (D, A, B, C, 12, 0xfd987193);
141     OP (C, D, A, B, 17, 0xa679438e);
142     OP (B, C, D, A, 22, 0x49b40821);
143    
144     #undef OP
145     #define OP(f, a, b, c, d, k, s, T) \
146     do \
147     { \
148     a += f (b, c, d) + correct_words[k] + T; \
149     a = rol(a, s); \
150     a += b; \
151     } \
152     while (0)
153    
154     /* Round 2. */
155     OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
156     OP (FG, D, A, B, C, 6, 9, 0xc040b340);
157     OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
158     OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
159     OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
160     OP (FG, D, A, B, C, 10, 9, 0x02441453);
161     OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
162     OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
163     OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
164     OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
165     OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
166     OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
167     OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
168     OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
169     OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
170     OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
171    
172     /* Round 3. */
173     OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
174     OP (FH, D, A, B, C, 8, 11, 0x8771f681);
175     OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
176     OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
177     OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
178     OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
179     OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
180     OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
181     OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
182     OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
183     OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
184     OP (FH, B, C, D, A, 6, 23, 0x04881d05);
185     OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
186     OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
187     OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
188     OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
189    
190     /* Round 4. */
191     OP (FI, A, B, C, D, 0, 6, 0xf4292244);
192     OP (FI, D, A, B, C, 7, 10, 0x432aff97);
193     OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
194     OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
195     OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
196     OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
197     OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
198     OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
199     OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
200     OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
201     OP (FI, C, D, A, B, 6, 15, 0xa3014314);
202     OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
203     OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
204     OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
205     OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
206     OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
207    
208     /* Put checksum in context given as argument. */
209     ctx->A += A;
210     ctx->B += B;
211     ctx->C += C;
212     ctx->D += D;
213     }
214    
215    
216    
217     /* The routine updates the message-digest context to
218     * account for the presence of each of the characters inBuf[0..inLen-1]
219     * in the message whose digest is being computed.
220     */
221     void
222     md5_write( MD5_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
223     {
224     if( hd->count == 64 ) { /* flush the buffer */
225     transform( hd, hd->buf );
226     burn_stack (80+6*sizeof(void*));
227     hd->count = 0;
228     hd->nblocks++;
229     }
230     if( !inbuf )
231     return;
232     if( hd->count ) {
233     for( ; inlen && hd->count < 64; inlen-- )
234     hd->buf[hd->count++] = *inbuf++;
235     md5_write( hd, NULL, 0 );
236     if( !inlen )
237     return;
238     }
239    
240     while( inlen >= 64 ) {
241     transform( hd, inbuf );
242     hd->count = 0;
243     hd->nblocks++;
244     inlen -= 64;
245     inbuf += 64;
246     }
247     burn_stack (80+6*sizeof(void*));
248     for( ; inlen && hd->count < 64; inlen-- )
249     hd->buf[hd->count++] = *inbuf++;
250     }
251    
252    
253    
254     /* The routine final terminates the message-digest computation and
255     * ends with the desired message digest in mdContext->digest[0...15].
256     * The handle is prepared for a new MD5 cycle.
257     * Returns 16 bytes representing the digest.
258     */
259    
260     void
261     md5_final( MD5_CONTEXT *hd )
262     {
263     unsigned long t, msb, lsb;
264     unsigned char *p;
265    
266     md5_write(hd, NULL, 0); /* flush */;
267    
268     t = hd->nblocks;
269     /* multiply by 64 to make a unsigned char count */
270     lsb = t << 6;
271     msb = t >> 26;
272     /* add the count */
273     t = lsb;
274     if( (lsb += hd->count) < t )
275     msb++;
276     /* multiply by 8 to make a bit count */
277     t = lsb;
278     lsb <<= 3;
279     msb <<= 3;
280     msb |= t >> 29;
281    
282     if( hd->count < 56 ) { /* enough room */
283     hd->buf[hd->count++] = 0x80; /* pad */
284     while( hd->count < 56 )
285     hd->buf[hd->count++] = 0; /* pad */
286     }
287     else { /* need one extra block */
288     hd->buf[hd->count++] = 0x80; /* pad character */
289     while( hd->count < 64 )
290     hd->buf[hd->count++] = 0;
291     md5_write(hd, NULL, 0); /* flush */;
292     memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
293     }
294     /* append the 64 bit count */
295     hd->buf[56] = lsb ;
296     hd->buf[57] = lsb >> 8;
297     hd->buf[58] = lsb >> 16;
298     hd->buf[59] = lsb >> 24;
299     hd->buf[60] = msb ;
300     hd->buf[61] = msb >> 8;
301     hd->buf[62] = msb >> 16;
302     hd->buf[63] = msb >> 24;
303     transform( hd, hd->buf );
304     burn_stack (80+6*sizeof(void*));
305    
306     p = hd->buf;
307     #ifdef BIG_ENDIAN_HOST
308     #define X(a) do { *p++ = hd-> a ; *p++ = hd-> a >> 8; \
309     *p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0)
310     #else /* little endian */
311     #define X(a) do { *(unsigned long*)p = hd-> a ; p += 4; } while(0)
312     #endif
313     X(A);
314     X(B);
315     X(C);
316     X(D);
317     #undef X
318    
319     }
320    
321     unsigned char *
322     md5_read( MD5_CONTEXT *hd )
323     {
324     return hd->buf;
325     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26