]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/keymgmt/ec_kmgmt.c
Add EVP_PKEY_public_check_quick.
[thirdparty/openssl.git] / providers / implementations / keymgmt / ec_kmgmt.c
1 /*
2 * Copyright 2020-2021 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 "e_os.h" /* strcasecmp */
17 #include <string.h>
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/bn.h>
21 #include <openssl/err.h>
22 #include <openssl/objects.h>
23 #include <openssl/proverr.h>
24 #include "crypto/bn.h"
25 #include "crypto/ec.h"
26 #include "prov/implementations.h"
27 #include "prov/providercommon.h"
28 #include "prov/provider_ctx.h"
29 #include "internal/param_build_set.h"
30
31 #ifndef FIPS_MODULE
32 # ifndef OPENSSL_NO_SM2
33 # include "crypto/sm2.h"
34 # endif
35 #endif
36
37 static OSSL_FUNC_keymgmt_new_fn ec_newdata;
38 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init;
39 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template;
40 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params;
41 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params;
42 static OSSL_FUNC_keymgmt_gen_fn ec_gen;
43 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup;
44 static OSSL_FUNC_keymgmt_load_fn ec_load;
45 static OSSL_FUNC_keymgmt_free_fn ec_freedata;
46 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params;
47 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params;
48 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params;
49 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params;
50 static OSSL_FUNC_keymgmt_has_fn ec_has;
51 static OSSL_FUNC_keymgmt_match_fn ec_match;
52 static OSSL_FUNC_keymgmt_validate_fn ec_validate;
53 static OSSL_FUNC_keymgmt_import_fn ec_import;
54 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types;
55 static OSSL_FUNC_keymgmt_export_fn ec_export;
56 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
57 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
58 #ifndef FIPS_MODULE
59 # ifndef OPENSSL_NO_SM2
60 static OSSL_FUNC_keymgmt_gen_fn sm2_gen;
61 static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params;
62 static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params;
63 static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params;
64 static OSSL_FUNC_keymgmt_import_fn sm2_import;
65 static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name;
66 static OSSL_FUNC_keymgmt_validate_fn sm2_validate;
67 # endif
68 #endif
69
70 #define EC_DEFAULT_MD "SHA256"
71 #define EC_POSSIBLE_SELECTIONS \
72 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
73 #define SM2_DEFAULT_MD "SM3"
74
75 static
76 const char *ec_query_operation_name(int operation_id)
77 {
78 switch (operation_id) {
79 case OSSL_OP_KEYEXCH:
80 return "ECDH";
81 case OSSL_OP_SIGNATURE:
82 return "ECDSA";
83 }
84 return NULL;
85 }
86
87 #ifndef FIPS_MODULE
88 # ifndef OPENSSL_NO_SM2
89 static
90 const char *sm2_query_operation_name(int operation_id)
91 {
92 switch (operation_id) {
93 case OSSL_OP_SIGNATURE:
94 return "SM2";
95 }
96 return NULL;
97 }
98 # endif
99 #endif
100
101 /*
102 * Callers of key_to_params MUST make sure that domparams_to_params is also
103 * called!
104 *
105 * This function only exports the bare keypair, domain parameters and other
106 * parameters are exported separately.
107 */
108 static ossl_inline
109 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
110 OSSL_PARAM params[], int include_private,
111 unsigned char **pub_key)
112 {
113 BIGNUM *x = NULL, *y = NULL;
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 BN_CTX *bnctx = NULL;
120
121 if (eckey == NULL
122 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
123 return 0;
124
125 priv_key = EC_KEY_get0_private_key(eckey);
126 pub_point = EC_KEY_get0_public_key(eckey);
127
128 if (pub_point != NULL) {
129 OSSL_PARAM *p = NULL, *px = NULL, *py = NULL;
130 /*
131 * EC_POINT_point2buf() can generate random numbers in some
132 * implementations so we need to ensure we use the correct libctx.
133 */
134 bnctx = BN_CTX_new_ex(ec_key_get_libctx(eckey));
135 if (bnctx == NULL)
136 goto err;
137
138
139 /* If we are doing a get then check first before decoding the point */
140 if (tmpl == NULL) {
141 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
142 px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
143 py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
144 }
145
146 if (p != NULL || tmpl != NULL) {
147 /* convert pub_point to a octet string according to the SECG standard */
148 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
149 POINT_CONVERSION_COMPRESSED,
150 pub_key, bnctx)) == 0
151 || !ossl_param_build_set_octet_string(tmpl, p,
152 OSSL_PKEY_PARAM_PUB_KEY,
153 *pub_key, pub_key_len))
154 goto err;
155 }
156 if (px != NULL || py != NULL) {
157 if (px != NULL)
158 x = BN_CTX_get(bnctx);
159 if (py != NULL)
160 y = BN_CTX_get(bnctx);
161
162 if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx))
163 goto err;
164 if (px != NULL
165 && !ossl_param_build_set_bn(tmpl, px,
166 OSSL_PKEY_PARAM_EC_PUB_X, x))
167 goto err;
168 if (py != NULL
169 && !ossl_param_build_set_bn(tmpl, py,
170 OSSL_PKEY_PARAM_EC_PUB_Y, y))
171 goto err;
172 }
173 }
174
175 if (priv_key != NULL && include_private) {
176 size_t sz;
177 int ecbits;
178
179 /*
180 * Key import/export should never leak the bit length of the secret
181 * scalar in the key.
182 *
183 * For this reason, on export we use padded BIGNUMs with fixed length.
184 *
185 * When importing we also should make sure that, even if short lived,
186 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
187 * soon as possible, so that any processing of this BIGNUM might opt for
188 * constant time implementations in the backend.
189 *
190 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
191 * to preallocate the BIGNUM internal buffer to a fixed public size big
192 * enough that operations performed during the processing never trigger
193 * a realloc which would leak the size of the scalar through memory
194 * accesses.
195 *
196 * Fixed Length
197 * ------------
198 *
199 * The order of the large prime subgroup of the curve is our choice for
200 * a fixed public size, as that is generally the upper bound for
201 * generating a private key in EC cryptosystems and should fit all valid
202 * secret scalars.
203 *
204 * For padding on export we just use the bit length of the order
205 * converted to bytes (rounding up).
206 *
207 * For preallocating the BIGNUM storage we look at the number of "words"
208 * required for the internal representation of the order, and we
209 * preallocate 2 extra "words" in case any of the subsequent processing
210 * might temporarily overflow the order length.
211 */
212 ecbits = EC_GROUP_order_bits(ecg);
213 if (ecbits <= 0)
214 goto err;
215 sz = (ecbits + 7 ) / 8;
216
217 if (!ossl_param_build_set_bn_pad(tmpl, params,
218 OSSL_PKEY_PARAM_PRIV_KEY,
219 priv_key, sz))
220 goto err;
221 }
222 ret = 1;
223 err:
224 BN_CTX_free(bnctx);
225 return ret;
226 }
227
228 static ossl_inline
229 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
230 OSSL_PARAM params[])
231 {
232 int ecdh_cofactor_mode = 0, group_check = 0;
233 const char *name = NULL;
234 point_conversion_form_t format;
235
236 if (ec == NULL)
237 return 0;
238
239 format = EC_KEY_get_conv_form(ec);
240 name = ec_pt_format_id2name((int)format);
241 if (name != NULL
242 && !ossl_param_build_set_utf8_string(tmpl, params,
243 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
244 name))
245 return 0;
246
247 group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK;
248 name = ec_check_group_type_id2name(group_check);
249 if (name != NULL
250 && !ossl_param_build_set_utf8_string(tmpl, params,
251 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
252 name))
253 return 0;
254
255 if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0)
256 ossl_param_build_set_int(tmpl, params,
257 OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0);
258
259 ecdh_cofactor_mode =
260 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
261 return ossl_param_build_set_int(tmpl, params,
262 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
263 ecdh_cofactor_mode);
264 }
265
266 static
267 void *ec_newdata(void *provctx)
268 {
269 if (!ossl_prov_is_running())
270 return NULL;
271 return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL);
272 }
273
274 static
275 void ec_freedata(void *keydata)
276 {
277 EC_KEY_free(keydata);
278 }
279
280 static
281 int ec_has(const void *keydata, int selection)
282 {
283 const EC_KEY *ec = keydata;
284 int ok = 0;
285
286 if (ossl_prov_is_running() && ec != NULL) {
287 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
288 ok = 1;
289
290 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
291 ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
292 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
293 ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
294 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
295 ok = ok && (EC_KEY_get0_group(ec) != NULL);
296 /*
297 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
298 * available, so no extra check is needed other than the previous one
299 * against EC_POSSIBLE_SELECTIONS.
300 */
301 }
302 return ok;
303 }
304
305 static int ec_match(const void *keydata1, const void *keydata2, int selection)
306 {
307 const EC_KEY *ec1 = keydata1;
308 const EC_KEY *ec2 = keydata2;
309 const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
310 const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
311 BN_CTX *ctx = NULL;
312 int ok = 1;
313
314 if (!ossl_prov_is_running())
315 return 0;
316
317 ctx = BN_CTX_new_ex(ec_key_get_libctx(ec1));
318 if (ctx == NULL)
319 return 0;
320
321 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
322 ok = ok && group_a != NULL && group_b != NULL
323 && EC_GROUP_cmp(group_a, group_b, ctx) == 0;
324 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
325 const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
326 const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
327
328 ok = ok && BN_cmp(pa, pb) == 0;
329 }
330 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
331 const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
332 const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
333
334 ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0;
335 }
336 BN_CTX_free(ctx);
337 return ok;
338 }
339
340 static int common_check_sm2(const EC_KEY *ec, int sm2_wanted)
341 {
342 const EC_GROUP *ecg = NULL;
343
344 /*
345 * sm2_wanted: import the keys or domparams only on SM2 Curve
346 * !sm2_wanted: import the keys or domparams only not on SM2 Curve
347 */
348 if ((ecg = EC_KEY_get0_group(ec)) == NULL
349 || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2)))
350 return 0;
351 return 1;
352 }
353
354 static
355 int common_import(void *keydata, int selection, const OSSL_PARAM params[],
356 int sm2_wanted)
357 {
358 EC_KEY *ec = keydata;
359 int ok = 1;
360
361 if (!ossl_prov_is_running() || ec == NULL)
362 return 0;
363
364 /*
365 * In this implementation, we can export/import only keydata in the
366 * following combinations:
367 * - domain parameters (+optional other params)
368 * - public key with associated domain parameters (+optional other params)
369 * - private key with associated domain parameters and optional public key
370 * (+optional other params)
371 *
372 * This means:
373 * - domain parameters must always be requested
374 * - private key must be requested alongside public key
375 * - other parameters are always optional
376 */
377 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
378 return 0;
379
380 ok = ok && ec_group_fromdata(ec, params);
381
382 if (!common_check_sm2(ec, sm2_wanted))
383 return 0;
384
385 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
386 int include_private =
387 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
388
389 ok = ok && ec_key_fromdata(ec, params, include_private);
390 }
391 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
392 ok = ok && ec_key_otherparams_fromdata(ec, params);
393
394 return ok;
395 }
396
397 static
398 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
399 {
400 return common_import(keydata, selection, params, 0);
401 }
402
403 #ifndef FIPS_MODULE
404 # ifndef OPENSSL_NO_SM2
405 static
406 int sm2_import(void *keydata, int selection, const OSSL_PARAM params[])
407 {
408 return common_import(keydata, selection, params, 1);
409 }
410 # endif
411 #endif
412
413 static
414 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
415 void *cbarg)
416 {
417 EC_KEY *ec = keydata;
418 OSSL_PARAM_BLD *tmpl = NULL;
419 OSSL_PARAM *params = NULL;
420 unsigned char *pub_key = NULL, *genbuf = NULL;
421 BN_CTX *bnctx = NULL;
422 int ok = 1;
423
424 if (!ossl_prov_is_running() || ec == NULL)
425 return 0;
426
427 /*
428 * In this implementation, we can export/import only keydata in the
429 * following combinations:
430 * - domain parameters (+optional other params)
431 * - public key with associated domain parameters (+optional other params)
432 * - private key with associated public key and domain parameters
433 * (+optional other params)
434 *
435 * This means:
436 * - domain parameters must always be requested
437 * - private key must be requested alongside public key
438 * - other parameters are always optional
439 */
440 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
441 return 0;
442 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
443 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
444 return 0;
445 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
446 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
447 return 0;
448
449 tmpl = OSSL_PARAM_BLD_new();
450 if (tmpl == NULL)
451 return 0;
452
453 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
454 bnctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
455 if (bnctx == NULL) {
456 ok = 0;
457 goto end;
458 }
459 BN_CTX_start(bnctx);
460 ok = ok && ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL,
461 ec_key_get_libctx(ec), ec_key_get0_propq(ec),
462 bnctx, &genbuf);
463 }
464
465 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
466 int include_private =
467 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
468
469 ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
470 }
471 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
472 ok = ok && otherparams_to_params(ec, tmpl, NULL);
473
474 if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
475 ok = param_cb(params, cbarg);
476 end:
477 OSSL_PARAM_BLD_free_params(params);
478 OSSL_PARAM_BLD_free(tmpl);
479 OPENSSL_free(pub_key);
480 OPENSSL_free(genbuf);
481 BN_CTX_end(bnctx);
482 BN_CTX_free(bnctx);
483 return ok;
484 }
485
486 /* IMEXPORT = IMPORT + EXPORT */
487
488 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
489 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
490 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
491 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
492 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
493 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
494 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
495 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
496 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
497 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
498 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
499 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
500
501 # define EC_IMEXPORTABLE_PUBLIC_KEY \
502 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
503 # define EC_IMEXPORTABLE_PRIVATE_KEY \
504 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
505 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
506 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
507 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
508
509 /*
510 * Include all the possible combinations of OSSL_PARAM arrays for
511 * ec_imexport_types().
512 *
513 * They are in a separate file as it is ~100 lines of unreadable and
514 * uninteresting machine generated stuff.
515 */
516 #include "ec_kmgmt_imexport.inc"
517
518 static ossl_inline
519 const OSSL_PARAM *ec_imexport_types(int selection)
520 {
521 int type_select = 0;
522
523 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
524 type_select += 1;
525 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
526 type_select += 2;
527 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
528 type_select += 4;
529 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
530 type_select += 8;
531 return ec_types[type_select];
532 }
533
534 static
535 const OSSL_PARAM *ec_import_types(int selection)
536 {
537 return ec_imexport_types(selection);
538 }
539
540 static
541 const OSSL_PARAM *ec_export_types(int selection)
542 {
543 return ec_imexport_types(selection);
544 }
545
546 static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
547 {
548 #ifdef OPENSSL_NO_EC2M
549 return 1;
550 #else
551 int ret = 0, m;
552 unsigned int k1 = 0, k2 = 0, k3 = 0;
553 int basis_nid;
554 const char *basis_name = NULL;
555 int fid = EC_GROUP_get_field_type(group);
556
557 if (fid != NID_X9_62_characteristic_two_field)
558 return 1;
559
560 basis_nid = EC_GROUP_get_basis_type(group);
561 if (basis_nid == NID_X9_62_tpBasis)
562 basis_name = SN_X9_62_tpBasis;
563 else if (basis_nid == NID_X9_62_ppBasis)
564 basis_name = SN_X9_62_ppBasis;
565 else
566 goto err;
567
568 m = EC_GROUP_get_degree(group);
569 if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
570 || !ossl_param_build_set_utf8_string(NULL, params,
571 OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
572 basis_name))
573 goto err;
574
575 if (basis_nid == NID_X9_62_tpBasis) {
576 if (!EC_GROUP_get_trinomial_basis(group, &k1)
577 || !ossl_param_build_set_int(NULL, params,
578 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
579 (int)k1))
580 goto err;
581 } else {
582 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
583 || !ossl_param_build_set_int(NULL, params,
584 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
585 || !ossl_param_build_set_int(NULL, params,
586 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
587 || !ossl_param_build_set_int(NULL, params,
588 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
589 goto err;
590 }
591 ret = 1;
592 err:
593 return ret;
594 #endif /* OPENSSL_NO_EC2M */
595 }
596
597 static
598 int common_get_params(void *key, OSSL_PARAM params[], int sm2)
599 {
600 int ret = 0;
601 EC_KEY *eck = key;
602 const EC_GROUP *ecg = NULL;
603 OSSL_PARAM *p;
604 unsigned char *pub_key = NULL, *genbuf = NULL;
605 OSSL_LIB_CTX *libctx;
606 const char *propq;
607 BN_CTX *bnctx = NULL;
608
609 ecg = EC_KEY_get0_group(eck);
610 if (ecg == NULL)
611 return 0;
612
613 libctx = ec_key_get_libctx(eck);
614 propq = ec_key_get0_propq(eck);
615
616 bnctx = BN_CTX_new_ex(libctx);
617 if (bnctx == NULL)
618 return 0;
619 BN_CTX_start(bnctx);
620
621 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
622 && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
623 goto err;
624 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
625 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
626 goto err;
627 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
628 int ecbits, sec_bits;
629
630 ecbits = EC_GROUP_order_bits(ecg);
631
632 /*
633 * The following estimates are based on the values published
634 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
635 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
636 *
637 * Note that the above reference explicitly categorizes algorithms in a
638 * discrete set of values {80, 112, 128, 192, 256}, and that it is
639 * relevant only for NIST approved Elliptic Curves, while OpenSSL
640 * applies the same logic also to other curves.
641 *
642 * Classifications produced by other standardazing bodies might differ,
643 * so the results provided for "bits of security" by this provider are
644 * to be considered merely indicative, and it is the users'
645 * responsibility to compare these values against the normative
646 * references that may be relevant for their intent and purposes.
647 */
648 if (ecbits >= 512)
649 sec_bits = 256;
650 else if (ecbits >= 384)
651 sec_bits = 192;
652 else if (ecbits >= 256)
653 sec_bits = 128;
654 else if (ecbits >= 224)
655 sec_bits = 112;
656 else if (ecbits >= 160)
657 sec_bits = 80;
658 else
659 sec_bits = ecbits / 2;
660
661 if (!OSSL_PARAM_set_int(p, sec_bits))
662 goto err;
663 }
664
665 if (!sm2) {
666 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
667 && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
668 goto err;
669 } else {
670 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
671 && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
672 goto err;
673 }
674
675 /* SM2 doesn't support this PARAM */
676 if (!sm2) {
677 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
678 if (p != NULL) {
679 int ecdh_cofactor_mode = 0;
680
681 ecdh_cofactor_mode =
682 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
683
684 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
685 goto err;
686 }
687 }
688 if ((p = OSSL_PARAM_locate(params,
689 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
690 p->return_size = EC_POINT_point2oct(EC_KEY_get0_group(key),
691 EC_KEY_get0_public_key(key),
692 POINT_CONVERSION_UNCOMPRESSED,
693 p->data, p->return_size, bnctx);
694 if (p->return_size == 0)
695 goto err;
696 }
697
698 ret = ec_get_ecm_params(ecg, params)
699 && ec_group_todata(ecg, NULL, params, libctx, propq, bnctx, &genbuf)
700 && key_to_params(eck, NULL, params, 1, &pub_key)
701 && otherparams_to_params(eck, NULL, params);
702 err:
703 OPENSSL_free(genbuf);
704 OPENSSL_free(pub_key);
705 BN_CTX_end(bnctx);
706 BN_CTX_free(bnctx);
707 return ret;
708 }
709
710 static
711 int ec_get_params(void *key, OSSL_PARAM params[])
712 {
713 return common_get_params(key, params, 0);
714 }
715
716 #ifndef OPENSSL_NO_EC2M
717 # define EC2M_GETTABLE_DOM_PARAMS \
718 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
719 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
720 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
721 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
722 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
723 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
724 #else
725 # define EC2M_GETTABLE_DOM_PARAMS
726 #endif
727
728 static const OSSL_PARAM ec_known_gettable_params[] = {
729 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
730 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
731 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
732 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
733 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
734 EC_IMEXPORTABLE_DOM_PARAMETERS,
735 EC2M_GETTABLE_DOM_PARAMS
736 EC_IMEXPORTABLE_PUBLIC_KEY,
737 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
738 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
739 EC_IMEXPORTABLE_PRIVATE_KEY,
740 EC_IMEXPORTABLE_OTHER_PARAMETERS,
741 OSSL_PARAM_END
742 };
743
744 static
745 const OSSL_PARAM *ec_gettable_params(void *provctx)
746 {
747 return ec_known_gettable_params;
748 }
749
750 static const OSSL_PARAM ec_known_settable_params[] = {
751 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
752 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
753 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
754 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
755 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
756 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
757 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
758 OSSL_PARAM_END
759 };
760
761 static
762 const OSSL_PARAM *ec_settable_params(void *provctx)
763 {
764 return ec_known_settable_params;
765 }
766
767 static
768 int ec_set_params(void *key, const OSSL_PARAM params[])
769 {
770 EC_KEY *eck = key;
771 const OSSL_PARAM *p;
772
773 if (key == NULL)
774 return 0;
775
776 if (!ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
777 return 0;
778
779 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
780 if (p != NULL) {
781 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
782 int ret = 1;
783
784 if (ctx == NULL
785 || p->data_type != OSSL_PARAM_OCTET_STRING
786 || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
787 ret = 0;
788 BN_CTX_free(ctx);
789 if (!ret)
790 return 0;
791 }
792
793 return ec_key_otherparams_fromdata(eck, params);
794 }
795
796 #ifndef FIPS_MODULE
797 # ifndef OPENSSL_NO_SM2
798 static
799 int sm2_get_params(void *key, OSSL_PARAM params[])
800 {
801 return common_get_params(key, params, 1);
802 }
803
804 static const OSSL_PARAM sm2_known_gettable_params[] = {
805 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
806 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
807 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
808 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
809 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
810 EC_IMEXPORTABLE_DOM_PARAMETERS,
811 EC_IMEXPORTABLE_PUBLIC_KEY,
812 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
813 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
814 EC_IMEXPORTABLE_PRIVATE_KEY,
815 OSSL_PARAM_END
816 };
817
818 static
819 const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
820 {
821 return sm2_known_gettable_params;
822 }
823
824 static const OSSL_PARAM sm2_known_settable_params[] = {
825 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
826 OSSL_PARAM_END
827 };
828
829 static
830 const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
831 {
832 return sm2_known_settable_params;
833 }
834
835 static
836 int sm2_validate(const void *keydata, int selection, int checktype)
837 {
838 const EC_KEY *eck = keydata;
839 int ok = 0;
840 BN_CTX *ctx = NULL;
841
842 if (!ossl_prov_is_running())
843 return 0;
844
845 ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
846 if (ctx == NULL)
847 return 0;
848
849 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
850 ok = 1;
851
852 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
853 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
854
855 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
856 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
857 ok = ok && ec_key_public_check_quick(eck, ctx);
858 else
859 ok = ok && ec_key_public_check(eck, ctx);
860 }
861
862 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
863 ok = ok && sm2_key_private_check(eck);
864
865 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
866 ok = ok && ec_key_pairwise_check(eck, ctx);
867
868 BN_CTX_free(ctx);
869 return ok;
870 }
871 # endif
872 #endif
873
874 static
875 int ec_validate(const void *keydata, int selection, int checktype)
876 {
877 const EC_KEY *eck = keydata;
878 int ok = 0;
879 BN_CTX *ctx = NULL;
880
881 if (!ossl_prov_is_running())
882 return 0;
883
884 ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
885 if (ctx == NULL)
886 return 0;
887
888 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
889 ok = 1;
890
891 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
892 int flags = EC_KEY_get_flags(eck);
893
894 if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
895 ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck),
896 (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx);
897 else
898 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
899 }
900
901 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
902 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
903 ok = ok && ec_key_public_check_quick(eck, ctx);
904 else
905 ok = ok && ec_key_public_check(eck, ctx);
906 }
907
908 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
909 ok = ok && ec_key_private_check(eck);
910
911 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
912 ok = ok && ec_key_pairwise_check(eck, ctx);
913
914 BN_CTX_free(ctx);
915 return ok;
916 }
917
918 struct ec_gen_ctx {
919 OSSL_LIB_CTX *libctx;
920 char *group_name;
921 char *encoding;
922 char *pt_format;
923 char *group_check;
924 char *field_type;
925 BIGNUM *p, *a, *b, *order, *cofactor;
926 unsigned char *gen, *seed;
927 size_t gen_len, seed_len;
928 int selection;
929 int ecdh_mode;
930 EC_GROUP *gen_group;
931 };
932
933 static void *ec_gen_init(void *provctx, int selection)
934 {
935 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
936 struct ec_gen_ctx *gctx = NULL;
937
938 if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
939 return NULL;
940
941 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
942 gctx->libctx = libctx;
943 gctx->selection = selection;
944 gctx->ecdh_mode = 0;
945 }
946 return gctx;
947 }
948
949 static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
950 {
951 struct ec_gen_ctx *gctx = genctx;
952 EC_GROUP *group;
953
954 group = EC_GROUP_dup(src);
955 if (group == NULL) {
956 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
957 return 0;
958 }
959 EC_GROUP_free(gctx->gen_group);
960 gctx->gen_group = group;
961 return 1;
962 }
963
964 static int ec_gen_set_template(void *genctx, void *templ)
965 {
966 struct ec_gen_ctx *gctx = genctx;
967 EC_KEY *ec = templ;
968 const EC_GROUP *ec_group;
969
970 if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
971 return 0;
972 if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
973 return 0;
974 return ec_gen_set_group(gctx, ec_group);
975 }
976
977 #define COPY_INT_PARAM(params, key, val) \
978 p = OSSL_PARAM_locate_const(params, key); \
979 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
980 goto err;
981
982 #define COPY_UTF8_PARAM(params, key, val) \
983 p = OSSL_PARAM_locate_const(params, key); \
984 if (p != NULL) { \
985 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
986 goto err; \
987 OPENSSL_free(val); \
988 val = OPENSSL_strdup(p->data); \
989 if (val == NULL) \
990 goto err; \
991 }
992
993 #define COPY_OCTET_PARAM(params, key, val, len) \
994 p = OSSL_PARAM_locate_const(params, key); \
995 if (p != NULL) { \
996 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
997 goto err; \
998 OPENSSL_free(val); \
999 len = p->data_size; \
1000 val = OPENSSL_memdup(p->data, p->data_size); \
1001 if (val == NULL) \
1002 goto err; \
1003 }
1004
1005 #define COPY_BN_PARAM(params, key, bn) \
1006 p = OSSL_PARAM_locate_const(params, key); \
1007 if (p != NULL) { \
1008 if (bn == NULL) \
1009 bn = BN_new(); \
1010 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1011 goto err; \
1012 }
1013
1014 static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
1015 {
1016 int ret = 0;
1017 struct ec_gen_ctx *gctx = genctx;
1018 const OSSL_PARAM *p;
1019 EC_GROUP *group = NULL;
1020
1021 COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
1022
1023 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
1024 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
1025 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
1026 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
1027 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
1028
1029 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
1030 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
1031 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
1032 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
1033 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
1034
1035 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
1036 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
1037 gctx->gen_len);
1038
1039 ret = 1;
1040 err:
1041 EC_GROUP_free(group);
1042 return ret;
1043 }
1044
1045 static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
1046 {
1047 int ret = 0;
1048 OSSL_PARAM_BLD *bld;
1049 OSSL_PARAM *params = NULL;
1050 EC_GROUP *group = NULL;
1051
1052 bld = OSSL_PARAM_BLD_new();
1053 if (bld == NULL)
1054 return 0;
1055
1056 if (gctx->encoding != NULL
1057 && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
1058 gctx->encoding, 0))
1059 goto err;
1060
1061 if (gctx->pt_format != NULL
1062 && !OSSL_PARAM_BLD_push_utf8_string(bld,
1063 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
1064 gctx->pt_format, 0))
1065 goto err;
1066
1067 if (gctx->group_name != NULL) {
1068 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1069 gctx->group_name, 0))
1070 goto err;
1071 /* Ignore any other parameters if there is a group name */
1072 goto build;
1073 } else if (gctx->field_type != NULL) {
1074 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1075 gctx->field_type, 0))
1076 goto err;
1077 } else {
1078 goto err;
1079 }
1080 if (gctx->p == NULL
1081 || gctx->a == NULL
1082 || gctx->b == NULL
1083 || gctx->order == NULL
1084 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
1085 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
1086 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
1087 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
1088 goto err;
1089
1090 if (gctx->cofactor != NULL
1091 && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1092 gctx->cofactor))
1093 goto err;
1094
1095 if (gctx->seed != NULL
1096 && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
1097 gctx->seed, gctx->seed_len))
1098 goto err;
1099
1100 if (gctx->gen == NULL
1101 || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
1102 gctx->gen, gctx->gen_len))
1103 goto err;
1104 build:
1105 params = OSSL_PARAM_BLD_to_param(bld);
1106 if (params == NULL)
1107 goto err;
1108 group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
1109 if (group == NULL)
1110 goto err;
1111
1112 EC_GROUP_free(gctx->gen_group);
1113 gctx->gen_group = group;
1114
1115 ret = 1;
1116 err:
1117 OSSL_PARAM_BLD_free_params(params);
1118 OSSL_PARAM_BLD_free(bld);
1119 return ret;
1120 }
1121
1122 static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
1123 {
1124 static OSSL_PARAM settable[] = {
1125 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
1126 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1127 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
1128 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
1129 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
1130 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
1131 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
1132 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
1133 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
1134 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
1135 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
1136 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
1137 OSSL_PARAM_END
1138 };
1139
1140 return settable;
1141 }
1142
1143 static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
1144 {
1145 if (group == NULL) {
1146 ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
1147 return 0;
1148 }
1149 return EC_KEY_set_group(ec, group) > 0;
1150 }
1151
1152 /*
1153 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1154 */
1155 static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1156 {
1157 struct ec_gen_ctx *gctx = genctx;
1158 EC_KEY *ec = NULL;
1159 int ret = 0;
1160
1161 if (!ossl_prov_is_running()
1162 || gctx == NULL
1163 || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1164 return NULL;
1165
1166 if (gctx->gen_group == NULL) {
1167 if (!ec_gen_set_group_from_params(gctx))
1168 goto err;
1169 } else {
1170 if (gctx->encoding != NULL) {
1171 int flags = ec_encoding_name2id(gctx->encoding);
1172
1173 if (flags < 0)
1174 goto err;
1175 EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1176 }
1177 if (gctx->pt_format != NULL) {
1178 int format = ec_pt_format_name2id(gctx->pt_format);
1179
1180 if (format < 0)
1181 goto err;
1182 EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1183 }
1184 }
1185
1186 /* We must always assign a group, no matter what */
1187 ret = ec_gen_assign_group(ec, gctx->gen_group);
1188
1189 /* Whether you want it or not, you get a keypair, not just one half */
1190 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
1191 ret = ret && EC_KEY_generate_key(ec);
1192
1193 if (gctx->ecdh_mode != -1)
1194 ret = ret && ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
1195
1196 if (gctx->group_check != NULL)
1197 ret = ret && ec_set_check_group_type_from_name(ec, gctx->group_check);
1198 if (ret)
1199 return ec;
1200 err:
1201 /* Something went wrong, throw the key away */
1202 EC_KEY_free(ec);
1203 return NULL;
1204 }
1205
1206 #ifndef FIPS_MODULE
1207 # ifndef OPENSSL_NO_SM2
1208 /*
1209 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1210 */
1211 static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1212 {
1213 struct ec_gen_ctx *gctx = genctx;
1214 EC_KEY *ec = NULL;
1215 int ret = 1;
1216
1217 if (gctx == NULL
1218 || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1219 return NULL;
1220
1221 if (gctx->gen_group == NULL) {
1222 if (!ec_gen_set_group_from_params(gctx))
1223 goto err;
1224 } else {
1225 if (gctx->encoding) {
1226 int flags = ec_encoding_name2id(gctx->encoding);
1227
1228 if (flags < 0)
1229 goto err;
1230 EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1231 }
1232 if (gctx->pt_format != NULL) {
1233 int format = ec_pt_format_name2id(gctx->pt_format);
1234
1235 if (format < 0)
1236 goto err;
1237 EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1238 }
1239 }
1240
1241 /* We must always assign a group, no matter what */
1242 ret = ec_gen_assign_group(ec, gctx->gen_group);
1243
1244 /* Whether you want it or not, you get a keypair, not just one half */
1245 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1246 /*
1247 * For SM2, we need a new flag to indicate the 'generate' function
1248 * to use a new range
1249 */
1250 EC_KEY_set_flags(ec, EC_FLAG_SM2_RANGE);
1251 ret = ret && EC_KEY_generate_key(ec);
1252 }
1253
1254 if (ret)
1255 return ec;
1256 err:
1257 /* Something went wrong, throw the key away */
1258 EC_KEY_free(ec);
1259 return NULL;
1260 }
1261 # endif
1262 #endif
1263
1264 static void ec_gen_cleanup(void *genctx)
1265 {
1266 struct ec_gen_ctx *gctx = genctx;
1267
1268 if (gctx == NULL)
1269 return;
1270
1271 EC_GROUP_free(gctx->gen_group);
1272 BN_free(gctx->p);
1273 BN_free(gctx->a);
1274 BN_free(gctx->b);
1275 BN_free(gctx->order);
1276 BN_free(gctx->cofactor);
1277 OPENSSL_free(gctx->group_name);
1278 OPENSSL_free(gctx->field_type);
1279 OPENSSL_free(gctx->pt_format);
1280 OPENSSL_free(gctx->encoding);
1281 OPENSSL_free(gctx->seed);
1282 OPENSSL_free(gctx->gen);
1283 OPENSSL_free(gctx);
1284 }
1285
1286 static void *common_load(const void *reference, size_t reference_sz,
1287 int sm2_wanted)
1288 {
1289 EC_KEY *ec = NULL;
1290
1291 if (ossl_prov_is_running() && reference_sz == sizeof(ec)) {
1292 /* The contents of the reference is the address to our object */
1293 ec = *(EC_KEY **)reference;
1294
1295 if (!common_check_sm2(ec, sm2_wanted))
1296 return NULL;
1297
1298 /* We grabbed, so we detach it */
1299 *(EC_KEY **)reference = NULL;
1300 return ec;
1301 }
1302 return NULL;
1303 }
1304
1305 static void *ec_load(const void *reference, size_t reference_sz)
1306 {
1307 return common_load(reference, reference_sz, 0);
1308 }
1309
1310 #ifndef FIPS_MODULE
1311 # ifndef OPENSSL_NO_SM2
1312 static void *sm2_load(const void *reference, size_t reference_sz)
1313 {
1314 return common_load(reference, reference_sz, 1);
1315 }
1316 # endif
1317 #endif
1318
1319 const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
1320 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1321 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1322 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1323 (void (*)(void))ec_gen_set_template },
1324 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1325 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1326 (void (*)(void))ec_gen_settable_params },
1327 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
1328 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1329 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
1330 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1331 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
1332 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
1333 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
1334 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
1335 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1336 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1337 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
1338 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
1339 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1340 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1341 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1342 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1343 (void (*)(void))ec_query_operation_name },
1344 { 0, NULL }
1345 };
1346
1347 #ifndef FIPS_MODULE
1348 # ifndef OPENSSL_NO_SM2
1349 const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = {
1350 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1351 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1352 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1353 (void (*)(void))ec_gen_set_template },
1354 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1355 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1356 (void (*)(void))ec_gen_settable_params },
1357 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen },
1358 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1359 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load },
1360 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1361 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))sm2_get_params },
1362 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))sm2_gettable_params },
1363 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
1364 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))sm2_settable_params },
1365 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1366 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1367 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate },
1368 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import },
1369 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1370 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1371 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1372 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1373 (void (*)(void))sm2_query_operation_name },
1374 { 0, NULL }
1375 };
1376 # endif
1377 #endif