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 <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/err.h>
20 #include <openssl/objects.h>
21 #include "crypto/bn.h"
22 #include "crypto/ec.h"
23 #include "prov/implementations.h"
24 #include "prov/providercommon.h"
25 #include "prov/providercommonerr.h"
26 #include "prov/provider_ctx.h"
27 #include "internal/param_build_set.h"
29 static OSSL_FUNC_keymgmt_new_fn ec_newdata
;
30 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init
;
31 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template
;
32 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params
;
33 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params
;
34 static OSSL_FUNC_keymgmt_gen_fn ec_gen
;
35 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup
;
36 static OSSL_FUNC_keymgmt_load_fn ec_load
;
37 static OSSL_FUNC_keymgmt_free_fn ec_freedata
;
38 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params
;
39 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params
;
40 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params
;
41 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params
;
42 static OSSL_FUNC_keymgmt_has_fn ec_has
;
43 static OSSL_FUNC_keymgmt_match_fn ec_match
;
44 static OSSL_FUNC_keymgmt_validate_fn ec_validate
;
45 static OSSL_FUNC_keymgmt_import_fn ec_import
;
46 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types
;
47 static OSSL_FUNC_keymgmt_export_fn ec_export
;
48 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types
;
49 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name
;
51 #define EC_DEFAULT_MD "SHA256"
52 #define EC_POSSIBLE_SELECTIONS \
53 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
56 const char *ec_query_operation_name(int operation_id
)
58 switch (operation_id
) {
61 case OSSL_OP_SIGNATURE
:
68 int domparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
77 ecg
= EC_KEY_get0_group(ec
);
81 curve_nid
= EC_GROUP_get_curve_name(ecg
);
83 if (curve_nid
== NID_undef
) {
84 /* TODO(3.0): should we support explicit parameters curves? */
88 const char *curve_name
= NULL
;
90 if ((curve_name
= ec_curve_nid2name(curve_nid
)) == NULL
)
92 if (!ossl_param_build_set_utf8_string(tmpl
, params
,
93 OSSL_PKEY_PARAM_GROUP_NAME
,
103 * Callers of key_to_params MUST make sure that domparams_to_params is also
106 * This function only exports the bare keypair, domain parameters and other
107 * parameters are exported separately.
110 int key_to_params(const EC_KEY
*eckey
, OSSL_PARAM_BLD
*tmpl
,
111 OSSL_PARAM params
[], int include_private
,
112 unsigned char **pub_key
)
114 BIGNUM
*x
= NULL
, *y
= NULL
;
115 const BIGNUM
*priv_key
= NULL
;
116 const EC_POINT
*pub_point
= NULL
;
117 const EC_GROUP
*ecg
= NULL
;
118 size_t pub_key_len
= 0;
120 BN_CTX
*bnctx
= NULL
;
123 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
126 priv_key
= EC_KEY_get0_private_key(eckey
);
127 pub_point
= EC_KEY_get0_public_key(eckey
);
129 if (pub_point
!= NULL
) {
130 OSSL_PARAM
*p
= NULL
, *px
= NULL
, *py
= NULL
;
132 * EC_POINT_point2buf() can generate random numbers in some
133 * implementations so we need to ensure we use the correct libctx.
135 bnctx
= BN_CTX_new_ex(ec_key_get_libctx(eckey
));
140 /* If we are doing a get then check first before decoding the point */
142 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_PUB_KEY
);
143 px
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_X
);
144 py
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_EC_PUB_Y
);
147 if (p
!= NULL
|| tmpl
!= NULL
) {
148 /* convert pub_point to a octet string according to the SECG standard */
149 if ((pub_key_len
= EC_POINT_point2buf(ecg
, pub_point
,
150 POINT_CONVERSION_COMPRESSED
,
151 pub_key
, bnctx
)) == 0
152 || !ossl_param_build_set_octet_string(tmpl
, p
,
153 OSSL_PKEY_PARAM_PUB_KEY
,
154 *pub_key
, pub_key_len
))
157 if (px
!= NULL
|| py
!= NULL
) {
159 x
= BN_CTX_get(bnctx
);
161 y
= BN_CTX_get(bnctx
);
163 if (!EC_POINT_get_affine_coordinates(ecg
, pub_point
, x
, y
, bnctx
))
166 && !ossl_param_build_set_bn(tmpl
, px
,
167 OSSL_PKEY_PARAM_EC_PUB_X
, x
))
170 && !ossl_param_build_set_bn(tmpl
, py
,
171 OSSL_PKEY_PARAM_EC_PUB_Y
, y
))
176 if (priv_key
!= NULL
&& include_private
) {
181 * Key import/export should never leak the bit length of the secret
184 * For this reason, on export we use padded BIGNUMs with fixed length.
186 * When importing we also should make sure that, even if short lived,
187 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
188 * soon as possible, so that any processing of this BIGNUM might opt for
189 * constant time implementations in the backend.
191 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
192 * to preallocate the BIGNUM internal buffer to a fixed public size big
193 * enough that operations performed during the processing never trigger
194 * a realloc which would leak the size of the scalar through memory
200 * The order of the large prime subgroup of the curve is our choice for
201 * a fixed public size, as that is generally the upper bound for
202 * generating a private key in EC cryptosystems and should fit all valid
205 * For padding on export we just use the bit length of the order
206 * converted to bytes (rounding up).
208 * For preallocating the BIGNUM storage we look at the number of "words"
209 * required for the internal representation of the order, and we
210 * preallocate 2 extra "words" in case any of the subsequent processing
211 * might temporarily overflow the order length.
213 ecbits
= EC_GROUP_order_bits(ecg
);
216 sz
= (ecbits
+ 7 ) / 8;
218 if (!ossl_param_build_set_bn_pad(tmpl
, params
,
219 OSSL_PKEY_PARAM_PRIV_KEY
,
230 int otherparams_to_params(const EC_KEY
*ec
, OSSL_PARAM_BLD
*tmpl
,
233 int ecdh_cofactor_mode
= 0;
239 (EC_KEY_get_flags(ec
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
240 return ossl_param_build_set_int(tmpl
, params
,
241 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
246 void *ec_newdata(void *provctx
)
248 return EC_KEY_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx
), NULL
);
252 void ec_freedata(void *keydata
)
254 EC_KEY_free(keydata
);
258 int ec_has(void *keydata
, int selection
)
260 EC_KEY
*ec
= keydata
;
264 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
267 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
268 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
269 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
270 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
271 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
272 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
274 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
275 * available, so no extra check is needed other than the previous one
276 * against EC_POSSIBLE_SELECTIONS.
282 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
284 const EC_KEY
*ec1
= keydata1
;
285 const EC_KEY
*ec2
= keydata2
;
286 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
287 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
290 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
291 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
292 && EC_GROUP_cmp(group_a
, group_b
, NULL
) == 0;
293 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
294 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
295 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
297 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
299 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
300 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
301 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
303 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, NULL
) == 0;
309 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
311 EC_KEY
*ec
= keydata
;
318 * In this implementation, we can export/import only keydata in the
319 * following combinations:
320 * - domain parameters only
321 * - public key with associated domain parameters (+optional other params)
322 * - private key with associated public key and domain parameters
323 * (+optional other params)
326 * - domain parameters must always be requested
327 * - private key must be requested alongside public key
328 * - other parameters must be requested only alongside a key
330 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
332 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
333 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
335 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0
336 && (selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
339 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
340 ok
= ok
&& ec_key_domparams_fromdata(ec
, params
);
341 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
342 int include_private
=
343 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
345 ok
= ok
&& ec_key_fromdata(ec
, params
, include_private
);
347 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
348 ok
= ok
&& ec_key_otherparams_fromdata(ec
, params
);
354 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
357 EC_KEY
*ec
= keydata
;
358 OSSL_PARAM_BLD
*tmpl
;
359 OSSL_PARAM
*params
= NULL
;
360 unsigned char *pub_key
= NULL
;
367 * In this implementation, we can export/import only keydata in the
368 * following combinations:
369 * - domain parameters only
370 * - public key with associated domain parameters (+optional other params)
371 * - private key with associated public key and domain parameters
372 * (+optional other params)
375 * - domain parameters must always be requested
376 * - private key must be requested alongside public key
377 * - other parameters must be requested only alongside a key
379 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
381 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
382 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
384 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0
385 && (selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
388 tmpl
= OSSL_PARAM_BLD_new();
392 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
393 ok
= ok
&& domparams_to_params(ec
, tmpl
, NULL
);
395 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
396 int include_private
=
397 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
399 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
401 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
402 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
404 if (ok
&& (params
= OSSL_PARAM_BLD_to_param(tmpl
)) != NULL
)
405 ok
= param_cb(params
, cbarg
);
407 OSSL_PARAM_BLD_free_params(params
);
408 OSSL_PARAM_BLD_free(tmpl
);
409 OPENSSL_free(pub_key
);
413 /* IMEXPORT = IMPORT + EXPORT */
415 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
416 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
417 # define EC_IMEXPORTABLE_PUBLIC_KEY \
418 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
419 # define EC_IMEXPORTABLE_PRIVATE_KEY \
420 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
421 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
422 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
425 * Include all the possible combinations of OSSL_PARAM arrays for
426 * ec_imexport_types().
428 * They are in a separate file as it is ~100 lines of unreadable and
429 * uninteresting machine generated stuff.
431 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
432 * combinations of the bits in `selection`, it also includes combinations that
433 * are not really useful: we might want to consider alternatives to this
436 #include "ec_kmgmt_imexport.inc"
439 const OSSL_PARAM
*ec_imexport_types(int selection
)
443 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
445 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
447 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
449 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
451 return ec_types
[type_select
];
455 const OSSL_PARAM
*ec_import_types(int selection
)
457 return ec_imexport_types(selection
);
461 const OSSL_PARAM
*ec_export_types(int selection
)
463 return ec_imexport_types(selection
);
467 int ec_get_params(void *key
, OSSL_PARAM params
[])
471 const EC_GROUP
*ecg
= NULL
;
473 unsigned char *pub_key
= NULL
;
475 ecg
= EC_KEY_get0_group(eck
);
479 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
480 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
482 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
483 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
485 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
486 int ecbits
, sec_bits
;
488 ecbits
= EC_GROUP_order_bits(ecg
);
491 * The following estimates are based on the values published
492 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
493 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
495 * Note that the above reference explicitly categorizes algorithms in a
496 * discrete set of values {80, 112, 128, 192, 256}, and that it is
497 * relevant only for NIST approved Elliptic Curves, while OpenSSL
498 * applies the same logic also to other curves.
500 * Classifications produced by other standardazing bodies might differ,
501 * so the results provided for "bits of security" by this provider are
502 * to be considered merely indicative, and it is the users'
503 * responsibility to compare these values against the normative
504 * references that may be relevant for their intent and purposes.
508 else if (ecbits
>= 384)
510 else if (ecbits
>= 256)
512 else if (ecbits
>= 224)
514 else if (ecbits
>= 160)
517 sec_bits
= ecbits
/ 2;
519 if (!OSSL_PARAM_set_int(p
, sec_bits
))
523 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
524 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
527 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
529 int ecdh_cofactor_mode
= 0;
532 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
534 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
537 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
)) != NULL
) {
538 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(key
));
542 p
->return_size
= EC_POINT_point2oct(EC_KEY_get0_group(key
),
543 EC_KEY_get0_public_key(key
),
544 POINT_CONVERSION_UNCOMPRESSED
,
545 p
->data
, p
->return_size
, ctx
);
547 if (p
->return_size
== 0)
551 ret
= domparams_to_params(eck
, NULL
, params
)
552 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
553 && otherparams_to_params(eck
, NULL
, params
);
554 OPENSSL_free(pub_key
);
558 static const OSSL_PARAM ec_known_gettable_params
[] = {
559 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
560 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
561 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
562 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
563 EC_IMEXPORTABLE_DOM_PARAMETERS
,
564 EC_IMEXPORTABLE_PUBLIC_KEY
,
565 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
566 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
567 EC_IMEXPORTABLE_PRIVATE_KEY
,
568 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
573 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
575 return ec_known_gettable_params
;
578 static const OSSL_PARAM ec_known_settable_params
[] = {
579 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
580 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
585 const OSSL_PARAM
*ec_settable_params(void *provctx
)
587 return ec_known_settable_params
;
591 int ec_set_params(void *key
, const OSSL_PARAM params
[])
596 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
);
598 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(key
));
602 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
603 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
610 return ec_key_otherparams_fromdata(eck
, params
);
614 int ec_validate(void *keydata
, int selection
)
616 EC_KEY
*eck
= keydata
;
618 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(eck
));
623 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
626 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
627 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
629 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
630 ok
= ok
&& ec_key_public_check(eck
, ctx
);
632 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
633 ok
= ok
&& ec_key_private_check(eck
);
635 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
636 ok
= ok
&& ec_key_pairwise_check(eck
, ctx
);
649 static void *ec_gen_init(void *provctx
, int selection
)
651 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
652 struct ec_gen_ctx
*gctx
= NULL
;
654 if ((selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
657 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
658 gctx
->libctx
= libctx
;
659 gctx
->gen_group
= NULL
;
660 gctx
->selection
= selection
;
666 static int ec_gen_set_group(void *genctx
, int nid
)
668 struct ec_gen_ctx
*gctx
= genctx
;
671 group
= EC_GROUP_new_by_curve_name_with_libctx(gctx
->libctx
, NULL
, nid
);
673 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
676 EC_GROUP_free(gctx
->gen_group
);
677 gctx
->gen_group
= group
;
680 static int ec_gen_set_template(void *genctx
, void *templ
)
682 struct ec_gen_ctx
*gctx
= genctx
;
684 const EC_GROUP
*ec_group
;
686 if (gctx
== NULL
|| ec
== NULL
)
688 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
690 return ec_gen_set_group(gctx
, EC_GROUP_get_curve_name(ec_group
));
693 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
695 struct ec_gen_ctx
*gctx
= genctx
;
698 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
))
700 if (!OSSL_PARAM_get_int(p
, &gctx
->ecdh_mode
))
703 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
))
705 const char *curve_name
= NULL
;
708 switch (p
->data_type
) {
709 case OSSL_PARAM_UTF8_STRING
:
710 /* The OSSL_PARAM functions have no support for this */
711 curve_name
= p
->data
;
712 ret
= (curve_name
!= NULL
);
714 case OSSL_PARAM_UTF8_PTR
:
715 ret
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
720 int nid
= ec_curve_name2nid(curve_name
);
722 if (nid
== NID_undef
) {
723 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
726 ret
= ec_gen_set_group(gctx
, nid
);
734 static const OSSL_PARAM
*ec_gen_settable_params(void *provctx
)
736 static OSSL_PARAM settable
[] = {
737 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
738 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
745 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
748 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
751 return EC_KEY_set_group(ec
, group
) > 0;
755 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
757 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
759 struct ec_gen_ctx
*gctx
= genctx
;
761 int ret
= 1; /* Start optimistically */
764 || (ec
= EC_KEY_new_with_libctx(gctx
->libctx
, NULL
)) == NULL
)
767 /* We must always assign a group, no matter what */
768 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
769 /* Whether you want it or not, you get a keypair, not just one half */
770 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
771 ret
= ret
&& EC_KEY_generate_key(ec
);
773 if (gctx
->ecdh_mode
!= -1)
774 ret
= ret
&& ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
779 /* Something went wrong, throw the key away */
784 static void ec_gen_cleanup(void *genctx
)
786 struct ec_gen_ctx
*gctx
= genctx
;
791 EC_GROUP_free(gctx
->gen_group
);
795 void *ec_load(const void *reference
, size_t reference_sz
)
799 if (reference_sz
== sizeof(ec
)) {
800 /* The contents of the reference is the address to our object */
801 ec
= *(EC_KEY
**)reference
;
802 /* We grabbed, so we detach it */
803 *(EC_KEY
**)reference
= NULL
;
809 const OSSL_DISPATCH ec_keymgmt_functions
[] = {
810 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
811 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
812 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
813 (void (*)(void))ec_gen_set_template
},
814 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
815 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
816 (void (*)(void))ec_gen_settable_params
},
817 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
818 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
819 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
820 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
821 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
822 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
823 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
824 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
825 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
826 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
827 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
828 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
829 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
830 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
831 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
832 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
833 (void (*)(void))ec_query_operation_name
},