| 1 |
/* |
| 2 |
* Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright (c) 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 |
/* clang-format off */ |
| 11 |
{- |
| 12 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 13 |
-} |
| 14 |
/* clang-format on */ |
| 15 |
|
| 16 |
/* |
| 17 |
* Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
| 18 |
* Section 4.1. |
| 19 |
* |
| 20 |
* The Single Step KDF algorithm is given by: |
| 21 |
* |
| 22 |
* Result(0) = empty bit string (i.e., the null string). |
| 23 |
* For i = 1 to reps, do the following: |
| 24 |
* Increment counter by 1. |
| 25 |
* Result(i) = Result(i - 1) || H(counter || Z || FixedInfo). |
| 26 |
* DKM = LeftmostBits(Result(reps), L)) |
| 27 |
* |
| 28 |
* NOTES: |
| 29 |
* Z is a shared secret required to produce the derived key material. |
| 30 |
* counter is a 4 byte buffer. |
| 31 |
* FixedInfo is a bit string containing context specific data. |
| 32 |
* DKM is the output derived key material. |
| 33 |
* L is the required size of the DKM. |
| 34 |
* reps = [L / H_outputBits] |
| 35 |
* H(x) is the auxiliary function that can be either a hash, HMAC or KMAC. |
| 36 |
* H_outputBits is the length of the output of the auxiliary function H(x). |
| 37 |
* |
| 38 |
* Currently there is not a comprehensive list of test vectors for this |
| 39 |
* algorithm, especially for H(x) = HMAC and H(x) = KMAC. |
| 40 |
* Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests. |
| 41 |
*/ |
| 42 |
#include <stdlib.h> |
| 43 |
#include <stdarg.h> |
| 44 |
#include <string.h> |
| 45 |
#include <openssl/hmac.h> |
| 46 |
#include <openssl/evp.h> |
| 47 |
#include <openssl/kdf.h> |
| 48 |
#include <openssl/core_names.h> |
| 49 |
#include <openssl/params.h> |
| 50 |
#include <openssl/proverr.h> |
| 51 |
#include "internal/cryptlib.h" |
| 52 |
#include "internal/numbers.h" |
| 53 |
#include "crypto/evp.h" |
| 54 |
#include "prov/provider_ctx.h" |
| 55 |
#include "prov/providercommon.h" |
| 56 |
#include "prov/implementations.h" |
| 57 |
#include "prov/provider_util.h" |
| 58 |
#include "prov/securitycheck.h" |
| 59 |
#include "internal/params.h" |
| 60 |
|
| 61 |
typedef struct { |
| 62 |
void *provctx; |
| 63 |
EVP_MAC_CTX *macctx; /* H(x) = HMAC_hash OR H(x) = KMAC */ |
| 64 |
PROV_DIGEST digest; /* H(x) = hash(x) */ |
| 65 |
unsigned char *secret; |
| 66 |
size_t secret_len; |
| 67 |
unsigned char *info; |
| 68 |
size_t info_len; |
| 69 |
unsigned char *salt; |
| 70 |
size_t salt_len; |
| 71 |
size_t out_len; /* optional KMAC parameter */ |
| 72 |
int is_kmac; |
| 73 |
OSSL_FIPS_IND_DECLARE |
| 74 |
} KDF_SSKDF; |
| 75 |
|
| 76 |
#define SSKDF_MAX_INLEN (1 << 30) |
| 77 |
#define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4) |
| 78 |
#define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4) |
| 79 |
|
| 80 |
#define SSKDF_MAX_INFOS 5 |
| 81 |
|
| 82 |
/* KMAC uses a Customisation string of 'KDF' */ |
| 83 |
static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 }; |
| 84 |
|
| 85 |
static OSSL_FUNC_kdf_newctx_fn sskdf_new; |
| 86 |
static OSSL_FUNC_kdf_dupctx_fn sskdf_dup; |
| 87 |
static OSSL_FUNC_kdf_freectx_fn sskdf_free; |
| 88 |
static OSSL_FUNC_kdf_reset_fn sskdf_reset; |
| 89 |
static OSSL_FUNC_kdf_derive_fn sskdf_derive; |
| 90 |
static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params; |
| 91 |
static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params; |
| 92 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_common_gettable_ctx_params; |
| 93 |
static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_common_get_ctx_params; |
| 94 |
static OSSL_FUNC_kdf_derive_fn x963kdf_derive; |
| 95 |
static OSSL_FUNC_kdf_settable_ctx_params_fn x963kdf_settable_ctx_params; |
| 96 |
static OSSL_FUNC_kdf_set_ctx_params_fn x963kdf_set_ctx_params; |
| 97 |
|
| 98 |
/* |
| 99 |
* Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
| 100 |
* Section 4. One-Step Key Derivation using H(x) = hash(x) |
| 101 |
* Note: X9.63 also uses this code with the only difference being that the |
| 102 |
* counter is appended to the secret 'z'. |
| 103 |
* i.e. |
| 104 |
* result[i] = Hash(counter || z || info) for One Step OR |
| 105 |
* result[i] = Hash(z || counter || info) for X9.63. |
| 106 |
*/ |
| 107 |
static int SSKDF_hash_kdm(const EVP_MD *kdf_md, |
| 108 |
const unsigned char *z, size_t z_len, |
| 109 |
const unsigned char *info, size_t info_len, |
| 110 |
unsigned int append_ctr, |
| 111 |
unsigned char *derived_key, size_t derived_key_len) |
| 112 |
{ |
| 113 |
int ret = 0, hlen; |
| 114 |
size_t counter, out_len, len = derived_key_len; |
| 115 |
unsigned char c[4]; |
| 116 |
unsigned char mac[EVP_MAX_MD_SIZE]; |
| 117 |
unsigned char *out = derived_key; |
| 118 |
EVP_MD_CTX *ctx = NULL, *ctx_init = NULL; |
| 119 |
|
| 120 |
if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN |
| 121 |
|| derived_key_len > SSKDF_MAX_INLEN |
| 122 |
|| derived_key_len == 0) |
| 123 |
return 0; |
| 124 |
|
| 125 |
hlen = EVP_MD_get_size(kdf_md); |
| 126 |
if (hlen <= 0) |
| 127 |
return 0; |
| 128 |
out_len = (size_t)hlen; |
| 129 |
|
| 130 |
ctx = EVP_MD_CTX_create(); |
| 131 |
ctx_init = EVP_MD_CTX_create(); |
| 132 |
if (ctx == NULL || ctx_init == NULL) |
| 133 |
goto end; |
| 134 |
|
| 135 |
if (!EVP_DigestInit(ctx_init, kdf_md)) |
| 136 |
goto end; |
| 137 |
|
| 138 |
for (counter = 1;; counter++) { |
| 139 |
c[0] = (unsigned char)((counter >> 24) & 0xff); |
| 140 |
c[1] = (unsigned char)((counter >> 16) & 0xff); |
| 141 |
c[2] = (unsigned char)((counter >> 8) & 0xff); |
| 142 |
c[3] = (unsigned char)(counter & 0xff); |
| 143 |
|
| 144 |
if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init) |
| 145 |
&& (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
| 146 |
&& EVP_DigestUpdate(ctx, z, z_len) |
| 147 |
&& (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
| 148 |
&& EVP_DigestUpdate(ctx, info, info_len))) |
| 149 |
goto end; |
| 150 |
if (len >= out_len) { |
| 151 |
if (!EVP_DigestFinal_ex(ctx, out, NULL)) |
| 152 |
goto end; |
| 153 |
out += out_len; |
| 154 |
len -= out_len; |
| 155 |
if (len == 0) |
| 156 |
break; |
| 157 |
} else { |
| 158 |
if (!EVP_DigestFinal_ex(ctx, mac, NULL)) |
| 159 |
goto end; |
| 160 |
memcpy(out, mac, len); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
ret = 1; |
| 165 |
end: |
| 166 |
EVP_MD_CTX_destroy(ctx); |
| 167 |
EVP_MD_CTX_destroy(ctx_init); |
| 168 |
OPENSSL_cleanse(mac, sizeof(mac)); |
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, |
| 173 |
size_t custom_len, size_t kmac_out_len, |
| 174 |
size_t derived_key_len, unsigned char **out) |
| 175 |
{ |
| 176 |
OSSL_PARAM params[2]; |
| 177 |
|
| 178 |
/* Only KMAC has custom data - so return if not KMAC */ |
| 179 |
if (custom == NULL) |
| 180 |
return 1; |
| 181 |
|
| 182 |
params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, |
| 183 |
(void *)custom, custom_len); |
| 184 |
params[1] = OSSL_PARAM_construct_end(); |
| 185 |
|
| 186 |
if (!EVP_MAC_CTX_set_params(ctx, params)) |
| 187 |
return 0; |
| 188 |
|
| 189 |
/* By default only do one iteration if kmac_out_len is not specified */ |
| 190 |
if (kmac_out_len == 0) |
| 191 |
kmac_out_len = derived_key_len; |
| 192 |
/* otherwise check the size is valid */ |
| 193 |
else if (!(kmac_out_len == derived_key_len |
| 194 |
|| kmac_out_len == 20 |
| 195 |
|| kmac_out_len == 28 |
| 196 |
|| kmac_out_len == 32 |
| 197 |
|| kmac_out_len == 48 |
| 198 |
|| kmac_out_len == 64)) |
| 199 |
return 0; |
| 200 |
|
| 201 |
params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, |
| 202 |
&kmac_out_len); |
| 203 |
|
| 204 |
if (EVP_MAC_CTX_set_params(ctx, params) <= 0) |
| 205 |
return 0; |
| 206 |
|
| 207 |
/* |
| 208 |
* For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so |
| 209 |
* alloc a buffer for this case. |
| 210 |
*/ |
| 211 |
if (kmac_out_len > EVP_MAX_MD_SIZE) { |
| 212 |
*out = OPENSSL_zalloc(kmac_out_len); |
| 213 |
if (*out == NULL) |
| 214 |
return 0; |
| 215 |
} |
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
/* |
| 220 |
* Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
| 221 |
* Section 4. One-Step Key Derivation using MAC: i.e either |
| 222 |
* H(x) = HMAC-hash(salt, x) OR |
| 223 |
* H(x) = KMAC#(salt, x, outbits, CustomString='KDF') |
| 224 |
*/ |
| 225 |
static int SSKDF_mac_kdm(EVP_MAC_CTX *ctx_init, |
| 226 |
const unsigned char *kmac_custom, |
| 227 |
size_t kmac_custom_len, size_t kmac_out_len, |
| 228 |
const unsigned char *salt, size_t salt_len, |
| 229 |
const unsigned char *z, size_t z_len, |
| 230 |
const unsigned char *info, size_t info_len, |
| 231 |
unsigned char *derived_key, size_t derived_key_len) |
| 232 |
{ |
| 233 |
int ret = 0; |
| 234 |
size_t counter, out_len, len; |
| 235 |
unsigned char c[4]; |
| 236 |
unsigned char mac_buf[EVP_MAX_MD_SIZE]; |
| 237 |
unsigned char *out = derived_key; |
| 238 |
EVP_MAC_CTX *ctx = NULL; |
| 239 |
unsigned char *mac = mac_buf, *kmac_buffer = NULL; |
| 240 |
|
| 241 |
if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN |
| 242 |
|| derived_key_len > SSKDF_MAX_INLEN |
| 243 |
|| derived_key_len == 0) |
| 244 |
return 0; |
| 245 |
|
| 246 |
if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len, |
| 247 |
derived_key_len, &kmac_buffer)) |
| 248 |
goto end; |
| 249 |
if (kmac_buffer != NULL) |
| 250 |
mac = kmac_buffer; |
| 251 |
|
| 252 |
if (!EVP_MAC_init(ctx_init, salt, salt_len, NULL)) |
| 253 |
goto end; |
| 254 |
|
| 255 |
out_len = EVP_MAC_CTX_get_mac_size(ctx_init); /* output size */ |
| 256 |
if (out_len <= 0 || (mac == mac_buf && out_len > sizeof(mac_buf))) |
| 257 |
goto end; |
| 258 |
len = derived_key_len; |
| 259 |
|
| 260 |
for (counter = 1;; counter++) { |
| 261 |
c[0] = (unsigned char)((counter >> 24) & 0xff); |
| 262 |
c[1] = (unsigned char)((counter >> 16) & 0xff); |
| 263 |
c[2] = (unsigned char)((counter >> 8) & 0xff); |
| 264 |
c[3] = (unsigned char)(counter & 0xff); |
| 265 |
|
| 266 |
ctx = EVP_MAC_CTX_dup(ctx_init); |
| 267 |
if (!(ctx != NULL |
| 268 |
&& EVP_MAC_update(ctx, c, sizeof(c)) |
| 269 |
&& EVP_MAC_update(ctx, z, z_len) |
| 270 |
&& EVP_MAC_update(ctx, info, info_len))) |
| 271 |
goto end; |
| 272 |
if (len >= out_len) { |
| 273 |
if (!EVP_MAC_final(ctx, out, NULL, len)) |
| 274 |
goto end; |
| 275 |
out += out_len; |
| 276 |
len -= out_len; |
| 277 |
if (len == 0) |
| 278 |
break; |
| 279 |
} else { |
| 280 |
if (!EVP_MAC_final(ctx, mac, NULL, out_len)) |
| 281 |
goto end; |
| 282 |
memcpy(out, mac, len); |
| 283 |
break; |
| 284 |
} |
| 285 |
EVP_MAC_CTX_free(ctx); |
| 286 |
ctx = NULL; |
| 287 |
} |
| 288 |
ret = 1; |
| 289 |
end: |
| 290 |
if (kmac_buffer != NULL) |
| 291 |
OPENSSL_clear_free(kmac_buffer, kmac_out_len); |
| 292 |
else |
| 293 |
OPENSSL_cleanse(mac_buf, sizeof(mac_buf)); |
| 294 |
|
| 295 |
EVP_MAC_CTX_free(ctx); |
| 296 |
return ret; |
| 297 |
} |
| 298 |
|
| 299 |
static void *sskdf_new(void *provctx) |
| 300 |
{ |
| 301 |
KDF_SSKDF *ctx; |
| 302 |
|
| 303 |
if (!ossl_prov_is_running()) |
| 304 |
return NULL; |
| 305 |
|
| 306 |
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
| 307 |
ctx->provctx = provctx; |
| 308 |
OSSL_FIPS_IND_INIT(ctx) |
| 309 |
} |
| 310 |
return ctx; |
| 311 |
} |
| 312 |
|
| 313 |
static void sskdf_reset(void *vctx) |
| 314 |
{ |
| 315 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 316 |
void *provctx = ctx->provctx; |
| 317 |
|
| 318 |
EVP_MAC_CTX_free(ctx->macctx); |
| 319 |
ossl_prov_digest_reset(&ctx->digest); |
| 320 |
OPENSSL_clear_free(ctx->secret, ctx->secret_len); |
| 321 |
OPENSSL_clear_free(ctx->info, ctx->info_len); |
| 322 |
OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
| 323 |
memset(ctx, 0, sizeof(*ctx)); |
| 324 |
ctx->provctx = provctx; |
| 325 |
} |
| 326 |
|
| 327 |
static void sskdf_free(void *vctx) |
| 328 |
{ |
| 329 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 330 |
|
| 331 |
if (ctx != NULL) { |
| 332 |
sskdf_reset(ctx); |
| 333 |
OPENSSL_free(ctx); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static void *sskdf_dup(void *vctx) |
| 338 |
{ |
| 339 |
const KDF_SSKDF *src = (const KDF_SSKDF *)vctx; |
| 340 |
KDF_SSKDF *dest; |
| 341 |
|
| 342 |
dest = sskdf_new(src->provctx); |
| 343 |
if (dest != NULL) { |
| 344 |
if (src->macctx != NULL) { |
| 345 |
dest->macctx = EVP_MAC_CTX_dup(src->macctx); |
| 346 |
if (dest->macctx == NULL) |
| 347 |
goto err; |
| 348 |
} |
| 349 |
if (!ossl_prov_memdup(src->info, src->info_len, |
| 350 |
&dest->info, &dest->info_len) |
| 351 |
|| !ossl_prov_memdup(src->salt, src->salt_len, |
| 352 |
&dest->salt, &dest->salt_len) |
| 353 |
|| !ossl_prov_memdup(src->secret, src->secret_len, |
| 354 |
&dest->secret, &dest->secret_len) |
| 355 |
|| !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
| 356 |
goto err; |
| 357 |
dest->out_len = src->out_len; |
| 358 |
dest->is_kmac = src->is_kmac; |
| 359 |
OSSL_FIPS_IND_COPY(dest, src) |
| 360 |
} |
| 361 |
return dest; |
| 362 |
|
| 363 |
err: |
| 364 |
sskdf_free(dest); |
| 365 |
return NULL; |
| 366 |
} |
| 367 |
|
| 368 |
static size_t sskdf_size(KDF_SSKDF *ctx) |
| 369 |
{ |
| 370 |
int len; |
| 371 |
const EVP_MD *md = NULL; |
| 372 |
|
| 373 |
if (ctx->is_kmac) |
| 374 |
return SIZE_MAX; |
| 375 |
|
| 376 |
md = ossl_prov_digest_md(&ctx->digest); |
| 377 |
if (md == NULL) { |
| 378 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
len = EVP_MD_get_size(md); |
| 382 |
return (len <= 0) ? 0 : (size_t)len; |
| 383 |
} |
| 384 |
|
| 385 |
#ifdef FIPS_MODULE |
| 386 |
static int fips_sskdf_key_check_passed(KDF_SSKDF *ctx) |
| 387 |
{ |
| 388 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 389 |
int key_approved = ossl_kdf_check_key_size(ctx->secret_len); |
| 390 |
|
| 391 |
if (!key_approved) { |
| 392 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
| 393 |
libctx, "SSKDF", "Key size", |
| 394 |
ossl_fips_config_sskdf_key_check)) { |
| 395 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 396 |
return 0; |
| 397 |
} |
| 398 |
} |
| 399 |
return 1; |
| 400 |
} |
| 401 |
#endif |
| 402 |
|
| 403 |
static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 404 |
const OSSL_PARAM params[]) |
| 405 |
{ |
| 406 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 407 |
const EVP_MD *md; |
| 408 |
|
| 409 |
if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params)) |
| 410 |
return 0; |
| 411 |
if (ctx->secret == NULL) { |
| 412 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
md = ossl_prov_digest_md(&ctx->digest); |
| 417 |
|
| 418 |
if (ctx->macctx != NULL) { |
| 419 |
/* H(x) = KMAC or H(x) = HMAC */ |
| 420 |
int ret; |
| 421 |
const unsigned char *custom = NULL; |
| 422 |
size_t custom_len = 0; |
| 423 |
int default_salt_len; |
| 424 |
EVP_MAC *mac = EVP_MAC_CTX_get0_mac(ctx->macctx); |
| 425 |
|
| 426 |
if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_HMAC)) { |
| 427 |
/* H(x) = HMAC(x, salt, hash) */ |
| 428 |
if (md == NULL) { |
| 429 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
default_salt_len = EVP_MD_get_size(md); |
| 433 |
if (default_salt_len <= 0) |
| 434 |
return 0; |
| 435 |
} else if (ctx->is_kmac) { |
| 436 |
/* H(x) = KMACzzz(x, salt, custom) */ |
| 437 |
custom = kmac_custom_str; |
| 438 |
custom_len = sizeof(kmac_custom_str); |
| 439 |
if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_KMAC128)) |
| 440 |
default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE; |
| 441 |
else |
| 442 |
default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE; |
| 443 |
} else { |
| 444 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE); |
| 445 |
return 0; |
| 446 |
} |
| 447 |
/* If no salt is set then use a default_salt of zeros */ |
| 448 |
if (ctx->salt == NULL || ctx->salt_len <= 0) { |
| 449 |
ctx->salt = OPENSSL_zalloc(default_salt_len); |
| 450 |
if (ctx->salt == NULL) |
| 451 |
return 0; |
| 452 |
ctx->salt_len = default_salt_len; |
| 453 |
} |
| 454 |
ret = SSKDF_mac_kdm(ctx->macctx, |
| 455 |
custom, custom_len, ctx->out_len, |
| 456 |
ctx->salt, ctx->salt_len, |
| 457 |
ctx->secret, ctx->secret_len, |
| 458 |
ctx->info, ctx->info_len, key, keylen); |
| 459 |
return ret; |
| 460 |
} else { |
| 461 |
/* H(x) = hash */ |
| 462 |
if (md == NULL) { |
| 463 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 464 |
return 0; |
| 465 |
} |
| 466 |
return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
| 467 |
ctx->info, ctx->info_len, 0, key, keylen); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#ifdef FIPS_MODULE |
| 472 |
static int fips_x963kdf_digest_check_passed(KDF_SSKDF *ctx, const EVP_MD *md) |
| 473 |
{ |
| 474 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 475 |
/* |
| 476 |
* Perform digest check |
| 477 |
* |
| 478 |
* X963KDF is a KDF defined in ANSI-X9.63. According to ACVP specification |
| 479 |
* section 7.3.1, only SHA-2 and SHA-3 can be regarded as valid hash |
| 480 |
* functions. |
| 481 |
*/ |
| 482 |
int digest_unapproved = (ctx->is_kmac != 1) && EVP_MD_is_a(md, SN_sha1); |
| 483 |
|
| 484 |
if (digest_unapproved) { |
| 485 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
| 486 |
libctx, "X963KDF", "Digest", |
| 487 |
ossl_fips_config_x963kdf_digest_check)) { |
| 488 |
ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
| 489 |
return 0; |
| 490 |
} |
| 491 |
} |
| 492 |
return 1; |
| 493 |
} |
| 494 |
|
| 495 |
static int fips_x963kdf_key_check_passed(KDF_SSKDF *ctx) |
| 496 |
{ |
| 497 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 498 |
int key_approved = ossl_kdf_check_key_size(ctx->secret_len); |
| 499 |
|
| 500 |
if (!key_approved) { |
| 501 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1, |
| 502 |
libctx, "X963KDF", "Key size", |
| 503 |
ossl_fips_config_x963kdf_key_check)) { |
| 504 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 505 |
return 0; |
| 506 |
} |
| 507 |
} |
| 508 |
return 1; |
| 509 |
} |
| 510 |
#endif |
| 511 |
|
| 512 |
static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 513 |
const OSSL_PARAM params[]) |
| 514 |
{ |
| 515 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 516 |
const EVP_MD *md; |
| 517 |
|
| 518 |
if (!ossl_prov_is_running() || !x963kdf_set_ctx_params(ctx, params)) |
| 519 |
return 0; |
| 520 |
|
| 521 |
if (ctx->secret == NULL) { |
| 522 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
if (ctx->macctx != NULL) { |
| 527 |
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED); |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
/* H(x) = hash */ |
| 532 |
md = ossl_prov_digest_md(&ctx->digest); |
| 533 |
if (md == NULL) { |
| 534 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
| 539 |
ctx->info, ctx->info_len, 1, key, keylen); |
| 540 |
} |
| 541 |
|
| 542 |
struct sskdf_all_set_ctx_params_st { |
| 543 |
OSSL_PARAM *secret; |
| 544 |
OSSL_PARAM *propq; |
| 545 |
OSSL_PARAM *engine; |
| 546 |
OSSL_PARAM *digest; |
| 547 |
OSSL_PARAM *mac; |
| 548 |
OSSL_PARAM *salt; |
| 549 |
OSSL_PARAM *size; |
| 550 |
#ifdef FIPS_MODULE |
| 551 |
OSSL_PARAM *ind_k; |
| 552 |
OSSL_PARAM *ind_d; |
| 553 |
#endif |
| 554 |
OSSL_PARAM *info[SSKDF_MAX_INFOS]; |
| 555 |
int num_info; |
| 556 |
}; |
| 557 |
|
| 558 |
static int sskdf_common_set_ctx_params(KDF_SSKDF *ctx, struct sskdf_all_set_ctx_params_st *p, |
| 559 |
const OSSL_PARAM *params) |
| 560 |
{ |
| 561 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 562 |
const EVP_MD *md = NULL; |
| 563 |
size_t sz; |
| 564 |
int r; |
| 565 |
|
| 566 |
if (!ossl_prov_macctx_load(&ctx->macctx, |
| 567 |
p->mac, NULL, p->digest, p->propq, p->engine, |
| 568 |
NULL, NULL, NULL, libctx)) |
| 569 |
return 0; |
| 570 |
if (ctx->macctx != NULL) { |
| 571 |
if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), |
| 572 |
OSSL_MAC_NAME_KMAC128) |
| 573 |
|| EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), |
| 574 |
OSSL_MAC_NAME_KMAC256)) { |
| 575 |
ctx->is_kmac = 1; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (p->digest != NULL) { |
| 580 |
if (!ossl_prov_digest_load(&ctx->digest, p->digest, |
| 581 |
p->propq, p->engine, libctx)) |
| 582 |
return 0; |
| 583 |
|
| 584 |
md = ossl_prov_digest_md(&ctx->digest); |
| 585 |
if (EVP_MD_xof(md)) { |
| 586 |
ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
| 587 |
return 0; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
r = ossl_param_get1_octet_string_from_param(p->secret, &ctx->secret, |
| 592 |
&ctx->secret_len); |
| 593 |
if (r == 0) |
| 594 |
return 0; |
| 595 |
|
| 596 |
if (ossl_param_get1_concat_octet_string(p->num_info, p->info, &ctx->info, |
| 597 |
&ctx->info_len) |
| 598 |
== 0) |
| 599 |
return 0; |
| 600 |
|
| 601 |
if (ossl_param_get1_octet_string_from_param(p->salt, &ctx->salt, |
| 602 |
&ctx->salt_len) |
| 603 |
== 0) |
| 604 |
return 0; |
| 605 |
|
| 606 |
if (p->size != NULL) { |
| 607 |
if (!OSSL_PARAM_get_size_t(p->size, &sz) || sz == 0) |
| 608 |
return 0; |
| 609 |
ctx->out_len = sz; |
| 610 |
} |
| 611 |
return 1; |
| 612 |
} |
| 613 |
|
| 614 |
#define sskdf_set_ctx_params_st sskdf_all_set_ctx_params_st |
| 615 |
|
| 616 |
/* clang-format off */ |
| 617 |
{- produce_param_decoder('sskdf_set_ctx_params', |
| 618 |
(['OSSL_KDF_PARAM_SECRET', 'secret', 'octet_string'], |
| 619 |
['OSSL_KDF_PARAM_KEY', 'secret', 'octet_string'], |
| 620 |
['OSSL_KDF_PARAM_INFO', 'info', 'octet_string', SSKDF_MAX_INFOS], |
| 621 |
['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 622 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 623 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 624 |
['OSSL_KDF_PARAM_MAC', 'mac', 'utf8_string'], |
| 625 |
['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'], |
| 626 |
['OSSL_KDF_PARAM_MAC_SIZE', 'size', 'size_t'], |
| 627 |
['OSSL_KDF_PARAM_FIPS_KEY_CHECK', 'ind_k', 'int', 'fips'], |
| 628 |
)); -} |
| 629 |
/* clang-format on */ |
| 630 |
|
| 631 |
static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 632 |
{ |
| 633 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 634 |
struct sskdf_all_set_ctx_params_st p; |
| 635 |
|
| 636 |
if (ctx == NULL || !sskdf_set_ctx_params_decoder(params, &p)) |
| 637 |
return 0; |
| 638 |
|
| 639 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
| 640 |
return 0; |
| 641 |
|
| 642 |
if (!sskdf_common_set_ctx_params(ctx, &p, params)) |
| 643 |
return 0; |
| 644 |
|
| 645 |
#ifdef FIPS_MODULE |
| 646 |
if (p.secret != NULL) |
| 647 |
if (!fips_sskdf_key_check_passed(ctx)) |
| 648 |
return 0; |
| 649 |
#endif |
| 650 |
|
| 651 |
return 1; |
| 652 |
} |
| 653 |
|
| 654 |
static const OSSL_PARAM *sskdf_settable_ctx_params(ossl_unused void *ctx, |
| 655 |
ossl_unused void *provctx) |
| 656 |
{ |
| 657 |
return sskdf_set_ctx_params_list; |
| 658 |
} |
| 659 |
|
| 660 |
/* clang-format off */ |
| 661 |
{- produce_param_decoder('sskdf_get_ctx_params', |
| 662 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 663 |
['OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR', 'ind', 'int', 'fips'], |
| 664 |
)); -} |
| 665 |
/* clang-format on */ |
| 666 |
|
| 667 |
static int sskdf_common_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 668 |
{ |
| 669 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 670 |
struct sskdf_get_ctx_params_st p; |
| 671 |
|
| 672 |
if (ctx == NULL || !sskdf_get_ctx_params_decoder(params, &p)) |
| 673 |
return 0; |
| 674 |
|
| 675 |
if (p.size != NULL) { |
| 676 |
if (!OSSL_PARAM_set_size_t(p.size, sskdf_size(ctx))) |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, p.ind)) |
| 681 |
return 0; |
| 682 |
|
| 683 |
return 1; |
| 684 |
} |
| 685 |
|
| 686 |
static const OSSL_PARAM *sskdf_common_gettable_ctx_params(ossl_unused void *ctx, ossl_unused void *provctx) |
| 687 |
{ |
| 688 |
return sskdf_get_ctx_params_list; |
| 689 |
} |
| 690 |
|
| 691 |
#define x963kdf_set_ctx_params_st sskdf_all_set_ctx_params_st |
| 692 |
|
| 693 |
/* clang-format off */ |
| 694 |
{- produce_param_decoder('x963kdf_set_ctx_params', |
| 695 |
(['OSSL_KDF_PARAM_SECRET', 'secret', 'octet_string'], |
| 696 |
['OSSL_KDF_PARAM_KEY', 'secret', 'octet_string'], |
| 697 |
['OSSL_KDF_PARAM_INFO', 'info', 'octet_string', SSKDF_MAX_INFOS], |
| 698 |
['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 699 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 700 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 701 |
['OSSL_KDF_PARAM_MAC', 'mac', 'utf8_string'], |
| 702 |
['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'], |
| 703 |
['OSSL_KDF_PARAM_MAC_SIZE', 'size', 'size_t'], |
| 704 |
['OSSL_KDF_PARAM_FIPS_DIGEST_CHECK', 'ind_d', 'int', 'fips'], |
| 705 |
['OSSL_KDF_PARAM_FIPS_KEY_CHECK', 'ind_k', 'int', 'fips'], |
| 706 |
)); -} |
| 707 |
/* clang-format on */ |
| 708 |
|
| 709 |
static int x963kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 710 |
{ |
| 711 |
KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
| 712 |
struct sskdf_all_set_ctx_params_st p; |
| 713 |
|
| 714 |
if (ctx == NULL || !x963kdf_set_ctx_params_decoder(params, &p)) |
| 715 |
return 0; |
| 716 |
|
| 717 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_d)) |
| 718 |
return 0; |
| 719 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
| 720 |
return 0; |
| 721 |
|
| 722 |
if (!sskdf_common_set_ctx_params(ctx, &p, params)) |
| 723 |
return 0; |
| 724 |
|
| 725 |
#ifdef FIPS_MODULE |
| 726 |
if (p.digest != NULL) { |
| 727 |
const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
| 728 |
|
| 729 |
if (!fips_x963kdf_digest_check_passed(ctx, md)) |
| 730 |
return 0; |
| 731 |
} |
| 732 |
|
| 733 |
if (p.secret != NULL) |
| 734 |
if (!fips_x963kdf_key_check_passed(ctx)) |
| 735 |
return 0; |
| 736 |
#endif |
| 737 |
|
| 738 |
return 1; |
| 739 |
} |
| 740 |
|
| 741 |
static const OSSL_PARAM *x963kdf_settable_ctx_params(ossl_unused void *ctx, |
| 742 |
ossl_unused void *provctx) |
| 743 |
{ |
| 744 |
return x963kdf_set_ctx_params_list; |
| 745 |
} |
| 746 |
|
| 747 |
const OSSL_DISPATCH ossl_kdf_sskdf_functions[] = { |
| 748 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))sskdf_new }, |
| 749 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))sskdf_dup }, |
| 750 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))sskdf_free }, |
| 751 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))sskdf_reset }, |
| 752 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))sskdf_derive }, |
| 753 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 754 |
(void (*)(void))sskdf_settable_ctx_params }, |
| 755 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))sskdf_set_ctx_params }, |
| 756 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 757 |
(void (*)(void))sskdf_common_gettable_ctx_params }, |
| 758 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))sskdf_common_get_ctx_params }, |
| 759 |
OSSL_DISPATCH_END |
| 760 |
}; |
| 761 |
|
| 762 |
const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[] = { |
| 763 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))sskdf_new }, |
| 764 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))sskdf_dup }, |
| 765 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))sskdf_free }, |
| 766 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))sskdf_reset }, |
| 767 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))x963kdf_derive }, |
| 768 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 769 |
(void (*)(void))x963kdf_settable_ctx_params }, |
| 770 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))x963kdf_set_ctx_params }, |
| 771 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 772 |
(void (*)(void))sskdf_common_gettable_ctx_params }, |
| 773 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))sskdf_common_get_ctx_params }, |
| 774 |
OSSL_DISPATCH_END |
| 775 |
}; |