md5-internal.c revision 337817
1234370Sjasone/* 2234370Sjasone * MD5 hash implementation and interface functions 3234370Sjasone * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 4234370Sjasone * 5234370Sjasone * This software may be distributed under the terms of the BSD license. 6234370Sjasone * See README for more details. 7234370Sjasone */ 8234370Sjasone 9234370Sjasone#include "includes.h" 10234370Sjasone 11234370Sjasone#include "common.h" 12234370Sjasone#include "md5.h" 13234370Sjasone#include "md5_i.h" 14234370Sjasone#include "crypto.h" 15234370Sjasone 16234370Sjasone 17234370Sjasonestatic void MD5Transform(u32 buf[4], u32 const in[16]); 18234370Sjasone 19234370Sjasone 20234370Sjasonetypedef struct MD5Context MD5_CTX; 21234370Sjasone 22234370Sjasone 23234370Sjasone/** 24234370Sjasone * md5_vector - MD5 hash for data vector 25234370Sjasone * @num_elem: Number of elements in the data vector 26234370Sjasone * @addr: Pointers to the data areas 27234370Sjasone * @len: Lengths of the data blocks 28234370Sjasone * @mac: Buffer for the hash 29234370Sjasone * Returns: 0 on success, -1 of failure 30234370Sjasone */ 31234370Sjasoneint md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 32234370Sjasone{ 33234370Sjasone MD5_CTX ctx; 34234370Sjasone size_t i; 35234370Sjasone 36234370Sjasone if (TEST_FAIL()) 37234370Sjasone return -1; 38234370Sjasone 39234370Sjasone MD5Init(&ctx); 40234370Sjasone for (i = 0; i < num_elem; i++) 41242844Sjasone MD5Update(&ctx, addr[i], len[i]); 42242844Sjasone MD5Final(mac, &ctx); 43234370Sjasone return 0; 44242844Sjasone} 45234370Sjasone 46234370Sjasone 47234370Sjasone/* ===== start - public domain MD5 implementation ===== */ 48234370Sjasone/* 49234370Sjasone * This code implements the MD5 message-digest algorithm. 50234370Sjasone * The algorithm is due to Ron Rivest. This code was 51234370Sjasone * written by Colin Plumb in 1993, no copyright is claimed. 52234370Sjasone * This code is in the public domain; do with it what you wish. 53234370Sjasone * 54234370Sjasone * Equivalent code is available from RSA Data Security, Inc. 55234370Sjasone * This code has been tested against that, and is equivalent, 56234370Sjasone * except that you don't need to include two pages of legalese 57234370Sjasone * with every copy. 58234370Sjasone * 59234370Sjasone * To compute the message digest of a chunk of bytes, declare an 60234370Sjasone * MD5Context structure, pass it to MD5Init, call MD5Update as 61234370Sjasone * needed on buffers full of bytes, and then call MD5Final, which 62234370Sjasone * will fill a supplied 16-byte array with the digest. 63234370Sjasone */ 64234370Sjasone 65234370Sjasone#ifndef WORDS_BIGENDIAN 66234370Sjasone#define byteReverse(buf, len) /* Nothing */ 67234370Sjasone#else 68234370Sjasone/* 69234370Sjasone * Note: this code is harmless on little-endian machines. 70234370Sjasone */ 71234370Sjasonestatic void byteReverse(unsigned char *buf, unsigned longs) 72242844Sjasone{ 73234370Sjasone u32 t; 74234370Sjasone do { 75234370Sjasone t = (u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | 76234370Sjasone ((unsigned) buf[1] << 8 | buf[0]); 77234370Sjasone *(u32 *) buf = t; 78234370Sjasone buf += 4; 79234370Sjasone } while (--longs); 80234370Sjasone} 81234370Sjasone#endif 82234370Sjasone 83234370Sjasone/* 84234370Sjasone * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 85234370Sjasone * initialization constants. 86234370Sjasone */ 87234370Sjasonevoid MD5Init(struct MD5Context *ctx) 88234370Sjasone{ 89234370Sjasone ctx->buf[0] = 0x67452301; 90234370Sjasone ctx->buf[1] = 0xefcdab89; 91234370Sjasone ctx->buf[2] = 0x98badcfe; 92234370Sjasone ctx->buf[3] = 0x10325476; 93234370Sjasone 94234370Sjasone ctx->bits[0] = 0; 95234370Sjasone ctx->bits[1] = 0; 96235322Sjasone} 97234370Sjasone 98234370Sjasone/* 99234370Sjasone * Update context to reflect the concatenation of another buffer full 100234370Sjasone * of bytes. 101234370Sjasone */ 102235322Sjasonevoid MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) 103234370Sjasone{ 104234370Sjasone u32 t; 105234370Sjasone 106234370Sjasone /* Update bitcount */ 107234370Sjasone 108234370Sjasone t = ctx->bits[0]; 109234370Sjasone if ((ctx->bits[0] = t + ((u32) len << 3)) < t) 110234370Sjasone ctx->bits[1]++; /* Carry from low to high */ 111234370Sjasone ctx->bits[1] += len >> 29; 112235238Sjasone 113235238Sjasone t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 114234370Sjasone 115234370Sjasone /* Handle any leading odd-sized chunks */ 116234370Sjasone 117234370Sjasone if (t) { 118234370Sjasone unsigned char *p = (unsigned char *) ctx->in + t; 119234370Sjasone 120234370Sjasone t = 64 - t; 121235322Sjasone if (len < t) { 122235238Sjasone os_memcpy(p, buf, len); 123235322Sjasone return; 124234370Sjasone } 125234370Sjasone os_memcpy(p, buf, t); 126235322Sjasone byteReverse(ctx->in, 16); 127235238Sjasone MD5Transform(ctx->buf, (u32 *) ctx->in); 128235322Sjasone buf += t; 129234370Sjasone len -= t; 130234370Sjasone } 131235238Sjasone /* Process data in 64-byte chunks */ 132235238Sjasone 133235238Sjasone while (len >= 64) { 134234370Sjasone os_memcpy(ctx->in, buf, 64); 135234370Sjasone byteReverse(ctx->in, 16); 136235322Sjasone MD5Transform(ctx->buf, (u32 *) ctx->in); 137235238Sjasone buf += 64; 138235322Sjasone len -= 64; 139234370Sjasone } 140234370Sjasone 141235238Sjasone /* Handle any remaining bytes of data. */ 142234370Sjasone 143234370Sjasone os_memcpy(ctx->in, buf, len); 144235322Sjasone} 145234370Sjasone 146234370Sjasone/* 147235238Sjasone * Final wrapup - pad to 64-byte boundary with the bit pattern 148235238Sjasone * 1 0* (64-bit count of bits processed, MSB-first) 149235238Sjasone */ 150235238Sjasonevoid MD5Final(unsigned char digest[16], struct MD5Context *ctx) 151235238Sjasone{ 152235238Sjasone unsigned count; 153234370Sjasone unsigned char *p; 154234370Sjasone 155234370Sjasone /* Compute number of bytes mod 64 */ 156234370Sjasone count = (ctx->bits[0] >> 3) & 0x3F; 157234370Sjasone 158234370Sjasone /* Set the first char of padding to 0x80. This is safe since there is 159234370Sjasone always at least one byte free */ 160234370Sjasone p = ctx->in + count; 161234370Sjasone *p++ = 0x80; 162234370Sjasone 163234370Sjasone /* Bytes of padding needed to make 64 bytes */ 164234370Sjasone count = 64 - 1 - count; 165242844Sjasone 166234370Sjasone /* Pad out to 56 mod 64 */ 167242844Sjasone if (count < 8) { 168242844Sjasone /* Two lots of padding: Pad the first block to 64 bytes */ 169234370Sjasone os_memset(p, 0, count); 170234370Sjasone byteReverse(ctx->in, 16); 171242844Sjasone MD5Transform(ctx->buf, (u32 *) ctx->in); 172234370Sjasone 173242844Sjasone /* Now fill the next block with 56 bytes */ 174242844Sjasone os_memset(ctx->in, 0, 56); 175242844Sjasone } else { 176234370Sjasone /* Pad block to 56 bytes */ 177242844Sjasone os_memset(p, 0, count - 8); 178242844Sjasone } 179242844Sjasone byteReverse(ctx->in, 14); 180242844Sjasone 181242844Sjasone /* Append length in bits and transform */ 182242844Sjasone ((u32 *) aliasing_hide_typecast(ctx->in, u32))[14] = ctx->bits[0]; 183242844Sjasone ((u32 *) aliasing_hide_typecast(ctx->in, u32))[15] = ctx->bits[1]; 184242844Sjasone 185234370Sjasone MD5Transform(ctx->buf, (u32 *) ctx->in); 186234370Sjasone byteReverse((unsigned char *) ctx->buf, 4); 187234370Sjasone os_memcpy(digest, ctx->buf, 16); 188234370Sjasone os_memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ 189234370Sjasone} 190242844Sjasone 191234370Sjasone/* The four core functions - F1 is optimized somewhat */ 192234370Sjasone 193234370Sjasone/* #define F1(x, y, z) (x & y | ~x & z) */ 194234370Sjasone#define F1(x, y, z) (z ^ (x & (y ^ z))) 195234370Sjasone#define F2(x, y, z) F1(z, x, y) 196234370Sjasone#define F3(x, y, z) (x ^ y ^ z) 197234370Sjasone#define F4(x, y, z) (y ^ (x | ~z)) 198234370Sjasone 199234370Sjasone/* This is the central step in the MD5 algorithm. */ 200234370Sjasone#define MD5STEP(f, w, x, y, z, data, s) \ 201234370Sjasone ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 202234370Sjasone 203234370Sjasone/* 204234370Sjasone * The core of the MD5 algorithm, this alters an existing MD5 hash to 205234370Sjasone * reflect the addition of 16 longwords of new data. MD5Update blocks 206234370Sjasone * the data and converts bytes into longwords for this routine. 207234370Sjasone */ 208234370Sjasonestatic void MD5Transform(u32 buf[4], u32 const in[16]) 209234370Sjasone{ 210234370Sjasone register u32 a, b, c, d; 211234370Sjasone 212234370Sjasone a = buf[0]; 213234370Sjasone b = buf[1]; 214234370Sjasone c = buf[2]; 215234370Sjasone d = buf[3]; 216234370Sjasone 217234370Sjasone MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 218234370Sjasone MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 219234370Sjasone MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 220234370Sjasone MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 221234370Sjasone MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 222234370Sjasone MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 223234370Sjasone MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 224234370Sjasone MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 225234370Sjasone MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 226234370Sjasone MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 227234370Sjasone MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 228234370Sjasone MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 229234370Sjasone MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 230234370Sjasone MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 231234370Sjasone MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 232234370Sjasone MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 233234370Sjasone 234234370Sjasone MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 235234370Sjasone MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 236234370Sjasone MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 237234370Sjasone MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 238234370Sjasone MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 239234370Sjasone MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 240234370Sjasone MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 241234370Sjasone MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 242234370Sjasone MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 243234370Sjasone MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 244234370Sjasone MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 245234370Sjasone MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 246234370Sjasone MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 247234370Sjasone MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 248234370Sjasone MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 249234370Sjasone MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 250234370Sjasone 251234370Sjasone MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 252234370Sjasone MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 253234370Sjasone MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 254234370Sjasone MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 255234370Sjasone MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 256234370Sjasone MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 257234370Sjasone MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 258234370Sjasone MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 259234370Sjasone MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 260234370Sjasone MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 261234370Sjasone MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 262234370Sjasone MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 263234370Sjasone MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 264234370Sjasone MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 265234370Sjasone MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 266234370Sjasone MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 267234370Sjasone 268234370Sjasone MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 269234370Sjasone MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 270234370Sjasone MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 271234370Sjasone MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 272234370Sjasone MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 273234370Sjasone MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 274234370Sjasone MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 275234370Sjasone MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 276234370Sjasone MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 277234370Sjasone MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 278234370Sjasone MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 279234370Sjasone MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 280234370Sjasone MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 281234370Sjasone MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 282234370Sjasone MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 283234370Sjasone MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 284234370Sjasone 285234370Sjasone buf[0] += a; 286234370Sjasone buf[1] += b; 287234370Sjasone buf[2] += c; 288234370Sjasone buf[3] += d; 289234370Sjasone} 290234370Sjasone/* ===== end - public domain MD5 implementation ===== */ 291234370Sjasone