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