Path: blob/main/crypto/openssl/providers/implementations/kdfs/pvkkdf.c
48383 views
/*1* Copyright 2018-2023 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 <openssl/evp.h>10#include <openssl/core_names.h>11#include <openssl/proverr.h>12#include <openssl/err.h>13#include "internal/numbers.h" /* SIZE_MAX */14#include "prov/provider_ctx.h"15#include "prov/providercommon.h"16#include "prov/implementations.h"17#include "prov/provider_util.h"1819static OSSL_FUNC_kdf_newctx_fn kdf_pvk_new;20static OSSL_FUNC_kdf_dupctx_fn kdf_pvk_dup;21static OSSL_FUNC_kdf_freectx_fn kdf_pvk_free;22static OSSL_FUNC_kdf_reset_fn kdf_pvk_reset;23static OSSL_FUNC_kdf_derive_fn kdf_pvk_derive;24static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pvk_settable_ctx_params;25static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pvk_set_ctx_params;26static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pvk_gettable_ctx_params;27static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pvk_get_ctx_params;2829typedef struct {30void *provctx;31unsigned char *pass;32size_t pass_len;33unsigned char *salt;34size_t salt_len;35PROV_DIGEST digest;36} KDF_PVK;3738static void kdf_pvk_init(KDF_PVK *ctx);3940static void *kdf_pvk_new(void *provctx)41{42KDF_PVK *ctx;4344if (!ossl_prov_is_running())45return NULL;4647ctx = OPENSSL_zalloc(sizeof(*ctx));48if (ctx == NULL)49return NULL;50ctx->provctx = provctx;51kdf_pvk_init(ctx);52return ctx;53}5455static void kdf_pvk_cleanup(KDF_PVK *ctx)56{57ossl_prov_digest_reset(&ctx->digest);58OPENSSL_free(ctx->salt);59OPENSSL_clear_free(ctx->pass, ctx->pass_len);60OPENSSL_cleanse(ctx, sizeof(*ctx));61}6263static void kdf_pvk_free(void *vctx)64{65KDF_PVK *ctx = (KDF_PVK *)vctx;6667if (ctx != NULL) {68kdf_pvk_cleanup(ctx);69OPENSSL_free(ctx);70}71}7273static void *kdf_pvk_dup(void *vctx)74{75const KDF_PVK *src = (const KDF_PVK *)vctx;76KDF_PVK *dest;7778dest = kdf_pvk_new(src->provctx);79if (dest != NULL)80if (!ossl_prov_memdup(src->salt, src->salt_len,81&dest->salt, &dest->salt_len)82|| !ossl_prov_memdup(src->pass, src->pass_len,83&dest->pass , &dest->pass_len)84|| !ossl_prov_digest_copy(&dest->digest, &src->digest))85goto err;86return dest;8788err:89kdf_pvk_free(dest);90return NULL;91}9293static void kdf_pvk_reset(void *vctx)94{95KDF_PVK *ctx = (KDF_PVK *)vctx;96void *provctx = ctx->provctx;9798kdf_pvk_cleanup(ctx);99ctx->provctx = provctx;100kdf_pvk_init(ctx);101}102103static void kdf_pvk_init(KDF_PVK *ctx)104{105OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };106OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);107108params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,109SN_sha1, 0);110if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))111/* This is an error, but there is no way to indicate such directly */112ossl_prov_digest_reset(&ctx->digest);113}114115static int pvk_set_membuf(unsigned char **buffer, size_t *buflen,116const OSSL_PARAM *p)117{118OPENSSL_clear_free(*buffer, *buflen);119*buffer = NULL;120*buflen = 0;121122if (p->data_size == 0) {123if ((*buffer = OPENSSL_malloc(1)) == NULL)124return 0;125} else if (p->data != NULL) {126if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))127return 0;128}129return 1;130}131132static int kdf_pvk_derive(void *vctx, unsigned char *key, size_t keylen,133const OSSL_PARAM params[])134{135KDF_PVK *ctx = (KDF_PVK *)vctx;136const EVP_MD *md;137EVP_MD_CTX *mctx;138int res;139140if (!ossl_prov_is_running() || !kdf_pvk_set_ctx_params(ctx, params))141return 0;142143if (ctx->pass == NULL) {144ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);145return 0;146}147148if (ctx->salt == NULL) {149ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);150return 0;151}152153md = ossl_prov_digest_md(&ctx->digest);154if (md == NULL) {155ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);156return 0;157}158res = EVP_MD_get_size(md);159if (res <= 0) {160ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);161return 0;162}163if ((size_t)res > keylen) {164ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);165return 0;166}167168mctx = EVP_MD_CTX_new();169res = mctx != NULL170&& EVP_DigestInit_ex(mctx, md, NULL)171&& EVP_DigestUpdate(mctx, ctx->salt, ctx->salt_len)172&& EVP_DigestUpdate(mctx, ctx->pass, ctx->pass_len)173&& EVP_DigestFinal_ex(mctx, key, NULL);174EVP_MD_CTX_free(mctx);175return res;176}177178static int kdf_pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])179{180const OSSL_PARAM *p;181KDF_PVK *ctx = vctx;182OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);183184if (ossl_param_is_empty(params))185return 1;186187if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))188return 0;189190if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)191if (!pvk_set_membuf(&ctx->pass, &ctx->pass_len, p))192return 0;193194if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {195if (!pvk_set_membuf(&ctx->salt, &ctx->salt_len, p))196return 0;197}198199return 1;200}201202static const OSSL_PARAM *kdf_pvk_settable_ctx_params(ossl_unused void *ctx,203ossl_unused void *p_ctx)204{205static const OSSL_PARAM known_settable_ctx_params[] = {206OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),207OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),208OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),209OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),210OSSL_PARAM_END211};212return known_settable_ctx_params;213}214215static int kdf_pvk_get_ctx_params(void *vctx, OSSL_PARAM params[])216{217OSSL_PARAM *p;218219if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)220return OSSL_PARAM_set_size_t(p, SIZE_MAX);221return -2;222}223224static const OSSL_PARAM *kdf_pvk_gettable_ctx_params(ossl_unused void *ctx,225ossl_unused void *p_ctx)226{227static const OSSL_PARAM known_gettable_ctx_params[] = {228OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),229OSSL_PARAM_END230};231return known_gettable_ctx_params;232}233234const OSSL_DISPATCH ossl_kdf_pvk_functions[] = {235{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pvk_new },236{ OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pvk_dup },237{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pvk_free },238{ OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pvk_reset },239{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pvk_derive },240{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,241(void(*)(void))kdf_pvk_settable_ctx_params },242{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pvk_set_ctx_params },243{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,244(void(*)(void))kdf_pvk_gettable_ctx_params },245{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pvk_get_ctx_params },246OSSL_DISPATCH_END247};248249250