Path: blob/main/crypto/openssl/providers/implementations/kdfs/hmacdrbg_kdf.c
108604 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)80{81if (src->ctx != NULL) {82dst->ctx = EVP_MAC_CTX_dup(src->ctx);83if (dst->ctx == NULL)84return 0;85}86if (!ossl_prov_digest_copy(&dst->digest, &src->digest))87return 0;88memcpy(dst->K, src->K, sizeof(dst->K));89memcpy(dst->V, src->V, sizeof(dst->V));90dst->blocklen = src->blocklen;91return 1;92}9394static void *hmac_drbg_kdf_dup(void *vctx)95{96const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;97KDF_HMAC_DRBG *dst;9899dst = hmac_drbg_kdf_new(src->provctx);100if (dst != NULL) {101if (!ossl_drbg_hmac_dup(&dst->base, &src->base)102|| !ossl_prov_memdup(src->entropy, src->entropylen,103&dst->entropy, &dst->entropylen)104|| !ossl_prov_memdup(src->nonce, src->noncelen,105&dst->nonce, &dst->noncelen))106goto err;107dst->init = src->init;108}109return dst;110111err:112hmac_drbg_kdf_free(dst);113return NULL;114}115116static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,117const OSSL_PARAM params[])118{119KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;120PROV_DRBG_HMAC *drbg = &ctx->base;121122if (!ossl_prov_is_running()123|| !hmac_drbg_kdf_set_ctx_params(vctx, params))124return 0;125if (!ctx->init) {126if (ctx->entropy == NULL127|| ctx->entropylen == 0128|| ctx->nonce == NULL129|| ctx->noncelen == 0130|| !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,131ctx->nonce, ctx->noncelen, NULL, 0))132return 0;133ctx->init = 1;134}135136return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);137}138139static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])140{141KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;142PROV_DRBG_HMAC *drbg = &hmac->base;143const char *name;144const EVP_MD *md;145OSSL_PARAM *p;146147p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_MAC);148if (p != NULL) {149if (drbg->ctx == NULL)150return 0;151name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));152if (!OSSL_PARAM_set_utf8_string(p, name))153return 0;154}155156p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_DIGEST);157if (p != NULL) {158md = ossl_prov_digest_md(&drbg->digest);159if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))160return 0;161}162return 1;163}164165static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(166ossl_unused void *vctx, ossl_unused void *p_ctx)167{168static const OSSL_PARAM known_gettable_ctx_params[] = {169OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),170OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),171OSSL_PARAM_END172};173return known_gettable_ctx_params;174}175176static int hmac_drbg_kdf_set_ctx_params(void *vctx,177const OSSL_PARAM params[])178{179KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;180PROV_DRBG_HMAC *drbg = &hmac->base;181OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx);182const EVP_MD *md;183const OSSL_PARAM *p;184void *ptr = NULL;185size_t size = 0;186int md_size;187188if (ossl_param_is_empty(params))189return 1;190191p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_ENTROPY);192if (p != NULL) {193if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))194return 0;195OPENSSL_free(hmac->entropy);196hmac->entropy = ptr;197hmac->entropylen = size;198hmac->init = 0;199ptr = NULL;200}201202p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_NONCE);203if (p != NULL) {204if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))205return 0;206OPENSSL_free(hmac->nonce);207hmac->nonce = ptr;208hmac->noncelen = size;209hmac->init = 0;210}211212p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);213if (p != NULL) {214if (!ossl_prov_digest_load_from_params(&drbg->digest, params, libctx))215return 0;216217/* Confirm digest is allowed. Allow all digests that are not XOF */218md = ossl_prov_digest_md(&drbg->digest);219if (md != NULL) {220if (EVP_MD_xof(md)) {221ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);222return 0;223}224md_size = EVP_MD_get_size(md);225if (md_size <= 0)226return 0;227drbg->blocklen = (size_t)md_size;228}229return ossl_prov_macctx_load_from_params(&drbg->ctx, params,230"HMAC", NULL, NULL, libctx);231}232return 1;233}234235static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(236ossl_unused void *vctx, ossl_unused void *p_ctx)237{238static const OSSL_PARAM known_settable_ctx_params[] = {239OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0),240OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0),241OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),242OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),243OSSL_PARAM_END244};245return known_settable_ctx_params;246}247248const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = {249{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))hmac_drbg_kdf_new },250{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))hmac_drbg_kdf_free },251{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))hmac_drbg_kdf_dup },252{ OSSL_FUNC_KDF_RESET, (void (*)(void))hmac_drbg_kdf_reset },253{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))hmac_drbg_kdf_derive },254{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,255(void (*)(void))hmac_drbg_kdf_settable_ctx_params },256{ OSSL_FUNC_KDF_SET_CTX_PARAMS,257(void (*)(void))hmac_drbg_kdf_set_ctx_params },258{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,259(void (*)(void))hmac_drbg_kdf_gettable_ctx_params },260{ OSSL_FUNC_KDF_GET_CTX_PARAMS,261(void (*)(void))hmac_drbg_kdf_get_ctx_params },262OSSL_DISPATCH_END263};264265266