1bb610346Schristos /*
20a73ee0aSchristos * HMAC-SHA256 KDF (RFC 5295) and HKDF-Expand(SHA256) (RFC 5869)
30a73ee0aSchristos * Copyright (c) 2014-2017, Jouni Malinen <j@w1.fi>
4bb610346Schristos *
5bb610346Schristos * This software may be distributed under the terms of the BSD license.
6bb610346Schristos * See README for more details.
7bb610346Schristos */
8bb610346Schristos
9bb610346Schristos #include "includes.h"
10bb610346Schristos
11bb610346Schristos #include "common.h"
12bb610346Schristos #include "sha256.h"
13bb610346Schristos
14bb610346Schristos
15bb610346Schristos /**
16bb610346Schristos * hmac_sha256_kdf - HMAC-SHA256 based KDF (RFC 5295)
17bb610346Schristos * @secret: Key for KDF
18bb610346Schristos * @secret_len: Length of the key in bytes
190a73ee0aSchristos * @label: A unique label for each purpose of the KDF or %NULL to select
200a73ee0aSchristos * RFC 5869 HKDF-Expand() with arbitrary seed (= info)
21bb610346Schristos * @seed: Seed value to bind into the key
22bb610346Schristos * @seed_len: Length of the seed
23bb610346Schristos * @out: Buffer for the generated pseudo-random key
24bb610346Schristos * @outlen: Number of bytes of key to generate
25bb610346Schristos * Returns: 0 on success, -1 on failure.
26bb610346Schristos *
27bb610346Schristos * This function is used to derive new, cryptographically separate keys from a
280a73ee0aSchristos * given key in ERP. This KDF is defined in RFC 5295, Chapter 3.1.2. When used
290a73ee0aSchristos * with label = NULL and seed = info, this matches HKDF-Expand() defined in
300a73ee0aSchristos * RFC 5869, Chapter 2.3.
31bb610346Schristos */
hmac_sha256_kdf(const u8 * secret,size_t secret_len,const char * label,const u8 * seed,size_t seed_len,u8 * out,size_t outlen)32bb610346Schristos int hmac_sha256_kdf(const u8 *secret, size_t secret_len,
33bb610346Schristos const char *label, const u8 *seed, size_t seed_len,
34bb610346Schristos u8 *out, size_t outlen)
35bb610346Schristos {
36bb610346Schristos u8 T[SHA256_MAC_LEN];
37bb610346Schristos u8 iter = 1;
38bb610346Schristos const unsigned char *addr[4];
39bb610346Schristos size_t len[4];
40bb610346Schristos size_t pos, clen;
41bb610346Schristos
42bb610346Schristos addr[0] = T;
43bb610346Schristos len[0] = SHA256_MAC_LEN;
440a73ee0aSchristos if (label) {
45bb610346Schristos addr[1] = (const unsigned char *) label;
46bb610346Schristos len[1] = os_strlen(label) + 1;
470a73ee0aSchristos } else {
480a73ee0aSchristos addr[1] = (const u8 *) "";
490a73ee0aSchristos len[1] = 0;
500a73ee0aSchristos }
51bb610346Schristos addr[2] = seed;
52bb610346Schristos len[2] = seed_len;
53bb610346Schristos addr[3] = &iter;
54bb610346Schristos len[3] = 1;
55bb610346Schristos
56bb610346Schristos if (hmac_sha256_vector(secret, secret_len, 3, &addr[1], &len[1], T) < 0)
57bb610346Schristos return -1;
58bb610346Schristos
59bb610346Schristos pos = 0;
60bb610346Schristos for (;;) {
61bb610346Schristos clen = outlen - pos;
62bb610346Schristos if (clen > SHA256_MAC_LEN)
63bb610346Schristos clen = SHA256_MAC_LEN;
64bb610346Schristos os_memcpy(out + pos, T, clen);
65bb610346Schristos pos += clen;
66bb610346Schristos
67bb610346Schristos if (pos == outlen)
68bb610346Schristos break;
69bb610346Schristos
70bb610346Schristos if (iter == 255) {
71bb610346Schristos os_memset(out, 0, outlen);
72*3d6c0713Schristos forced_memzero(T, SHA256_MAC_LEN);
73bb610346Schristos return -1;
74bb610346Schristos }
75bb610346Schristos iter++;
76bb610346Schristos
77bb610346Schristos if (hmac_sha256_vector(secret, secret_len, 4, addr, len, T) < 0)
78bb610346Schristos {
79bb610346Schristos os_memset(out, 0, outlen);
80*3d6c0713Schristos forced_memzero(T, SHA256_MAC_LEN);
81bb610346Schristos return -1;
82bb610346Schristos }
83bb610346Schristos }
84bb610346Schristos
85*3d6c0713Schristos forced_memzero(T, SHA256_MAC_LEN);
86bb610346Schristos return 0;
87bb610346Schristos }
88