2 * SHA-256 hash implementation and interface functions
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 * hmac_sha256_vector - HMAC-SHA256 over data vector (RFC 2104)
18 * @key: Key for HMAC operations
19 * @key_len: Length of the key in bytes
20 * @num_elem: Number of elements in the data vector
21 * @addr: Pointers to the data areas
22 * @len: Lengths of the data blocks
23 * @mac: Buffer for the hash (32 bytes)
25 void hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
26 const u8 *addr[], const size_t *len, u8 *mac)
28 unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
35 * Fixed limit on the number of fragments to avoid having to
36 * allocate memory (which could fail).
41 /* if key is longer than 64 bytes reset it to key = SHA256(key) */
43 sha256_vector(1, &key, &key_len, tk);
48 /* the HMAC_SHA256 transform looks like:
50 * SHA256(K XOR opad, SHA256(K XOR ipad, text))
52 * where K is an n byte key
53 * ipad is the byte 0x36 repeated 64 times
54 * opad is the byte 0x5c repeated 64 times
55 * and text is the data being protected */
57 /* start out by storing key in ipad */
58 os_memset(k_pad, 0, sizeof(k_pad));
59 os_memcpy(k_pad, key, key_len);
60 /* XOR key with ipad values */
61 for (i = 0; i < 64; i++)
64 /* perform inner SHA256 */
67 for (i = 0; i < num_elem; i++) {
68 _addr[i + 1] = addr[i];
71 sha256_vector(1 + num_elem, _addr, _len, mac);
73 os_memset(k_pad, 0, sizeof(k_pad));
74 os_memcpy(k_pad, key, key_len);
75 /* XOR key with opad values */
76 for (i = 0; i < 64; i++)
79 /* perform outer SHA256 */
83 _len[1] = SHA256_MAC_LEN;
84 sha256_vector(2, _addr, _len, mac);
89 * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104)
90 * @key: Key for HMAC operations
91 * @key_len: Length of the key in bytes
92 * @data: Pointers to the data area
93 * @data_len: Length of the data area
94 * @mac: Buffer for the hash (20 bytes)
96 void hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
97 size_t data_len, u8 *mac)
99 hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);