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