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