Coverage Report

Created: 2026-07-16 06:59

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