Path: blob/main/crypto/openssl/providers/implementations/kdfs/hmacdrbg_kdf.c
48383 views
/*1* Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdlib.h>10#include <string.h>11#include <openssl/crypto.h>12#include <openssl/err.h>13#include <openssl/kdf.h>14#include <openssl/proverr.h>15#include <openssl/core_names.h>16#include "prov/providercommon.h"17#include "prov/implementations.h"18#include "prov/hmac_drbg.h"19#include "prov/provider_ctx.h"2021static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new;22static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup;23static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free;24static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset;25static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive;26static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params;27static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params;28static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params;29static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params;3031typedef struct {32PROV_DRBG_HMAC base;33void *provctx;34unsigned char *entropy, *nonce;35size_t entropylen, noncelen;36int init;37} KDF_HMAC_DRBG;3839static void *hmac_drbg_kdf_new(void *provctx)40{41KDF_HMAC_DRBG *ctx;4243if (!ossl_prov_is_running())44return NULL;4546ctx = OPENSSL_zalloc(sizeof(*ctx));47if (ctx == NULL) {48ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);49return NULL;50}51ctx->provctx = provctx;52return ctx;53}5455static void hmac_drbg_kdf_reset(void *vctx)56{57KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;58PROV_DRBG_HMAC *drbg = &ctx->base;59void *provctx = ctx->provctx;6061EVP_MAC_CTX_free(drbg->ctx);62ossl_prov_digest_reset(&drbg->digest);63OPENSSL_clear_free(ctx->entropy, ctx->entropylen);64OPENSSL_clear_free(ctx->nonce, ctx->noncelen);65OPENSSL_cleanse(ctx, sizeof(*ctx));66ctx->provctx = provctx;67}6869static void hmac_drbg_kdf_free(void *vctx)70{71KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;7273if (ctx != NULL) {74hmac_drbg_kdf_reset(ctx);75OPENSSL_free(ctx);76}77}7879static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src) {80if (src->ctx != NULL) {81dst->ctx = EVP_MAC_CTX_dup(src->ctx);82if (dst->ctx == NULL)83return 0;84}85if (!ossl_prov_digest_copy(&dst->digest, &src->digest))86return 0;87memcpy(dst->K, src->K, sizeof(dst->K));88memcpy(dst->V, src->V, sizeof(dst->V));89dst->blocklen = src->blocklen;90return 1;91}9293static void *hmac_drbg_kdf_dup(void *vctx)94{95const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;96KDF_HMAC_DRBG *dst;9798dst = hmac_drbg_kdf_new(src->provctx);99if (dst != NULL) {100if (!ossl_drbg_hmac_dup(&dst->base, &src->base)101|| !ossl_prov_memdup(src->entropy, src->entropylen,102&dst->entropy , &dst->entropylen)103|| !ossl_prov_memdup(src->nonce, src->noncelen,104&dst->nonce, &dst->noncelen))105goto err;106dst->init = src->init;107}108return dst;109110err:111hmac_drbg_kdf_free(dst);112return NULL;113}114115static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,116const OSSL_PARAM params[])117{118KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;119PROV_DRBG_HMAC *drbg = &ctx->base;120121if (!ossl_prov_is_running()122|| !hmac_drbg_kdf_set_ctx_params(vctx, params))123return 0;124if (!ctx->init) {125if (ctx->entropy == NULL126|| ctx->entropylen == 0127|| ctx->nonce == NULL128|| ctx->noncelen == 0129|| !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,130ctx->nonce, ctx->noncelen, NULL, 0))131return 0;132ctx->init = 1;133}134135return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);136}137138static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])139{140KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;141PROV_DRBG_HMAC *drbg = &hmac->base;142const char *name;143const EVP_MD *md;144OSSL_PARAM *p;145146p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_MAC);147if (p != NULL) {148if (drbg->ctx == NULL)149return 0;150name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));151if (!OSSL_PARAM_set_utf8_string(p, name))152return 0;153}154155p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_DIGEST);156if (p != NULL) {157md = ossl_prov_digest_md(&drbg->digest);158if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))159return 0;160}161return 1;162}163164static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(165ossl_unused void *vctx, ossl_unused void *p_ctx)166{167static const OSSL_PARAM known_gettable_ctx_params[] = {168OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),169OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),170OSSL_PARAM_END171};172return known_gettable_ctx_params;173}174175static int hmac_drbg_kdf_set_ctx_params(void *vctx,176const OSSL_PARAM params[])177{178KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;179PROV_DRBG_HMAC *drbg = &hmac->base;180OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx);181const EVP_MD *md;182const OSSL_PARAM *p;183void *ptr = NULL;184size_t size = 0;185int md_size;186187if (ossl_param_is_empty(params))188return 1;189190p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_ENTROPY);191if (p != NULL) {192if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))193return 0;194OPENSSL_free(hmac->entropy);195hmac->entropy = ptr;196hmac->entropylen = size;197hmac->init = 0;198ptr = NULL;199}200201p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_NONCE);202if (p != NULL) {203if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))204return 0;205OPENSSL_free(hmac->nonce);206hmac->nonce = ptr;207hmac->noncelen = size;208hmac->init = 0;209}210211p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);212if (p != NULL) {213if (!ossl_prov_digest_load_from_params(&drbg->digest, params, libctx))214return 0;215216/* Confirm digest is allowed. Allow all digests that are not XOF */217md = ossl_prov_digest_md(&drbg->digest);218if (md != NULL) {219if (EVP_MD_xof(md)) {220ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);221return 0;222}223md_size = EVP_MD_get_size(md);224if (md_size <= 0)225return 0;226drbg->blocklen = (size_t)md_size;227}228return ossl_prov_macctx_load_from_params(&drbg->ctx, params,229"HMAC", NULL, NULL, libctx);230}231return 1;232}233234static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(235ossl_unused void *vctx, ossl_unused void *p_ctx)236{237static const OSSL_PARAM known_settable_ctx_params[] = {238OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0),239OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0),240OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),241OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),242OSSL_PARAM_END243};244return known_settable_ctx_params;245}246247const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = {248{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))hmac_drbg_kdf_new },249{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))hmac_drbg_kdf_free },250{ OSSL_FUNC_KDF_DUPCTX, (void(*)(void))hmac_drbg_kdf_dup },251{ OSSL_FUNC_KDF_RESET, (void(*)(void))hmac_drbg_kdf_reset },252{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))hmac_drbg_kdf_derive },253{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,254(void(*)(void))hmac_drbg_kdf_settable_ctx_params },255{ OSSL_FUNC_KDF_SET_CTX_PARAMS,256(void(*)(void))hmac_drbg_kdf_set_ctx_params },257{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,258(void(*)(void))hmac_drbg_kdf_gettable_ctx_params },259{ OSSL_FUNC_KDF_GET_CTX_PARAMS,260(void(*)(void))hmac_drbg_kdf_get_ctx_params },261OSSL_DISPATCH_END262};263264265