185732ac8SCy Schubert /*
285732ac8SCy Schubert * SHA512-based KDF (IEEE 802.11ac)
385732ac8SCy Schubert * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
485732ac8SCy Schubert *
585732ac8SCy Schubert * This software may be distributed under the terms of the BSD license.
685732ac8SCy Schubert * See README for more details.
785732ac8SCy Schubert */
885732ac8SCy Schubert
985732ac8SCy Schubert #include "includes.h"
1085732ac8SCy Schubert
1185732ac8SCy Schubert #include "common.h"
1285732ac8SCy Schubert #include "sha512.h"
1385732ac8SCy Schubert #include "crypto.h"
1485732ac8SCy Schubert
1585732ac8SCy Schubert
1685732ac8SCy Schubert /**
1785732ac8SCy Schubert * sha512_prf - SHA512-based Key derivation function (IEEE 802.11ac, 11.6.1.7.2)
1885732ac8SCy Schubert * @key: Key for KDF
1985732ac8SCy Schubert * @key_len: Length of the key in bytes
2085732ac8SCy Schubert * @label: A unique label for each purpose of the PRF
2185732ac8SCy Schubert * @data: Extra data to bind into the key
2285732ac8SCy Schubert * @data_len: Length of the data
2385732ac8SCy Schubert * @buf: Buffer for the generated pseudo-random key
2485732ac8SCy Schubert * @buf_len: Number of bytes of key to generate
2585732ac8SCy Schubert * Returns: 0 on success, -1 on failure
2685732ac8SCy Schubert *
2785732ac8SCy Schubert * This function is used to derive new, cryptographically separate keys from a
2885732ac8SCy Schubert * given key.
2985732ac8SCy Schubert */
sha512_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 sha512_prf(const u8 *key, size_t key_len, const char *label,
3185732ac8SCy Schubert const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
3285732ac8SCy Schubert {
3385732ac8SCy Schubert return sha512_prf_bits(key, key_len, label, data, data_len, buf,
3485732ac8SCy Schubert buf_len * 8);
3585732ac8SCy Schubert }
3685732ac8SCy Schubert
3785732ac8SCy Schubert
3885732ac8SCy Schubert /**
3985732ac8SCy Schubert * sha512_prf_bits - IEEE Std 802.11ac-2013, 11.6.1.7.2 Key derivation function
4085732ac8SCy Schubert * @key: Key for KDF
4185732ac8SCy Schubert * @key_len: Length of the key in bytes
4285732ac8SCy Schubert * @label: A unique label for each purpose of the PRF
4385732ac8SCy Schubert * @data: Extra data to bind into the key
4485732ac8SCy Schubert * @data_len: Length of the data
4585732ac8SCy Schubert * @buf: Buffer for the generated pseudo-random key
4685732ac8SCy Schubert * @buf_len: Number of bits of key to generate
4785732ac8SCy Schubert * Returns: 0 on success, -1 on failure
4885732ac8SCy Schubert *
4985732ac8SCy Schubert * This function is used to derive new, cryptographically separate keys from a
5085732ac8SCy Schubert * given key. If the requested buf_len is not divisible by eight, the least
5185732ac8SCy Schubert * significant 1-7 bits of the last octet in the output are not part of the
5285732ac8SCy Schubert * requested output.
5385732ac8SCy Schubert */
sha512_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 sha512_prf_bits(const u8 *key, size_t key_len, const char *label,
5585732ac8SCy Schubert const u8 *data, size_t data_len, u8 *buf,
5685732ac8SCy Schubert size_t buf_len_bits)
5785732ac8SCy Schubert {
5885732ac8SCy Schubert u16 counter = 1;
5985732ac8SCy Schubert size_t pos, plen;
6085732ac8SCy Schubert u8 hash[SHA512_MAC_LEN];
6185732ac8SCy Schubert const u8 *addr[4];
6285732ac8SCy Schubert size_t len[4];
6385732ac8SCy Schubert u8 counter_le[2], length_le[2];
6485732ac8SCy Schubert size_t buf_len = (buf_len_bits + 7) / 8;
6585732ac8SCy Schubert
6685732ac8SCy Schubert addr[0] = counter_le;
6785732ac8SCy Schubert len[0] = 2;
6885732ac8SCy Schubert addr[1] = (u8 *) label;
6985732ac8SCy Schubert len[1] = os_strlen(label);
7085732ac8SCy Schubert addr[2] = data;
7185732ac8SCy Schubert len[2] = data_len;
7285732ac8SCy Schubert addr[3] = length_le;
7385732ac8SCy Schubert len[3] = sizeof(length_le);
7485732ac8SCy Schubert
7585732ac8SCy Schubert WPA_PUT_LE16(length_le, buf_len_bits);
7685732ac8SCy Schubert pos = 0;
7785732ac8SCy Schubert while (pos < buf_len) {
7885732ac8SCy Schubert plen = buf_len - pos;
7985732ac8SCy Schubert WPA_PUT_LE16(counter_le, counter);
8085732ac8SCy Schubert if (plen >= SHA512_MAC_LEN) {
8185732ac8SCy Schubert if (hmac_sha512_vector(key, key_len, 4, addr, len,
8285732ac8SCy Schubert &buf[pos]) < 0)
8385732ac8SCy Schubert return -1;
8485732ac8SCy Schubert pos += SHA512_MAC_LEN;
8585732ac8SCy Schubert } else {
8685732ac8SCy Schubert if (hmac_sha512_vector(key, key_len, 4, addr, len,
8785732ac8SCy Schubert hash) < 0)
8885732ac8SCy Schubert return -1;
8985732ac8SCy Schubert os_memcpy(&buf[pos], hash, plen);
9085732ac8SCy Schubert pos += plen;
9185732ac8SCy Schubert break;
9285732ac8SCy Schubert }
9385732ac8SCy Schubert counter++;
9485732ac8SCy Schubert }
9585732ac8SCy Schubert
9685732ac8SCy Schubert /*
9785732ac8SCy Schubert * Mask out unused bits in the last octet if it does not use all the
9885732ac8SCy Schubert * bits.
9985732ac8SCy Schubert */
10085732ac8SCy Schubert if (buf_len_bits % 8) {
10185732ac8SCy Schubert u8 mask = 0xff << (8 - buf_len_bits % 8);
10285732ac8SCy Schubert buf[pos - 1] &= mask;
10385732ac8SCy Schubert }
10485732ac8SCy Schubert
105*206b73d0SCy Schubert forced_memzero(hash, sizeof(hash));
10685732ac8SCy Schubert
10785732ac8SCy Schubert return 0;
10885732ac8SCy Schubert }
109