2 * SHA-256 hash implementation and interface functions
3 * Copyright (c) 2003-2007, 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_sha256_vector - HMAC-SHA256 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 (32 bytes)
31 void hmac_sha256_vector(const u8
*key
, size_t key_len
, size_t num_elem
,
32 const u8
*addr
[], const size_t *len
, u8
*mac
)
34 unsigned char 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 = SHA256(key) */
49 sha256_vector(1, &key
, &key_len
, tk
);
54 /* the HMAC_SHA256 transform looks like:
56 * SHA256(K XOR opad, SHA256(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
);
66 /* XOR key with ipad values */
67 for (i
= 0; i
< 64; i
++)
70 /* perform inner SHA256 */
73 for (i
= 0; i
< num_elem
; i
++) {
74 _addr
[i
+ 1] = addr
[i
];
77 sha256_vector(1 + num_elem
, _addr
, _len
, mac
);
79 os_memset(k_pad
, 0, sizeof(k_pad
));
80 os_memcpy(k_pad
, key
, key_len
);
81 /* XOR key with opad values */
82 for (i
= 0; i
< 64; i
++)
85 /* perform outer SHA256 */
89 _len
[1] = SHA256_MAC_LEN
;
90 sha256_vector(2, _addr
, _len
, mac
);
95 * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104)
96 * @key: Key for HMAC operations
97 * @key_len: Length of the key in bytes
98 * @data: Pointers to the data area
99 * @data_len: Length of the data area
100 * @mac: Buffer for the hash (20 bytes)
102 void hmac_sha256(const u8
*key
, size_t key_len
, const u8
*data
,
103 size_t data_len
, u8
*mac
)
105 hmac_sha256_vector(key
, key_len
, 1, &data
, &data_len
, mac
);
110 * sha256_prf - SHA256-based Pseudo-Random Function (IEEE 802.11r, 8.5.1.5.2)
112 * @key_len: Length of the key in bytes
113 * @label: A unique label for each purpose of the PRF
114 * @data: Extra data to bind into the key
115 * @data_len: Length of the data
116 * @buf: Buffer for the generated pseudo-random key
117 * @buf_len: Number of bytes of key to generate
119 * This function is used to derive new, cryptographically separate keys from a
122 void sha256_prf(const u8
*key
, size_t key_len
, const char *label
,
123 const u8
*data
, size_t data_len
, u8
*buf
, size_t buf_len
)
127 u8 hash
[SHA256_MAC_LEN
];
130 u8 counter_le
[2], length_le
[2];
132 addr
[0] = counter_le
;
134 addr
[1] = (u8
*) label
;
135 len
[1] = os_strlen(label
);
139 len
[3] = sizeof(length_le
);
141 WPA_PUT_LE16(length_le
, buf_len
* 8);
143 while (pos
< buf_len
) {
144 plen
= buf_len
- pos
;
145 WPA_PUT_LE16(counter_le
, counter
);
146 if (plen
>= SHA256_MAC_LEN
) {
147 hmac_sha256_vector(key
, key_len
, 4, addr
, len
,
149 pos
+= SHA256_MAC_LEN
;
151 hmac_sha256_vector(key
, key_len
, 4, addr
, len
, hash
);
152 os_memcpy(&buf
[pos
], hash
, plen
);