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 return EC_KEY_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx
), NULL
);
219 void ec_freedata(void *keydata
)
221 EC_KEY_free(keydata
);
225 int ec_has(void *keydata
, int selection
)
227 EC_KEY
*ec
= keydata
;
231 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
234 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
235 ok
= ok
&& (EC_KEY_get0_public_key(ec
) != NULL
);
236 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
237 ok
= ok
&& (EC_KEY_get0_private_key(ec
) != NULL
);
238 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
239 ok
= ok
&& (EC_KEY_get0_group(ec
) != NULL
);
241 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
242 * available, so no extra check is needed other than the previous one
243 * against EC_POSSIBLE_SELECTIONS.
249 static int ec_match(const void *keydata1
, const void *keydata2
, int selection
)
251 const EC_KEY
*ec1
= keydata1
;
252 const EC_KEY
*ec2
= keydata2
;
253 const EC_GROUP
*group_a
= EC_KEY_get0_group(ec1
);
254 const EC_GROUP
*group_b
= EC_KEY_get0_group(ec2
);
255 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(ec1
));
258 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
259 ok
= ok
&& group_a
!= NULL
&& group_b
!= NULL
260 && EC_GROUP_cmp(group_a
, group_b
, ctx
) == 0;
261 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
262 const BIGNUM
*pa
= EC_KEY_get0_private_key(ec1
);
263 const BIGNUM
*pb
= EC_KEY_get0_private_key(ec2
);
265 ok
= ok
&& BN_cmp(pa
, pb
) == 0;
267 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
268 const EC_POINT
*pa
= EC_KEY_get0_public_key(ec1
);
269 const EC_POINT
*pb
= EC_KEY_get0_public_key(ec2
);
271 ok
= ok
&& EC_POINT_cmp(group_b
, pa
, pb
, ctx
) == 0;
278 int ec_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
280 EC_KEY
*ec
= keydata
;
287 * In this implementation, we can export/import only keydata in the
288 * following combinations:
289 * - domain parameters (+optional other params)
290 * - public key with associated domain parameters (+optional other params)
291 * - private key with associated public key and domain parameters
292 * (+optional other params)
295 * - domain parameters must always be requested
296 * - private key must be requested alongside public key
297 * - other parameters are always optional
299 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
301 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
302 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
305 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
306 ok
= ok
&& ec_group_fromdata(ec
, params
);
307 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
308 int include_private
=
309 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
311 ok
= ok
&& ec_key_fromdata(ec
, params
, include_private
);
313 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
314 ok
= ok
&& ec_key_otherparams_fromdata(ec
, params
);
320 int ec_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
323 EC_KEY
*ec
= keydata
;
324 OSSL_PARAM_BLD
*tmpl
;
325 OSSL_PARAM
*params
= NULL
;
326 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
327 BN_CTX
*bnctx
= NULL
;
334 * In this implementation, we can export/import only keydata in the
335 * following combinations:
336 * - domain parameters (+optional other params)
337 * - public key with associated domain parameters (+optional other params)
338 * - private key with associated public key and domain parameters
339 * (+optional other params)
342 * - domain parameters must always be requested
343 * - private key must be requested alongside public key
344 * - other parameters are always optional
346 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) == 0)
348 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
349 && (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) == 0)
351 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0
352 && (selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
355 tmpl
= OSSL_PARAM_BLD_new();
359 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
360 bnctx
= BN_CTX_new_ex(ec_key_get_libctx(ec
));
362 ok
= ok
&& (bnctx
!= NULL
);
363 ok
= ok
&& ec_group_todata(EC_KEY_get0_group(ec
), tmpl
, NULL
,
364 ec_key_get_libctx(ec
), ec_key_get0_propq(ec
),
368 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0) {
369 int include_private
=
370 selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
? 1 : 0;
372 ok
= ok
&& key_to_params(ec
, tmpl
, NULL
, include_private
, &pub_key
);
374 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
375 ok
= ok
&& otherparams_to_params(ec
, tmpl
, NULL
);
377 if (ok
&& (params
= OSSL_PARAM_BLD_to_param(tmpl
)) != NULL
)
378 ok
= param_cb(params
, cbarg
);
380 OSSL_PARAM_BLD_free_params(params
);
381 OSSL_PARAM_BLD_free(tmpl
);
382 OPENSSL_free(pub_key
);
383 OPENSSL_free(genbuf
);
389 /* IMEXPORT = IMPORT + EXPORT */
391 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
392 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
393 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
394 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
395 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
396 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
397 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
398 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
399 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
400 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
401 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
403 # define EC_IMEXPORTABLE_PUBLIC_KEY \
404 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
405 # define EC_IMEXPORTABLE_PRIVATE_KEY \
406 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
407 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
408 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
411 * Include all the possible combinations of OSSL_PARAM arrays for
412 * ec_imexport_types().
414 * They are in a separate file as it is ~100 lines of unreadable and
415 * uninteresting machine generated stuff.
417 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
418 * combinations of the bits in `selection`, it also includes combinations that
419 * are not really useful: we might want to consider alternatives to this
422 #include "ec_kmgmt_imexport.inc"
425 const OSSL_PARAM
*ec_imexport_types(int selection
)
429 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
431 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
433 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
435 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
437 return ec_types
[type_select
];
441 const OSSL_PARAM
*ec_import_types(int selection
)
443 return ec_imexport_types(selection
);
447 const OSSL_PARAM
*ec_export_types(int selection
)
449 return ec_imexport_types(selection
);
452 static int ec_get_ecm_params(const EC_GROUP
*group
, OSSL_PARAM params
[])
454 #ifdef OPENSSL_NO_EC2M
458 unsigned int k1
= 0, k2
= 0, k3
= 0;
460 const char *basis_name
= NULL
;
461 int fid
= EC_GROUP_get_field_type(group
);
463 if (fid
!= NID_X9_62_characteristic_two_field
)
466 basis_nid
= EC_GROUP_get_basis_type(group
);
467 if (basis_nid
== NID_X9_62_tpBasis
)
468 basis_name
= SN_X9_62_tpBasis
;
469 else if (basis_nid
== NID_X9_62_ppBasis
)
470 basis_name
= SN_X9_62_ppBasis
;
474 m
= EC_GROUP_get_degree(group
);
475 if (!ossl_param_build_set_int(NULL
, params
, OSSL_PKEY_PARAM_EC_CHAR2_M
, m
)
476 || !ossl_param_build_set_utf8_string(NULL
, params
,
477 OSSL_PKEY_PARAM_EC_CHAR2_TYPE
,
481 if (basis_nid
== NID_X9_62_tpBasis
) {
482 if (!EC_GROUP_get_trinomial_basis(group
, &k1
)
483 || !ossl_param_build_set_int(NULL
, params
,
484 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS
,
488 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
)
489 || !ossl_param_build_set_int(NULL
, params
,
490 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1
, (int)k1
)
491 || !ossl_param_build_set_int(NULL
, params
,
492 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2
, (int)k2
)
493 || !ossl_param_build_set_int(NULL
, params
,
494 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3
, (int)k3
))
500 #endif /* OPENSSL_NO_EC2M */
504 int ec_get_params(void *key
, OSSL_PARAM params
[])
508 const EC_GROUP
*ecg
= NULL
;
510 unsigned char *pub_key
= NULL
, *genbuf
= NULL
;
513 BN_CTX
*bnctx
= NULL
;
515 ecg
= EC_KEY_get0_group(eck
);
519 libctx
= ec_key_get_libctx(eck
);
520 propq
= ec_key_get0_propq(eck
);
522 bnctx
= BN_CTX_new_ex(libctx
);
527 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
528 && !OSSL_PARAM_set_int(p
, ECDSA_size(eck
)))
530 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
531 && !OSSL_PARAM_set_int(p
, EC_GROUP_order_bits(ecg
)))
533 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
) {
534 int ecbits
, sec_bits
;
536 ecbits
= EC_GROUP_order_bits(ecg
);
539 * The following estimates are based on the values published
540 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
541 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
543 * Note that the above reference explicitly categorizes algorithms in a
544 * discrete set of values {80, 112, 128, 192, 256}, and that it is
545 * relevant only for NIST approved Elliptic Curves, while OpenSSL
546 * applies the same logic also to other curves.
548 * Classifications produced by other standardazing bodies might differ,
549 * so the results provided for "bits of security" by this provider are
550 * to be considered merely indicative, and it is the users'
551 * responsibility to compare these values against the normative
552 * references that may be relevant for their intent and purposes.
556 else if (ecbits
>= 384)
558 else if (ecbits
>= 256)
560 else if (ecbits
>= 224)
562 else if (ecbits
>= 160)
565 sec_bits
= ecbits
/ 2;
567 if (!OSSL_PARAM_set_int(p
, sec_bits
))
571 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
572 && !OSSL_PARAM_set_utf8_string(p
, EC_DEFAULT_MD
))
575 p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
);
577 int ecdh_cofactor_mode
= 0;
580 (EC_KEY_get_flags(eck
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
582 if (!OSSL_PARAM_set_int(p
, ecdh_cofactor_mode
))
585 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
)) != NULL
) {
586 p
->return_size
= EC_POINT_point2oct(EC_KEY_get0_group(key
),
587 EC_KEY_get0_public_key(key
),
588 POINT_CONVERSION_UNCOMPRESSED
,
589 p
->data
, p
->return_size
, bnctx
);
590 if (p
->return_size
== 0)
594 ret
= ec_get_ecm_params(ecg
, params
)
595 && ec_group_todata(ecg
, NULL
, params
, libctx
, propq
, bnctx
, &genbuf
)
596 && key_to_params(eck
, NULL
, params
, 1, &pub_key
)
597 && otherparams_to_params(eck
, NULL
, params
);
599 OPENSSL_free(genbuf
);
600 OPENSSL_free(pub_key
);
606 #ifndef OPENSSL_NO_EC2M
607 # define EC2M_GETTABLE_DOM_PARAMS \
608 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
609 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
610 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
611 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
612 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
613 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
615 # define EC2M_GETTABLE_DOM_PARAMS
618 static const OSSL_PARAM ec_known_gettable_params
[] = {
619 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
620 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
621 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
622 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
623 EC_IMEXPORTABLE_DOM_PARAMETERS
,
624 EC2M_GETTABLE_DOM_PARAMS
625 EC_IMEXPORTABLE_PUBLIC_KEY
,
626 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X
, NULL
, 0),
627 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y
, NULL
, 0),
628 EC_IMEXPORTABLE_PRIVATE_KEY
,
629 EC_IMEXPORTABLE_OTHER_PARAMETERS
,
634 const OSSL_PARAM
*ec_gettable_params(void *provctx
)
636 return ec_known_gettable_params
;
639 static const OSSL_PARAM ec_known_settable_params
[] = {
640 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
641 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT
, NULL
, 0),
646 const OSSL_PARAM
*ec_settable_params(void *provctx
)
648 return ec_known_settable_params
;
652 int ec_set_params(void *key
, const OSSL_PARAM params
[])
657 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_TLS_ENCODED_PT
);
659 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(key
));
663 || p
->data_type
!= OSSL_PARAM_OCTET_STRING
664 || !EC_KEY_oct2key(key
, p
->data
, p
->data_size
, ctx
))
671 return ec_key_otherparams_fromdata(eck
, params
);
675 int ec_validate(void *keydata
, int selection
)
677 EC_KEY
*eck
= keydata
;
679 BN_CTX
*ctx
= BN_CTX_new_ex(ec_key_get_libctx(eck
));
684 if ((selection
& EC_POSSIBLE_SELECTIONS
) != 0)
687 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
688 ok
= ok
&& EC_GROUP_check(EC_KEY_get0_group(eck
), ctx
);
690 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
691 ok
= ok
&& ec_key_public_check(eck
, ctx
);
693 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
694 ok
= ok
&& ec_key_private_check(eck
);
696 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == OSSL_KEYMGMT_SELECT_KEYPAIR
)
697 ok
= ok
&& ec_key_pairwise_check(eck
, ctx
);
708 BIGNUM
*p
, *a
, *b
, *order
, *cofactor
;
709 unsigned char *gen
, *seed
;
710 size_t gen_len
, seed_len
;
716 static void *ec_gen_init(void *provctx
, int selection
)
718 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
719 struct ec_gen_ctx
*gctx
= NULL
;
721 if ((selection
& (EC_POSSIBLE_SELECTIONS
)) == 0)
724 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
725 gctx
->libctx
= libctx
;
726 gctx
->selection
= selection
;
732 static int ec_gen_set_group(void *genctx
, const EC_GROUP
*src
)
734 struct ec_gen_ctx
*gctx
= genctx
;
737 group
= EC_GROUP_dup(src
);
739 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CURVE
);
742 EC_GROUP_free(gctx
->gen_group
);
743 gctx
->gen_group
= group
;
747 static int ec_gen_set_template(void *genctx
, void *templ
)
749 struct ec_gen_ctx
*gctx
= genctx
;
751 const EC_GROUP
*ec_group
;
753 if (gctx
== NULL
|| ec
== NULL
)
755 if ((ec_group
= EC_KEY_get0_group(ec
)) == NULL
)
757 return ec_gen_set_group(gctx
, ec_group
);
760 #define COPY_INT_PARAM(params, key, val) \
761 p = OSSL_PARAM_locate_const(params, key); \
762 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
765 #define COPY_UTF8_PARAM(params, key, val) \
766 p = OSSL_PARAM_locate_const(params, key); \
768 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
771 val = OPENSSL_strdup(p->data); \
776 #define COPY_OCTET_PARAM(params, key, val, len) \
777 p = OSSL_PARAM_locate_const(params, key); \
779 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
782 len = p->data_size; \
783 val = OPENSSL_memdup(p->data, p->data_size); \
788 #define COPY_BN_PARAM(params, key, bn) \
789 p = OSSL_PARAM_locate_const(params, key); \
793 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
797 static int ec_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
800 struct ec_gen_ctx
*gctx
= genctx
;
802 EC_GROUP
*group
= NULL
;
804 COPY_INT_PARAM(params
, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, gctx
->ecdh_mode
);
806 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_GROUP_NAME
, gctx
->group_name
);
807 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
, gctx
->field_type
);
808 COPY_UTF8_PARAM(params
, OSSL_PKEY_PARAM_EC_ENCODING
, gctx
->encoding
);
810 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
);
811 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
);
812 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
);
813 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
);
814 COPY_BN_PARAM(params
, OSSL_PKEY_PARAM_EC_COFACTOR
, gctx
->cofactor
);
816 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_SEED
, gctx
->seed
, gctx
->seed_len
);
817 COPY_OCTET_PARAM(params
, OSSL_PKEY_PARAM_EC_GENERATOR
, gctx
->gen
,
822 EC_GROUP_free(group
);
826 static int ec_gen_set_group_from_params(struct ec_gen_ctx
*gctx
)
830 OSSL_PARAM
*params
= NULL
;
831 EC_GROUP
*group
= NULL
;
833 bld
= OSSL_PARAM_BLD_new();
837 if (gctx
->encoding
!= NULL
838 && !OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_ENCODING
,
842 if (gctx
->group_name
!= NULL
) {
843 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_GROUP_NAME
,
844 gctx
->group_name
, 0))
846 /* Ignore any other parameters if there is a group name */
848 } else if (gctx
->field_type
!= NULL
) {
849 if (!OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
850 gctx
->field_type
, 0))
858 || gctx
->order
== NULL
859 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, gctx
->p
)
860 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, gctx
->a
)
861 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, gctx
->b
)
862 || !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_ORDER
, gctx
->order
))
865 if (gctx
->cofactor
!= NULL
866 && !OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
870 if (gctx
->seed
!= NULL
871 && !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_SEED
,
872 gctx
->seed
, gctx
->seed_len
))
875 if (gctx
->gen
== NULL
876 || !OSSL_PARAM_BLD_push_octet_string(bld
, OSSL_PKEY_PARAM_EC_GENERATOR
,
877 gctx
->gen
, gctx
->gen_len
))
880 params
= OSSL_PARAM_BLD_to_param(bld
);
883 group
= EC_GROUP_new_from_params(params
, gctx
->libctx
, NULL
);
887 EC_GROUP_free(gctx
->gen_group
);
888 gctx
->gen_group
= group
;
892 OSSL_PARAM_BLD_free_params(params
);
893 OSSL_PARAM_BLD_free(bld
);
897 static const OSSL_PARAM
*ec_gen_settable_params(void *provctx
)
899 static OSSL_PARAM settable
[] = {
900 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
, NULL
, 0),
901 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
, NULL
),
902 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
, NULL
, 0),
903 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE
, NULL
, 0),
904 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P
, NULL
, 0),
905 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A
, NULL
, 0),
906 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B
, NULL
, 0),
907 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR
, NULL
, 0),
908 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER
, NULL
, 0),
909 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR
, NULL
, 0),
910 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED
, NULL
, 0),
917 static int ec_gen_assign_group(EC_KEY
*ec
, EC_GROUP
*group
)
920 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_PARAMETERS_SET
);
923 return EC_KEY_set_group(ec
, group
) > 0;
927 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
929 static void *ec_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
931 struct ec_gen_ctx
*gctx
= genctx
;
936 || (ec
= EC_KEY_new_with_libctx(gctx
->libctx
, NULL
)) == NULL
)
939 if (gctx
->gen_group
== NULL
) {
940 if (!ec_gen_set_group_from_params(gctx
))
943 if (gctx
->encoding
) {
944 int flags
= ec_encoding_name2id(gctx
->encoding
);
947 EC_GROUP_set_asn1_flag(gctx
->gen_group
, flags
);
951 /* We must always assign a group, no matter what */
952 ret
= ec_gen_assign_group(ec
, gctx
->gen_group
);
954 /* Whether you want it or not, you get a keypair, not just one half */
955 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
956 ret
= ret
&& EC_KEY_generate_key(ec
);
958 if (gctx
->ecdh_mode
!= -1)
959 ret
= ret
&& ec_set_ecdh_cofactor_mode(ec
, gctx
->ecdh_mode
);
964 /* Something went wrong, throw the key away */
969 static void ec_gen_cleanup(void *genctx
)
971 struct ec_gen_ctx
*gctx
= genctx
;
976 EC_GROUP_free(gctx
->gen_group
);
980 BN_free(gctx
->order
);
981 BN_free(gctx
->cofactor
);
982 OPENSSL_free(gctx
->group_name
);
983 OPENSSL_free(gctx
->field_type
);;
984 OPENSSL_free(gctx
->encoding
);
985 OPENSSL_free(gctx
->seed
);
986 OPENSSL_free(gctx
->gen
);
990 void *ec_load(const void *reference
, size_t reference_sz
)
994 if (reference_sz
== sizeof(ec
)) {
995 /* The contents of the reference is the address to our object */
996 ec
= *(EC_KEY
**)reference
;
997 /* We grabbed, so we detach it */
998 *(EC_KEY
**)reference
= NULL
;
1004 const OSSL_DISPATCH ec_keymgmt_functions
[] = {
1005 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))ec_newdata
},
1006 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))ec_gen_init
},
1007 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
,
1008 (void (*)(void))ec_gen_set_template
},
1009 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))ec_gen_set_params
},
1010 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
1011 (void (*)(void))ec_gen_settable_params
},
1012 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))ec_gen
},
1013 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))ec_gen_cleanup
},
1014 { OSSL_FUNC_KEYMGMT_LOAD
, (void (*)(void))ec_load
},
1015 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))ec_freedata
},
1016 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))ec_get_params
},
1017 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))ec_gettable_params
},
1018 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))ec_set_params
},
1019 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))ec_settable_params
},
1020 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))ec_has
},
1021 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))ec_match
},
1022 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))ec_validate
},
1023 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))ec_import
},
1024 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))ec_import_types
},
1025 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))ec_export
},
1026 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))ec_export_types
},
1027 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
1028 (void (*)(void))ec_query_operation_name
},