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)
30 * Returns: 0 on success, -1 on failure
32 int hmac_md5_vector(const u8
*key
, size_t key_len
, size_t num_elem
,
33 const u8
*addr
[], const size_t *len
, u8
*mac
)
35 u8 k_pad
[64]; /* padding - key XORd with ipad/opad */
42 * Fixed limit on the number of fragments to avoid having to
43 * allocate memory (which could fail).
48 /* if key is longer than 64 bytes reset it to key = MD5(key) */
50 if (md5_vector(1, &key
, &key_len
, tk
))
56 /* the HMAC_MD5 transform looks like:
58 * MD5(K XOR opad, MD5(K XOR ipad, text))
60 * where K is an n byte key
61 * ipad is the byte 0x36 repeated 64 times
62 * opad is the byte 0x5c repeated 64 times
63 * and text is the data being protected */
65 /* start out by storing key in ipad */
66 os_memset(k_pad
, 0, sizeof(k_pad
));
67 os_memcpy(k_pad
, key
, key_len
);
69 /* XOR key with ipad values */
70 for (i
= 0; i
< 64; i
++)
73 /* perform inner MD5 */
76 for (i
= 0; i
< num_elem
; i
++) {
77 _addr
[i
+ 1] = addr
[i
];
80 if (md5_vector(1 + num_elem
, _addr
, _len
, mac
))
83 os_memset(k_pad
, 0, sizeof(k_pad
));
84 os_memcpy(k_pad
, key
, key_len
);
85 /* XOR key with opad values */
86 for (i
= 0; i
< 64; i
++)
89 /* perform outer MD5 */
93 _len
[1] = MD5_MAC_LEN
;
94 return md5_vector(2, _addr
, _len
, mac
);
99 * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
100 * @key: Key for HMAC operations
101 * @key_len: Length of the key in bytes
102 * @data: Pointers to the data area
103 * @data_len: Length of the data area
104 * @mac: Buffer for the hash (16 bytes)
105 * Returns: 0 on success, -1 on failure
107 int hmac_md5(const u8
*key
, size_t key_len
, const u8
*data
, size_t data_len
,
110 return hmac_md5_vector(key
, key_len
, 1, &data
, &data_len
, mac
);