1 /* 2 * Copyright 2020-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 /* 11 * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for 12 * internal use. 13 */ 14 #include "internal/deprecated.h" 15 16 #include <string.h> 17 #include <openssl/core_dispatch.h> 18 #include <openssl/core_names.h> 19 #include <openssl/bn.h> 20 #include <openssl/err.h> 21 #include <openssl/objects.h> 22 #include <openssl/proverr.h> 23 #include "crypto/bn.h" 24 #include "crypto/ec.h" 25 #include "prov/implementations.h" 26 #include "prov/providercommon.h" 27 #include "prov/provider_ctx.h" 28 #include "prov/securitycheck.h" 29 #include "internal/param_build_set.h" 30 31 #ifndef FIPS_MODULE 32 # ifndef OPENSSL_NO_SM2 33 # include "crypto/sm2.h" 34 # endif 35 #endif 36 37 static OSSL_FUNC_keymgmt_new_fn ec_newdata; 38 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init; 39 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template; 40 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params; 41 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params; 42 static OSSL_FUNC_keymgmt_gen_get_params_fn ec_gen_get_params; 43 static OSSL_FUNC_keymgmt_gen_gettable_params_fn ec_gen_gettable_params; 44 static OSSL_FUNC_keymgmt_gen_fn ec_gen; 45 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup; 46 static OSSL_FUNC_keymgmt_load_fn ec_load; 47 static OSSL_FUNC_keymgmt_free_fn ec_freedata; 48 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params; 49 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params; 50 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params; 51 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params; 52 static OSSL_FUNC_keymgmt_has_fn ec_has; 53 static OSSL_FUNC_keymgmt_match_fn ec_match; 54 static OSSL_FUNC_keymgmt_validate_fn ec_validate; 55 static OSSL_FUNC_keymgmt_import_fn ec_import; 56 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types; 57 static OSSL_FUNC_keymgmt_export_fn ec_export; 58 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types; 59 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name; 60 static OSSL_FUNC_keymgmt_dup_fn ec_dup; 61 #ifndef FIPS_MODULE 62 # ifndef OPENSSL_NO_SM2 63 static OSSL_FUNC_keymgmt_new_fn sm2_newdata; 64 static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init; 65 static OSSL_FUNC_keymgmt_gen_fn sm2_gen; 66 static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params; 67 static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params; 68 static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params; 69 static OSSL_FUNC_keymgmt_import_fn sm2_import; 70 static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name; 71 static OSSL_FUNC_keymgmt_validate_fn sm2_validate; 72 # endif 73 #endif 74 75 #define EC_DEFAULT_MD "SHA256" 76 #define EC_POSSIBLE_SELECTIONS \ 77 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) 78 #define SM2_DEFAULT_MD "SM3" 79 80 static 81 const char *ec_query_operation_name(int operation_id) 82 { 83 switch (operation_id) { 84 case OSSL_OP_KEYEXCH: 85 return "ECDH"; 86 case OSSL_OP_SIGNATURE: 87 return "ECDSA"; 88 } 89 return NULL; 90 } 91 92 #ifndef FIPS_MODULE 93 # ifndef OPENSSL_NO_SM2 94 static 95 const char *sm2_query_operation_name(int operation_id) 96 { 97 switch (operation_id) { 98 case OSSL_OP_SIGNATURE: 99 return "SM2"; 100 } 101 return NULL; 102 } 103 # endif 104 #endif 105 106 /* 107 * Callers of key_to_params MUST make sure that domparams_to_params is also 108 * called! 109 * 110 * This function only exports the bare keypair, domain parameters and other 111 * parameters are exported separately. 112 */ 113 static ossl_inline 114 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, 115 OSSL_PARAM params[], int include_private, 116 unsigned char **pub_key) 117 { 118 BIGNUM *x = NULL, *y = NULL; 119 const BIGNUM *priv_key = NULL; 120 const EC_POINT *pub_point = NULL; 121 const EC_GROUP *ecg = NULL; 122 size_t pub_key_len = 0; 123 int ret = 0; 124 BN_CTX *bnctx = NULL; 125 126 if (eckey == NULL 127 || (ecg = EC_KEY_get0_group(eckey)) == NULL) 128 return 0; 129 130 priv_key = EC_KEY_get0_private_key(eckey); 131 pub_point = EC_KEY_get0_public_key(eckey); 132 133 if (pub_point != NULL) { 134 OSSL_PARAM *p = NULL, *px = NULL, *py = NULL; 135 /* 136 * EC_POINT_point2buf() can generate random numbers in some 137 * implementations so we need to ensure we use the correct libctx. 138 */ 139 bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey)); 140 if (bnctx == NULL) 141 goto err; 142 143 144 /* If we are doing a get then check first before decoding the point */ 145 if (tmpl == NULL) { 146 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY); 147 px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X); 148 py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y); 149 } 150 151 if (p != NULL || tmpl != NULL) { 152 /* convert pub_point to a octet string according to the SECG standard */ 153 point_conversion_form_t format = EC_KEY_get_conv_form(eckey); 154 155 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point, 156 format, 157 pub_key, bnctx)) == 0 158 || !ossl_param_build_set_octet_string(tmpl, p, 159 OSSL_PKEY_PARAM_PUB_KEY, 160 *pub_key, pub_key_len)) 161 goto err; 162 } 163 if (px != NULL || py != NULL) { 164 if (px != NULL) { 165 x = BN_CTX_get(bnctx); 166 if (x == NULL) 167 goto err; 168 } 169 if (py != NULL) { 170 y = BN_CTX_get(bnctx); 171 if (y == NULL) 172 goto err; 173 } 174 175 if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx)) 176 goto err; 177 if (px != NULL 178 && !ossl_param_build_set_bn(tmpl, px, 179 OSSL_PKEY_PARAM_EC_PUB_X, x)) 180 goto err; 181 if (py != NULL 182 && !ossl_param_build_set_bn(tmpl, py, 183 OSSL_PKEY_PARAM_EC_PUB_Y, y)) 184 goto err; 185 } 186 } 187 188 if (priv_key != NULL && include_private) { 189 size_t sz; 190 int ecbits; 191 192 /* 193 * Key import/export should never leak the bit length of the secret 194 * scalar in the key. 195 * 196 * For this reason, on export we use padded BIGNUMs with fixed length. 197 * 198 * When importing we also should make sure that, even if short lived, 199 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as 200 * soon as possible, so that any processing of this BIGNUM might opt for 201 * constant time implementations in the backend. 202 * 203 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have 204 * to preallocate the BIGNUM internal buffer to a fixed public size big 205 * enough that operations performed during the processing never trigger 206 * a realloc which would leak the size of the scalar through memory 207 * accesses. 208 * 209 * Fixed Length 210 * ------------ 211 * 212 * The order of the large prime subgroup of the curve is our choice for 213 * a fixed public size, as that is generally the upper bound for 214 * generating a private key in EC cryptosystems and should fit all valid 215 * secret scalars. 216 * 217 * For padding on export we just use the bit length of the order 218 * converted to bytes (rounding up). 219 * 220 * For preallocating the BIGNUM storage we look at the number of "words" 221 * required for the internal representation of the order, and we 222 * preallocate 2 extra "words" in case any of the subsequent processing 223 * might temporarily overflow the order length. 224 */ 225 ecbits = EC_GROUP_order_bits(ecg); 226 if (ecbits <= 0) 227 goto err; 228 sz = (ecbits + 7) / 8; 229 230 if (!ossl_param_build_set_bn_pad(tmpl, params, 231 OSSL_PKEY_PARAM_PRIV_KEY, 232 priv_key, sz)) 233 goto err; 234 } 235 ret = 1; 236 err: 237 BN_CTX_free(bnctx); 238 return ret; 239 } 240 241 static ossl_inline 242 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl, 243 OSSL_PARAM params[]) 244 { 245 int ecdh_cofactor_mode = 0, group_check = 0; 246 const char *name = NULL; 247 point_conversion_form_t format; 248 249 if (ec == NULL) 250 return 0; 251 252 format = EC_KEY_get_conv_form(ec); 253 name = ossl_ec_pt_format_id2name((int)format); 254 if (name != NULL 255 && !ossl_param_build_set_utf8_string(tmpl, params, 256 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, 257 name)) 258 return 0; 259 260 group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK; 261 name = ossl_ec_check_group_type_id2name(group_check); 262 if (name != NULL 263 && !ossl_param_build_set_utf8_string(tmpl, params, 264 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, 265 name)) 266 return 0; 267 268 if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0 269 && !ossl_param_build_set_int(tmpl, params, 270 OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) 271 return 0; 272 273 ecdh_cofactor_mode = 274 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; 275 return ossl_param_build_set_int(tmpl, params, 276 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, 277 ecdh_cofactor_mode); 278 } 279 280 static 281 void *ec_newdata(void *provctx) 282 { 283 if (!ossl_prov_is_running()) 284 return NULL; 285 return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL); 286 } 287 288 #ifndef FIPS_MODULE 289 # ifndef OPENSSL_NO_SM2 290 static 291 void *sm2_newdata(void *provctx) 292 { 293 if (!ossl_prov_is_running()) 294 return NULL; 295 return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2); 296 } 297 # endif 298 #endif 299 300 static 301 void ec_freedata(void *keydata) 302 { 303 EC_KEY_free(keydata); 304 } 305 306 static 307 int ec_has(const void *keydata, int selection) 308 { 309 const EC_KEY *ec = keydata; 310 int ok = 1; 311 312 if (!ossl_prov_is_running() || ec == NULL) 313 return 0; 314 if ((selection & EC_POSSIBLE_SELECTIONS) == 0) 315 return 1; /* the selection is not missing */ 316 317 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) 318 ok = ok && (EC_KEY_get0_public_key(ec) != NULL); 319 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 320 ok = ok && (EC_KEY_get0_private_key(ec) != NULL); 321 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 322 ok = ok && (EC_KEY_get0_group(ec) != NULL); 323 /* 324 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be 325 * available, so no extra check is needed other than the previous one 326 * against EC_POSSIBLE_SELECTIONS. 327 */ 328 return ok; 329 } 330 331 static int ec_match(const void *keydata1, const void *keydata2, int selection) 332 { 333 const EC_KEY *ec1 = keydata1; 334 const EC_KEY *ec2 = keydata2; 335 const EC_GROUP *group_a = EC_KEY_get0_group(ec1); 336 const EC_GROUP *group_b = EC_KEY_get0_group(ec2); 337 BN_CTX *ctx = NULL; 338 int ok = 1; 339 340 if (!ossl_prov_is_running()) 341 return 0; 342 343 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1)); 344 if (ctx == NULL) 345 return 0; 346 347 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 348 ok = ok && group_a != NULL && group_b != NULL 349 && EC_GROUP_cmp(group_a, group_b, ctx) == 0; 350 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 351 int key_checked = 0; 352 353 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { 354 const EC_POINT *pa = EC_KEY_get0_public_key(ec1); 355 const EC_POINT *pb = EC_KEY_get0_public_key(ec2); 356 357 if (pa != NULL && pb != NULL) { 358 ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0; 359 key_checked = 1; 360 } 361 } 362 if (!key_checked 363 && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { 364 const BIGNUM *pa = EC_KEY_get0_private_key(ec1); 365 const BIGNUM *pb = EC_KEY_get0_private_key(ec2); 366 367 if (pa != NULL && pb != NULL) { 368 ok = ok && BN_cmp(pa, pb) == 0; 369 key_checked = 1; 370 } 371 } 372 ok = ok && key_checked; 373 } 374 BN_CTX_free(ctx); 375 return ok; 376 } 377 378 static int common_check_sm2(const EC_KEY *ec, int sm2_wanted) 379 { 380 const EC_GROUP *ecg = NULL; 381 382 /* 383 * sm2_wanted: import the keys or domparams only on SM2 Curve 384 * !sm2_wanted: import the keys or domparams only not on SM2 Curve 385 */ 386 if ((ecg = EC_KEY_get0_group(ec)) == NULL 387 || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2))) 388 return 0; 389 return 1; 390 } 391 392 static 393 int common_import(void *keydata, int selection, const OSSL_PARAM params[], 394 int sm2_wanted) 395 { 396 EC_KEY *ec = keydata; 397 int ok = 1; 398 399 if (!ossl_prov_is_running() || ec == NULL) 400 return 0; 401 402 /* 403 * In this implementation, we can export/import only keydata in the 404 * following combinations: 405 * - domain parameters (+optional other params) 406 * - public key with associated domain parameters (+optional other params) 407 * - private key with associated domain parameters and optional public key 408 * (+optional other params) 409 * 410 * This means: 411 * - domain parameters must always be requested 412 * - private key must be requested alongside public key 413 * - other parameters are always optional 414 */ 415 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0) 416 return 0; 417 418 ok = ok && ossl_ec_group_fromdata(ec, params); 419 420 if (!common_check_sm2(ec, sm2_wanted)) 421 return 0; 422 423 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 424 int include_private = 425 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; 426 427 ok = ok && ossl_ec_key_fromdata(ec, params, include_private); 428 } 429 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) 430 ok = ok && ossl_ec_key_otherparams_fromdata(ec, params); 431 432 return ok; 433 } 434 435 static 436 int ec_import(void *keydata, int selection, const OSSL_PARAM params[]) 437 { 438 return common_import(keydata, selection, params, 0); 439 } 440 441 #ifndef FIPS_MODULE 442 # ifndef OPENSSL_NO_SM2 443 static 444 int sm2_import(void *keydata, int selection, const OSSL_PARAM params[]) 445 { 446 return common_import(keydata, selection, params, 1); 447 } 448 # endif 449 #endif 450 451 static 452 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, 453 void *cbarg) 454 { 455 EC_KEY *ec = keydata; 456 OSSL_PARAM_BLD *tmpl = NULL; 457 OSSL_PARAM *params = NULL; 458 unsigned char *pub_key = NULL, *genbuf = NULL; 459 BN_CTX *bnctx = NULL; 460 int ok = 1; 461 462 if (!ossl_prov_is_running() || ec == NULL) 463 return 0; 464 465 /* 466 * In this implementation, we can export/import only keydata in the 467 * following combinations: 468 * - domain parameters (+optional other params) 469 * - public key with associated domain parameters (+optional other params) 470 * - private key with associated public key and domain parameters 471 * (+optional other params) 472 * 473 * This means: 474 * - domain parameters must always be requested 475 * - private key must be requested alongside public key 476 * - other parameters are always optional 477 */ 478 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0) 479 return 0; 480 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 481 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0) 482 return 0; 483 484 tmpl = OSSL_PARAM_BLD_new(); 485 if (tmpl == NULL) 486 return 0; 487 488 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { 489 bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec)); 490 if (bnctx == NULL) { 491 ok = 0; 492 goto end; 493 } 494 BN_CTX_start(bnctx); 495 ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL, 496 ossl_ec_key_get_libctx(ec), 497 ossl_ec_key_get0_propq(ec), 498 bnctx, &genbuf); 499 } 500 501 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 502 int include_private = 503 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; 504 505 ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key); 506 } 507 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) 508 ok = ok && otherparams_to_params(ec, tmpl, NULL); 509 510 if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { 511 ok = 0; 512 goto end; 513 } 514 515 ok = param_cb(params, cbarg); 516 OSSL_PARAM_free(params); 517 end: 518 OSSL_PARAM_BLD_free(tmpl); 519 OPENSSL_free(pub_key); 520 OPENSSL_free(genbuf); 521 BN_CTX_end(bnctx); 522 BN_CTX_free(bnctx); 523 return ok; 524 } 525 526 /* IMEXPORT = IMPORT + EXPORT */ 527 528 # define EC_IMEXPORTABLE_DOM_PARAMETERS \ 529 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \ 530 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \ 531 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\ 532 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \ 533 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \ 534 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \ 535 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \ 536 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \ 537 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \ 538 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \ 539 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), \ 540 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL) 541 542 # define EC_IMEXPORTABLE_PUBLIC_KEY \ 543 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) 544 # define EC_IMEXPORTABLE_PRIVATE_KEY \ 545 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) 546 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \ 547 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \ 548 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL) 549 550 /* 551 * Include all the possible combinations of OSSL_PARAM arrays for 552 * ec_imexport_types(). 553 * 554 * They are in a separate file as it is ~100 lines of unreadable and 555 * uninteresting machine generated stuff. 556 */ 557 #include "ec_kmgmt_imexport.inc" 558 559 static ossl_inline 560 const OSSL_PARAM *ec_imexport_types(int selection) 561 { 562 int type_select = 0; 563 564 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 565 type_select += 1; 566 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) 567 type_select += 2; 568 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 569 type_select += 4; 570 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) 571 type_select += 8; 572 return ec_types[type_select]; 573 } 574 575 static 576 const OSSL_PARAM *ec_import_types(int selection) 577 { 578 return ec_imexport_types(selection); 579 } 580 581 static 582 const OSSL_PARAM *ec_export_types(int selection) 583 { 584 return ec_imexport_types(selection); 585 } 586 587 static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[]) 588 { 589 #ifdef OPENSSL_NO_EC2M 590 return 1; 591 #else 592 int ret = 0, m; 593 unsigned int k1 = 0, k2 = 0, k3 = 0; 594 int basis_nid; 595 const char *basis_name = NULL; 596 int fid = EC_GROUP_get_field_type(group); 597 598 if (fid != NID_X9_62_characteristic_two_field) 599 return 1; 600 601 basis_nid = EC_GROUP_get_basis_type(group); 602 if (basis_nid == NID_X9_62_tpBasis) 603 basis_name = SN_X9_62_tpBasis; 604 else if (basis_nid == NID_X9_62_ppBasis) 605 basis_name = SN_X9_62_ppBasis; 606 else 607 goto err; 608 609 m = EC_GROUP_get_degree(group); 610 if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m) 611 || !ossl_param_build_set_utf8_string(NULL, params, 612 OSSL_PKEY_PARAM_EC_CHAR2_TYPE, 613 basis_name)) 614 goto err; 615 616 if (basis_nid == NID_X9_62_tpBasis) { 617 if (!EC_GROUP_get_trinomial_basis(group, &k1) 618 || !ossl_param_build_set_int(NULL, params, 619 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, 620 (int)k1)) 621 goto err; 622 } else { 623 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3) 624 || !ossl_param_build_set_int(NULL, params, 625 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1) 626 || !ossl_param_build_set_int(NULL, params, 627 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2) 628 || !ossl_param_build_set_int(NULL, params, 629 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3)) 630 goto err; 631 } 632 ret = 1; 633 err: 634 return ret; 635 #endif /* OPENSSL_NO_EC2M */ 636 } 637 638 static 639 int common_get_params(void *key, OSSL_PARAM params[], int sm2) 640 { 641 int ret = 0; 642 EC_KEY *eck = key; 643 const EC_GROUP *ecg = NULL; 644 OSSL_PARAM *p; 645 unsigned char *pub_key = NULL, *genbuf = NULL; 646 OSSL_LIB_CTX *libctx; 647 const char *propq; 648 BN_CTX *bnctx = NULL; 649 650 ecg = EC_KEY_get0_group(eck); 651 if (ecg == NULL) { 652 ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET); 653 return 0; 654 } 655 656 libctx = ossl_ec_key_get_libctx(eck); 657 propq = ossl_ec_key_get0_propq(eck); 658 659 bnctx = BN_CTX_new_ex(libctx); 660 if (bnctx == NULL) 661 return 0; 662 BN_CTX_start(bnctx); 663 664 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL 665 && !OSSL_PARAM_set_int(p, ECDSA_size(eck))) 666 goto err; 667 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL 668 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg))) 669 goto err; 670 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) { 671 int ecbits, sec_bits; 672 673 ecbits = EC_GROUP_order_bits(ecg); 674 675 /* 676 * The following estimates are based on the values published 677 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4" 678 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 . 679 * 680 * Note that the above reference explicitly categorizes algorithms in a 681 * discrete set of values {80, 112, 128, 192, 256}, and that it is 682 * relevant only for NIST approved Elliptic Curves, while OpenSSL 683 * applies the same logic also to other curves. 684 * 685 * Classifications produced by other standardazing bodies might differ, 686 * so the results provided for "bits of security" by this provider are 687 * to be considered merely indicative, and it is the users' 688 * responsibility to compare these values against the normative 689 * references that may be relevant for their intent and purposes. 690 */ 691 if (ecbits >= 512) 692 sec_bits = 256; 693 else if (ecbits >= 384) 694 sec_bits = 192; 695 else if (ecbits >= 256) 696 sec_bits = 128; 697 else if (ecbits >= 224) 698 sec_bits = 112; 699 else if (ecbits >= 160) 700 sec_bits = 80; 701 else 702 sec_bits = ecbits / 2; 703 704 if (!OSSL_PARAM_set_int(p, sec_bits)) 705 goto err; 706 } 707 708 if ((p = OSSL_PARAM_locate(params, 709 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS)) 710 != NULL) { 711 int explicitparams = EC_KEY_decoded_from_explicit_params(eck); 712 713 if (explicitparams < 0 714 || !OSSL_PARAM_set_int(p, explicitparams)) 715 goto err; 716 } 717 718 if (!sm2) { 719 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL 720 && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD)) 721 goto err; 722 } else { 723 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL 724 && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD)) 725 goto err; 726 } 727 728 /* SM2 doesn't support this PARAM */ 729 if (!sm2) { 730 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH); 731 if (p != NULL) { 732 int ecdh_cofactor_mode = 0; 733 734 ecdh_cofactor_mode = 735 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; 736 737 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode)) 738 goto err; 739 } 740 } 741 if ((p = OSSL_PARAM_locate(params, 742 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) { 743 const EC_POINT *ecp = EC_KEY_get0_public_key(key); 744 745 if (ecp == NULL) { 746 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); 747 goto err; 748 } 749 p->return_size = EC_POINT_point2oct(ecg, ecp, 750 POINT_CONVERSION_UNCOMPRESSED, 751 p->data, p->data_size, bnctx); 752 if (p->return_size == 0) 753 goto err; 754 } 755 756 ret = ec_get_ecm_params(ecg, params) 757 && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx, 758 &genbuf) 759 && key_to_params(eck, NULL, params, 1, &pub_key) 760 && otherparams_to_params(eck, NULL, params); 761 err: 762 OPENSSL_free(genbuf); 763 OPENSSL_free(pub_key); 764 BN_CTX_end(bnctx); 765 BN_CTX_free(bnctx); 766 return ret; 767 } 768 769 static 770 int ec_get_params(void *key, OSSL_PARAM params[]) 771 { 772 return common_get_params(key, params, 0); 773 } 774 775 #ifndef OPENSSL_NO_EC2M 776 # define EC2M_GETTABLE_DOM_PARAMS \ 777 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \ 778 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \ 779 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \ 780 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \ 781 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \ 782 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL), 783 #else 784 # define EC2M_GETTABLE_DOM_PARAMS 785 #endif 786 787 static const OSSL_PARAM ec_known_gettable_params[] = { 788 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), 789 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), 790 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), 791 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), 792 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), 793 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL), 794 EC_IMEXPORTABLE_DOM_PARAMETERS, 795 EC2M_GETTABLE_DOM_PARAMS 796 EC_IMEXPORTABLE_PUBLIC_KEY, 797 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0), 798 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0), 799 EC_IMEXPORTABLE_PRIVATE_KEY, 800 EC_IMEXPORTABLE_OTHER_PARAMETERS, 801 OSSL_PARAM_END 802 }; 803 804 static 805 const OSSL_PARAM *ec_gettable_params(void *provctx) 806 { 807 return ec_known_gettable_params; 808 } 809 810 static const OSSL_PARAM ec_known_settable_params[] = { 811 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), 812 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), 813 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), 814 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), 815 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), 816 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL), 817 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0), 818 OSSL_PARAM_END 819 }; 820 821 static 822 const OSSL_PARAM *ec_settable_params(void *provctx) 823 { 824 return ec_known_settable_params; 825 } 826 827 static 828 int ec_set_params(void *key, const OSSL_PARAM params[]) 829 { 830 EC_KEY *eck = key; 831 const OSSL_PARAM *p; 832 833 if (key == NULL) 834 return 0; 835 if (ossl_param_is_empty(params)) 836 return 1; 837 838 if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params)) 839 return 0; 840 841 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY); 842 if (p != NULL) { 843 BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key)); 844 int ret = 1; 845 846 if (ctx == NULL 847 || p->data_type != OSSL_PARAM_OCTET_STRING 848 || !EC_KEY_oct2key(key, p->data, p->data_size, ctx)) 849 ret = 0; 850 BN_CTX_free(ctx); 851 if (!ret) 852 return 0; 853 } 854 855 return ossl_ec_key_otherparams_fromdata(eck, params); 856 } 857 858 #ifndef FIPS_MODULE 859 # ifndef OPENSSL_NO_SM2 860 static 861 int sm2_get_params(void *key, OSSL_PARAM params[]) 862 { 863 return common_get_params(key, params, 1); 864 } 865 866 static const OSSL_PARAM sm2_known_gettable_params[] = { 867 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), 868 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), 869 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), 870 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), 871 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), 872 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL), 873 EC_IMEXPORTABLE_DOM_PARAMETERS, 874 EC_IMEXPORTABLE_PUBLIC_KEY, 875 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0), 876 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0), 877 EC_IMEXPORTABLE_PRIVATE_KEY, 878 OSSL_PARAM_END 879 }; 880 881 static 882 const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx) 883 { 884 return sm2_known_gettable_params; 885 } 886 887 static const OSSL_PARAM sm2_known_settable_params[] = { 888 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), 889 OSSL_PARAM_END 890 }; 891 892 static 893 const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx) 894 { 895 return sm2_known_settable_params; 896 } 897 898 static 899 int sm2_validate(const void *keydata, int selection, int checktype) 900 { 901 const EC_KEY *eck = keydata; 902 int ok = 1; 903 BN_CTX *ctx = NULL; 904 905 if (!ossl_prov_is_running()) 906 return 0; 907 908 if ((selection & EC_POSSIBLE_SELECTIONS) == 0) 909 return 1; /* nothing to validate */ 910 911 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck)); 912 if (ctx == NULL) 913 return 0; 914 915 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 916 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx); 917 918 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { 919 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) 920 ok = ok && ossl_ec_key_public_check_quick(eck, ctx); 921 else 922 ok = ok && ossl_ec_key_public_check(eck, ctx); 923 } 924 925 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 926 ok = ok && ossl_sm2_key_private_check(eck); 927 928 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) 929 ok = ok && ossl_ec_key_pairwise_check(eck, ctx); 930 931 BN_CTX_free(ctx); 932 return ok; 933 } 934 # endif 935 #endif 936 937 static 938 int ec_validate(const void *keydata, int selection, int checktype) 939 { 940 const EC_KEY *eck = keydata; 941 int ok = 1; 942 BN_CTX *ctx = NULL; 943 944 if (!ossl_prov_is_running()) 945 return 0; 946 947 if ((selection & EC_POSSIBLE_SELECTIONS) == 0) 948 return 1; /* nothing to validate */ 949 950 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck)); 951 if (ctx == NULL) 952 return 0; 953 954 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { 955 int flags = EC_KEY_get_flags(eck); 956 957 if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0) 958 ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck), 959 (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0; 960 else 961 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx); 962 } 963 964 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { 965 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) 966 ok = ok && ossl_ec_key_public_check_quick(eck, ctx); 967 else 968 ok = ok && ossl_ec_key_public_check(eck, ctx); 969 } 970 971 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 972 ok = ok && ossl_ec_key_private_check(eck); 973 974 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) 975 ok = ok && ossl_ec_key_pairwise_check(eck, ctx); 976 977 BN_CTX_free(ctx); 978 return ok; 979 } 980 981 struct ec_gen_ctx { 982 OSSL_LIB_CTX *libctx; 983 char *group_name; 984 char *encoding; 985 char *pt_format; 986 char *group_check; 987 char *field_type; 988 BIGNUM *p, *a, *b, *order, *cofactor; 989 unsigned char *gen, *seed; 990 size_t gen_len, seed_len; 991 int selection; 992 int ecdh_mode; 993 EC_GROUP *gen_group; 994 unsigned char *dhkem_ikm; 995 size_t dhkem_ikmlen; 996 OSSL_FIPS_IND_DECLARE 997 }; 998 999 static void *ec_gen_init(void *provctx, int selection, 1000 const OSSL_PARAM params[]) 1001 { 1002 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); 1003 struct ec_gen_ctx *gctx = NULL; 1004 1005 if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0) 1006 return NULL; 1007 1008 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { 1009 gctx->libctx = libctx; 1010 gctx->selection = selection; 1011 gctx->ecdh_mode = 0; 1012 OSSL_FIPS_IND_INIT(gctx) 1013 if (!ec_gen_set_params(gctx, params)) { 1014 OPENSSL_free(gctx); 1015 gctx = NULL; 1016 } 1017 } 1018 return gctx; 1019 } 1020 1021 #ifndef FIPS_MODULE 1022 # ifndef OPENSSL_NO_SM2 1023 static void *sm2_gen_init(void *provctx, int selection, 1024 const OSSL_PARAM params[]) 1025 { 1026 struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params); 1027 1028 if (gctx != NULL) { 1029 if (gctx->group_name != NULL) 1030 return gctx; 1031 if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL) 1032 return gctx; 1033 ec_gen_cleanup(gctx); 1034 } 1035 return NULL; 1036 } 1037 # endif 1038 #endif 1039 1040 static int ec_gen_set_group(void *genctx, const EC_GROUP *src) 1041 { 1042 struct ec_gen_ctx *gctx = genctx; 1043 EC_GROUP *group; 1044 1045 group = EC_GROUP_dup(src); 1046 if (group == NULL) { 1047 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE); 1048 return 0; 1049 } 1050 EC_GROUP_free(gctx->gen_group); 1051 gctx->gen_group = group; 1052 return 1; 1053 } 1054 1055 static int ec_gen_set_template(void *genctx, void *templ) 1056 { 1057 struct ec_gen_ctx *gctx = genctx; 1058 EC_KEY *ec = templ; 1059 const EC_GROUP *ec_group; 1060 1061 if (!ossl_prov_is_running() || gctx == NULL || ec == NULL) 1062 return 0; 1063 if ((ec_group = EC_KEY_get0_group(ec)) == NULL) 1064 return 0; 1065 return ec_gen_set_group(gctx, ec_group); 1066 } 1067 1068 #define COPY_INT_PARAM(params, key, val) \ 1069 p = OSSL_PARAM_locate_const(params, key); \ 1070 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \ 1071 goto err; 1072 1073 #define COPY_UTF8_PARAM(params, key, val) \ 1074 p = OSSL_PARAM_locate_const(params, key); \ 1075 if (p != NULL) { \ 1076 if (p->data_type != OSSL_PARAM_UTF8_STRING) \ 1077 goto err; \ 1078 OPENSSL_free(val); \ 1079 val = OPENSSL_strdup(p->data); \ 1080 if (val == NULL) \ 1081 goto err; \ 1082 } 1083 1084 #define COPY_OCTET_PARAM(params, key, val, len) \ 1085 p = OSSL_PARAM_locate_const(params, key); \ 1086 if (p != NULL) { \ 1087 if (p->data_type != OSSL_PARAM_OCTET_STRING) \ 1088 goto err; \ 1089 OPENSSL_free(val); \ 1090 len = p->data_size; \ 1091 val = OPENSSL_memdup(p->data, p->data_size); \ 1092 if (val == NULL) \ 1093 goto err; \ 1094 } 1095 1096 #define COPY_BN_PARAM(params, key, bn) \ 1097 p = OSSL_PARAM_locate_const(params, key); \ 1098 if (p != NULL) { \ 1099 if (bn == NULL) \ 1100 bn = BN_new(); \ 1101 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \ 1102 goto err; \ 1103 } 1104 1105 static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[]) 1106 { 1107 int ret = 0; 1108 struct ec_gen_ctx *gctx = genctx; 1109 const OSSL_PARAM *p; 1110 1111 if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params, 1112 OSSL_PKEY_PARAM_FIPS_KEY_CHECK)) 1113 goto err; 1114 1115 COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode); 1116 1117 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name); 1118 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type); 1119 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding); 1120 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format); 1121 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check); 1122 1123 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p); 1124 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a); 1125 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b); 1126 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order); 1127 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor); 1128 1129 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len); 1130 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen, 1131 gctx->gen_len); 1132 1133 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm, 1134 gctx->dhkem_ikmlen); 1135 1136 ret = 1; 1137 err: 1138 return ret; 1139 } 1140 1141 static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx) 1142 { 1143 int ret = 0; 1144 OSSL_PARAM_BLD *bld; 1145 OSSL_PARAM *params = NULL; 1146 EC_GROUP *group = NULL; 1147 1148 bld = OSSL_PARAM_BLD_new(); 1149 if (bld == NULL) 1150 return 0; 1151 1152 if (gctx->encoding != NULL 1153 && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING, 1154 gctx->encoding, 0)) 1155 goto err; 1156 1157 if (gctx->pt_format != NULL 1158 && !OSSL_PARAM_BLD_push_utf8_string(bld, 1159 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, 1160 gctx->pt_format, 0)) 1161 goto err; 1162 1163 if (gctx->group_name != NULL) { 1164 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, 1165 gctx->group_name, 0)) 1166 goto err; 1167 /* Ignore any other parameters if there is a group name */ 1168 goto build; 1169 } else if (gctx->field_type != NULL) { 1170 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE, 1171 gctx->field_type, 0)) 1172 goto err; 1173 } else { 1174 goto err; 1175 } 1176 if (gctx->p == NULL 1177 || gctx->a == NULL 1178 || gctx->b == NULL 1179 || gctx->order == NULL 1180 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p) 1181 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a) 1182 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b) 1183 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order)) 1184 goto err; 1185 1186 if (gctx->cofactor != NULL 1187 && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, 1188 gctx->cofactor)) 1189 goto err; 1190 1191 if (gctx->seed != NULL 1192 && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED, 1193 gctx->seed, gctx->seed_len)) 1194 goto err; 1195 1196 if (gctx->gen == NULL 1197 || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR, 1198 gctx->gen, gctx->gen_len)) 1199 goto err; 1200 build: 1201 params = OSSL_PARAM_BLD_to_param(bld); 1202 if (params == NULL) 1203 goto err; 1204 group = EC_GROUP_new_from_params(params, gctx->libctx, NULL); 1205 if (group == NULL) 1206 goto err; 1207 1208 EC_GROUP_free(gctx->gen_group); 1209 gctx->gen_group = group; 1210 1211 ret = 1; 1212 err: 1213 OSSL_PARAM_free(params); 1214 OSSL_PARAM_BLD_free(bld); 1215 return ret; 1216 } 1217 1218 static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx, 1219 ossl_unused void *provctx) 1220 { 1221 static OSSL_PARAM settable[] = { 1222 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), 1223 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), 1224 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), 1225 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), 1226 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), 1227 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), 1228 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), 1229 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), 1230 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), 1231 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), 1232 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), 1233 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), 1234 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0), 1235 OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_KEY_CHECK) 1236 OSSL_PARAM_END 1237 }; 1238 return settable; 1239 } 1240 1241 static const OSSL_PARAM *ec_gen_gettable_params(ossl_unused void *genctx, 1242 ossl_unused void *provctx) 1243 { 1244 static const OSSL_PARAM known_ec_gen_gettable_ctx_params[] = { 1245 OSSL_FIPS_IND_GETTABLE_CTX_PARAM() 1246 OSSL_PARAM_END 1247 }; 1248 return known_ec_gen_gettable_ctx_params; 1249 } 1250 1251 static int ec_gen_get_params(void *genctx, OSSL_PARAM *params) 1252 { 1253 struct ec_gen_ctx *gctx = genctx; 1254 1255 if (gctx == NULL) 1256 return 0; 1257 1258 if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params)) 1259 return 0; 1260 1261 return 1; 1262 } 1263 1264 static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group) 1265 { 1266 if (group == NULL) { 1267 ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET); 1268 return 0; 1269 } 1270 return EC_KEY_set_group(ec, group) > 0; 1271 } 1272 1273 /* 1274 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation 1275 */ 1276 static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) 1277 { 1278 struct ec_gen_ctx *gctx = genctx; 1279 EC_KEY *ec = NULL; 1280 int ret = 0; 1281 1282 if (!ossl_prov_is_running() 1283 || gctx == NULL 1284 || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL) 1285 return NULL; 1286 1287 if (gctx->gen_group == NULL) { 1288 if (!ec_gen_set_group_from_params(gctx)) 1289 goto err; 1290 } else { 1291 if (gctx->encoding != NULL) { 1292 int flags = ossl_ec_encoding_name2id(gctx->encoding); 1293 1294 if (flags < 0) 1295 goto err; 1296 EC_GROUP_set_asn1_flag(gctx->gen_group, flags); 1297 } 1298 if (gctx->pt_format != NULL) { 1299 int format = ossl_ec_pt_format_name2id(gctx->pt_format); 1300 1301 if (format < 0) 1302 goto err; 1303 EC_GROUP_set_point_conversion_form(gctx->gen_group, format); 1304 } 1305 } 1306 #ifdef FIPS_MODULE 1307 if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(gctx), 1308 OSSL_FIPS_IND_SETTABLE0, gctx->libctx, 1309 gctx->gen_group, "EC KeyGen", 1)) 1310 goto err; 1311 #endif 1312 1313 /* We must always assign a group, no matter what */ 1314 ret = ec_gen_assign_group(ec, gctx->gen_group); 1315 1316 /* Whether you want it or not, you get a keypair, not just one half */ 1317 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 1318 #ifndef FIPS_MODULE 1319 if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) 1320 ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm, 1321 gctx->dhkem_ikmlen); 1322 else 1323 #endif 1324 ret = ret && EC_KEY_generate_key(ec); 1325 } 1326 1327 if (gctx->ecdh_mode != -1) 1328 ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode); 1329 1330 if (gctx->group_check != NULL) 1331 ret = ret && ossl_ec_set_check_group_type_from_name(ec, 1332 gctx->group_check); 1333 if (ret) 1334 return ec; 1335 err: 1336 /* Something went wrong, throw the key away */ 1337 EC_KEY_free(ec); 1338 return NULL; 1339 } 1340 1341 #ifndef FIPS_MODULE 1342 # ifndef OPENSSL_NO_SM2 1343 /* 1344 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation 1345 */ 1346 static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) 1347 { 1348 struct ec_gen_ctx *gctx = genctx; 1349 EC_KEY *ec = NULL; 1350 int ret = 1; 1351 1352 if (gctx == NULL 1353 || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL) 1354 return NULL; 1355 1356 if (gctx->gen_group == NULL) { 1357 if (!ec_gen_set_group_from_params(gctx)) 1358 goto err; 1359 } else { 1360 if (gctx->encoding) { 1361 int flags = ossl_ec_encoding_name2id(gctx->encoding); 1362 1363 if (flags < 0) 1364 goto err; 1365 EC_GROUP_set_asn1_flag(gctx->gen_group, flags); 1366 } 1367 if (gctx->pt_format != NULL) { 1368 int format = ossl_ec_pt_format_name2id(gctx->pt_format); 1369 1370 if (format < 0) 1371 goto err; 1372 EC_GROUP_set_point_conversion_form(gctx->gen_group, format); 1373 } 1374 } 1375 1376 /* We must always assign a group, no matter what */ 1377 ret = ec_gen_assign_group(ec, gctx->gen_group); 1378 1379 /* Whether you want it or not, you get a keypair, not just one half */ 1380 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) 1381 ret = ret && EC_KEY_generate_key(ec); 1382 1383 if (ret) 1384 return ec; 1385 err: 1386 /* Something went wrong, throw the key away */ 1387 EC_KEY_free(ec); 1388 return NULL; 1389 } 1390 # endif 1391 #endif 1392 1393 static void ec_gen_cleanup(void *genctx) 1394 { 1395 struct ec_gen_ctx *gctx = genctx; 1396 1397 if (gctx == NULL) 1398 return; 1399 1400 OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen); 1401 EC_GROUP_free(gctx->gen_group); 1402 BN_free(gctx->p); 1403 BN_free(gctx->a); 1404 BN_free(gctx->b); 1405 BN_free(gctx->order); 1406 BN_free(gctx->cofactor); 1407 OPENSSL_free(gctx->group_name); 1408 OPENSSL_free(gctx->field_type); 1409 OPENSSL_free(gctx->pt_format); 1410 OPENSSL_free(gctx->encoding); 1411 OPENSSL_free(gctx->seed); 1412 OPENSSL_free(gctx->gen); 1413 OPENSSL_free(gctx); 1414 } 1415 1416 static void *common_load(const void *reference, size_t reference_sz, 1417 int sm2_wanted) 1418 { 1419 EC_KEY *ec = NULL; 1420 1421 if (ossl_prov_is_running() && reference_sz == sizeof(ec)) { 1422 /* The contents of the reference is the address to our object */ 1423 ec = *(EC_KEY **)reference; 1424 1425 if (!common_check_sm2(ec, sm2_wanted)) 1426 return NULL; 1427 1428 /* We grabbed, so we detach it */ 1429 *(EC_KEY **)reference = NULL; 1430 return ec; 1431 } 1432 return NULL; 1433 } 1434 1435 static void *ec_load(const void *reference, size_t reference_sz) 1436 { 1437 return common_load(reference, reference_sz, 0); 1438 } 1439 1440 #ifndef FIPS_MODULE 1441 # ifndef OPENSSL_NO_SM2 1442 static void *sm2_load(const void *reference, size_t reference_sz) 1443 { 1444 return common_load(reference, reference_sz, 1); 1445 } 1446 # endif 1447 #endif 1448 1449 static void *ec_dup(const void *keydata_from, int selection) 1450 { 1451 if (ossl_prov_is_running()) 1452 return ossl_ec_key_dup(keydata_from, selection); 1453 return NULL; 1454 } 1455 1456 const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = { 1457 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata }, 1458 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init }, 1459 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, 1460 (void (*)(void))ec_gen_set_template }, 1461 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params }, 1462 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, 1463 (void (*)(void))ec_gen_settable_params }, 1464 { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params }, 1465 { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, 1466 (void (*)(void))ec_gen_gettable_params }, 1467 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen }, 1468 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup }, 1469 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load }, 1470 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata }, 1471 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params }, 1472 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params }, 1473 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params }, 1474 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params }, 1475 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has }, 1476 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match }, 1477 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate }, 1478 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import }, 1479 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types }, 1480 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export }, 1481 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types }, 1482 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, 1483 (void (*)(void))ec_query_operation_name }, 1484 { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup }, 1485 OSSL_DISPATCH_END 1486 }; 1487 1488 #ifndef FIPS_MODULE 1489 # ifndef OPENSSL_NO_SM2 1490 const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = { 1491 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata }, 1492 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init }, 1493 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, 1494 (void (*)(void))ec_gen_set_template }, 1495 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params }, 1496 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, 1497 (void (*)(void))ec_gen_settable_params }, 1498 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen }, 1499 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup }, 1500 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load }, 1501 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata }, 1502 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))sm2_get_params }, 1503 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))sm2_gettable_params }, 1504 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params }, 1505 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))sm2_settable_params }, 1506 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has }, 1507 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match }, 1508 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate }, 1509 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import }, 1510 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types }, 1511 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export }, 1512 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types }, 1513 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, 1514 (void (*)(void))sm2_query_operation_name }, 1515 { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup }, 1516 OSSL_DISPATCH_END 1517 }; 1518 # endif 1519 #endif 1520