/src/openssl/providers/implementations/keymgmt/ec_kmgmt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2025 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 | 0 | #define EC_DEFAULT_MD "SHA256" |
76 | | #define EC_POSSIBLE_SELECTIONS \ |
77 | 5.03k | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) |
78 | 0 | #define SM2_DEFAULT_MD "SM3" |
79 | | |
80 | | static |
81 | | const char *ec_query_operation_name(int operation_id) |
82 | 0 | { |
83 | 0 | switch (operation_id) { |
84 | 0 | case OSSL_OP_KEYEXCH: |
85 | 0 | return "ECDH"; |
86 | 0 | case OSSL_OP_SIGNATURE: |
87 | 0 | return "ECDSA"; |
88 | 0 | } |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | | #ifndef FIPS_MODULE |
93 | | # ifndef OPENSSL_NO_SM2 |
94 | | static |
95 | | const char *sm2_query_operation_name(int operation_id) |
96 | 0 | { |
97 | 0 | switch (operation_id) { |
98 | 0 | case OSSL_OP_SIGNATURE: |
99 | 0 | return "SM2"; |
100 | 0 | } |
101 | 0 | return NULL; |
102 | 0 | } |
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 |
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 | 1.72k | { |
118 | 1.72k | BIGNUM *x = NULL, *y = NULL; |
119 | 1.72k | const BIGNUM *priv_key = NULL; |
120 | 1.72k | const EC_POINT *pub_point = NULL; |
121 | 1.72k | const EC_GROUP *ecg = NULL; |
122 | 1.72k | size_t pub_key_len = 0; |
123 | 1.72k | int ret = 0; |
124 | 1.72k | BN_CTX *bnctx = NULL; |
125 | | |
126 | 1.72k | if (eckey == NULL |
127 | 1.72k | || (ecg = EC_KEY_get0_group(eckey)) == NULL) |
128 | 0 | return 0; |
129 | | |
130 | 1.72k | priv_key = EC_KEY_get0_private_key(eckey); |
131 | 1.72k | pub_point = EC_KEY_get0_public_key(eckey); |
132 | | |
133 | 1.72k | if (pub_point != NULL) { |
134 | 1.44k | 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 | 1.44k | bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey)); |
140 | 1.44k | if (bnctx == NULL) |
141 | 0 | goto err; |
142 | | |
143 | | |
144 | | /* If we are doing a get then check first before decoding the point */ |
145 | 1.44k | if (tmpl == NULL) { |
146 | 1.44k | p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY); |
147 | 1.44k | px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X); |
148 | 1.44k | py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y); |
149 | 1.44k | } |
150 | | |
151 | 1.44k | if (p != NULL || tmpl != NULL) { |
152 | | /* convert pub_point to a octet string according to the SECG standard */ |
153 | 0 | point_conversion_form_t format = EC_KEY_get_conv_form(eckey); |
154 | |
|
155 | 0 | if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point, |
156 | 0 | format, |
157 | 0 | pub_key, bnctx)) == 0 |
158 | 0 | || !ossl_param_build_set_octet_string(tmpl, p, |
159 | 0 | OSSL_PKEY_PARAM_PUB_KEY, |
160 | 0 | *pub_key, pub_key_len)) |
161 | 0 | goto err; |
162 | 0 | } |
163 | 1.44k | if (px != NULL || py != NULL) { |
164 | 0 | if (px != NULL) { |
165 | 0 | x = BN_CTX_get(bnctx); |
166 | 0 | if (x == NULL) |
167 | 0 | goto err; |
168 | 0 | } |
169 | 0 | if (py != NULL) { |
170 | 0 | y = BN_CTX_get(bnctx); |
171 | 0 | if (y == NULL) |
172 | 0 | goto err; |
173 | 0 | } |
174 | | |
175 | 0 | if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx)) |
176 | 0 | goto err; |
177 | 0 | if (px != NULL |
178 | 0 | && !ossl_param_build_set_bn(tmpl, px, |
179 | 0 | OSSL_PKEY_PARAM_EC_PUB_X, x)) |
180 | 0 | goto err; |
181 | 0 | if (py != NULL |
182 | 0 | && !ossl_param_build_set_bn(tmpl, py, |
183 | 0 | OSSL_PKEY_PARAM_EC_PUB_Y, y)) |
184 | 0 | goto err; |
185 | 0 | } |
186 | 1.44k | } |
187 | | |
188 | 1.72k | if (priv_key != NULL && include_private) { |
189 | 1.18k | size_t sz; |
190 | 1.18k | 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 | 1.18k | ecbits = EC_GROUP_order_bits(ecg); |
226 | 1.18k | if (ecbits <= 0) |
227 | 0 | goto err; |
228 | 1.18k | sz = (ecbits + 7) / 8; |
229 | | |
230 | 1.18k | if (!ossl_param_build_set_bn_pad(tmpl, params, |
231 | 1.18k | OSSL_PKEY_PARAM_PRIV_KEY, |
232 | 1.18k | priv_key, sz)) |
233 | 0 | goto err; |
234 | 1.18k | } |
235 | 1.72k | ret = 1; |
236 | 1.72k | err: |
237 | 1.72k | BN_CTX_free(bnctx); |
238 | 1.72k | return ret; |
239 | 1.72k | } |
240 | | |
241 | | static ossl_inline |
242 | | int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl, |
243 | | OSSL_PARAM params[]) |
244 | 1.72k | { |
245 | 1.72k | int ecdh_cofactor_mode = 0, group_check = 0; |
246 | 1.72k | const char *name = NULL; |
247 | 1.72k | point_conversion_form_t format; |
248 | | |
249 | 1.72k | if (ec == NULL) |
250 | 0 | return 0; |
251 | | |
252 | 1.72k | format = EC_KEY_get_conv_form(ec); |
253 | 1.72k | name = ossl_ec_pt_format_id2name((int)format); |
254 | 1.72k | if (name != NULL |
255 | 1.72k | && !ossl_param_build_set_utf8_string(tmpl, params, |
256 | 1.70k | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, |
257 | 1.70k | name)) |
258 | 0 | return 0; |
259 | | |
260 | 1.72k | group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK; |
261 | 1.72k | name = ossl_ec_check_group_type_id2name(group_check); |
262 | 1.72k | if (name != NULL |
263 | 1.72k | && !ossl_param_build_set_utf8_string(tmpl, params, |
264 | 1.72k | OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, |
265 | 1.72k | name)) |
266 | 0 | return 0; |
267 | | |
268 | 1.72k | if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0 |
269 | 1.72k | && !ossl_param_build_set_int(tmpl, params, |
270 | 1.01k | OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) |
271 | 0 | return 0; |
272 | | |
273 | 1.72k | ecdh_cofactor_mode = |
274 | 1.72k | (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; |
275 | 1.72k | return ossl_param_build_set_int(tmpl, params, |
276 | 1.72k | OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, |
277 | 1.72k | ecdh_cofactor_mode); |
278 | 1.72k | } |
279 | | |
280 | | static |
281 | | void *ec_newdata(void *provctx) |
282 | 0 | { |
283 | 0 | if (!ossl_prov_is_running()) |
284 | 0 | return NULL; |
285 | 0 | return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL); |
286 | 0 | } |
287 | | |
288 | | #ifndef FIPS_MODULE |
289 | | # ifndef OPENSSL_NO_SM2 |
290 | | static |
291 | | void *sm2_newdata(void *provctx) |
292 | 0 | { |
293 | 0 | if (!ossl_prov_is_running()) |
294 | 0 | return NULL; |
295 | 0 | return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2); |
296 | 0 | } |
297 | | # endif |
298 | | #endif |
299 | | |
300 | | static |
301 | | void ec_freedata(void *keydata) |
302 | 1.72k | { |
303 | 1.72k | EC_KEY_free(keydata); |
304 | 1.72k | } |
305 | | |
306 | | static |
307 | | int ec_has(const void *keydata, int selection) |
308 | 1.58k | { |
309 | 1.58k | const EC_KEY *ec = keydata; |
310 | 1.58k | int ok = 1; |
311 | | |
312 | 1.58k | if (!ossl_prov_is_running() || ec == NULL) |
313 | 0 | return 0; |
314 | 1.58k | if ((selection & EC_POSSIBLE_SELECTIONS) == 0) |
315 | 0 | return 1; /* the selection is not missing */ |
316 | | |
317 | 1.58k | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
318 | 1.58k | ok = ok && (EC_KEY_get0_public_key(ec) != NULL); |
319 | 1.58k | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
320 | 0 | ok = ok && (EC_KEY_get0_private_key(ec) != NULL); |
321 | 1.58k | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
322 | 0 | 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 | 1.58k | return ok; |
329 | 1.58k | } |
330 | | |
331 | | static int ec_match(const void *keydata1, const void *keydata2, int selection) |
332 | 863 | { |
333 | 863 | const EC_KEY *ec1 = keydata1; |
334 | 863 | const EC_KEY *ec2 = keydata2; |
335 | 863 | const EC_GROUP *group_a = EC_KEY_get0_group(ec1); |
336 | 863 | const EC_GROUP *group_b = EC_KEY_get0_group(ec2); |
337 | 863 | BN_CTX *ctx = NULL; |
338 | 863 | int ok = 1; |
339 | | |
340 | 863 | if (!ossl_prov_is_running()) |
341 | 0 | return 0; |
342 | | |
343 | 863 | ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1)); |
344 | 863 | if (ctx == NULL) |
345 | 0 | return 0; |
346 | | |
347 | 863 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
348 | 863 | ok = ok && group_a != NULL && group_b != NULL |
349 | 863 | && EC_GROUP_cmp(group_a, group_b, ctx) == 0; |
350 | 863 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
351 | 863 | int key_checked = 0; |
352 | | |
353 | 863 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
354 | 863 | const EC_POINT *pa = EC_KEY_get0_public_key(ec1); |
355 | 863 | const EC_POINT *pb = EC_KEY_get0_public_key(ec2); |
356 | | |
357 | 863 | if (pa != NULL && pb != NULL) { |
358 | 721 | ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0; |
359 | 721 | key_checked = 1; |
360 | 721 | } |
361 | 863 | } |
362 | 863 | if (!key_checked |
363 | 863 | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
364 | 142 | const BIGNUM *pa = EC_KEY_get0_private_key(ec1); |
365 | 142 | const BIGNUM *pb = EC_KEY_get0_private_key(ec2); |
366 | | |
367 | 142 | if (pa != NULL && pb != NULL) { |
368 | 0 | ok = ok && BN_cmp(pa, pb) == 0; |
369 | 0 | key_checked = 1; |
370 | 0 | } |
371 | 142 | } |
372 | 863 | ok = ok && key_checked; |
373 | 863 | } |
374 | 863 | BN_CTX_free(ctx); |
375 | 863 | return ok; |
376 | 863 | } |
377 | | |
378 | | static int common_check_sm2(const EC_KEY *ec, int sm2_wanted) |
379 | 863 | { |
380 | 863 | 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 | 863 | if ((ecg = EC_KEY_get0_group(ec)) == NULL |
387 | 863 | || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2))) |
388 | 0 | return 0; |
389 | 863 | return 1; |
390 | 863 | } |
391 | | |
392 | | static |
393 | | int common_import(void *keydata, int selection, const OSSL_PARAM params[], |
394 | | int sm2_wanted) |
395 | 0 | { |
396 | 0 | EC_KEY *ec = keydata; |
397 | 0 | int ok = 1; |
398 | |
|
399 | 0 | if (!ossl_prov_is_running() || ec == NULL) |
400 | 0 | 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 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0) |
416 | 0 | return 0; |
417 | | |
418 | 0 | ok = ok && ossl_ec_group_fromdata(ec, params); |
419 | |
|
420 | 0 | if (!common_check_sm2(ec, sm2_wanted)) |
421 | 0 | return 0; |
422 | | |
423 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
424 | 0 | int include_private = |
425 | 0 | selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
426 | |
|
427 | 0 | ok = ok && ossl_ec_key_fromdata(ec, params, include_private); |
428 | 0 | } |
429 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) |
430 | 0 | ok = ok && ossl_ec_key_otherparams_fromdata(ec, params); |
431 | |
|
432 | 0 | return ok; |
433 | 0 | } |
434 | | |
435 | | static |
436 | | int ec_import(void *keydata, int selection, const OSSL_PARAM params[]) |
437 | 0 | { |
438 | 0 | return common_import(keydata, selection, params, 0); |
439 | 0 | } |
440 | | |
441 | | #ifndef FIPS_MODULE |
442 | | # ifndef OPENSSL_NO_SM2 |
443 | | static |
444 | | int sm2_import(void *keydata, int selection, const OSSL_PARAM params[]) |
445 | 0 | { |
446 | 0 | return common_import(keydata, selection, params, 1); |
447 | 0 | } |
448 | | # endif |
449 | | #endif |
450 | | |
451 | | static |
452 | | int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
453 | | void *cbarg) |
454 | 0 | { |
455 | 0 | EC_KEY *ec = keydata; |
456 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
457 | 0 | OSSL_PARAM *params = NULL; |
458 | 0 | unsigned char *pub_key = NULL, *genbuf = NULL; |
459 | 0 | BN_CTX *bnctx = NULL; |
460 | 0 | int ok = 1; |
461 | |
|
462 | 0 | if (!ossl_prov_is_running() || ec == NULL) |
463 | 0 | 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 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0) |
479 | 0 | return 0; |
480 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 |
481 | 0 | && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0) |
482 | 0 | return 0; |
483 | | |
484 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
485 | 0 | if (tmpl == NULL) |
486 | 0 | return 0; |
487 | | |
488 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
489 | 0 | bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec)); |
490 | 0 | if (bnctx == NULL) { |
491 | 0 | ok = 0; |
492 | 0 | goto end; |
493 | 0 | } |
494 | 0 | BN_CTX_start(bnctx); |
495 | 0 | ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL, |
496 | 0 | ossl_ec_key_get_libctx(ec), |
497 | 0 | ossl_ec_key_get0_propq(ec), |
498 | 0 | bnctx, &genbuf); |
499 | 0 | } |
500 | | |
501 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
502 | 0 | int include_private = |
503 | 0 | selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
504 | |
|
505 | 0 | ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key); |
506 | 0 | } |
507 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) |
508 | 0 | ok = ok && otherparams_to_params(ec, tmpl, NULL); |
509 | |
|
510 | 0 | if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
511 | 0 | ok = 0; |
512 | 0 | goto end; |
513 | 0 | } |
514 | | |
515 | 0 | ok = param_cb(params, cbarg); |
516 | 0 | OSSL_PARAM_free(params); |
517 | 0 | end: |
518 | 0 | OSSL_PARAM_BLD_free(tmpl); |
519 | 0 | OPENSSL_free(pub_key); |
520 | 0 | OPENSSL_free(genbuf); |
521 | 0 | BN_CTX_end(bnctx); |
522 | 0 | BN_CTX_free(bnctx); |
523 | 0 | return ok; |
524 | 0 | } |
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 |
560 | | const OSSL_PARAM *ec_imexport_types(int selection) |
561 | 0 | { |
562 | 0 | int type_select = 0; |
563 | |
|
564 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
565 | 0 | type_select += 1; |
566 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
567 | 0 | type_select += 2; |
568 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
569 | 0 | type_select += 4; |
570 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) |
571 | 0 | type_select += 8; |
572 | 0 | return ec_types[type_select]; |
573 | 0 | } |
574 | | |
575 | | static |
576 | | const OSSL_PARAM *ec_import_types(int selection) |
577 | 0 | { |
578 | 0 | return ec_imexport_types(selection); |
579 | 0 | } |
580 | | |
581 | | static |
582 | | const OSSL_PARAM *ec_export_types(int selection) |
583 | 0 | { |
584 | 0 | return ec_imexport_types(selection); |
585 | 0 | } |
586 | | |
587 | | static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[]) |
588 | 1.72k | { |
589 | | #ifdef OPENSSL_NO_EC2M |
590 | | return 1; |
591 | | #else |
592 | 1.72k | int ret = 0, m; |
593 | 1.72k | unsigned int k1 = 0, k2 = 0, k3 = 0; |
594 | 1.72k | int basis_nid; |
595 | 1.72k | const char *basis_name = NULL; |
596 | 1.72k | int fid = EC_GROUP_get_field_type(group); |
597 | | |
598 | 1.72k | if (fid != NID_X9_62_characteristic_two_field) |
599 | 1.06k | return 1; |
600 | | |
601 | 658 | basis_nid = EC_GROUP_get_basis_type(group); |
602 | 658 | if (basis_nid == NID_X9_62_tpBasis) |
603 | 388 | basis_name = SN_X9_62_tpBasis; |
604 | 270 | else if (basis_nid == NID_X9_62_ppBasis) |
605 | 270 | basis_name = SN_X9_62_ppBasis; |
606 | 0 | else |
607 | 0 | goto err; |
608 | | |
609 | 658 | m = EC_GROUP_get_degree(group); |
610 | 658 | if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m) |
611 | 658 | || !ossl_param_build_set_utf8_string(NULL, params, |
612 | 658 | OSSL_PKEY_PARAM_EC_CHAR2_TYPE, |
613 | 658 | basis_name)) |
614 | 0 | goto err; |
615 | | |
616 | 658 | if (basis_nid == NID_X9_62_tpBasis) { |
617 | 388 | if (!EC_GROUP_get_trinomial_basis(group, &k1) |
618 | 388 | || !ossl_param_build_set_int(NULL, params, |
619 | 388 | OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, |
620 | 388 | (int)k1)) |
621 | 0 | goto err; |
622 | 388 | } else { |
623 | 270 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3) |
624 | 270 | || !ossl_param_build_set_int(NULL, params, |
625 | 270 | OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1) |
626 | 270 | || !ossl_param_build_set_int(NULL, params, |
627 | 270 | OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2) |
628 | 270 | || !ossl_param_build_set_int(NULL, params, |
629 | 270 | OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3)) |
630 | 0 | goto err; |
631 | 270 | } |
632 | 658 | ret = 1; |
633 | 658 | err: |
634 | 658 | return ret; |
635 | 658 | #endif /* OPENSSL_NO_EC2M */ |
636 | 658 | } |
637 | | |
638 | | static |
639 | | int common_get_params(void *key, OSSL_PARAM params[], int sm2) |
640 | 1.72k | { |
641 | 1.72k | int ret = 0; |
642 | 1.72k | EC_KEY *eck = key; |
643 | 1.72k | const EC_GROUP *ecg = NULL; |
644 | 1.72k | OSSL_PARAM *p; |
645 | 1.72k | unsigned char *pub_key = NULL, *genbuf = NULL; |
646 | 1.72k | OSSL_LIB_CTX *libctx; |
647 | 1.72k | const char *propq; |
648 | 1.72k | BN_CTX *bnctx = NULL; |
649 | | |
650 | 1.72k | ecg = EC_KEY_get0_group(eck); |
651 | 1.72k | if (ecg == NULL) { |
652 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET); |
653 | 0 | return 0; |
654 | 0 | } |
655 | | |
656 | 1.72k | libctx = ossl_ec_key_get_libctx(eck); |
657 | 1.72k | propq = ossl_ec_key_get0_propq(eck); |
658 | | |
659 | 1.72k | bnctx = BN_CTX_new_ex(libctx); |
660 | 1.72k | if (bnctx == NULL) |
661 | 0 | return 0; |
662 | 1.72k | BN_CTX_start(bnctx); |
663 | | |
664 | 1.72k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL |
665 | 1.72k | && !OSSL_PARAM_set_int(p, ECDSA_size(eck))) |
666 | 0 | goto err; |
667 | 1.72k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL |
668 | 1.72k | && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg))) |
669 | 0 | goto err; |
670 | 1.72k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) { |
671 | 1.72k | int ecbits, sec_bits; |
672 | | |
673 | 1.72k | 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 | 1.72k | if (ecbits >= 512) |
692 | 152 | sec_bits = 256; |
693 | 1.57k | else if (ecbits >= 384) |
694 | 186 | sec_bits = 192; |
695 | 1.38k | else if (ecbits >= 256) |
696 | 128 | sec_bits = 128; |
697 | 1.26k | else if (ecbits >= 224) |
698 | 344 | sec_bits = 112; |
699 | 916 | else if (ecbits >= 160) |
700 | 288 | sec_bits = 80; |
701 | 628 | else |
702 | 628 | sec_bits = ecbits / 2; |
703 | | |
704 | 1.72k | if (!OSSL_PARAM_set_int(p, sec_bits)) |
705 | 0 | goto err; |
706 | 1.72k | } |
707 | 1.72k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_CATEGORY)) != NULL) |
708 | 1.72k | if (!OSSL_PARAM_set_int(p, 0)) |
709 | 0 | goto err; |
710 | | |
711 | 1.72k | if ((p = OSSL_PARAM_locate(params, |
712 | 1.72k | OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS)) |
713 | 1.72k | != NULL) { |
714 | 0 | int explicitparams = EC_KEY_decoded_from_explicit_params(eck); |
715 | |
|
716 | 0 | if (explicitparams < 0 |
717 | 0 | || !OSSL_PARAM_set_int(p, explicitparams)) |
718 | 0 | goto err; |
719 | 0 | } |
720 | | |
721 | 1.72k | if (!sm2) { |
722 | 1.70k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL |
723 | 1.70k | && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD)) |
724 | 0 | goto err; |
725 | 1.70k | } else { |
726 | 20 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL |
727 | 20 | && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD)) |
728 | 0 | goto err; |
729 | 20 | } |
730 | | |
731 | | /* SM2 doesn't support this PARAM */ |
732 | 1.72k | if (!sm2) { |
733 | 1.70k | p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH); |
734 | 1.70k | if (p != NULL) { |
735 | 0 | int ecdh_cofactor_mode = 0; |
736 | |
|
737 | 0 | ecdh_cofactor_mode = |
738 | 0 | (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; |
739 | |
|
740 | 0 | if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode)) |
741 | 0 | goto err; |
742 | 0 | } |
743 | 1.70k | } |
744 | 1.72k | if ((p = OSSL_PARAM_locate(params, |
745 | 1.72k | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) { |
746 | 0 | const EC_POINT *ecp = EC_KEY_get0_public_key(key); |
747 | |
|
748 | 0 | if (ecp == NULL) { |
749 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); |
750 | 0 | goto err; |
751 | 0 | } |
752 | 0 | p->return_size = EC_POINT_point2oct(ecg, ecp, |
753 | 0 | POINT_CONVERSION_UNCOMPRESSED, |
754 | 0 | p->data, p->data_size, bnctx); |
755 | 0 | if (p->return_size == 0) |
756 | 0 | goto err; |
757 | 0 | } |
758 | | |
759 | 1.72k | ret = ec_get_ecm_params(ecg, params) |
760 | 1.72k | && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx, |
761 | 1.72k | &genbuf) |
762 | 1.72k | && key_to_params(eck, NULL, params, 1, &pub_key) |
763 | 1.72k | && otherparams_to_params(eck, NULL, params); |
764 | 1.72k | err: |
765 | 1.72k | OPENSSL_free(genbuf); |
766 | 1.72k | OPENSSL_free(pub_key); |
767 | 1.72k | BN_CTX_end(bnctx); |
768 | 1.72k | BN_CTX_free(bnctx); |
769 | 1.72k | return ret; |
770 | 1.72k | } |
771 | | |
772 | | static |
773 | | int ec_get_params(void *key, OSSL_PARAM params[]) |
774 | 1.70k | { |
775 | 1.70k | return common_get_params(key, params, 0); |
776 | 1.70k | } |
777 | | |
778 | | #ifndef OPENSSL_NO_EC2M |
779 | | # define EC2M_GETTABLE_DOM_PARAMS \ |
780 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \ |
781 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \ |
782 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \ |
783 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \ |
784 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \ |
785 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL), |
786 | | #else |
787 | | # define EC2M_GETTABLE_DOM_PARAMS |
788 | | #endif |
789 | | |
790 | | static const OSSL_PARAM ec_known_gettable_params[] = { |
791 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
792 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
793 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
794 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL), |
795 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), |
796 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
797 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL), |
798 | | EC_IMEXPORTABLE_DOM_PARAMETERS, |
799 | | EC2M_GETTABLE_DOM_PARAMS |
800 | | EC_IMEXPORTABLE_PUBLIC_KEY, |
801 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0), |
802 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0), |
803 | | EC_IMEXPORTABLE_PRIVATE_KEY, |
804 | | EC_IMEXPORTABLE_OTHER_PARAMETERS, |
805 | | OSSL_PARAM_END |
806 | | }; |
807 | | |
808 | | static |
809 | | const OSSL_PARAM *ec_gettable_params(void *provctx) |
810 | 0 | { |
811 | 0 | return ec_known_gettable_params; |
812 | 0 | } |
813 | | |
814 | | static const OSSL_PARAM ec_known_settable_params[] = { |
815 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), |
816 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
817 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), |
818 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), |
819 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), |
820 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL), |
821 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0), |
822 | | OSSL_PARAM_END |
823 | | }; |
824 | | |
825 | | static |
826 | | const OSSL_PARAM *ec_settable_params(void *provctx) |
827 | 0 | { |
828 | 0 | return ec_known_settable_params; |
829 | 0 | } |
830 | | |
831 | | static |
832 | | int ec_set_params(void *key, const OSSL_PARAM params[]) |
833 | 0 | { |
834 | 0 | EC_KEY *eck = key; |
835 | 0 | const OSSL_PARAM *p; |
836 | |
|
837 | 0 | if (key == NULL) |
838 | 0 | return 0; |
839 | 0 | if (ossl_param_is_empty(params)) |
840 | 0 | return 1; |
841 | | |
842 | 0 | if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params)) |
843 | 0 | return 0; |
844 | | |
845 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY); |
846 | 0 | if (p != NULL) { |
847 | 0 | BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key)); |
848 | 0 | int ret = 1; |
849 | |
|
850 | 0 | if (ctx == NULL |
851 | 0 | || p->data_type != OSSL_PARAM_OCTET_STRING |
852 | 0 | || !EC_KEY_oct2key(key, p->data, p->data_size, ctx)) |
853 | 0 | ret = 0; |
854 | 0 | BN_CTX_free(ctx); |
855 | 0 | if (!ret) |
856 | 0 | return 0; |
857 | 0 | } |
858 | | |
859 | 0 | return ossl_ec_key_otherparams_fromdata(eck, params); |
860 | 0 | } |
861 | | |
862 | | #ifndef FIPS_MODULE |
863 | | # ifndef OPENSSL_NO_SM2 |
864 | | static |
865 | | int sm2_get_params(void *key, OSSL_PARAM params[]) |
866 | 20 | { |
867 | 20 | return common_get_params(key, params, 1); |
868 | 20 | } |
869 | | |
870 | | static const OSSL_PARAM sm2_known_gettable_params[] = { |
871 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
872 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
873 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
874 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), |
875 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
876 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL), |
877 | | EC_IMEXPORTABLE_DOM_PARAMETERS, |
878 | | EC_IMEXPORTABLE_PUBLIC_KEY, |
879 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0), |
880 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0), |
881 | | EC_IMEXPORTABLE_PRIVATE_KEY, |
882 | | OSSL_PARAM_END |
883 | | }; |
884 | | |
885 | | static |
886 | | const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx) |
887 | 0 | { |
888 | 0 | return sm2_known_gettable_params; |
889 | 0 | } |
890 | | |
891 | | static const OSSL_PARAM sm2_known_settable_params[] = { |
892 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
893 | | OSSL_PARAM_END |
894 | | }; |
895 | | |
896 | | static |
897 | | const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx) |
898 | 0 | { |
899 | 0 | return sm2_known_settable_params; |
900 | 0 | } |
901 | | |
902 | | static |
903 | | int sm2_validate(const void *keydata, int selection, int checktype) |
904 | 40 | { |
905 | 40 | const EC_KEY *eck = keydata; |
906 | 40 | int ok = 1; |
907 | 40 | BN_CTX *ctx = NULL; |
908 | | |
909 | 40 | if (!ossl_prov_is_running()) |
910 | 0 | return 0; |
911 | | |
912 | 40 | if ((selection & EC_POSSIBLE_SELECTIONS) == 0) |
913 | 0 | return 1; /* nothing to validate */ |
914 | | |
915 | 40 | ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck)); |
916 | 40 | if (ctx == NULL) |
917 | 0 | return 0; |
918 | | |
919 | 40 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
920 | 10 | ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx); |
921 | | |
922 | 40 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
923 | 20 | if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) |
924 | 0 | ok = ok && ossl_ec_key_public_check_quick(eck, ctx); |
925 | 20 | else |
926 | 20 | ok = ok && ossl_ec_key_public_check(eck, ctx); |
927 | 20 | } |
928 | | |
929 | 40 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
930 | 20 | ok = ok && ossl_sm2_key_private_check(eck); |
931 | | |
932 | 40 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
933 | 10 | ok = ok && ossl_ec_key_pairwise_check(eck, ctx); |
934 | | |
935 | 40 | BN_CTX_free(ctx); |
936 | 40 | return ok; |
937 | 40 | } |
938 | | # endif |
939 | | #endif |
940 | | |
941 | | static |
942 | | int ec_validate(const void *keydata, int selection, int checktype) |
943 | 3.41k | { |
944 | 3.41k | const EC_KEY *eck = keydata; |
945 | 3.41k | int ok = 1; |
946 | 3.41k | BN_CTX *ctx = NULL; |
947 | | |
948 | 3.41k | if (!ossl_prov_is_running()) |
949 | 0 | return 0; |
950 | | |
951 | 3.41k | if ((selection & EC_POSSIBLE_SELECTIONS) == 0) |
952 | 0 | return 1; /* nothing to validate */ |
953 | | |
954 | 3.41k | ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck)); |
955 | 3.41k | if (ctx == NULL) |
956 | 0 | return 0; |
957 | | |
958 | 3.41k | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
959 | 853 | int flags = EC_KEY_get_flags(eck); |
960 | | |
961 | 853 | if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0) |
962 | 0 | ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck), |
963 | 0 | (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0; |
964 | 853 | else |
965 | 853 | ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx); |
966 | 853 | } |
967 | | |
968 | 3.41k | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
969 | 1.70k | if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) |
970 | 0 | ok = ok && ossl_ec_key_public_check_quick(eck, ctx); |
971 | 1.70k | else |
972 | 1.70k | ok = ok && ossl_ec_key_public_check(eck, ctx); |
973 | 1.70k | } |
974 | | |
975 | 3.41k | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
976 | 1.70k | ok = ok && ossl_ec_key_private_check(eck); |
977 | | |
978 | 3.41k | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
979 | 853 | ok = ok && ossl_ec_key_pairwise_check(eck, ctx); |
980 | | |
981 | 3.41k | BN_CTX_free(ctx); |
982 | 3.41k | return ok; |
983 | 3.41k | } |
984 | | |
985 | | struct ec_gen_ctx { |
986 | | OSSL_LIB_CTX *libctx; |
987 | | char *group_name; |
988 | | char *encoding; |
989 | | char *pt_format; |
990 | | char *group_check; |
991 | | char *field_type; |
992 | | BIGNUM *p, *a, *b, *order, *cofactor; |
993 | | unsigned char *gen, *seed; |
994 | | size_t gen_len, seed_len; |
995 | | int selection; |
996 | | int ecdh_mode; |
997 | | EC_GROUP *gen_group; |
998 | | unsigned char *dhkem_ikm; |
999 | | size_t dhkem_ikmlen; |
1000 | | OSSL_FIPS_IND_DECLARE |
1001 | | }; |
1002 | | |
1003 | | static void *ec_gen_init(void *provctx, int selection, |
1004 | | const OSSL_PARAM params[]) |
1005 | 0 | { |
1006 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
1007 | 0 | struct ec_gen_ctx *gctx = NULL; |
1008 | |
|
1009 | 0 | if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0) |
1010 | 0 | return NULL; |
1011 | | |
1012 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
1013 | 0 | gctx->libctx = libctx; |
1014 | 0 | gctx->selection = selection; |
1015 | 0 | gctx->ecdh_mode = 0; |
1016 | 0 | OSSL_FIPS_IND_INIT(gctx) |
1017 | 0 | if (!ec_gen_set_params(gctx, params)) { |
1018 | 0 | OPENSSL_free(gctx); |
1019 | 0 | gctx = NULL; |
1020 | 0 | } |
1021 | 0 | } |
1022 | 0 | return gctx; |
1023 | 0 | } |
1024 | | |
1025 | | #ifndef FIPS_MODULE |
1026 | | # ifndef OPENSSL_NO_SM2 |
1027 | | static void *sm2_gen_init(void *provctx, int selection, |
1028 | | const OSSL_PARAM params[]) |
1029 | 0 | { |
1030 | 0 | struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params); |
1031 | |
|
1032 | 0 | if (gctx != NULL) { |
1033 | 0 | if (gctx->group_name != NULL) |
1034 | 0 | return gctx; |
1035 | 0 | if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL) |
1036 | 0 | return gctx; |
1037 | 0 | ec_gen_cleanup(gctx); |
1038 | 0 | } |
1039 | 0 | return NULL; |
1040 | 0 | } |
1041 | | # endif |
1042 | | #endif |
1043 | | |
1044 | | static int ec_gen_set_group(void *genctx, const EC_GROUP *src) |
1045 | 0 | { |
1046 | 0 | struct ec_gen_ctx *gctx = genctx; |
1047 | 0 | EC_GROUP *group; |
1048 | |
|
1049 | 0 | group = EC_GROUP_dup(src); |
1050 | 0 | if (group == NULL) { |
1051 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE); |
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | 0 | EC_GROUP_free(gctx->gen_group); |
1055 | 0 | gctx->gen_group = group; |
1056 | 0 | return 1; |
1057 | 0 | } |
1058 | | |
1059 | | static int ec_gen_set_template(void *genctx, void *templ) |
1060 | 0 | { |
1061 | 0 | struct ec_gen_ctx *gctx = genctx; |
1062 | 0 | EC_KEY *ec = templ; |
1063 | 0 | const EC_GROUP *ec_group; |
1064 | |
|
1065 | 0 | if (!ossl_prov_is_running() || gctx == NULL || ec == NULL) |
1066 | 0 | return 0; |
1067 | 0 | if ((ec_group = EC_KEY_get0_group(ec)) == NULL) |
1068 | 0 | return 0; |
1069 | 0 | return ec_gen_set_group(gctx, ec_group); |
1070 | 0 | } |
1071 | | |
1072 | 0 | #define COPY_INT_PARAM(params, key, val) \ |
1073 | 0 | p = OSSL_PARAM_locate_const(params, key); \ |
1074 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \ |
1075 | 0 | goto err; |
1076 | | |
1077 | 0 | #define COPY_UTF8_PARAM(params, key, val) \ |
1078 | 0 | p = OSSL_PARAM_locate_const(params, key); \ |
1079 | 0 | if (p != NULL) { \ |
1080 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) \ |
1081 | 0 | goto err; \ |
1082 | 0 | OPENSSL_free(val); \ |
1083 | 0 | val = OPENSSL_strdup(p->data); \ |
1084 | 0 | if (val == NULL) \ |
1085 | 0 | goto err; \ |
1086 | 0 | } |
1087 | | |
1088 | 0 | #define COPY_OCTET_PARAM(params, key, val, len) \ |
1089 | 0 | p = OSSL_PARAM_locate_const(params, key); \ |
1090 | 0 | if (p != NULL) { \ |
1091 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) \ |
1092 | 0 | goto err; \ |
1093 | 0 | OPENSSL_free(val); \ |
1094 | 0 | len = p->data_size; \ |
1095 | 0 | val = OPENSSL_memdup(p->data, p->data_size); \ |
1096 | 0 | if (val == NULL) \ |
1097 | 0 | goto err; \ |
1098 | 0 | } |
1099 | | |
1100 | 0 | #define COPY_BN_PARAM(params, key, bn) \ |
1101 | 0 | p = OSSL_PARAM_locate_const(params, key); \ |
1102 | 0 | if (p != NULL) { \ |
1103 | 0 | if (bn == NULL) \ |
1104 | 0 | bn = BN_new(); \ |
1105 | 0 | if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \ |
1106 | 0 | goto err; \ |
1107 | 0 | } |
1108 | | |
1109 | | static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
1110 | 0 | { |
1111 | 0 | int ret = 0; |
1112 | 0 | struct ec_gen_ctx *gctx = genctx; |
1113 | 0 | const OSSL_PARAM *p; |
1114 | |
|
1115 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params, |
1116 | 0 | OSSL_PKEY_PARAM_FIPS_KEY_CHECK)) |
1117 | 0 | goto err; |
1118 | | |
1119 | 0 | COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode); |
1120 | |
|
1121 | 0 | COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name); |
1122 | 0 | COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type); |
1123 | 0 | COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding); |
1124 | 0 | COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format); |
1125 | 0 | COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check); |
1126 | |
|
1127 | 0 | COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p); |
1128 | 0 | COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a); |
1129 | 0 | COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b); |
1130 | 0 | COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order); |
1131 | 0 | COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor); |
1132 | |
|
1133 | 0 | COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len); |
1134 | 0 | COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen, |
1135 | 0 | gctx->gen_len); |
1136 | |
|
1137 | 0 | COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm, |
1138 | 0 | gctx->dhkem_ikmlen); |
1139 | |
|
1140 | 0 | ret = 1; |
1141 | 0 | err: |
1142 | 0 | return ret; |
1143 | 0 | } |
1144 | | |
1145 | | static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx) |
1146 | 0 | { |
1147 | 0 | int ret = 0; |
1148 | 0 | OSSL_PARAM_BLD *bld; |
1149 | 0 | OSSL_PARAM *params = NULL; |
1150 | 0 | EC_GROUP *group = NULL; |
1151 | |
|
1152 | 0 | bld = OSSL_PARAM_BLD_new(); |
1153 | 0 | if (bld == NULL) |
1154 | 0 | return 0; |
1155 | | |
1156 | 0 | if (gctx->encoding != NULL |
1157 | 0 | && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING, |
1158 | 0 | gctx->encoding, 0)) |
1159 | 0 | goto err; |
1160 | | |
1161 | 0 | if (gctx->pt_format != NULL |
1162 | 0 | && !OSSL_PARAM_BLD_push_utf8_string(bld, |
1163 | 0 | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, |
1164 | 0 | gctx->pt_format, 0)) |
1165 | 0 | goto err; |
1166 | | |
1167 | 0 | if (gctx->group_name != NULL) { |
1168 | 0 | if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, |
1169 | 0 | gctx->group_name, 0)) |
1170 | 0 | goto err; |
1171 | | /* Ignore any other parameters if there is a group name */ |
1172 | 0 | goto build; |
1173 | 0 | } else if (gctx->field_type != NULL) { |
1174 | 0 | if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE, |
1175 | 0 | gctx->field_type, 0)) |
1176 | 0 | goto err; |
1177 | 0 | } else { |
1178 | 0 | goto err; |
1179 | 0 | } |
1180 | 0 | if (gctx->p == NULL |
1181 | 0 | || gctx->a == NULL |
1182 | 0 | || gctx->b == NULL |
1183 | 0 | || gctx->order == NULL |
1184 | 0 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p) |
1185 | 0 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a) |
1186 | 0 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b) |
1187 | 0 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order)) |
1188 | 0 | goto err; |
1189 | | |
1190 | 0 | if (gctx->cofactor != NULL |
1191 | 0 | && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, |
1192 | 0 | gctx->cofactor)) |
1193 | 0 | goto err; |
1194 | | |
1195 | 0 | if (gctx->seed != NULL |
1196 | 0 | && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED, |
1197 | 0 | gctx->seed, gctx->seed_len)) |
1198 | 0 | goto err; |
1199 | | |
1200 | 0 | if (gctx->gen == NULL |
1201 | 0 | || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR, |
1202 | 0 | gctx->gen, gctx->gen_len)) |
1203 | 0 | goto err; |
1204 | 0 | build: |
1205 | 0 | params = OSSL_PARAM_BLD_to_param(bld); |
1206 | 0 | if (params == NULL) |
1207 | 0 | goto err; |
1208 | 0 | group = EC_GROUP_new_from_params(params, gctx->libctx, NULL); |
1209 | 0 | if (group == NULL) |
1210 | 0 | goto err; |
1211 | | |
1212 | 0 | EC_GROUP_free(gctx->gen_group); |
1213 | 0 | gctx->gen_group = group; |
1214 | |
|
1215 | 0 | ret = 1; |
1216 | 0 | err: |
1217 | 0 | OSSL_PARAM_free(params); |
1218 | 0 | OSSL_PARAM_BLD_free(bld); |
1219 | 0 | return ret; |
1220 | 0 | } |
1221 | | |
1222 | | static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx, |
1223 | | ossl_unused void *provctx) |
1224 | 0 | { |
1225 | 0 | static OSSL_PARAM settable[] = { |
1226 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), |
1227 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), |
1228 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), |
1229 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), |
1230 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), |
1231 | 0 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), |
1232 | 0 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), |
1233 | 0 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), |
1234 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), |
1235 | 0 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), |
1236 | 0 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), |
1237 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), |
1238 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0), |
1239 | 0 | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_KEY_CHECK) |
1240 | 0 | OSSL_PARAM_END |
1241 | 0 | }; |
1242 | 0 | return settable; |
1243 | 0 | } |
1244 | | |
1245 | | static const OSSL_PARAM *ec_gen_gettable_params(ossl_unused void *genctx, |
1246 | | ossl_unused void *provctx) |
1247 | 0 | { |
1248 | 0 | static const OSSL_PARAM known_ec_gen_gettable_ctx_params[] = { |
1249 | 0 | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
1250 | 0 | OSSL_PARAM_END |
1251 | 0 | }; |
1252 | 0 | return known_ec_gen_gettable_ctx_params; |
1253 | 0 | } |
1254 | | |
1255 | | static int ec_gen_get_params(void *genctx, OSSL_PARAM *params) |
1256 | 0 | { |
1257 | 0 | struct ec_gen_ctx *gctx = genctx; |
1258 | |
|
1259 | 0 | if (gctx == NULL) |
1260 | 0 | return 0; |
1261 | | |
1262 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params)) |
1263 | 0 | return 0; |
1264 | | |
1265 | 0 | return 1; |
1266 | 0 | } |
1267 | | |
1268 | | static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group) |
1269 | 0 | { |
1270 | 0 | if (group == NULL) { |
1271 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET); |
1272 | 0 | return 0; |
1273 | 0 | } |
1274 | 0 | return EC_KEY_set_group(ec, group) > 0; |
1275 | 0 | } |
1276 | | |
1277 | | /* |
1278 | | * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation |
1279 | | */ |
1280 | | static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) |
1281 | 0 | { |
1282 | 0 | struct ec_gen_ctx *gctx = genctx; |
1283 | 0 | EC_KEY *ec = NULL; |
1284 | 0 | int ret = 0; |
1285 | |
|
1286 | 0 | if (!ossl_prov_is_running() |
1287 | 0 | || gctx == NULL |
1288 | 0 | || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL) |
1289 | 0 | return NULL; |
1290 | | |
1291 | 0 | if (gctx->gen_group == NULL) { |
1292 | 0 | if (!ec_gen_set_group_from_params(gctx)) |
1293 | 0 | goto err; |
1294 | 0 | } else { |
1295 | 0 | if (gctx->encoding != NULL) { |
1296 | 0 | int flags = ossl_ec_encoding_name2id(gctx->encoding); |
1297 | |
|
1298 | 0 | if (flags < 0) |
1299 | 0 | goto err; |
1300 | 0 | EC_GROUP_set_asn1_flag(gctx->gen_group, flags); |
1301 | 0 | } |
1302 | 0 | if (gctx->pt_format != NULL) { |
1303 | 0 | int format = ossl_ec_pt_format_name2id(gctx->pt_format); |
1304 | |
|
1305 | 0 | if (format < 0) |
1306 | 0 | goto err; |
1307 | 0 | EC_GROUP_set_point_conversion_form(gctx->gen_group, format); |
1308 | 0 | } |
1309 | 0 | } |
1310 | | #ifdef FIPS_MODULE |
1311 | | if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(gctx), |
1312 | | OSSL_FIPS_IND_SETTABLE0, gctx->libctx, |
1313 | | gctx->gen_group, "EC KeyGen", 1)) |
1314 | | goto err; |
1315 | | #endif |
1316 | | |
1317 | | /* We must always assign a group, no matter what */ |
1318 | 0 | ret = ec_gen_assign_group(ec, gctx->gen_group); |
1319 | | |
1320 | | /* Whether you want it or not, you get a keypair, not just one half */ |
1321 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
1322 | 0 | #ifndef FIPS_MODULE |
1323 | 0 | if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) |
1324 | 0 | ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm, |
1325 | 0 | gctx->dhkem_ikmlen); |
1326 | 0 | else |
1327 | 0 | #endif |
1328 | 0 | ret = ret && EC_KEY_generate_key(ec); |
1329 | 0 | } |
1330 | |
|
1331 | 0 | if (gctx->ecdh_mode != -1) |
1332 | 0 | ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode); |
1333 | |
|
1334 | 0 | if (gctx->group_check != NULL) |
1335 | 0 | ret = ret && ossl_ec_set_check_group_type_from_name(ec, |
1336 | 0 | gctx->group_check); |
1337 | 0 | if (ret) |
1338 | 0 | return ec; |
1339 | 0 | err: |
1340 | | /* Something went wrong, throw the key away */ |
1341 | 0 | EC_KEY_free(ec); |
1342 | 0 | return NULL; |
1343 | 0 | } |
1344 | | |
1345 | | #ifndef FIPS_MODULE |
1346 | | # ifndef OPENSSL_NO_SM2 |
1347 | | /* |
1348 | | * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation |
1349 | | */ |
1350 | | static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) |
1351 | 0 | { |
1352 | 0 | struct ec_gen_ctx *gctx = genctx; |
1353 | 0 | EC_KEY *ec = NULL; |
1354 | 0 | int ret = 1; |
1355 | |
|
1356 | 0 | if (gctx == NULL |
1357 | 0 | || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL) |
1358 | 0 | return NULL; |
1359 | | |
1360 | 0 | if (gctx->gen_group == NULL) { |
1361 | 0 | if (!ec_gen_set_group_from_params(gctx)) |
1362 | 0 | goto err; |
1363 | 0 | } else { |
1364 | 0 | if (gctx->encoding) { |
1365 | 0 | int flags = ossl_ec_encoding_name2id(gctx->encoding); |
1366 | |
|
1367 | 0 | if (flags < 0) |
1368 | 0 | goto err; |
1369 | 0 | EC_GROUP_set_asn1_flag(gctx->gen_group, flags); |
1370 | 0 | } |
1371 | 0 | if (gctx->pt_format != NULL) { |
1372 | 0 | int format = ossl_ec_pt_format_name2id(gctx->pt_format); |
1373 | |
|
1374 | 0 | if (format < 0) |
1375 | 0 | goto err; |
1376 | 0 | EC_GROUP_set_point_conversion_form(gctx->gen_group, format); |
1377 | 0 | } |
1378 | 0 | } |
1379 | | |
1380 | | /* We must always assign a group, no matter what */ |
1381 | 0 | ret = ec_gen_assign_group(ec, gctx->gen_group); |
1382 | | |
1383 | | /* Whether you want it or not, you get a keypair, not just one half */ |
1384 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
1385 | 0 | ret = ret && EC_KEY_generate_key(ec); |
1386 | |
|
1387 | 0 | if (ret) |
1388 | 0 | return ec; |
1389 | 0 | err: |
1390 | | /* Something went wrong, throw the key away */ |
1391 | 0 | EC_KEY_free(ec); |
1392 | 0 | return NULL; |
1393 | 0 | } |
1394 | | # endif |
1395 | | #endif |
1396 | | |
1397 | | static void ec_gen_cleanup(void *genctx) |
1398 | 0 | { |
1399 | 0 | struct ec_gen_ctx *gctx = genctx; |
1400 | |
|
1401 | 0 | if (gctx == NULL) |
1402 | 0 | return; |
1403 | | |
1404 | 0 | OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen); |
1405 | 0 | EC_GROUP_free(gctx->gen_group); |
1406 | 0 | BN_free(gctx->p); |
1407 | 0 | BN_free(gctx->a); |
1408 | 0 | BN_free(gctx->b); |
1409 | 0 | BN_free(gctx->order); |
1410 | 0 | BN_free(gctx->cofactor); |
1411 | 0 | OPENSSL_free(gctx->group_name); |
1412 | 0 | OPENSSL_free(gctx->field_type); |
1413 | 0 | OPENSSL_free(gctx->pt_format); |
1414 | 0 | OPENSSL_free(gctx->encoding); |
1415 | 0 | OPENSSL_free(gctx->seed); |
1416 | 0 | OPENSSL_free(gctx->gen); |
1417 | 0 | OPENSSL_free(gctx); |
1418 | 0 | } |
1419 | | |
1420 | | static void *common_load(const void *reference, size_t reference_sz, |
1421 | | int sm2_wanted) |
1422 | 863 | { |
1423 | 863 | EC_KEY *ec = NULL; |
1424 | | |
1425 | 863 | if (ossl_prov_is_running() && reference_sz == sizeof(ec)) { |
1426 | | /* The contents of the reference is the address to our object */ |
1427 | 863 | ec = *(EC_KEY **)reference; |
1428 | | |
1429 | 863 | if (!common_check_sm2(ec, sm2_wanted)) |
1430 | 0 | return NULL; |
1431 | | |
1432 | | /* We grabbed, so we detach it */ |
1433 | 863 | *(EC_KEY **)reference = NULL; |
1434 | 863 | return ec; |
1435 | 863 | } |
1436 | 0 | return NULL; |
1437 | 863 | } |
1438 | | |
1439 | | static void *ec_load(const void *reference, size_t reference_sz) |
1440 | 853 | { |
1441 | 853 | return common_load(reference, reference_sz, 0); |
1442 | 853 | } |
1443 | | |
1444 | | #ifndef FIPS_MODULE |
1445 | | # ifndef OPENSSL_NO_SM2 |
1446 | | static void *sm2_load(const void *reference, size_t reference_sz) |
1447 | 10 | { |
1448 | 10 | return common_load(reference, reference_sz, 1); |
1449 | 10 | } |
1450 | | # endif |
1451 | | #endif |
1452 | | |
1453 | | static void *ec_dup(const void *keydata_from, int selection) |
1454 | 863 | { |
1455 | 863 | if (ossl_prov_is_running()) |
1456 | 863 | return ossl_ec_key_dup(keydata_from, selection); |
1457 | 0 | return NULL; |
1458 | 863 | } |
1459 | | |
1460 | | const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = { |
1461 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata }, |
1462 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init }, |
1463 | | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, |
1464 | | (void (*)(void))ec_gen_set_template }, |
1465 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params }, |
1466 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
1467 | | (void (*)(void))ec_gen_settable_params }, |
1468 | | { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params }, |
1469 | | { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, |
1470 | | (void (*)(void))ec_gen_gettable_params }, |
1471 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen }, |
1472 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup }, |
1473 | | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load }, |
1474 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata }, |
1475 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params }, |
1476 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params }, |
1477 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params }, |
1478 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params }, |
1479 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has }, |
1480 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match }, |
1481 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate }, |
1482 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import }, |
1483 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types }, |
1484 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export }, |
1485 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types }, |
1486 | | { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, |
1487 | | (void (*)(void))ec_query_operation_name }, |
1488 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup }, |
1489 | | OSSL_DISPATCH_END |
1490 | | }; |
1491 | | |
1492 | | #ifndef FIPS_MODULE |
1493 | | # ifndef OPENSSL_NO_SM2 |
1494 | | const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = { |
1495 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata }, |
1496 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init }, |
1497 | | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, |
1498 | | (void (*)(void))ec_gen_set_template }, |
1499 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params }, |
1500 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
1501 | | (void (*)(void))ec_gen_settable_params }, |
1502 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen }, |
1503 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup }, |
1504 | | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load }, |
1505 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata }, |
1506 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))sm2_get_params }, |
1507 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))sm2_gettable_params }, |
1508 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params }, |
1509 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))sm2_settable_params }, |
1510 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has }, |
1511 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match }, |
1512 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate }, |
1513 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import }, |
1514 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types }, |
1515 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export }, |
1516 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types }, |
1517 | | { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, |
1518 | | (void (*)(void))sm2_query_operation_name }, |
1519 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup }, |
1520 | | OSSL_DISPATCH_END |
1521 | | }; |
1522 | | # endif |
1523 | | #endif |