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