1341618Scy/*
2341618Scy * SHA512-based KDF (IEEE 802.11ac)
3341618Scy * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
4341618Scy *
5341618Scy * This software may be distributed under the terms of the BSD license.
6341618Scy * See README for more details.
7341618Scy */
8341618Scy
9341618Scy#include "includes.h"
10341618Scy
11341618Scy#include "common.h"
12341618Scy#include "sha512.h"
13341618Scy#include "crypto.h"
14341618Scy
15341618Scy
16341618Scy/**
17341618Scy * sha512_prf - SHA512-based Key derivation function (IEEE 802.11ac, 11.6.1.7.2)
18341618Scy * @key: Key for KDF
19341618Scy * @key_len: Length of the key in bytes
20341618Scy * @label: A unique label for each purpose of the PRF
21341618Scy * @data: Extra data to bind into the key
22341618Scy * @data_len: Length of the data
23341618Scy * @buf: Buffer for the generated pseudo-random key
24341618Scy * @buf_len: Number of bytes of key to generate
25341618Scy * Returns: 0 on success, -1 on failure
26341618Scy *
27341618Scy * This function is used to derive new, cryptographically separate keys from a
28341618Scy * given key.
29341618Scy */
30341618Scyint sha512_prf(const u8 *key, size_t key_len, const char *label,
31341618Scy	       const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
32341618Scy{
33341618Scy	return sha512_prf_bits(key, key_len, label, data, data_len, buf,
34341618Scy			       buf_len * 8);
35341618Scy}
36341618Scy
37341618Scy
38341618Scy/**
39341618Scy * sha512_prf_bits - IEEE Std 802.11ac-2013, 11.6.1.7.2 Key derivation function
40341618Scy * @key: Key for KDF
41341618Scy * @key_len: Length of the key in bytes
42341618Scy * @label: A unique label for each purpose of the PRF
43341618Scy * @data: Extra data to bind into the key
44341618Scy * @data_len: Length of the data
45341618Scy * @buf: Buffer for the generated pseudo-random key
46341618Scy * @buf_len: Number of bits of key to generate
47341618Scy * Returns: 0 on success, -1 on failure
48341618Scy *
49341618Scy * This function is used to derive new, cryptographically separate keys from a
50341618Scy * given key. If the requested buf_len is not divisible by eight, the least
51341618Scy * significant 1-7 bits of the last octet in the output are not part of the
52341618Scy * requested output.
53341618Scy */
54341618Scyint sha512_prf_bits(const u8 *key, size_t key_len, const char *label,
55341618Scy		    const u8 *data, size_t data_len, u8 *buf,
56341618Scy		    size_t buf_len_bits)
57341618Scy{
58341618Scy	u16 counter = 1;
59341618Scy	size_t pos, plen;
60341618Scy	u8 hash[SHA512_MAC_LEN];
61341618Scy	const u8 *addr[4];
62341618Scy	size_t len[4];
63341618Scy	u8 counter_le[2], length_le[2];
64341618Scy	size_t buf_len = (buf_len_bits + 7) / 8;
65341618Scy
66341618Scy	addr[0] = counter_le;
67341618Scy	len[0] = 2;
68341618Scy	addr[1] = (u8 *) label;
69341618Scy	len[1] = os_strlen(label);
70341618Scy	addr[2] = data;
71341618Scy	len[2] = data_len;
72341618Scy	addr[3] = length_le;
73341618Scy	len[3] = sizeof(length_le);
74341618Scy
75341618Scy	WPA_PUT_LE16(length_le, buf_len_bits);
76341618Scy	pos = 0;
77341618Scy	while (pos < buf_len) {
78341618Scy		plen = buf_len - pos;
79341618Scy		WPA_PUT_LE16(counter_le, counter);
80341618Scy		if (plen >= SHA512_MAC_LEN) {
81341618Scy			if (hmac_sha512_vector(key, key_len, 4, addr, len,
82341618Scy					       &buf[pos]) < 0)
83341618Scy				return -1;
84341618Scy			pos += SHA512_MAC_LEN;
85341618Scy		} else {
86341618Scy			if (hmac_sha512_vector(key, key_len, 4, addr, len,
87341618Scy					       hash) < 0)
88341618Scy				return -1;
89341618Scy			os_memcpy(&buf[pos], hash, plen);
90341618Scy			pos += plen;
91341618Scy			break;
92341618Scy		}
93341618Scy		counter++;
94341618Scy	}
95341618Scy
96341618Scy	/*
97341618Scy	 * Mask out unused bits in the last octet if it does not use all the
98341618Scy	 * bits.
99341618Scy	 */
100341618Scy	if (buf_len_bits % 8) {
101341618Scy		u8 mask = 0xff << (8 - buf_len_bits % 8);
102341618Scy		buf[pos - 1] &= mask;
103341618Scy	}
104341618Scy
105351611Scy	forced_memzero(hash, sizeof(hash));
106341618Scy
107341618Scy	return 0;
108341618Scy}
109