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 "internal/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;
115 ecg = EC_KEY_get0_group(eckey);
116 priv_key = EC_KEY_get0_private_key(eckey);
117 pub_point = EC_KEY_get0_public_key(eckey);
119 /* group and public_key must be present, priv_key is optional */
120 if (ecg == NULL || pub_point == NULL)
122 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
123 POINT_CONVERSION_COMPRESSED,
124 &pub_key, NULL)) == 0)
127 if (!ossl_param_bld_push_octet_string(tmpl,
128 OSSL_PKEY_PARAM_PUB_KEY,
129 pub_key, pub_key_len))
132 if (priv_key != NULL && include_private) {
137 * Key import/export should never leak the bit length of the secret
140 * For this reason, on export we use padded BIGNUMs with fixed length.
142 * When importing we also should make sure that, even if short lived,
143 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
144 * soon as possible, so that any processing of this BIGNUM might opt for
145 * constant time implementations in the backend.
147 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
148 * to preallocate the BIGNUM internal buffer to a fixed public size big
149 * enough that operations performed during the processing never trigger
150 * a realloc which would leak the size of the scalar through memory
156 * The order of the large prime subgroup of the curve is our choice for
157 * a fixed public size, as that is generally the upper bound for
158 * generating a private key in EC cryptosystems and should fit all valid
161 * For padding on export we just use the bit length of the order
162 * converted to bytes (rounding up).
164 * For preallocating the BIGNUM storage we look at the number of "words"
165 * required for the internal representation of the order, and we
166 * preallocate 2 extra "words" in case any of the subsequent processing
167 * might temporarily overflow the order length.
169 ecbits = EC_GROUP_order_bits(ecg);
172 sz = (ecbits + 7 ) / 8;
173 if (!ossl_param_bld_push_BN_pad(tmpl,
174 OSSL_PKEY_PARAM_PRIV_KEY,
182 OPENSSL_free(pub_key);
187 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
189 int ecdh_cofactor_mode = 0;
195 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
196 if (!ossl_param_bld_push_int(tmpl,
197 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
205 void *ec_newdata(void *provctx)
207 return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
211 void ec_freedata(void *keydata)
213 EC_KEY_free(keydata);
217 int ec_has(void *keydata, int selection)
219 EC_KEY *ec = keydata;
223 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
226 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
227 ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
228 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
229 ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
230 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
231 ok = ok && (EC_KEY_get0_group(ec) != NULL);
233 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
234 * available, so no extra check is needed other than the previous one
235 * against EC_POSSIBLE_SELECTIONS.
241 static int ec_match(const void *keydata1, const void *keydata2, int selection)
243 const EC_KEY *ec1 = keydata1;
244 const EC_KEY *ec2 = keydata2;
245 const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
246 const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
249 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
250 ok = ok && group_a != NULL && group_b != NULL
251 && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
252 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
253 const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
254 const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
256 ok = ok && BN_cmp(pa, pb) == 0;
258 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
259 const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
260 const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
262 ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
268 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
270 EC_KEY *ec = keydata;
277 * In this implementation, we can export/import only keydata in the
278 * following combinations:
279 * - domain parameters only
280 * - public key with associated domain parameters (+optional other params)
281 * - private key with associated public key and domain parameters
282 * (+optional other params)
285 * - domain parameters must always be requested
286 * - private key must be requested alongside public key
287 * - other parameters must be requested only alongside a key
289 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
291 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
292 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
294 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
295 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
298 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
299 ok = ok && ec_key_domparams_fromdata(ec, params);
300 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
301 int include_private =
302 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
304 ok = ok && ec_key_fromdata(ec, params, include_private);
306 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
307 ok = ok && ec_key_otherparams_fromdata(ec, params);
313 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
316 EC_KEY *ec = keydata;
318 OSSL_PARAM *params = NULL;
325 * In this implementation, we can export/import only keydata in the
326 * following combinations:
327 * - domain parameters only
328 * - public key with associated domain parameters (+optional other params)
329 * - private key with associated public key and domain parameters
330 * (+optional other params)
333 * - domain parameters must always be requested
334 * - private key must be requested alongside public key
335 * - other parameters must be requested only alongside a key
337 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
339 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
340 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
342 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
343 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
346 ossl_param_bld_init(&tmpl);
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);
368 /* IMEXPORT = IMPORT + EXPORT */
370 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
371 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_NAME, NULL, 0)
372 # define EC_IMEXPORTABLE_PUBLIC_KEY \
373 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
374 # define EC_IMEXPORTABLE_PRIVATE_KEY \
375 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
376 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
377 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
380 * Include all the possible combinations of OSSL_PARAM arrays for
381 * ec_imexport_types().
383 * They are in a separate file as it is ~100 lines of unreadable and
384 * uninteresting machine generated stuff.
386 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
387 * combinations of the bits in `selection`, it also includes combinations that
388 * are not really useful: we might want to consider alternatives to this
391 #include "ec_kmgmt_imexport.inc"
394 const OSSL_PARAM *ec_imexport_types(int selection)
398 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
400 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
402 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
404 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
406 return ec_types[type_select];
410 const OSSL_PARAM *ec_import_types(int selection)
412 return ec_imexport_types(selection);
416 const OSSL_PARAM *ec_export_types(int selection)
418 return ec_imexport_types(selection);
422 int ec_get_params(void *key, OSSL_PARAM params[])
425 const EC_GROUP *ecg = NULL;
428 ecg = EC_KEY_get0_group(eck);
432 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
433 && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
435 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
436 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
438 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
439 int ecbits, sec_bits;
441 ecbits = EC_GROUP_order_bits(ecg);
444 * The following estimates are based on the values published
445 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
446 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
448 * Note that the above reference explicitly categorizes algorithms in a
449 * discrete set of values {80, 112, 128, 192, 256}, and that it is
450 * relevant only for NIST approved Elliptic Curves, while OpenSSL
451 * applies the same logic also to other curves.
453 * Classifications produced by other standardazing bodies might differ,
454 * so the results provided for "bits of security" by this provider are
455 * to be considered merely indicative, and it is the users'
456 * responsibility to compare these values against the normative
457 * references that may be relevant for their intent and purposes.
461 else if (ecbits >= 384)
463 else if (ecbits >= 256)
465 else if (ecbits >= 224)
467 else if (ecbits >= 160)
470 sec_bits = ecbits / 2;
472 if (!OSSL_PARAM_set_int(p, sec_bits))
476 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
478 int ecdh_cofactor_mode = 0;
481 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
483 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
490 static const OSSL_PARAM ec_known_gettable_params[] = {
491 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
492 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
493 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
494 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
499 const OSSL_PARAM *ec_gettable_params(void)
501 return ec_known_gettable_params;
504 static const OSSL_PARAM ec_known_settable_params[] = {
505 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
510 const OSSL_PARAM *ec_settable_params(void)
512 return ec_known_settable_params;
516 int ec_set_params(void *key, const OSSL_PARAM params[])
521 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
522 if (p != NULL && !ec_set_param_ecdh_cofactor_mode(eck, p))
529 int ec_validate(void *keydata, int selection)
531 EC_KEY *eck = keydata;
533 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
538 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
541 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
542 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
544 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
545 ok = ok && ec_key_public_check(eck, ctx);
547 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
548 ok = ok && ec_key_private_check(eck);
550 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
551 ok = ok && ec_key_pairwise_check(eck, ctx);
557 const OSSL_DISPATCH ec_keymgmt_functions[] = {
558 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
559 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
560 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
561 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
562 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
563 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
564 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
565 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
566 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
567 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
568 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
569 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
570 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
571 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
572 (void (*)(void))ec_query_operation_name },