xref: /illumos-gate/usr/src/uts/common/fs/zfs/hkdf.c (revision eb633035)
1*eb633035STom Caputi /*
2*eb633035STom Caputi  * CDDL HEADER START
3*eb633035STom Caputi  *
4*eb633035STom Caputi  * This file and its contents are supplied under the terms of the
5*eb633035STom Caputi  * Common Development and Distribution License ("CDDL"), version 1.0.
6*eb633035STom Caputi  * You may only use this file in accordance with the terms of version
7*eb633035STom Caputi  * 1.0 of the CDDL.
8*eb633035STom Caputi  *
9*eb633035STom Caputi  * A full copy of the text of the CDDL should have accompanied this
10*eb633035STom Caputi  * source.  A copy of the CDDL is also available via the Internet at
11*eb633035STom Caputi  * http://www.illumos.org/license/CDDL.
12*eb633035STom Caputi  *
13*eb633035STom Caputi  * CDDL HEADER END
14*eb633035STom Caputi  */
15*eb633035STom Caputi 
16*eb633035STom Caputi /*
17*eb633035STom Caputi  * Copyright (c) 2017, Datto, Inc. All rights reserved.
18*eb633035STom Caputi  */
19*eb633035STom Caputi 
20*eb633035STom Caputi #include <sys/dmu.h>
21*eb633035STom Caputi #include <sys/hkdf.h>
22*eb633035STom Caputi #include <sys/crypto/api.h>
23*eb633035STom Caputi #include <sys/sha2.h>
24*eb633035STom Caputi #include <sys/hkdf.h>
25*eb633035STom Caputi 
26*eb633035STom Caputi static int
hkdf_sha512_extract(uint8_t * salt,uint_t salt_len,uint8_t * key_material,uint_t km_len,uint8_t * out_buf)27*eb633035STom Caputi hkdf_sha512_extract(uint8_t *salt, uint_t salt_len, uint8_t *key_material,
28*eb633035STom Caputi     uint_t km_len, uint8_t *out_buf)
29*eb633035STom Caputi {
30*eb633035STom Caputi 	int ret;
31*eb633035STom Caputi 	crypto_mechanism_t mech;
32*eb633035STom Caputi 	crypto_key_t key;
33*eb633035STom Caputi 	crypto_data_t input_cd, output_cd;
34*eb633035STom Caputi 
35*eb633035STom Caputi 	/* initialize HMAC mechanism */
36*eb633035STom Caputi 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
37*eb633035STom Caputi 	mech.cm_param = NULL;
38*eb633035STom Caputi 	mech.cm_param_len = 0;
39*eb633035STom Caputi 
40*eb633035STom Caputi 	/* initialize the salt as a crypto key */
41*eb633035STom Caputi 	key.ck_format = CRYPTO_KEY_RAW;
42*eb633035STom Caputi 	key.ck_length = CRYPTO_BYTES2BITS(salt_len);
43*eb633035STom Caputi 	key.ck_data = salt;
44*eb633035STom Caputi 
45*eb633035STom Caputi 	/* initialize crypto data for the input and output data */
46*eb633035STom Caputi 	input_cd.cd_format = CRYPTO_DATA_RAW;
47*eb633035STom Caputi 	input_cd.cd_offset = 0;
48*eb633035STom Caputi 	input_cd.cd_length = km_len;
49*eb633035STom Caputi 	input_cd.cd_raw.iov_base = (char *)key_material;
50*eb633035STom Caputi 	input_cd.cd_raw.iov_len = input_cd.cd_length;
51*eb633035STom Caputi 
52*eb633035STom Caputi 	output_cd.cd_format = CRYPTO_DATA_RAW;
53*eb633035STom Caputi 	output_cd.cd_offset = 0;
54*eb633035STom Caputi 	output_cd.cd_length = SHA512_DIGEST_LENGTH;
55*eb633035STom Caputi 	output_cd.cd_raw.iov_base = (char *)out_buf;
56*eb633035STom Caputi 	output_cd.cd_raw.iov_len = output_cd.cd_length;
57*eb633035STom Caputi 
58*eb633035STom Caputi 	ret = crypto_mac(&mech, &input_cd, &key, NULL, &output_cd, NULL);
59*eb633035STom Caputi 	if (ret != CRYPTO_SUCCESS)
60*eb633035STom Caputi 		return (SET_ERROR(EIO));
61*eb633035STom Caputi 
62*eb633035STom Caputi 	return (0);
63*eb633035STom Caputi }
64*eb633035STom Caputi 
65*eb633035STom Caputi static int
hkdf_sha512_expand(uint8_t * extract_key,uint8_t * info,uint_t info_len,uint8_t * out_buf,uint_t out_len)66*eb633035STom Caputi hkdf_sha512_expand(uint8_t *extract_key, uint8_t *info, uint_t info_len,
67*eb633035STom Caputi     uint8_t *out_buf, uint_t out_len)
68*eb633035STom Caputi {
69*eb633035STom Caputi 	int ret;
70*eb633035STom Caputi 	crypto_mechanism_t mech;
71*eb633035STom Caputi 	crypto_context_t ctx;
72*eb633035STom Caputi 	crypto_key_t key;
73*eb633035STom Caputi 	crypto_data_t T_cd, info_cd, c_cd;
74*eb633035STom Caputi 	uint_t i, T_len = 0, pos = 0;
75*eb633035STom Caputi 	uint8_t c;
76*eb633035STom Caputi 	uint_t N = (out_len + SHA512_DIGEST_LENGTH) / SHA512_DIGEST_LENGTH;
77*eb633035STom Caputi 	uint8_t T[SHA512_DIGEST_LENGTH];
78*eb633035STom Caputi 
79*eb633035STom Caputi 	if (N > 255)
80*eb633035STom Caputi 		return (SET_ERROR(EINVAL));
81*eb633035STom Caputi 
82*eb633035STom Caputi 	/* initialize HMAC mechanism */
83*eb633035STom Caputi 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
84*eb633035STom Caputi 	mech.cm_param = NULL;
85*eb633035STom Caputi 	mech.cm_param_len = 0;
86*eb633035STom Caputi 
87*eb633035STom Caputi 	/* initialize the salt as a crypto key */
88*eb633035STom Caputi 	key.ck_format = CRYPTO_KEY_RAW;
89*eb633035STom Caputi 	key.ck_length = CRYPTO_BYTES2BITS(SHA512_DIGEST_LENGTH);
90*eb633035STom Caputi 	key.ck_data = extract_key;
91*eb633035STom Caputi 
92*eb633035STom Caputi 	/* initialize crypto data for the input and output data */
93*eb633035STom Caputi 	T_cd.cd_format = CRYPTO_DATA_RAW;
94*eb633035STom Caputi 	T_cd.cd_offset = 0;
95*eb633035STom Caputi 	T_cd.cd_raw.iov_base = (char *)T;
96*eb633035STom Caputi 
97*eb633035STom Caputi 	c_cd.cd_format = CRYPTO_DATA_RAW;
98*eb633035STom Caputi 	c_cd.cd_offset = 0;
99*eb633035STom Caputi 	c_cd.cd_length = 1;
100*eb633035STom Caputi 	c_cd.cd_raw.iov_base = (char *)&c;
101*eb633035STom Caputi 	c_cd.cd_raw.iov_len = c_cd.cd_length;
102*eb633035STom Caputi 
103*eb633035STom Caputi 	info_cd.cd_format = CRYPTO_DATA_RAW;
104*eb633035STom Caputi 	info_cd.cd_offset = 0;
105*eb633035STom Caputi 	info_cd.cd_length = info_len;
106*eb633035STom Caputi 	info_cd.cd_raw.iov_base = (char *)info;
107*eb633035STom Caputi 	info_cd.cd_raw.iov_len = info_cd.cd_length;
108*eb633035STom Caputi 
109*eb633035STom Caputi 	for (i = 1; i <= N; i++) {
110*eb633035STom Caputi 		c = i;
111*eb633035STom Caputi 
112*eb633035STom Caputi 		T_cd.cd_length = T_len;
113*eb633035STom Caputi 		T_cd.cd_raw.iov_len = T_cd.cd_length;
114*eb633035STom Caputi 
115*eb633035STom Caputi 		ret = crypto_mac_init(&mech, &key, NULL, &ctx, NULL);
116*eb633035STom Caputi 		if (ret != CRYPTO_SUCCESS)
117*eb633035STom Caputi 			return (SET_ERROR(EIO));
118*eb633035STom Caputi 
119*eb633035STom Caputi 		ret = crypto_mac_update(ctx, &T_cd, NULL);
120*eb633035STom Caputi 		if (ret != CRYPTO_SUCCESS)
121*eb633035STom Caputi 			return (SET_ERROR(EIO));
122*eb633035STom Caputi 
123*eb633035STom Caputi 		ret = crypto_mac_update(ctx, &info_cd, NULL);
124*eb633035STom Caputi 		if (ret != CRYPTO_SUCCESS)
125*eb633035STom Caputi 			return (SET_ERROR(EIO));
126*eb633035STom Caputi 
127*eb633035STom Caputi 		ret = crypto_mac_update(ctx, &c_cd, NULL);
128*eb633035STom Caputi 		if (ret != CRYPTO_SUCCESS)
129*eb633035STom Caputi 			return (SET_ERROR(EIO));
130*eb633035STom Caputi 
131*eb633035STom Caputi 		T_len = SHA512_DIGEST_LENGTH;
132*eb633035STom Caputi 		T_cd.cd_length = T_len;
133*eb633035STom Caputi 		T_cd.cd_raw.iov_len = T_cd.cd_length;
134*eb633035STom Caputi 
135*eb633035STom Caputi 		ret = crypto_mac_final(ctx, &T_cd, NULL);
136*eb633035STom Caputi 		if (ret != CRYPTO_SUCCESS)
137*eb633035STom Caputi 			return (SET_ERROR(EIO));
138*eb633035STom Caputi 
139*eb633035STom Caputi 		bcopy(T, out_buf + pos,
140*eb633035STom Caputi 		    (i != N) ? SHA512_DIGEST_LENGTH : (out_len - pos));
141*eb633035STom Caputi 		pos += SHA512_DIGEST_LENGTH;
142*eb633035STom Caputi 	}
143*eb633035STom Caputi 
144*eb633035STom Caputi 	return (0);
145*eb633035STom Caputi }
146*eb633035STom Caputi 
147*eb633035STom Caputi /*
148*eb633035STom Caputi  * HKDF is designed to be a relatively fast function for deriving keys from a
149*eb633035STom Caputi  * master key + a salt. We use this function to generate new encryption keys
150*eb633035STom Caputi  * so as to avoid hitting the cryptographic limits of the underlying
151*eb633035STom Caputi  * encryption modes. Note that, for the sake of deriving encryption keys, the
152*eb633035STom Caputi  * info parameter is called the "salt" everywhere else in the code.
153*eb633035STom Caputi  */
154*eb633035STom Caputi 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)155*eb633035STom Caputi hkdf_sha512(uint8_t *key_material, uint_t km_len, uint8_t *salt,
156*eb633035STom Caputi     uint_t salt_len, uint8_t *info, uint_t info_len, uint8_t *output_key,
157*eb633035STom Caputi     uint_t out_len)
158*eb633035STom Caputi {
159*eb633035STom Caputi 	int ret;
160*eb633035STom Caputi 	uint8_t extract_key[SHA512_DIGEST_LENGTH];
161*eb633035STom Caputi 
162*eb633035STom Caputi 	ret = hkdf_sha512_extract(salt, salt_len, key_material, km_len,
163*eb633035STom Caputi 	    extract_key);
164*eb633035STom Caputi 	if (ret != 0)
165*eb633035STom Caputi 		return (ret);
166*eb633035STom Caputi 
167*eb633035STom Caputi 	ret = hkdf_sha512_expand(extract_key, info, info_len, output_key,
168*eb633035STom Caputi 	    out_len);
169*eb633035STom Caputi 	if (ret != 0)
170*eb633035STom Caputi 		return (ret);
171*eb633035STom Caputi 
172*eb633035STom Caputi 	return (0);
173*eb633035STom Caputi }
174