| 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 |
#include "internal/e_os.h" |
| 17 |
#include <openssl/core_names.h> |
| 18 |
#include <openssl/core_dispatch.h> |
| 19 |
#include <openssl/err.h> |
| 20 |
#include <openssl/evp.h> |
| 21 |
#include <openssl/params.h> |
| 22 |
#include <openssl/proverr.h> |
| 23 |
#include "internal/common.h" |
| 24 |
#include "internal/packet.h" |
| 25 |
#include "internal/der.h" |
| 26 |
#include "internal/nelem.h" |
| 27 |
#include "prov/provider_ctx.h" |
| 28 |
#include "prov/providercommon.h" |
| 29 |
#include "prov/implementations.h" |
| 30 |
#include "prov/provider_util.h" |
| 31 |
#include "prov/securitycheck.h" |
| 32 |
#include "prov/der_wrap.h" |
| 33 |
|
| 34 |
#define X942KDF_MAX_INLEN (1 << 30) |
| 35 |
|
| 36 |
static OSSL_FUNC_kdf_newctx_fn x942kdf_new; |
| 37 |
static OSSL_FUNC_kdf_dupctx_fn x942kdf_dup; |
| 38 |
static OSSL_FUNC_kdf_freectx_fn x942kdf_free; |
| 39 |
static OSSL_FUNC_kdf_reset_fn x942kdf_reset; |
| 40 |
static OSSL_FUNC_kdf_derive_fn x942kdf_derive; |
| 41 |
static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params; |
| 42 |
static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params; |
| 43 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params; |
| 44 |
static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params; |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
void *provctx; |
| 48 |
PROV_DIGEST digest; |
| 49 |
unsigned char *secret; |
| 50 |
size_t secret_len; |
| 51 |
unsigned char *acvpinfo; |
| 52 |
size_t acvpinfo_len; |
| 53 |
unsigned char *partyuinfo, *partyvinfo, *supp_pubinfo, *supp_privinfo; |
| 54 |
size_t partyuinfo_len, partyvinfo_len, supp_pubinfo_len, supp_privinfo_len; |
| 55 |
size_t dkm_len; |
| 56 |
const unsigned char *cek_oid; |
| 57 |
size_t cek_oid_len; |
| 58 |
int use_keybits; |
| 59 |
OSSL_FIPS_IND_DECLARE |
| 60 |
} KDF_X942; |
| 61 |
|
| 62 |
/* |
| 63 |
* A table of allowed wrapping algorithms, oids and the associated output |
| 64 |
* lengths. |
| 65 |
* NOTE: RC2wrap and camellia128_wrap have been removed as there are no |
| 66 |
* corresponding ciphers for these operations. |
| 67 |
*/ |
| 68 |
static const struct { |
| 69 |
const char *name; |
| 70 |
const unsigned char *oid; |
| 71 |
size_t oid_len; |
| 72 |
size_t keklen; /* size in bytes */ |
| 73 |
} kek_algs[] = { |
| 74 |
{ "AES-128-WRAP", ossl_der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap, |
| 75 |
16 }, |
| 76 |
{ "AES-192-WRAP", ossl_der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap, |
| 77 |
24 }, |
| 78 |
{ "AES-256-WRAP", ossl_der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap, |
| 79 |
32 }, |
| 80 |
#ifndef FIPS_MODULE |
| 81 |
{ "DES3-WRAP", ossl_der_oid_id_alg_CMS3DESwrap, |
| 82 |
DER_OID_SZ_id_alg_CMS3DESwrap, 24 }, |
| 83 |
#endif |
| 84 |
}; |
| 85 |
|
| 86 |
static int find_alg_id(OSSL_LIB_CTX *libctx, const char *algname, |
| 87 |
const char *propq, size_t *id) |
| 88 |
{ |
| 89 |
int ret = 1; |
| 90 |
size_t i; |
| 91 |
EVP_CIPHER *cipher; |
| 92 |
|
| 93 |
cipher = EVP_CIPHER_fetch(libctx, algname, propq); |
| 94 |
if (cipher != NULL) { |
| 95 |
for (i = 0; i < OSSL_NELEM(kek_algs); i++) { |
| 96 |
if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) { |
| 97 |
*id = i; |
| 98 |
goto end; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
ret = 0; |
| 103 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG); |
| 104 |
end: |
| 105 |
EVP_CIPHER_free(cipher); |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
static int DER_w_keyinfo(WPACKET *pkt, |
| 110 |
const unsigned char *der_oid, size_t der_oidlen, |
| 111 |
unsigned char **pcounter) |
| 112 |
{ |
| 113 |
return ossl_DER_w_begin_sequence(pkt, -1) |
| 114 |
/* Store the initial value of 1 into the counter */ |
| 115 |
&& ossl_DER_w_octet_string_uint32(pkt, -1, 1) |
| 116 |
/* Remember where we stored the counter in the buffer */ |
| 117 |
&& (pcounter == NULL |
| 118 |
|| (*pcounter = WPACKET_get_curr(pkt)) != NULL) |
| 119 |
&& ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen) |
| 120 |
&& ossl_DER_w_end_sequence(pkt, -1); |
| 121 |
} |
| 122 |
|
| 123 |
static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen, |
| 124 |
const unsigned char *der_oid, size_t der_oidlen, |
| 125 |
const unsigned char *acvp, size_t acvplen, |
| 126 |
const unsigned char *partyu, size_t partyulen, |
| 127 |
const unsigned char *partyv, size_t partyvlen, |
| 128 |
const unsigned char *supp_pub, size_t supp_publen, |
| 129 |
const unsigned char *supp_priv, size_t supp_privlen, |
| 130 |
uint32_t keylen_bits, unsigned char **pcounter) |
| 131 |
{ |
| 132 |
return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) : WPACKET_init_null_der(pkt)) |
| 133 |
&& ossl_DER_w_begin_sequence(pkt, -1) |
| 134 |
&& (supp_priv == NULL |
| 135 |
|| ossl_DER_w_octet_string(pkt, 3, supp_priv, supp_privlen)) |
| 136 |
&& (supp_pub == NULL |
| 137 |
|| ossl_DER_w_octet_string(pkt, 2, supp_pub, supp_publen)) |
| 138 |
&& (keylen_bits == 0 |
| 139 |
|| ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits)) |
| 140 |
&& (partyv == NULL || ossl_DER_w_octet_string(pkt, 1, partyv, partyvlen)) |
| 141 |
&& (partyu == NULL || ossl_DER_w_octet_string(pkt, 0, partyu, partyulen)) |
| 142 |
&& (acvp == NULL || ossl_DER_w_precompiled(pkt, -1, acvp, acvplen)) |
| 143 |
&& DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter) |
| 144 |
&& ossl_DER_w_end_sequence(pkt, -1) |
| 145 |
&& WPACKET_finish(pkt); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* Encode the other info structure. |
| 150 |
* |
| 151 |
* The ANS X9.42-2003 standard uses OtherInfo: |
| 152 |
* |
| 153 |
* OtherInfo ::= SEQUENCE { |
| 154 |
* keyInfo KeySpecificInfo, |
| 155 |
* partyUInfo [0] OCTET STRING OPTIONAL, |
| 156 |
* partyVInfo [1] OCTET STRING OPTIONAL, |
| 157 |
* suppPubInfo [2] OCTET STRING OPTIONAL, |
| 158 |
* suppPrivInfo [3] OCTET STRING OPTIONAL |
| 159 |
* } |
| 160 |
* |
| 161 |
* KeySpecificInfo ::= SEQUENCE { |
| 162 |
* algorithm OBJECT IDENTIFIER, |
| 163 |
* counter OCTET STRING SIZE (4..4) |
| 164 |
* } |
| 165 |
* |
| 166 |
* RFC2631 Section 2.1.2 Contains the following definition for OtherInfo |
| 167 |
* |
| 168 |
* OtherInfo ::= SEQUENCE { |
| 169 |
* keyInfo KeySpecificInfo, |
| 170 |
* partyAInfo [0] OCTET STRING OPTIONAL, |
| 171 |
* suppPubInfo [2] OCTET STRING |
| 172 |
* } |
| 173 |
* Where suppPubInfo is the key length (in bits) (stored into 4 bytes) |
| 174 |
* |
| 175 |
* |keylen| is the length (in bytes) of the generated KEK. It is stored into |
| 176 |
* suppPubInfo (in bits). It is ignored if the value is 0. |
| 177 |
* |cek_oid| The oid of the key wrapping algorithm. |
| 178 |
* |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid, |
| 179 |
* |acvp| is the optional blob of DER data representing one or more of the |
| 180 |
* OtherInfo fields related to |partyu|, |partyv|, |supp_pub| and |supp_priv|. |
| 181 |
* This field should normally be NULL. If |acvp| is non NULL then |partyu|, |
| 182 |
* |partyv|, |supp_pub| and |supp_priv| should all be NULL. |
| 183 |
* |acvp_len| is the |acvp| length (in bytes). |
| 184 |
* |partyu| is the optional public info contributed by the initiator. |
| 185 |
* It can be NULL. (It is also used as the ukm by CMS). |
| 186 |
* |partyu_len| is the |partyu| length (in bytes). |
| 187 |
* |partyv| is the optional public info contributed by the responder. |
| 188 |
* It can be NULL. |
| 189 |
* |partyv_len| is the |partyv| length (in bytes). |
| 190 |
* |supp_pub| is the optional additional, mutually-known public information. |
| 191 |
* It can be NULL. |keylen| should be 0 if this is not NULL. |
| 192 |
* |supp_pub_len| is the |supp_pub| length (in bytes). |
| 193 |
* |supp_priv| is the optional additional, mutually-known private information. |
| 194 |
* It can be NULL. |
| 195 |
* |supp_priv_len| is the |supp_priv| length (in bytes). |
| 196 |
* |der| is the returned encoded data. It must be freed by the caller. |
| 197 |
* |der_len| is the returned size of the encoded data. |
| 198 |
* |out_ctr| returns a pointer to the counter data which is embedded inside the |
| 199 |
* encoded data. This allows the counter bytes to be updated without |
| 200 |
* re-encoding. |
| 201 |
* |
| 202 |
* Returns: 1 if successfully encoded, or 0 otherwise. |
| 203 |
* Assumptions: |der|, |der_len| & |out_ctr| are not NULL. |
| 204 |
*/ |
| 205 |
static int |
| 206 |
x942_encode_otherinfo(size_t keylen, |
| 207 |
const unsigned char *cek_oid, size_t cek_oid_len, |
| 208 |
const unsigned char *acvp, size_t acvp_len, |
| 209 |
const unsigned char *partyu, size_t partyu_len, |
| 210 |
const unsigned char *partyv, size_t partyv_len, |
| 211 |
const unsigned char *supp_pub, size_t supp_pub_len, |
| 212 |
const unsigned char *supp_priv, size_t supp_priv_len, |
| 213 |
unsigned char **der, size_t *der_len, |
| 214 |
unsigned char **out_ctr) |
| 215 |
{ |
| 216 |
int ret = 0; |
| 217 |
unsigned char *pcounter = NULL, *der_buf = NULL; |
| 218 |
size_t der_buflen = 0; |
| 219 |
WPACKET pkt; |
| 220 |
uint32_t keylen_bits; |
| 221 |
|
| 222 |
/* keylenbits must fit into 4 bytes */ |
| 223 |
if (keylen > 0xFFFFFF) |
| 224 |
return 0; |
| 225 |
keylen_bits = (uint32_t)(8 * keylen); |
| 226 |
|
| 227 |
/* Calculate the size of the buffer */ |
| 228 |
if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oid_len, |
| 229 |
acvp, acvp_len, |
| 230 |
partyu, partyu_len, partyv, partyv_len, |
| 231 |
supp_pub, supp_pub_len, supp_priv, supp_priv_len, |
| 232 |
keylen_bits, NULL) |
| 233 |
|| !WPACKET_get_total_written(&pkt, &der_buflen)) |
| 234 |
goto err; |
| 235 |
WPACKET_cleanup(&pkt); |
| 236 |
/* Alloc the buffer */ |
| 237 |
der_buf = OPENSSL_zalloc(der_buflen); |
| 238 |
if (der_buf == NULL) |
| 239 |
goto err; |
| 240 |
/* Encode into the buffer */ |
| 241 |
if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oid_len, |
| 242 |
acvp, acvp_len, |
| 243 |
partyu, partyu_len, partyv, partyv_len, |
| 244 |
supp_pub, supp_pub_len, supp_priv, supp_priv_len, |
| 245 |
keylen_bits, &pcounter)) |
| 246 |
goto err; |
| 247 |
/* |
| 248 |
* Since we allocated the exact size required, the buffer should point to the |
| 249 |
* start of the allocated buffer at this point. |
| 250 |
*/ |
| 251 |
if (WPACKET_get_curr(&pkt) != der_buf) |
| 252 |
goto err; |
| 253 |
|
| 254 |
/* |
| 255 |
* The data for the DER encoded octet string of a 32 bit counter = 1 |
| 256 |
* should be 04 04 00 00 00 01 |
| 257 |
* So just check the header is correct and skip over it. |
| 258 |
* This counter will be incremented in the kdf update loop. |
| 259 |
*/ |
| 260 |
if (pcounter == NULL |
| 261 |
|| pcounter[0] != 0x04 |
| 262 |
|| pcounter[1] != 0x04) |
| 263 |
goto err; |
| 264 |
*out_ctr = (pcounter + 2); |
| 265 |
*der = der_buf; |
| 266 |
*der_len = der_buflen; |
| 267 |
ret = 1; |
| 268 |
err: |
| 269 |
WPACKET_cleanup(&pkt); |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
static int x942kdf_hash_kdm(const EVP_MD *kdf_md, |
| 274 |
const unsigned char *z, size_t z_len, |
| 275 |
const unsigned char *other, size_t other_len, |
| 276 |
unsigned char *ctr, |
| 277 |
unsigned char *derived_key, size_t derived_key_len) |
| 278 |
{ |
| 279 |
int ret = 0, hlen; |
| 280 |
size_t counter, out_len, len = derived_key_len; |
| 281 |
unsigned char mac[EVP_MAX_MD_SIZE]; |
| 282 |
unsigned char *out = derived_key; |
| 283 |
EVP_MD_CTX *ctx = NULL, *ctx_init = NULL; |
| 284 |
|
| 285 |
if (z_len > X942KDF_MAX_INLEN |
| 286 |
|| other_len > X942KDF_MAX_INLEN |
| 287 |
|| derived_key_len > X942KDF_MAX_INLEN |
| 288 |
|| derived_key_len == 0) { |
| 289 |
ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
hlen = EVP_MD_get_size(kdf_md); |
| 294 |
if (hlen <= 0) |
| 295 |
return 0; |
| 296 |
out_len = (size_t)hlen; |
| 297 |
|
| 298 |
ctx = EVP_MD_CTX_create(); |
| 299 |
ctx_init = EVP_MD_CTX_create(); |
| 300 |
if (ctx == NULL || ctx_init == NULL) |
| 301 |
goto end; |
| 302 |
|
| 303 |
if (!EVP_DigestInit(ctx_init, kdf_md)) |
| 304 |
goto end; |
| 305 |
|
| 306 |
for (counter = 1;; counter++) { |
| 307 |
/* updating the ctr modifies 4 bytes in the 'other' buffer */ |
| 308 |
ctr[0] = (unsigned char)((counter >> 24) & 0xff); |
| 309 |
ctr[1] = (unsigned char)((counter >> 16) & 0xff); |
| 310 |
ctr[2] = (unsigned char)((counter >> 8) & 0xff); |
| 311 |
ctr[3] = (unsigned char)(counter & 0xff); |
| 312 |
|
| 313 |
if (!EVP_MD_CTX_copy_ex(ctx, ctx_init) |
| 314 |
|| !EVP_DigestUpdate(ctx, z, z_len) |
| 315 |
|| !EVP_DigestUpdate(ctx, other, other_len)) |
| 316 |
goto end; |
| 317 |
if (len >= out_len) { |
| 318 |
if (!EVP_DigestFinal_ex(ctx, out, NULL)) |
| 319 |
goto end; |
| 320 |
out += out_len; |
| 321 |
len -= out_len; |
| 322 |
if (len == 0) |
| 323 |
break; |
| 324 |
} else { |
| 325 |
if (!EVP_DigestFinal_ex(ctx, mac, NULL)) |
| 326 |
goto end; |
| 327 |
memcpy(out, mac, len); |
| 328 |
break; |
| 329 |
} |
| 330 |
} |
| 331 |
ret = 1; |
| 332 |
end: |
| 333 |
EVP_MD_CTX_free(ctx); |
| 334 |
EVP_MD_CTX_free(ctx_init); |
| 335 |
OPENSSL_cleanse(mac, sizeof(mac)); |
| 336 |
return ret; |
| 337 |
} |
| 338 |
|
| 339 |
static void *x942kdf_new(void *provctx) |
| 340 |
{ |
| 341 |
KDF_X942 *ctx; |
| 342 |
|
| 343 |
if (!ossl_prov_is_running()) |
| 344 |
return NULL; |
| 345 |
|
| 346 |
ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 347 |
if (ctx == NULL) |
| 348 |
return NULL; |
| 349 |
|
| 350 |
ctx->provctx = provctx; |
| 351 |
OSSL_FIPS_IND_INIT(ctx) |
| 352 |
ctx->use_keybits = 1; |
| 353 |
return ctx; |
| 354 |
} |
| 355 |
|
| 356 |
static void x942kdf_reset(void *vctx) |
| 357 |
{ |
| 358 |
KDF_X942 *ctx = (KDF_X942 *)vctx; |
| 359 |
void *provctx = ctx->provctx; |
| 360 |
|
| 361 |
ossl_prov_digest_reset(&ctx->digest); |
| 362 |
OPENSSL_clear_free(ctx->secret, ctx->secret_len); |
| 363 |
OPENSSL_clear_free(ctx->acvpinfo, ctx->acvpinfo_len); |
| 364 |
OPENSSL_clear_free(ctx->partyuinfo, ctx->partyuinfo_len); |
| 365 |
OPENSSL_clear_free(ctx->partyvinfo, ctx->partyvinfo_len); |
| 366 |
OPENSSL_clear_free(ctx->supp_pubinfo, ctx->supp_pubinfo_len); |
| 367 |
OPENSSL_clear_free(ctx->supp_privinfo, ctx->supp_privinfo_len); |
| 368 |
memset(ctx, 0, sizeof(*ctx)); |
| 369 |
ctx->provctx = provctx; |
| 370 |
ctx->use_keybits = 1; |
| 371 |
} |
| 372 |
|
| 373 |
static void x942kdf_free(void *vctx) |
| 374 |
{ |
| 375 |
KDF_X942 *ctx = (KDF_X942 *)vctx; |
| 376 |
|
| 377 |
if (ctx != NULL) { |
| 378 |
x942kdf_reset(ctx); |
| 379 |
OPENSSL_free(ctx); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
static void *x942kdf_dup(void *vctx) |
| 384 |
{ |
| 385 |
const KDF_X942 *src = (const KDF_X942 *)vctx; |
| 386 |
KDF_X942 *dest; |
| 387 |
|
| 388 |
dest = x942kdf_new(src->provctx); |
| 389 |
if (dest != NULL) { |
| 390 |
if (!ossl_prov_memdup(src->secret, src->secret_len, |
| 391 |
&dest->secret, &dest->secret_len) |
| 392 |
|| !ossl_prov_memdup(src->acvpinfo, src->acvpinfo_len, |
| 393 |
&dest->acvpinfo, &dest->acvpinfo_len) |
| 394 |
|| !ossl_prov_memdup(src->partyuinfo, src->partyuinfo_len, |
| 395 |
&dest->partyuinfo, &dest->partyuinfo_len) |
| 396 |
|| !ossl_prov_memdup(src->partyvinfo, src->partyvinfo_len, |
| 397 |
&dest->partyvinfo, &dest->partyvinfo_len) |
| 398 |
|| !ossl_prov_memdup(src->supp_pubinfo, src->supp_pubinfo_len, |
| 399 |
&dest->supp_pubinfo, |
| 400 |
&dest->supp_pubinfo_len) |
| 401 |
|| !ossl_prov_memdup(src->supp_privinfo, src->supp_privinfo_len, |
| 402 |
&dest->supp_privinfo, |
| 403 |
&dest->supp_privinfo_len) |
| 404 |
|| !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
| 405 |
goto err; |
| 406 |
dest->cek_oid = src->cek_oid; |
| 407 |
dest->cek_oid_len = src->cek_oid_len; |
| 408 |
dest->dkm_len = src->dkm_len; |
| 409 |
dest->use_keybits = src->use_keybits; |
| 410 |
OSSL_FIPS_IND_COPY(dest, src) |
| 411 |
} |
| 412 |
return dest; |
| 413 |
|
| 414 |
err: |
| 415 |
x942kdf_free(dest); |
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
static int x942kdf_set_buffer(unsigned char **out, size_t *out_len, |
| 420 |
const OSSL_PARAM *p) |
| 421 |
{ |
| 422 |
if (p->data_size == 0 || p->data == NULL) |
| 423 |
return 1; |
| 424 |
|
| 425 |
OPENSSL_free(*out); |
| 426 |
*out = NULL; |
| 427 |
return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len); |
| 428 |
} |
| 429 |
|
| 430 |
static size_t x942kdf_size(KDF_X942 *ctx) |
| 431 |
{ |
| 432 |
int len; |
| 433 |
const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
| 434 |
|
| 435 |
if (md == NULL) { |
| 436 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 437 |
return 0; |
| 438 |
} |
| 439 |
len = EVP_MD_get_size(md); |
| 440 |
return (len <= 0) ? 0 : (size_t)len; |
| 441 |
} |
| 442 |
|
| 443 |
#ifdef FIPS_MODULE |
| 444 |
static int fips_x942kdf_key_check_passed(KDF_X942 *ctx) |
| 445 |
{ |
| 446 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 447 |
int key_approved = ossl_kdf_check_key_size(ctx->secret_len); |
| 448 |
|
| 449 |
if (!key_approved) { |
| 450 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
| 451 |
libctx, "X942KDF", "Key size", |
| 452 |
ossl_fips_config_x942kdf_key_check)) { |
| 453 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
} |
| 457 |
return 1; |
| 458 |
} |
| 459 |
#endif |
| 460 |
|
| 461 |
static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 462 |
const OSSL_PARAM params[]) |
| 463 |
{ |
| 464 |
KDF_X942 *ctx = (KDF_X942 *)vctx; |
| 465 |
const EVP_MD *md; |
| 466 |
int ret = 0; |
| 467 |
unsigned char *ctr; |
| 468 |
unsigned char *der = NULL; |
| 469 |
size_t der_len = 0; |
| 470 |
|
| 471 |
if (!ossl_prov_is_running() || !x942kdf_set_ctx_params(ctx, params)) |
| 472 |
return 0; |
| 473 |
|
| 474 |
/* |
| 475 |
* These 2 options encode to the same field so only one of them should be |
| 476 |
* active at once. |
| 477 |
*/ |
| 478 |
if (ctx->use_keybits && ctx->supp_pubinfo != NULL) { |
| 479 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PUBINFO); |
| 480 |
return 0; |
| 481 |
} |
| 482 |
/* |
| 483 |
* If the blob of acvp data is used then the individual info fields that it |
| 484 |
* replaces should not also be defined. |
| 485 |
*/ |
| 486 |
if (ctx->acvpinfo != NULL |
| 487 |
&& (ctx->partyuinfo != NULL |
| 488 |
|| ctx->partyvinfo != NULL |
| 489 |
|| ctx->supp_pubinfo != NULL |
| 490 |
|| ctx->supp_privinfo != NULL)) { |
| 491 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
| 492 |
return 0; |
| 493 |
} |
| 494 |
if (ctx->secret == NULL) { |
| 495 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
| 496 |
return 0; |
| 497 |
} |
| 498 |
md = ossl_prov_digest_md(&ctx->digest); |
| 499 |
if (md == NULL) { |
| 500 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) { |
| 504 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG); |
| 505 |
return 0; |
| 506 |
} |
| 507 |
if (ctx->partyuinfo != NULL && ctx->partyuinfo_len >= X942KDF_MAX_INLEN) { |
| 508 |
/* |
| 509 |
* Note the ukm length MUST be 512 bits if it is used. |
| 510 |
* For backwards compatibility the old check is being done. |
| 511 |
*/ |
| 512 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_UKM_LENGTH); |
| 513 |
return 0; |
| 514 |
} |
| 515 |
/* generate the otherinfo der */ |
| 516 |
if (!x942_encode_otherinfo(ctx->use_keybits ? ctx->dkm_len : 0, |
| 517 |
ctx->cek_oid, ctx->cek_oid_len, |
| 518 |
ctx->acvpinfo, ctx->acvpinfo_len, |
| 519 |
ctx->partyuinfo, ctx->partyuinfo_len, |
| 520 |
ctx->partyvinfo, ctx->partyvinfo_len, |
| 521 |
ctx->supp_pubinfo, ctx->supp_pubinfo_len, |
| 522 |
ctx->supp_privinfo, ctx->supp_privinfo_len, |
| 523 |
&der, &der_len, &ctr)) { |
| 524 |
ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING); |
| 525 |
return 0; |
| 526 |
} |
| 527 |
ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len, |
| 528 |
der, der_len, ctr, key, keylen); |
| 529 |
OPENSSL_free(der); |
| 530 |
return ret; |
| 531 |
} |
| 532 |
|
| 533 |
/* clang-format off */ |
| 534 |
{- produce_param_decoder('sshkdf_set_ctx_params', |
| 535 |
(['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 536 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 537 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 538 |
['OSSL_KDF_PARAM_SECRET', 'secret', 'octet_string'], |
| 539 |
['OSSL_KDF_PARAM_KEY', 'secret', 'octet_string'], |
| 540 |
['OSSL_KDF_PARAM_UKM', 'uinfo', 'octet_string'], |
| 541 |
['OSSL_KDF_PARAM_X942_ACVPINFO', 'acvp', 'octet_string'], |
| 542 |
['OSSL_KDF_PARAM_X942_PARTYUINFO', 'uinfo', 'octet_string'], |
| 543 |
['OSSL_KDF_PARAM_X942_PARTYVINFO', 'vinfo', 'octet_string'], |
| 544 |
['OSSL_KDF_PARAM_X942_SUPP_PUBINFO', 'pub', 'octet_string'], |
| 545 |
['OSSL_KDF_PARAM_X942_SUPP_PRIVINFO', 'priv', 'octet_string'], |
| 546 |
['OSSL_KDF_PARAM_X942_USE_KEYBITS', 'kbits', 'int'], |
| 547 |
['OSSL_KDF_PARAM_CEK_ALG', 'cekalg', 'utf8_string'], |
| 548 |
['OSSL_KDF_PARAM_FIPS_KEY_CHECK', 'ind_k', 'int', 'fips'], |
| 549 |
)); -} |
| 550 |
/* clang-format on */ |
| 551 |
|
| 552 |
static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 553 |
{ |
| 554 |
struct sshkdf_set_ctx_params_st p; |
| 555 |
KDF_X942 *ctx = vctx; |
| 556 |
OSSL_LIB_CTX *provctx; |
| 557 |
const char *cekalg, *propq = NULL; |
| 558 |
const EVP_MD *md; |
| 559 |
size_t id; |
| 560 |
|
| 561 |
if (ctx == NULL || !sshkdf_set_ctx_params_decoder(params, &p)) |
| 562 |
return 0; |
| 563 |
|
| 564 |
provctx = PROV_LIBCTX_OF(ctx->provctx); |
| 565 |
|
| 566 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
| 567 |
return 0; |
| 568 |
|
| 569 |
if (p.digest != NULL) { |
| 570 |
if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
| 571 |
p.propq, p.engine, provctx)) |
| 572 |
return 0; |
| 573 |
md = ossl_prov_digest_md(&ctx->digest); |
| 574 |
if (EVP_MD_xof(md)) { |
| 575 |
ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
| 576 |
return 0; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
if (p.secret != NULL) { |
| 581 |
if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p.secret)) |
| 582 |
return 0; |
| 583 |
#ifdef FIPS_MODULE |
| 584 |
if (!fips_x942kdf_key_check_passed(ctx)) |
| 585 |
return 0; |
| 586 |
#endif |
| 587 |
} |
| 588 |
|
| 589 |
if (p.acvp != NULL |
| 590 |
&& !x942kdf_set_buffer(&ctx->acvpinfo, &ctx->acvpinfo_len, p.acvp)) |
| 591 |
return 0; |
| 592 |
|
| 593 |
if (p.uinfo != NULL |
| 594 |
&& !x942kdf_set_buffer(&ctx->partyuinfo, &ctx->partyuinfo_len, p.uinfo)) |
| 595 |
return 0; |
| 596 |
|
| 597 |
if (p.vinfo != NULL |
| 598 |
&& !x942kdf_set_buffer(&ctx->partyvinfo, &ctx->partyvinfo_len, p.vinfo)) |
| 599 |
return 0; |
| 600 |
|
| 601 |
if (p.kbits != NULL && !OSSL_PARAM_get_int(p.kbits, &ctx->use_keybits)) |
| 602 |
return 0; |
| 603 |
|
| 604 |
if (p.pub != NULL) { |
| 605 |
if (!x942kdf_set_buffer(&ctx->supp_pubinfo, &ctx->supp_pubinfo_len, p.pub)) |
| 606 |
return 0; |
| 607 |
ctx->use_keybits = 0; |
| 608 |
} |
| 609 |
|
| 610 |
if (p.priv != NULL |
| 611 |
&& !x942kdf_set_buffer(&ctx->supp_privinfo, &ctx->supp_privinfo_len, p.priv)) |
| 612 |
return 0; |
| 613 |
|
| 614 |
if (p.cekalg != NULL) { |
| 615 |
if (!OSSL_PARAM_get_utf8_string_ptr(p.cekalg, &cekalg)) |
| 616 |
return 0; |
| 617 |
if (p.propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(p.propq, &propq)) |
| 618 |
return 0; |
| 619 |
if (find_alg_id(provctx, cekalg, propq, &id) == 0) |
| 620 |
return 0; |
| 621 |
ctx->cek_oid = kek_algs[id].oid; |
| 622 |
ctx->cek_oid_len = kek_algs[id].oid_len; |
| 623 |
ctx->dkm_len = kek_algs[id].keklen; |
| 624 |
} |
| 625 |
return 1; |
| 626 |
} |
| 627 |
|
| 628 |
static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *ctx, |
| 629 |
ossl_unused void *provctx) |
| 630 |
{ |
| 631 |
return sshkdf_set_ctx_params_list; |
| 632 |
} |
| 633 |
|
| 634 |
/* clang-format off */ |
| 635 |
{- produce_param_decoder('sshkdf_get_ctx_params', |
| 636 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 637 |
['OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR', 'ind', 'int', 'fips'], |
| 638 |
)); -} |
| 639 |
/* clang-format on */ |
| 640 |
|
| 641 |
static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 642 |
{ |
| 643 |
KDF_X942 *ctx = (KDF_X942 *)vctx; |
| 644 |
struct sshkdf_get_ctx_params_st p; |
| 645 |
|
| 646 |
if (ctx == NULL || !sshkdf_get_ctx_params_decoder(params, &p)) |
| 647 |
return 0; |
| 648 |
|
| 649 |
if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, x942kdf_size(ctx))) |
| 650 |
return 0; |
| 651 |
|
| 652 |
if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
| 653 |
return 0; |
| 654 |
return 1; |
| 655 |
} |
| 656 |
|
| 657 |
static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *ctx, |
| 658 |
ossl_unused void *provctx) |
| 659 |
{ |
| 660 |
return sshkdf_get_ctx_params_list; |
| 661 |
} |
| 662 |
|
| 663 |
const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = { |
| 664 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))x942kdf_new }, |
| 665 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))x942kdf_dup }, |
| 666 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))x942kdf_free }, |
| 667 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))x942kdf_reset }, |
| 668 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))x942kdf_derive }, |
| 669 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 670 |
(void (*)(void))x942kdf_settable_ctx_params }, |
| 671 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))x942kdf_set_ctx_params }, |
| 672 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 673 |
(void (*)(void))x942kdf_gettable_ctx_params }, |
| 674 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))x942kdf_get_ctx_params }, |
| 675 |
OSSL_DISPATCH_END |
| 676 |
}; |