xref: /netbsd-src/external/bsd/wpa/dist/src/crypto/md5-internal.c (revision 36ebd06e5ab61115eab7acca17a2350fc8222071)
18dbcf02cSchristos /*
28dbcf02cSchristos  * MD5 hash implementation and interface functions
38dbcf02cSchristos  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
78dbcf02cSchristos  */
88dbcf02cSchristos 
98dbcf02cSchristos #include "includes.h"
108dbcf02cSchristos 
118dbcf02cSchristos #include "common.h"
128dbcf02cSchristos #include "md5.h"
138dbcf02cSchristos #include "md5_i.h"
148dbcf02cSchristos #include "crypto.h"
158dbcf02cSchristos 
168dbcf02cSchristos 
178dbcf02cSchristos static void MD5Transform(u32 buf[4], u32 const in[16]);
188dbcf02cSchristos 
198dbcf02cSchristos 
208dbcf02cSchristos typedef struct MD5Context MD5_CTX;
218dbcf02cSchristos 
228dbcf02cSchristos 
238dbcf02cSchristos /**
248dbcf02cSchristos  * md5_vector - MD5 hash for data vector
258dbcf02cSchristos  * @num_elem: Number of elements in the data vector
268dbcf02cSchristos  * @addr: Pointers to the data areas
278dbcf02cSchristos  * @len: Lengths of the data blocks
288dbcf02cSchristos  * @mac: Buffer for the hash
298dbcf02cSchristos  * Returns: 0 on success, -1 of failure
308dbcf02cSchristos  */
md5_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)318dbcf02cSchristos int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
328dbcf02cSchristos {
338dbcf02cSchristos 	MD5_CTX ctx;
348dbcf02cSchristos 	size_t i;
358dbcf02cSchristos 
36*36ebd06eSchristos 	if (TEST_FAIL())
37*36ebd06eSchristos 		return -1;
38*36ebd06eSchristos 
398dbcf02cSchristos 	MD5Init(&ctx);
408dbcf02cSchristos 	for (i = 0; i < num_elem; i++)
418dbcf02cSchristos 		MD5Update(&ctx, addr[i], len[i]);
428dbcf02cSchristos 	MD5Final(mac, &ctx);
438dbcf02cSchristos 	return 0;
448dbcf02cSchristos }
458dbcf02cSchristos 
468dbcf02cSchristos 
478dbcf02cSchristos /* ===== start - public domain MD5 implementation ===== */
488dbcf02cSchristos /*
498dbcf02cSchristos  * This code implements the MD5 message-digest algorithm.
508dbcf02cSchristos  * The algorithm is due to Ron Rivest.  This code was
518dbcf02cSchristos  * written by Colin Plumb in 1993, no copyright is claimed.
528dbcf02cSchristos  * This code is in the public domain; do with it what you wish.
538dbcf02cSchristos  *
548dbcf02cSchristos  * Equivalent code is available from RSA Data Security, Inc.
558dbcf02cSchristos  * This code has been tested against that, and is equivalent,
568dbcf02cSchristos  * except that you don't need to include two pages of legalese
578dbcf02cSchristos  * with every copy.
588dbcf02cSchristos  *
598dbcf02cSchristos  * To compute the message digest of a chunk of bytes, declare an
608dbcf02cSchristos  * MD5Context structure, pass it to MD5Init, call MD5Update as
618dbcf02cSchristos  * needed on buffers full of bytes, and then call MD5Final, which
628dbcf02cSchristos  * will fill a supplied 16-byte array with the digest.
638dbcf02cSchristos  */
648dbcf02cSchristos 
658dbcf02cSchristos #ifndef WORDS_BIGENDIAN
668dbcf02cSchristos #define byteReverse(buf, len)	/* Nothing */
678dbcf02cSchristos #else
688dbcf02cSchristos /*
698dbcf02cSchristos  * Note: this code is harmless on little-endian machines.
708dbcf02cSchristos  */
byteReverse(unsigned char * buf,unsigned longs)718dbcf02cSchristos static void byteReverse(unsigned char *buf, unsigned longs)
728dbcf02cSchristos {
738dbcf02cSchristos     u32 t;
748dbcf02cSchristos     do {
758dbcf02cSchristos 	t = (u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
768dbcf02cSchristos 	    ((unsigned) buf[1] << 8 | buf[0]);
778dbcf02cSchristos 	*(u32 *) buf = t;
788dbcf02cSchristos 	buf += 4;
798dbcf02cSchristos     } while (--longs);
808dbcf02cSchristos }
818dbcf02cSchristos #endif
828dbcf02cSchristos 
838dbcf02cSchristos /*
848dbcf02cSchristos  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
858dbcf02cSchristos  * initialization constants.
868dbcf02cSchristos  */
MD5Init(struct MD5Context * ctx)878dbcf02cSchristos void MD5Init(struct MD5Context *ctx)
888dbcf02cSchristos {
898dbcf02cSchristos     ctx->buf[0] = 0x67452301;
908dbcf02cSchristos     ctx->buf[1] = 0xefcdab89;
918dbcf02cSchristos     ctx->buf[2] = 0x98badcfe;
928dbcf02cSchristos     ctx->buf[3] = 0x10325476;
938dbcf02cSchristos 
948dbcf02cSchristos     ctx->bits[0] = 0;
958dbcf02cSchristos     ctx->bits[1] = 0;
968dbcf02cSchristos }
978dbcf02cSchristos 
988dbcf02cSchristos /*
998dbcf02cSchristos  * Update context to reflect the concatenation of another buffer full
1008dbcf02cSchristos  * of bytes.
1018dbcf02cSchristos  */
MD5Update(struct MD5Context * ctx,unsigned char const * buf,unsigned len)1028dbcf02cSchristos void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
1038dbcf02cSchristos {
1048dbcf02cSchristos     u32 t;
1058dbcf02cSchristos 
1068dbcf02cSchristos     /* Update bitcount */
1078dbcf02cSchristos 
1088dbcf02cSchristos     t = ctx->bits[0];
1098dbcf02cSchristos     if ((ctx->bits[0] = t + ((u32) len << 3)) < t)
1108dbcf02cSchristos 	ctx->bits[1]++;		/* Carry from low to high */
1118dbcf02cSchristos     ctx->bits[1] += len >> 29;
1128dbcf02cSchristos 
1138dbcf02cSchristos     t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
1148dbcf02cSchristos 
1158dbcf02cSchristos     /* Handle any leading odd-sized chunks */
1168dbcf02cSchristos 
1178dbcf02cSchristos     if (t) {
1188dbcf02cSchristos 	unsigned char *p = (unsigned char *) ctx->in + t;
1198dbcf02cSchristos 
1208dbcf02cSchristos 	t = 64 - t;
1218dbcf02cSchristos 	if (len < t) {
1228dbcf02cSchristos 	    os_memcpy(p, buf, len);
1238dbcf02cSchristos 	    return;
1248dbcf02cSchristos 	}
1258dbcf02cSchristos 	os_memcpy(p, buf, t);
1268dbcf02cSchristos 	byteReverse(ctx->in, 16);
1278dbcf02cSchristos 	MD5Transform(ctx->buf, (u32 *) ctx->in);
1288dbcf02cSchristos 	buf += t;
1298dbcf02cSchristos 	len -= t;
1308dbcf02cSchristos     }
1318dbcf02cSchristos     /* Process data in 64-byte chunks */
1328dbcf02cSchristos 
1338dbcf02cSchristos     while (len >= 64) {
1348dbcf02cSchristos 	os_memcpy(ctx->in, buf, 64);
1358dbcf02cSchristos 	byteReverse(ctx->in, 16);
1368dbcf02cSchristos 	MD5Transform(ctx->buf, (u32 *) ctx->in);
1378dbcf02cSchristos 	buf += 64;
1388dbcf02cSchristos 	len -= 64;
1398dbcf02cSchristos     }
1408dbcf02cSchristos 
1418dbcf02cSchristos     /* Handle any remaining bytes of data. */
1428dbcf02cSchristos 
1438dbcf02cSchristos     os_memcpy(ctx->in, buf, len);
1448dbcf02cSchristos }
1458dbcf02cSchristos 
1468dbcf02cSchristos /*
1478dbcf02cSchristos  * Final wrapup - pad to 64-byte boundary with the bit pattern
1488dbcf02cSchristos  * 1 0* (64-bit count of bits processed, MSB-first)
1498dbcf02cSchristos  */
MD5Final(unsigned char digest[16],struct MD5Context * ctx)1508dbcf02cSchristos void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1518dbcf02cSchristos {
1528dbcf02cSchristos     unsigned count;
1538dbcf02cSchristos     unsigned char *p;
1548dbcf02cSchristos 
1558dbcf02cSchristos     /* Compute number of bytes mod 64 */
1568dbcf02cSchristos     count = (ctx->bits[0] >> 3) & 0x3F;
1578dbcf02cSchristos 
1588dbcf02cSchristos     /* Set the first char of padding to 0x80.  This is safe since there is
1598dbcf02cSchristos        always at least one byte free */
1608dbcf02cSchristos     p = ctx->in + count;
1618dbcf02cSchristos     *p++ = 0x80;
1628dbcf02cSchristos 
1638dbcf02cSchristos     /* Bytes of padding needed to make 64 bytes */
1648dbcf02cSchristos     count = 64 - 1 - count;
1658dbcf02cSchristos 
1668dbcf02cSchristos     /* Pad out to 56 mod 64 */
1678dbcf02cSchristos     if (count < 8) {
1688dbcf02cSchristos 	/* Two lots of padding:  Pad the first block to 64 bytes */
1698dbcf02cSchristos 	os_memset(p, 0, count);
1708dbcf02cSchristos 	byteReverse(ctx->in, 16);
1718dbcf02cSchristos 	MD5Transform(ctx->buf, (u32 *) ctx->in);
1728dbcf02cSchristos 
1738dbcf02cSchristos 	/* Now fill the next block with 56 bytes */
1748dbcf02cSchristos 	os_memset(ctx->in, 0, 56);
1758dbcf02cSchristos     } else {
1768dbcf02cSchristos 	/* Pad block to 56 bytes */
1778dbcf02cSchristos 	os_memset(p, 0, count - 8);
1788dbcf02cSchristos     }
1798dbcf02cSchristos     byteReverse(ctx->in, 14);
1808dbcf02cSchristos 
1818dbcf02cSchristos     /* Append length in bits and transform */
182e604d861Schristos     ((u32 *) aliasing_hide_typecast(ctx->in, u32))[14] = ctx->bits[0];
183e604d861Schristos     ((u32 *) aliasing_hide_typecast(ctx->in, u32))[15] = ctx->bits[1];
1848dbcf02cSchristos 
1858dbcf02cSchristos     MD5Transform(ctx->buf, (u32 *) ctx->in);
1868dbcf02cSchristos     byteReverse((unsigned char *) ctx->buf, 4);
1878dbcf02cSchristos     os_memcpy(digest, ctx->buf, 16);
188111b9fd8Schristos     os_memset(ctx, 0, sizeof(*ctx));	/* In case it's sensitive */
1898dbcf02cSchristos }
1908dbcf02cSchristos 
1918dbcf02cSchristos /* The four core functions - F1 is optimized somewhat */
1928dbcf02cSchristos 
1938dbcf02cSchristos /* #define F1(x, y, z) (x & y | ~x & z) */
1948dbcf02cSchristos #define F1(x, y, z) (z ^ (x & (y ^ z)))
1958dbcf02cSchristos #define F2(x, y, z) F1(z, x, y)
1968dbcf02cSchristos #define F3(x, y, z) (x ^ y ^ z)
1978dbcf02cSchristos #define F4(x, y, z) (y ^ (x | ~z))
1988dbcf02cSchristos 
1998dbcf02cSchristos /* This is the central step in the MD5 algorithm. */
2008dbcf02cSchristos #define MD5STEP(f, w, x, y, z, data, s) \
2018dbcf02cSchristos 	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
2028dbcf02cSchristos 
2038dbcf02cSchristos /*
2048dbcf02cSchristos  * The core of the MD5 algorithm, this alters an existing MD5 hash to
2058dbcf02cSchristos  * reflect the addition of 16 longwords of new data.  MD5Update blocks
2068dbcf02cSchristos  * the data and converts bytes into longwords for this routine.
2078dbcf02cSchristos  */
MD5Transform(u32 buf[4],u32 const in[16])2088dbcf02cSchristos static void MD5Transform(u32 buf[4], u32 const in[16])
2098dbcf02cSchristos {
2108dbcf02cSchristos     register u32 a, b, c, d;
2118dbcf02cSchristos 
2128dbcf02cSchristos     a = buf[0];
2138dbcf02cSchristos     b = buf[1];
2148dbcf02cSchristos     c = buf[2];
2158dbcf02cSchristos     d = buf[3];
2168dbcf02cSchristos 
2178dbcf02cSchristos     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
2188dbcf02cSchristos     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
2198dbcf02cSchristos     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
2208dbcf02cSchristos     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
2218dbcf02cSchristos     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
2228dbcf02cSchristos     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
2238dbcf02cSchristos     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
2248dbcf02cSchristos     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
2258dbcf02cSchristos     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
2268dbcf02cSchristos     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
2278dbcf02cSchristos     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
2288dbcf02cSchristos     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
2298dbcf02cSchristos     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
2308dbcf02cSchristos     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
2318dbcf02cSchristos     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
2328dbcf02cSchristos     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
2338dbcf02cSchristos 
2348dbcf02cSchristos     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
2358dbcf02cSchristos     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
2368dbcf02cSchristos     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
2378dbcf02cSchristos     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
2388dbcf02cSchristos     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
2398dbcf02cSchristos     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
2408dbcf02cSchristos     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
2418dbcf02cSchristos     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
2428dbcf02cSchristos     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
2438dbcf02cSchristos     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
2448dbcf02cSchristos     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
2458dbcf02cSchristos     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
2468dbcf02cSchristos     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
2478dbcf02cSchristos     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
2488dbcf02cSchristos     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
2498dbcf02cSchristos     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
2508dbcf02cSchristos 
2518dbcf02cSchristos     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
2528dbcf02cSchristos     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
2538dbcf02cSchristos     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
2548dbcf02cSchristos     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
2558dbcf02cSchristos     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
2568dbcf02cSchristos     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
2578dbcf02cSchristos     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
2588dbcf02cSchristos     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
2598dbcf02cSchristos     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
2608dbcf02cSchristos     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
2618dbcf02cSchristos     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
2628dbcf02cSchristos     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
2638dbcf02cSchristos     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
2648dbcf02cSchristos     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
2658dbcf02cSchristos     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
2668dbcf02cSchristos     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
2678dbcf02cSchristos 
2688dbcf02cSchristos     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
2698dbcf02cSchristos     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
2708dbcf02cSchristos     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
2718dbcf02cSchristos     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
2728dbcf02cSchristos     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
2738dbcf02cSchristos     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
2748dbcf02cSchristos     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
2758dbcf02cSchristos     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
2768dbcf02cSchristos     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
2778dbcf02cSchristos     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
2788dbcf02cSchristos     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
2798dbcf02cSchristos     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
2808dbcf02cSchristos     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
2818dbcf02cSchristos     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
2828dbcf02cSchristos     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
2838dbcf02cSchristos     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
2848dbcf02cSchristos 
2858dbcf02cSchristos     buf[0] += a;
2868dbcf02cSchristos     buf[1] += b;
2878dbcf02cSchristos     buf[2] += c;
2888dbcf02cSchristos     buf[3] += d;
2898dbcf02cSchristos }
2908dbcf02cSchristos /* ===== end - public domain MD5 implementation ===== */
291