2 * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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 "internal/param_build_set.h"
31 # ifndef OPENSSL_NO_SM2
32 # include "crypto/sm2.h"
36 static OSSL_FUNC_keymgmt_new_fn ec_newdata
;
37 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init
;
38 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template
;
39 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params
;
40 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params
;
41 static OSSL_FUNC_keymgmt_gen_fn ec_gen
;
42 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup
;
43 static OSSL_FUNC_keymgmt_load_fn ec_load
;
44 static OSSL_FUNC_keymgmt_free_fn ec_freedata
;
45 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params
;
46 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params
;
47 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params
;
48 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params
;
49 static OSSL_FUNC_keymgmt_has_fn ec_has
;
50 static OSSL_FUNC_keymgmt_match_fn ec_match
;
51 static OSSL_FUNC_keymgmt_validate_fn ec_validate
;
52 static OSSL_FUNC_keymgmt_import_fn ec_import
;
53 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types
;
54 static OSSL_FUNC_keymgmt_export_fn ec_export
;
55 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types
;
56 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name
;
57 static OSSL_FUNC_keymgmt_dup_fn ec_dup
;
59 # ifndef OPENSSL_NO_SM2
60 static OSSL_FUNC_keymgmt_new_fn sm2_newdata
;
61 static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init
;
62 static OSSL_FUNC_keymgmt_gen_fn sm2_gen
;
63 static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params
;
64 static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params
;
65 static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params
;
66 static OSSL_FUNC_keymgmt_import_fn sm2_import
;
67 static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name
;
68 static OSSL_FUNC_keymgmt_validate_fn sm2_validate
;
72 #define EC_DEFAULT_MD "SHA256"
73 #define EC_POSSIBLE_SELECTIONS \
74 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
75 #define SM2_DEFAULT_MD "SM3"
78 const char *ec_query_operation_name(int operation_id
)
80 switch (operation_id
) {
83 case OSSL_OP_SIGNATURE
:
90 # ifndef OPENSSL_NO_SM2
92 const char *sm2_query_operation_name(int operation_id
)
94 switch (operation_id
) {
95 case OSSL_OP_SIGNATURE
:
104 * Callers of key_to_params MUST make sure that domparams_to_params is also
107 * This function only exports the bare keypair, domain parameters and other
108 * parameters are exported separately.
111 int key_to_params(const EC_KEY
*eckey
, OSSL_PARAM_BLD
*tmpl
,
112 OSSL_PARAM params
[], int include_private
,
113 unsigned char **pub_key
)
115 BIGNUM
*x
= NULL
, *y
= NULL
;
116 const BIGNUM
*priv_key
= NULL
;
117 const EC_POINT
*pub_point
= NULL
;
118 const EC_GROUP
*ecg
= NULL
;
119 size_t pub_key_len
= 0;
121 BN_CTX
*bnctx
= NULL
;
124 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
127 priv_key
= EC_KEY_get0_private_key(eckey
);
128 pub_point
= EC_KEY_get0_public_key(eckey
);
130 if (pub_point
!= NULL
) {
131 OSSL_PARAM
*p
= NULL
, *px
= NULL
, *py
= NULL
;
133 * EC_POINT_point2buf() can generate random numbers in some
134 * implementations so we need to ensure we use the correct libctx.
136 bnctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey
));
141 /* If we are doing a get then check first before decoding the point */
143 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_PUB_KEY
);
144 px
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_X
);
145 py
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_Y
);
148 if (p
!= NULL
|| tmpl
!= NULL
) {
149 /* convert pub_point to a octet string according to the SECG standard */
150 point_conversion_form_t format
= EC_KEY_get_conv_form(eckey
);
152 if ((pub_key_len
= EC_POINT_point2buf(ecg
, pub_point
,
154 pub_key
, bnctx
)) == 0
155 || !ossl_param_build_set_octet_string(tmpl
, p
,
156 OSSL_PKEY_PARAM_PUB_KEY
,
157 *pub_key
, pub_key_len
))
160 if (px
!= NULL
|| py
!= NULL
) {
162 x
= BN_CTX_get(bnctx
);
167 y
= BN_CTX_get(bnctx
);
172 if (!EC_POINT_get_affine_coordinates(ecg
, pub_point
, x
, y
, bnctx
))
175 && !ossl_param_build_set_bn(tmpl
, px
,
176 OSSL_PKEY_PARAM_EC_PUB_X
, x
))
179 && !ossl_param_build_set_bn(tmpl
, py
,
180 OSSL_PKEY_PARAM_EC_PUB_Y
, y
))
185 if (priv_key
!= NULL
&& include_private
) {
190 * Key import/export should never leak the bit length of the secret
193 * For this reason, on export we use padded BIGNUMs with fixed length.
195 * When importing we also should make sure that, even if short lived,
196 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
197 * soon as possible, so that any processing of this BIGNUM might opt for
198 * constant time implementations in the backend.
200 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
201 * to preallocate the BIGNUM internal buffer to a fixed public size big
202 * enough that operations performed during the processing never trigger
203 * a realloc which would leak the size of the scalar through memory
209 * The order of the large prime subgroup of the curve is our choice for
210 * a fixed public size, as that is generally the upper bound for
211 * generating a private key in EC cryptosystems and should fit all valid
214 * For padding on export we just use the bit length of the order
215 * converted to bytes (rounding up).
217 * For preallocating the BIGNUM storage we look at the number of "words"
218 * required for the internal representation of the order, and we
219 * preallocate 2 extra "words" in case any of the subsequent processing
220 * might temporarily overflow the order length.
222 ecbits
= EC_GROUP_order_bits(ecg
);
225 sz
= (ecbits
+ 7) / 8;
227 if (!ossl_param_build_set_bn_pad(tmpl
, params
,
228 OSSL_PKEY_PARAM_PRIV_KEY
,
239 int otherparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
242 int ecdh_cofactor_mode
= 0, group_check
= 0;
243 const char *name
= NULL
;
244 point_conversion_form_t format
;
249 format
= EC_KEY_get_conv_form(ec
);
250 name
= ossl_ec_pt_format_id2name((int)format
);
252 && !ossl_param_build_set_utf8_string(tmpl
, params
,
253 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
257 group_check
= EC_KEY_get_flags(ec
) & EC_FLAG_CHECK_NAMED_GROUP_MASK
;
258 name
= ossl_ec_check_group_type_id2name(group_check
);
260 && !ossl_param_build_set_utf8_string(tmpl
, params
,
261 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
,
265 if ((EC_KEY_get_enc_flags(ec
) & EC_PKEY_NO_PUBKEY
) != 0
266 && !ossl_param_build_set_int(tmpl
, params
,
267 OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, 0))
271 (EC_KEY_get_flags(ec
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
272 return ossl_param_build_set_int(tmpl
, params
,
273 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
278 void *ec_newdata(void *provctx
)
280 if (!ossl_prov_is_running())
282 return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx
), NULL
);
286 # ifndef OPENSSL_NO_SM2
288 void *sm2_newdata(void *provctx
)
290 if (!ossl_prov_is_running())
292 return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx
), NULL
, NID_sm2
);
298 void ec_freedata(void *keydata
)
300 EC_KEY_free(keydata
);
304 int ec_has(const void *keydata
, int selection
)
306 const EC_KEY
*ec
= keydata
;
309 if (!ossl_prov_is_running() || ec
== NULL
)
311 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
312 return 1; /* the selection is not missing */
314 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
315 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
316 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
317 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
318 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
319 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
321 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
322 * available, so no extra check is needed other than the previous one
323 * against EC_POSSIBLE_SELECTIONS.
328 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
330 const EC_KEY
*ec1
= keydata1
;
331 const EC_KEY
*ec2
= keydata2
;
332 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
333 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
337 if (!ossl_prov_is_running())
340 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1
));
344 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
345 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
346 && EC_GROUP_cmp(group_a
, group_b
, ctx
) == 0;
347 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
350 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
351 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
352 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
354 if (pa
!= NULL
&& pb
!= NULL
) {
355 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, ctx
) == 0;
360 && (selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
361 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
362 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
364 if (pa
!= NULL
&& pb
!= NULL
) {
365 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
369 ok
= ok
&& key_checked
;
375 static int common_check_sm2(const EC_KEY
*ec
, int sm2_wanted
)
377 const EC_GROUP
*ecg
= NULL
;
380 * sm2_wanted: import the keys or domparams only on SM2 Curve
381 * !sm2_wanted: import the keys or domparams only not on SM2 Curve
383 if ((ecg
= EC_KEY_get0_group(ec
)) == NULL
384 || (sm2_wanted
^ (EC_GROUP_get_curve_name(ecg
) == NID_sm2
)))
390 int common_import(void *keydata
, int selection
, const OSSL_PARAM params
[],
393 EC_KEY
*ec
= keydata
;
396 if (!ossl_prov_is_running() || ec
== NULL
)
400 * In this implementation, we can export/import only keydata in the
401 * following combinations:
402 * - domain parameters (+optional other params)
403 * - public key with associated domain parameters (+optional other params)
404 * - private key with associated domain parameters and optional public key
405 * (+optional other params)
408 * - domain parameters must always be requested
409 * - private key must be requested alongside public key
410 * - other parameters are always optional
412 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
415 ok
= ok
&& ossl_ec_group_fromdata(ec
, params
);
417 if (!common_check_sm2(ec
, sm2_wanted
))
420 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
421 int include_private
=
422 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
424 ok
= ok
&& ossl_ec_key_fromdata(ec
, params
, include_private
);
426 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
427 ok
= ok
&& ossl_ec_key_otherparams_fromdata(ec
, params
);
433 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
435 return common_import(keydata
, selection
, params
, 0);
439 # ifndef OPENSSL_NO_SM2
441 int sm2_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
443 return common_import(keydata
, selection
, params
, 1);
449 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
452 EC_KEY
*ec
= keydata
;
453 OSSL_PARAM_BLD
*tmpl
= NULL
;
454 OSSL_PARAM
*params
= NULL
;
455 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
456 BN_CTX
*bnctx
= NULL
;
459 if (!ossl_prov_is_running() || ec
== NULL
)
463 * In this implementation, we can export/import only keydata in the
464 * following combinations:
465 * - domain parameters (+optional other params)
466 * - public key with associated domain parameters (+optional other params)
467 * - private key with associated public key and domain parameters
468 * (+optional other params)
471 * - domain parameters must always be requested
472 * - private key must be requested alongside public key
473 * - other parameters are always optional
475 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
477 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
478 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
481 tmpl
= OSSL_PARAM_BLD_new();
485 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
486 bnctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec
));
492 ok
= ok
&& ossl_ec_group_todata(EC_KEY_get0_group(ec
), tmpl
, NULL
,
493 ossl_ec_key_get_libctx(ec
),
494 ossl_ec_key_get0_propq(ec
),
498 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
499 int include_private
=
500 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
502 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
504 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
505 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
507 if (!ok
|| (params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
) {
512 ok
= param_cb(params
, cbarg
);
513 OSSL_PARAM_free(params
);
515 OSSL_PARAM_BLD_free(tmpl
);
516 OPENSSL_free(pub_key
);
517 OPENSSL_free(genbuf
);
523 /* IMEXPORT = IMPORT + EXPORT */
525 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
526 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
527 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
528 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
529 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
530 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
531 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
532 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
533 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
534 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
535 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
536 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), \
537 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL)
539 # define EC_IMEXPORTABLE_PUBLIC_KEY \
540 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
541 # define EC_IMEXPORTABLE_PRIVATE_KEY \
542 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
543 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
544 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
545 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
548 * Include all the possible combinations of OSSL_PARAM arrays for
549 * ec_imexport_types().
551 * They are in a separate file as it is ~100 lines of unreadable and
552 * uninteresting machine generated stuff.
554 #include "ec_kmgmt_imexport.inc"
557 const OSSL_PARAM
*ec_imexport_types(int selection
)
561 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
563 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
565 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
567 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
569 return ec_types
[type_select
];
573 const OSSL_PARAM
*ec_import_types(int selection
)
575 return ec_imexport_types(selection
);
579 const OSSL_PARAM
*ec_export_types(int selection
)
581 return ec_imexport_types(selection
);
584 static int ec_get_ecm_params(const EC_GROUP
*group
, OSSL_PARAM params
[])
586 #ifdef OPENSSL_NO_EC2M
590 unsigned int k1
= 0, k2
= 0, k3
= 0;
592 const char *basis_name
= NULL
;
593 int fid
= EC_GROUP_get_field_type(group
);
595 if (fid
!= NID_X9_62_characteristic_two_field
)
598 basis_nid
= EC_GROUP_get_basis_type(group
);
599 if (basis_nid
== NID_X9_62_tpBasis
)
600 basis_name
= SN_X9_62_tpBasis
;
601 else if (basis_nid
== NID_X9_62_ppBasis
)
602 basis_name
= SN_X9_62_ppBasis
;
606 m
= EC_GROUP_get_degree(group
);
607 if (!ossl_param_build_set_int(NULL
, params
, OSSL_PKEY_PARAM_EC_CHAR2_M
, m
)
608 || !ossl_param_build_set_utf8_string(NULL
, params
,
609 OSSL_PKEY_PARAM_EC_CHAR2_TYPE
,
613 if (basis_nid
== NID_X9_62_tpBasis
) {
614 if (!EC_GROUP_get_trinomial_basis(group
, &k1
)
615 || !ossl_param_build_set_int(NULL
, params
,
616 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS
,
620 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
)
621 || !ossl_param_build_set_int(NULL
, params
,
622 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1
, (int)k1
)
623 || !ossl_param_build_set_int(NULL
, params
,
624 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2
, (int)k2
)
625 || !ossl_param_build_set_int(NULL
, params
,
626 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3
, (int)k3
))
632 #endif /* OPENSSL_NO_EC2M */
636 int common_get_params(void *key
, OSSL_PARAM params
[], int sm2
)
640 const EC_GROUP
*ecg
= NULL
;
642 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
643 OSSL_LIB_CTX
*libctx
;
645 BN_CTX
*bnctx
= NULL
;
647 ecg
= EC_KEY_get0_group(eck
);
649 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
653 libctx
= ossl_ec_key_get_libctx(eck
);
654 propq
= ossl_ec_key_get0_propq(eck
);
656 bnctx
= BN_CTX_new_ex(libctx
);
661 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
662 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
664 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
665 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
667 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
668 int ecbits
, sec_bits
;
670 ecbits
= EC_GROUP_order_bits(ecg
);
673 * The following estimates are based on the values published
674 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
675 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
677 * Note that the above reference explicitly categorizes algorithms in a
678 * discrete set of values {80, 112, 128, 192, 256}, and that it is
679 * relevant only for NIST approved Elliptic Curves, while OpenSSL
680 * applies the same logic also to other curves.
682 * Classifications produced by other standardazing bodies might differ,
683 * so the results provided for "bits of security" by this provider are
684 * to be considered merely indicative, and it is the users'
685 * responsibility to compare these values against the normative
686 * references that may be relevant for their intent and purposes.
690 else if (ecbits
>= 384)
692 else if (ecbits
>= 256)
694 else if (ecbits
>= 224)
696 else if (ecbits
>= 160)
699 sec_bits
= ecbits
/ 2;
701 if (!OSSL_PARAM_set_int(p
, sec_bits
))
705 if ((p
= OSSL_PARAM_locate(params
,
706 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
))
708 int explicitparams
= EC_KEY_decoded_from_explicit_params(eck
);
710 if (explicitparams
< 0
711 || !OSSL_PARAM_set_int(p
, explicitparams
))
716 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
717 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
720 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
721 && !OSSL_PARAM_set_utf8_string(p
, SM2_DEFAULT_MD
))
725 /* SM2 doesn't support this PARAM */
727 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
729 int ecdh_cofactor_mode
= 0;
732 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
734 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
738 if ((p
= OSSL_PARAM_locate(params
,
739 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
)) != NULL
) {
740 const EC_POINT
*ecp
= EC_KEY_get0_public_key(key
);
743 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PUBLIC_KEY
);
746 p
->return_size
= EC_POINT_point2oct(ecg
, ecp
,
747 POINT_CONVERSION_UNCOMPRESSED
,
748 p
->data
, p
->return_size
, bnctx
);
749 if (p
->return_size
== 0)
753 ret
= ec_get_ecm_params(ecg
, params
)
754 && ossl_ec_group_todata(ecg
, NULL
, params
, libctx
, propq
, bnctx
,
756 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
757 && otherparams_to_params(eck
, NULL
, params
);
759 OPENSSL_free(genbuf
);
760 OPENSSL_free(pub_key
);
767 int ec_get_params(void *key
, OSSL_PARAM params
[])
769 return common_get_params(key
, params
, 0);
772 #ifndef OPENSSL_NO_EC2M
773 # define EC2M_GETTABLE_DOM_PARAMS \
774 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
775 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
776 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
777 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
778 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
779 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
781 # define EC2M_GETTABLE_DOM_PARAMS
784 static const OSSL_PARAM ec_known_gettable_params
[] = {
785 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
786 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
787 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
788 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
789 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
790 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
791 EC_IMEXPORTABLE_DOM_PARAMETERS
,
792 EC2M_GETTABLE_DOM_PARAMS
793 EC_IMEXPORTABLE_PUBLIC_KEY
,
794 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
795 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
796 EC_IMEXPORTABLE_PRIVATE_KEY
,
797 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
802 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
804 return ec_known_gettable_params
;
807 static const OSSL_PARAM ec_known_settable_params
[] = {
808 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
809 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
810 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
811 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
812 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
813 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, NULL
),
814 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, NULL
, 0),
819 const OSSL_PARAM
*ec_settable_params(void *provctx
)
821 return ec_known_settable_params
;
825 int ec_set_params(void *key
, const OSSL_PARAM params
[])
836 if (!ossl_ec_group_set_params((EC_GROUP
*)EC_KEY_get0_group(key
), params
))
839 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
);
841 BN_CTX
*ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(key
));
845 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
846 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
853 return ossl_ec_key_otherparams_fromdata(eck
, params
);
857 # ifndef OPENSSL_NO_SM2
859 int sm2_get_params(void *key
, OSSL_PARAM params
[])
861 return common_get_params(key
, params
, 1);
864 static const OSSL_PARAM sm2_known_gettable_params
[] = {
865 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
866 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
867 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
868 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
869 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
870 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
871 EC_IMEXPORTABLE_DOM_PARAMETERS
,
872 EC_IMEXPORTABLE_PUBLIC_KEY
,
873 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
874 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
875 EC_IMEXPORTABLE_PRIVATE_KEY
,
880 const OSSL_PARAM
*sm2_gettable_params(ossl_unused
void *provctx
)
882 return sm2_known_gettable_params
;
885 static const OSSL_PARAM sm2_known_settable_params
[] = {
886 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
891 const OSSL_PARAM
*sm2_settable_params(ossl_unused
void *provctx
)
893 return sm2_known_settable_params
;
897 int sm2_validate(const void *keydata
, int selection
, int checktype
)
899 const EC_KEY
*eck
= keydata
;
903 if (!ossl_prov_is_running())
906 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
907 return 1; /* nothing to validate */
909 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
913 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
914 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
916 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
917 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
918 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
920 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
923 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
924 ok
= ok
&& ossl_sm2_key_private_check(eck
);
926 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
927 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
936 int ec_validate(const void *keydata
, int selection
, int checktype
)
938 const EC_KEY
*eck
= keydata
;
942 if (!ossl_prov_is_running())
945 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
946 return 1; /* nothing to validate */
948 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
952 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
953 int flags
= EC_KEY_get_flags(eck
);
955 if ((flags
& EC_FLAG_CHECK_NAMED_GROUP
) != 0)
956 ok
= ok
&& EC_GROUP_check_named_curve(EC_KEY_get0_group(eck
),
957 (flags
& EC_FLAG_CHECK_NAMED_GROUP_NIST
) != 0, ctx
) > 0;
959 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
962 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
963 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
964 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
966 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
969 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
970 ok
= ok
&& ossl_ec_key_private_check(eck
);
972 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
973 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
980 OSSL_LIB_CTX
*libctx
;
986 BIGNUM
*p
, *a
, *b
, *order
, *cofactor
;
987 unsigned char *gen
, *seed
;
988 size_t gen_len
, seed_len
;
992 unsigned char *dhkem_ikm
;
996 static void *ec_gen_init(void *provctx
, int selection
,
997 const OSSL_PARAM params
[])
999 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(provctx
);
1000 struct ec_gen_ctx
*gctx
= NULL
;
1002 if (!ossl_prov_is_running() || (selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
1005 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
1006 gctx
->libctx
= libctx
;
1007 gctx
->selection
= selection
;
1008 gctx
->ecdh_mode
= 0;
1009 if (!ec_gen_set_params(gctx
, params
)) {
1018 # ifndef OPENSSL_NO_SM2
1019 static void *sm2_gen_init(void *provctx
, int selection
,
1020 const OSSL_PARAM params
[])
1022 struct ec_gen_ctx
*gctx
= ec_gen_init(provctx
, selection
, params
);
1025 if (gctx
->group_name
!= NULL
)
1027 if ((gctx
->group_name
= OPENSSL_strdup("sm2")) != NULL
)
1029 ec_gen_cleanup(gctx
);
1036 static int ec_gen_set_group(void *genctx
, const EC_GROUP
*src
)
1038 struct ec_gen_ctx
*gctx
= genctx
;
1041 group
= EC_GROUP_dup(src
);
1042 if (group
== NULL
) {
1043 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
1046 EC_GROUP_free(gctx
->gen_group
);
1047 gctx
->gen_group
= group
;
1051 static int ec_gen_set_template(void *genctx
, void *templ
)
1053 struct ec_gen_ctx
*gctx
= genctx
;
1055 const EC_GROUP
*ec_group
;
1057 if (!ossl_prov_is_running() || gctx
== NULL
|| ec
== NULL
)
1059 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
1061 return ec_gen_set_group(gctx
, ec_group
);
1064 #define COPY_INT_PARAM(params, key, val) \
1065 p = OSSL_PARAM_locate_const(params, key); \
1066 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
1069 #define COPY_UTF8_PARAM(params, key, val) \
1070 p = OSSL_PARAM_locate_const(params, key); \
1072 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
1074 OPENSSL_free(val); \
1075 val = OPENSSL_strdup(p->data); \
1080 #define COPY_OCTET_PARAM(params, key, val, len) \
1081 p = OSSL_PARAM_locate_const(params, key); \
1083 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
1085 OPENSSL_free(val); \
1086 len = p->data_size; \
1087 val = OPENSSL_memdup(p->data, p->data_size); \
1092 #define COPY_BN_PARAM(params, key, bn) \
1093 p = OSSL_PARAM_locate_const(params, key); \
1097 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1101 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
1104 struct ec_gen_ctx
*gctx
= genctx
;
1105 const OSSL_PARAM
*p
;
1106 EC_GROUP
*group
= NULL
;
1108 COPY_INT_PARAM(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, gctx
->ecdh_mode
);
1110 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_GROUP_NAME
, gctx
->group_name
);
1111 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
, gctx
->field_type
);
1112 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_ENCODING
, gctx
->encoding
);
1113 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, gctx
->pt_format
);
1114 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, gctx
->group_check
);
1116 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
);
1117 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
);
1118 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
);
1119 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
);
1120 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_COFACTOR
, gctx
->cofactor
);
1122 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_SEED
, gctx
->seed
, gctx
->seed_len
);
1123 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_GENERATOR
, gctx
->gen
,
1126 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_DHKEM_IKM
, gctx
->dhkem_ikm
,
1127 gctx
->dhkem_ikmlen
);
1131 EC_GROUP_free(group
);
1135 static int ec_gen_set_group_from_params(struct ec_gen_ctx
*gctx
)
1138 OSSL_PARAM_BLD
*bld
;
1139 OSSL_PARAM
*params
= NULL
;
1140 EC_GROUP
*group
= NULL
;
1142 bld
= OSSL_PARAM_BLD_new();
1146 if (gctx
->encoding
!= NULL
1147 && !OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_ENCODING
,
1151 if (gctx
->pt_format
!= NULL
1152 && !OSSL_PARAM_BLD_push_utf8_string(bld
,
1153 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
1154 gctx
->pt_format
, 0))
1157 if (gctx
->group_name
!= NULL
) {
1158 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_GROUP_NAME
,
1159 gctx
->group_name
, 0))
1161 /* Ignore any other parameters if there is a group name */
1163 } else if (gctx
->field_type
!= NULL
) {
1164 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
1165 gctx
->field_type
, 0))
1173 || gctx
->order
== NULL
1174 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
)
1175 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
)
1176 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
)
1177 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
))
1180 if (gctx
->cofactor
!= NULL
1181 && !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1185 if (gctx
->seed
!= NULL
1186 && !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_SEED
,
1187 gctx
->seed
, gctx
->seed_len
))
1190 if (gctx
->gen
== NULL
1191 || !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_GENERATOR
,
1192 gctx
->gen
, gctx
->gen_len
))
1195 params
= OSSL_PARAM_BLD_to_param(bld
);
1198 group
= EC_GROUP_new_from_params(params
, gctx
->libctx
, NULL
);
1202 EC_GROUP_free(gctx
->gen_group
);
1203 gctx
->gen_group
= group
;
1207 OSSL_PARAM_free(params
);
1208 OSSL_PARAM_BLD_free(bld
);
1212 static const OSSL_PARAM
*ec_gen_settable_params(ossl_unused
void *genctx
,
1213 ossl_unused
void *provctx
)
1215 static OSSL_PARAM settable
[] = {
1216 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
1217 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
1218 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
1219 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
1220 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE
, NULL
, 0),
1221 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P
, NULL
, 0),
1222 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A
, NULL
, 0),
1223 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B
, NULL
, 0),
1224 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR
, NULL
, 0),
1225 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER
, NULL
, 0),
1226 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR
, NULL
, 0),
1227 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
1228 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM
, NULL
, 0),
1235 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
1237 if (group
== NULL
) {
1238 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
1241 return EC_KEY_set_group(ec
, group
) > 0;
1245 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1247 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1249 struct ec_gen_ctx
*gctx
= genctx
;
1253 if (!ossl_prov_is_running()
1255 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1258 if (gctx
->gen_group
== NULL
) {
1259 if (!ec_gen_set_group_from_params(gctx
))
1262 if (gctx
->encoding
!= NULL
) {
1263 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1267 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1269 if (gctx
->pt_format
!= NULL
) {
1270 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1274 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1278 /* We must always assign a group, no matter what */
1279 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1281 /* Whether you want it or not, you get a keypair, not just one half */
1282 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
1284 if (gctx
->dhkem_ikm
!= NULL
&& gctx
->dhkem_ikmlen
!= 0)
1285 ret
= ret
&& ossl_ec_generate_key_dhkem(ec
, gctx
->dhkem_ikm
,
1286 gctx
->dhkem_ikmlen
);
1289 ret
= ret
&& EC_KEY_generate_key(ec
);
1292 if (gctx
->ecdh_mode
!= -1)
1293 ret
= ret
&& ossl_ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
1295 if (gctx
->group_check
!= NULL
)
1296 ret
= ret
&& ossl_ec_set_check_group_type_from_name(ec
,
1301 /* Something went wrong, throw the key away */
1307 # ifndef OPENSSL_NO_SM2
1309 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1311 static void *sm2_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1313 struct ec_gen_ctx
*gctx
= genctx
;
1318 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1321 if (gctx
->gen_group
== NULL
) {
1322 if (!ec_gen_set_group_from_params(gctx
))
1325 if (gctx
->encoding
) {
1326 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1330 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1332 if (gctx
->pt_format
!= NULL
) {
1333 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1337 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1341 /* We must always assign a group, no matter what */
1342 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1344 /* Whether you want it or not, you get a keypair, not just one half */
1345 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
1346 ret
= ret
&& EC_KEY_generate_key(ec
);
1351 /* Something went wrong, throw the key away */
1358 static void ec_gen_cleanup(void *genctx
)
1360 struct ec_gen_ctx
*gctx
= genctx
;
1365 OPENSSL_clear_free(gctx
->dhkem_ikm
, gctx
->dhkem_ikmlen
);
1366 EC_GROUP_free(gctx
->gen_group
);
1370 BN_free(gctx
->order
);
1371 BN_free(gctx
->cofactor
);
1372 OPENSSL_free(gctx
->group_name
);
1373 OPENSSL_free(gctx
->field_type
);
1374 OPENSSL_free(gctx
->pt_format
);
1375 OPENSSL_free(gctx
->encoding
);
1376 OPENSSL_free(gctx
->seed
);
1377 OPENSSL_free(gctx
->gen
);
1381 static void *common_load(const void *reference
, size_t reference_sz
,
1386 if (ossl_prov_is_running() && reference_sz
== sizeof(ec
)) {
1387 /* The contents of the reference is the address to our object */
1388 ec
= *(EC_KEY
**)reference
;
1390 if (!common_check_sm2(ec
, sm2_wanted
))
1393 /* We grabbed, so we detach it */
1394 *(EC_KEY
**)reference
= NULL
;
1400 static void *ec_load(const void *reference
, size_t reference_sz
)
1402 return common_load(reference
, reference_sz
, 0);
1406 # ifndef OPENSSL_NO_SM2
1407 static void *sm2_load(const void *reference
, size_t reference_sz
)
1409 return common_load(reference
, reference_sz
, 1);
1414 static void *ec_dup(const void *keydata_from
, int selection
)
1416 if (ossl_prov_is_running())
1417 return ossl_ec_key_dup(keydata_from
, selection
);
1421 const OSSL_DISPATCH ossl_ec_keymgmt_functions
[] = {
1422 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
1423 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
1424 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1425 (void (*)(void))ec_gen_set_template
},
1426 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1427 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1428 (void (*)(void))ec_gen_settable_params
},
1429 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
1430 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1431 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
1432 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1433 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
1434 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
1435 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1436 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
1437 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1438 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1439 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
1440 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
1441 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1442 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1443 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1444 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1445 (void (*)(void))ec_query_operation_name
},
1446 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},
1451 # ifndef OPENSSL_NO_SM2
1452 const OSSL_DISPATCH ossl_sm2_keymgmt_functions
[] = {
1453 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))sm2_newdata
},
1454 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))sm2_gen_init
},
1455 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1456 (void (*)(void))ec_gen_set_template
},
1457 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1458 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1459 (void (*)(void))ec_gen_settable_params
},
1460 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))sm2_gen
},
1461 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1462 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))sm2_load
},
1463 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1464 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))sm2_get_params
},
1465 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))sm2_gettable_params
},
1466 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1467 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))sm2_settable_params
},
1468 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1469 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1470 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))sm2_validate
},
1471 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))sm2_import
},
1472 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1473 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1474 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1475 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1476 (void (*)(void))sm2_query_operation_name
},
1477 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},