185732ac8SCy Schubert /*
285732ac8SCy Schubert * HMAC-SHA512 KDF (RFC 5295) and HKDF-Expand(SHA512) (RFC 5869)
385732ac8SCy Schubert * Copyright (c) 2014-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
1485732ac8SCy Schubert
1585732ac8SCy Schubert /**
1685732ac8SCy Schubert * hmac_sha512_kdf - HMAC-SHA512 based KDF (RFC 5295)
1785732ac8SCy Schubert * @secret: Key for KDF
1885732ac8SCy Schubert * @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)
2185732ac8SCy Schubert * @seed: Seed value to bind into the key
2285732ac8SCy Schubert * @seed_len: Length of the seed
2385732ac8SCy Schubert * @out: Buffer for the generated pseudo-random key
2485732ac8SCy Schubert * @outlen: 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 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.
3185732ac8SCy Schubert */
hmac_sha512_kdf(const u8 * secret,size_t secret_len,const char * label,const u8 * seed,size_t seed_len,u8 * out,size_t outlen)3285732ac8SCy Schubert int hmac_sha512_kdf(const u8 *secret, size_t secret_len,
3385732ac8SCy Schubert const char *label, const u8 *seed, size_t seed_len,
3485732ac8SCy Schubert u8 *out, size_t outlen)
3585732ac8SCy Schubert {
3685732ac8SCy Schubert u8 T[SHA512_MAC_LEN];
3785732ac8SCy Schubert u8 iter = 1;
3885732ac8SCy Schubert const unsigned char *addr[4];
3985732ac8SCy Schubert size_t len[4];
4085732ac8SCy Schubert size_t pos, clen;
4185732ac8SCy Schubert
4285732ac8SCy Schubert addr[0] = T;
4385732ac8SCy Schubert len[0] = SHA512_MAC_LEN;
4485732ac8SCy Schubert if (label) {
4585732ac8SCy Schubert addr[1] = (const unsigned char *) label;
4685732ac8SCy Schubert len[1] = os_strlen(label) + 1;
4785732ac8SCy Schubert } else {
4885732ac8SCy Schubert addr[1] = (const u8 *) "";
4985732ac8SCy Schubert len[1] = 0;
5085732ac8SCy Schubert }
5185732ac8SCy Schubert addr[2] = seed;
5285732ac8SCy Schubert len[2] = seed_len;
5385732ac8SCy Schubert addr[3] = &iter;
5485732ac8SCy Schubert len[3] = 1;
5585732ac8SCy Schubert
5685732ac8SCy Schubert if (hmac_sha512_vector(secret, secret_len, 3, &addr[1], &len[1], T) < 0)
5785732ac8SCy Schubert return -1;
5885732ac8SCy Schubert
5985732ac8SCy Schubert pos = 0;
6085732ac8SCy Schubert for (;;) {
6185732ac8SCy Schubert clen = outlen - pos;
6285732ac8SCy Schubert if (clen > SHA512_MAC_LEN)
6385732ac8SCy Schubert clen = SHA512_MAC_LEN;
6485732ac8SCy Schubert os_memcpy(out + pos, T, clen);
6585732ac8SCy Schubert pos += clen;
6685732ac8SCy Schubert
6785732ac8SCy Schubert if (pos == outlen)
6885732ac8SCy Schubert break;
6985732ac8SCy Schubert
7085732ac8SCy Schubert if (iter == 255) {
7185732ac8SCy Schubert os_memset(out, 0, outlen);
72*206b73d0SCy Schubert forced_memzero(T, SHA512_MAC_LEN);
7385732ac8SCy Schubert return -1;
7485732ac8SCy Schubert }
7585732ac8SCy Schubert iter++;
7685732ac8SCy Schubert
7785732ac8SCy Schubert if (hmac_sha512_vector(secret, secret_len, 4, addr, len, T) < 0)
7885732ac8SCy Schubert {
7985732ac8SCy Schubert os_memset(out, 0, outlen);
80*206b73d0SCy Schubert forced_memzero(T, SHA512_MAC_LEN);
8185732ac8SCy Schubert return -1;
8285732ac8SCy Schubert }
8385732ac8SCy Schubert }
8485732ac8SCy Schubert
85*206b73d0SCy Schubert forced_memzero(T, SHA512_MAC_LEN);
8685732ac8SCy Schubert return 0;
8785732ac8SCy Schubert }
88