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