]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/crypto/md5.c
Crypto build cleanup: remove INTERNAL_MD5
[thirdparty/hostap.git] / src / crypto / md5.c
1 /*
2 * MD5 hash implementation and interface functions
3 * Copyright (c) 2003-2005, 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 "md5.h"
19 #include "crypto.h"
20
21
22 /**
23 * hmac_md5_vector - HMAC-MD5 over data vector (RFC 2104)
24 * @key: Key for HMAC operations
25 * @key_len: Length of the key in bytes
26 * @num_elem: Number of elements in the data vector
27 * @addr: Pointers to the data areas
28 * @len: Lengths of the data blocks
29 * @mac: Buffer for the hash (16 bytes)
30 */
31 void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
32 const u8 *addr[], const size_t *len, u8 *mac)
33 {
34 u8 k_pad[64]; /* padding - key XORd with ipad/opad */
35 u8 tk[16];
36 const u8 *_addr[6];
37 size_t i, _len[6];
38
39 if (num_elem > 5) {
40 /*
41 * Fixed limit on the number of fragments to avoid having to
42 * allocate memory (which could fail).
43 */
44 return;
45 }
46
47 /* if key is longer than 64 bytes reset it to key = MD5(key) */
48 if (key_len > 64) {
49 md5_vector(1, &key, &key_len, tk);
50 key = tk;
51 key_len = 16;
52 }
53
54 /* the HMAC_MD5 transform looks like:
55 *
56 * MD5(K XOR opad, MD5(K XOR ipad, text))
57 *
58 * where K is an n byte key
59 * ipad is the byte 0x36 repeated 64 times
60 * opad is the byte 0x5c repeated 64 times
61 * and text is the data being protected */
62
63 /* start out by storing key in ipad */
64 os_memset(k_pad, 0, sizeof(k_pad));
65 os_memcpy(k_pad, key, key_len);
66
67 /* XOR key with ipad values */
68 for (i = 0; i < 64; i++)
69 k_pad[i] ^= 0x36;
70
71 /* perform inner MD5 */
72 _addr[0] = k_pad;
73 _len[0] = 64;
74 for (i = 0; i < num_elem; i++) {
75 _addr[i + 1] = addr[i];
76 _len[i + 1] = len[i];
77 }
78 md5_vector(1 + num_elem, _addr, _len, mac);
79
80 os_memset(k_pad, 0, sizeof(k_pad));
81 os_memcpy(k_pad, key, key_len);
82 /* XOR key with opad values */
83 for (i = 0; i < 64; i++)
84 k_pad[i] ^= 0x5c;
85
86 /* perform outer MD5 */
87 _addr[0] = k_pad;
88 _len[0] = 64;
89 _addr[1] = mac;
90 _len[1] = MD5_MAC_LEN;
91 md5_vector(2, _addr, _len, mac);
92 }
93
94
95 /**
96 * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
97 * @key: Key for HMAC operations
98 * @key_len: Length of the key in bytes
99 * @data: Pointers to the data area
100 * @data_len: Length of the data area
101 * @mac: Buffer for the hash (16 bytes)
102 */
103 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
104 u8 *mac)
105 {
106 hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
107 }