| 1 |
/* |
| 2 |
* Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <openssl/evp.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/core.h> |
| 13 |
#include <openssl/core_dispatch.h> |
| 14 |
#include <openssl/kdf.h> |
| 15 |
#include "internal/provider.h" |
| 16 |
#include "internal/core.h" |
| 17 |
#include "crypto/evp.h" |
| 18 |
#include "evp_local.h" |
| 19 |
|
| 20 |
static int evp_kdf_up_ref(void *vkdf) |
| 21 |
{ |
| 22 |
EVP_KDF *kdf = (EVP_KDF *)vkdf; |
| 23 |
int ref = 0; |
| 24 |
|
| 25 |
CRYPTO_UP_REF(&kdf->refcnt, &ref); |
| 26 |
return 1; |
| 27 |
} |
| 28 |
|
| 29 |
static void evp_kdf_free(void *vkdf) |
| 30 |
{ |
| 31 |
EVP_KDF *kdf = (EVP_KDF *)vkdf; |
| 32 |
int ref = 0; |
| 33 |
|
| 34 |
if (kdf == NULL) |
| 35 |
return; |
| 36 |
|
| 37 |
CRYPTO_DOWN_REF(&kdf->refcnt, &ref); |
| 38 |
if (ref > 0) |
| 39 |
return; |
| 40 |
OPENSSL_free(kdf->type_name); |
| 41 |
ossl_provider_free(kdf->prov); |
| 42 |
CRYPTO_FREE_REF(&kdf->refcnt); |
| 43 |
OPENSSL_free(kdf); |
| 44 |
} |
| 45 |
|
| 46 |
static void *evp_kdf_new(void) |
| 47 |
{ |
| 48 |
EVP_KDF *kdf = NULL; |
| 49 |
|
| 50 |
if ((kdf = OPENSSL_zalloc(sizeof(*kdf))) == NULL |
| 51 |
|| !CRYPTO_NEW_REF(&kdf->refcnt, 1)) { |
| 52 |
OPENSSL_free(kdf); |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
return kdf; |
| 56 |
} |
| 57 |
|
| 58 |
static void *evp_kdf_from_algorithm(int name_id, |
| 59 |
const OSSL_ALGORITHM *algodef, |
| 60 |
OSSL_PROVIDER *prov) |
| 61 |
{ |
| 62 |
const OSSL_DISPATCH *fns = algodef->implementation; |
| 63 |
EVP_KDF *kdf = NULL; |
| 64 |
int fnkdfcnt = 0, fnctxcnt = 0; |
| 65 |
|
| 66 |
if ((kdf = evp_kdf_new()) == NULL) { |
| 67 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
kdf->name_id = name_id; |
| 71 |
if ((kdf->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
| 72 |
goto err; |
| 73 |
|
| 74 |
kdf->description = algodef->algorithm_description; |
| 75 |
|
| 76 |
for (; fns->function_id != 0; fns++) { |
| 77 |
switch (fns->function_id) { |
| 78 |
case OSSL_FUNC_KDF_NEWCTX: |
| 79 |
if (kdf->newctx != NULL) |
| 80 |
break; |
| 81 |
kdf->newctx = OSSL_FUNC_kdf_newctx(fns); |
| 82 |
fnctxcnt++; |
| 83 |
break; |
| 84 |
case OSSL_FUNC_KDF_DUPCTX: |
| 85 |
if (kdf->dupctx != NULL) |
| 86 |
break; |
| 87 |
kdf->dupctx = OSSL_FUNC_kdf_dupctx(fns); |
| 88 |
break; |
| 89 |
case OSSL_FUNC_KDF_FREECTX: |
| 90 |
if (kdf->freectx != NULL) |
| 91 |
break; |
| 92 |
kdf->freectx = OSSL_FUNC_kdf_freectx(fns); |
| 93 |
fnctxcnt++; |
| 94 |
break; |
| 95 |
case OSSL_FUNC_KDF_RESET: |
| 96 |
if (kdf->reset != NULL) |
| 97 |
break; |
| 98 |
kdf->reset = OSSL_FUNC_kdf_reset(fns); |
| 99 |
break; |
| 100 |
case OSSL_FUNC_KDF_DERIVE: |
| 101 |
if (kdf->derive != NULL) |
| 102 |
break; |
| 103 |
kdf->derive = OSSL_FUNC_kdf_derive(fns); |
| 104 |
fnkdfcnt++; |
| 105 |
break; |
| 106 |
case OSSL_FUNC_KDF_GETTABLE_PARAMS: |
| 107 |
if (kdf->gettable_params != NULL) |
| 108 |
break; |
| 109 |
kdf->gettable_params = OSSL_FUNC_kdf_gettable_params(fns); |
| 110 |
break; |
| 111 |
case OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS: |
| 112 |
if (kdf->gettable_ctx_params != NULL) |
| 113 |
break; |
| 114 |
kdf->gettable_ctx_params = OSSL_FUNC_kdf_gettable_ctx_params(fns); |
| 115 |
break; |
| 116 |
case OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS: |
| 117 |
if (kdf->settable_ctx_params != NULL) |
| 118 |
break; |
| 119 |
kdf->settable_ctx_params = OSSL_FUNC_kdf_settable_ctx_params(fns); |
| 120 |
break; |
| 121 |
case OSSL_FUNC_KDF_GET_PARAMS: |
| 122 |
if (kdf->get_params != NULL) |
| 123 |
break; |
| 124 |
kdf->get_params = OSSL_FUNC_kdf_get_params(fns); |
| 125 |
break; |
| 126 |
case OSSL_FUNC_KDF_GET_CTX_PARAMS: |
| 127 |
if (kdf->get_ctx_params != NULL) |
| 128 |
break; |
| 129 |
kdf->get_ctx_params = OSSL_FUNC_kdf_get_ctx_params(fns); |
| 130 |
break; |
| 131 |
case OSSL_FUNC_KDF_SET_CTX_PARAMS: |
| 132 |
if (kdf->set_ctx_params != NULL) |
| 133 |
break; |
| 134 |
kdf->set_ctx_params = OSSL_FUNC_kdf_set_ctx_params(fns); |
| 135 |
break; |
| 136 |
case OSSL_FUNC_KDF_SET_SKEY: |
| 137 |
if (kdf->set_skey != NULL) |
| 138 |
break; |
| 139 |
kdf->set_skey = OSSL_FUNC_kdf_set_skey(fns); |
| 140 |
break; |
| 141 |
case OSSL_FUNC_KDF_DERIVE_SKEY: |
| 142 |
if (kdf->derive_skey != NULL) |
| 143 |
break; |
| 144 |
kdf->derive_skey = OSSL_FUNC_kdf_derive_skey(fns); |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
if (fnkdfcnt != 1 || fnctxcnt != 2) { |
| 149 |
/* |
| 150 |
* In order to be a consistent set of functions we must have at least |
| 151 |
* a derive function, and a complete set of context management |
| 152 |
* functions. |
| 153 |
*/ |
| 154 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
| 155 |
goto err; |
| 156 |
} |
| 157 |
if (prov != NULL && !ossl_provider_up_ref(prov)) |
| 158 |
goto err; |
| 159 |
|
| 160 |
kdf->prov = prov; |
| 161 |
|
| 162 |
return kdf; |
| 163 |
|
| 164 |
err: |
| 165 |
evp_kdf_free(kdf); |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
EVP_KDF *EVP_KDF_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, |
| 170 |
const char *properties) |
| 171 |
{ |
| 172 |
return evp_generic_fetch(libctx, OSSL_OP_KDF, algorithm, properties, |
| 173 |
evp_kdf_from_algorithm, evp_kdf_up_ref, |
| 174 |
evp_kdf_free); |
| 175 |
} |
| 176 |
|
| 177 |
int EVP_KDF_up_ref(EVP_KDF *kdf) |
| 178 |
{ |
| 179 |
return evp_kdf_up_ref(kdf); |
| 180 |
} |
| 181 |
|
| 182 |
void EVP_KDF_free(EVP_KDF *kdf) |
| 183 |
{ |
| 184 |
evp_kdf_free(kdf); |
| 185 |
} |
| 186 |
|
| 187 |
const OSSL_PARAM *EVP_KDF_gettable_params(const EVP_KDF *kdf) |
| 188 |
{ |
| 189 |
if (kdf->gettable_params == NULL) |
| 190 |
return NULL; |
| 191 |
return kdf->gettable_params(ossl_provider_ctx(EVP_KDF_get0_provider(kdf))); |
| 192 |
} |
| 193 |
|
| 194 |
const OSSL_PARAM *EVP_KDF_gettable_ctx_params(const EVP_KDF *kdf) |
| 195 |
{ |
| 196 |
void *alg; |
| 197 |
|
| 198 |
if (kdf->gettable_ctx_params == NULL) |
| 199 |
return NULL; |
| 200 |
alg = ossl_provider_ctx(EVP_KDF_get0_provider(kdf)); |
| 201 |
return kdf->gettable_ctx_params(NULL, alg); |
| 202 |
} |
| 203 |
|
| 204 |
const OSSL_PARAM *EVP_KDF_settable_ctx_params(const EVP_KDF *kdf) |
| 205 |
{ |
| 206 |
void *alg; |
| 207 |
|
| 208 |
if (kdf->settable_ctx_params == NULL) |
| 209 |
return NULL; |
| 210 |
alg = ossl_provider_ctx(EVP_KDF_get0_provider(kdf)); |
| 211 |
return kdf->settable_ctx_params(NULL, alg); |
| 212 |
} |
| 213 |
|
| 214 |
const OSSL_PARAM *EVP_KDF_CTX_gettable_params(EVP_KDF_CTX *ctx) |
| 215 |
{ |
| 216 |
void *alg; |
| 217 |
|
| 218 |
if (ctx->meth->gettable_ctx_params == NULL) |
| 219 |
return NULL; |
| 220 |
alg = ossl_provider_ctx(EVP_KDF_get0_provider(ctx->meth)); |
| 221 |
return ctx->meth->gettable_ctx_params(ctx->algctx, alg); |
| 222 |
} |
| 223 |
|
| 224 |
const OSSL_PARAM *EVP_KDF_CTX_settable_params(EVP_KDF_CTX *ctx) |
| 225 |
{ |
| 226 |
void *alg; |
| 227 |
|
| 228 |
if (ctx->meth->settable_ctx_params == NULL) |
| 229 |
return NULL; |
| 230 |
alg = ossl_provider_ctx(EVP_KDF_get0_provider(ctx->meth)); |
| 231 |
return ctx->meth->settable_ctx_params(ctx->algctx, alg); |
| 232 |
} |
| 233 |
|
| 234 |
void EVP_KDF_do_all_provided(OSSL_LIB_CTX *libctx, |
| 235 |
void (*fn)(EVP_KDF *kdf, void *arg), |
| 236 |
void *arg) |
| 237 |
{ |
| 238 |
evp_generic_do_all(libctx, OSSL_OP_KDF, |
| 239 |
(void (*)(void *, void *))fn, arg, |
| 240 |
evp_kdf_from_algorithm, evp_kdf_up_ref, evp_kdf_free); |
| 241 |
} |