Coverage Report

Created: 2024-09-08 06:24

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