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