2 * Copyright 2020-2021 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"
16 #include "e_os.h" /* strcasecmp */
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/bn.h>
21 #include <openssl/err.h>
22 #include <openssl/objects.h>
23 #include <openssl/proverr.h>
24 #include "crypto/bn.h"
25 #include "crypto/ec.h"
26 #include "prov/implementations.h"
27 #include "prov/providercommon.h"
28 #include "prov/provider_ctx.h"
29 #include "internal/param_build_set.h"
32 # ifndef OPENSSL_NO_SM2
33 # include "crypto/sm2.h"
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_fn ec_gen
;
43 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup
;
44 static OSSL_FUNC_keymgmt_load_fn ec_load
;
45 static OSSL_FUNC_keymgmt_free_fn ec_freedata
;
46 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params
;
47 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params
;
48 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params
;
49 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params
;
50 static OSSL_FUNC_keymgmt_has_fn ec_has
;
51 static OSSL_FUNC_keymgmt_match_fn ec_match
;
52 static OSSL_FUNC_keymgmt_validate_fn ec_validate
;
53 static OSSL_FUNC_keymgmt_import_fn ec_import
;
54 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types
;
55 static OSSL_FUNC_keymgmt_export_fn ec_export
;
56 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types
;
57 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name
;
58 static OSSL_FUNC_keymgmt_dup_fn ec_dup
;
60 # ifndef OPENSSL_NO_SM2
61 static OSSL_FUNC_keymgmt_new_fn sm2_newdata
;
62 static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init
;
63 static OSSL_FUNC_keymgmt_gen_fn sm2_gen
;
64 static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params
;
65 static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params
;
66 static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params
;
67 static OSSL_FUNC_keymgmt_import_fn sm2_import
;
68 static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name
;
69 static OSSL_FUNC_keymgmt_validate_fn sm2_validate
;
73 #define EC_DEFAULT_MD "SHA256"
74 #define EC_POSSIBLE_SELECTIONS \
75 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
76 #define SM2_DEFAULT_MD "SM3"
79 const char *ec_query_operation_name(int operation_id
)
81 switch (operation_id
) {
84 case OSSL_OP_SIGNATURE
:
91 # ifndef OPENSSL_NO_SM2
93 const char *sm2_query_operation_name(int operation_id
)
95 switch (operation_id
) {
96 case OSSL_OP_SIGNATURE
:
105 * Callers of key_to_params MUST make sure that domparams_to_params is also
108 * This function only exports the bare keypair, domain parameters and other
109 * parameters are exported separately.
112 int key_to_params(const EC_KEY
*eckey
, OSSL_PARAM_BLD
*tmpl
,
113 OSSL_PARAM params
[], int include_private
,
114 unsigned char **pub_key
)
116 BIGNUM
*x
= NULL
, *y
= NULL
;
117 const BIGNUM
*priv_key
= NULL
;
118 const EC_POINT
*pub_point
= NULL
;
119 const EC_GROUP
*ecg
= NULL
;
120 size_t pub_key_len
= 0;
122 BN_CTX
*bnctx
= NULL
;
125 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
128 priv_key
= EC_KEY_get0_private_key(eckey
);
129 pub_point
= EC_KEY_get0_public_key(eckey
);
131 if (pub_point
!= NULL
) {
132 OSSL_PARAM
*p
= NULL
, *px
= NULL
, *py
= NULL
;
134 * EC_POINT_point2buf() can generate random numbers in some
135 * implementations so we need to ensure we use the correct libctx.
137 bnctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey
));
142 /* If we are doing a get then check first before decoding the point */
144 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_PUB_KEY
);
145 px
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_X
);
146 py
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_Y
);
149 if (p
!= NULL
|| tmpl
!= NULL
) {
150 /* convert pub_point to a octet string according to the SECG standard */
151 if ((pub_key_len
= EC_POINT_point2buf(ecg
, pub_point
,
152 POINT_CONVERSION_COMPRESSED
,
153 pub_key
, bnctx
)) == 0
154 || !ossl_param_build_set_octet_string(tmpl
, p
,
155 OSSL_PKEY_PARAM_PUB_KEY
,
156 *pub_key
, pub_key_len
))
159 if (px
!= NULL
|| py
!= NULL
) {
161 x
= BN_CTX_get(bnctx
);
163 y
= BN_CTX_get(bnctx
);
165 if (!EC_POINT_get_affine_coordinates(ecg
, pub_point
, x
, y
, bnctx
))
168 && !ossl_param_build_set_bn(tmpl
, px
,
169 OSSL_PKEY_PARAM_EC_PUB_X
, x
))
172 && !ossl_param_build_set_bn(tmpl
, py
,
173 OSSL_PKEY_PARAM_EC_PUB_Y
, y
))
178 if (priv_key
!= NULL
&& include_private
) {
183 * Key import/export should never leak the bit length of the secret
186 * For this reason, on export we use padded BIGNUMs with fixed length.
188 * When importing we also should make sure that, even if short lived,
189 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
190 * soon as possible, so that any processing of this BIGNUM might opt for
191 * constant time implementations in the backend.
193 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
194 * to preallocate the BIGNUM internal buffer to a fixed public size big
195 * enough that operations performed during the processing never trigger
196 * a realloc which would leak the size of the scalar through memory
202 * The order of the large prime subgroup of the curve is our choice for
203 * a fixed public size, as that is generally the upper bound for
204 * generating a private key in EC cryptosystems and should fit all valid
207 * For padding on export we just use the bit length of the order
208 * converted to bytes (rounding up).
210 * For preallocating the BIGNUM storage we look at the number of "words"
211 * required for the internal representation of the order, and we
212 * preallocate 2 extra "words" in case any of the subsequent processing
213 * might temporarily overflow the order length.
215 ecbits
= EC_GROUP_order_bits(ecg
);
218 sz
= (ecbits
+ 7) / 8;
220 if (!ossl_param_build_set_bn_pad(tmpl
, params
,
221 OSSL_PKEY_PARAM_PRIV_KEY
,
232 int otherparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
235 int ecdh_cofactor_mode
= 0, group_check
= 0;
236 const char *name
= NULL
;
237 point_conversion_form_t format
;
242 format
= EC_KEY_get_conv_form(ec
);
243 name
= ossl_ec_pt_format_id2name((int)format
);
245 && !ossl_param_build_set_utf8_string(tmpl
, params
,
246 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
250 group_check
= EC_KEY_get_flags(ec
) & EC_FLAG_CHECK_NAMED_GROUP_MASK
;
251 name
= ossl_ec_check_group_type_id2name(group_check
);
253 && !ossl_param_build_set_utf8_string(tmpl
, params
,
254 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
,
258 if ((EC_KEY_get_enc_flags(ec
) & EC_PKEY_NO_PUBKEY
) != 0
259 && !ossl_param_build_set_int(tmpl
, params
,
260 OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, 0))
264 (EC_KEY_get_flags(ec
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
265 return ossl_param_build_set_int(tmpl
, params
,
266 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
271 void *ec_newdata(void *provctx
)
273 if (!ossl_prov_is_running())
275 return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx
), NULL
);
279 # ifndef OPENSSL_NO_SM2
281 void *sm2_newdata(void *provctx
)
283 if (!ossl_prov_is_running())
285 return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx
), NULL
, NID_sm2
);
291 void ec_freedata(void *keydata
)
293 EC_KEY_free(keydata
);
297 int ec_has(const void *keydata
, int selection
)
299 const EC_KEY
*ec
= keydata
;
302 if (!ossl_prov_is_running() || ec
== NULL
)
304 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
305 return 1; /* the selection is not missing */
307 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
308 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
309 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
310 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
311 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
312 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
314 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
315 * available, so no extra check is needed other than the previous one
316 * against EC_POSSIBLE_SELECTIONS.
321 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
323 const EC_KEY
*ec1
= keydata1
;
324 const EC_KEY
*ec2
= keydata2
;
325 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
326 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
330 if (!ossl_prov_is_running())
333 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1
));
337 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
338 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
339 && EC_GROUP_cmp(group_a
, group_b
, ctx
) == 0;
340 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
341 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
342 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
344 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
346 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
347 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
348 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
350 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, ctx
) == 0;
356 static int common_check_sm2(const EC_KEY
*ec
, int sm2_wanted
)
358 const EC_GROUP
*ecg
= NULL
;
361 * sm2_wanted: import the keys or domparams only on SM2 Curve
362 * !sm2_wanted: import the keys or domparams only not on SM2 Curve
364 if ((ecg
= EC_KEY_get0_group(ec
)) == NULL
365 || (sm2_wanted
^ (EC_GROUP_get_curve_name(ecg
) == NID_sm2
)))
371 int common_import(void *keydata
, int selection
, const OSSL_PARAM params
[],
374 EC_KEY
*ec
= keydata
;
377 if (!ossl_prov_is_running() || ec
== NULL
)
381 * In this implementation, we can export/import only keydata in the
382 * following combinations:
383 * - domain parameters (+optional other params)
384 * - public key with associated domain parameters (+optional other params)
385 * - private key with associated domain parameters and optional public key
386 * (+optional other params)
389 * - domain parameters must always be requested
390 * - private key must be requested alongside public key
391 * - other parameters are always optional
393 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
396 ok
= ok
&& ossl_ec_group_fromdata(ec
, params
);
398 if (!common_check_sm2(ec
, sm2_wanted
))
401 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
402 int include_private
=
403 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
405 ok
= ok
&& ossl_ec_key_fromdata(ec
, params
, include_private
);
407 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
408 ok
= ok
&& ossl_ec_key_otherparams_fromdata(ec
, params
);
414 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
416 return common_import(keydata
, selection
, params
, 0);
420 # ifndef OPENSSL_NO_SM2
422 int sm2_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
424 return common_import(keydata
, selection
, params
, 1);
430 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
433 EC_KEY
*ec
= keydata
;
434 OSSL_PARAM_BLD
*tmpl
= NULL
;
435 OSSL_PARAM
*params
= NULL
;
436 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
437 BN_CTX
*bnctx
= NULL
;
440 if (!ossl_prov_is_running() || ec
== NULL
)
444 * In this implementation, we can export/import only keydata in the
445 * following combinations:
446 * - domain parameters (+optional other params)
447 * - public key with associated domain parameters (+optional other params)
448 * - private key with associated public key and domain parameters
449 * (+optional other params)
452 * - domain parameters must always be requested
453 * - private key must be requested alongside public key
454 * - other parameters are always optional
456 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
458 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
459 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
461 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0
462 && (selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
465 tmpl
= OSSL_PARAM_BLD_new();
469 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
470 bnctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec
));
476 ok
= ok
&& ossl_ec_group_todata(EC_KEY_get0_group(ec
), tmpl
, NULL
,
477 ossl_ec_key_get_libctx(ec
),
478 ossl_ec_key_get0_propq(ec
),
482 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
483 int include_private
=
484 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
486 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
488 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
489 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
491 if (ok
&& (params
= OSSL_PARAM_BLD_to_param(tmpl
)) != NULL
)
492 ok
= param_cb(params
, cbarg
);
494 OSSL_PARAM_free(params
);
495 OSSL_PARAM_BLD_free(tmpl
);
496 OPENSSL_free(pub_key
);
497 OPENSSL_free(genbuf
);
503 /* IMEXPORT = IMPORT + EXPORT */
505 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
506 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
507 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
508 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
509 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
510 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
511 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
512 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
513 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
514 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
515 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
516 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
518 # define EC_IMEXPORTABLE_PUBLIC_KEY \
519 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
520 # define EC_IMEXPORTABLE_PRIVATE_KEY \
521 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
522 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
523 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
524 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
527 * Include all the possible combinations of OSSL_PARAM arrays for
528 * ec_imexport_types().
530 * They are in a separate file as it is ~100 lines of unreadable and
531 * uninteresting machine generated stuff.
533 #include "ec_kmgmt_imexport.inc"
536 const OSSL_PARAM
*ec_imexport_types(int selection
)
540 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
542 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
544 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
546 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
548 return ec_types
[type_select
];
552 const OSSL_PARAM
*ec_import_types(int selection
)
554 return ec_imexport_types(selection
);
558 const OSSL_PARAM
*ec_export_types(int selection
)
560 return ec_imexport_types(selection
);
563 static int ec_get_ecm_params(const EC_GROUP
*group
, OSSL_PARAM params
[])
565 #ifdef OPENSSL_NO_EC2M
569 unsigned int k1
= 0, k2
= 0, k3
= 0;
571 const char *basis_name
= NULL
;
572 int fid
= EC_GROUP_get_field_type(group
);
574 if (fid
!= NID_X9_62_characteristic_two_field
)
577 basis_nid
= EC_GROUP_get_basis_type(group
);
578 if (basis_nid
== NID_X9_62_tpBasis
)
579 basis_name
= SN_X9_62_tpBasis
;
580 else if (basis_nid
== NID_X9_62_ppBasis
)
581 basis_name
= SN_X9_62_ppBasis
;
585 m
= EC_GROUP_get_degree(group
);
586 if (!ossl_param_build_set_int(NULL
, params
, OSSL_PKEY_PARAM_EC_CHAR2_M
, m
)
587 || !ossl_param_build_set_utf8_string(NULL
, params
,
588 OSSL_PKEY_PARAM_EC_CHAR2_TYPE
,
592 if (basis_nid
== NID_X9_62_tpBasis
) {
593 if (!EC_GROUP_get_trinomial_basis(group
, &k1
)
594 || !ossl_param_build_set_int(NULL
, params
,
595 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS
,
599 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
)
600 || !ossl_param_build_set_int(NULL
, params
,
601 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1
, (int)k1
)
602 || !ossl_param_build_set_int(NULL
, params
,
603 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2
, (int)k2
)
604 || !ossl_param_build_set_int(NULL
, params
,
605 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3
, (int)k3
))
611 #endif /* OPENSSL_NO_EC2M */
615 int common_get_params(void *key
, OSSL_PARAM params
[], int sm2
)
619 const EC_GROUP
*ecg
= NULL
;
621 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
622 OSSL_LIB_CTX
*libctx
;
624 BN_CTX
*bnctx
= NULL
;
626 ecg
= EC_KEY_get0_group(eck
);
630 libctx
= ossl_ec_key_get_libctx(eck
);
631 propq
= ossl_ec_key_get0_propq(eck
);
633 bnctx
= BN_CTX_new_ex(libctx
);
638 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
639 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
641 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
642 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
644 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
645 int ecbits
, sec_bits
;
647 ecbits
= EC_GROUP_order_bits(ecg
);
650 * The following estimates are based on the values published
651 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
652 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
654 * Note that the above reference explicitly categorizes algorithms in a
655 * discrete set of values {80, 112, 128, 192, 256}, and that it is
656 * relevant only for NIST approved Elliptic Curves, while OpenSSL
657 * applies the same logic also to other curves.
659 * Classifications produced by other standardazing bodies might differ,
660 * so the results provided for "bits of security" by this provider are
661 * to be considered merely indicative, and it is the users'
662 * responsibility to compare these values against the normative
663 * references that may be relevant for their intent and purposes.
667 else if (ecbits
>= 384)
669 else if (ecbits
>= 256)
671 else if (ecbits
>= 224)
673 else if (ecbits
>= 160)
676 sec_bits
= ecbits
/ 2;
678 if (!OSSL_PARAM_set_int(p
, sec_bits
))
682 if ((p
= OSSL_PARAM_locate(params
,
683 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
))
685 int explicitparams
= EC_KEY_decoded_from_explicit_params(eck
);
687 if (explicitparams
< 0
688 || !OSSL_PARAM_set_int(p
, explicitparams
))
693 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
694 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
697 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
698 && !OSSL_PARAM_set_utf8_string(p
, SM2_DEFAULT_MD
))
702 /* SM2 doesn't support this PARAM */
704 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
706 int ecdh_cofactor_mode
= 0;
709 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
711 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
715 if ((p
= OSSL_PARAM_locate(params
,
716 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
)) != NULL
) {
717 p
->return_size
= EC_POINT_point2oct(EC_KEY_get0_group(key
),
718 EC_KEY_get0_public_key(key
),
719 POINT_CONVERSION_UNCOMPRESSED
,
720 p
->data
, p
->return_size
, bnctx
);
721 if (p
->return_size
== 0)
725 ret
= ec_get_ecm_params(ecg
, params
)
726 && ossl_ec_group_todata(ecg
, NULL
, params
, libctx
, propq
, bnctx
,
728 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
729 && otherparams_to_params(eck
, NULL
, params
);
731 OPENSSL_free(genbuf
);
732 OPENSSL_free(pub_key
);
739 int ec_get_params(void *key
, OSSL_PARAM params
[])
741 return common_get_params(key
, params
, 0);
744 #ifndef OPENSSL_NO_EC2M
745 # define EC2M_GETTABLE_DOM_PARAMS \
746 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
747 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
748 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
749 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
750 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
751 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
753 # define EC2M_GETTABLE_DOM_PARAMS
756 static const OSSL_PARAM ec_known_gettable_params
[] = {
757 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
758 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
759 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
760 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
761 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
762 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
763 EC_IMEXPORTABLE_DOM_PARAMETERS
,
764 EC2M_GETTABLE_DOM_PARAMS
765 EC_IMEXPORTABLE_PUBLIC_KEY
,
766 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
767 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
768 EC_IMEXPORTABLE_PRIVATE_KEY
,
769 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
774 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
776 return ec_known_gettable_params
;
779 static const OSSL_PARAM ec_known_settable_params
[] = {
780 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
781 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
782 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
783 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
784 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
785 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, NULL
),
786 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, NULL
, 0),
791 const OSSL_PARAM
*ec_settable_params(void *provctx
)
793 return ec_known_settable_params
;
797 int ec_set_params(void *key
, const OSSL_PARAM params
[])
808 if (!ossl_ec_group_set_params((EC_GROUP
*)EC_KEY_get0_group(key
), params
))
811 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
);
813 BN_CTX
*ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(key
));
817 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
818 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
825 return ossl_ec_key_otherparams_fromdata(eck
, params
);
829 # ifndef OPENSSL_NO_SM2
831 int sm2_get_params(void *key
, OSSL_PARAM params
[])
833 return common_get_params(key
, params
, 1);
836 static const OSSL_PARAM sm2_known_gettable_params
[] = {
837 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
838 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
839 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
840 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
841 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
842 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
843 EC_IMEXPORTABLE_DOM_PARAMETERS
,
844 EC_IMEXPORTABLE_PUBLIC_KEY
,
845 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
846 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
847 EC_IMEXPORTABLE_PRIVATE_KEY
,
852 const OSSL_PARAM
*sm2_gettable_params(ossl_unused
void *provctx
)
854 return sm2_known_gettable_params
;
857 static const OSSL_PARAM sm2_known_settable_params
[] = {
858 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
863 const OSSL_PARAM
*sm2_settable_params(ossl_unused
void *provctx
)
865 return sm2_known_settable_params
;
869 int sm2_validate(const void *keydata
, int selection
, int checktype
)
871 const EC_KEY
*eck
= keydata
;
875 if (!ossl_prov_is_running())
878 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
879 return 1; /* nothing to validate */
881 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
885 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
886 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
888 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
889 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
890 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
892 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
895 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
896 ok
= ok
&& ossl_sm2_key_private_check(eck
);
898 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
899 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
908 int ec_validate(const void *keydata
, int selection
, int checktype
)
910 const EC_KEY
*eck
= keydata
;
914 if (!ossl_prov_is_running())
917 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
918 return 1; /* nothing to validate */
920 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
924 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
925 int flags
= EC_KEY_get_flags(eck
);
927 if ((flags
& EC_FLAG_CHECK_NAMED_GROUP
) != 0)
928 ok
= ok
&& EC_GROUP_check_named_curve(EC_KEY_get0_group(eck
),
929 (flags
& EC_FLAG_CHECK_NAMED_GROUP_NIST
) != 0, ctx
);
931 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
934 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
935 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
936 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
938 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
941 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
942 ok
= ok
&& ossl_ec_key_private_check(eck
);
944 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
945 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
952 OSSL_LIB_CTX
*libctx
;
958 BIGNUM
*p
, *a
, *b
, *order
, *cofactor
;
959 unsigned char *gen
, *seed
;
960 size_t gen_len
, seed_len
;
966 static void *ec_gen_init(void *provctx
, int selection
,
967 const OSSL_PARAM params
[])
969 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(provctx
);
970 struct ec_gen_ctx
*gctx
= NULL
;
972 if (!ossl_prov_is_running() || (selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
975 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
976 gctx
->libctx
= libctx
;
977 gctx
->selection
= selection
;
980 if (!ec_gen_set_params(gctx
, params
)) {
988 # ifndef OPENSSL_NO_SM2
989 static void *sm2_gen_init(void *provctx
, int selection
,
990 const OSSL_PARAM params
[])
992 struct ec_gen_ctx
*gctx
= ec_gen_init(provctx
, selection
, params
);
995 if (gctx
->group_name
!= NULL
)
997 if ((gctx
->group_name
= OPENSSL_strdup("sm2")) != NULL
)
999 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
1000 ec_gen_cleanup(gctx
);
1007 static int ec_gen_set_group(void *genctx
, const EC_GROUP
*src
)
1009 struct ec_gen_ctx
*gctx
= genctx
;
1012 group
= EC_GROUP_dup(src
);
1013 if (group
== NULL
) {
1014 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
1017 EC_GROUP_free(gctx
->gen_group
);
1018 gctx
->gen_group
= group
;
1022 static int ec_gen_set_template(void *genctx
, void *templ
)
1024 struct ec_gen_ctx
*gctx
= genctx
;
1026 const EC_GROUP
*ec_group
;
1028 if (!ossl_prov_is_running() || gctx
== NULL
|| ec
== NULL
)
1030 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
1032 return ec_gen_set_group(gctx
, ec_group
);
1035 #define COPY_INT_PARAM(params, key, val) \
1036 p = OSSL_PARAM_locate_const(params, key); \
1037 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
1040 #define COPY_UTF8_PARAM(params, key, val) \
1041 p = OSSL_PARAM_locate_const(params, key); \
1043 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
1045 OPENSSL_free(val); \
1046 val = OPENSSL_strdup(p->data); \
1051 #define COPY_OCTET_PARAM(params, key, val, len) \
1052 p = OSSL_PARAM_locate_const(params, key); \
1054 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
1056 OPENSSL_free(val); \
1057 len = p->data_size; \
1058 val = OPENSSL_memdup(p->data, p->data_size); \
1063 #define COPY_BN_PARAM(params, key, bn) \
1064 p = OSSL_PARAM_locate_const(params, key); \
1068 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1072 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
1075 struct ec_gen_ctx
*gctx
= genctx
;
1076 const OSSL_PARAM
*p
;
1077 EC_GROUP
*group
= NULL
;
1079 COPY_INT_PARAM(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, gctx
->ecdh_mode
);
1081 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_GROUP_NAME
, gctx
->group_name
);
1082 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
, gctx
->field_type
);
1083 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_ENCODING
, gctx
->encoding
);
1084 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, gctx
->pt_format
);
1085 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, gctx
->group_check
);
1087 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
);
1088 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
);
1089 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
);
1090 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
);
1091 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_COFACTOR
, gctx
->cofactor
);
1093 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_SEED
, gctx
->seed
, gctx
->seed_len
);
1094 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_GENERATOR
, gctx
->gen
,
1099 EC_GROUP_free(group
);
1103 static int ec_gen_set_group_from_params(struct ec_gen_ctx
*gctx
)
1106 OSSL_PARAM_BLD
*bld
;
1107 OSSL_PARAM
*params
= NULL
;
1108 EC_GROUP
*group
= NULL
;
1110 bld
= OSSL_PARAM_BLD_new();
1114 if (gctx
->encoding
!= NULL
1115 && !OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_ENCODING
,
1119 if (gctx
->pt_format
!= NULL
1120 && !OSSL_PARAM_BLD_push_utf8_string(bld
,
1121 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
1122 gctx
->pt_format
, 0))
1125 if (gctx
->group_name
!= NULL
) {
1126 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_GROUP_NAME
,
1127 gctx
->group_name
, 0))
1129 /* Ignore any other parameters if there is a group name */
1131 } else if (gctx
->field_type
!= NULL
) {
1132 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
1133 gctx
->field_type
, 0))
1141 || gctx
->order
== NULL
1142 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
)
1143 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
)
1144 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
)
1145 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
))
1148 if (gctx
->cofactor
!= NULL
1149 && !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1153 if (gctx
->seed
!= NULL
1154 && !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_SEED
,
1155 gctx
->seed
, gctx
->seed_len
))
1158 if (gctx
->gen
== NULL
1159 || !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_GENERATOR
,
1160 gctx
->gen
, gctx
->gen_len
))
1163 params
= OSSL_PARAM_BLD_to_param(bld
);
1166 group
= EC_GROUP_new_from_params(params
, gctx
->libctx
, NULL
);
1170 EC_GROUP_free(gctx
->gen_group
);
1171 gctx
->gen_group
= group
;
1175 OSSL_PARAM_free(params
);
1176 OSSL_PARAM_BLD_free(bld
);
1180 static const OSSL_PARAM
*ec_gen_settable_params(ossl_unused
void *genctx
,
1181 ossl_unused
void *provctx
)
1183 static OSSL_PARAM settable
[] = {
1184 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
1185 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
1186 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
1187 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
1188 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE
, NULL
, 0),
1189 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P
, NULL
, 0),
1190 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A
, NULL
, 0),
1191 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B
, NULL
, 0),
1192 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR
, NULL
, 0),
1193 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER
, NULL
, 0),
1194 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR
, NULL
, 0),
1195 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
1202 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
1204 if (group
== NULL
) {
1205 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
1208 return EC_KEY_set_group(ec
, group
) > 0;
1212 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1214 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1216 struct ec_gen_ctx
*gctx
= genctx
;
1220 if (!ossl_prov_is_running()
1222 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1225 if (gctx
->gen_group
== NULL
) {
1226 if (!ec_gen_set_group_from_params(gctx
))
1229 if (gctx
->encoding
!= NULL
) {
1230 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1234 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1236 if (gctx
->pt_format
!= NULL
) {
1237 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1241 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1245 /* We must always assign a group, no matter what */
1246 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1248 /* Whether you want it or not, you get a keypair, not just one half */
1249 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
1250 ret
= ret
&& EC_KEY_generate_key(ec
);
1252 if (gctx
->ecdh_mode
!= -1)
1253 ret
= ret
&& ossl_ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
1255 if (gctx
->group_check
!= NULL
)
1256 ret
= ret
&& ossl_ec_set_check_group_type_from_name(ec
, gctx
->group_check
);
1260 /* Something went wrong, throw the key away */
1266 # ifndef OPENSSL_NO_SM2
1268 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1270 static void *sm2_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1272 struct ec_gen_ctx
*gctx
= genctx
;
1277 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1280 if (gctx
->gen_group
== NULL
) {
1281 if (!ec_gen_set_group_from_params(gctx
))
1284 if (gctx
->encoding
) {
1285 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1289 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1291 if (gctx
->pt_format
!= NULL
) {
1292 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1296 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1300 /* We must always assign a group, no matter what */
1301 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1303 /* Whether you want it or not, you get a keypair, not just one half */
1304 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
1305 ret
= ret
&& EC_KEY_generate_key(ec
);
1310 /* Something went wrong, throw the key away */
1317 static void ec_gen_cleanup(void *genctx
)
1319 struct ec_gen_ctx
*gctx
= genctx
;
1324 EC_GROUP_free(gctx
->gen_group
);
1328 BN_free(gctx
->order
);
1329 BN_free(gctx
->cofactor
);
1330 OPENSSL_free(gctx
->group_name
);
1331 OPENSSL_free(gctx
->field_type
);
1332 OPENSSL_free(gctx
->pt_format
);
1333 OPENSSL_free(gctx
->encoding
);
1334 OPENSSL_free(gctx
->seed
);
1335 OPENSSL_free(gctx
->gen
);
1339 static void *common_load(const void *reference
, size_t reference_sz
,
1344 if (ossl_prov_is_running() && reference_sz
== sizeof(ec
)) {
1345 /* The contents of the reference is the address to our object */
1346 ec
= *(EC_KEY
**)reference
;
1348 if (!common_check_sm2(ec
, sm2_wanted
))
1351 /* We grabbed, so we detach it */
1352 *(EC_KEY
**)reference
= NULL
;
1358 static void *ec_load(const void *reference
, size_t reference_sz
)
1360 return common_load(reference
, reference_sz
, 0);
1364 # ifndef OPENSSL_NO_SM2
1365 static void *sm2_load(const void *reference
, size_t reference_sz
)
1367 return common_load(reference
, reference_sz
, 1);
1372 static void *ec_dup(const void *keydata_from
, int selection
)
1374 if (ossl_prov_is_running())
1375 return ossl_ec_key_dup(keydata_from
, selection
);
1379 const OSSL_DISPATCH ossl_ec_keymgmt_functions
[] = {
1380 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
1381 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
1382 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1383 (void (*)(void))ec_gen_set_template
},
1384 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1385 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1386 (void (*)(void))ec_gen_settable_params
},
1387 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
1388 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1389 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
1390 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1391 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
1392 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
1393 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1394 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
1395 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1396 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1397 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
1398 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
1399 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1400 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1401 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1402 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1403 (void (*)(void))ec_query_operation_name
},
1404 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},
1409 # ifndef OPENSSL_NO_SM2
1410 const OSSL_DISPATCH ossl_sm2_keymgmt_functions
[] = {
1411 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))sm2_newdata
},
1412 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))sm2_gen_init
},
1413 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1414 (void (*)(void))ec_gen_set_template
},
1415 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1416 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1417 (void (*)(void))ec_gen_settable_params
},
1418 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))sm2_gen
},
1419 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1420 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))sm2_load
},
1421 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1422 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))sm2_get_params
},
1423 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))sm2_gettable_params
},
1424 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1425 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))sm2_settable_params
},
1426 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1427 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1428 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))sm2_validate
},
1429 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))sm2_import
},
1430 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1431 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1432 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1433 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1434 (void (*)(void))sm2_query_operation_name
},
1435 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},