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 if ((pub_key_len
= EC_POINT_point2buf(ecg
, pub_point
,
151 POINT_CONVERSION_COMPRESSED
,
152 pub_key
, bnctx
)) == 0
153 || !ossl_param_build_set_octet_string(tmpl
, p
,
154 OSSL_PKEY_PARAM_PUB_KEY
,
155 *pub_key
, pub_key_len
))
158 if (px
!= NULL
|| py
!= NULL
) {
160 x
= BN_CTX_get(bnctx
);
162 y
= BN_CTX_get(bnctx
);
164 if (!EC_POINT_get_affine_coordinates(ecg
, pub_point
, x
, y
, bnctx
))
167 && !ossl_param_build_set_bn(tmpl
, px
,
168 OSSL_PKEY_PARAM_EC_PUB_X
, x
))
171 && !ossl_param_build_set_bn(tmpl
, py
,
172 OSSL_PKEY_PARAM_EC_PUB_Y
, y
))
177 if (priv_key
!= NULL
&& include_private
) {
182 * Key import/export should never leak the bit length of the secret
185 * For this reason, on export we use padded BIGNUMs with fixed length.
187 * When importing we also should make sure that, even if short lived,
188 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
189 * soon as possible, so that any processing of this BIGNUM might opt for
190 * constant time implementations in the backend.
192 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
193 * to preallocate the BIGNUM internal buffer to a fixed public size big
194 * enough that operations performed during the processing never trigger
195 * a realloc which would leak the size of the scalar through memory
201 * The order of the large prime subgroup of the curve is our choice for
202 * a fixed public size, as that is generally the upper bound for
203 * generating a private key in EC cryptosystems and should fit all valid
206 * For padding on export we just use the bit length of the order
207 * converted to bytes (rounding up).
209 * For preallocating the BIGNUM storage we look at the number of "words"
210 * required for the internal representation of the order, and we
211 * preallocate 2 extra "words" in case any of the subsequent processing
212 * might temporarily overflow the order length.
214 ecbits
= EC_GROUP_order_bits(ecg
);
217 sz
= (ecbits
+ 7) / 8;
219 if (!ossl_param_build_set_bn_pad(tmpl
, params
,
220 OSSL_PKEY_PARAM_PRIV_KEY
,
231 int otherparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
234 int ecdh_cofactor_mode
= 0, group_check
= 0;
235 const char *name
= NULL
;
236 point_conversion_form_t format
;
241 format
= EC_KEY_get_conv_form(ec
);
242 name
= ossl_ec_pt_format_id2name((int)format
);
244 && !ossl_param_build_set_utf8_string(tmpl
, params
,
245 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
249 group_check
= EC_KEY_get_flags(ec
) & EC_FLAG_CHECK_NAMED_GROUP_MASK
;
250 name
= ossl_ec_check_group_type_id2name(group_check
);
252 && !ossl_param_build_set_utf8_string(tmpl
, params
,
253 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
,
257 if ((EC_KEY_get_enc_flags(ec
) & EC_PKEY_NO_PUBKEY
) != 0
258 && !ossl_param_build_set_int(tmpl
, params
,
259 OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, 0))
263 (EC_KEY_get_flags(ec
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
264 return ossl_param_build_set_int(tmpl
, params
,
265 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
270 void *ec_newdata(void *provctx
)
272 if (!ossl_prov_is_running())
274 return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx
), NULL
);
278 # ifndef OPENSSL_NO_SM2
280 void *sm2_newdata(void *provctx
)
282 if (!ossl_prov_is_running())
284 return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx
), NULL
, NID_sm2
);
290 void ec_freedata(void *keydata
)
292 EC_KEY_free(keydata
);
296 int ec_has(const void *keydata
, int selection
)
298 const EC_KEY
*ec
= keydata
;
301 if (!ossl_prov_is_running() || ec
== NULL
)
303 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
304 return 1; /* the selection is not missing */
306 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
307 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
308 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
309 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
310 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
311 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
313 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
314 * available, so no extra check is needed other than the previous one
315 * against EC_POSSIBLE_SELECTIONS.
320 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
322 const EC_KEY
*ec1
= keydata1
;
323 const EC_KEY
*ec2
= keydata2
;
324 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
325 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
329 if (!ossl_prov_is_running())
332 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1
));
336 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
337 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
338 && EC_GROUP_cmp(group_a
, group_b
, ctx
) == 0;
339 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
342 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
343 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
344 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
346 if (pa
!= NULL
&& pb
!= NULL
) {
347 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, ctx
) == 0;
352 && (selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
353 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
354 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
356 if (pa
!= NULL
&& pb
!= NULL
) {
357 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
361 ok
= ok
&& key_checked
;
367 static int common_check_sm2(const EC_KEY
*ec
, int sm2_wanted
)
369 const EC_GROUP
*ecg
= NULL
;
372 * sm2_wanted: import the keys or domparams only on SM2 Curve
373 * !sm2_wanted: import the keys or domparams only not on SM2 Curve
375 if ((ecg
= EC_KEY_get0_group(ec
)) == NULL
376 || (sm2_wanted
^ (EC_GROUP_get_curve_name(ecg
) == NID_sm2
)))
382 int common_import(void *keydata
, int selection
, const OSSL_PARAM params
[],
385 EC_KEY
*ec
= keydata
;
388 if (!ossl_prov_is_running() || ec
== NULL
)
392 * In this implementation, we can export/import only keydata in the
393 * following combinations:
394 * - domain parameters (+optional other params)
395 * - public key with associated domain parameters (+optional other params)
396 * - private key with associated domain parameters and optional public key
397 * (+optional other params)
400 * - domain parameters must always be requested
401 * - private key must be requested alongside public key
402 * - other parameters are always optional
404 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
407 ok
= ok
&& ossl_ec_group_fromdata(ec
, params
);
409 if (!common_check_sm2(ec
, sm2_wanted
))
412 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
413 int include_private
=
414 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
416 ok
= ok
&& ossl_ec_key_fromdata(ec
, params
, include_private
);
418 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
419 ok
= ok
&& ossl_ec_key_otherparams_fromdata(ec
, params
);
425 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
427 return common_import(keydata
, selection
, params
, 0);
431 # ifndef OPENSSL_NO_SM2
433 int sm2_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
435 return common_import(keydata
, selection
, params
, 1);
441 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
444 EC_KEY
*ec
= keydata
;
445 OSSL_PARAM_BLD
*tmpl
= NULL
;
446 OSSL_PARAM
*params
= NULL
;
447 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
448 BN_CTX
*bnctx
= NULL
;
451 if (!ossl_prov_is_running() || ec
== NULL
)
455 * In this implementation, we can export/import only keydata in the
456 * following combinations:
457 * - domain parameters (+optional other params)
458 * - public key with associated domain parameters (+optional other params)
459 * - private key with associated public key and domain parameters
460 * (+optional other params)
463 * - domain parameters must always be requested
464 * - private key must be requested alongside public key
465 * - other parameters are always optional
467 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
469 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
470 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
473 tmpl
= OSSL_PARAM_BLD_new();
477 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
478 bnctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(ec
));
484 ok
= ok
&& ossl_ec_group_todata(EC_KEY_get0_group(ec
), tmpl
, NULL
,
485 ossl_ec_key_get_libctx(ec
),
486 ossl_ec_key_get0_propq(ec
),
490 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
491 int include_private
=
492 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
494 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
496 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
497 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
499 if (ok
&& (params
= OSSL_PARAM_BLD_to_param(tmpl
)) != NULL
)
500 ok
= param_cb(params
, cbarg
);
502 OSSL_PARAM_free(params
);
503 OSSL_PARAM_BLD_free(tmpl
);
504 OPENSSL_free(pub_key
);
505 OPENSSL_free(genbuf
);
511 /* IMEXPORT = IMPORT + EXPORT */
513 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
514 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
515 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
516 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
517 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
518 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
519 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
520 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
521 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
522 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
523 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
524 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
526 # define EC_IMEXPORTABLE_PUBLIC_KEY \
527 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
528 # define EC_IMEXPORTABLE_PRIVATE_KEY \
529 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
530 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
531 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
532 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
535 * Include all the possible combinations of OSSL_PARAM arrays for
536 * ec_imexport_types().
538 * They are in a separate file as it is ~100 lines of unreadable and
539 * uninteresting machine generated stuff.
541 #include "ec_kmgmt_imexport.inc"
544 const OSSL_PARAM
*ec_imexport_types(int selection
)
548 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
550 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
552 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
554 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
556 return ec_types
[type_select
];
560 const OSSL_PARAM
*ec_import_types(int selection
)
562 return ec_imexport_types(selection
);
566 const OSSL_PARAM
*ec_export_types(int selection
)
568 return ec_imexport_types(selection
);
571 static int ec_get_ecm_params(const EC_GROUP
*group
, OSSL_PARAM params
[])
573 #ifdef OPENSSL_NO_EC2M
577 unsigned int k1
= 0, k2
= 0, k3
= 0;
579 const char *basis_name
= NULL
;
580 int fid
= EC_GROUP_get_field_type(group
);
582 if (fid
!= NID_X9_62_characteristic_two_field
)
585 basis_nid
= EC_GROUP_get_basis_type(group
);
586 if (basis_nid
== NID_X9_62_tpBasis
)
587 basis_name
= SN_X9_62_tpBasis
;
588 else if (basis_nid
== NID_X9_62_ppBasis
)
589 basis_name
= SN_X9_62_ppBasis
;
593 m
= EC_GROUP_get_degree(group
);
594 if (!ossl_param_build_set_int(NULL
, params
, OSSL_PKEY_PARAM_EC_CHAR2_M
, m
)
595 || !ossl_param_build_set_utf8_string(NULL
, params
,
596 OSSL_PKEY_PARAM_EC_CHAR2_TYPE
,
600 if (basis_nid
== NID_X9_62_tpBasis
) {
601 if (!EC_GROUP_get_trinomial_basis(group
, &k1
)
602 || !ossl_param_build_set_int(NULL
, params
,
603 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS
,
607 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
)
608 || !ossl_param_build_set_int(NULL
, params
,
609 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1
, (int)k1
)
610 || !ossl_param_build_set_int(NULL
, params
,
611 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2
, (int)k2
)
612 || !ossl_param_build_set_int(NULL
, params
,
613 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3
, (int)k3
))
619 #endif /* OPENSSL_NO_EC2M */
623 int common_get_params(void *key
, OSSL_PARAM params
[], int sm2
)
627 const EC_GROUP
*ecg
= NULL
;
629 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
630 OSSL_LIB_CTX
*libctx
;
632 BN_CTX
*bnctx
= NULL
;
634 ecg
= EC_KEY_get0_group(eck
);
638 libctx
= ossl_ec_key_get_libctx(eck
);
639 propq
= ossl_ec_key_get0_propq(eck
);
641 bnctx
= BN_CTX_new_ex(libctx
);
646 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
647 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
649 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
650 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
652 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
653 int ecbits
, sec_bits
;
655 ecbits
= EC_GROUP_order_bits(ecg
);
658 * The following estimates are based on the values published
659 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
660 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
662 * Note that the above reference explicitly categorizes algorithms in a
663 * discrete set of values {80, 112, 128, 192, 256}, and that it is
664 * relevant only for NIST approved Elliptic Curves, while OpenSSL
665 * applies the same logic also to other curves.
667 * Classifications produced by other standardazing bodies might differ,
668 * so the results provided for "bits of security" by this provider are
669 * to be considered merely indicative, and it is the users'
670 * responsibility to compare these values against the normative
671 * references that may be relevant for their intent and purposes.
675 else if (ecbits
>= 384)
677 else if (ecbits
>= 256)
679 else if (ecbits
>= 224)
681 else if (ecbits
>= 160)
684 sec_bits
= ecbits
/ 2;
686 if (!OSSL_PARAM_set_int(p
, sec_bits
))
690 if ((p
= OSSL_PARAM_locate(params
,
691 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
))
693 int explicitparams
= EC_KEY_decoded_from_explicit_params(eck
);
695 if (explicitparams
< 0
696 || !OSSL_PARAM_set_int(p
, explicitparams
))
701 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
702 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
705 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
706 && !OSSL_PARAM_set_utf8_string(p
, SM2_DEFAULT_MD
))
710 /* SM2 doesn't support this PARAM */
712 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
714 int ecdh_cofactor_mode
= 0;
717 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
719 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
723 if ((p
= OSSL_PARAM_locate(params
,
724 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
)) != NULL
) {
725 p
->return_size
= EC_POINT_point2oct(EC_KEY_get0_group(key
),
726 EC_KEY_get0_public_key(key
),
727 POINT_CONVERSION_UNCOMPRESSED
,
728 p
->data
, p
->return_size
, bnctx
);
729 if (p
->return_size
== 0)
733 ret
= ec_get_ecm_params(ecg
, params
)
734 && ossl_ec_group_todata(ecg
, NULL
, params
, libctx
, propq
, bnctx
,
736 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
737 && otherparams_to_params(eck
, NULL
, params
);
739 OPENSSL_free(genbuf
);
740 OPENSSL_free(pub_key
);
747 int ec_get_params(void *key
, OSSL_PARAM params
[])
749 return common_get_params(key
, params
, 0);
752 #ifndef OPENSSL_NO_EC2M
753 # define EC2M_GETTABLE_DOM_PARAMS \
754 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
755 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
756 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
757 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
758 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
759 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
761 # define EC2M_GETTABLE_DOM_PARAMS
764 static const OSSL_PARAM ec_known_gettable_params
[] = {
765 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
766 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
767 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
768 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
769 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
770 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
771 EC_IMEXPORTABLE_DOM_PARAMETERS
,
772 EC2M_GETTABLE_DOM_PARAMS
773 EC_IMEXPORTABLE_PUBLIC_KEY
,
774 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
775 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
776 EC_IMEXPORTABLE_PRIVATE_KEY
,
777 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
782 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
784 return ec_known_gettable_params
;
787 static const OSSL_PARAM ec_known_settable_params
[] = {
788 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
789 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
790 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
791 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
792 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
793 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC
, NULL
),
794 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, NULL
, 0),
799 const OSSL_PARAM
*ec_settable_params(void *provctx
)
801 return ec_known_settable_params
;
805 int ec_set_params(void *key
, const OSSL_PARAM params
[])
816 if (!ossl_ec_group_set_params((EC_GROUP
*)EC_KEY_get0_group(key
), params
))
819 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
);
821 BN_CTX
*ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(key
));
825 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
826 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
833 return ossl_ec_key_otherparams_fromdata(eck
, params
);
837 # ifndef OPENSSL_NO_SM2
839 int sm2_get_params(void *key
, OSSL_PARAM params
[])
841 return common_get_params(key
, params
, 1);
844 static const OSSL_PARAM sm2_known_gettable_params
[] = {
845 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
846 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
847 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
848 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
849 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
850 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
, NULL
),
851 EC_IMEXPORTABLE_DOM_PARAMETERS
,
852 EC_IMEXPORTABLE_PUBLIC_KEY
,
853 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
854 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
855 EC_IMEXPORTABLE_PRIVATE_KEY
,
860 const OSSL_PARAM
*sm2_gettable_params(ossl_unused
void *provctx
)
862 return sm2_known_gettable_params
;
865 static const OSSL_PARAM sm2_known_settable_params
[] = {
866 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
, NULL
, 0),
871 const OSSL_PARAM
*sm2_settable_params(ossl_unused
void *provctx
)
873 return sm2_known_settable_params
;
877 int sm2_validate(const void *keydata
, int selection
, int checktype
)
879 const EC_KEY
*eck
= keydata
;
883 if (!ossl_prov_is_running())
886 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
887 return 1; /* nothing to validate */
889 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
893 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
894 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
896 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
897 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
898 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
900 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
903 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
904 ok
= ok
&& ossl_sm2_key_private_check(eck
);
906 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
907 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
916 int ec_validate(const void *keydata
, int selection
, int checktype
)
918 const EC_KEY
*eck
= keydata
;
922 if (!ossl_prov_is_running())
925 if ((selection
& EC_POSSIBLE_SELECTIONS
) == 0)
926 return 1; /* nothing to validate */
928 ctx
= BN_CTX_new_ex(ossl_ec_key_get_libctx(eck
));
932 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
933 int flags
= EC_KEY_get_flags(eck
);
935 if ((flags
& EC_FLAG_CHECK_NAMED_GROUP
) != 0)
936 ok
= ok
&& EC_GROUP_check_named_curve(EC_KEY_get0_group(eck
),
937 (flags
& EC_FLAG_CHECK_NAMED_GROUP_NIST
) != 0, ctx
);
939 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
942 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
943 if (checktype
== OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
)
944 ok
= ok
&& ossl_ec_key_public_check_quick(eck
, ctx
);
946 ok
= ok
&& ossl_ec_key_public_check(eck
, ctx
);
949 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
950 ok
= ok
&& ossl_ec_key_private_check(eck
);
952 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
953 ok
= ok
&& ossl_ec_key_pairwise_check(eck
, ctx
);
960 OSSL_LIB_CTX
*libctx
;
966 BIGNUM
*p
, *a
, *b
, *order
, *cofactor
;
967 unsigned char *gen
, *seed
;
968 size_t gen_len
, seed_len
;
974 static void *ec_gen_init(void *provctx
, int selection
,
975 const OSSL_PARAM params
[])
977 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(provctx
);
978 struct ec_gen_ctx
*gctx
= NULL
;
980 if (!ossl_prov_is_running() || (selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
983 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
984 gctx
->libctx
= libctx
;
985 gctx
->selection
= selection
;
988 if (!ec_gen_set_params(gctx
, params
)) {
996 # ifndef OPENSSL_NO_SM2
997 static void *sm2_gen_init(void *provctx
, int selection
,
998 const OSSL_PARAM params
[])
1000 struct ec_gen_ctx
*gctx
= ec_gen_init(provctx
, selection
, params
);
1003 if (gctx
->group_name
!= NULL
)
1005 if ((gctx
->group_name
= OPENSSL_strdup("sm2")) != NULL
)
1007 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
1008 ec_gen_cleanup(gctx
);
1015 static int ec_gen_set_group(void *genctx
, const EC_GROUP
*src
)
1017 struct ec_gen_ctx
*gctx
= genctx
;
1020 group
= EC_GROUP_dup(src
);
1021 if (group
== NULL
) {
1022 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
1025 EC_GROUP_free(gctx
->gen_group
);
1026 gctx
->gen_group
= group
;
1030 static int ec_gen_set_template(void *genctx
, void *templ
)
1032 struct ec_gen_ctx
*gctx
= genctx
;
1034 const EC_GROUP
*ec_group
;
1036 if (!ossl_prov_is_running() || gctx
== NULL
|| ec
== NULL
)
1038 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
1040 return ec_gen_set_group(gctx
, ec_group
);
1043 #define COPY_INT_PARAM(params, key, val) \
1044 p = OSSL_PARAM_locate_const(params, key); \
1045 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
1048 #define COPY_UTF8_PARAM(params, key, val) \
1049 p = OSSL_PARAM_locate_const(params, key); \
1051 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
1053 OPENSSL_free(val); \
1054 val = OPENSSL_strdup(p->data); \
1059 #define COPY_OCTET_PARAM(params, key, val, len) \
1060 p = OSSL_PARAM_locate_const(params, key); \
1062 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
1064 OPENSSL_free(val); \
1065 len = p->data_size; \
1066 val = OPENSSL_memdup(p->data, p->data_size); \
1071 #define COPY_BN_PARAM(params, key, bn) \
1072 p = OSSL_PARAM_locate_const(params, key); \
1076 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1080 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
1083 struct ec_gen_ctx
*gctx
= genctx
;
1084 const OSSL_PARAM
*p
;
1085 EC_GROUP
*group
= NULL
;
1087 COPY_INT_PARAM(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, gctx
->ecdh_mode
);
1089 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_GROUP_NAME
, gctx
->group_name
);
1090 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
, gctx
->field_type
);
1091 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_ENCODING
, gctx
->encoding
);
1092 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, gctx
->pt_format
);
1093 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
, gctx
->group_check
);
1095 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
);
1096 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
);
1097 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
);
1098 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
);
1099 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_COFACTOR
, gctx
->cofactor
);
1101 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_SEED
, gctx
->seed
, gctx
->seed_len
);
1102 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_GENERATOR
, gctx
->gen
,
1107 EC_GROUP_free(group
);
1111 static int ec_gen_set_group_from_params(struct ec_gen_ctx
*gctx
)
1114 OSSL_PARAM_BLD
*bld
;
1115 OSSL_PARAM
*params
= NULL
;
1116 EC_GROUP
*group
= NULL
;
1118 bld
= OSSL_PARAM_BLD_new();
1122 if (gctx
->encoding
!= NULL
1123 && !OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_ENCODING
,
1127 if (gctx
->pt_format
!= NULL
1128 && !OSSL_PARAM_BLD_push_utf8_string(bld
,
1129 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
1130 gctx
->pt_format
, 0))
1133 if (gctx
->group_name
!= NULL
) {
1134 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_GROUP_NAME
,
1135 gctx
->group_name
, 0))
1137 /* Ignore any other parameters if there is a group name */
1139 } else if (gctx
->field_type
!= NULL
) {
1140 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
1141 gctx
->field_type
, 0))
1149 || gctx
->order
== NULL
1150 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
)
1151 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
)
1152 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
)
1153 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
))
1156 if (gctx
->cofactor
!= NULL
1157 && !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1161 if (gctx
->seed
!= NULL
1162 && !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_SEED
,
1163 gctx
->seed
, gctx
->seed_len
))
1166 if (gctx
->gen
== NULL
1167 || !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_GENERATOR
,
1168 gctx
->gen
, gctx
->gen_len
))
1171 params
= OSSL_PARAM_BLD_to_param(bld
);
1174 group
= EC_GROUP_new_from_params(params
, gctx
->libctx
, NULL
);
1178 EC_GROUP_free(gctx
->gen_group
);
1179 gctx
->gen_group
= group
;
1183 OSSL_PARAM_free(params
);
1184 OSSL_PARAM_BLD_free(bld
);
1188 static const OSSL_PARAM
*ec_gen_settable_params(ossl_unused
void *genctx
,
1189 ossl_unused
void *provctx
)
1191 static OSSL_PARAM settable
[] = {
1192 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
1193 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
1194 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
1195 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
, NULL
, 0),
1196 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE
, NULL
, 0),
1197 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P
, NULL
, 0),
1198 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A
, NULL
, 0),
1199 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B
, NULL
, 0),
1200 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR
, NULL
, 0),
1201 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER
, NULL
, 0),
1202 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR
, NULL
, 0),
1203 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
1210 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
1212 if (group
== NULL
) {
1213 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
1216 return EC_KEY_set_group(ec
, group
) > 0;
1220 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1222 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1224 struct ec_gen_ctx
*gctx
= genctx
;
1228 if (!ossl_prov_is_running()
1230 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1233 if (gctx
->gen_group
== NULL
) {
1234 if (!ec_gen_set_group_from_params(gctx
))
1237 if (gctx
->encoding
!= NULL
) {
1238 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1242 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1244 if (gctx
->pt_format
!= NULL
) {
1245 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1249 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1253 /* We must always assign a group, no matter what */
1254 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1256 /* Whether you want it or not, you get a keypair, not just one half */
1257 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
1258 ret
= ret
&& EC_KEY_generate_key(ec
);
1260 if (gctx
->ecdh_mode
!= -1)
1261 ret
= ret
&& ossl_ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
1263 if (gctx
->group_check
!= NULL
)
1264 ret
= ret
&& ossl_ec_set_check_group_type_from_name(ec
, gctx
->group_check
);
1268 /* Something went wrong, throw the key away */
1274 # ifndef OPENSSL_NO_SM2
1276 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1278 static void *sm2_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
1280 struct ec_gen_ctx
*gctx
= genctx
;
1285 || (ec
= EC_KEY_new_ex(gctx
->libctx
, NULL
)) == NULL
)
1288 if (gctx
->gen_group
== NULL
) {
1289 if (!ec_gen_set_group_from_params(gctx
))
1292 if (gctx
->encoding
) {
1293 int flags
= ossl_ec_encoding_name2id(gctx
->encoding
);
1297 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
1299 if (gctx
->pt_format
!= NULL
) {
1300 int format
= ossl_ec_pt_format_name2id(gctx
->pt_format
);
1304 EC_GROUP_set_point_conversion_form(gctx
->gen_group
, format
);
1308 /* We must always assign a group, no matter what */
1309 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
1311 /* Whether you want it or not, you get a keypair, not just one half */
1312 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
1313 ret
= ret
&& EC_KEY_generate_key(ec
);
1318 /* Something went wrong, throw the key away */
1325 static void ec_gen_cleanup(void *genctx
)
1327 struct ec_gen_ctx
*gctx
= genctx
;
1332 EC_GROUP_free(gctx
->gen_group
);
1336 BN_free(gctx
->order
);
1337 BN_free(gctx
->cofactor
);
1338 OPENSSL_free(gctx
->group_name
);
1339 OPENSSL_free(gctx
->field_type
);
1340 OPENSSL_free(gctx
->pt_format
);
1341 OPENSSL_free(gctx
->encoding
);
1342 OPENSSL_free(gctx
->seed
);
1343 OPENSSL_free(gctx
->gen
);
1347 static void *common_load(const void *reference
, size_t reference_sz
,
1352 if (ossl_prov_is_running() && reference_sz
== sizeof(ec
)) {
1353 /* The contents of the reference is the address to our object */
1354 ec
= *(EC_KEY
**)reference
;
1356 if (!common_check_sm2(ec
, sm2_wanted
))
1359 /* We grabbed, so we detach it */
1360 *(EC_KEY
**)reference
= NULL
;
1366 static void *ec_load(const void *reference
, size_t reference_sz
)
1368 return common_load(reference
, reference_sz
, 0);
1372 # ifndef OPENSSL_NO_SM2
1373 static void *sm2_load(const void *reference
, size_t reference_sz
)
1375 return common_load(reference
, reference_sz
, 1);
1380 static void *ec_dup(const void *keydata_from
, int selection
)
1382 if (ossl_prov_is_running())
1383 return ossl_ec_key_dup(keydata_from
, selection
);
1387 const OSSL_DISPATCH ossl_ec_keymgmt_functions
[] = {
1388 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
1389 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
1390 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1391 (void (*)(void))ec_gen_set_template
},
1392 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1393 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1394 (void (*)(void))ec_gen_settable_params
},
1395 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
1396 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1397 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
1398 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1399 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
1400 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
1401 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1402 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
1403 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1404 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1405 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
1406 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
1407 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1408 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1409 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1410 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1411 (void (*)(void))ec_query_operation_name
},
1412 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},
1417 # ifndef OPENSSL_NO_SM2
1418 const OSSL_DISPATCH ossl_sm2_keymgmt_functions
[] = {
1419 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))sm2_newdata
},
1420 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))sm2_gen_init
},
1421 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1422 (void (*)(void))ec_gen_set_template
},
1423 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1424 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1425 (void (*)(void))ec_gen_settable_params
},
1426 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))sm2_gen
},
1427 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1428 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))sm2_load
},
1429 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1430 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))sm2_get_params
},
1431 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))sm2_gettable_params
},
1432 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1433 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))sm2_settable_params
},
1434 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1435 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1436 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))sm2_validate
},
1437 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))sm2_import
},
1438 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1439 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1440 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1441 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1442 (void (*)(void))sm2_query_operation_name
},
1443 { OSSL_FUNC_KEYMGMT_DUP
, (void (*)(void))ec_dup
},