2 * MD5 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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)
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)
34 u8 k_pad[64]; /* padding - key XORd with ipad/opad */
41 * Fixed limit on the number of fragments to avoid having to
42 * allocate memory (which could fail).
47 /* if key is longer than 64 bytes reset it to key = MD5(key) */
49 md5_vector(1, &key, &key_len, tk);
54 /* the HMAC_MD5 transform looks like:
56 * MD5(K XOR opad, MD5(K XOR ipad, text))
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 */
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);
67 /* XOR key with ipad values */
68 for (i = 0; i < 64; i++)
71 /* perform inner MD5 */
74 for (i = 0; i < num_elem; i++) {
75 _addr[i + 1] = addr[i];
78 md5_vector(1 + num_elem, _addr, _len, mac);
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++)
86 /* perform outer MD5 */
90 _len[1] = MD5_MAC_LEN;
91 md5_vector(2, _addr, _len, mac);
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)
103 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
106 hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);