2 * RFC 1186/1320 compliant MD4 implementation
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The MD4 algorithm was designed by Ron Rivest in 1990.
38 * http://www.ietf.org/rfc/rfc1186.txt
39 * http://www.ietf.org/rfc/rfc1320.txt
42 #include "tropicssl/config.h"
44 #if defined(TROPICSSL_MD4_C)
46 #include "tropicssl/md4.h"
52 * 32-bit integer manipulation macros (little endian)
55 #define GET_ULONG_LE(n,b,i) \
57 (n) = ( (unsigned long) (b)[(i) ] ) \
58 | ( (unsigned long) (b)[(i) + 1] << 8 ) \
59 | ( (unsigned long) (b)[(i) + 2] << 16 ) \
60 | ( (unsigned long) (b)[(i) + 3] << 24 ); \
65 #define PUT_ULONG_LE(n,b,i) \
67 (b)[(i) ] = (unsigned char) ( (n) ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
77 void md4_starts(md4_context
* ctx
)
82 ctx
->state
[0] = 0x67452301;
83 ctx
->state
[1] = 0xEFCDAB89;
84 ctx
->state
[2] = 0x98BADCFE;
85 ctx
->state
[3] = 0x10325476;
88 static void md4_process(md4_context
* ctx
, const unsigned char data
[64])
90 unsigned long X
[16], A
, B
, C
, D
;
92 GET_ULONG_LE(X
[0], data
, 0);
93 GET_ULONG_LE(X
[1], data
, 4);
94 GET_ULONG_LE(X
[2], data
, 8);
95 GET_ULONG_LE(X
[3], data
, 12);
96 GET_ULONG_LE(X
[4], data
, 16);
97 GET_ULONG_LE(X
[5], data
, 20);
98 GET_ULONG_LE(X
[6], data
, 24);
99 GET_ULONG_LE(X
[7], data
, 28);
100 GET_ULONG_LE(X
[8], data
, 32);
101 GET_ULONG_LE(X
[9], data
, 36);
102 GET_ULONG_LE(X
[10], data
, 40);
103 GET_ULONG_LE(X
[11], data
, 44);
104 GET_ULONG_LE(X
[12], data
, 48);
105 GET_ULONG_LE(X
[13], data
, 52);
106 GET_ULONG_LE(X
[14], data
, 56);
107 GET_ULONG_LE(X
[15], data
, 60);
109 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
116 #define F(x, y, z) ((x & y) | ((~x) & z))
117 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
119 P(A
, B
, C
, D
, X
[0], 3);
120 P(D
, A
, B
, C
, X
[1], 7);
121 P(C
, D
, A
, B
, X
[2], 11);
122 P(B
, C
, D
, A
, X
[3], 19);
123 P(A
, B
, C
, D
, X
[4], 3);
124 P(D
, A
, B
, C
, X
[5], 7);
125 P(C
, D
, A
, B
, X
[6], 11);
126 P(B
, C
, D
, A
, X
[7], 19);
127 P(A
, B
, C
, D
, X
[8], 3);
128 P(D
, A
, B
, C
, X
[9], 7);
129 P(C
, D
, A
, B
, X
[10], 11);
130 P(B
, C
, D
, A
, X
[11], 19);
131 P(A
, B
, C
, D
, X
[12], 3);
132 P(D
, A
, B
, C
, X
[13], 7);
133 P(C
, D
, A
, B
, X
[14], 11);
134 P(B
, C
, D
, A
, X
[15], 19);
139 #define F(x,y,z) ((x & y) | (x & z) | (y & z))
140 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
142 P(A
, B
, C
, D
, X
[0], 3);
143 P(D
, A
, B
, C
, X
[4], 5);
144 P(C
, D
, A
, B
, X
[8], 9);
145 P(B
, C
, D
, A
, X
[12], 13);
146 P(A
, B
, C
, D
, X
[1], 3);
147 P(D
, A
, B
, C
, X
[5], 5);
148 P(C
, D
, A
, B
, X
[9], 9);
149 P(B
, C
, D
, A
, X
[13], 13);
150 P(A
, B
, C
, D
, X
[2], 3);
151 P(D
, A
, B
, C
, X
[6], 5);
152 P(C
, D
, A
, B
, X
[10], 9);
153 P(B
, C
, D
, A
, X
[14], 13);
154 P(A
, B
, C
, D
, X
[3], 3);
155 P(D
, A
, B
, C
, X
[7], 5);
156 P(C
, D
, A
, B
, X
[11], 9);
157 P(B
, C
, D
, A
, X
[15], 13);
162 #define F(x,y,z) (x ^ y ^ z)
163 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
165 P(A
, B
, C
, D
, X
[0], 3);
166 P(D
, A
, B
, C
, X
[8], 9);
167 P(C
, D
, A
, B
, X
[4], 11);
168 P(B
, C
, D
, A
, X
[12], 15);
169 P(A
, B
, C
, D
, X
[2], 3);
170 P(D
, A
, B
, C
, X
[10], 9);
171 P(C
, D
, A
, B
, X
[6], 11);
172 P(B
, C
, D
, A
, X
[14], 15);
173 P(A
, B
, C
, D
, X
[1], 3);
174 P(D
, A
, B
, C
, X
[9], 9);
175 P(C
, D
, A
, B
, X
[5], 11);
176 P(B
, C
, D
, A
, X
[13], 15);
177 P(A
, B
, C
, D
, X
[3], 3);
178 P(D
, A
, B
, C
, X
[11], 9);
179 P(C
, D
, A
, B
, X
[7], 11);
180 P(B
, C
, D
, A
, X
[15], 15);
194 void md4_update(md4_context
* ctx
, const unsigned char *input
, int ilen
)
202 left
= ctx
->total
[0] & 0x3F;
205 ctx
->total
[0] += ilen
;
206 ctx
->total
[0] &= 0xFFFFFFFF;
208 if (ctx
->total
[0] < (unsigned long)ilen
)
211 if (left
&& ilen
>= fill
) {
212 memcpy((void *)(ctx
->buffer
+ left
), (const void *)input
, fill
);
213 md4_process(ctx
, ctx
->buffer
);
220 md4_process(ctx
, input
);
226 memcpy((void *)(ctx
->buffer
+ left
), (const void *)input
, ilen
);
230 static const unsigned char md4_padding
[64] = {
231 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
240 void md4_finish(md4_context
* ctx
, unsigned char output
[16])
242 unsigned long last
, padn
;
243 unsigned long high
, low
;
244 unsigned char msglen
[8];
246 high
= (ctx
->total
[0] >> 29)
247 | (ctx
->total
[1] << 3);
248 low
= (ctx
->total
[0] << 3);
250 PUT_ULONG_LE(low
, msglen
, 0);
251 PUT_ULONG_LE(high
, msglen
, 4);
253 last
= ctx
->total
[0] & 0x3F;
254 padn
= (last
< 56) ? (56 - last
) : (120 - last
);
256 md4_update(ctx
, md4_padding
, padn
);
257 md4_update(ctx
, msglen
, 8);
259 PUT_ULONG_LE(ctx
->state
[0], output
, 0);
260 PUT_ULONG_LE(ctx
->state
[1], output
, 4);
261 PUT_ULONG_LE(ctx
->state
[2], output
, 8);
262 PUT_ULONG_LE(ctx
->state
[3], output
, 12);
266 * output = MD4( input buffer )
268 void md4(const unsigned char *input
, int ilen
, unsigned char output
[16])
273 md4_update(&ctx
, input
, ilen
);
274 md4_finish(&ctx
, output
);
276 memset(&ctx
, 0, sizeof(md4_context
));
280 * output = MD4( file contents )
282 int md4_file(const char *path
, unsigned char output
[16])
287 unsigned char buf
[1024];
289 if ((f
= fopen(path
, "rb")) == NULL
)
294 while ((n
= fread(buf
, 1, sizeof(buf
), f
)) > 0)
295 md4_update(&ctx
, buf
, (int)n
);
297 md4_finish(&ctx
, output
);
299 memset(&ctx
, 0, sizeof(md4_context
));
301 if (ferror(f
) != 0) {
311 * MD4 HMAC context setup
313 void md4_hmac_starts(md4_context
* ctx
, const unsigned char *key
, int keylen
)
316 unsigned char sum
[16];
319 md4(key
, keylen
, sum
);
324 memset(ctx
->ipad
, 0x36, 64);
325 memset(ctx
->opad
, 0x5C, 64);
327 for (i
= 0; i
< keylen
; i
++) {
328 ctx
->ipad
[i
] = (unsigned char)(ctx
->ipad
[i
] ^ key
[i
]);
329 ctx
->opad
[i
] = (unsigned char)(ctx
->opad
[i
] ^ key
[i
]);
333 md4_update(ctx
, ctx
->ipad
, 64);
335 memset(sum
, 0, sizeof(sum
));
339 * MD4 HMAC process buffer
341 void md4_hmac_update(md4_context
* ctx
, const unsigned char *input
, int ilen
)
343 md4_update(ctx
, input
, ilen
);
347 * MD4 HMAC final digest
349 void md4_hmac_finish(md4_context
* ctx
, unsigned char output
[16])
351 unsigned char tmpbuf
[16];
353 md4_finish(ctx
, tmpbuf
);
355 md4_update(ctx
, ctx
->opad
, 64);
356 md4_update(ctx
, tmpbuf
, 16);
357 md4_finish(ctx
, output
);
359 memset(tmpbuf
, 0, sizeof(tmpbuf
));
363 * output = HMAC-MD4( hmac key, input buffer )
365 void md4_hmac(const unsigned char *key
, int keylen
,
366 const unsigned char *input
, int ilen
,
367 unsigned char output
[16])
371 md4_hmac_starts(&ctx
, key
, keylen
);
372 md4_hmac_update(&ctx
, input
, ilen
);
373 md4_hmac_finish(&ctx
, output
);
375 memset(&ctx
, 0, sizeof(md4_context
));
378 #if defined(TROPICSSL_SELF_TEST)
381 * RFC 1320 test vectors
383 static const char md4_test_str
[7][81] = {
388 {"abcdefghijklmnopqrstuvwxyz"},
389 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"},
391 "12345678901234567890123456789012345678901234567890123456789012"
392 "345678901234567890"}
395 static const unsigned char md4_test_sum
[7][16] = {
397 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
398 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0},
400 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
401 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24},
403 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
404 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D},
406 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
407 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B},
409 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
410 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9},
412 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
413 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4},
415 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
416 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36}
422 int md4_self_test(int verbose
)
425 unsigned char md4sum
[16];
427 for (i
= 0; i
< 7; i
++) {
429 printf(" MD4 test #%d: ", i
+ 1);
431 md4((const unsigned char *)md4_test_str
[i
],
432 strlen(md4_test_str
[i
]), md4sum
);
434 if (memcmp(md4sum
, md4_test_sum
[i
], 16) != 0) {