15b9c547cSRui Paulo /*
285732ac8SCy Schubert * HMAC-SHA256 KDF (RFC 5295) and HKDF-Expand(SHA256) (RFC 5869)
385732ac8SCy Schubert * Copyright (c) 2014-2017, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo *
55b9c547cSRui Paulo * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo * See README for more details.
75b9c547cSRui Paulo */
85b9c547cSRui Paulo
95b9c547cSRui Paulo #include "includes.h"
105b9c547cSRui Paulo
115b9c547cSRui Paulo #include "common.h"
125b9c547cSRui Paulo #include "sha256.h"
135b9c547cSRui Paulo
145b9c547cSRui Paulo
155b9c547cSRui Paulo /**
165b9c547cSRui Paulo * hmac_sha256_kdf - HMAC-SHA256 based KDF (RFC 5295)
175b9c547cSRui Paulo * @secret: Key for KDF
185b9c547cSRui Paulo * @secret_len: Length of the key in bytes
1985732ac8SCy Schubert * @label: A unique label for each purpose of the KDF or %NULL to select
2085732ac8SCy Schubert * RFC 5869 HKDF-Expand() with arbitrary seed (= info)
215b9c547cSRui Paulo * @seed: Seed value to bind into the key
225b9c547cSRui Paulo * @seed_len: Length of the seed
235b9c547cSRui Paulo * @out: Buffer for the generated pseudo-random key
245b9c547cSRui Paulo * @outlen: Number of bytes of key to generate
255b9c547cSRui Paulo * Returns: 0 on success, -1 on failure.
265b9c547cSRui Paulo *
275b9c547cSRui Paulo * This function is used to derive new, cryptographically separate keys from a
2885732ac8SCy Schubert * given key in ERP. This KDF is defined in RFC 5295, Chapter 3.1.2. When used
2985732ac8SCy Schubert * with label = NULL and seed = info, this matches HKDF-Expand() defined in
3085732ac8SCy Schubert * RFC 5869, Chapter 2.3.
315b9c547cSRui Paulo */
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)325b9c547cSRui Paulo int hmac_sha256_kdf(const u8 *secret, size_t secret_len,
335b9c547cSRui Paulo const char *label, const u8 *seed, size_t seed_len,
345b9c547cSRui Paulo u8 *out, size_t outlen)
355b9c547cSRui Paulo {
365b9c547cSRui Paulo u8 T[SHA256_MAC_LEN];
375b9c547cSRui Paulo u8 iter = 1;
385b9c547cSRui Paulo const unsigned char *addr[4];
395b9c547cSRui Paulo size_t len[4];
405b9c547cSRui Paulo size_t pos, clen;
415b9c547cSRui Paulo
425b9c547cSRui Paulo addr[0] = T;
435b9c547cSRui Paulo len[0] = SHA256_MAC_LEN;
4485732ac8SCy Schubert if (label) {
455b9c547cSRui Paulo addr[1] = (const unsigned char *) label;
465b9c547cSRui Paulo len[1] = os_strlen(label) + 1;
4785732ac8SCy Schubert } else {
4885732ac8SCy Schubert addr[1] = (const u8 *) "";
4985732ac8SCy Schubert len[1] = 0;
5085732ac8SCy Schubert }
515b9c547cSRui Paulo addr[2] = seed;
525b9c547cSRui Paulo len[2] = seed_len;
535b9c547cSRui Paulo addr[3] = &iter;
545b9c547cSRui Paulo len[3] = 1;
555b9c547cSRui Paulo
565b9c547cSRui Paulo if (hmac_sha256_vector(secret, secret_len, 3, &addr[1], &len[1], T) < 0)
575b9c547cSRui Paulo return -1;
585b9c547cSRui Paulo
595b9c547cSRui Paulo pos = 0;
605b9c547cSRui Paulo for (;;) {
615b9c547cSRui Paulo clen = outlen - pos;
625b9c547cSRui Paulo if (clen > SHA256_MAC_LEN)
635b9c547cSRui Paulo clen = SHA256_MAC_LEN;
645b9c547cSRui Paulo os_memcpy(out + pos, T, clen);
655b9c547cSRui Paulo pos += clen;
665b9c547cSRui Paulo
675b9c547cSRui Paulo if (pos == outlen)
685b9c547cSRui Paulo break;
695b9c547cSRui Paulo
705b9c547cSRui Paulo if (iter == 255) {
715b9c547cSRui Paulo os_memset(out, 0, outlen);
72*206b73d0SCy Schubert forced_memzero(T, SHA256_MAC_LEN);
735b9c547cSRui Paulo return -1;
745b9c547cSRui Paulo }
755b9c547cSRui Paulo iter++;
765b9c547cSRui Paulo
775b9c547cSRui Paulo if (hmac_sha256_vector(secret, secret_len, 4, addr, len, T) < 0)
785b9c547cSRui Paulo {
795b9c547cSRui Paulo os_memset(out, 0, outlen);
80*206b73d0SCy Schubert forced_memzero(T, SHA256_MAC_LEN);
815b9c547cSRui Paulo return -1;
825b9c547cSRui Paulo }
835b9c547cSRui Paulo }
845b9c547cSRui Paulo
85*206b73d0SCy Schubert forced_memzero(T, SHA256_MAC_LEN);
865b9c547cSRui Paulo return 0;
875b9c547cSRui Paulo }
88