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