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