Path: blob/main/sys/contrib/openzfs/module/zfs/hkdf.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*17* Copyright (c) 2017, Datto, Inc. All rights reserved.18*/1920#include <sys/crypto/api.h>21#include <sys/sha2.h>22#include <sys/hkdf.h>2324static int25hkdf_sha512_extract(uint8_t *salt, uint_t salt_len, uint8_t *key_material,26uint_t km_len, uint8_t *out_buf)27{28int ret;29crypto_mechanism_t mech;30crypto_key_t key;31crypto_data_t input_cd, output_cd;3233/* initialize HMAC mechanism */34mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);35mech.cm_param = NULL;36mech.cm_param_len = 0;3738/* initialize the salt as a crypto key */39key.ck_length = CRYPTO_BYTES2BITS(salt_len);40key.ck_data = salt;4142/* initialize crypto data for the input and output data */43input_cd.cd_format = CRYPTO_DATA_RAW;44input_cd.cd_offset = 0;45input_cd.cd_length = km_len;46input_cd.cd_raw.iov_base = (char *)key_material;47input_cd.cd_raw.iov_len = input_cd.cd_length;4849output_cd.cd_format = CRYPTO_DATA_RAW;50output_cd.cd_offset = 0;51output_cd.cd_length = SHA512_DIGEST_LENGTH;52output_cd.cd_raw.iov_base = (char *)out_buf;53output_cd.cd_raw.iov_len = output_cd.cd_length;5455ret = crypto_mac(&mech, &input_cd, &key, NULL, &output_cd);56if (ret != CRYPTO_SUCCESS)57return (SET_ERROR(EIO));5859return (0);60}6162static int63hkdf_sha512_expand(uint8_t *extract_key, uint8_t *info, uint_t info_len,64uint8_t *out_buf, uint_t out_len)65{66int ret;67crypto_mechanism_t mech;68crypto_context_t ctx;69crypto_key_t key;70crypto_data_t T_cd, info_cd, c_cd;71uint_t i, T_len = 0, pos = 0;72uint8_t c;73uint_t N = (out_len + SHA512_DIGEST_LENGTH) / SHA512_DIGEST_LENGTH;74uint8_t T[SHA512_DIGEST_LENGTH];7576if (N > 255)77return (SET_ERROR(EINVAL));7879/* initialize HMAC mechanism */80mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);81mech.cm_param = NULL;82mech.cm_param_len = 0;8384/* initialize the salt as a crypto key */85key.ck_length = CRYPTO_BYTES2BITS(SHA512_DIGEST_LENGTH);86key.ck_data = extract_key;8788/* initialize crypto data for the input and output data */89T_cd.cd_format = CRYPTO_DATA_RAW;90T_cd.cd_offset = 0;91T_cd.cd_raw.iov_base = (char *)T;9293c_cd.cd_format = CRYPTO_DATA_RAW;94c_cd.cd_offset = 0;95c_cd.cd_length = 1;96c_cd.cd_raw.iov_base = (char *)&c;97c_cd.cd_raw.iov_len = c_cd.cd_length;9899info_cd.cd_format = CRYPTO_DATA_RAW;100info_cd.cd_offset = 0;101info_cd.cd_length = info_len;102info_cd.cd_raw.iov_base = (char *)info;103info_cd.cd_raw.iov_len = info_cd.cd_length;104105for (i = 1; i <= N; i++) {106c = i;107108T_cd.cd_length = T_len;109T_cd.cd_raw.iov_len = T_cd.cd_length;110111ret = crypto_mac_init(&mech, &key, NULL, &ctx);112if (ret != CRYPTO_SUCCESS)113return (SET_ERROR(EIO));114115ret = crypto_mac_update(ctx, &T_cd);116if (ret != CRYPTO_SUCCESS)117return (SET_ERROR(EIO));118119ret = crypto_mac_update(ctx, &info_cd);120if (ret != CRYPTO_SUCCESS)121return (SET_ERROR(EIO));122123ret = crypto_mac_update(ctx, &c_cd);124if (ret != CRYPTO_SUCCESS)125return (SET_ERROR(EIO));126127T_len = SHA512_DIGEST_LENGTH;128T_cd.cd_length = T_len;129T_cd.cd_raw.iov_len = T_cd.cd_length;130131ret = crypto_mac_final(ctx, &T_cd);132if (ret != CRYPTO_SUCCESS)133return (SET_ERROR(EIO));134135memcpy(out_buf + pos, T,136(i != N) ? SHA512_DIGEST_LENGTH : (out_len - pos));137pos += SHA512_DIGEST_LENGTH;138}139140return (0);141}142143/*144* HKDF is designed to be a relatively fast function for deriving keys from a145* master key + a salt. We use this function to generate new encryption keys146* so as to avoid hitting the cryptographic limits of the underlying147* encryption modes. Note that, for the sake of deriving encryption keys, the148* info parameter is called the "salt" everywhere else in the code.149*/150int151hkdf_sha512(uint8_t *key_material, uint_t km_len, uint8_t *salt,152uint_t salt_len, uint8_t *info, uint_t info_len, uint8_t *output_key,153uint_t out_len)154{155int ret;156uint8_t extract_key[SHA512_DIGEST_LENGTH];157158ret = hkdf_sha512_extract(salt, salt_len, key_material, km_len,159extract_key);160if (ret != 0)161return (ret);162163ret = hkdf_sha512_expand(extract_key, info, info_len, output_key,164out_len);165if (ret != 0)166return (ret);167168return (0);169}170171172