2 * Copyright 2020 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 "crypto/bn.h"
24 #include "crypto/ec.h"
25 #include "prov/implementations.h"
26 #include "prov/providercommon.h"
27 #include "prov/providercommonerr.h"
28 #include "prov/provider_ctx.h"
29 #include "internal/param_build_set.h"
31 static OSSL_FUNC_keymgmt_new_fn ec_newdata
;
32 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init
;
33 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template
;
34 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params
;
35 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params
;
36 static OSSL_FUNC_keymgmt_gen_fn ec_gen
;
37 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup
;
38 static OSSL_FUNC_keymgmt_load_fn ec_load
;
39 static OSSL_FUNC_keymgmt_free_fn ec_freedata
;
40 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params
;
41 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params
;
42 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params
;
43 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params
;
44 static OSSL_FUNC_keymgmt_has_fn ec_has
;
45 static OSSL_FUNC_keymgmt_match_fn ec_match
;
46 static OSSL_FUNC_keymgmt_validate_fn ec_validate
;
47 static OSSL_FUNC_keymgmt_import_fn ec_import
;
48 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types
;
49 static OSSL_FUNC_keymgmt_export_fn ec_export
;
50 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types
;
51 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name
;
53 #define EC_DEFAULT_MD "SHA256"
54 #define EC_POSSIBLE_SELECTIONS \
55 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
58 const char *ec_query_operation_name(int operation_id
)
60 switch (operation_id
) {
63 case OSSL_OP_SIGNATURE
:
70 * Callers of key_to_params MUST make sure that domparams_to_params is also
73 * This function only exports the bare keypair, domain parameters and other
74 * parameters are exported separately.
77 int key_to_params(const EC_KEY
*eckey
, OSSL_PARAM_BLD
*tmpl
,
78 OSSL_PARAM params
[], int include_private
,
79 unsigned char **pub_key
)
81 BIGNUM
*x
= NULL
, *y
= NULL
;
82 const BIGNUM
*priv_key
= NULL
;
83 const EC_POINT
*pub_point
= NULL
;
84 const EC_GROUP
*ecg
= NULL
;
85 size_t pub_key_len
= 0;
90 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
93 priv_key
= EC_KEY_get0_private_key(eckey
);
94 pub_point
= EC_KEY_get0_public_key(eckey
);
96 if (pub_point
!= NULL
) {
97 OSSL_PARAM
*p
= NULL
, *px
= NULL
, *py
= NULL
;
99 * EC_POINT_point2buf() can generate random numbers in some
100 * implementations so we need to ensure we use the correct libctx.
102 bnctx
= BN_CTX_new_ex(ec_key_get_libctx(eckey
));
107 /* If we are doing a get then check first before decoding the point */
109 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_PUB_KEY
);
110 px
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_X
);
111 py
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_Y
);
114 if (p
!= NULL
|| tmpl
!= NULL
) {
115 /* convert pub_point to a octet string according to the SECG standard */
116 if ((pub_key_len
= EC_POINT_point2buf(ecg
, pub_point
,
117 POINT_CONVERSION_COMPRESSED
,
118 pub_key
, bnctx
)) == 0
119 || !ossl_param_build_set_octet_string(tmpl
, p
,
120 OSSL_PKEY_PARAM_PUB_KEY
,
121 *pub_key
, pub_key_len
))
124 if (px
!= NULL
|| py
!= NULL
) {
126 x
= BN_CTX_get(bnctx
);
128 y
= BN_CTX_get(bnctx
);
130 if (!EC_POINT_get_affine_coordinates(ecg
, pub_point
, x
, y
, bnctx
))
133 && !ossl_param_build_set_bn(tmpl
, px
,
134 OSSL_PKEY_PARAM_EC_PUB_X
, x
))
137 && !ossl_param_build_set_bn(tmpl
, py
,
138 OSSL_PKEY_PARAM_EC_PUB_Y
, y
))
143 if (priv_key
!= NULL
&& include_private
) {
148 * Key import/export should never leak the bit length of the secret
151 * For this reason, on export we use padded BIGNUMs with fixed length.
153 * When importing we also should make sure that, even if short lived,
154 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
155 * soon as possible, so that any processing of this BIGNUM might opt for
156 * constant time implementations in the backend.
158 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
159 * to preallocate the BIGNUM internal buffer to a fixed public size big
160 * enough that operations performed during the processing never trigger
161 * a realloc which would leak the size of the scalar through memory
167 * The order of the large prime subgroup of the curve is our choice for
168 * a fixed public size, as that is generally the upper bound for
169 * generating a private key in EC cryptosystems and should fit all valid
172 * For padding on export we just use the bit length of the order
173 * converted to bytes (rounding up).
175 * For preallocating the BIGNUM storage we look at the number of "words"
176 * required for the internal representation of the order, and we
177 * preallocate 2 extra "words" in case any of the subsequent processing
178 * might temporarily overflow the order length.
180 ecbits
= EC_GROUP_order_bits(ecg
);
183 sz
= (ecbits
+ 7 ) / 8;
185 if (!ossl_param_build_set_bn_pad(tmpl
, params
,
186 OSSL_PKEY_PARAM_PRIV_KEY
,
197 int otherparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
200 int ecdh_cofactor_mode
= 0;
206 (EC_KEY_get_flags(ec
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
207 return ossl_param_build_set_int(tmpl
, params
,
208 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
213 void *ec_newdata(void *provctx
)
215 if (!ossl_prov_is_running())
217 return EC_KEY_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx
), NULL
);
221 void ec_freedata(void *keydata
)
223 EC_KEY_free(keydata
);
227 int ec_has(void *keydata
, int selection
)
229 EC_KEY
*ec
= keydata
;
232 if (ossl_prov_is_running() && ec
!= NULL
) {
233 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
236 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
237 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
238 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
239 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
240 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
241 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
243 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
244 * available, so no extra check is needed other than the previous one
245 * against EC_POSSIBLE_SELECTIONS.
251 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
253 const EC_KEY
*ec1
= keydata1
;
254 const EC_KEY
*ec2
= keydata2
;
255 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
256 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
257 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(ec1
));
260 if (!ossl_prov_is_running())
263 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
264 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
265 && EC_GROUP_cmp(group_a
, group_b
, ctx
) == 0;
266 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
267 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
268 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
270 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
272 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
273 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
274 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
276 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, ctx
) == 0;
283 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
285 EC_KEY
*ec
= keydata
;
288 if (!ossl_prov_is_running() || ec
== NULL
)
292 * In this implementation, we can export/import only keydata in the
293 * following combinations:
294 * - domain parameters (+optional other params)
295 * - public key with associated domain parameters (+optional other params)
296 * - private key with associated public key and domain parameters
297 * (+optional other params)
300 * - domain parameters must always be requested
301 * - private key must be requested alongside public key
302 * - other parameters are always optional
304 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
306 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
307 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
310 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
311 ok
= ok
&& ec_group_fromdata(ec
, params
);
312 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
313 int include_private
=
314 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
316 ok
= ok
&& ec_key_fromdata(ec
, params
, include_private
);
318 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
319 ok
= ok
&& ec_key_otherparams_fromdata(ec
, params
);
325 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
328 EC_KEY
*ec
= keydata
;
329 OSSL_PARAM_BLD
*tmpl
= NULL
;
330 OSSL_PARAM
*params
= NULL
;
331 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
332 BN_CTX
*bnctx
= NULL
;
335 if (!ossl_prov_is_running() || ec
== NULL
)
339 * In this implementation, we can export/import only keydata in the
340 * following combinations:
341 * - domain parameters (+optional other params)
342 * - public key with associated domain parameters (+optional other params)
343 * - private key with associated public key and domain parameters
344 * (+optional other params)
347 * - domain parameters must always be requested
348 * - private key must be requested alongside public key
349 * - other parameters are always optional
351 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
353 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
354 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
356 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0
357 && (selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
360 tmpl
= OSSL_PARAM_BLD_new();
364 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
365 bnctx
= BN_CTX_new_ex(ec_key_get_libctx(ec
));
371 ok
= ok
&& ec_group_todata(EC_KEY_get0_group(ec
), tmpl
, NULL
,
372 ec_key_get_libctx(ec
), ec_key_get0_propq(ec
),
376 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
377 int include_private
=
378 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
380 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
382 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
383 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
385 if (ok
&& (params
= OSSL_PARAM_BLD_to_param(tmpl
)) != NULL
)
386 ok
= param_cb(params
, cbarg
);
388 OSSL_PARAM_BLD_free_params(params
);
389 OSSL_PARAM_BLD_free(tmpl
);
390 OPENSSL_free(pub_key
);
391 OPENSSL_free(genbuf
);
397 /* IMEXPORT = IMPORT + EXPORT */
399 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
400 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
401 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
402 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
403 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
404 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
405 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
406 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
407 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
408 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
409 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
411 # define EC_IMEXPORTABLE_PUBLIC_KEY \
412 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
413 # define EC_IMEXPORTABLE_PRIVATE_KEY \
414 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
415 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
416 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
419 * Include all the possible combinations of OSSL_PARAM arrays for
420 * ec_imexport_types().
422 * They are in a separate file as it is ~100 lines of unreadable and
423 * uninteresting machine generated stuff.
425 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
426 * combinations of the bits in `selection`, it also includes combinations that
427 * are not really useful: we might want to consider alternatives to this
430 #include "ec_kmgmt_imexport.inc"
433 const OSSL_PARAM
*ec_imexport_types(int selection
)
437 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
439 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
441 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
443 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
445 return ec_types
[type_select
];
449 const OSSL_PARAM
*ec_import_types(int selection
)
451 return ec_imexport_types(selection
);
455 const OSSL_PARAM
*ec_export_types(int selection
)
457 return ec_imexport_types(selection
);
460 static int ec_get_ecm_params(const EC_GROUP
*group
, OSSL_PARAM params
[])
462 #ifdef OPENSSL_NO_EC2M
466 unsigned int k1
= 0, k2
= 0, k3
= 0;
468 const char *basis_name
= NULL
;
469 int fid
= EC_GROUP_get_field_type(group
);
471 if (fid
!= NID_X9_62_characteristic_two_field
)
474 basis_nid
= EC_GROUP_get_basis_type(group
);
475 if (basis_nid
== NID_X9_62_tpBasis
)
476 basis_name
= SN_X9_62_tpBasis
;
477 else if (basis_nid
== NID_X9_62_ppBasis
)
478 basis_name
= SN_X9_62_ppBasis
;
482 m
= EC_GROUP_get_degree(group
);
483 if (!ossl_param_build_set_int(NULL
, params
, OSSL_PKEY_PARAM_EC_CHAR2_M
, m
)
484 || !ossl_param_build_set_utf8_string(NULL
, params
,
485 OSSL_PKEY_PARAM_EC_CHAR2_TYPE
,
489 if (basis_nid
== NID_X9_62_tpBasis
) {
490 if (!EC_GROUP_get_trinomial_basis(group
, &k1
)
491 || !ossl_param_build_set_int(NULL
, params
,
492 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS
,
496 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
)
497 || !ossl_param_build_set_int(NULL
, params
,
498 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1
, (int)k1
)
499 || !ossl_param_build_set_int(NULL
, params
,
500 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2
, (int)k2
)
501 || !ossl_param_build_set_int(NULL
, params
,
502 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3
, (int)k3
))
508 #endif /* OPENSSL_NO_EC2M */
512 int ec_get_params(void *key
, OSSL_PARAM params
[])
516 const EC_GROUP
*ecg
= NULL
;
518 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
521 BN_CTX
*bnctx
= NULL
;
523 ecg
= EC_KEY_get0_group(eck
);
527 libctx
= ec_key_get_libctx(eck
);
528 propq
= ec_key_get0_propq(eck
);
530 bnctx
= BN_CTX_new_ex(libctx
);
535 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
536 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
538 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
539 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
541 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
542 int ecbits
, sec_bits
;
544 ecbits
= EC_GROUP_order_bits(ecg
);
547 * The following estimates are based on the values published
548 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
549 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
551 * Note that the above reference explicitly categorizes algorithms in a
552 * discrete set of values {80, 112, 128, 192, 256}, and that it is
553 * relevant only for NIST approved Elliptic Curves, while OpenSSL
554 * applies the same logic also to other curves.
556 * Classifications produced by other standardazing bodies might differ,
557 * so the results provided for "bits of security" by this provider are
558 * to be considered merely indicative, and it is the users'
559 * responsibility to compare these values against the normative
560 * references that may be relevant for their intent and purposes.
564 else if (ecbits
>= 384)
566 else if (ecbits
>= 256)
568 else if (ecbits
>= 224)
570 else if (ecbits
>= 160)
573 sec_bits
= ecbits
/ 2;
575 if (!OSSL_PARAM_set_int(p
, sec_bits
))
579 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
580 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
583 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
585 int ecdh_cofactor_mode
= 0;
588 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
590 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
593 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
)) != NULL
) {
594 p
->return_size
= EC_POINT_point2oct(EC_KEY_get0_group(key
),
595 EC_KEY_get0_public_key(key
),
596 POINT_CONVERSION_UNCOMPRESSED
,
597 p
->data
, p
->return_size
, bnctx
);
598 if (p
->return_size
== 0)
602 ret
= ec_get_ecm_params(ecg
, params
)
603 && ec_group_todata(ecg
, NULL
, params
, libctx
, propq
, bnctx
, &genbuf
)
604 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
605 && otherparams_to_params(eck
, NULL
, params
);
607 OPENSSL_free(genbuf
);
608 OPENSSL_free(pub_key
);
614 #ifndef OPENSSL_NO_EC2M
615 # define EC2M_GETTABLE_DOM_PARAMS \
616 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
617 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
618 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
619 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
620 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
621 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
623 # define EC2M_GETTABLE_DOM_PARAMS
626 static const OSSL_PARAM ec_known_gettable_params
[] = {
627 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
628 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
629 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
630 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
631 EC_IMEXPORTABLE_DOM_PARAMETERS
,
632 EC2M_GETTABLE_DOM_PARAMS
633 EC_IMEXPORTABLE_PUBLIC_KEY
,
634 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
635 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
636 EC_IMEXPORTABLE_PRIVATE_KEY
,
637 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
642 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
644 return ec_known_gettable_params
;
647 static const OSSL_PARAM ec_known_settable_params
[] = {
648 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
649 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
654 const OSSL_PARAM
*ec_settable_params(void *provctx
)
656 return ec_known_settable_params
;
660 int ec_set_params(void *key
, const OSSL_PARAM params
[])
665 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
);
667 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(key
));
671 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
672 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
679 return ec_key_otherparams_fromdata(eck
, params
);
683 int ec_validate(void *keydata
, int selection
)
685 EC_KEY
*eck
= keydata
;
687 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(eck
));
689 if (!ossl_prov_is_running() || ctx
== NULL
)
692 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
695 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
696 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
698 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
699 ok
= ok
&& ec_key_public_check(eck
, ctx
);
701 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
702 ok
= ok
&& ec_key_private_check(eck
);
704 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
705 ok
= ok
&& ec_key_pairwise_check(eck
, ctx
);
716 BIGNUM
*p
, *a
, *b
, *order
, *cofactor
;
717 unsigned char *gen
, *seed
;
718 size_t gen_len
, seed_len
;
724 static void *ec_gen_init(void *provctx
, int selection
)
726 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
727 struct ec_gen_ctx
*gctx
= NULL
;
729 if (!ossl_prov_is_running() || (selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
732 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
733 gctx
->libctx
= libctx
;
734 gctx
->selection
= selection
;
740 static int ec_gen_set_group(void *genctx
, const EC_GROUP
*src
)
742 struct ec_gen_ctx
*gctx
= genctx
;
745 group
= EC_GROUP_dup(src
);
747 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
750 EC_GROUP_free(gctx
->gen_group
);
751 gctx
->gen_group
= group
;
755 static int ec_gen_set_template(void *genctx
, void *templ
)
757 struct ec_gen_ctx
*gctx
= genctx
;
759 const EC_GROUP
*ec_group
;
761 if (!ossl_prov_is_running() || gctx
== NULL
|| ec
== NULL
)
763 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
765 return ec_gen_set_group(gctx
, ec_group
);
768 #define COPY_INT_PARAM(params, key, val) \
769 p = OSSL_PARAM_locate_const(params, key); \
770 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
773 #define COPY_UTF8_PARAM(params, key, val) \
774 p = OSSL_PARAM_locate_const(params, key); \
776 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
779 val = OPENSSL_strdup(p->data); \
784 #define COPY_OCTET_PARAM(params, key, val, len) \
785 p = OSSL_PARAM_locate_const(params, key); \
787 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
790 len = p->data_size; \
791 val = OPENSSL_memdup(p->data, p->data_size); \
796 #define COPY_BN_PARAM(params, key, bn) \
797 p = OSSL_PARAM_locate_const(params, key); \
801 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
805 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
808 struct ec_gen_ctx
*gctx
= genctx
;
810 EC_GROUP
*group
= NULL
;
812 COPY_INT_PARAM(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, gctx
->ecdh_mode
);
814 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_GROUP_NAME
, gctx
->group_name
);
815 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
, gctx
->field_type
);
816 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_ENCODING
, gctx
->encoding
);
818 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
);
819 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
);
820 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
);
821 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
);
822 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_COFACTOR
, gctx
->cofactor
);
824 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_SEED
, gctx
->seed
, gctx
->seed_len
);
825 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_GENERATOR
, gctx
->gen
,
830 EC_GROUP_free(group
);
834 static int ec_gen_set_group_from_params(struct ec_gen_ctx
*gctx
)
838 OSSL_PARAM
*params
= NULL
;
839 EC_GROUP
*group
= NULL
;
841 bld
= OSSL_PARAM_BLD_new();
845 if (gctx
->encoding
!= NULL
846 && !OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_ENCODING
,
850 if (gctx
->group_name
!= NULL
) {
851 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_GROUP_NAME
,
852 gctx
->group_name
, 0))
854 /* Ignore any other parameters if there is a group name */
856 } else if (gctx
->field_type
!= NULL
) {
857 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
858 gctx
->field_type
, 0))
866 || gctx
->order
== NULL
867 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
)
868 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
)
869 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
)
870 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
))
873 if (gctx
->cofactor
!= NULL
874 && !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
878 if (gctx
->seed
!= NULL
879 && !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_SEED
,
880 gctx
->seed
, gctx
->seed_len
))
883 if (gctx
->gen
== NULL
884 || !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_GENERATOR
,
885 gctx
->gen
, gctx
->gen_len
))
888 params
= OSSL_PARAM_BLD_to_param(bld
);
891 group
= EC_GROUP_new_from_params(params
, gctx
->libctx
, NULL
);
895 EC_GROUP_free(gctx
->gen_group
);
896 gctx
->gen_group
= group
;
900 OSSL_PARAM_BLD_free_params(params
);
901 OSSL_PARAM_BLD_free(bld
);
905 static const OSSL_PARAM
*ec_gen_settable_params(void *provctx
)
907 static OSSL_PARAM settable
[] = {
908 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
909 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
910 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
911 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE
, NULL
, 0),
912 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P
, NULL
, 0),
913 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A
, NULL
, 0),
914 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B
, NULL
, 0),
915 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR
, NULL
, 0),
916 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER
, NULL
, 0),
917 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR
, NULL
, 0),
918 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
925 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
928 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
931 return EC_KEY_set_group(ec
, group
) > 0;
935 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
937 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
939 struct ec_gen_ctx
*gctx
= genctx
;
943 if (!ossl_prov_is_running()
945 || (ec
= EC_KEY_new_with_libctx(gctx
->libctx
, NULL
)) == NULL
)
948 if (gctx
->gen_group
== NULL
) {
949 if (!ec_gen_set_group_from_params(gctx
))
952 if (gctx
->encoding
) {
953 int flags
= ec_encoding_name2id(gctx
->encoding
);
956 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
960 /* We must always assign a group, no matter what */
961 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
963 /* Whether you want it or not, you get a keypair, not just one half */
964 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
965 ret
= ret
&& EC_KEY_generate_key(ec
);
967 if (gctx
->ecdh_mode
!= -1)
968 ret
= ret
&& ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
973 /* Something went wrong, throw the key away */
978 static void ec_gen_cleanup(void *genctx
)
980 struct ec_gen_ctx
*gctx
= genctx
;
985 EC_GROUP_free(gctx
->gen_group
);
989 BN_free(gctx
->order
);
990 BN_free(gctx
->cofactor
);
991 OPENSSL_free(gctx
->group_name
);
992 OPENSSL_free(gctx
->field_type
);;
993 OPENSSL_free(gctx
->encoding
);
994 OPENSSL_free(gctx
->seed
);
995 OPENSSL_free(gctx
->gen
);
999 void *ec_load(const void *reference
, size_t reference_sz
)
1003 if (ossl_prov_is_running() && reference_sz
== sizeof(ec
)) {
1004 /* The contents of the reference is the address to our object */
1005 ec
= *(EC_KEY
**)reference
;
1006 /* We grabbed, so we detach it */
1007 *(EC_KEY
**)reference
= NULL
;
1013 const OSSL_DISPATCH ec_keymgmt_functions
[] = {
1014 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
1015 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
1016 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1017 (void (*)(void))ec_gen_set_template
},
1018 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1019 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1020 (void (*)(void))ec_gen_settable_params
},
1021 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
1022 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1023 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
1024 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1025 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
1026 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
1027 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1028 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
1029 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1030 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1031 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
1032 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
1033 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1034 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1035 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1036 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1037 (void (*)(void))ec_query_operation_name
},