blob: 055fcf0aecce7a621d1357ddd51f6fc1b1bb8e82 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************/
2/* freeDiameter author note:
3 * The content from this file comes directly from the hostap project.
4 * It is redistributed under the terms of the BSD license, as allowed
5 * by the original copyright reproduced below.
6 * In addition to this notice, only the #include directives have been modified.
7 */
8#include "rgw_common.h"
9/*********************************************************************************/
10
11
12/*
13 * MD5 hash implementation and interface functions
14 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 *
20 * Alternatively, this software may be distributed under the terms of BSD
21 * license.
22 *
23 * See README and COPYING for more details.
24 */
25
26
27/**
28 * hmac_md5_vector - HMAC-MD5 over data vector (RFC 2104)
29 * @key: Key for HMAC operations
30 * @key_len: Length of the key in bytes
31 * @num_elem: Number of elements in the data vector
32 * @addr: Pointers to the data areas
33 * @len: Lengths of the data blocks
34 * @mac: Buffer for the hash (16 bytes)
35 */
36void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
37 const u8 *addr[], const size_t *len, u8 *mac)
38{
39 u8 k_pad[64]; /* padding - key XORd with ipad/opad */
40 u8 tk[16];
41 const u8 *_addr[6];
42 size_t i, _len[6];
43
44 if (num_elem > 5) {
45 /*
46 * Fixed limit on the number of fragments to avoid having to
47 * allocate memory (which could fail).
48 */
49 return;
50 }
51
52 /* if key is longer than 64 bytes reset it to key = MD5(key) */
53 if (key_len > 64) {
54 md5_vector(1, &key, &key_len, tk);
55 key = tk;
56 key_len = 16;
57 }
58
59 /* the HMAC_MD5 transform looks like:
60 *
61 * MD5(K XOR opad, MD5(K XOR ipad, text))
62 *
63 * where K is an n byte key
64 * ipad is the byte 0x36 repeated 64 times
65 * opad is the byte 0x5c repeated 64 times
66 * and text is the data being protected */
67
68 /* start out by storing key in ipad */
69 os_memset(k_pad, 0, sizeof(k_pad));
70 os_memcpy(k_pad, key, key_len);
71
72 /* XOR key with ipad values */
73 for (i = 0; i < 64; i++)
74 k_pad[i] ^= 0x36;
75
76 /* perform inner MD5 */
77 _addr[0] = k_pad;
78 _len[0] = 64;
79 for (i = 0; i < num_elem; i++) {
80 _addr[i + 1] = addr[i];
81 _len[i + 1] = len[i];
82 }
83 md5_vector(1 + num_elem, _addr, _len, mac);
84
85 os_memset(k_pad, 0, sizeof(k_pad));
86 os_memcpy(k_pad, key, key_len);
87 /* XOR key with opad values */
88 for (i = 0; i < 64; i++)
89 k_pad[i] ^= 0x5c;
90
91 /* perform outer MD5 */
92 _addr[0] = k_pad;
93 _len[0] = 64;
94 _addr[1] = mac;
95 _len[1] = MD5_MAC_LEN;
96 md5_vector(2, _addr, _len, mac);
97}
98
99
100/**
101 * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
102 * @key: Key for HMAC operations
103 * @key_len: Length of the key in bytes
104 * @data: Pointers to the data area
105 * @data_len: Length of the data area
106 * @mac: Buffer for the hash (16 bytes)
107 */
108void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
109 u8 *mac)
110{
111 hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
112}
113
114
115#ifdef INTERNAL_MD5
116
117struct MD5Context {
118 u32 buf[4];
119 u32 bits[2];
120 u8 in[64];
121};
122
123#ifndef CONFIG_CRYPTO_INTERNAL
124static void MD5Init(struct MD5Context *context);
125static void MD5Update(struct MD5Context *context, unsigned char const *buf,
126 unsigned len);
127static void MD5Final(unsigned char digest[16], struct MD5Context *context);
128#endif /* CONFIG_CRYPTO_INTERNAL */
129static void MD5Transform(u32 buf[4], u32 const in[16]);
130
131
132typedef struct MD5Context MD5_CTX;
133
134
135/**
136 * md5_vector - MD5 hash for data vector
137 * @num_elem: Number of elements in the data vector
138 * @addr: Pointers to the data areas
139 * @len: Lengths of the data blocks
140 * @mac: Buffer for the hash
141 */
142void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
143{
144 MD5_CTX ctx;
145 size_t i;
146
147 MD5Init(&ctx);
148 for (i = 0; i < num_elem; i++)
149 MD5Update(&ctx, addr[i], len[i]);
150 MD5Final(mac, &ctx);
151}
152
153
154/* ===== start - public domain MD5 implementation ===== */
155/*
156 * This code implements the MD5 message-digest algorithm.
157 * The algorithm is due to Ron Rivest. This code was
158 * written by Colin Plumb in 1993, no copyright is claimed.
159 * This code is in the public domain; do with it what you wish.
160 *
161 * Equivalent code is available from RSA Data Security, Inc.
162 * This code has been tested against that, and is equivalent,
163 * except that you don't need to include two pages of legalese
164 * with every copy.
165 *
166 * To compute the message digest of a chunk of bytes, declare an
167 * MD5Context structure, pass it to MD5Init, call MD5Update as
168 * needed on buffers full of bytes, and then call MD5Final, which
169 * will fill a supplied 16-byte array with the digest.
170 */
171
172#ifndef WORDS_BIGENDIAN
173#define byteReverse(buf, len) /* Nothing */
174#else
175/*
176 * Note: this code is harmless on little-endian machines.
177 */
178static void byteReverse(unsigned char *buf, unsigned longs)
179{
180 u32 t;
181 do {
182 t = (u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
183 ((unsigned) buf[1] << 8 | buf[0]);
184 *(u32 *) buf = t;
185 buf += 4;
186 } while (--longs);
187}
188#endif
189
190/*
191 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
192 * initialization constants.
193 */
194void MD5Init(struct MD5Context *ctx)
195{
196 ctx->buf[0] = 0x67452301;
197 ctx->buf[1] = 0xefcdab89;
198 ctx->buf[2] = 0x98badcfe;
199 ctx->buf[3] = 0x10325476;
200
201 ctx->bits[0] = 0;
202 ctx->bits[1] = 0;
203}
204
205/*
206 * Update context to reflect the concatenation of another buffer full
207 * of bytes.
208 */
209void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
210{
211 u32 t;
212
213 /* Update bitcount */
214
215 t = ctx->bits[0];
216 if ((ctx->bits[0] = t + ((u32) len << 3)) < t)
217 ctx->bits[1]++; /* Carry from low to high */
218 ctx->bits[1] += len >> 29;
219
220 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
221
222 /* Handle any leading odd-sized chunks */
223
224 if (t) {
225 unsigned char *p = (unsigned char *) ctx->in + t;
226
227 t = 64 - t;
228 if (len < t) {
229 os_memcpy(p, buf, len);
230 return;
231 }
232 os_memcpy(p, buf, t);
233 byteReverse(ctx->in, 16);
234 MD5Transform(ctx->buf, (u32 *) ctx->in);
235 buf += t;
236 len -= t;
237 }
238 /* Process data in 64-byte chunks */
239
240 while (len >= 64) {
241 os_memcpy(ctx->in, buf, 64);
242 byteReverse(ctx->in, 16);
243 MD5Transform(ctx->buf, (u32 *) ctx->in);
244 buf += 64;
245 len -= 64;
246 }
247
248 /* Handle any remaining bytes of data. */
249
250 os_memcpy(ctx->in, buf, len);
251}
252
253/*
254 * Final wrapup - pad to 64-byte boundary with the bit pattern
255 * 1 0* (64-bit count of bits processed, MSB-first)
256 */
257void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
258{
259 unsigned count;
260 unsigned char *p;
261
262 /* Compute number of bytes mod 64 */
263 count = (ctx->bits[0] >> 3) & 0x3F;
264
265 /* Set the first char of padding to 0x80. This is safe since there is
266 always at least one byte free */
267 p = ctx->in + count;
268 *p++ = 0x80;
269
270 /* Bytes of padding needed to make 64 bytes */
271 count = 64 - 1 - count;
272
273 /* Pad out to 56 mod 64 */
274 if (count < 8) {
275 /* Two lots of padding: Pad the first block to 64 bytes */
276 os_memset(p, 0, count);
277 byteReverse(ctx->in, 16);
278 MD5Transform(ctx->buf, (u32 *) ctx->in);
279
280 /* Now fill the next block with 56 bytes */
281 os_memset(ctx->in, 0, 56);
282 } else {
283 /* Pad block to 56 bytes */
284 os_memset(p, 0, count - 8);
285 }
286 byteReverse(ctx->in, 14);
287
288 /* Append length in bits and transform */
289 ((u32 *) ctx->in)[14] = ctx->bits[0];
290 ((u32 *) ctx->in)[15] = ctx->bits[1];
291
292 MD5Transform(ctx->buf, (u32 *) ctx->in);
293 byteReverse((unsigned char *) ctx->buf, 4);
294 os_memcpy(digest, ctx->buf, 16);
295 os_memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
296}
297
298/* The four core functions - F1 is optimized somewhat */
299
300/* #define F1(x, y, z) (x & y | ~x & z) */
301#define F1(x, y, z) (z ^ (x & (y ^ z)))
302#define F2(x, y, z) F1(z, x, y)
303#define F3(x, y, z) (x ^ y ^ z)
304#define F4(x, y, z) (y ^ (x | ~z))
305
306/* This is the central step in the MD5 algorithm. */
307#define MD5STEP(f, w, x, y, z, data, s) \
308 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
309
310/*
311 * The core of the MD5 algorithm, this alters an existing MD5 hash to
312 * reflect the addition of 16 longwords of new data. MD5Update blocks
313 * the data and converts bytes into longwords for this routine.
314 */
315static void MD5Transform(u32 buf[4], u32 const in[16])
316{
317 register u32 a, b, c, d;
318
319 a = buf[0];
320 b = buf[1];
321 c = buf[2];
322 d = buf[3];
323
324 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
325 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
326 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
327 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
328 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
329 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
330 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
331 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
332 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
333 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
334 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
335 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
336 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
337 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
338 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
339 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
340
341 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
342 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
343 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
344 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
345 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
346 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
347 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
348 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
349 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
350 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
351 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
352 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
353 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
354 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
355 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
356 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
357
358 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
359 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
360 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
361 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
362 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
363 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
364 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
365 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
366 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
367 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
368 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
369 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
370 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
371 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
372 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
373 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
374
375 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
376 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
377 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
378 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
379 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
380 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
381 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
382 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
383 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
384 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
385 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
386 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
387 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
388 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
389 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
390 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
391
392 buf[0] += a;
393 buf[1] += b;
394 buf[2] += c;
395 buf[3] += d;
396}
397/* ===== end - public domain MD5 implementation ===== */
398
399#endif /* INTERNAL_MD5 */