xref: /freebsd/contrib/wpa/src/crypto/md5-internal.c (revision e28a4053b110e06768631ac8401ed4a3c05e68a5)
1*e28a4053SRui Paulo /*
2*e28a4053SRui Paulo  * MD5 hash implementation and interface functions
3*e28a4053SRui Paulo  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4*e28a4053SRui Paulo  *
5*e28a4053SRui Paulo  * This program is free software; you can redistribute it and/or modify
6*e28a4053SRui Paulo  * it under the terms of the GNU General Public License version 2 as
7*e28a4053SRui Paulo  * published by the Free Software Foundation.
8*e28a4053SRui Paulo  *
9*e28a4053SRui Paulo  * Alternatively, this software may be distributed under the terms of BSD
10*e28a4053SRui Paulo  * license.
11*e28a4053SRui Paulo  *
12*e28a4053SRui Paulo  * See README and COPYING for more details.
13*e28a4053SRui Paulo  */
14*e28a4053SRui Paulo 
15*e28a4053SRui Paulo #include "includes.h"
16*e28a4053SRui Paulo 
17*e28a4053SRui Paulo #include "common.h"
18*e28a4053SRui Paulo #include "md5.h"
19*e28a4053SRui Paulo #include "md5_i.h"
20*e28a4053SRui Paulo #include "crypto.h"
21*e28a4053SRui Paulo 
22*e28a4053SRui Paulo 
23*e28a4053SRui Paulo static void MD5Transform(u32 buf[4], u32 const in[16]);
24*e28a4053SRui Paulo 
25*e28a4053SRui Paulo 
26*e28a4053SRui Paulo typedef struct MD5Context MD5_CTX;
27*e28a4053SRui Paulo 
28*e28a4053SRui Paulo 
29*e28a4053SRui Paulo /**
30*e28a4053SRui Paulo  * md5_vector - MD5 hash for data vector
31*e28a4053SRui Paulo  * @num_elem: Number of elements in the data vector
32*e28a4053SRui Paulo  * @addr: Pointers to the data areas
33*e28a4053SRui Paulo  * @len: Lengths of the data blocks
34*e28a4053SRui Paulo  * @mac: Buffer for the hash
35*e28a4053SRui Paulo  * Returns: 0 on success, -1 of failure
36*e28a4053SRui Paulo  */
37*e28a4053SRui Paulo int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
38*e28a4053SRui Paulo {
39*e28a4053SRui Paulo 	MD5_CTX ctx;
40*e28a4053SRui Paulo 	size_t i;
41*e28a4053SRui Paulo 
42*e28a4053SRui Paulo 	MD5Init(&ctx);
43*e28a4053SRui Paulo 	for (i = 0; i < num_elem; i++)
44*e28a4053SRui Paulo 		MD5Update(&ctx, addr[i], len[i]);
45*e28a4053SRui Paulo 	MD5Final(mac, &ctx);
46*e28a4053SRui Paulo 	return 0;
47*e28a4053SRui Paulo }
48*e28a4053SRui Paulo 
49*e28a4053SRui Paulo 
50*e28a4053SRui Paulo /* ===== start - public domain MD5 implementation ===== */
51*e28a4053SRui Paulo /*
52*e28a4053SRui Paulo  * This code implements the MD5 message-digest algorithm.
53*e28a4053SRui Paulo  * The algorithm is due to Ron Rivest.  This code was
54*e28a4053SRui Paulo  * written by Colin Plumb in 1993, no copyright is claimed.
55*e28a4053SRui Paulo  * This code is in the public domain; do with it what you wish.
56*e28a4053SRui Paulo  *
57*e28a4053SRui Paulo  * Equivalent code is available from RSA Data Security, Inc.
58*e28a4053SRui Paulo  * This code has been tested against that, and is equivalent,
59*e28a4053SRui Paulo  * except that you don't need to include two pages of legalese
60*e28a4053SRui Paulo  * with every copy.
61*e28a4053SRui Paulo  *
62*e28a4053SRui Paulo  * To compute the message digest of a chunk of bytes, declare an
63*e28a4053SRui Paulo  * MD5Context structure, pass it to MD5Init, call MD5Update as
64*e28a4053SRui Paulo  * needed on buffers full of bytes, and then call MD5Final, which
65*e28a4053SRui Paulo  * will fill a supplied 16-byte array with the digest.
66*e28a4053SRui Paulo  */
67*e28a4053SRui Paulo 
68*e28a4053SRui Paulo #ifndef WORDS_BIGENDIAN
69*e28a4053SRui Paulo #define byteReverse(buf, len)	/* Nothing */
70*e28a4053SRui Paulo #else
71*e28a4053SRui Paulo /*
72*e28a4053SRui Paulo  * Note: this code is harmless on little-endian machines.
73*e28a4053SRui Paulo  */
74*e28a4053SRui Paulo static void byteReverse(unsigned char *buf, unsigned longs)
75*e28a4053SRui Paulo {
76*e28a4053SRui Paulo     u32 t;
77*e28a4053SRui Paulo     do {
78*e28a4053SRui Paulo 	t = (u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
79*e28a4053SRui Paulo 	    ((unsigned) buf[1] << 8 | buf[0]);
80*e28a4053SRui Paulo 	*(u32 *) buf = t;
81*e28a4053SRui Paulo 	buf += 4;
82*e28a4053SRui Paulo     } while (--longs);
83*e28a4053SRui Paulo }
84*e28a4053SRui Paulo #endif
85*e28a4053SRui Paulo 
86*e28a4053SRui Paulo /*
87*e28a4053SRui Paulo  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
88*e28a4053SRui Paulo  * initialization constants.
89*e28a4053SRui Paulo  */
90*e28a4053SRui Paulo void MD5Init(struct MD5Context *ctx)
91*e28a4053SRui Paulo {
92*e28a4053SRui Paulo     ctx->buf[0] = 0x67452301;
93*e28a4053SRui Paulo     ctx->buf[1] = 0xefcdab89;
94*e28a4053SRui Paulo     ctx->buf[2] = 0x98badcfe;
95*e28a4053SRui Paulo     ctx->buf[3] = 0x10325476;
96*e28a4053SRui Paulo 
97*e28a4053SRui Paulo     ctx->bits[0] = 0;
98*e28a4053SRui Paulo     ctx->bits[1] = 0;
99*e28a4053SRui Paulo }
100*e28a4053SRui Paulo 
101*e28a4053SRui Paulo /*
102*e28a4053SRui Paulo  * Update context to reflect the concatenation of another buffer full
103*e28a4053SRui Paulo  * of bytes.
104*e28a4053SRui Paulo  */
105*e28a4053SRui Paulo void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
106*e28a4053SRui Paulo {
107*e28a4053SRui Paulo     u32 t;
108*e28a4053SRui Paulo 
109*e28a4053SRui Paulo     /* Update bitcount */
110*e28a4053SRui Paulo 
111*e28a4053SRui Paulo     t = ctx->bits[0];
112*e28a4053SRui Paulo     if ((ctx->bits[0] = t + ((u32) len << 3)) < t)
113*e28a4053SRui Paulo 	ctx->bits[1]++;		/* Carry from low to high */
114*e28a4053SRui Paulo     ctx->bits[1] += len >> 29;
115*e28a4053SRui Paulo 
116*e28a4053SRui Paulo     t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
117*e28a4053SRui Paulo 
118*e28a4053SRui Paulo     /* Handle any leading odd-sized chunks */
119*e28a4053SRui Paulo 
120*e28a4053SRui Paulo     if (t) {
121*e28a4053SRui Paulo 	unsigned char *p = (unsigned char *) ctx->in + t;
122*e28a4053SRui Paulo 
123*e28a4053SRui Paulo 	t = 64 - t;
124*e28a4053SRui Paulo 	if (len < t) {
125*e28a4053SRui Paulo 	    os_memcpy(p, buf, len);
126*e28a4053SRui Paulo 	    return;
127*e28a4053SRui Paulo 	}
128*e28a4053SRui Paulo 	os_memcpy(p, buf, t);
129*e28a4053SRui Paulo 	byteReverse(ctx->in, 16);
130*e28a4053SRui Paulo 	MD5Transform(ctx->buf, (u32 *) ctx->in);
131*e28a4053SRui Paulo 	buf += t;
132*e28a4053SRui Paulo 	len -= t;
133*e28a4053SRui Paulo     }
134*e28a4053SRui Paulo     /* Process data in 64-byte chunks */
135*e28a4053SRui Paulo 
136*e28a4053SRui Paulo     while (len >= 64) {
137*e28a4053SRui Paulo 	os_memcpy(ctx->in, buf, 64);
138*e28a4053SRui Paulo 	byteReverse(ctx->in, 16);
139*e28a4053SRui Paulo 	MD5Transform(ctx->buf, (u32 *) ctx->in);
140*e28a4053SRui Paulo 	buf += 64;
141*e28a4053SRui Paulo 	len -= 64;
142*e28a4053SRui Paulo     }
143*e28a4053SRui Paulo 
144*e28a4053SRui Paulo     /* Handle any remaining bytes of data. */
145*e28a4053SRui Paulo 
146*e28a4053SRui Paulo     os_memcpy(ctx->in, buf, len);
147*e28a4053SRui Paulo }
148*e28a4053SRui Paulo 
149*e28a4053SRui Paulo /*
150*e28a4053SRui Paulo  * Final wrapup - pad to 64-byte boundary with the bit pattern
151*e28a4053SRui Paulo  * 1 0* (64-bit count of bits processed, MSB-first)
152*e28a4053SRui Paulo  */
153*e28a4053SRui Paulo void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
154*e28a4053SRui Paulo {
155*e28a4053SRui Paulo     unsigned count;
156*e28a4053SRui Paulo     unsigned char *p;
157*e28a4053SRui Paulo 
158*e28a4053SRui Paulo     /* Compute number of bytes mod 64 */
159*e28a4053SRui Paulo     count = (ctx->bits[0] >> 3) & 0x3F;
160*e28a4053SRui Paulo 
161*e28a4053SRui Paulo     /* Set the first char of padding to 0x80.  This is safe since there is
162*e28a4053SRui Paulo        always at least one byte free */
163*e28a4053SRui Paulo     p = ctx->in + count;
164*e28a4053SRui Paulo     *p++ = 0x80;
165*e28a4053SRui Paulo 
166*e28a4053SRui Paulo     /* Bytes of padding needed to make 64 bytes */
167*e28a4053SRui Paulo     count = 64 - 1 - count;
168*e28a4053SRui Paulo 
169*e28a4053SRui Paulo     /* Pad out to 56 mod 64 */
170*e28a4053SRui Paulo     if (count < 8) {
171*e28a4053SRui Paulo 	/* Two lots of padding:  Pad the first block to 64 bytes */
172*e28a4053SRui Paulo 	os_memset(p, 0, count);
173*e28a4053SRui Paulo 	byteReverse(ctx->in, 16);
174*e28a4053SRui Paulo 	MD5Transform(ctx->buf, (u32 *) ctx->in);
175*e28a4053SRui Paulo 
176*e28a4053SRui Paulo 	/* Now fill the next block with 56 bytes */
177*e28a4053SRui Paulo 	os_memset(ctx->in, 0, 56);
178*e28a4053SRui Paulo     } else {
179*e28a4053SRui Paulo 	/* Pad block to 56 bytes */
180*e28a4053SRui Paulo 	os_memset(p, 0, count - 8);
181*e28a4053SRui Paulo     }
182*e28a4053SRui Paulo     byteReverse(ctx->in, 14);
183*e28a4053SRui Paulo 
184*e28a4053SRui Paulo     /* Append length in bits and transform */
185*e28a4053SRui Paulo     ((u32 *) ctx->in)[14] = ctx->bits[0];
186*e28a4053SRui Paulo     ((u32 *) ctx->in)[15] = ctx->bits[1];
187*e28a4053SRui Paulo 
188*e28a4053SRui Paulo     MD5Transform(ctx->buf, (u32 *) ctx->in);
189*e28a4053SRui Paulo     byteReverse((unsigned char *) ctx->buf, 4);
190*e28a4053SRui Paulo     os_memcpy(digest, ctx->buf, 16);
191*e28a4053SRui Paulo     os_memset(ctx, 0, sizeof(ctx));	/* In case it's sensitive */
192*e28a4053SRui Paulo }
193*e28a4053SRui Paulo 
194*e28a4053SRui Paulo /* The four core functions - F1 is optimized somewhat */
195*e28a4053SRui Paulo 
196*e28a4053SRui Paulo /* #define F1(x, y, z) (x & y | ~x & z) */
197*e28a4053SRui Paulo #define F1(x, y, z) (z ^ (x & (y ^ z)))
198*e28a4053SRui Paulo #define F2(x, y, z) F1(z, x, y)
199*e28a4053SRui Paulo #define F3(x, y, z) (x ^ y ^ z)
200*e28a4053SRui Paulo #define F4(x, y, z) (y ^ (x | ~z))
201*e28a4053SRui Paulo 
202*e28a4053SRui Paulo /* This is the central step in the MD5 algorithm. */
203*e28a4053SRui Paulo #define MD5STEP(f, w, x, y, z, data, s) \
204*e28a4053SRui Paulo 	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
205*e28a4053SRui Paulo 
206*e28a4053SRui Paulo /*
207*e28a4053SRui Paulo  * The core of the MD5 algorithm, this alters an existing MD5 hash to
208*e28a4053SRui Paulo  * reflect the addition of 16 longwords of new data.  MD5Update blocks
209*e28a4053SRui Paulo  * the data and converts bytes into longwords for this routine.
210*e28a4053SRui Paulo  */
211*e28a4053SRui Paulo static void MD5Transform(u32 buf[4], u32 const in[16])
212*e28a4053SRui Paulo {
213*e28a4053SRui Paulo     register u32 a, b, c, d;
214*e28a4053SRui Paulo 
215*e28a4053SRui Paulo     a = buf[0];
216*e28a4053SRui Paulo     b = buf[1];
217*e28a4053SRui Paulo     c = buf[2];
218*e28a4053SRui Paulo     d = buf[3];
219*e28a4053SRui Paulo 
220*e28a4053SRui Paulo     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
221*e28a4053SRui Paulo     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
222*e28a4053SRui Paulo     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
223*e28a4053SRui Paulo     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
224*e28a4053SRui Paulo     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
225*e28a4053SRui Paulo     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
226*e28a4053SRui Paulo     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
227*e28a4053SRui Paulo     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
228*e28a4053SRui Paulo     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
229*e28a4053SRui Paulo     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
230*e28a4053SRui Paulo     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
231*e28a4053SRui Paulo     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
232*e28a4053SRui Paulo     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
233*e28a4053SRui Paulo     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
234*e28a4053SRui Paulo     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
235*e28a4053SRui Paulo     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
236*e28a4053SRui Paulo 
237*e28a4053SRui Paulo     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
238*e28a4053SRui Paulo     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
239*e28a4053SRui Paulo     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
240*e28a4053SRui Paulo     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
241*e28a4053SRui Paulo     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
242*e28a4053SRui Paulo     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
243*e28a4053SRui Paulo     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
244*e28a4053SRui Paulo     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
245*e28a4053SRui Paulo     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
246*e28a4053SRui Paulo     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
247*e28a4053SRui Paulo     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
248*e28a4053SRui Paulo     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
249*e28a4053SRui Paulo     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
250*e28a4053SRui Paulo     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
251*e28a4053SRui Paulo     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
252*e28a4053SRui Paulo     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
253*e28a4053SRui Paulo 
254*e28a4053SRui Paulo     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
255*e28a4053SRui Paulo     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
256*e28a4053SRui Paulo     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
257*e28a4053SRui Paulo     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
258*e28a4053SRui Paulo     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
259*e28a4053SRui Paulo     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
260*e28a4053SRui Paulo     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
261*e28a4053SRui Paulo     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
262*e28a4053SRui Paulo     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
263*e28a4053SRui Paulo     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
264*e28a4053SRui Paulo     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
265*e28a4053SRui Paulo     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
266*e28a4053SRui Paulo     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
267*e28a4053SRui Paulo     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
268*e28a4053SRui Paulo     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
269*e28a4053SRui Paulo     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
270*e28a4053SRui Paulo 
271*e28a4053SRui Paulo     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
272*e28a4053SRui Paulo     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
273*e28a4053SRui Paulo     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
274*e28a4053SRui Paulo     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
275*e28a4053SRui Paulo     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
276*e28a4053SRui Paulo     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
277*e28a4053SRui Paulo     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
278*e28a4053SRui Paulo     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
279*e28a4053SRui Paulo     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
280*e28a4053SRui Paulo     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
281*e28a4053SRui Paulo     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
282*e28a4053SRui Paulo     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
283*e28a4053SRui Paulo     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
284*e28a4053SRui Paulo     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
285*e28a4053SRui Paulo     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
286*e28a4053SRui Paulo     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
287*e28a4053SRui Paulo 
288*e28a4053SRui Paulo     buf[0] += a;
289*e28a4053SRui Paulo     buf[1] += b;
290*e28a4053SRui Paulo     buf[2] += c;
291*e28a4053SRui Paulo     buf[3] += d;
292*e28a4053SRui Paulo }
293*e28a4053SRui Paulo /* ===== end - public domain MD5 implementation ===== */
294