2 * HMAC-SHA256 KDF (RFC 5295) and HKDF-Expand(SHA256) (RFC 5869)
3 * Copyright (c) 2014-2017, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
16 * hmac_sha256_kdf - HMAC-SHA256 based KDF (RFC 5295)
17 * @secret: Key for KDF
18 * @secret_len: Length of the key in bytes
19 * @label: A unique label for each purpose of the KDF or %NULL to select
20 * RFC 5869 HKDF-Expand() with arbitrary seed (= info)
21 * @seed: Seed value to bind into the key
22 * @seed_len: Length of the seed
23 * @out: Buffer for the generated pseudo-random key
24 * @outlen: Number of bytes of key to generate
25 * Returns: 0 on success, -1 on failure.
27 * This function is used to derive new, cryptographically separate keys from a
28 * given key in ERP. This KDF is defined in RFC 5295, Chapter 3.1.2. When used
29 * with label = NULL and seed = info, this matches HKDF-Expand() defined in
30 * RFC 5869, Chapter 2.3.
32 int hmac_sha256_kdf(const u8 *secret, size_t secret_len,
33 const char *label, const u8 *seed, size_t seed_len,
34 u8 *out, size_t outlen)
38 const unsigned char *addr[4];
43 len[0] = SHA256_MAC_LEN;
45 addr[1] = (const unsigned char *) label;
46 len[1] = os_strlen(label) + 1;
48 addr[1] = (const u8 *) "";
56 if (hmac_sha256_vector(secret, secret_len, 3, &addr[1], &len[1], T) < 0)
62 if (clen > SHA256_MAC_LEN)
63 clen = SHA256_MAC_LEN;
64 os_memcpy(out + pos, T, clen);
71 os_memset(out, 0, outlen);
72 os_memset(T, 0, SHA256_MAC_LEN);
77 if (hmac_sha256_vector(secret, secret_len, 4, addr, len, T) < 0)
79 os_memset(out, 0, outlen);
80 os_memset(T, 0, SHA256_MAC_LEN);
85 os_memset(T, 0, SHA256_MAC_LEN);