1e5b75505Sopenharmony_ci/* 2e5b75505Sopenharmony_ci * SHA256-based PRF (IEEE 802.11r) 3e5b75505Sopenharmony_ci * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi> 4e5b75505Sopenharmony_ci * 5e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license. 6e5b75505Sopenharmony_ci * See README for more details. 7e5b75505Sopenharmony_ci */ 8e5b75505Sopenharmony_ci 9e5b75505Sopenharmony_ci#include "includes.h" 10e5b75505Sopenharmony_ci 11e5b75505Sopenharmony_ci#include "common.h" 12e5b75505Sopenharmony_ci#include "sha256.h" 13e5b75505Sopenharmony_ci#include "crypto.h" 14e5b75505Sopenharmony_ci 15e5b75505Sopenharmony_ci 16e5b75505Sopenharmony_ci/** 17e5b75505Sopenharmony_ci * sha256_prf - SHA256-based Pseudo-Random Function (IEEE 802.11r, 8.5.1.5.2) 18e5b75505Sopenharmony_ci * @key: Key for PRF 19e5b75505Sopenharmony_ci * @key_len: Length of the key in bytes 20e5b75505Sopenharmony_ci * @label: A unique label for each purpose of the PRF 21e5b75505Sopenharmony_ci * @data: Extra data to bind into the key 22e5b75505Sopenharmony_ci * @data_len: Length of the data 23e5b75505Sopenharmony_ci * @buf: Buffer for the generated pseudo-random key 24e5b75505Sopenharmony_ci * @buf_len: Number of bytes of key to generate 25e5b75505Sopenharmony_ci * Returns: 0 on success, -1 on failure 26e5b75505Sopenharmony_ci * 27e5b75505Sopenharmony_ci * This function is used to derive new, cryptographically separate keys from a 28e5b75505Sopenharmony_ci * given key. 29e5b75505Sopenharmony_ci */ 30e5b75505Sopenharmony_ciint sha256_prf(const u8 *key, size_t key_len, const char *label, 31e5b75505Sopenharmony_ci const u8 *data, size_t data_len, u8 *buf, size_t buf_len) 32e5b75505Sopenharmony_ci{ 33e5b75505Sopenharmony_ci return sha256_prf_bits(key, key_len, label, data, data_len, buf, 34e5b75505Sopenharmony_ci buf_len * 8); 35e5b75505Sopenharmony_ci} 36e5b75505Sopenharmony_ci 37e5b75505Sopenharmony_ci 38e5b75505Sopenharmony_ci/** 39e5b75505Sopenharmony_ci * sha256_prf_bits - IEEE Std 802.11-2012, 11.6.1.7.2 Key derivation function 40e5b75505Sopenharmony_ci * @key: Key for KDF 41e5b75505Sopenharmony_ci * @key_len: Length of the key in bytes 42e5b75505Sopenharmony_ci * @label: A unique label for each purpose of the PRF 43e5b75505Sopenharmony_ci * @data: Extra data to bind into the key 44e5b75505Sopenharmony_ci * @data_len: Length of the data 45e5b75505Sopenharmony_ci * @buf: Buffer for the generated pseudo-random key 46e5b75505Sopenharmony_ci * @buf_len: Number of bits of key to generate 47e5b75505Sopenharmony_ci * Returns: 0 on success, -1 on failure 48e5b75505Sopenharmony_ci * 49e5b75505Sopenharmony_ci * This function is used to derive new, cryptographically separate keys from a 50e5b75505Sopenharmony_ci * given key. If the requested buf_len is not divisible by eight, the least 51e5b75505Sopenharmony_ci * significant 1-7 bits of the last octet in the output are not part of the 52e5b75505Sopenharmony_ci * requested output. 53e5b75505Sopenharmony_ci */ 54e5b75505Sopenharmony_ciint sha256_prf_bits(const u8 *key, size_t key_len, const char *label, 55e5b75505Sopenharmony_ci const u8 *data, size_t data_len, u8 *buf, 56e5b75505Sopenharmony_ci size_t buf_len_bits) 57e5b75505Sopenharmony_ci{ 58e5b75505Sopenharmony_ci u16 counter = 1; 59e5b75505Sopenharmony_ci size_t pos, plen; 60e5b75505Sopenharmony_ci u8 hash[SHA256_MAC_LEN]; 61e5b75505Sopenharmony_ci const u8 *addr[4]; 62e5b75505Sopenharmony_ci size_t len[4]; 63e5b75505Sopenharmony_ci u8 counter_le[2], length_le[2]; 64e5b75505Sopenharmony_ci size_t buf_len = (buf_len_bits + 7) / 8; 65e5b75505Sopenharmony_ci 66e5b75505Sopenharmony_ci addr[0] = counter_le; 67e5b75505Sopenharmony_ci len[0] = 2; 68e5b75505Sopenharmony_ci addr[1] = (u8 *) label; 69e5b75505Sopenharmony_ci len[1] = os_strlen(label); 70e5b75505Sopenharmony_ci addr[2] = data; 71e5b75505Sopenharmony_ci len[2] = data_len; 72e5b75505Sopenharmony_ci addr[3] = length_le; 73e5b75505Sopenharmony_ci len[3] = sizeof(length_le); 74e5b75505Sopenharmony_ci 75e5b75505Sopenharmony_ci WPA_PUT_LE16(length_le, buf_len_bits); 76e5b75505Sopenharmony_ci pos = 0; 77e5b75505Sopenharmony_ci while (pos < buf_len) { 78e5b75505Sopenharmony_ci plen = buf_len - pos; 79e5b75505Sopenharmony_ci WPA_PUT_LE16(counter_le, counter); 80e5b75505Sopenharmony_ci if (plen >= SHA256_MAC_LEN) { 81e5b75505Sopenharmony_ci if (hmac_sha256_vector(key, key_len, 4, addr, len, 82e5b75505Sopenharmony_ci &buf[pos]) < 0) 83e5b75505Sopenharmony_ci return -1; 84e5b75505Sopenharmony_ci pos += SHA256_MAC_LEN; 85e5b75505Sopenharmony_ci } else { 86e5b75505Sopenharmony_ci if (hmac_sha256_vector(key, key_len, 4, addr, len, 87e5b75505Sopenharmony_ci hash) < 0) 88e5b75505Sopenharmony_ci return -1; 89e5b75505Sopenharmony_ci os_memcpy(&buf[pos], hash, plen); 90e5b75505Sopenharmony_ci pos += plen; 91e5b75505Sopenharmony_ci break; 92e5b75505Sopenharmony_ci } 93e5b75505Sopenharmony_ci counter++; 94e5b75505Sopenharmony_ci } 95e5b75505Sopenharmony_ci 96e5b75505Sopenharmony_ci /* 97e5b75505Sopenharmony_ci * Mask out unused bits in the last octet if it does not use all the 98e5b75505Sopenharmony_ci * bits. 99e5b75505Sopenharmony_ci */ 100e5b75505Sopenharmony_ci if (buf_len_bits % 8) { 101e5b75505Sopenharmony_ci u8 mask = 0xff << (8 - buf_len_bits % 8); 102e5b75505Sopenharmony_ci buf[pos - 1] &= mask; 103e5b75505Sopenharmony_ci } 104e5b75505Sopenharmony_ci 105e5b75505Sopenharmony_ci forced_memzero(hash, sizeof(hash)); 106e5b75505Sopenharmony_ci 107e5b75505Sopenharmony_ci return 0; 108e5b75505Sopenharmony_ci} 109