6a358aa93bc58d07116a8ac17814ae51656f0669
[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/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"
26
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;
40
41 #define EC_POSSIBLE_SELECTIONS                 \
42     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
43
44 static
45 const char *ec_query_operation_name(int operation_id)
46 {
47     switch (operation_id) {
48     case OSSL_OP_KEYEXCH:
49         return "ECDH";
50 #if 0
51     case OSSL_OP_SIGNATURE:
52         return deflt_signature;
53 #endif
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 = OBJ_sn2nid(curve_name)) == NID_undef)
84             goto err;
85
86         if ((ecg = EC_GROUP_new_by_curve_name(curve_nid)) == NULL)
87             goto err;
88     }
89
90     if (!EC_KEY_set_group(ec, ecg))
91         goto err;
92
93     /*
94      * TODO(3.0): if the group has changed, should we invalidate the private and
95      * public key?
96      */
97
98     ok = 1;
99
100  err:
101     OPENSSL_free(curve_name);
102     EC_GROUP_free(ecg);
103     return ok;
104 }
105
106 static ossl_inline
107 int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
108 {
109     const EC_GROUP *ecg;
110     int curve_nid;
111
112     if (ec == NULL)
113         return 0;
114
115     ecg = EC_KEY_get0_group(ec);
116     if (ecg == NULL)
117         return 0;
118
119     curve_nid = EC_GROUP_get_curve_name(ecg);
120
121     if (curve_nid == NID_undef) {
122         /* explicit parameters */
123
124         /*
125          * TODO(3.0): should we support explicit parameters curves?
126          */
127         return 0;
128     } else {
129         /* named curve */
130         const char *curve_name = NULL;
131
132         if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
133             return 0;
134
135         if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
136             return 0;
137     }
138
139     return 1;
140 }
141
142 /*
143  * Callers of params_to_key MUST make sure that params_to_domparams has been
144  * called before!
145  *
146  * This function only imports the bare keypair, domain parameters and other
147  * parameters are imported separately, and domain parameters are required to
148  * define a keypair.
149  */
150 static ossl_inline
151 int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
152 {
153     const OSSL_PARAM *param_priv_key, *param_pub_key;
154     BIGNUM *priv_key = NULL;
155     unsigned char *pub_key = NULL;
156     size_t pub_key_len;
157     const EC_GROUP *ecg = NULL;
158     EC_POINT *pub_point = NULL;
159     int ok = 0;
160
161     ecg = EC_KEY_get0_group(ec);
162     if (ecg == NULL)
163         return 0;
164
165     param_priv_key =
166         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
167     param_pub_key =
168         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
169
170     /*
171      * We want to have at least a public key either way, so we end up
172      * requiring it unconditionally.
173      */
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))
180         goto err;
181
182     if (param_priv_key != NULL && include_private) {
183         int fixed_top;
184         const BIGNUM *order;
185
186         /*
187          * Key import/export should never leak the bit length of the secret
188          * scalar in the key.
189          *
190          * For this reason, on export we use padded BIGNUMs with fixed length.
191          *
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.
196          *
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
201          * accesses.
202          *
203          * Fixed Length
204          * ------------
205          *
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
209          * secret scalars.
210          *
211          * For padding on export we just use the bit length of the order
212          * converted to bytes (rounding up).
213          *
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.
218          */
219         order = EC_GROUP_get0_order(ecg);
220         if (order == NULL || BN_is_zero(order))
221             goto err;
222
223         fixed_top = bn_get_top(order) + 2;
224
225         if ((priv_key = BN_new()) == NULL)
226             goto err;
227         if (bn_wexpand(priv_key, fixed_top) == NULL)
228             goto err;
229         BN_set_flags(priv_key, BN_FLG_CONSTTIME);
230
231         if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
232             goto err;
233     }
234
235     if (priv_key != NULL
236             && !EC_KEY_set_private_key(ec, priv_key))
237         goto err;
238
239     if (!EC_KEY_set_public_key(ec, pub_point))
240         goto err;
241
242     ok = 1;
243
244  err:
245     BN_clear_free(priv_key);
246     OPENSSL_free(pub_key);
247     EC_POINT_free(pub_point);
248     return ok;
249 }
250
251 /*
252  * Callers of key_to_params MUST make sure that domparams_to_params is also
253  * called!
254  *
255  * This function only exports the bare keypair, domain parameters and other
256  * parameters are exported separately.
257  */
258 static ossl_inline
259 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
260 {
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;
266     int ret = 0;
267
268     if (eckey == NULL)
269         return 0;
270
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);
274
275     /* group and public_key must be present, priv_key is optional */
276     if (ecg == NULL || pub_point == NULL)
277         return 0;
278     if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
279                                           POINT_CONVERSION_COMPRESSED,
280                                           &pub_key, NULL)) == 0)
281         return 0;
282
283     if (!ossl_param_bld_push_octet_string(tmpl,
284                                           OSSL_PKEY_PARAM_PUB_KEY,
285                                           pub_key, pub_key_len))
286         goto err;
287
288     if (priv_key != NULL && include_private) {
289         size_t sz;
290         int ecbits;
291
292         /*
293          * Key import/export should never leak the bit length of the secret
294          * scalar in the key.
295          *
296          * For this reason, on export we use padded BIGNUMs with fixed length.
297          *
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.
302          *
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
307          * accesses.
308          *
309          * Fixed Length
310          * ------------
311          *
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
315          * secret scalars.
316          *
317          * For padding on export we just use the bit length of the order
318          * converted to bytes (rounding up).
319          *
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.
324          */
325         ecbits = EC_GROUP_order_bits(ecg);
326         if (ecbits <= 0)
327             goto err;
328         sz = (ecbits + 7 ) / 8;
329         if (!ossl_param_bld_push_BN_pad(tmpl,
330                                         OSSL_PKEY_PARAM_PRIV_KEY,
331                                         priv_key, sz))
332             goto err;
333     }
334
335     ret = 1;
336
337  err:
338     OPENSSL_free(pub_key);
339     return ret;
340 }
341
342 static ossl_inline
343 int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
344 {
345     const EC_GROUP *ecg = EC_KEY_get0_group(ec);
346     const BIGNUM *cofactor;
347     int mode;
348
349     if (!OSSL_PARAM_get_int(p, &mode))
350         return 0;
351
352     /*
353      * mode can be only 0 for disable, or 1 for enable here.
354      *
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".
358      */
359     if (mode < 0 || mode > 1)
360         return 0;
361
362     if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
363         return 0;
364
365     /* ECDH cofactor mode has no effect if cofactor is 1 */
366     if (BN_is_one(cofactor))
367         return 1;
368
369     if (mode == 1)
370         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
371     else if (mode == 0)
372         EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
373
374     return 1;
375 }
376
377 static ossl_inline
378 int params_to_otherparams(EC_KEY *ec, const OSSL_PARAM params[])
379 {
380     const OSSL_PARAM *p;
381
382     if (ec == NULL)
383         return 0;
384
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))
387         return 0;
388
389     return 1;
390 }
391
392 static ossl_inline
393 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
394 {
395     int ecdh_cofactor_mode = 0;
396
397     if (ec == NULL)
398         return 0;
399
400     ecdh_cofactor_mode =
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,
404                 ecdh_cofactor_mode))
405         return 0;
406
407     return 1;
408 }
409
410 static
411 void *ec_newdata(void *provctx)
412 {
413     return EC_KEY_new();
414 }
415
416 static
417 void ec_freedata(void *keydata)
418 {
419     EC_KEY_free(keydata);
420 }
421
422 static
423 int ec_has(void *keydata, int selection)
424 {
425     EC_KEY *ec = keydata;
426     int ok = 0;
427
428     if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
429         ok = 1;
430
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);
437     /*
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.
441      */
442
443     return ok;
444 }
445
446 static int ec_match(const void *keydata1, const void *keydata2, int selection)
447 {
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);
452     int ok = 1;
453
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);
460
461         ok = ok && BN_cmp(pa, pb) == 0;
462     }
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);
466
467         ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
468     }
469     return ok;
470 }
471
472 static
473 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
474 {
475     EC_KEY *ec = keydata;
476     int ok = 0;
477
478     if (ec == NULL)
479         return 0;
480
481     /*
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)
488      *
489      * This means:
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
493      */
494     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
495         return 0;
496     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
497             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
498         return 0;
499     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
500             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
501         return 0;
502
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;
508
509         ok = ok && params_to_key(ec, params, include_private);
510     }
511     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
512         ok = ok && params_to_otherparams(ec, params);
513
514     return ok;
515 }
516
517 static
518 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
519               void *cbarg)
520 {
521     EC_KEY *ec = keydata;
522     OSSL_PARAM_BLD tmpl;
523     OSSL_PARAM *params = NULL;
524     int ok = 1;
525
526     if (ec == NULL)
527         return 0;
528
529     /*
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)
536      *
537      * This means:
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
541      */
542     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
543         return 0;
544     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
545             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
546         return 0;
547     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
548             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
549         return 0;
550
551     ossl_param_bld_init(&tmpl);
552
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;
558
559         ok = ok && key_to_params(ec, &tmpl, include_private);
560     }
561     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
562         ok = ok && otherparams_to_params(ec, &tmpl);
563
564     if (!ok
565         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
566         return 0;
567
568     ok = param_cb(params, cbarg);
569     ossl_param_bld_free(params);
570     return ok;
571 }
572
573 /* IMEXPORT = IMPORT + EXPORT */
574
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)
583
584 /*
585  * Include all the possible combinations of OSSL_PARAM arrays for
586  * ec_imexport_types().
587  *
588  * They are in a separate file as it is ~100 lines of unreadable and
589  * uninteresting machine generated stuff.
590  *
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
594  * solution.
595  */
596 #include "ec_kmgmt_imexport.inc"
597
598 static ossl_inline
599 const OSSL_PARAM *ec_imexport_types(int selection)
600 {
601     int type_select = 0;
602
603     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
604         type_select += 1;
605     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
606         type_select += 2;
607     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
608         type_select += 4;
609     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
610         type_select += 8;
611     return ec_types[type_select];
612 }
613
614 static
615 const OSSL_PARAM *ec_import_types(int selection)
616 {
617     return ec_imexport_types(selection);
618 }
619
620 static
621 const OSSL_PARAM *ec_export_types(int selection)
622 {
623     return ec_imexport_types(selection);
624 }
625
626 static
627 int ec_get_params(void *key, OSSL_PARAM params[])
628 {
629     EC_KEY *eck = key;
630     const EC_GROUP *ecg = NULL;
631     OSSL_PARAM *p;
632
633     ecg = EC_KEY_get0_group(eck);
634     if (ecg == NULL)
635         return 0;
636
637     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
638         && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
639         return 0;
640     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
641         && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
642         return 0;
643     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
644         int ecbits, sec_bits;
645
646         ecbits = EC_GROUP_order_bits(ecg);
647
648         /*
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 .
652          *
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.
657          *
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.
663          */
664         if (ecbits >= 512)
665             sec_bits = 256;
666         else if (ecbits >= 384)
667             sec_bits = 192;
668         else if (ecbits >= 256)
669             sec_bits = 128;
670         else if (ecbits >= 224)
671             sec_bits = 112;
672         else if (ecbits >= 160)
673             sec_bits = 80;
674         else
675             sec_bits = ecbits / 2;
676
677         if (!OSSL_PARAM_set_int(p, sec_bits))
678             return 0;
679     }
680
681     p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
682     if (p != NULL) {
683         int ecdh_cofactor_mode = 0;
684
685         ecdh_cofactor_mode =
686             (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
687
688         if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
689             return 0;
690     }
691
692     return 1;
693 }
694
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),
700     OSSL_PARAM_END
701 };
702
703 static
704 const OSSL_PARAM *ec_gettable_params(void)
705 {
706     return ec_known_gettable_params;
707 }
708
709 static const OSSL_PARAM ec_known_settable_params[] = {
710     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
711     OSSL_PARAM_END
712 };
713
714 static
715 const OSSL_PARAM *ec_settable_params(void)
716 {
717     return ec_known_settable_params;
718 }
719
720 static
721 int ec_set_params(void *key, const OSSL_PARAM params[])
722 {
723     EC_KEY *eck = key;
724     const OSSL_PARAM *p;
725
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))
728         return 0;
729
730     return 1;
731 }
732
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 },
748     { 0, NULL }
749 };