xref: /freebsd-src/sys/contrib/openzfs/module/os/freebsd/zfs/hkdf.c (revision da5137abdf463bb5fee85061958a14dd12bc043e)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
6eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
7eda14cbcSMatt Macy  * 1.0 of the CDDL.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
10eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
11eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * CDDL HEADER END
14eda14cbcSMatt Macy  */
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2017, Datto, Inc. All rights reserved.
18eda14cbcSMatt Macy  */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy #include <sys/dmu.h>
21eda14cbcSMatt Macy #include <sys/hkdf.h>
22eda14cbcSMatt Macy #include <sys/freebsd_crypto.h>
23eda14cbcSMatt Macy #include <sys/hkdf.h>
24eda14cbcSMatt Macy 
25eda14cbcSMatt Macy static int
hkdf_sha512_extract(uint8_t * salt,uint_t salt_len,uint8_t * key_material,uint_t km_len,uint8_t * out_buf)26eda14cbcSMatt Macy hkdf_sha512_extract(uint8_t *salt, uint_t salt_len, uint8_t *key_material,
27eda14cbcSMatt Macy     uint_t km_len, uint8_t *out_buf)
28eda14cbcSMatt Macy {
29eda14cbcSMatt Macy 	crypto_key_t key;
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy 	/* initialize the salt as a crypto key */
32eda14cbcSMatt Macy 	key.ck_length = CRYPTO_BYTES2BITS(salt_len);
33eda14cbcSMatt Macy 	key.ck_data = salt;
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy 	crypto_mac(&key, key_material, km_len, out_buf, SHA512_DIGEST_LENGTH);
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy 	return (0);
38eda14cbcSMatt Macy }
39eda14cbcSMatt Macy 
40eda14cbcSMatt Macy static int
hkdf_sha512_expand(uint8_t * extract_key,uint8_t * info,uint_t info_len,uint8_t * out_buf,uint_t out_len)41eda14cbcSMatt Macy hkdf_sha512_expand(uint8_t *extract_key, uint8_t *info, uint_t info_len,
42eda14cbcSMatt Macy     uint8_t *out_buf, uint_t out_len)
43eda14cbcSMatt Macy {
44eda14cbcSMatt Macy 	struct hmac_ctx ctx;
45eda14cbcSMatt Macy 	crypto_key_t key;
46eda14cbcSMatt Macy 	uint_t i, T_len = 0, pos = 0;
47eda14cbcSMatt Macy 	uint8_t c;
48eda14cbcSMatt Macy 	uint_t N = (out_len + SHA512_DIGEST_LENGTH) / SHA512_DIGEST_LENGTH;
49eda14cbcSMatt Macy 	uint8_t T[SHA512_DIGEST_LENGTH];
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy 	if (N > 255)
52eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy 	/* initialize the salt as a crypto key */
55eda14cbcSMatt Macy 	key.ck_length = CRYPTO_BYTES2BITS(SHA512_DIGEST_LENGTH);
56eda14cbcSMatt Macy 	key.ck_data = extract_key;
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy 	for (i = 1; i <= N; i++) {
59eda14cbcSMatt Macy 		c = i;
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy 		crypto_mac_init(&ctx, &key);
62eda14cbcSMatt Macy 		crypto_mac_update(&ctx, T, T_len);
63eda14cbcSMatt Macy 		crypto_mac_update(&ctx, info, info_len);
64eda14cbcSMatt Macy 		crypto_mac_update(&ctx, &c, 1);
65eda14cbcSMatt Macy 		crypto_mac_final(&ctx, T, SHA512_DIGEST_LENGTH);
66*da5137abSMartin Matuska 		memcpy(out_buf + pos, T,
67eda14cbcSMatt Macy 		    (i != N) ? SHA512_DIGEST_LENGTH : (out_len - pos));
68eda14cbcSMatt Macy 		pos += SHA512_DIGEST_LENGTH;
69eda14cbcSMatt Macy 	}
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy 	return (0);
72eda14cbcSMatt Macy }
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy /*
75eda14cbcSMatt Macy  * HKDF is designed to be a relatively fast function for deriving keys from a
76eda14cbcSMatt Macy  * master key + a salt. We use this function to generate new encryption keys
77eda14cbcSMatt Macy  * so as to avoid hitting the cryptographic limits of the underlying
78eda14cbcSMatt Macy  * encryption modes. Note that, for the sake of deriving encryption keys, the
79eda14cbcSMatt Macy  * info parameter is called the "salt" everywhere else in the code.
80eda14cbcSMatt Macy  */
81eda14cbcSMatt Macy int
hkdf_sha512(uint8_t * key_material,uint_t km_len,uint8_t * salt,uint_t salt_len,uint8_t * info,uint_t info_len,uint8_t * output_key,uint_t out_len)82eda14cbcSMatt Macy hkdf_sha512(uint8_t *key_material, uint_t km_len, uint8_t *salt,
83eda14cbcSMatt Macy     uint_t salt_len, uint8_t *info, uint_t info_len, uint8_t *output_key,
84eda14cbcSMatt Macy     uint_t out_len)
85eda14cbcSMatt Macy {
86eda14cbcSMatt Macy 	int ret;
87eda14cbcSMatt Macy 	uint8_t extract_key[SHA512_DIGEST_LENGTH];
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy 	ret = hkdf_sha512_extract(salt, salt_len, key_material, km_len,
90eda14cbcSMatt Macy 	    extract_key);
91eda14cbcSMatt Macy 	if (ret != 0)
92eda14cbcSMatt Macy 		return (ret);
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	ret = hkdf_sha512_expand(extract_key, info, info_len, output_key,
95eda14cbcSMatt Macy 	    out_len);
96eda14cbcSMatt Macy 	if (ret != 0)
97eda14cbcSMatt Macy 		return (ret);
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	return (0);
100eda14cbcSMatt Macy }
101