43ccb5d0a9e282be68da902160ae4703a87aa7c2
[oweals/openssl.git] / providers / implementations / keymgmt / ec_kmgmt.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
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"
27
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;
42
43 #define EC_POSSIBLE_SELECTIONS                 \
44     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
45
46 static
47 const char *ec_query_operation_name(int operation_id)
48 {
49     switch (operation_id) {
50     case OSSL_OP_KEYEXCH:
51         return "ECDH";
52 #if 0
53     case OSSL_OP_SIGNATURE:
54         return deflt_signature;
55 #endif
56     }
57     return NULL;
58 }
59
60 static ossl_inline
61 int params_to_domparams(EC_KEY *ec, const OSSL_PARAM params[])
62 {
63     const OSSL_PARAM *param_ec_name;
64     EC_GROUP *ecg = NULL;
65     char *curve_name = NULL;
66     int ok = 0;
67
68     if (ec == NULL)
69         return 0;
70
71     param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
72     if (param_ec_name == NULL) {
73         /* explicit parameters */
74
75         /*
76          * TODO(3.0): should we support explicit parameters curves?
77          */
78         return 0;
79     } else {
80         /* named curve */
81         int curve_nid;
82
83         if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
84                 || curve_name == NULL
85                 || (curve_nid = ec_curve_name2nid(curve_name)) == NID_undef)
86             goto err;
87
88         if ((ecg = EC_GROUP_new_by_curve_name_ex(ec_key_get_libctx(ec),
89                                                  curve_nid)) == NULL)
90             goto err;
91     }
92
93     if (!EC_KEY_set_group(ec, ecg))
94         goto err;
95
96     /*
97      * TODO(3.0): if the group has changed, should we invalidate the private and
98      * public key?
99      */
100
101     ok = 1;
102
103  err:
104     OPENSSL_free(curve_name);
105     EC_GROUP_free(ecg);
106     return ok;
107 }
108
109 static ossl_inline
110 int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
111 {
112     const EC_GROUP *ecg;
113     int curve_nid;
114
115     if (ec == NULL)
116         return 0;
117
118     ecg = EC_KEY_get0_group(ec);
119     if (ecg == NULL)
120         return 0;
121
122     curve_nid = EC_GROUP_get_curve_name(ecg);
123
124     if (curve_nid == NID_undef) {
125         /* explicit parameters */
126
127         /*
128          * TODO(3.0): should we support explicit parameters curves?
129          */
130         return 0;
131     } else {
132         /* named curve */
133         const char *curve_name = NULL;
134
135         if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
136             return 0;
137
138         if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
139             return 0;
140     }
141
142     return 1;
143 }
144
145 /*
146  * Callers of params_to_key MUST make sure that params_to_domparams has been
147  * called before!
148  *
149  * This function only imports the bare keypair, domain parameters and other
150  * parameters are imported separately, and domain parameters are required to
151  * define a keypair.
152  */
153 static ossl_inline
154 int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
155 {
156     const OSSL_PARAM *param_priv_key, *param_pub_key;
157     BN_CTX *ctx = NULL;
158     BIGNUM *priv_key = NULL;
159     unsigned char *pub_key = NULL;
160     size_t pub_key_len;
161     const EC_GROUP *ecg = NULL;
162     EC_POINT *pub_point = NULL;
163     int ok = 0;
164
165     ecg = EC_KEY_get0_group(ec);
166     if (ecg == NULL)
167         return 0;
168
169     param_priv_key =
170         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
171     param_pub_key =
172         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
173
174     ctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
175     if (ctx == NULL)
176         goto err;
177     /*
178      * We want to have at least a public key either way, so we end up
179      * requiring it unconditionally.
180      */
181     if (param_pub_key == NULL
182             || !OSSL_PARAM_get_octet_string(param_pub_key,
183                                             (void **)&pub_key, 0, &pub_key_len)
184             || (pub_point = EC_POINT_new(ecg)) == NULL
185             || !EC_POINT_oct2point(ecg, pub_point,
186                                    pub_key, pub_key_len, ctx))
187         goto err;
188
189     if (param_priv_key != NULL && include_private) {
190         int fixed_top;
191         const BIGNUM *order;
192
193         /*
194          * Key import/export should never leak the bit length of the secret
195          * scalar in the key.
196          *
197          * For this reason, on export we use padded BIGNUMs with fixed length.
198          *
199          * When importing we also should make sure that, even if short lived,
200          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
201          * soon as possible, so that any processing of this BIGNUM might opt for
202          * constant time implementations in the backend.
203          *
204          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
205          * to preallocate the BIGNUM internal buffer to a fixed public size big
206          * enough that operations performed during the processing never trigger
207          * a realloc which would leak the size of the scalar through memory
208          * accesses.
209          *
210          * Fixed Length
211          * ------------
212          *
213          * The order of the large prime subgroup of the curve is our choice for
214          * a fixed public size, as that is generally the upper bound for
215          * generating a private key in EC cryptosystems and should fit all valid
216          * secret scalars.
217          *
218          * For padding on export we just use the bit length of the order
219          * converted to bytes (rounding up).
220          *
221          * For preallocating the BIGNUM storage we look at the number of "words"
222          * required for the internal representation of the order, and we
223          * preallocate 2 extra "words" in case any of the subsequent processing
224          * might temporarily overflow the order length.
225          */
226         order = EC_GROUP_get0_order(ecg);
227         if (order == NULL || BN_is_zero(order))
228             goto err;
229
230         fixed_top = bn_get_top(order) + 2;
231
232         if ((priv_key = BN_secure_new()) == NULL)
233             goto err;
234         if (bn_wexpand(priv_key, fixed_top) == NULL)
235             goto err;
236         BN_set_flags(priv_key, BN_FLG_CONSTTIME);
237
238         if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
239             goto err;
240     }
241
242     if (priv_key != NULL
243             && !EC_KEY_set_private_key(ec, priv_key))
244         goto err;
245
246     if (!EC_KEY_set_public_key(ec, pub_point))
247         goto err;
248
249     ok = 1;
250
251  err:
252     BN_CTX_free(ctx);
253     BN_clear_free(priv_key);
254     OPENSSL_free(pub_key);
255     EC_POINT_free(pub_point);
256     return ok;
257 }
258
259 /*
260  * Callers of key_to_params MUST make sure that domparams_to_params is also
261  * called!
262  *
263  * This function only exports the bare keypair, domain parameters and other
264  * parameters are exported separately.
265  */
266 static ossl_inline
267 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
268 {
269     const BIGNUM *priv_key = NULL;
270     const EC_POINT *pub_point = NULL;
271     const EC_GROUP *ecg = NULL;
272     unsigned char *pub_key = NULL;
273     size_t pub_key_len = 0;
274     int ret = 0;
275
276     if (eckey == NULL)
277         return 0;
278
279     ecg = EC_KEY_get0_group(eckey);
280     priv_key = EC_KEY_get0_private_key(eckey);
281     pub_point = EC_KEY_get0_public_key(eckey);
282
283     /* group and public_key must be present, priv_key is optional */
284     if (ecg == NULL || pub_point == NULL)
285         return 0;
286     if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
287                                           POINT_CONVERSION_COMPRESSED,
288                                           &pub_key, NULL)) == 0)
289         return 0;
290
291     if (!ossl_param_bld_push_octet_string(tmpl,
292                                           OSSL_PKEY_PARAM_PUB_KEY,
293                                           pub_key, pub_key_len))
294         goto err;
295
296     if (priv_key != NULL && include_private) {
297         size_t sz;
298         int ecbits;
299
300         /*
301          * Key import/export should never leak the bit length of the secret
302          * scalar in the key.
303          *
304          * For this reason, on export we use padded BIGNUMs with fixed length.
305          *
306          * When importing we also should make sure that, even if short lived,
307          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
308          * soon as possible, so that any processing of this BIGNUM might opt for
309          * constant time implementations in the backend.
310          *
311          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
312          * to preallocate the BIGNUM internal buffer to a fixed public size big
313          * enough that operations performed during the processing never trigger
314          * a realloc which would leak the size of the scalar through memory
315          * accesses.
316          *
317          * Fixed Length
318          * ------------
319          *
320          * The order of the large prime subgroup of the curve is our choice for
321          * a fixed public size, as that is generally the upper bound for
322          * generating a private key in EC cryptosystems and should fit all valid
323          * secret scalars.
324          *
325          * For padding on export we just use the bit length of the order
326          * converted to bytes (rounding up).
327          *
328          * For preallocating the BIGNUM storage we look at the number of "words"
329          * required for the internal representation of the order, and we
330          * preallocate 2 extra "words" in case any of the subsequent processing
331          * might temporarily overflow the order length.
332          */
333         ecbits = EC_GROUP_order_bits(ecg);
334         if (ecbits <= 0)
335             goto err;
336         sz = (ecbits + 7 ) / 8;
337         if (!ossl_param_bld_push_BN_pad(tmpl,
338                                         OSSL_PKEY_PARAM_PRIV_KEY,
339                                         priv_key, sz))
340             goto err;
341     }
342
343     ret = 1;
344
345  err:
346     OPENSSL_free(pub_key);
347     return ret;
348 }
349
350 static ossl_inline
351 int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
352 {
353     const EC_GROUP *ecg = EC_KEY_get0_group(ec);
354     const BIGNUM *cofactor;
355     int mode;
356
357     if (!OSSL_PARAM_get_int(p, &mode))
358         return 0;
359
360     /*
361      * mode can be only 0 for disable, or 1 for enable here.
362      *
363      * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
364      * also supports mode == -1 with the meaning of "reset to the default for
365      * the associated key".
366      */
367     if (mode < 0 || mode > 1)
368         return 0;
369
370     if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
371         return 0;
372
373     /* ECDH cofactor mode has no effect if cofactor is 1 */
374     if (BN_is_one(cofactor))
375         return 1;
376
377     if (mode == 1)
378         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
379     else if (mode == 0)
380         EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
381
382     return 1;
383 }
384
385 static ossl_inline
386 int params_to_otherparams(EC_KEY *ec, const OSSL_PARAM params[])
387 {
388     const OSSL_PARAM *p;
389
390     if (ec == NULL)
391         return 0;
392
393     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
394     if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))
395         return 0;
396
397     return 1;
398 }
399
400 static ossl_inline
401 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
402 {
403     int ecdh_cofactor_mode = 0;
404
405     if (ec == NULL)
406         return 0;
407
408     ecdh_cofactor_mode =
409         (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
410     if (!ossl_param_bld_push_int(tmpl,
411                 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
412                 ecdh_cofactor_mode))
413         return 0;
414
415     return 1;
416 }
417
418 static
419 void *ec_newdata(void *provctx)
420 {
421     return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
422 }
423
424 static
425 void ec_freedata(void *keydata)
426 {
427     EC_KEY_free(keydata);
428 }
429
430 static
431 int ec_has(void *keydata, int selection)
432 {
433     EC_KEY *ec = keydata;
434     int ok = 0;
435
436     if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
437         ok = 1;
438
439     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
440         ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
441     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
442         ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
443     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
444         ok = ok && (EC_KEY_get0_group(ec) != NULL);
445     /*
446      * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be available,
447      * so no extra check is needed other than the previous one against
448      * EC_POSSIBLE_SELECTIONS.
449      */
450
451     return ok;
452 }
453
454 static int ec_match(const void *keydata1, const void *keydata2, int selection)
455 {
456     const EC_KEY *ec1 = keydata1;
457     const EC_KEY *ec2 = keydata2;
458     const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
459     const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
460     int ok = 1;
461
462     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
463         ok = ok && group_a != NULL && group_b != NULL
464             && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
465     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
466         const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
467         const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
468
469         ok = ok && BN_cmp(pa, pb) == 0;
470     }
471     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
472         const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
473         const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
474
475         ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
476     }
477     return ok;
478 }
479
480 static
481 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
482 {
483     EC_KEY *ec = keydata;
484     int ok = 1;
485
486     if (ec == NULL)
487         return 0;
488
489     /*
490      * In this implementation, we can export/import only keydata in the
491      * following combinations:
492      *   - domain parameters only
493      *   - public key with associated domain parameters (+optional other params)
494      *   - private key with associated public key and domain parameters
495      *         (+optional other params)
496      *
497      * This means:
498      *   - domain parameters must always be requested
499      *   - private key must be requested alongside public key
500      *   - other parameters must be requested only alongside a key
501      */
502     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
503         return 0;
504     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
505             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
506         return 0;
507     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
508             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
509         return 0;
510
511     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
512         ok = ok && params_to_domparams(ec, params);
513     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
514         int include_private =
515             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
516
517         ok = ok && params_to_key(ec, params, include_private);
518     }
519     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
520         ok = ok && params_to_otherparams(ec, params);
521
522     return ok;
523 }
524
525 static
526 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
527               void *cbarg)
528 {
529     EC_KEY *ec = keydata;
530     OSSL_PARAM_BLD tmpl;
531     OSSL_PARAM *params = NULL;
532     int ok = 1;
533
534     if (ec == NULL)
535         return 0;
536
537     /*
538      * In this implementation, we can export/import only keydata in the
539      * following combinations:
540      *   - domain parameters only
541      *   - public key with associated domain parameters (+optional other params)
542      *   - private key with associated public key and domain parameters
543      *         (+optional other params)
544      *
545      * This means:
546      *   - domain parameters must always be requested
547      *   - private key must be requested alongside public key
548      *   - other parameters must be requested only alongside a key
549      */
550     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
551         return 0;
552     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
553             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
554         return 0;
555     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
556             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
557         return 0;
558
559     ossl_param_bld_init(&tmpl);
560
561     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
562         ok = ok && domparams_to_params(ec, &tmpl);
563     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
564         int include_private =
565             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
566
567         ok = ok && key_to_params(ec, &tmpl, include_private);
568     }
569     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
570         ok = ok && otherparams_to_params(ec, &tmpl);
571
572     if (!ok
573         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
574         return 0;
575
576     ok = param_cb(params, cbarg);
577     ossl_param_bld_free(params);
578     return ok;
579 }
580
581 /* IMEXPORT = IMPORT + EXPORT */
582
583 # define EC_IMEXPORTABLE_DOM_PARAMETERS                          \
584     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_NAME, NULL, 0)
585 # define EC_IMEXPORTABLE_PUBLIC_KEY                              \
586     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
587 # define EC_IMEXPORTABLE_PRIVATE_KEY                             \
588     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
589 # define EC_IMEXPORTABLE_OTHER_PARAMETERS                        \
590     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
591
592 /*
593  * Include all the possible combinations of OSSL_PARAM arrays for
594  * ec_imexport_types().
595  *
596  * They are in a separate file as it is ~100 lines of unreadable and
597  * uninteresting machine generated stuff.
598  *
599  * TODO(3.0): the generated list looks quite ugly, as to cover all possible
600  * combinations of the bits in `selection`, it also includes combinations that
601  * are not really useful: we might want to consider alternatives to this
602  * solution.
603  */
604 #include "ec_kmgmt_imexport.inc"
605
606 static ossl_inline
607 const OSSL_PARAM *ec_imexport_types(int selection)
608 {
609     int type_select = 0;
610
611     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
612         type_select += 1;
613     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
614         type_select += 2;
615     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
616         type_select += 4;
617     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
618         type_select += 8;
619     return ec_types[type_select];
620 }
621
622 static
623 const OSSL_PARAM *ec_import_types(int selection)
624 {
625     return ec_imexport_types(selection);
626 }
627
628 static
629 const OSSL_PARAM *ec_export_types(int selection)
630 {
631     return ec_imexport_types(selection);
632 }
633
634 static
635 int ec_get_params(void *key, OSSL_PARAM params[])
636 {
637     EC_KEY *eck = key;
638     const EC_GROUP *ecg = NULL;
639     OSSL_PARAM *p;
640
641     ecg = EC_KEY_get0_group(eck);
642     if (ecg == NULL)
643         return 0;
644
645     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
646         && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
647         return 0;
648     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
649         && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
650         return 0;
651     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
652         int ecbits, sec_bits;
653
654         ecbits = EC_GROUP_order_bits(ecg);
655
656         /*
657          * The following estimates are based on the values published
658          * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
659          * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
660          *
661          * Note that the above reference explicitly categorizes algorithms in a
662          * discrete set of values {80, 112, 128, 192, 256}, and that it is
663          * relevant only for NIST approved Elliptic Curves, while OpenSSL
664          * applies the same logic also to other curves.
665          *
666          * Classifications produced by other standardazing bodies might differ,
667          * so the results provided for "bits of security" by this provider are
668          * to be considered merely indicative, and it is the users'
669          * responsibility to compare these values against the normative
670          * references that may be relevant for their intent and purposes.
671          */
672         if (ecbits >= 512)
673             sec_bits = 256;
674         else if (ecbits >= 384)
675             sec_bits = 192;
676         else if (ecbits >= 256)
677             sec_bits = 128;
678         else if (ecbits >= 224)
679             sec_bits = 112;
680         else if (ecbits >= 160)
681             sec_bits = 80;
682         else
683             sec_bits = ecbits / 2;
684
685         if (!OSSL_PARAM_set_int(p, sec_bits))
686             return 0;
687     }
688
689     p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
690     if (p != NULL) {
691         int ecdh_cofactor_mode = 0;
692
693         ecdh_cofactor_mode =
694             (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
695
696         if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
697             return 0;
698     }
699
700     return 1;
701 }
702
703 static const OSSL_PARAM ec_known_gettable_params[] = {
704     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
705     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
706     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
707     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
708     OSSL_PARAM_END
709 };
710
711 static
712 const OSSL_PARAM *ec_gettable_params(void)
713 {
714     return ec_known_gettable_params;
715 }
716
717 static const OSSL_PARAM ec_known_settable_params[] = {
718     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
719     OSSL_PARAM_END
720 };
721
722 static
723 const OSSL_PARAM *ec_settable_params(void)
724 {
725     return ec_known_settable_params;
726 }
727
728 static
729 int ec_set_params(void *key, const OSSL_PARAM params[])
730 {
731     EC_KEY *eck = key;
732     const OSSL_PARAM *p;
733
734     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
735     if (p != NULL && !ec_set_param_ecdh_cofactor_mode(eck, p))
736         return 0;
737
738     return 1;
739 }
740
741 static
742 int ec_validate(void *keydata, int selection)
743 {
744     EC_KEY *eck = keydata;
745     int ok = 0;
746     BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
747
748     if (ctx == NULL)
749         return 0;
750
751     if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
752         ok = 1;
753
754     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
755         ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
756
757     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
758         ok = ok && ec_key_public_check(eck, ctx);
759
760     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
761         ok = ok && ec_key_private_check(eck);
762
763     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
764         ok = ok && ec_key_pairwise_check(eck, ctx);
765
766     BN_CTX_free(ctx);
767     return ok;
768 }
769
770 const OSSL_DISPATCH ec_keymgmt_functions[] = {
771     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
772     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
773     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
774     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
775     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
776     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
777     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
778     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
779     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
780     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
781     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
782     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
783     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
784     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
785         (void (*)(void))ec_query_operation_name },
786     { 0, NULL }
787 };