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/objects.h>
20 #include <openssl/params.h>
21 #include "crypto/bn.h"
22 #include "crypto/ec.h"
23 #include "openssl/param_build.h"
24 #include "prov/implementations.h"
25 #include "prov/providercommon.h"
26 #include "prov/provider_ctx.h"
28 static OSSL_OP_keymgmt_new_fn ec_newdata;
29 static OSSL_OP_keymgmt_free_fn ec_freedata;
30 static OSSL_OP_keymgmt_get_params_fn ec_get_params;
31 static OSSL_OP_keymgmt_gettable_params_fn ec_gettable_params;
32 static OSSL_OP_keymgmt_set_params_fn ec_set_params;
33 static OSSL_OP_keymgmt_settable_params_fn ec_settable_params;
34 static OSSL_OP_keymgmt_has_fn ec_has;
35 static OSSL_OP_keymgmt_match_fn ec_match;
36 static OSSL_OP_keymgmt_validate_fn ec_validate;
37 static OSSL_OP_keymgmt_import_fn ec_import;
38 static OSSL_OP_keymgmt_import_types_fn ec_import_types;
39 static OSSL_OP_keymgmt_export_fn ec_export;
40 static OSSL_OP_keymgmt_export_types_fn ec_export_types;
41 static OSSL_OP_keymgmt_query_operation_name_fn ec_query_operation_name;
43 #define EC_POSSIBLE_SELECTIONS \
44 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
47 const char *ec_query_operation_name(int operation_id)
49 switch (operation_id) {
52 case OSSL_OP_SIGNATURE:
59 int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
67 ecg = EC_KEY_get0_group(ec);
71 curve_nid = EC_GROUP_get_curve_name(ecg);
73 if (curve_nid == NID_undef) {
74 /* explicit parameters */
77 * TODO(3.0): should we support explicit parameters curves?
82 const char *curve_name = NULL;
84 if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
87 if (!OSSL_PARAM_BLD_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME,
96 * Callers of key_to_params MUST make sure that domparams_to_params is also
99 * This function only exports the bare keypair, domain parameters and other
100 * parameters are exported separately.
103 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
105 const BIGNUM *priv_key = NULL;
106 const EC_POINT *pub_point = NULL;
107 const EC_GROUP *ecg = NULL;
108 unsigned char *pub_key = NULL;
109 size_t pub_key_len = 0;
113 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
116 priv_key = EC_KEY_get0_private_key(eckey);
117 pub_point = EC_KEY_get0_public_key(eckey);
119 if (pub_point != NULL) {
120 /* convert pub_point to a octet string according to the SECG standard */
121 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
122 POINT_CONVERSION_COMPRESSED,
123 &pub_key, NULL)) == 0
124 || !OSSL_PARAM_BLD_push_octet_string(tmpl,
125 OSSL_PKEY_PARAM_PUB_KEY,
126 pub_key, pub_key_len))
130 if (priv_key != NULL && include_private) {
135 * Key import/export should never leak the bit length of the secret
138 * For this reason, on export we use padded BIGNUMs with fixed length.
140 * When importing we also should make sure that, even if short lived,
141 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
142 * soon as possible, so that any processing of this BIGNUM might opt for
143 * constant time implementations in the backend.
145 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
146 * to preallocate the BIGNUM internal buffer to a fixed public size big
147 * enough that operations performed during the processing never trigger
148 * a realloc which would leak the size of the scalar through memory
154 * The order of the large prime subgroup of the curve is our choice for
155 * a fixed public size, as that is generally the upper bound for
156 * generating a private key in EC cryptosystems and should fit all valid
159 * For padding on export we just use the bit length of the order
160 * converted to bytes (rounding up).
162 * For preallocating the BIGNUM storage we look at the number of "words"
163 * required for the internal representation of the order, and we
164 * preallocate 2 extra "words" in case any of the subsequent processing
165 * might temporarily overflow the order length.
167 ecbits = EC_GROUP_order_bits(ecg);
170 sz = (ecbits + 7 ) / 8;
171 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
172 OSSL_PKEY_PARAM_PRIV_KEY,
180 OPENSSL_free(pub_key);
185 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
187 int ecdh_cofactor_mode = 0;
193 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
194 if (!OSSL_PARAM_BLD_push_int(tmpl,
195 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
203 void *ec_newdata(void *provctx)
205 return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
209 void ec_freedata(void *keydata)
211 EC_KEY_free(keydata);
215 int ec_has(void *keydata, int selection)
217 EC_KEY *ec = keydata;
221 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
224 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
225 ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
226 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
227 ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
228 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
229 ok = ok && (EC_KEY_get0_group(ec) != NULL);
231 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
232 * available, so no extra check is needed other than the previous one
233 * against EC_POSSIBLE_SELECTIONS.
239 static int ec_match(const void *keydata1, const void *keydata2, int selection)
241 const EC_KEY *ec1 = keydata1;
242 const EC_KEY *ec2 = keydata2;
243 const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
244 const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
247 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
248 ok = ok && group_a != NULL && group_b != NULL
249 && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
250 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
251 const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
252 const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
254 ok = ok && BN_cmp(pa, pb) == 0;
256 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
257 const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
258 const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
260 ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
266 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
268 EC_KEY *ec = keydata;
275 * In this implementation, we can export/import only keydata in the
276 * following combinations:
277 * - domain parameters only
278 * - public key with associated domain parameters (+optional other params)
279 * - private key with associated public key and domain parameters
280 * (+optional other params)
283 * - domain parameters must always be requested
284 * - private key must be requested alongside public key
285 * - other parameters must be requested only alongside a key
287 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
289 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
290 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
292 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
293 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
296 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
297 ok = ok && ec_key_domparams_fromdata(ec, params);
298 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
299 int include_private =
300 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
302 ok = ok && ec_key_fromdata(ec, params, include_private);
304 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
305 ok = ok && ec_key_otherparams_fromdata(ec, params);
311 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
314 EC_KEY *ec = keydata;
315 OSSL_PARAM_BLD *tmpl;
316 OSSL_PARAM *params = NULL;
323 * In this implementation, we can export/import only keydata in the
324 * following combinations:
325 * - domain parameters only
326 * - public key with associated domain parameters (+optional other params)
327 * - private key with associated public key and domain parameters
328 * (+optional other params)
331 * - domain parameters must always be requested
332 * - private key must be requested alongside public key
333 * - other parameters must be requested only alongside a key
335 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
337 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
338 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
340 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
341 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
344 tmpl = OSSL_PARAM_BLD_new();
348 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
349 ok = ok && domparams_to_params(ec, tmpl);
350 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
351 int include_private =
352 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
354 ok = ok && key_to_params(ec, tmpl, include_private);
356 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
357 ok = ok && otherparams_to_params(ec, tmpl);
360 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
363 ok = param_cb(params, cbarg);
364 OSSL_PARAM_BLD_free_params(params);
366 OSSL_PARAM_BLD_free(tmpl);
370 /* IMEXPORT = IMPORT + EXPORT */
372 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
373 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_NAME, NULL, 0)
374 # define EC_IMEXPORTABLE_PUBLIC_KEY \
375 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
376 # define EC_IMEXPORTABLE_PRIVATE_KEY \
377 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
378 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
379 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
382 * Include all the possible combinations of OSSL_PARAM arrays for
383 * ec_imexport_types().
385 * They are in a separate file as it is ~100 lines of unreadable and
386 * uninteresting machine generated stuff.
388 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
389 * combinations of the bits in `selection`, it also includes combinations that
390 * are not really useful: we might want to consider alternatives to this
393 #include "ec_kmgmt_imexport.inc"
396 const OSSL_PARAM *ec_imexport_types(int selection)
400 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
402 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
404 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
406 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
408 return ec_types[type_select];
412 const OSSL_PARAM *ec_import_types(int selection)
414 return ec_imexport_types(selection);
418 const OSSL_PARAM *ec_export_types(int selection)
420 return ec_imexport_types(selection);
424 int ec_get_params(void *key, OSSL_PARAM params[])
427 const EC_GROUP *ecg = NULL;
430 ecg = EC_KEY_get0_group(eck);
434 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
435 && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
437 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
438 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
440 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
441 int ecbits, sec_bits;
443 ecbits = EC_GROUP_order_bits(ecg);
446 * The following estimates are based on the values published
447 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
448 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
450 * Note that the above reference explicitly categorizes algorithms in a
451 * discrete set of values {80, 112, 128, 192, 256}, and that it is
452 * relevant only for NIST approved Elliptic Curves, while OpenSSL
453 * applies the same logic also to other curves.
455 * Classifications produced by other standardazing bodies might differ,
456 * so the results provided for "bits of security" by this provider are
457 * to be considered merely indicative, and it is the users'
458 * responsibility to compare these values against the normative
459 * references that may be relevant for their intent and purposes.
463 else if (ecbits >= 384)
465 else if (ecbits >= 256)
467 else if (ecbits >= 224)
469 else if (ecbits >= 160)
472 sec_bits = ecbits / 2;
474 if (!OSSL_PARAM_set_int(p, sec_bits))
478 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
480 int ecdh_cofactor_mode = 0;
483 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
485 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
492 static const OSSL_PARAM ec_known_gettable_params[] = {
493 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
494 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
495 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
496 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
501 const OSSL_PARAM *ec_gettable_params(void)
503 return ec_known_gettable_params;
506 static const OSSL_PARAM ec_known_settable_params[] = {
507 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
512 const OSSL_PARAM *ec_settable_params(void)
514 return ec_known_settable_params;
518 int ec_set_params(void *key, const OSSL_PARAM params[])
523 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
524 if (p != NULL && !ec_set_param_ecdh_cofactor_mode(eck, p))
531 int ec_validate(void *keydata, int selection)
533 EC_KEY *eck = keydata;
535 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
540 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
543 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
544 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
546 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
547 ok = ok && ec_key_public_check(eck, ctx);
549 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
550 ok = ok && ec_key_private_check(eck);
552 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
553 ok = ok && ec_key_pairwise_check(eck, ctx);
559 const OSSL_DISPATCH ec_keymgmt_functions[] = {
560 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
561 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
562 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
563 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
564 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
565 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
566 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
567 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
568 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
569 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
570 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
571 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
572 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
573 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
574 (void (*)(void))ec_query_operation_name },