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