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_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/ec.h>
20 #include <openssl/objects.h>
21 #include <openssl/params.h>
22 #include "crypto/bn.h"
23 #include "internal/param_build.h"
24 #include "prov/implementations.h"
25 #include "prov/providercommon.h"
27 static OSSL_OP_keymgmt_new_fn ec_newdata;
28 static OSSL_OP_keymgmt_free_fn ec_freedata;
29 static OSSL_OP_keymgmt_get_params_fn ec_get_params;
30 static OSSL_OP_keymgmt_gettable_params_fn ec_gettable_params;
31 static OSSL_OP_keymgmt_set_params_fn ec_set_params;
32 static OSSL_OP_keymgmt_settable_params_fn ec_settable_params;
33 static OSSL_OP_keymgmt_has_fn ec_has;
34 static OSSL_OP_keymgmt_match_fn ec_match;
35 static OSSL_OP_keymgmt_import_fn ec_import;
36 static OSSL_OP_keymgmt_import_types_fn ec_import_types;
37 static OSSL_OP_keymgmt_export_fn ec_export;
38 static OSSL_OP_keymgmt_export_types_fn ec_export_types;
39 static OSSL_OP_keymgmt_query_operation_name_fn ec_query_operation_name;
41 #define EC_POSSIBLE_SELECTIONS \
42 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
45 const char *ec_query_operation_name(int operation_id)
47 switch (operation_id) {
51 case OSSL_OP_SIGNATURE:
52 return deflt_signature;
59 int params_to_domparams(EC_KEY *ec, const OSSL_PARAM params[])
61 const OSSL_PARAM *param_ec_name;
63 char *curve_name = NULL;
69 param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
70 if (param_ec_name == NULL) {
71 /* explicit parameters */
74 * TODO(3.0): should we support explicit parameters curves?
81 if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
83 || (curve_nid = OBJ_sn2nid(curve_name)) == NID_undef)
86 if ((ecg = EC_GROUP_new_by_curve_name(curve_nid)) == NULL)
90 if (!EC_KEY_set_group(ec, ecg))
94 * TODO(3.0): if the group has changed, should we invalidate the private and
101 OPENSSL_free(curve_name);
107 int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
115 ecg = EC_KEY_get0_group(ec);
119 curve_nid = EC_GROUP_get_curve_name(ecg);
121 if (curve_nid == NID_undef) {
122 /* explicit parameters */
125 * TODO(3.0): should we support explicit parameters curves?
130 const char *curve_name = NULL;
132 if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
135 if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
143 * Callers of params_to_key MUST make sure that params_to_domparams has been
146 * This function only imports the bare keypair, domain parameters and other
147 * parameters are imported separately, and domain parameters are required to
151 int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
153 const OSSL_PARAM *param_priv_key, *param_pub_key;
154 BIGNUM *priv_key = NULL;
155 unsigned char *pub_key = NULL;
157 const EC_GROUP *ecg = NULL;
158 EC_POINT *pub_point = NULL;
161 ecg = EC_KEY_get0_group(ec);
166 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
168 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
171 * We want to have at least a public key either way, so we end up
172 * requiring it unconditionally.
174 if (param_pub_key == NULL
175 || !OSSL_PARAM_get_octet_string(param_pub_key,
176 (void **)&pub_key, 0, &pub_key_len)
177 || (pub_point = EC_POINT_new(ecg)) == NULL
178 || !EC_POINT_oct2point(ecg, pub_point,
179 pub_key, pub_key_len, NULL))
182 if (param_priv_key != NULL && include_private) {
187 * Key import/export should never leak the bit length of the secret
190 * For this reason, on export we use padded BIGNUMs with fixed length.
192 * When importing we also should make sure that, even if short lived,
193 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
194 * soon as possible, so that any processing of this BIGNUM might opt for
195 * constant time implementations in the backend.
197 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
198 * to preallocate the BIGNUM internal buffer to a fixed public size big
199 * enough that operations performed during the processing never trigger
200 * a realloc which would leak the size of the scalar through memory
206 * The order of the large prime subgroup of the curve is our choice for
207 * a fixed public size, as that is generally the upper bound for
208 * generating a private key in EC cryptosystems and should fit all valid
211 * For padding on export we just use the bit length of the order
212 * converted to bytes (rounding up).
214 * For preallocating the BIGNUM storage we look at the number of "words"
215 * required for the internal representation of the order, and we
216 * preallocate 2 extra "words" in case any of the subsequent processing
217 * might temporarily overflow the order length.
219 order = EC_GROUP_get0_order(ecg);
220 if (order == NULL || BN_is_zero(order))
223 fixed_top = bn_get_top(order) + 2;
225 if ((priv_key = BN_new()) == NULL)
227 if (bn_wexpand(priv_key, fixed_top) == NULL)
229 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
231 if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
236 && !EC_KEY_set_private_key(ec, priv_key))
239 if (!EC_KEY_set_public_key(ec, pub_point))
245 BN_clear_free(priv_key);
246 OPENSSL_free(pub_key);
247 EC_POINT_free(pub_point);
252 * Callers of key_to_params MUST make sure that domparams_to_params is also
255 * This function only exports the bare keypair, domain parameters and other
256 * parameters are exported separately.
259 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
261 const BIGNUM *priv_key = NULL;
262 const EC_POINT *pub_point = NULL;
263 const EC_GROUP *ecg = NULL;
264 unsigned char *pub_key = NULL;
265 size_t pub_key_len = 0;
271 ecg = EC_KEY_get0_group(eckey);
272 priv_key = EC_KEY_get0_private_key(eckey);
273 pub_point = EC_KEY_get0_public_key(eckey);
275 /* group and public_key must be present, priv_key is optional */
276 if (ecg == NULL || pub_point == NULL)
278 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
279 POINT_CONVERSION_COMPRESSED,
280 &pub_key, NULL)) == 0)
283 if (!ossl_param_bld_push_octet_string(tmpl,
284 OSSL_PKEY_PARAM_PUB_KEY,
285 pub_key, pub_key_len))
288 if (priv_key != NULL && include_private) {
293 * Key import/export should never leak the bit length of the secret
296 * For this reason, on export we use padded BIGNUMs with fixed length.
298 * When importing we also should make sure that, even if short lived,
299 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
300 * soon as possible, so that any processing of this BIGNUM might opt for
301 * constant time implementations in the backend.
303 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
304 * to preallocate the BIGNUM internal buffer to a fixed public size big
305 * enough that operations performed during the processing never trigger
306 * a realloc which would leak the size of the scalar through memory
312 * The order of the large prime subgroup of the curve is our choice for
313 * a fixed public size, as that is generally the upper bound for
314 * generating a private key in EC cryptosystems and should fit all valid
317 * For padding on export we just use the bit length of the order
318 * converted to bytes (rounding up).
320 * For preallocating the BIGNUM storage we look at the number of "words"
321 * required for the internal representation of the order, and we
322 * preallocate 2 extra "words" in case any of the subsequent processing
323 * might temporarily overflow the order length.
325 ecbits = EC_GROUP_order_bits(ecg);
328 sz = (ecbits + 7 ) / 8;
329 if (!ossl_param_bld_push_BN_pad(tmpl,
330 OSSL_PKEY_PARAM_PRIV_KEY,
338 OPENSSL_free(pub_key);
343 int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
345 const EC_GROUP *ecg = EC_KEY_get0_group(ec);
346 const BIGNUM *cofactor;
349 if (!OSSL_PARAM_get_int(p, &mode))
353 * mode can be only 0 for disable, or 1 for enable here.
355 * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
356 * also supports mode == -1 with the meaning of "reset to the default for
357 * the associated key".
359 if (mode < 0 || mode > 1)
362 if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
365 /* ECDH cofactor mode has no effect if cofactor is 1 */
366 if (BN_is_one(cofactor))
370 EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
372 EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
378 int params_to_otherparams(EC_KEY *ec, const OSSL_PARAM params[])
385 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
386 if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))
393 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
395 int ecdh_cofactor_mode = 0;
401 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
402 if (!ossl_param_bld_push_int(tmpl,
403 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
411 void *ec_newdata(void *provctx)
417 void ec_freedata(void *keydata)
419 EC_KEY_free(keydata);
423 int ec_has(void *keydata, int selection)
425 EC_KEY *ec = keydata;
428 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
431 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
432 ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
433 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
434 ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
435 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
436 ok = ok && (EC_KEY_get0_group(ec) != NULL);
438 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be available,
439 * so no extra check is needed other than the previous one against
440 * EC_POSSIBLE_SELECTIONS.
446 static int ec_match(const void *keydata1, const void *keydata2, int selection)
448 const EC_KEY *ec1 = keydata1;
449 const EC_KEY *ec2 = keydata2;
450 const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
451 const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
454 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
455 ok = ok && group_a != NULL && group_b != NULL
456 && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
457 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
458 const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
459 const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
461 ok = ok && BN_cmp(pa, pb) == 0;
463 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
464 const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
465 const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
467 ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
473 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
475 EC_KEY *ec = keydata;
482 * In this implementation, we can export/import only keydata in the
483 * following combinations:
484 * - domain parameters only
485 * - public key with associated domain parameters (+optional other params)
486 * - private key with associated public key and domain parameters
487 * (+optional other params)
490 * - domain parameters must always be requested
491 * - private key must be requested alongside public key
492 * - other parameters must be requested only alongside a key
494 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
496 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
497 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
499 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
500 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
503 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
504 ok = ok && params_to_domparams(ec, params);
505 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
506 int include_private =
507 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
509 ok = ok && params_to_key(ec, params, include_private);
511 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
512 ok = ok && params_to_otherparams(ec, params);
518 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
521 EC_KEY *ec = keydata;
523 OSSL_PARAM *params = NULL;
530 * In this implementation, we can export/import only keydata in the
531 * following combinations:
532 * - domain parameters only
533 * - public key with associated domain parameters (+optional other params)
534 * - private key with associated public key and domain parameters
535 * (+optional other params)
538 * - domain parameters must always be requested
539 * - private key must be requested alongside public key
540 * - other parameters must be requested only alongside a key
542 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
544 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
545 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
547 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
548 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
551 ossl_param_bld_init(&tmpl);
553 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
554 ok = ok && domparams_to_params(ec, &tmpl);
555 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
556 int include_private =
557 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
559 ok = ok && key_to_params(ec, &tmpl, include_private);
561 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
562 ok = ok && otherparams_to_params(ec, &tmpl);
565 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
568 ok = param_cb(params, cbarg);
569 ossl_param_bld_free(params);
573 /* IMEXPORT = IMPORT + EXPORT */
575 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
576 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_NAME, NULL, 0)
577 # define EC_IMEXPORTABLE_PUBLIC_KEY \
578 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
579 # define EC_IMEXPORTABLE_PRIVATE_KEY \
580 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
581 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
582 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
585 * Include all the possible combinations of OSSL_PARAM arrays for
586 * ec_imexport_types().
588 * They are in a separate file as it is ~100 lines of unreadable and
589 * uninteresting machine generated stuff.
591 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
592 * combinations of the bits in `selection`, it also includes combinations that
593 * are not really useful: we might want to consider alternatives to this
596 #include "ec_kmgmt_imexport.inc"
599 const OSSL_PARAM *ec_imexport_types(int selection)
603 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
605 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
607 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
609 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
611 return ec_types[type_select];
615 const OSSL_PARAM *ec_import_types(int selection)
617 return ec_imexport_types(selection);
621 const OSSL_PARAM *ec_export_types(int selection)
623 return ec_imexport_types(selection);
627 int ec_get_params(void *key, OSSL_PARAM params[])
630 const EC_GROUP *ecg = NULL;
633 ecg = EC_KEY_get0_group(eck);
637 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
638 && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
640 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
641 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
643 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
644 int ecbits, sec_bits;
646 ecbits = EC_GROUP_order_bits(ecg);
649 * The following estimates are based on the values published
650 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
651 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
653 * Note that the above reference explicitly categorizes algorithms in a
654 * discrete set of values {80, 112, 128, 192, 256}, and that it is
655 * relevant only for NIST approved Elliptic Curves, while OpenSSL
656 * applies the same logic also to other curves.
658 * Classifications produced by other standardazing bodies might differ,
659 * so the results provided for "bits of security" by this provider are
660 * to be considered merely indicative, and it is the users'
661 * responsibility to compare these values against the normative
662 * references that may be relevant for their intent and purposes.
666 else if (ecbits >= 384)
668 else if (ecbits >= 256)
670 else if (ecbits >= 224)
672 else if (ecbits >= 160)
675 sec_bits = ecbits / 2;
677 if (!OSSL_PARAM_set_int(p, sec_bits))
681 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
683 int ecdh_cofactor_mode = 0;
686 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
688 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
695 static const OSSL_PARAM ec_known_gettable_params[] = {
696 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
697 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
698 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
699 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
704 const OSSL_PARAM *ec_gettable_params(void)
706 return ec_known_gettable_params;
709 static const OSSL_PARAM ec_known_settable_params[] = {
710 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
715 const OSSL_PARAM *ec_settable_params(void)
717 return ec_known_settable_params;
721 int ec_set_params(void *key, const OSSL_PARAM params[])
726 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
727 if (p != NULL && !ec_set_param_ecdh_cofactor_mode(eck, p))
733 const OSSL_DISPATCH ec_keymgmt_functions[] = {
734 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
735 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
736 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
737 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
738 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
739 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
740 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
741 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
742 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
743 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
744 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
745 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
746 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
747 (void (*)(void))ec_query_operation_name },