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