| 1 |
/* |
| 2 |
* Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright 2019 Red Hat, Inc. |
| 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 |
/* clang-format off */ |
| 11 |
{- |
| 12 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 13 |
-} |
| 14 |
/* clang-format on */ |
| 15 |
|
| 16 |
/* |
| 17 |
* This implements https://csrc.nist.gov/publications/detail/sp/800-108/final |
| 18 |
* section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC |
| 19 |
* and CMAC. That document does not name the KDFs it defines; the name is |
| 20 |
* derived from |
| 21 |
* https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation |
| 22 |
* |
| 23 |
* Note that section 5.3 ("double-pipeline mode") is not implemented, though |
| 24 |
* it would be possible to do so in the future. |
| 25 |
* |
| 26 |
* These versions all assume the counter is used. It would be relatively |
| 27 |
* straightforward to expose a configuration handle should the need arise. |
| 28 |
* |
| 29 |
* Variable names attempt to match those of SP800-108. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <stdarg.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include <openssl/core_names.h> |
| 37 |
#include <openssl/evp.h> |
| 38 |
#include <openssl/hmac.h> |
| 39 |
#include <openssl/kdf.h> |
| 40 |
#include <openssl/params.h> |
| 41 |
#include <openssl/proverr.h> |
| 42 |
|
| 43 |
#include "internal/cryptlib.h" |
| 44 |
#include "crypto/evp.h" |
| 45 |
#include "internal/numbers.h" |
| 46 |
#include "internal/endian.h" |
| 47 |
#include "prov/implementations.h" |
| 48 |
#include "prov/provider_ctx.h" |
| 49 |
#include "prov/provider_util.h" |
| 50 |
#include "prov/providercommon.h" |
| 51 |
#include "prov/securitycheck.h" |
| 52 |
#include "internal/e_os.h" |
| 53 |
#include "internal/params.h" |
| 54 |
|
| 55 |
#define ossl_min(a, b) ((a) < (b)) ? (a) : (b) |
| 56 |
|
| 57 |
#define KBKDF_MAX_INFOS 5 |
| 58 |
|
| 59 |
typedef enum { |
| 60 |
COUNTER = 0, |
| 61 |
FEEDBACK |
| 62 |
} kbkdf_mode; |
| 63 |
|
| 64 |
/* Our context structure. */ |
| 65 |
typedef struct { |
| 66 |
void *provctx; |
| 67 |
kbkdf_mode mode; |
| 68 |
EVP_MAC_CTX *ctx_init; |
| 69 |
|
| 70 |
/* Names are lowercased versions of those found in SP800-108. */ |
| 71 |
int r; |
| 72 |
unsigned char *ki; |
| 73 |
size_t ki_len; |
| 74 |
unsigned char *label; |
| 75 |
size_t label_len; |
| 76 |
unsigned char *context; |
| 77 |
size_t context_len; |
| 78 |
unsigned char *iv; |
| 79 |
size_t iv_len; |
| 80 |
int use_l; |
| 81 |
int is_kmac; |
| 82 |
int use_separator; |
| 83 |
OSSL_FIPS_IND_DECLARE |
| 84 |
} KBKDF; |
| 85 |
|
| 86 |
/* Definitions needed for typechecking. */ |
| 87 |
static OSSL_FUNC_kdf_newctx_fn kbkdf_new; |
| 88 |
static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup; |
| 89 |
static OSSL_FUNC_kdf_freectx_fn kbkdf_free; |
| 90 |
static OSSL_FUNC_kdf_reset_fn kbkdf_reset; |
| 91 |
static OSSL_FUNC_kdf_derive_fn kbkdf_derive; |
| 92 |
static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params; |
| 93 |
static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params; |
| 94 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params; |
| 95 |
static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params; |
| 96 |
|
| 97 |
/* Not all platforms have htobe32(). */ |
| 98 |
static uint32_t be32(uint32_t host) |
| 99 |
{ |
| 100 |
uint32_t big = 0; |
| 101 |
DECLARE_IS_ENDIAN; |
| 102 |
|
| 103 |
if (!IS_LITTLE_ENDIAN) |
| 104 |
return host; |
| 105 |
|
| 106 |
big |= (host & 0xff000000) >> 24; |
| 107 |
big |= (host & 0x00ff0000) >> 8; |
| 108 |
big |= (host & 0x0000ff00) << 8; |
| 109 |
big |= (host & 0x000000ff) << 24; |
| 110 |
return big; |
| 111 |
} |
| 112 |
|
| 113 |
static void init(KBKDF *ctx) |
| 114 |
{ |
| 115 |
ctx->r = 32; |
| 116 |
ctx->use_l = 1; |
| 117 |
ctx->use_separator = 1; |
| 118 |
ctx->is_kmac = 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void *kbkdf_new(void *provctx) |
| 122 |
{ |
| 123 |
KBKDF *ctx; |
| 124 |
|
| 125 |
if (!ossl_prov_is_running()) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 129 |
if (ctx == NULL) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
ctx->provctx = provctx; |
| 133 |
OSSL_FIPS_IND_INIT(ctx) |
| 134 |
init(ctx); |
| 135 |
return ctx; |
| 136 |
} |
| 137 |
|
| 138 |
static void kbkdf_free(void *vctx) |
| 139 |
{ |
| 140 |
KBKDF *ctx = (KBKDF *)vctx; |
| 141 |
|
| 142 |
if (ctx != NULL) { |
| 143 |
kbkdf_reset(ctx); |
| 144 |
OPENSSL_free(ctx); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
static void kbkdf_reset(void *vctx) |
| 149 |
{ |
| 150 |
KBKDF *ctx = (KBKDF *)vctx; |
| 151 |
void *provctx = ctx->provctx; |
| 152 |
|
| 153 |
EVP_MAC_CTX_free(ctx->ctx_init); |
| 154 |
OPENSSL_clear_free(ctx->context, ctx->context_len); |
| 155 |
OPENSSL_clear_free(ctx->label, ctx->label_len); |
| 156 |
OPENSSL_clear_free(ctx->ki, ctx->ki_len); |
| 157 |
OPENSSL_clear_free(ctx->iv, ctx->iv_len); |
| 158 |
memset(ctx, 0, sizeof(*ctx)); |
| 159 |
ctx->provctx = provctx; |
| 160 |
init(ctx); |
| 161 |
} |
| 162 |
|
| 163 |
static void *kbkdf_dup(void *vctx) |
| 164 |
{ |
| 165 |
const KBKDF *src = (const KBKDF *)vctx; |
| 166 |
KBKDF *dest; |
| 167 |
|
| 168 |
dest = kbkdf_new(src->provctx); |
| 169 |
if (dest != NULL) { |
| 170 |
dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init); |
| 171 |
if (dest->ctx_init == NULL |
| 172 |
|| !ossl_prov_memdup(src->ki, src->ki_len, |
| 173 |
&dest->ki, &dest->ki_len) |
| 174 |
|| !ossl_prov_memdup(src->label, src->label_len, |
| 175 |
&dest->label, &dest->label_len) |
| 176 |
|| !ossl_prov_memdup(src->context, src->context_len, |
| 177 |
&dest->context, &dest->context_len) |
| 178 |
|| !ossl_prov_memdup(src->iv, src->iv_len, |
| 179 |
&dest->iv, &dest->iv_len)) |
| 180 |
goto err; |
| 181 |
dest->mode = src->mode; |
| 182 |
dest->r = src->r; |
| 183 |
dest->use_l = src->use_l; |
| 184 |
dest->use_separator = src->use_separator; |
| 185 |
dest->is_kmac = src->is_kmac; |
| 186 |
OSSL_FIPS_IND_COPY(dest, src) |
| 187 |
} |
| 188 |
return dest; |
| 189 |
|
| 190 |
err: |
| 191 |
kbkdf_free(dest); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
#ifdef FIPS_MODULE |
| 196 |
static int fips_kbkdf_key_check_passed(KBKDF *ctx) |
| 197 |
{ |
| 198 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 199 |
int key_approved = ossl_kdf_check_key_size(ctx->ki_len); |
| 200 |
|
| 201 |
if (!key_approved) { |
| 202 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
| 203 |
libctx, "KBKDF", "Key size", |
| 204 |
ossl_fips_config_kbkdf_key_check)) { |
| 205 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
} |
| 209 |
return 1; |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
/* SP800-108 section 5.1 or section 5.2 depending on mode. */ |
| 214 |
static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv, |
| 215 |
size_t iv_len, unsigned char *label, size_t label_len, |
| 216 |
unsigned char *context, size_t context_len, |
| 217 |
unsigned char *k_i, size_t h, uint32_t l, int has_separator, |
| 218 |
unsigned char *ko, size_t ko_len, int r) |
| 219 |
{ |
| 220 |
int ret = 0; |
| 221 |
EVP_MAC_CTX *ctx = NULL; |
| 222 |
size_t written = 0, to_write, k_i_len = iv_len; |
| 223 |
const unsigned char zero = 0; |
| 224 |
uint32_t counter, i; |
| 225 |
/* |
| 226 |
* From SP800-108: |
| 227 |
* The fixed input data is a concatenation of a Label, |
| 228 |
* a separation indicator 0x00, the Context, and L. |
| 229 |
* One or more of these fixed input data fields may be omitted. |
| 230 |
* |
| 231 |
* has_separator == 0 means that the separator is omitted. |
| 232 |
* Passing a value of l == 0 means that L is omitted. |
| 233 |
* The Context and L are omitted automatically if a NULL buffer is passed. |
| 234 |
*/ |
| 235 |
int has_l = (l != 0); |
| 236 |
|
| 237 |
/* Setup K(0) for feedback mode. */ |
| 238 |
if (iv_len > 0) |
| 239 |
memcpy(k_i, iv, iv_len); |
| 240 |
|
| 241 |
for (counter = 1; written < ko_len; counter++) { |
| 242 |
i = be32(counter); |
| 243 |
|
| 244 |
ctx = EVP_MAC_CTX_dup(ctx_init); |
| 245 |
if (ctx == NULL) |
| 246 |
goto done; |
| 247 |
|
| 248 |
/* Perform feedback, if appropriate. */ |
| 249 |
if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len)) |
| 250 |
goto done; |
| 251 |
|
| 252 |
if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8) |
| 253 |
|| !EVP_MAC_update(ctx, label, label_len) |
| 254 |
|| (has_separator && !EVP_MAC_update(ctx, &zero, 1)) |
| 255 |
|| !EVP_MAC_update(ctx, context, context_len) |
| 256 |
|| (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4)) |
| 257 |
|| !EVP_MAC_final(ctx, k_i, NULL, h)) |
| 258 |
goto done; |
| 259 |
|
| 260 |
to_write = ko_len - written; |
| 261 |
memcpy(ko + written, k_i, ossl_min(to_write, h)); |
| 262 |
written += h; |
| 263 |
|
| 264 |
k_i_len = h; |
| 265 |
EVP_MAC_CTX_free(ctx); |
| 266 |
ctx = NULL; |
| 267 |
} |
| 268 |
|
| 269 |
ret = 1; |
| 270 |
done: |
| 271 |
EVP_MAC_CTX_free(ctx); |
| 272 |
return ret; |
| 273 |
} |
| 274 |
|
| 275 |
/* This must be run before the key is set */ |
| 276 |
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen) |
| 277 |
{ |
| 278 |
OSSL_PARAM params[2]; |
| 279 |
|
| 280 |
if (custom == NULL || customlen == 0) |
| 281 |
return 1; |
| 282 |
params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, |
| 283 |
(void *)custom, customlen); |
| 284 |
params[1] = OSSL_PARAM_construct_end(); |
| 285 |
return EVP_MAC_CTX_set_params(ctx, params) > 0; |
| 286 |
} |
| 287 |
|
| 288 |
static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen, |
| 289 |
const unsigned char *context, size_t contextlen) |
| 290 |
{ |
| 291 |
OSSL_PARAM params[2]; |
| 292 |
|
| 293 |
params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen); |
| 294 |
params[1] = OSSL_PARAM_construct_end(); |
| 295 |
return EVP_MAC_CTX_set_params(ctx, params) > 0 |
| 296 |
&& EVP_MAC_update(ctx, context, contextlen) |
| 297 |
&& EVP_MAC_final(ctx, out, NULL, outlen); |
| 298 |
} |
| 299 |
|
| 300 |
static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 301 |
const OSSL_PARAM params[]) |
| 302 |
{ |
| 303 |
KBKDF *ctx = (KBKDF *)vctx; |
| 304 |
int ret = 0; |
| 305 |
unsigned char *k_i = NULL; |
| 306 |
uint32_t l = 0; |
| 307 |
size_t h = 0; |
| 308 |
uint64_t counter_max; |
| 309 |
|
| 310 |
if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params)) |
| 311 |
return 0; |
| 312 |
|
| 313 |
/* label, context, and iv are permitted to be empty. Check everything |
| 314 |
* else. */ |
| 315 |
if (ctx->ctx_init == NULL) { |
| 316 |
if (ctx->ki_len == 0 || ctx->ki == NULL) { |
| 317 |
ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
/* Could either be missing MAC or missing message digest or missing |
| 321 |
* cipher - arbitrarily, I pick this one. */ |
| 322 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
/* Fail if the output length is zero */ |
| 327 |
if (keylen == 0) { |
| 328 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
if (ctx->is_kmac) { |
| 333 |
ret = kmac_derive(ctx->ctx_init, key, keylen, |
| 334 |
ctx->context, ctx->context_len); |
| 335 |
goto done; |
| 336 |
} |
| 337 |
|
| 338 |
h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init); |
| 339 |
if (h == 0) |
| 340 |
goto done; |
| 341 |
|
| 342 |
if (ctx->iv_len != 0 && ctx->iv_len != h) { |
| 343 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
| 344 |
goto done; |
| 345 |
} |
| 346 |
|
| 347 |
if (ctx->mode == COUNTER) { |
| 348 |
/* Fail if keylen is too large for r */ |
| 349 |
counter_max = (uint64_t)1 << (uint64_t)ctx->r; |
| 350 |
if ((uint64_t)(keylen / h) >= counter_max) { |
| 351 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 352 |
goto done; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
if (ctx->use_l != 0) |
| 357 |
l = be32((uint32_t)(keylen * 8)); |
| 358 |
|
| 359 |
k_i = OPENSSL_zalloc(h); |
| 360 |
if (k_i == NULL) |
| 361 |
goto done; |
| 362 |
|
| 363 |
ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label, |
| 364 |
ctx->label_len, ctx->context, ctx->context_len, k_i, h, l, |
| 365 |
ctx->use_separator, key, keylen, ctx->r); |
| 366 |
done: |
| 367 |
if (ret != 1) |
| 368 |
OPENSSL_cleanse(key, keylen); |
| 369 |
OPENSSL_clear_free(k_i, h); |
| 370 |
return ret; |
| 371 |
} |
| 372 |
|
| 373 |
/* clang-format off */ |
| 374 |
{- produce_param_decoder('kbkdf_set_ctx_params', |
| 375 |
(['OSSL_KDF_PARAM_INFO', 'info', 'octet_string', KBKDF_MAX_INFOS], |
| 376 |
['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'], |
| 377 |
['OSSL_KDF_PARAM_KEY', 'key', 'octet_string'], |
| 378 |
['OSSL_KDF_PARAM_SEED', 'seed', 'octet_string'], |
| 379 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 380 |
['OSSL_KDF_PARAM_CIPHER', 'cipher', 'utf8_string'], |
| 381 |
['OSSL_KDF_PARAM_MAC', 'mac', 'utf8_string'], |
| 382 |
['OSSL_KDF_PARAM_MODE', 'mode', 'utf8_string'], |
| 383 |
['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 384 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 385 |
['OSSL_KDF_PARAM_KBKDF_USE_L', 'use_l', 'int'], |
| 386 |
['OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR', 'sep', 'int'], |
| 387 |
['OSSL_KDF_PARAM_KBKDF_R', 'r', 'int'], |
| 388 |
['OSSL_KDF_PARAM_FIPS_KEY_CHECK', 'ind_k', 'int', 'fips'], |
| 389 |
)); -} |
| 390 |
/* clang-format on */ |
| 391 |
|
| 392 |
static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 393 |
{ |
| 394 |
KBKDF *ctx = (KBKDF *)vctx; |
| 395 |
OSSL_LIB_CTX *libctx; |
| 396 |
struct kbkdf_set_ctx_params_st p; |
| 397 |
const char *s; |
| 398 |
|
| 399 |
if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p)) |
| 400 |
return 0; |
| 401 |
|
| 402 |
libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 403 |
|
| 404 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
| 405 |
return 0; |
| 406 |
|
| 407 |
if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher, |
| 408 |
p.digest, p.propq, p.engine, |
| 409 |
NULL, NULL, NULL, libctx)) |
| 410 |
return 0; |
| 411 |
|
| 412 |
if (ctx->ctx_init != NULL) { |
| 413 |
ctx->is_kmac = 0; |
| 414 |
if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init), |
| 415 |
OSSL_MAC_NAME_KMAC128) |
| 416 |
|| EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init), |
| 417 |
OSSL_MAC_NAME_KMAC256)) { |
| 418 |
ctx->is_kmac = 1; |
| 419 |
} else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init), |
| 420 |
OSSL_MAC_NAME_HMAC) |
| 421 |
&& !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init), |
| 422 |
OSSL_MAC_NAME_CMAC)) { |
| 423 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC); |
| 424 |
return 0; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if (p.mode != NULL) { |
| 429 |
if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s)) |
| 430 |
return 0; |
| 431 |
if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) { |
| 432 |
ctx->mode = COUNTER; |
| 433 |
} else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) { |
| 434 |
ctx->mode = FEEDBACK; |
| 435 |
} else { |
| 436 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
| 437 |
return 0; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki, |
| 442 |
&ctx->ki_len) |
| 443 |
== 0) |
| 444 |
return 0; |
| 445 |
#ifdef FIPS_MODULE |
| 446 |
if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx)) |
| 447 |
return 0; |
| 448 |
#endif |
| 449 |
|
| 450 |
if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label, |
| 451 |
&ctx->label_len) |
| 452 |
== 0) |
| 453 |
return 0; |
| 454 |
|
| 455 |
if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context, |
| 456 |
&ctx->context_len) |
| 457 |
== 0) |
| 458 |
return 0; |
| 459 |
|
| 460 |
if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv, |
| 461 |
&ctx->iv_len) |
| 462 |
== 0) |
| 463 |
return 0; |
| 464 |
|
| 465 |
if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l)) |
| 466 |
return 0; |
| 467 |
|
| 468 |
if (p.r != NULL) { |
| 469 |
int new_r = 0; |
| 470 |
|
| 471 |
if (!OSSL_PARAM_get_int(p.r, &new_r)) |
| 472 |
return 0; |
| 473 |
if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32) |
| 474 |
return 0; |
| 475 |
ctx->r = new_r; |
| 476 |
} |
| 477 |
|
| 478 |
if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator)) |
| 479 |
return 0; |
| 480 |
|
| 481 |
/* Set up digest context, if we can. */ |
| 482 |
if (ctx->ctx_init != NULL && ctx->ki_len != 0) { |
| 483 |
if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len)) |
| 484 |
|| !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL)) |
| 485 |
return 0; |
| 486 |
} |
| 487 |
return 1; |
| 488 |
} |
| 489 |
|
| 490 |
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx, |
| 491 |
ossl_unused void *provctx) |
| 492 |
{ |
| 493 |
return kbkdf_set_ctx_params_list; |
| 494 |
} |
| 495 |
|
| 496 |
/* clang-format off */ |
| 497 |
{- produce_param_decoder('kbkdf_get_ctx_params', |
| 498 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 499 |
['OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR', 'ind', 'int', 'fips'], |
| 500 |
)); -} |
| 501 |
/* clang-format on */ |
| 502 |
|
| 503 |
static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 504 |
{ |
| 505 |
struct kbkdf_get_ctx_params_st p; |
| 506 |
KBKDF *ctx = (KBKDF *)vctx; |
| 507 |
|
| 508 |
if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p)) |
| 509 |
return 0; |
| 510 |
|
| 511 |
/* KBKDF can produce results as large as you like. */ |
| 512 |
if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
| 513 |
return 0; |
| 514 |
|
| 515 |
if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
| 516 |
return 0; |
| 517 |
return 1; |
| 518 |
} |
| 519 |
|
| 520 |
static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx, |
| 521 |
ossl_unused void *provctx) |
| 522 |
{ |
| 523 |
return kbkdf_get_ctx_params_list; |
| 524 |
} |
| 525 |
|
| 526 |
const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = { |
| 527 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kbkdf_new }, |
| 528 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kbkdf_dup }, |
| 529 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))kbkdf_free }, |
| 530 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))kbkdf_reset }, |
| 531 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))kbkdf_derive }, |
| 532 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 533 |
(void (*)(void))kbkdf_settable_ctx_params }, |
| 534 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kbkdf_set_ctx_params }, |
| 535 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 536 |
(void (*)(void))kbkdf_gettable_ctx_params }, |
| 537 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kbkdf_get_ctx_params }, |
| 538 |
OSSL_DISPATCH_END, |
| 539 |
}; |