xref: /freebsd/contrib/wpa/src/crypto/sha384-prf.c (revision c5c3ba6b43cac20dc9432eac15758d41cb2b8b1f)
1325151a3SRui Paulo /*
2325151a3SRui Paulo  * SHA384-based KDF (IEEE 802.11ac)
385732ac8SCy Schubert  * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
4325151a3SRui Paulo  *
5325151a3SRui Paulo  * This software may be distributed under the terms of the BSD license.
6325151a3SRui Paulo  * See README for more details.
7325151a3SRui Paulo  */
8325151a3SRui Paulo 
9325151a3SRui Paulo #include "includes.h"
10325151a3SRui Paulo 
11325151a3SRui Paulo #include "common.h"
12325151a3SRui Paulo #include "sha384.h"
13325151a3SRui Paulo #include "crypto.h"
14325151a3SRui Paulo 
15325151a3SRui Paulo 
16325151a3SRui Paulo /**
17325151a3SRui Paulo  * sha384_prf - SHA384-based Key derivation function (IEEE 802.11ac, 11.6.1.7.2)
18325151a3SRui Paulo  * @key: Key for KDF
19325151a3SRui Paulo  * @key_len: Length of the key in bytes
20325151a3SRui Paulo  * @label: A unique label for each purpose of the PRF
21325151a3SRui Paulo  * @data: Extra data to bind into the key
22325151a3SRui Paulo  * @data_len: Length of the data
23325151a3SRui Paulo  * @buf: Buffer for the generated pseudo-random key
24325151a3SRui Paulo  * @buf_len: Number of bytes of key to generate
2585732ac8SCy Schubert  * Returns: 0 on success, -1 on failure
26325151a3SRui Paulo  *
27325151a3SRui Paulo  * This function is used to derive new, cryptographically separate keys from a
28325151a3SRui Paulo  * given key.
29325151a3SRui Paulo  */
sha384_prf(const u8 * key,size_t key_len,const char * label,const u8 * data,size_t data_len,u8 * buf,size_t buf_len)3085732ac8SCy Schubert int sha384_prf(const u8 *key, size_t key_len, const char *label,
31325151a3SRui Paulo 	       const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
32325151a3SRui Paulo {
3385732ac8SCy Schubert 	return sha384_prf_bits(key, key_len, label, data, data_len, buf,
3485732ac8SCy Schubert 			       buf_len * 8);
35325151a3SRui Paulo }
36325151a3SRui Paulo 
37325151a3SRui Paulo 
38325151a3SRui Paulo /**
39325151a3SRui Paulo  * sha384_prf_bits - IEEE Std 802.11ac-2013, 11.6.1.7.2 Key derivation function
40325151a3SRui Paulo  * @key: Key for KDF
41325151a3SRui Paulo  * @key_len: Length of the key in bytes
42325151a3SRui Paulo  * @label: A unique label for each purpose of the PRF
43325151a3SRui Paulo  * @data: Extra data to bind into the key
44325151a3SRui Paulo  * @data_len: Length of the data
45325151a3SRui Paulo  * @buf: Buffer for the generated pseudo-random key
46325151a3SRui Paulo  * @buf_len: Number of bits of key to generate
4785732ac8SCy Schubert  * Returns: 0 on success, -1 on failure
48325151a3SRui Paulo  *
49325151a3SRui Paulo  * This function is used to derive new, cryptographically separate keys from a
50325151a3SRui Paulo  * given key. If the requested buf_len is not divisible by eight, the least
51325151a3SRui Paulo  * significant 1-7 bits of the last octet in the output are not part of the
52325151a3SRui Paulo  * requested output.
53325151a3SRui Paulo  */
sha384_prf_bits(const u8 * key,size_t key_len,const char * label,const u8 * data,size_t data_len,u8 * buf,size_t buf_len_bits)5485732ac8SCy Schubert int sha384_prf_bits(const u8 *key, size_t key_len, const char *label,
55325151a3SRui Paulo 		    const u8 *data, size_t data_len, u8 *buf,
56325151a3SRui Paulo 		    size_t buf_len_bits)
57325151a3SRui Paulo {
58325151a3SRui Paulo 	u16 counter = 1;
59325151a3SRui Paulo 	size_t pos, plen;
60325151a3SRui Paulo 	u8 hash[SHA384_MAC_LEN];
61325151a3SRui Paulo 	const u8 *addr[4];
62325151a3SRui Paulo 	size_t len[4];
63325151a3SRui Paulo 	u8 counter_le[2], length_le[2];
64325151a3SRui Paulo 	size_t buf_len = (buf_len_bits + 7) / 8;
65325151a3SRui Paulo 
66325151a3SRui Paulo 	addr[0] = counter_le;
67325151a3SRui Paulo 	len[0] = 2;
68325151a3SRui Paulo 	addr[1] = (u8 *) label;
69325151a3SRui Paulo 	len[1] = os_strlen(label);
70325151a3SRui Paulo 	addr[2] = data;
71325151a3SRui Paulo 	len[2] = data_len;
72325151a3SRui Paulo 	addr[3] = length_le;
73325151a3SRui Paulo 	len[3] = sizeof(length_le);
74325151a3SRui Paulo 
75325151a3SRui Paulo 	WPA_PUT_LE16(length_le, buf_len_bits);
76325151a3SRui Paulo 	pos = 0;
77325151a3SRui Paulo 	while (pos < buf_len) {
78325151a3SRui Paulo 		plen = buf_len - pos;
79325151a3SRui Paulo 		WPA_PUT_LE16(counter_le, counter);
80325151a3SRui Paulo 		if (plen >= SHA384_MAC_LEN) {
8185732ac8SCy Schubert 			if (hmac_sha384_vector(key, key_len, 4, addr, len,
8285732ac8SCy Schubert 					       &buf[pos]) < 0)
8385732ac8SCy Schubert 				return -1;
84325151a3SRui Paulo 			pos += SHA384_MAC_LEN;
85325151a3SRui Paulo 		} else {
8685732ac8SCy Schubert 			if (hmac_sha384_vector(key, key_len, 4, addr, len,
8785732ac8SCy Schubert 					       hash) < 0)
8885732ac8SCy Schubert 				return -1;
89325151a3SRui Paulo 			os_memcpy(&buf[pos], hash, plen);
90325151a3SRui Paulo 			pos += plen;
91325151a3SRui Paulo 			break;
92325151a3SRui Paulo 		}
93325151a3SRui Paulo 		counter++;
94325151a3SRui Paulo 	}
95325151a3SRui Paulo 
96325151a3SRui Paulo 	/*
97325151a3SRui Paulo 	 * Mask out unused bits in the last octet if it does not use all the
98325151a3SRui Paulo 	 * bits.
99325151a3SRui Paulo 	 */
100325151a3SRui Paulo 	if (buf_len_bits % 8) {
101325151a3SRui Paulo 		u8 mask = 0xff << (8 - buf_len_bits % 8);
102325151a3SRui Paulo 		buf[pos - 1] &= mask;
103325151a3SRui Paulo 	}
104325151a3SRui Paulo 
105*206b73d0SCy Schubert 	forced_memzero(hash, sizeof(hash));
10685732ac8SCy Schubert 
10785732ac8SCy Schubert 	return 0;
108325151a3SRui Paulo }
109