Path: blob/main/crypto/openssl/providers/implementations/kdfs/kbkdf.c
48383 views
/*1* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.2* Copyright 2019 Red Hat, Inc.3*4* Licensed under the Apache License 2.0 (the "License"). You may not use5* this file except in compliance with the License. You can obtain a copy6* in the file LICENSE in the source distribution or at7* https://www.openssl.org/source/license.html8*/910/*11* This implements https://csrc.nist.gov/publications/detail/sp/800-108/final12* section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC13* and CMAC. That document does not name the KDFs it defines; the name is14* derived from15* https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation16*17* Note that section 5.3 ("double-pipeline mode") is not implemented, though18* it would be possible to do so in the future.19*20* These versions all assume the counter is used. It would be relatively21* straightforward to expose a configuration handle should the need arise.22*23* Variable names attempt to match those of SP800-108.24*/2526#include <stdarg.h>27#include <stdlib.h>28#include <string.h>2930#include <openssl/core_names.h>31#include <openssl/evp.h>32#include <openssl/hmac.h>33#include <openssl/kdf.h>34#include <openssl/params.h>35#include <openssl/proverr.h>3637#include "internal/cryptlib.h"38#include "crypto/evp.h"39#include "internal/numbers.h"40#include "internal/endian.h"41#include "prov/implementations.h"42#include "prov/provider_ctx.h"43#include "prov/provider_util.h"44#include "prov/providercommon.h"45#include "prov/securitycheck.h"46#include "internal/e_os.h"47#include "internal/params.h"4849#define ossl_min(a, b) ((a) < (b)) ? (a) : (b)5051typedef enum {52COUNTER = 0,53FEEDBACK54} kbkdf_mode;5556/* Our context structure. */57typedef struct {58void *provctx;59kbkdf_mode mode;60EVP_MAC_CTX *ctx_init;6162/* Names are lowercased versions of those found in SP800-108. */63int r;64unsigned char *ki;65size_t ki_len;66unsigned char *label;67size_t label_len;68unsigned char *context;69size_t context_len;70unsigned char *iv;71size_t iv_len;72int use_l;73int is_kmac;74int use_separator;75OSSL_FIPS_IND_DECLARE76} KBKDF;7778/* Definitions needed for typechecking. */79static OSSL_FUNC_kdf_newctx_fn kbkdf_new;80static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;81static OSSL_FUNC_kdf_freectx_fn kbkdf_free;82static OSSL_FUNC_kdf_reset_fn kbkdf_reset;83static OSSL_FUNC_kdf_derive_fn kbkdf_derive;84static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;85static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;86static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;87static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;8889/* Not all platforms have htobe32(). */90static uint32_t be32(uint32_t host)91{92uint32_t big = 0;93DECLARE_IS_ENDIAN;9495if (!IS_LITTLE_ENDIAN)96return host;9798big |= (host & 0xff000000) >> 24;99big |= (host & 0x00ff0000) >> 8;100big |= (host & 0x0000ff00) << 8;101big |= (host & 0x000000ff) << 24;102return big;103}104105static void init(KBKDF *ctx)106{107ctx->r = 32;108ctx->use_l = 1;109ctx->use_separator = 1;110ctx->is_kmac = 0;111}112113static void *kbkdf_new(void *provctx)114{115KBKDF *ctx;116117if (!ossl_prov_is_running())118return NULL;119120ctx = OPENSSL_zalloc(sizeof(*ctx));121if (ctx == NULL)122return NULL;123124ctx->provctx = provctx;125OSSL_FIPS_IND_INIT(ctx)126init(ctx);127return ctx;128}129130static void kbkdf_free(void *vctx)131{132KBKDF *ctx = (KBKDF *)vctx;133134if (ctx != NULL) {135kbkdf_reset(ctx);136OPENSSL_free(ctx);137}138}139140static void kbkdf_reset(void *vctx)141{142KBKDF *ctx = (KBKDF *)vctx;143void *provctx = ctx->provctx;144145EVP_MAC_CTX_free(ctx->ctx_init);146OPENSSL_clear_free(ctx->context, ctx->context_len);147OPENSSL_clear_free(ctx->label, ctx->label_len);148OPENSSL_clear_free(ctx->ki, ctx->ki_len);149OPENSSL_clear_free(ctx->iv, ctx->iv_len);150memset(ctx, 0, sizeof(*ctx));151ctx->provctx = provctx;152init(ctx);153}154155static void *kbkdf_dup(void *vctx)156{157const KBKDF *src = (const KBKDF *)vctx;158KBKDF *dest;159160dest = kbkdf_new(src->provctx);161if (dest != NULL) {162dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);163if (dest->ctx_init == NULL164|| !ossl_prov_memdup(src->ki, src->ki_len,165&dest->ki, &dest->ki_len)166|| !ossl_prov_memdup(src->label, src->label_len,167&dest->label, &dest->label_len)168|| !ossl_prov_memdup(src->context, src->context_len,169&dest->context, &dest->context_len)170|| !ossl_prov_memdup(src->iv, src->iv_len,171&dest->iv, &dest->iv_len))172goto err;173dest->mode = src->mode;174dest->r = src->r;175dest->use_l = src->use_l;176dest->use_separator = src->use_separator;177dest->is_kmac = src->is_kmac;178OSSL_FIPS_IND_COPY(dest, src)179}180return dest;181182err:183kbkdf_free(dest);184return NULL;185}186187#ifdef FIPS_MODULE188static int fips_kbkdf_key_check_passed(KBKDF *ctx)189{190OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);191int key_approved = ossl_kdf_check_key_size(ctx->ki_len);192193if (!key_approved) {194if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,195libctx, "KBKDF", "Key size",196ossl_fips_config_kbkdf_key_check)) {197ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);198return 0;199}200}201return 1;202}203#endif204205/* SP800-108 section 5.1 or section 5.2 depending on mode. */206static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,207size_t iv_len, unsigned char *label, size_t label_len,208unsigned char *context, size_t context_len,209unsigned char *k_i, size_t h, uint32_t l, int has_separator,210unsigned char *ko, size_t ko_len, int r)211{212int ret = 0;213EVP_MAC_CTX *ctx = NULL;214size_t written = 0, to_write, k_i_len = iv_len;215const unsigned char zero = 0;216uint32_t counter, i;217/*218* From SP800-108:219* The fixed input data is a concatenation of a Label,220* a separation indicator 0x00, the Context, and L.221* One or more of these fixed input data fields may be omitted.222*223* has_separator == 0 means that the separator is omitted.224* Passing a value of l == 0 means that L is omitted.225* The Context and L are omitted automatically if a NULL buffer is passed.226*/227int has_l = (l != 0);228229/* Setup K(0) for feedback mode. */230if (iv_len > 0)231memcpy(k_i, iv, iv_len);232233for (counter = 1; written < ko_len; counter++) {234i = be32(counter);235236ctx = EVP_MAC_CTX_dup(ctx_init);237if (ctx == NULL)238goto done;239240/* Perform feedback, if appropriate. */241if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))242goto done;243244if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)245|| !EVP_MAC_update(ctx, label, label_len)246|| (has_separator && !EVP_MAC_update(ctx, &zero, 1))247|| !EVP_MAC_update(ctx, context, context_len)248|| (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))249|| !EVP_MAC_final(ctx, k_i, NULL, h))250goto done;251252to_write = ko_len - written;253memcpy(ko + written, k_i, ossl_min(to_write, h));254written += h;255256k_i_len = h;257EVP_MAC_CTX_free(ctx);258ctx = NULL;259}260261ret = 1;262done:263EVP_MAC_CTX_free(ctx);264return ret;265}266267/* This must be run before the key is set */268static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)269{270OSSL_PARAM params[2];271272if (custom == NULL || customlen == 0)273return 1;274params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,275(void *)custom, customlen);276params[1] = OSSL_PARAM_construct_end();277return EVP_MAC_CTX_set_params(ctx, params) > 0;278}279280static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,281const unsigned char *context, size_t contextlen)282{283OSSL_PARAM params[2];284285params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);286params[1] = OSSL_PARAM_construct_end();287return EVP_MAC_CTX_set_params(ctx, params) > 0288&& EVP_MAC_update(ctx, context, contextlen)289&& EVP_MAC_final(ctx, out, NULL, outlen);290}291292static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,293const OSSL_PARAM params[])294{295KBKDF *ctx = (KBKDF *)vctx;296int ret = 0;297unsigned char *k_i = NULL;298uint32_t l = 0;299size_t h = 0;300uint64_t counter_max;301302if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))303return 0;304305/* label, context, and iv are permitted to be empty. Check everything306* else. */307if (ctx->ctx_init == NULL) {308if (ctx->ki_len == 0 || ctx->ki == NULL) {309ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);310return 0;311}312/* Could either be missing MAC or missing message digest or missing313* cipher - arbitrarily, I pick this one. */314ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);315return 0;316}317318/* Fail if the output length is zero */319if (keylen == 0) {320ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);321return 0;322}323324if (ctx->is_kmac) {325ret = kmac_derive(ctx->ctx_init, key, keylen,326ctx->context, ctx->context_len);327goto done;328}329330h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);331if (h == 0)332goto done;333334if (ctx->iv_len != 0 && ctx->iv_len != h) {335ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);336goto done;337}338339if (ctx->mode == COUNTER) {340/* Fail if keylen is too large for r */341counter_max = (uint64_t)1 << (uint64_t)ctx->r;342if ((uint64_t)(keylen / h) >= counter_max) {343ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);344goto done;345}346}347348if (ctx->use_l != 0)349l = be32(keylen * 8);350351k_i = OPENSSL_zalloc(h);352if (k_i == NULL)353goto done;354355ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,356ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,357ctx->use_separator, key, keylen, ctx->r);358done:359if (ret != 1)360OPENSSL_cleanse(key, keylen);361OPENSSL_clear_free(k_i, h);362return ret;363}364365static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])366{367KBKDF *ctx = (KBKDF *)vctx;368OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);369const OSSL_PARAM *p;370371if (ossl_param_is_empty(params))372return 1;373374if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,375OSSL_KDF_PARAM_FIPS_KEY_CHECK))376return 0;377378if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,379NULL, NULL, libctx))380return 0;381if (ctx->ctx_init != NULL) {382ctx->is_kmac = 0;383if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),384OSSL_MAC_NAME_KMAC128)385|| EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),386OSSL_MAC_NAME_KMAC256)) {387ctx->is_kmac = 1;388} else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),389OSSL_MAC_NAME_HMAC)390&& !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),391OSSL_MAC_NAME_CMAC)) {392ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);393return 0;394}395}396397p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);398if (p != NULL399&& OPENSSL_strncasecmp("counter", p->data, p->data_size) == 0) {400ctx->mode = COUNTER;401} else if (p != NULL402&& OPENSSL_strncasecmp("feedback", p->data, p->data_size) == 0) {403ctx->mode = FEEDBACK;404} else if (p != NULL) {405ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);406return 0;407}408409p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);410if (p != NULL) {411if (ossl_param_get1_octet_string(p, OSSL_KDF_PARAM_KEY,412&ctx->ki, &ctx->ki_len) == 0)413return 0;414#ifdef FIPS_MODULE415if (!fips_kbkdf_key_check_passed(ctx))416return 0;417#endif418}419420if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SALT,421&ctx->label, &ctx->label_len) == 0)422return 0;423424if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO,425&ctx->context, &ctx->context_len,4260) == 0)427return 0;428429if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SEED,430&ctx->iv, &ctx->iv_len) == 0)431return 0;432433p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_L);434if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_l))435return 0;436437p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_R);438if (p != NULL) {439int new_r = 0;440441if (!OSSL_PARAM_get_int(p, &new_r))442return 0;443if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)444return 0;445ctx->r = new_r;446}447448p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR);449if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_separator))450return 0;451452/* Set up digest context, if we can. */453if (ctx->ctx_init != NULL && ctx->ki_len != 0) {454if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))455|| !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))456return 0;457}458return 1;459}460461static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,462ossl_unused void *provctx)463{464static const OSSL_PARAM known_settable_ctx_params[] = {465OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),466OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),467OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),468OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),469OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),470OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),471OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),472OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),473OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),474OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),475OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),476OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),477OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)478OSSL_PARAM_END,479};480return known_settable_ctx_params;481}482483static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])484{485#ifdef FIPS_MODULE486KBKDF *ctx = (KBKDF *)vctx;487#endif488OSSL_PARAM *p;489490/* KBKDF can produce results as large as you like. */491p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);492if (p != NULL && !OSSL_PARAM_set_size_t(p, SIZE_MAX))493return 0;494495if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))496return 0;497return 1;498}499500static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,501ossl_unused void *provctx)502{503static const OSSL_PARAM known_gettable_ctx_params[] = {504OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),505OSSL_FIPS_IND_GETTABLE_CTX_PARAM()506OSSL_PARAM_END507};508return known_gettable_ctx_params;509}510511const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {512{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },513{ OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },514{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },515{ OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },516{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },517{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,518(void(*)(void))kbkdf_settable_ctx_params },519{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },520{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,521(void(*)(void))kbkdf_gettable_ctx_params },522{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },523OSSL_DISPATCH_END,524};525526527