2 * MD4 hash implementation
3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #define MD4_BLOCK_LENGTH 64
15 #define MD4_DIGEST_LENGTH 16
17 typedef struct MD4Context {
18 u32 state[4]; /* state */
19 u64 count; /* number of bits, mod 2^64 */
20 u8 buffer[MD4_BLOCK_LENGTH]; /* input buffer */
24 static void MD4Init(MD4_CTX *ctx);
25 static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len);
26 static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx);
29 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
35 for (i = 0; i < num_elem; i++)
36 MD4Update(&ctx, addr[i], len[i]);
42 /* ===== start - public domain MD4 implementation ===== */
43 /* $OpenBSD: md4.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */
46 * This code implements the MD4 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 * Todd C. Miller modified the MD5 code to do MD4 based on RFC 1186.
52 * Equivalent code is available from RSA Data Security, Inc.
53 * This code has been tested against that, and is equivalent,
54 * except that you don't need to include two pages of legalese
57 * To compute the message digest of a chunk of bytes, declare an
58 * MD4Context structure, pass it to MD4Init, call MD4Update as
59 * needed on buffers full of bytes, and then call MD4Final, which
60 * will fill a supplied 16-byte array with the digest.
63 #define MD4_DIGEST_STRING_LENGTH (MD4_DIGEST_LENGTH * 2 + 1)
67 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH]);
69 #define PUT_64BIT_LE(cp, value) do { \
70 (cp)[7] = (value) >> 56; \
71 (cp)[6] = (value) >> 48; \
72 (cp)[5] = (value) >> 40; \
73 (cp)[4] = (value) >> 32; \
74 (cp)[3] = (value) >> 24; \
75 (cp)[2] = (value) >> 16; \
76 (cp)[1] = (value) >> 8; \
77 (cp)[0] = (value); } while (0)
79 #define PUT_32BIT_LE(cp, value) do { \
80 (cp)[3] = (value) >> 24; \
81 (cp)[2] = (value) >> 16; \
82 (cp)[1] = (value) >> 8; \
83 (cp)[0] = (value); } while (0)
85 static u8 PADDING[MD4_BLOCK_LENGTH] = {
86 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
92 * Start MD4 accumulation.
93 * Set bit count to 0 and buffer to mysterious initialization constants.
95 static void MD4Init(MD4_CTX *ctx)
98 ctx->state[0] = 0x67452301;
99 ctx->state[1] = 0xefcdab89;
100 ctx->state[2] = 0x98badcfe;
101 ctx->state[3] = 0x10325476;
105 * Update context to reflect the concatenation of another buffer full
108 static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len)
112 /* Check how many bytes we already have and how many more we need. */
113 have = (size_t)((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
114 need = MD4_BLOCK_LENGTH - have;
116 /* Update bitcount */
117 ctx->count += (u64)len << 3;
121 os_memcpy(ctx->buffer + have, input, need);
122 MD4Transform(ctx->state, ctx->buffer);
128 /* Process data in MD4_BLOCK_LENGTH-byte chunks. */
129 while (len >= MD4_BLOCK_LENGTH) {
130 MD4Transform(ctx->state, input);
131 input += MD4_BLOCK_LENGTH;
132 len -= MD4_BLOCK_LENGTH;
136 /* Handle any remaining bytes of data. */
138 os_memcpy(ctx->buffer + have, input, len);
142 * Pad pad to 64-byte boundary with the bit pattern
143 * 1 0* (64-bit count of bits processed, MSB-first)
145 static void MD4Pad(MD4_CTX *ctx)
150 /* Convert count to 8 bytes in little endian order. */
151 PUT_64BIT_LE(count, ctx->count);
153 /* Pad out to 56 mod 64. */
154 padlen = MD4_BLOCK_LENGTH -
155 ((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
157 padlen += MD4_BLOCK_LENGTH;
158 MD4Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
159 MD4Update(ctx, count, 8);
163 * Final wrapup--call MD4Pad, fill in digest and zero out ctx.
165 static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
170 if (digest != NULL) {
171 for (i = 0; i < 4; i++)
172 PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
173 os_memset(ctx, 0, sizeof(*ctx));
178 /* The three core functions - F1 is optimized somewhat */
180 /* #define F1(x, y, z) (x & y | ~x & z) */
181 #define F1(x, y, z) (z ^ (x & (y ^ z)))
182 #define F2(x, y, z) ((x & y) | (x & z) | (y & z))
183 #define F3(x, y, z) (x ^ y ^ z)
185 /* This is the central step in the MD4 algorithm. */
186 #define MD4STEP(f, w, x, y, z, data, s) \
187 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s) )
190 * The core of the MD4 algorithm, this alters an existing MD4 hash to
191 * reflect the addition of 16 longwords of new data. MD4Update blocks
192 * the data and converts bytes into longwords for this routine.
195 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH])
197 u32 a, b, c, d, in[MD4_BLOCK_LENGTH / 4];
199 #if BYTE_ORDER == LITTLE_ENDIAN
200 os_memcpy(in, block, sizeof(in));
202 for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) {
204 (u32)(block[a * 4 + 0]) |
205 (u32)(block[a * 4 + 1]) << 8 |
206 (u32)(block[a * 4 + 2]) << 16 |
207 (u32)(block[a * 4 + 3]) << 24);
216 MD4STEP(F1, a, b, c, d, in[ 0], 3);
217 MD4STEP(F1, d, a, b, c, in[ 1], 7);
218 MD4STEP(F1, c, d, a, b, in[ 2], 11);
219 MD4STEP(F1, b, c, d, a, in[ 3], 19);
220 MD4STEP(F1, a, b, c, d, in[ 4], 3);
221 MD4STEP(F1, d, a, b, c, in[ 5], 7);
222 MD4STEP(F1, c, d, a, b, in[ 6], 11);
223 MD4STEP(F1, b, c, d, a, in[ 7], 19);
224 MD4STEP(F1, a, b, c, d, in[ 8], 3);
225 MD4STEP(F1, d, a, b, c, in[ 9], 7);
226 MD4STEP(F1, c, d, a, b, in[10], 11);
227 MD4STEP(F1, b, c, d, a, in[11], 19);
228 MD4STEP(F1, a, b, c, d, in[12], 3);
229 MD4STEP(F1, d, a, b, c, in[13], 7);
230 MD4STEP(F1, c, d, a, b, in[14], 11);
231 MD4STEP(F1, b, c, d, a, in[15], 19);
233 MD4STEP(F2, a, b, c, d, in[ 0] + 0x5a827999, 3);
234 MD4STEP(F2, d, a, b, c, in[ 4] + 0x5a827999, 5);
235 MD4STEP(F2, c, d, a, b, in[ 8] + 0x5a827999, 9);
236 MD4STEP(F2, b, c, d, a, in[12] + 0x5a827999, 13);
237 MD4STEP(F2, a, b, c, d, in[ 1] + 0x5a827999, 3);
238 MD4STEP(F2, d, a, b, c, in[ 5] + 0x5a827999, 5);
239 MD4STEP(F2, c, d, a, b, in[ 9] + 0x5a827999, 9);
240 MD4STEP(F2, b, c, d, a, in[13] + 0x5a827999, 13);
241 MD4STEP(F2, a, b, c, d, in[ 2] + 0x5a827999, 3);
242 MD4STEP(F2, d, a, b, c, in[ 6] + 0x5a827999, 5);
243 MD4STEP(F2, c, d, a, b, in[10] + 0x5a827999, 9);
244 MD4STEP(F2, b, c, d, a, in[14] + 0x5a827999, 13);
245 MD4STEP(F2, a, b, c, d, in[ 3] + 0x5a827999, 3);
246 MD4STEP(F2, d, a, b, c, in[ 7] + 0x5a827999, 5);
247 MD4STEP(F2, c, d, a, b, in[11] + 0x5a827999, 9);
248 MD4STEP(F2, b, c, d, a, in[15] + 0x5a827999, 13);
250 MD4STEP(F3, a, b, c, d, in[ 0] + 0x6ed9eba1, 3);
251 MD4STEP(F3, d, a, b, c, in[ 8] + 0x6ed9eba1, 9);
252 MD4STEP(F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11);
253 MD4STEP(F3, b, c, d, a, in[12] + 0x6ed9eba1, 15);
254 MD4STEP(F3, a, b, c, d, in[ 2] + 0x6ed9eba1, 3);
255 MD4STEP(F3, d, a, b, c, in[10] + 0x6ed9eba1, 9);
256 MD4STEP(F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11);
257 MD4STEP(F3, b, c, d, a, in[14] + 0x6ed9eba1, 15);
258 MD4STEP(F3, a, b, c, d, in[ 1] + 0x6ed9eba1, 3);
259 MD4STEP(F3, d, a, b, c, in[ 9] + 0x6ed9eba1, 9);
260 MD4STEP(F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11);
261 MD4STEP(F3, b, c, d, a, in[13] + 0x6ed9eba1, 15);
262 MD4STEP(F3, a, b, c, d, in[ 3] + 0x6ed9eba1, 3);
263 MD4STEP(F3, d, a, b, c, in[11] + 0x6ed9eba1, 9);
264 MD4STEP(F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11);
265 MD4STEP(F3, b, c, d, a, in[15] + 0x6ed9eba1, 15);
272 /* ===== end - public domain MD4 implementation ===== */