1
/* md5.c - MD5 Message-Digest Algorithm
2
* Copyright (C) 1995, 1996, 1998, 1999,
3
* 2000, 2001 Free Software Foundation, Inc.
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
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.
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.
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.
22
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
23
/* Heavily modified for GnuPG by <wk@gnupg.org> */
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
39
#include "algorithms.h"
45
u32 A,B,C,D; /* chaining variables */
53
md5_init( MD5_CONTEXT *ctx )
67
/* These are the four functions used in the four steps of the MD5 algorithm
68
and defined in the RFC 1321. The first function is a little bit optimized
69
(as found in Colin Plumbs public domain implementation). */
70
/* #define FF(b, c, d) ((b & c) | (~b & d)) */
71
#define FF(b, c, d) (d ^ (b & (c ^ d)))
72
#define FG(b, c, d) FF (d, b, c)
73
#define FH(b, c, d) (b ^ c ^ d)
74
#define FI(b, c, d) (c ^ (b | ~d))
77
burn_stack (int bytes)
81
memset (buf, 0, sizeof buf);
90
* transform n*64 bytes
93
/*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/
94
transform( MD5_CONTEXT *ctx, byte *data )
96
u32 correct_words[16];
101
u32 *cwp = correct_words;
103
#ifdef BIG_ENDIAN_HOST
106
for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 ) {
114
memcpy( correct_words, data, 64 );
118
#define OP(a, b, c, d, s, T) \
121
a += FF (b, c, d) + (*cwp++) + T; \
127
/* Before we start, one word about the strange constants.
128
They are defined in RFC 1321 as
130
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
134
OP (A, B, C, D, 7, 0xd76aa478);
135
OP (D, A, B, C, 12, 0xe8c7b756);
136
OP (C, D, A, B, 17, 0x242070db);
137
OP (B, C, D, A, 22, 0xc1bdceee);
138
OP (A, B, C, D, 7, 0xf57c0faf);
139
OP (D, A, B, C, 12, 0x4787c62a);
140
OP (C, D, A, B, 17, 0xa8304613);
141
OP (B, C, D, A, 22, 0xfd469501);
142
OP (A, B, C, D, 7, 0x698098d8);
143
OP (D, A, B, C, 12, 0x8b44f7af);
144
OP (C, D, A, B, 17, 0xffff5bb1);
145
OP (B, C, D, A, 22, 0x895cd7be);
146
OP (A, B, C, D, 7, 0x6b901122);
147
OP (D, A, B, C, 12, 0xfd987193);
148
OP (C, D, A, B, 17, 0xa679438e);
149
OP (B, C, D, A, 22, 0x49b40821);
152
#define OP(f, a, b, c, d, k, s, T) \
155
a += f (b, c, d) + correct_words[k] + T; \
162
OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
163
OP (FG, D, A, B, C, 6, 9, 0xc040b340);
164
OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
165
OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
166
OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
167
OP (FG, D, A, B, C, 10, 9, 0x02441453);
168
OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
169
OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
170
OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
171
OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
172
OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
173
OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
174
OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
175
OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
176
OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
177
OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
180
OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
181
OP (FH, D, A, B, C, 8, 11, 0x8771f681);
182
OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
183
OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
184
OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
185
OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
186
OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
187
OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
188
OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
189
OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
190
OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
191
OP (FH, B, C, D, A, 6, 23, 0x04881d05);
192
OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
193
OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
194
OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
195
OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
198
OP (FI, A, B, C, D, 0, 6, 0xf4292244);
199
OP (FI, D, A, B, C, 7, 10, 0x432aff97);
200
OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
201
OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
202
OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
203
OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
204
OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
205
OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
206
OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
207
OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
208
OP (FI, C, D, A, B, 6, 15, 0xa3014314);
209
OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
210
OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
211
OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
212
OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
213
OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
215
/* Put checksum in context given as argument. */
224
/* The routine updates the message-digest context to
225
* account for the presence of each of the characters inBuf[0..inLen-1]
226
* in the message whose digest is being computed.
229
md5_write( MD5_CONTEXT *hd, byte *inbuf, size_t inlen)
231
if( hd->count == 64 ) { /* flush the buffer */
232
transform( hd, hd->buf );
233
burn_stack (80+6*sizeof(void*));
240
for( ; inlen && hd->count < 64; inlen-- )
241
hd->buf[hd->count++] = *inbuf++;
242
md5_write( hd, NULL, 0 );
247
while( inlen >= 64 ) {
248
transform( hd, inbuf );
254
burn_stack (80+6*sizeof(void*));
255
for( ; inlen && hd->count < 64; inlen-- )
256
hd->buf[hd->count++] = *inbuf++;
261
/* The routine final terminates the message-digest computation and
262
* ends with the desired message digest in mdContext->digest[0...15].
263
* The handle is prepared for a new MD5 cycle.
264
* Returns 16 bytes representing the digest.
268
md5_final( MD5_CONTEXT *hd )
273
md5_write(hd, NULL, 0); /* flush */;
276
/* multiply by 64 to make a byte count */
281
if( (lsb += hd->count) < t )
283
/* multiply by 8 to make a bit count */
289
if( hd->count < 56 ) { /* enough room */
290
hd->buf[hd->count++] = 0x80; /* pad */
291
while( hd->count < 56 )
292
hd->buf[hd->count++] = 0; /* pad */
294
else { /* need one extra block */
295
hd->buf[hd->count++] = 0x80; /* pad character */
296
while( hd->count < 64 )
297
hd->buf[hd->count++] = 0;
298
md5_write(hd, NULL, 0); /* flush */;
299
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
301
/* append the 64 bit count */
303
hd->buf[57] = lsb >> 8;
304
hd->buf[58] = lsb >> 16;
305
hd->buf[59] = lsb >> 24;
307
hd->buf[61] = msb >> 8;
308
hd->buf[62] = msb >> 16;
309
hd->buf[63] = msb >> 24;
310
transform( hd, hd->buf );
311
burn_stack (80+6*sizeof(void*));
314
#ifdef BIG_ENDIAN_HOST
315
#define X(a) do { *p++ = hd-> a ; *p++ = hd-> a >> 8; \
316
*p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0)
317
#else /* little endian */
318
#define X(a) do { *(u32*)p = hd-> a ; p += 4; } while(0)
329
md5_read( MD5_CONTEXT *hd )
335
* Return some information about the algorithm. We need algo here to
336
* distinguish different flavors of the algorithm.
337
* Returns: A pointer to string describing the algorithm or NULL if
338
* the ALGO is invalid.
341
md5_get_info( int algo, size_t *contextsize,
342
byte **r_asnoid, int *r_asnlen, int *r_mdlen,
343
void (**r_init)( void *c ),
344
void (**r_write)( void *c, byte *buf, size_t nbytes ),
345
void (**r_final)( void *c ),
346
byte *(**r_read)( void *c )
349
static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
350
{ 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
351
0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
356
*contextsize = sizeof(MD5_CONTEXT);
358
*r_asnlen = DIM(asn);
360
*(void (**)(MD5_CONTEXT *))r_init = md5_init;
361
*(void (**)(MD5_CONTEXT *, byte*, size_t))r_write = md5_write;
362
*(void (**)(MD5_CONTEXT *))r_final = md5_final;
363
*(byte *(**)(MD5_CONTEXT *))r_read = md5_read;