1 /* 2 * MD4 hash implementation 3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "crypto.h" 19 20 #define MD4_BLOCK_LENGTH 64 21 #define MD4_DIGEST_LENGTH 16 22 23 typedef struct MD4Context { 24 u32 state[4]; /* state */ 25 u64 count; /* number of bits, mod 2^64 */ 26 u8 buffer[MD4_BLOCK_LENGTH]; /* input buffer */ 27 } MD4_CTX; 28 29 30 static void MD4Init(MD4_CTX *ctx); 31 static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len); 32 static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx); 33 34 35 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 36 { 37 MD4_CTX ctx; 38 size_t i; 39 40 MD4Init(&ctx); 41 for (i = 0; i < num_elem; i++) 42 MD4Update(&ctx, addr[i], len[i]); 43 MD4Final(mac, &ctx); 44 return 0; 45 } 46 47 48 /* ===== start - public domain MD4 implementation ===== */ 49 /* $OpenBSD: md4.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */ 50 51 /* 52 * This code implements the MD4 message-digest algorithm. 53 * The algorithm is due to Ron Rivest. This code was 54 * written by Colin Plumb in 1993, no copyright is claimed. 55 * This code is in the public domain; do with it what you wish. 56 * Todd C. Miller modified the MD5 code to do MD4 based on RFC 1186. 57 * 58 * Equivalent code is available from RSA Data Security, Inc. 59 * This code has been tested against that, and is equivalent, 60 * except that you don't need to include two pages of legalese 61 * with every copy. 62 * 63 * To compute the message digest of a chunk of bytes, declare an 64 * MD4Context structure, pass it to MD4Init, call MD4Update as 65 * needed on buffers full of bytes, and then call MD4Final, which 66 * will fill a supplied 16-byte array with the digest. 67 */ 68 69 #define MD4_DIGEST_STRING_LENGTH (MD4_DIGEST_LENGTH * 2 + 1) 70 71 72 static void 73 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH]); 74 75 #define PUT_64BIT_LE(cp, value) do { \ 76 (cp)[7] = (value) >> 56; \ 77 (cp)[6] = (value) >> 48; \ 78 (cp)[5] = (value) >> 40; \ 79 (cp)[4] = (value) >> 32; \ 80 (cp)[3] = (value) >> 24; \ 81 (cp)[2] = (value) >> 16; \ 82 (cp)[1] = (value) >> 8; \ 83 (cp)[0] = (value); } while (0) 84 85 #define PUT_32BIT_LE(cp, value) do { \ 86 (cp)[3] = (value) >> 24; \ 87 (cp)[2] = (value) >> 16; \ 88 (cp)[1] = (value) >> 8; \ 89 (cp)[0] = (value); } while (0) 90 91 static u8 PADDING[MD4_BLOCK_LENGTH] = { 92 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 95 }; 96 97 /* 98 * Start MD4 accumulation. 99 * Set bit count to 0 and buffer to mysterious initialization constants. 100 */ 101 static void MD4Init(MD4_CTX *ctx) 102 { 103 ctx->count = 0; 104 ctx->state[0] = 0x67452301; 105 ctx->state[1] = 0xefcdab89; 106 ctx->state[2] = 0x98badcfe; 107 ctx->state[3] = 0x10325476; 108 } 109 110 /* 111 * Update context to reflect the concatenation of another buffer full 112 * of bytes. 113 */ 114 static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len) 115 { 116 size_t have, need; 117 118 /* Check how many bytes we already have and how many more we need. */ 119 have = (size_t)((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1)); 120 need = MD4_BLOCK_LENGTH - have; 121 122 /* Update bitcount */ 123 ctx->count += (u64)len << 3; 124 125 if (len >= need) { 126 if (have != 0) { 127 os_memcpy(ctx->buffer + have, input, need); 128 MD4Transform(ctx->state, ctx->buffer); 129 input += need; 130 len -= need; 131 have = 0; 132 } 133 134 /* Process data in MD4_BLOCK_LENGTH-byte chunks. */ 135 while (len >= MD4_BLOCK_LENGTH) { 136 MD4Transform(ctx->state, input); 137 input += MD4_BLOCK_LENGTH; 138 len -= MD4_BLOCK_LENGTH; 139 } 140 } 141 142 /* Handle any remaining bytes of data. */ 143 if (len != 0) 144 os_memcpy(ctx->buffer + have, input, len); 145 } 146 147 /* 148 * Pad pad to 64-byte boundary with the bit pattern 149 * 1 0* (64-bit count of bits processed, MSB-first) 150 */ 151 static void MD4Pad(MD4_CTX *ctx) 152 { 153 u8 count[8]; 154 size_t padlen; 155 156 /* Convert count to 8 bytes in little endian order. */ 157 PUT_64BIT_LE(count, ctx->count); 158 159 /* Pad out to 56 mod 64. */ 160 padlen = MD4_BLOCK_LENGTH - 161 ((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1)); 162 if (padlen < 1 + 8) 163 padlen += MD4_BLOCK_LENGTH; 164 MD4Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ 165 MD4Update(ctx, count, 8); 166 } 167 168 /* 169 * Final wrapup--call MD4Pad, fill in digest and zero out ctx. 170 */ 171 static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx) 172 { 173 int i; 174 175 MD4Pad(ctx); 176 if (digest != NULL) { 177 for (i = 0; i < 4; i++) 178 PUT_32BIT_LE(digest + i * 4, ctx->state[i]); 179 os_memset(ctx, 0, sizeof(*ctx)); 180 } 181 } 182 183 184 /* The three core functions - F1 is optimized somewhat */ 185 186 /* #define F1(x, y, z) (x & y | ~x & z) */ 187 #define F1(x, y, z) (z ^ (x & (y ^ z))) 188 #define F2(x, y, z) ((x & y) | (x & z) | (y & z)) 189 #define F3(x, y, z) (x ^ y ^ z) 190 191 /* This is the central step in the MD4 algorithm. */ 192 #define MD4STEP(f, w, x, y, z, data, s) \ 193 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s) ) 194 195 /* 196 * The core of the MD4 algorithm, this alters an existing MD4 hash to 197 * reflect the addition of 16 longwords of new data. MD4Update blocks 198 * the data and converts bytes into longwords for this routine. 199 */ 200 static void 201 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH]) 202 { 203 u32 a, b, c, d, in[MD4_BLOCK_LENGTH / 4]; 204 205 #if BYTE_ORDER == LITTLE_ENDIAN 206 os_memcpy(in, block, sizeof(in)); 207 #else 208 for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) { 209 in[a] = (u32)( 210 (u32)(block[a * 4 + 0]) | 211 (u32)(block[a * 4 + 1]) << 8 | 212 (u32)(block[a * 4 + 2]) << 16 | 213 (u32)(block[a * 4 + 3]) << 24); 214 } 215 #endif 216 217 a = state[0]; 218 b = state[1]; 219 c = state[2]; 220 d = state[3]; 221 222 MD4STEP(F1, a, b, c, d, in[ 0], 3); 223 MD4STEP(F1, d, a, b, c, in[ 1], 7); 224 MD4STEP(F1, c, d, a, b, in[ 2], 11); 225 MD4STEP(F1, b, c, d, a, in[ 3], 19); 226 MD4STEP(F1, a, b, c, d, in[ 4], 3); 227 MD4STEP(F1, d, a, b, c, in[ 5], 7); 228 MD4STEP(F1, c, d, a, b, in[ 6], 11); 229 MD4STEP(F1, b, c, d, a, in[ 7], 19); 230 MD4STEP(F1, a, b, c, d, in[ 8], 3); 231 MD4STEP(F1, d, a, b, c, in[ 9], 7); 232 MD4STEP(F1, c, d, a, b, in[10], 11); 233 MD4STEP(F1, b, c, d, a, in[11], 19); 234 MD4STEP(F1, a, b, c, d, in[12], 3); 235 MD4STEP(F1, d, a, b, c, in[13], 7); 236 MD4STEP(F1, c, d, a, b, in[14], 11); 237 MD4STEP(F1, b, c, d, a, in[15], 19); 238 239 MD4STEP(F2, a, b, c, d, in[ 0] + 0x5a827999, 3); 240 MD4STEP(F2, d, a, b, c, in[ 4] + 0x5a827999, 5); 241 MD4STEP(F2, c, d, a, b, in[ 8] + 0x5a827999, 9); 242 MD4STEP(F2, b, c, d, a, in[12] + 0x5a827999, 13); 243 MD4STEP(F2, a, b, c, d, in[ 1] + 0x5a827999, 3); 244 MD4STEP(F2, d, a, b, c, in[ 5] + 0x5a827999, 5); 245 MD4STEP(F2, c, d, a, b, in[ 9] + 0x5a827999, 9); 246 MD4STEP(F2, b, c, d, a, in[13] + 0x5a827999, 13); 247 MD4STEP(F2, a, b, c, d, in[ 2] + 0x5a827999, 3); 248 MD4STEP(F2, d, a, b, c, in[ 6] + 0x5a827999, 5); 249 MD4STEP(F2, c, d, a, b, in[10] + 0x5a827999, 9); 250 MD4STEP(F2, b, c, d, a, in[14] + 0x5a827999, 13); 251 MD4STEP(F2, a, b, c, d, in[ 3] + 0x5a827999, 3); 252 MD4STEP(F2, d, a, b, c, in[ 7] + 0x5a827999, 5); 253 MD4STEP(F2, c, d, a, b, in[11] + 0x5a827999, 9); 254 MD4STEP(F2, b, c, d, a, in[15] + 0x5a827999, 13); 255 256 MD4STEP(F3, a, b, c, d, in[ 0] + 0x6ed9eba1, 3); 257 MD4STEP(F3, d, a, b, c, in[ 8] + 0x6ed9eba1, 9); 258 MD4STEP(F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11); 259 MD4STEP(F3, b, c, d, a, in[12] + 0x6ed9eba1, 15); 260 MD4STEP(F3, a, b, c, d, in[ 2] + 0x6ed9eba1, 3); 261 MD4STEP(F3, d, a, b, c, in[10] + 0x6ed9eba1, 9); 262 MD4STEP(F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11); 263 MD4STEP(F3, b, c, d, a, in[14] + 0x6ed9eba1, 15); 264 MD4STEP(F3, a, b, c, d, in[ 1] + 0x6ed9eba1, 3); 265 MD4STEP(F3, d, a, b, c, in[ 9] + 0x6ed9eba1, 9); 266 MD4STEP(F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11); 267 MD4STEP(F3, b, c, d, a, in[13] + 0x6ed9eba1, 15); 268 MD4STEP(F3, a, b, c, d, in[ 3] + 0x6ed9eba1, 3); 269 MD4STEP(F3, d, a, b, c, in[11] + 0x6ed9eba1, 9); 270 MD4STEP(F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11); 271 MD4STEP(F3, b, c, d, a, in[15] + 0x6ed9eba1, 15); 272 273 state[0] += a; 274 state[1] += b; 275 state[2] += c; 276 state[3] += d; 277 } 278 /* ===== end - public domain MD4 implementation ===== */ 279