| 1 |
/* |
| 2 |
* Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved. |
| 4 |
* |
| 5 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 6 |
* this file except in compliance with the License. You can obtain a copy |
| 7 |
* in the file LICENSE in the source distribution or at |
| 8 |
* https://www.openssl.org/source/license.html |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include "internal/cryptlib.h" |
| 14 |
#include <openssl/evp.h> |
| 15 |
#include <openssl/kdf.h> |
| 16 |
#include <openssl/core.h> |
| 17 |
#include <openssl/core_names.h> |
| 18 |
#include "crypto/evp.h" |
| 19 |
#include "internal/numbers.h" |
| 20 |
#include "internal/provider.h" |
| 21 |
#include "evp_local.h" |
| 22 |
#include "internal/param_build_set.h" |
| 23 |
|
| 24 |
EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf) |
| 25 |
{ |
| 26 |
EVP_KDF_CTX *ctx = NULL; |
| 27 |
|
| 28 |
if (kdf == NULL) |
| 29 |
return NULL; |
| 30 |
|
| 31 |
ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); |
| 32 |
if (ctx == NULL |
| 33 |
|| (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL |
| 34 |
|| !EVP_KDF_up_ref(kdf)) { |
| 35 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 36 |
if (ctx != NULL) |
| 37 |
kdf->freectx(ctx->algctx); |
| 38 |
OPENSSL_free(ctx); |
| 39 |
ctx = NULL; |
| 40 |
} else { |
| 41 |
ctx->meth = kdf; |
| 42 |
} |
| 43 |
return ctx; |
| 44 |
} |
| 45 |
|
| 46 |
void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) |
| 47 |
{ |
| 48 |
if (ctx == NULL) |
| 49 |
return; |
| 50 |
ctx->meth->freectx(ctx->algctx); |
| 51 |
ctx->algctx = NULL; |
| 52 |
EVP_KDF_free(ctx->meth); |
| 53 |
OPENSSL_free(ctx); |
| 54 |
} |
| 55 |
|
| 56 |
EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src) |
| 57 |
{ |
| 58 |
EVP_KDF_CTX *dst; |
| 59 |
|
| 60 |
if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL) |
| 61 |
return NULL; |
| 62 |
|
| 63 |
dst = OPENSSL_malloc(sizeof(*dst)); |
| 64 |
if (dst == NULL) |
| 65 |
return NULL; |
| 66 |
|
| 67 |
memcpy(dst, src, sizeof(*dst)); |
| 68 |
if (!EVP_KDF_up_ref(dst->meth)) { |
| 69 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 70 |
OPENSSL_free(dst); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
dst->algctx = src->meth->dupctx(src->algctx); |
| 75 |
if (dst->algctx == NULL) { |
| 76 |
EVP_KDF_CTX_free(dst); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
return dst; |
| 80 |
} |
| 81 |
|
| 82 |
int evp_kdf_get_number(const EVP_KDF *kdf) |
| 83 |
{ |
| 84 |
return kdf->name_id; |
| 85 |
} |
| 86 |
|
| 87 |
const char *EVP_KDF_get0_name(const EVP_KDF *kdf) |
| 88 |
{ |
| 89 |
return kdf->type_name; |
| 90 |
} |
| 91 |
|
| 92 |
const char *EVP_KDF_get0_description(const EVP_KDF *kdf) |
| 93 |
{ |
| 94 |
return kdf->description; |
| 95 |
} |
| 96 |
|
| 97 |
int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name) |
| 98 |
{ |
| 99 |
return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name); |
| 100 |
} |
| 101 |
|
| 102 |
const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf) |
| 103 |
{ |
| 104 |
return kdf->prov; |
| 105 |
} |
| 106 |
|
| 107 |
const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx) |
| 108 |
{ |
| 109 |
return ctx->meth; |
| 110 |
} |
| 111 |
|
| 112 |
void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx) |
| 113 |
{ |
| 114 |
if (ctx == NULL) |
| 115 |
return; |
| 116 |
|
| 117 |
if (ctx->meth->reset != NULL) |
| 118 |
ctx->meth->reset(ctx->algctx); |
| 119 |
} |
| 120 |
|
| 121 |
size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx) |
| 122 |
{ |
| 123 |
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 124 |
size_t s = 0; |
| 125 |
|
| 126 |
if (ctx == NULL) |
| 127 |
return 0; |
| 128 |
|
| 129 |
*params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s); |
| 130 |
if (ctx->meth->get_ctx_params != NULL |
| 131 |
&& ctx->meth->get_ctx_params(ctx->algctx, params)) |
| 132 |
return s; |
| 133 |
if (ctx->meth->get_params != NULL |
| 134 |
&& ctx->meth->get_params(params)) |
| 135 |
return s; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, |
| 140 |
const OSSL_PARAM params[]) |
| 141 |
{ |
| 142 |
if (ctx == NULL) |
| 143 |
return 0; |
| 144 |
|
| 145 |
return ctx->meth->derive(ctx->algctx, key, keylen, params); |
| 146 |
} |
| 147 |
|
| 148 |
struct convert_key { |
| 149 |
const char *name; |
| 150 |
OSSL_PARAM *param; |
| 151 |
}; |
| 152 |
|
| 153 |
static int convert_key_cb(const OSSL_PARAM params[], void *arg) |
| 154 |
{ |
| 155 |
struct convert_key *ckey = arg; |
| 156 |
const OSSL_PARAM *raw_bytes; |
| 157 |
unsigned char *data; |
| 158 |
size_t len; |
| 159 |
|
| 160 |
raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES); |
| 161 |
if (raw_bytes == NULL) |
| 162 |
return 0; |
| 163 |
|
| 164 |
if (!OSSL_PARAM_get_octet_string_ptr(raw_bytes, (const void **)&data, &len)) |
| 165 |
return 0; |
| 166 |
|
| 167 |
*ckey->param = OSSL_PARAM_construct_octet_string(ckey->name, data, len); |
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
int EVP_KDF_CTX_set_SKEY(EVP_KDF_CTX *ctx, EVP_SKEY *key, const char *paramname) |
| 172 |
{ |
| 173 |
struct convert_key ckey; |
| 174 |
OSSL_PARAM params[2] = { |
| 175 |
OSSL_PARAM_END, |
| 176 |
OSSL_PARAM_END, |
| 177 |
}; |
| 178 |
|
| 179 |
if (ctx == NULL) |
| 180 |
return 0; |
| 181 |
|
| 182 |
ckey.name = (paramname != NULL) ? paramname : OSSL_KDF_PARAM_KEY; |
| 183 |
|
| 184 |
if (ctx->meth->set_skey != NULL && ctx->meth->prov == key->skeymgmt->prov) |
| 185 |
return ctx->meth->set_skey(ctx->algctx, key->keydata, ckey.name); |
| 186 |
|
| 187 |
/* |
| 188 |
* We can't use the opaque key directly. |
| 189 |
* Let's try to export it and set the ctx params in a traditional manner. |
| 190 |
*/ |
| 191 |
ckey.param = ¶ms[0]; |
| 192 |
|
| 193 |
if (!ctx->meth->set_ctx_params) |
| 194 |
return 0; |
| 195 |
|
| 196 |
if (EVP_SKEY_export(key, OSSL_SKEYMGMT_SELECT_SECRET_KEY, |
| 197 |
convert_key_cb, &ckey)) |
| 198 |
return ctx->meth->set_ctx_params(ctx->algctx, params); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
EVP_SKEY *EVP_KDF_derive_SKEY(EVP_KDF_CTX *ctx, EVP_SKEYMGMT *mgmt, |
| 204 |
const char *key_type, const char *propquery, |
| 205 |
size_t keylen, const OSSL_PARAM params[]) |
| 206 |
{ |
| 207 |
EVP_SKEYMGMT *skeymgmt = NULL; |
| 208 |
EVP_SKEY *ret = NULL; |
| 209 |
|
| 210 |
if (ctx == NULL || key_type == NULL) { |
| 211 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
|
| 215 |
if (mgmt != NULL) { |
| 216 |
skeymgmt = mgmt; |
| 217 |
} else { |
| 218 |
skeymgmt = evp_skeymgmt_fetch_from_prov(ctx->meth->prov, |
| 219 |
key_type, propquery); |
| 220 |
if (skeymgmt == NULL) { |
| 221 |
/* |
| 222 |
* The provider does not support skeymgmt, let's try to fallback |
| 223 |
* to a provider that supports it |
| 224 |
*/ |
| 225 |
skeymgmt = EVP_SKEYMGMT_fetch(ossl_provider_libctx(ctx->meth->prov), |
| 226 |
key_type, propquery); |
| 227 |
} |
| 228 |
|
| 229 |
if (skeymgmt == NULL) { |
| 230 |
ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/* Fallback to raw derive + import if necessary */ |
| 236 |
if (skeymgmt->prov != ctx->meth->prov || ctx->meth->derive_skey == NULL) { |
| 237 |
unsigned char *key = NULL; |
| 238 |
OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 239 |
|
| 240 |
if (ctx->meth->derive == NULL) { |
| 241 |
ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED); |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
key = OPENSSL_zalloc(keylen); |
| 246 |
if (!key) |
| 247 |
return NULL; |
| 248 |
|
| 249 |
if (!ctx->meth->derive(ctx->algctx, key, keylen, params)) { |
| 250 |
OPENSSL_free(key); |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
| 254 |
key, keylen); |
| 255 |
|
| 256 |
ret = EVP_SKEY_import_SKEYMGMT(ossl_provider_libctx(ctx->meth->prov), skeymgmt, |
| 257 |
OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params); |
| 258 |
|
| 259 |
if (mgmt != skeymgmt) |
| 260 |
EVP_SKEYMGMT_free(skeymgmt); |
| 261 |
|
| 262 |
OPENSSL_clear_free(key, keylen); |
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
ret = evp_skey_alloc(skeymgmt); |
| 267 |
if (ret == NULL) { |
| 268 |
if (mgmt != skeymgmt) |
| 269 |
EVP_SKEYMGMT_free(skeymgmt); |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
ret->keydata = ctx->meth->derive_skey(ctx->algctx, key_type, ossl_provider_ctx(skeymgmt->prov), |
| 274 |
skeymgmt->import, keylen, params); |
| 275 |
if (ret->keydata == NULL) { |
| 276 |
EVP_SKEY_free(ret); |
| 277 |
ret = NULL; |
| 278 |
} |
| 279 |
|
| 280 |
if (mgmt != skeymgmt) |
| 281 |
EVP_SKEYMGMT_free(skeymgmt); |
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* The {get,set}_params functions return 1 if there is no corresponding |
| 287 |
* function in the implementation. This is the same as if there was one, |
| 288 |
* but it didn't recognise any of the given params, i.e. nothing in the |
| 289 |
* bag of parameters was useful. |
| 290 |
*/ |
| 291 |
int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]) |
| 292 |
{ |
| 293 |
if (kdf->get_params != NULL) |
| 294 |
return kdf->get_params(params); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]) |
| 299 |
{ |
| 300 |
if (ctx->meth->get_ctx_params != NULL) |
| 301 |
return ctx->meth->get_ctx_params(ctx->algctx, params); |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]) |
| 306 |
{ |
| 307 |
if (ctx->meth->set_ctx_params != NULL) |
| 308 |
return ctx->meth->set_ctx_params(ctx->algctx, params); |
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
int EVP_KDF_names_do_all(const EVP_KDF *kdf, |
| 313 |
void (*fn)(const char *name, void *data), |
| 314 |
void *data) |
| 315 |
{ |
| 316 |
if (kdf->prov != NULL) |
| 317 |
return evp_names_do_all(kdf->prov, kdf->name_id, fn, data); |
| 318 |
|
| 319 |
return 1; |
| 320 |
} |