Merge branch 'vendor/OPENSSL'
[dragonfly.git] / contrib / hostapd / src / crypto / md4-internal.c
1 /*
2  * MD4 hash implementation
3  * Copyright (c) 2006, 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 "crypto.h"
13
14 #define MD4_BLOCK_LENGTH                64
15 #define MD4_DIGEST_LENGTH               16
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 */
21 } MD4_CTX;
22
23
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);
27
28
29 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
30 {
31         MD4_CTX ctx;
32         size_t i;
33
34         MD4Init(&ctx);
35         for (i = 0; i < num_elem; i++)
36                 MD4Update(&ctx, addr[i], len[i]);
37         MD4Final(mac, &ctx);
38         return 0;
39 }
40
41
42 /* ===== start - public domain MD4 implementation ===== */
43 /*      $OpenBSD: md4.c,v 1.7 2005/08/08 08:05:35 espie Exp $   */
44
45 /*
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.
51  *
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
55  * with every copy.
56  *
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.
61  */
62
63 #define MD4_DIGEST_STRING_LENGTH        (MD4_DIGEST_LENGTH * 2 + 1)
64
65
66 static void
67 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH]);
68
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)
78
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)
84
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
89 };
90
91 /*
92  * Start MD4 accumulation.
93  * Set bit count to 0 and buffer to mysterious initialization constants.
94  */
95 static void MD4Init(MD4_CTX *ctx)
96 {
97         ctx->count = 0;
98         ctx->state[0] = 0x67452301;
99         ctx->state[1] = 0xefcdab89;
100         ctx->state[2] = 0x98badcfe;
101         ctx->state[3] = 0x10325476;
102 }
103
104 /*
105  * Update context to reflect the concatenation of another buffer full
106  * of bytes.
107  */
108 static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len)
109 {
110         size_t have, need;
111
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;
115
116         /* Update bitcount */
117         ctx->count += (u64)len << 3;
118
119         if (len >= need) {
120                 if (have != 0) {
121                         os_memcpy(ctx->buffer + have, input, need);
122                         MD4Transform(ctx->state, ctx->buffer);
123                         input += need;
124                         len -= need;
125                         have = 0;
126                 }
127
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;
133                 }
134         }
135
136         /* Handle any remaining bytes of data. */
137         if (len != 0)
138                 os_memcpy(ctx->buffer + have, input, len);
139 }
140
141 /*
142  * Pad pad to 64-byte boundary with the bit pattern
143  * 1 0* (64-bit count of bits processed, MSB-first)
144  */
145 static void MD4Pad(MD4_CTX *ctx)
146 {
147         u8 count[8];
148         size_t padlen;
149
150         /* Convert count to 8 bytes in little endian order. */
151         PUT_64BIT_LE(count, ctx->count);
152
153         /* Pad out to 56 mod 64. */
154         padlen = MD4_BLOCK_LENGTH -
155             ((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
156         if (padlen < 1 + 8)
157                 padlen += MD4_BLOCK_LENGTH;
158         MD4Update(ctx, PADDING, padlen - 8);            /* padlen - 8 <= 64 */
159         MD4Update(ctx, count, 8);
160 }
161
162 /*
163  * Final wrapup--call MD4Pad, fill in digest and zero out ctx.
164  */
165 static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
166 {
167         int i;
168
169         MD4Pad(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));
174         }
175 }
176
177
178 /* The three core functions - F1 is optimized somewhat */
179
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)
184
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) )
188
189 /*
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.
193  */
194 static void
195 MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH])
196 {
197         u32 a, b, c, d, in[MD4_BLOCK_LENGTH / 4];
198
199 #if BYTE_ORDER == LITTLE_ENDIAN
200         os_memcpy(in, block, sizeof(in));
201 #else
202         for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) {
203                 in[a] = (u32)(
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);
208         }
209 #endif
210
211         a = state[0];
212         b = state[1];
213         c = state[2];
214         d = state[3];
215
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);
232
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);
249
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);
266
267         state[0] += a;
268         state[1] += b;
269         state[2] += c;
270         state[3] += d;
271 }
272 /* ===== end - public domain MD4 implementation ===== */