Coverage Report

Created: 2026-02-22 06:11

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-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/core_dispatch.h>
18
#include <openssl/core_names.h>
19
#include <openssl/bn.h>
20
#include <openssl/err.h>
21
#include <openssl/objects.h>
22
#include <openssl/proverr.h>
23
#include <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
0
    point_conversion_form_t format;
254
255
0
    if (ec == NULL)
256
0
        return 0;
257
258
0
    format = EC_KEY_get_conv_form(ec);
259
0
    name = ossl_ec_pt_format_id2name((int)format);
260
0
    if (name != NULL
261
0
        && !ossl_param_build_set_utf8_string(tmpl, params,
262
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
263
0
            name))
264
0
        return 0;
265
266
0
    group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK;
267
0
    name = ossl_ec_check_group_type_id2name(group_check);
268
0
    if (name != NULL
269
0
        && !ossl_param_build_set_utf8_string(tmpl, params,
270
0
            OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
271
0
            name))
272
0
        return 0;
273
274
0
    if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0
275
0
        && !ossl_param_build_set_int(tmpl, params,
276
0
            OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0))
277
0
        return 0;
278
279
0
    ecdh_cofactor_mode = (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
280
0
    return ossl_param_build_set_int(tmpl, params,
281
0
        OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
282
0
        ecdh_cofactor_mode);
283
0
}
284
285
static void *ec_newdata_ex(void *provctx, const OSSL_PARAM params[])
286
0
{
287
0
    EC_KEY *eckey = NULL;
288
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
289
290
0
    if (!ossl_prov_is_running())
291
0
        return NULL;
292
293
0
#ifndef FIPS_MODULE
294
0
    const OSSL_PARAM *p = NULL;
295
296
0
    if (params != NULL)
297
0
        p = OSSL_PARAM_locate_const(params, "legacy-object");
298
299
    /*
300
     * This only works because we are in the default provider. We are not
301
     * normally allowed to pass complex objects across the provider boundary
302
     * like this.
303
     */
304
0
    if (p != NULL && OSSL_PARAM_get_octet_ptr(p, (const void **)&eckey, NULL) && eckey != NULL) {
305
0
#ifdef OPENSSL_NO_EC_EXPLICIT_CURVES
306
0
        if (EC_GROUP_check_named_curve(EC_KEY_get0_group(eckey), 0, NULL) == NID_undef)
307
0
            return NULL;
308
0
#endif
309
0
        if (ossl_lib_ctx_get_concrete(ossl_ec_key_get_libctx(eckey)) != ossl_lib_ctx_get_concrete(libctx))
310
0
            eckey = NULL;
311
0
        else if (!EC_KEY_up_ref(eckey))
312
0
            return NULL;
313
0
    }
314
0
#endif
315
316
0
    if (eckey == NULL)
317
0
        eckey = EC_KEY_new_ex(libctx, NULL);
318
319
0
    return eckey;
320
0
}
321
322
static void *ec_newdata(void *provctx)
323
0
{
324
0
    return ec_newdata_ex(provctx, NULL);
325
0
}
326
327
#ifndef FIPS_MODULE
328
#ifndef OPENSSL_NO_SM2
329
static void *sm2_newdata(void *provctx)
330
0
{
331
0
    if (!ossl_prov_is_running())
332
0
        return NULL;
333
0
    return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2);
334
0
}
335
#endif
336
#endif
337
338
static void ec_freedata(void *keydata)
339
0
{
340
0
    EC_KEY_free(keydata);
341
0
}
342
343
static int ec_has(const void *keydata, int selection)
344
0
{
345
0
    const EC_KEY *ec = keydata;
346
0
    int ok = 1;
347
348
0
    if (!ossl_prov_is_running() || ec == NULL)
349
0
        return 0;
350
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
351
0
        return 1; /* the selection is not missing */
352
353
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
354
0
        ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
355
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
356
0
        ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
357
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
358
0
        ok = ok && (EC_KEY_get0_group(ec) != NULL);
359
    /*
360
     * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
361
     * available, so no extra check is needed other than the previous one
362
     * against EC_POSSIBLE_SELECTIONS.
363
     */
364
0
    return ok;
365
0
}
366
367
static int ec_match(const void *keydata1, const void *keydata2, int selection)
368
0
{
369
0
    const EC_KEY *ec1 = keydata1;
370
0
    const EC_KEY *ec2 = keydata2;
371
0
    const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
372
0
    const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
373
0
    BN_CTX *ctx = NULL;
374
0
    int ok = 1;
375
376
0
    if (!ossl_prov_is_running())
377
0
        return 0;
378
379
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1));
380
0
    if (ctx == NULL)
381
0
        return 0;
382
383
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
384
0
        ok = ok && group_a != NULL && group_b != NULL
385
0
            && EC_GROUP_cmp(group_a, group_b, ctx) == 0;
386
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
387
0
        int key_checked = 0;
388
389
0
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
390
0
            const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
391
0
            const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
392
393
0
            if (pa != NULL && pb != NULL) {
394
0
                ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0;
395
0
                key_checked = 1;
396
0
            }
397
0
        }
398
0
        if (!key_checked
399
0
            && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
400
0
            const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
401
0
            const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
402
403
0
            if (pa != NULL && pb != NULL) {
404
0
                ok = ok && BN_cmp(pa, pb) == 0;
405
0
                key_checked = 1;
406
0
            }
407
0
        }
408
0
        ok = ok && key_checked;
409
0
    }
410
0
    BN_CTX_free(ctx);
411
0
    return ok;
412
0
}
413
414
static int common_check_sm2(const EC_KEY *ec, int sm2_wanted)
415
0
{
416
0
    const EC_GROUP *ecg = NULL;
417
418
    /*
419
     * sm2_wanted: import the keys or domparams only on SM2 Curve
420
     * !sm2_wanted: import the keys or domparams only not on SM2 Curve
421
     */
422
0
    if ((ecg = EC_KEY_get0_group(ec)) == NULL
423
0
        || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2)))
424
0
        return 0;
425
0
    return 1;
426
0
}
427
428
static int common_import(void *keydata, int selection, const OSSL_PARAM params[],
429
    int sm2_wanted)
430
0
{
431
0
    EC_KEY *ec = keydata;
432
0
    int ok = 1;
433
434
0
    if (!ossl_prov_is_running() || ec == NULL)
435
0
        return 0;
436
437
    /*
438
     * In this implementation, we can export/import only keydata in the
439
     * following combinations:
440
     *   - domain parameters (+optional other params)
441
     *   - public key with associated domain parameters (+optional other params)
442
     *   - private key with associated domain parameters and optional public key
443
     *         (+optional other params)
444
     *
445
     * This means:
446
     *   - domain parameters must always be requested
447
     *   - private key must be requested alongside public key
448
     *   - other parameters are always optional
449
     */
450
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
451
0
        return 0;
452
453
0
    ok = ok && ossl_ec_group_fromdata(ec, params);
454
455
0
    if (!common_check_sm2(ec, sm2_wanted))
456
0
        return 0;
457
458
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
459
0
        int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
460
461
0
        ok = ok && ossl_ec_key_fromdata(ec, params, include_private);
462
0
    }
463
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
464
0
        ok = ok && ossl_ec_key_otherparams_fromdata(ec, params);
465
466
0
    return ok;
467
0
}
468
469
static int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
470
0
{
471
0
    return common_import(keydata, selection, params, 0);
472
0
}
473
474
#ifndef FIPS_MODULE
475
#ifndef OPENSSL_NO_SM2
476
static int sm2_import(void *keydata, int selection, const OSSL_PARAM params[])
477
0
{
478
0
    return common_import(keydata, selection, params, 1);
479
0
}
480
#endif
481
#endif
482
483
static int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
484
    void *cbarg)
485
0
{
486
0
    EC_KEY *ec = keydata;
487
0
    OSSL_PARAM_BLD *tmpl = NULL;
488
0
    OSSL_PARAM *params = NULL;
489
0
    unsigned char *pub_key = NULL, *genbuf = NULL;
490
0
    BN_CTX *bnctx = NULL;
491
0
    int ok = 1;
492
493
0
    if (!ossl_prov_is_running() || ec == NULL)
494
0
        return 0;
495
496
    /*
497
     * In this implementation, we can export/import only keydata in the
498
     * following combinations:
499
     *   - domain parameters (+optional other params)
500
     *   - public key with associated domain parameters (+optional other params)
501
     *   - private key with associated public key and domain parameters
502
     *         (+optional other params)
503
     *
504
     * This means:
505
     *   - domain parameters must always be requested
506
     *   - private key must be requested alongside public key
507
     *   - other parameters are always optional
508
     */
509
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
510
0
        return 0;
511
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
512
0
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
513
0
        return 0;
514
515
0
    tmpl = OSSL_PARAM_BLD_new();
516
0
    if (tmpl == NULL)
517
0
        return 0;
518
519
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
520
0
        bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
521
0
        if (bnctx == NULL) {
522
0
            ok = 0;
523
0
            goto end;
524
0
        }
525
0
        BN_CTX_start(bnctx);
526
0
        ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL, ossl_ec_key_get_libctx(ec), ossl_ec_key_get0_propq(ec), bnctx, &genbuf);
527
0
    }
528
529
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
530
0
        int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
531
532
0
        ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
533
0
    }
534
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
535
0
        ok = ok && otherparams_to_params(ec, tmpl, NULL);
536
537
0
    if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
538
0
        ok = 0;
539
0
        goto end;
540
0
    }
541
542
0
    ok = param_cb(params, cbarg);
543
0
    OSSL_PARAM_clear_free(params);
544
0
end:
545
0
    OSSL_PARAM_BLD_free(tmpl);
546
0
    OPENSSL_free(pub_key);
547
0
    OPENSSL_free(genbuf);
548
0
    BN_CTX_end(bnctx);
549
0
    BN_CTX_free(bnctx);
550
0
    return ok;
551
0
}
552
553
/* IMEXPORT = IMPORT + EXPORT */
554
555
#define EC_IMEXPORTABLE_DOM_PARAMETERS                                               \
556
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),                     \
557
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),                \
558
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), \
559
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),              \
560
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),                                \
561
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),                                \
562
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),                                \
563
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),              \
564
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),                            \
565
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),                         \
566
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),                   \
567
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL)
568
569
#define EC_IMEXPORTABLE_PUBLIC_KEY \
570
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
571
#define EC_IMEXPORTABLE_PRIVATE_KEY \
572
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
573
#define EC_IMEXPORTABLE_OTHER_PARAMETERS                     \
574
    OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
575
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
576
577
/*
578
 * Include all the possible combinations of OSSL_PARAM arrays for
579
 * ec_imexport_types().
580
 *
581
 * They are in a separate file as it is ~100 lines of unreadable and
582
 * uninteresting machine generated stuff.
583
 */
584
#include "ec_kmgmt_imexport.inc"
585
586
static ossl_inline const OSSL_PARAM *ec_imexport_types(int selection)
587
0
{
588
0
    int type_select = 0;
589
590
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
591
0
        type_select += 1;
592
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
593
0
        type_select += 2;
594
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
595
0
        type_select += 4;
596
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
597
0
        type_select += 8;
598
0
    return ec_types[type_select];
599
0
}
600
601
static const OSSL_PARAM *ec_import_types(int selection)
602
0
{
603
0
    return ec_imexport_types(selection);
604
0
}
605
606
static const OSSL_PARAM *ec_export_types(int selection)
607
0
{
608
0
    return ec_imexport_types(selection);
609
0
}
610
611
static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
612
0
{
613
#ifdef OPENSSL_NO_EC2M
614
    return 1;
615
#else
616
0
    int ret = 0, m;
617
0
    unsigned int k1 = 0, k2 = 0, k3 = 0;
618
0
    int basis_nid;
619
0
    const char *basis_name = NULL;
620
0
    int fid = EC_GROUP_get_field_type(group);
621
622
0
    if (fid != NID_X9_62_characteristic_two_field)
623
0
        return 1;
624
625
0
    basis_nid = EC_GROUP_get_basis_type(group);
626
0
    if (basis_nid == NID_X9_62_tpBasis)
627
0
        basis_name = SN_X9_62_tpBasis;
628
0
    else if (basis_nid == NID_X9_62_ppBasis)
629
0
        basis_name = SN_X9_62_ppBasis;
630
0
    else
631
0
        goto err;
632
633
0
    m = EC_GROUP_get_degree(group);
634
0
    if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
635
0
        || !ossl_param_build_set_utf8_string(NULL, params,
636
0
            OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
637
0
            basis_name))
638
0
        goto err;
639
640
0
    if (basis_nid == NID_X9_62_tpBasis) {
641
0
        if (!EC_GROUP_get_trinomial_basis(group, &k1)
642
0
            || !ossl_param_build_set_int(NULL, params,
643
0
                OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
644
0
                (int)k1))
645
0
            goto err;
646
0
    } else {
647
0
        if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
648
0
            || !ossl_param_build_set_int(NULL, params,
649
0
                OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
650
0
            || !ossl_param_build_set_int(NULL, params,
651
0
                OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
652
0
            || !ossl_param_build_set_int(NULL, params,
653
0
                OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
654
0
            goto err;
655
0
    }
656
0
    ret = 1;
657
0
err:
658
0
    return ret;
659
0
#endif /* OPENSSL_NO_EC2M */
660
0
}
661
662
static int common_get_params(void *key, OSSL_PARAM params[], int sm2)
663
0
{
664
0
    int ret = 0;
665
0
    EC_KEY *eck = key;
666
0
    const EC_GROUP *ecg = NULL;
667
0
    OSSL_PARAM *p;
668
0
    unsigned char *pub_key = NULL, *genbuf = NULL;
669
0
    OSSL_LIB_CTX *libctx;
670
0
    const char *propq;
671
0
    BN_CTX *bnctx = NULL;
672
673
0
    ecg = EC_KEY_get0_group(eck);
674
0
    if (ecg == NULL) {
675
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
676
0
        return 0;
677
0
    }
678
679
0
    libctx = ossl_ec_key_get_libctx(eck);
680
0
    propq = ossl_ec_key_get0_propq(eck);
681
682
0
    bnctx = BN_CTX_new_ex(libctx);
683
0
    if (bnctx == NULL)
684
0
        return 0;
685
0
    BN_CTX_start(bnctx);
686
687
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
688
0
        && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
689
0
        goto err;
690
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
691
0
        && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
692
0
        goto err;
693
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_FIELD_DEGREE)) != NULL
694
0
        && !OSSL_PARAM_set_int(p, EC_GROUP_get_degree(ecg)))
695
0
        goto err;
696
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
697
0
        && !OSSL_PARAM_set_int(p, EC_GROUP_security_bits(ecg)))
698
0
        goto err;
699
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_CATEGORY)) != NULL)
700
0
        if (!OSSL_PARAM_set_int(p, 0))
701
0
            goto err;
702
703
0
    if ((p = OSSL_PARAM_locate(params,
704
0
             OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS))
705
0
        != NULL) {
706
0
        int explicitparams = EC_KEY_decoded_from_explicit_params(eck);
707
708
0
        if (explicitparams < 0
709
0
            || !OSSL_PARAM_set_int(p, explicitparams))
710
0
            goto err;
711
0
    }
712
713
0
    if (!sm2) {
714
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
715
0
            && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
716
0
            goto err;
717
0
    } else {
718
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
719
0
            && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
720
0
            goto err;
721
0
    }
722
723
    /* SM2 doesn't support this PARAM */
724
0
    if (!sm2) {
725
0
        p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
726
0
        if (p != NULL) {
727
0
            int ecdh_cofactor_mode = 0;
728
729
0
            ecdh_cofactor_mode = (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
730
731
0
            if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
732
0
                goto err;
733
0
        }
734
0
    }
735
0
    if ((p = OSSL_PARAM_locate(params,
736
0
             OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY))
737
0
        != NULL) {
738
0
        const EC_POINT *ecp = EC_KEY_get0_public_key(key);
739
740
0
        if (ecp == NULL) {
741
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
742
0
            goto err;
743
0
        }
744
0
        p->return_size = EC_POINT_point2oct(ecg, ecp,
745
0
            POINT_CONVERSION_UNCOMPRESSED,
746
0
            p->data, p->data_size, bnctx);
747
0
        if (p->return_size == 0)
748
0
            goto err;
749
0
    }
750
751
0
    ret = ec_get_ecm_params(ecg, params)
752
0
        && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx,
753
0
            &genbuf)
754
0
        && key_to_params(eck, NULL, params, 1, &pub_key)
755
0
        && otherparams_to_params(eck, NULL, params);
756
0
err:
757
0
    OPENSSL_free(genbuf);
758
0
    OPENSSL_free(pub_key);
759
0
    BN_CTX_end(bnctx);
760
0
    BN_CTX_free(bnctx);
761
0
    return ret;
762
0
}
763
764
static int ec_get_params(void *key, OSSL_PARAM params[])
765
0
{
766
0
    return common_get_params(key, params, 0);
767
0
}
768
769
#ifndef OPENSSL_NO_EC2M
770
#define EC2M_GETTABLE_DOM_PARAMS                                        \
771
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL),                   \
772
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
773
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL),        \
774
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL),           \
775
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL),           \
776
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
777
#else
778
#define EC2M_GETTABLE_DOM_PARAMS
779
#endif
780
781
static const OSSL_PARAM ec_known_gettable_params[] = {
782
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
783
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_FIELD_DEGREE, NULL),
784
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
785
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
786
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
787
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
788
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
789
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
790
    EC_IMEXPORTABLE_DOM_PARAMETERS,
791
    EC2M_GETTABLE_DOM_PARAMS
792
        EC_IMEXPORTABLE_PUBLIC_KEY,
793
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
794
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
795
    EC_IMEXPORTABLE_PRIVATE_KEY,
796
    EC_IMEXPORTABLE_OTHER_PARAMETERS,
797
    OSSL_PARAM_END
798
};
799
800
static const OSSL_PARAM *ec_gettable_params(void *provctx)
801
0
{
802
0
    return ec_known_gettable_params;
803
0
}
804
805
static const OSSL_PARAM ec_known_settable_params[] = {
806
    OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
807
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
808
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
809
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
810
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
811
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
812
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
813
    OSSL_PARAM_END
814
};
815
816
static const OSSL_PARAM *ec_settable_params(void *provctx)
817
0
{
818
0
    return ec_known_settable_params;
819
0
}
820
821
static int ec_set_params(void *key, const OSSL_PARAM params[])
822
0
{
823
0
    EC_KEY *eck = key;
824
0
    const OSSL_PARAM *p;
825
826
0
    if (key == NULL)
827
0
        return 0;
828
0
    if (ossl_param_is_empty(params))
829
0
        return 1;
830
831
0
    if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
832
0
        return 0;
833
834
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
835
0
    if (p != NULL) {
836
0
        BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
837
0
        int ret = 1;
838
839
0
        if (ctx == NULL
840
0
            || p->data_type != OSSL_PARAM_OCTET_STRING
841
0
            || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
842
0
            ret = 0;
843
0
        BN_CTX_free(ctx);
844
0
        if (!ret)
845
0
            return 0;
846
0
    }
847
848
0
    return ossl_ec_key_otherparams_fromdata(eck, params);
849
0
}
850
851
#ifndef FIPS_MODULE
852
#ifndef OPENSSL_NO_SM2
853
static int sm2_get_params(void *key, OSSL_PARAM params[])
854
0
{
855
0
    return common_get_params(key, params, 1);
856
0
}
857
858
static const OSSL_PARAM sm2_known_gettable_params[] = {
859
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
860
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_FIELD_DEGREE, NULL),
861
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
862
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
863
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
864
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
865
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
866
    EC_IMEXPORTABLE_DOM_PARAMETERS,
867
    EC_IMEXPORTABLE_PUBLIC_KEY,
868
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
869
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
870
    EC_IMEXPORTABLE_PRIVATE_KEY,
871
    OSSL_PARAM_END
872
};
873
874
static const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
875
0
{
876
0
    return sm2_known_gettable_params;
877
0
}
878
879
static const OSSL_PARAM sm2_known_settable_params[] = {
880
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
881
    OSSL_PARAM_END
882
};
883
884
static const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
885
0
{
886
0
    return sm2_known_settable_params;
887
0
}
888
889
static int sm2_validate(const void *keydata, int selection, int checktype)
890
0
{
891
0
    const EC_KEY *eck = keydata;
892
0
    int ok = 1;
893
0
    BN_CTX *ctx = NULL;
894
895
0
    if (!ossl_prov_is_running())
896
0
        return 0;
897
898
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
899
0
        return 1; /* nothing to validate */
900
901
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
902
0
    if (ctx == NULL)
903
0
        return 0;
904
905
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
906
0
        ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
907
908
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
909
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
910
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
911
0
        else
912
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
913
0
    }
914
915
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
916
0
        ok = ok && ossl_sm2_key_private_check(eck);
917
918
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
919
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
920
921
0
    BN_CTX_free(ctx);
922
0
    return ok;
923
0
}
924
#endif
925
#endif
926
927
static int ec_validate(const void *keydata, int selection, int checktype)
928
0
{
929
0
    const EC_KEY *eck = keydata;
930
0
    int ok = 1;
931
0
    BN_CTX *ctx = NULL;
932
933
0
    if (!ossl_prov_is_running())
934
0
        return 0;
935
936
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
937
0
        return 1; /* nothing to validate */
938
939
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
940
0
    if (ctx == NULL)
941
0
        return 0;
942
943
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
944
0
        int flags = EC_KEY_get_flags(eck);
945
946
0
        if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
947
0
            ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck), (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0;
948
0
        else
949
0
            ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
950
0
    }
951
952
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
953
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
954
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
955
0
        else
956
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
957
0
    }
958
959
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
960
0
        ok = ok && ossl_ec_key_private_check(eck);
961
962
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
963
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
964
965
0
    BN_CTX_free(ctx);
966
0
    return ok;
967
0
}
968
969
struct ec_gen_ctx {
970
    OSSL_LIB_CTX *libctx;
971
    char *group_name;
972
    char *encoding;
973
    char *pt_format;
974
    char *group_check;
975
    char *field_type;
976
    BIGNUM *p, *a, *b, *order, *cofactor;
977
    unsigned char *gen, *seed;
978
    size_t gen_len, seed_len;
979
    int selection;
980
    int ecdh_mode;
981
    EC_GROUP *gen_group;
982
    unsigned char *dhkem_ikm;
983
    size_t dhkem_ikmlen;
984
    OSSL_FIPS_IND_DECLARE
985
};
986
987
static void *ec_gen_init(void *provctx, int selection,
988
    const OSSL_PARAM params[])
989
0
{
990
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
991
0
    struct ec_gen_ctx *gctx = NULL;
992
993
0
    if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
994
0
        return NULL;
995
996
0
    if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
997
0
        gctx->libctx = libctx;
998
0
        gctx->selection = selection;
999
0
        gctx->ecdh_mode = 0;
1000
0
        OSSL_FIPS_IND_INIT(gctx)
1001
0
        if (!ec_gen_set_params(gctx, params)) {
1002
0
            ec_gen_cleanup(gctx);
1003
0
            gctx = NULL;
1004
0
        }
1005
0
    }
1006
0
    return gctx;
1007
0
}
1008
1009
#ifndef FIPS_MODULE
1010
#ifndef OPENSSL_NO_SM2
1011
static void *sm2_gen_init(void *provctx, int selection,
1012
    const OSSL_PARAM params[])
1013
0
{
1014
0
    struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
1015
1016
0
    if (gctx != NULL) {
1017
0
        if (gctx->group_name != NULL)
1018
0
            return gctx;
1019
0
        if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
1020
0
            return gctx;
1021
0
        ec_gen_cleanup(gctx);
1022
0
    }
1023
0
    return NULL;
1024
0
}
1025
#endif
1026
#endif
1027
1028
static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
1029
0
{
1030
0
    struct ec_gen_ctx *gctx = genctx;
1031
0
    EC_GROUP *group;
1032
1033
0
    group = EC_GROUP_dup(src);
1034
0
    if (group == NULL) {
1035
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
1036
0
        return 0;
1037
0
    }
1038
0
    EC_GROUP_free(gctx->gen_group);
1039
0
    gctx->gen_group = group;
1040
0
    return 1;
1041
0
}
1042
1043
static int ec_gen_set_template(void *genctx, void *templ)
1044
0
{
1045
0
    struct ec_gen_ctx *gctx = genctx;
1046
0
    EC_KEY *ec = templ;
1047
0
    const EC_GROUP *ec_group;
1048
1049
0
    if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
1050
0
        return 0;
1051
0
    if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
1052
0
        return 0;
1053
0
    return ec_gen_set_group(gctx, ec_group);
1054
0
}
1055
1056
#define COPY_INT_PARAM(params, key, val)           \
1057
0
    p = OSSL_PARAM_locate_const(params, key);      \
1058
0
    if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
1059
0
        goto err;
1060
1061
#define COPY_UTF8_PARAM(params, key, val)           \
1062
0
    p = OSSL_PARAM_locate_const(params, key);       \
1063
0
    if (p != NULL) {                                \
1064
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING) \
1065
0
            goto err;                               \
1066
0
        OPENSSL_free(val);                          \
1067
0
        val = OPENSSL_strdup(p->data);              \
1068
0
        if (val == NULL)                            \
1069
0
            goto err;                               \
1070
0
    }
1071
1072
#define COPY_OCTET_PARAM(params, key, val, len)      \
1073
0
    p = OSSL_PARAM_locate_const(params, key);        \
1074
0
    if (p != NULL) {                                 \
1075
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) \
1076
0
            goto err;                                \
1077
0
        OPENSSL_free(val);                           \
1078
0
        len = p->data_size;                          \
1079
0
        val = OPENSSL_memdup(p->data, p->data_size); \
1080
0
        if (val == NULL)                             \
1081
0
            goto err;                                \
1082
0
    }
1083
1084
#define COPY_BN_PARAM(params, key, bn)                \
1085
0
    p = OSSL_PARAM_locate_const(params, key);         \
1086
0
    if (p != NULL) {                                  \
1087
0
        if (bn == NULL)                               \
1088
0
            bn = BN_new();                            \
1089
0
        if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1090
0
            goto err;                                 \
1091
0
    }
1092
1093
static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
1094
0
{
1095
0
    int ret = 0;
1096
0
    struct ec_gen_ctx *gctx = genctx;
1097
0
    const OSSL_PARAM *p;
1098
1099
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params,
1100
0
            OSSL_PKEY_PARAM_FIPS_KEY_CHECK))
1101
0
        goto err;
1102
1103
0
    COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
1104
1105
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
1106
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
1107
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
1108
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
1109
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
1110
1111
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
1112
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
1113
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
1114
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
1115
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
1116
1117
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
1118
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
1119
0
        gctx->gen_len);
1120
1121
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm,
1122
0
        gctx->dhkem_ikmlen);
1123
1124
0
    ret = 1;
1125
0
err:
1126
0
    return ret;
1127
0
}
1128
1129
static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
1130
0
{
1131
0
    int ret = 0;
1132
0
    OSSL_PARAM_BLD *bld;
1133
0
    OSSL_PARAM *params = NULL;
1134
0
    EC_GROUP *group = NULL;
1135
1136
0
    bld = OSSL_PARAM_BLD_new();
1137
0
    if (bld == NULL)
1138
0
        return 0;
1139
1140
0
    if (gctx->encoding != NULL
1141
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
1142
0
            gctx->encoding, 0))
1143
0
        goto err;
1144
1145
0
    if (gctx->pt_format != NULL
1146
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld,
1147
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
1148
0
            gctx->pt_format, 0))
1149
0
        goto err;
1150
1151
0
    if (gctx->group_name != NULL) {
1152
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1153
0
                gctx->group_name, 0))
1154
0
            goto err;
1155
        /* Ignore any other parameters if there is a group name */
1156
0
        goto build;
1157
0
    } else if (gctx->field_type != NULL) {
1158
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1159
0
                gctx->field_type, 0))
1160
0
            goto err;
1161
0
    } else {
1162
0
        goto err;
1163
0
    }
1164
0
    if (gctx->p == NULL
1165
0
        || gctx->a == NULL
1166
0
        || gctx->b == NULL
1167
0
        || gctx->order == NULL
1168
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
1169
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
1170
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
1171
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
1172
0
        goto err;
1173
1174
0
    if (gctx->cofactor != NULL
1175
0
        && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1176
0
            gctx->cofactor))
1177
0
        goto err;
1178
1179
0
    if (gctx->seed != NULL
1180
0
        && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
1181
0
            gctx->seed, gctx->seed_len))
1182
0
        goto err;
1183
1184
0
    if (gctx->gen == NULL
1185
0
        || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
1186
0
            gctx->gen, gctx->gen_len))
1187
0
        goto err;
1188
0
build:
1189
0
    params = OSSL_PARAM_BLD_to_param(bld);
1190
0
    if (params == NULL)
1191
0
        goto err;
1192
0
    group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
1193
0
    if (group == NULL)
1194
0
        goto err;
1195
1196
0
    EC_GROUP_free(gctx->gen_group);
1197
0
    gctx->gen_group = group;
1198
1199
0
    ret = 1;
1200
0
err:
1201
0
    OSSL_PARAM_free(params);
1202
0
    OSSL_PARAM_BLD_free(bld);
1203
0
    return ret;
1204
0
}
1205
1206
static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx,
1207
    ossl_unused void *provctx)
1208
0
{
1209
0
    static const OSSL_PARAM settable[] = {
1210
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
1211
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1212
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
1213
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
1214
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
1215
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
1216
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
1217
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
1218
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
1219
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
1220
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
1221
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
1222
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
1223
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_KEY_CHECK)
1224
0
            OSSL_PARAM_END
1225
0
    };
1226
0
    return settable;
1227
0
}
1228
1229
static const OSSL_PARAM *ec_gen_gettable_params(ossl_unused void *genctx,
1230
    ossl_unused void *provctx)
1231
0
{
1232
0
    static const OSSL_PARAM known_ec_gen_gettable_ctx_params[] = {
1233
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
1234
0
            OSSL_PARAM_END
1235
0
    };
1236
0
    return known_ec_gen_gettable_ctx_params;
1237
0
}
1238
1239
static int ec_gen_get_params(void *genctx, OSSL_PARAM *params)
1240
0
{
1241
0
    struct ec_gen_ctx *gctx = genctx;
1242
1243
0
    if (gctx == NULL)
1244
0
        return 0;
1245
1246
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params))
1247
0
        return 0;
1248
1249
0
    return 1;
1250
0
}
1251
1252
static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
1253
0
{
1254
0
    if (group == NULL) {
1255
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
1256
0
        return 0;
1257
0
    }
1258
0
    return EC_KEY_set_group(ec, group) > 0;
1259
0
}
1260
1261
/*
1262
 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1263
 */
1264
static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1265
0
{
1266
0
    struct ec_gen_ctx *gctx = genctx;
1267
0
    EC_KEY *ec = NULL;
1268
0
    int ret = 0;
1269
1270
0
    if (!ossl_prov_is_running()
1271
0
        || gctx == NULL
1272
0
        || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1273
0
        return NULL;
1274
1275
0
    if (gctx->gen_group == NULL) {
1276
0
        if (!ec_gen_set_group_from_params(gctx))
1277
0
            goto err;
1278
0
    } else {
1279
0
        if (gctx->encoding != NULL) {
1280
0
            int flags = ossl_ec_encoding_name2id(gctx->encoding);
1281
1282
0
            if (flags < 0)
1283
0
                goto err;
1284
0
            EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1285
0
        }
1286
0
        if (gctx->pt_format != NULL) {
1287
0
            int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1288
1289
0
            if (format < 0)
1290
0
                goto err;
1291
0
            EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1292
0
        }
1293
0
    }
1294
#ifdef FIPS_MODULE
1295
    if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(gctx),
1296
            OSSL_FIPS_IND_SETTABLE0, gctx->libctx,
1297
            gctx->gen_group, "EC KeyGen", 1))
1298
        goto err;
1299
#endif
1300
1301
    /* We must always assign a group, no matter what */
1302
0
    ret = ec_gen_assign_group(ec, gctx->gen_group);
1303
1304
    /* Whether you want it or not, you get a keypair, not just one half */
1305
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1306
0
#ifndef FIPS_MODULE
1307
0
        if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0)
1308
0
            ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm, gctx->dhkem_ikmlen);
1309
0
        else
1310
0
#endif
1311
0
            ret = ret && EC_KEY_generate_key(ec);
1312
0
    }
1313
1314
0
    if (gctx->ecdh_mode != -1)
1315
0
        ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
1316
1317
0
    if (gctx->group_check != NULL)
1318
0
        ret = ret && ossl_ec_set_check_group_type_from_name(ec, gctx->group_check);
1319
#ifdef FIPS_MODULE
1320
    if (ret > 0
1321
        && !ossl_fips_self_testing()
1322
        && EC_KEY_get0_public_key(ec) != NULL
1323
        && EC_KEY_get0_private_key(ec) != NULL
1324
        && EC_KEY_get0_group(ec) != NULL) {
1325
        BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
1326
1327
        ret = bnctx != NULL && ossl_ec_key_pairwise_check(ec, bnctx);
1328
        BN_CTX_free(bnctx);
1329
        if (ret <= 0)
1330
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
1331
    }
1332
#endif /* FIPS_MODULE */
1333
1334
0
    if (ret)
1335
0
        return ec;
1336
0
err:
1337
    /* Something went wrong, throw the key away */
1338
0
    EC_KEY_free(ec);
1339
0
    return NULL;
1340
0
}
1341
1342
#ifndef FIPS_MODULE
1343
#ifndef OPENSSL_NO_SM2
1344
/*
1345
 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1346
 */
1347
static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1348
0
{
1349
0
    struct ec_gen_ctx *gctx = genctx;
1350
0
    EC_KEY *ec = NULL;
1351
0
    int ret = 1;
1352
1353
0
    if (gctx == NULL
1354
0
        || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1355
0
        return NULL;
1356
1357
0
    if (gctx->gen_group == NULL) {
1358
0
        if (!ec_gen_set_group_from_params(gctx))
1359
0
            goto err;
1360
0
    } else {
1361
0
        if (gctx->encoding) {
1362
0
            int flags = ossl_ec_encoding_name2id(gctx->encoding);
1363
1364
0
            if (flags < 0)
1365
0
                goto err;
1366
0
            EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1367
0
        }
1368
0
        if (gctx->pt_format != NULL) {
1369
0
            int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1370
1371
0
            if (format < 0)
1372
0
                goto err;
1373
0
            EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1374
0
        }
1375
0
    }
1376
1377
    /* We must always assign a group, no matter what */
1378
0
    ret = ec_gen_assign_group(ec, gctx->gen_group);
1379
1380
    /* Whether you want it or not, you get a keypair, not just one half */
1381
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
1382
0
        ret = ret && EC_KEY_generate_key(ec);
1383
1384
0
    if (ret)
1385
0
        return ec;
1386
0
err:
1387
    /* Something went wrong, throw the key away */
1388
0
    EC_KEY_free(ec);
1389
0
    return NULL;
1390
0
}
1391
#endif
1392
#endif
1393
1394
static void ec_gen_cleanup(void *genctx)
1395
0
{
1396
0
    struct ec_gen_ctx *gctx = genctx;
1397
1398
0
    if (gctx == NULL)
1399
0
        return;
1400
1401
0
    OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen);
1402
0
    EC_GROUP_free(gctx->gen_group);
1403
0
    BN_free(gctx->p);
1404
0
    BN_free(gctx->a);
1405
0
    BN_free(gctx->b);
1406
0
    BN_free(gctx->order);
1407
0
    BN_free(gctx->cofactor);
1408
0
    OPENSSL_free(gctx->group_name);
1409
0
    OPENSSL_free(gctx->field_type);
1410
0
    OPENSSL_free(gctx->pt_format);
1411
0
    OPENSSL_free(gctx->encoding);
1412
0
    OPENSSL_free(gctx->seed);
1413
0
    OPENSSL_free(gctx->gen);
1414
0
    OPENSSL_free(gctx);
1415
0
}
1416
1417
static void *common_load(const void *reference, size_t reference_sz,
1418
    int sm2_wanted)
1419
0
{
1420
0
    EC_KEY *ec = NULL;
1421
1422
0
    if (ossl_prov_is_running() && reference_sz == sizeof(ec)) {
1423
        /* The contents of the reference is the address to our object */
1424
0
        ec = *(EC_KEY **)reference;
1425
1426
0
        if (!common_check_sm2(ec, sm2_wanted))
1427
0
            return NULL;
1428
1429
        /* We grabbed, so we detach it */
1430
0
        *(EC_KEY **)reference = NULL;
1431
0
        return ec;
1432
0
    }
1433
0
    return NULL;
1434
0
}
1435
1436
static void *ec_load(const void *reference, size_t reference_sz)
1437
0
{
1438
0
    return common_load(reference, reference_sz, 0);
1439
0
}
1440
1441
#ifndef FIPS_MODULE
1442
#ifndef OPENSSL_NO_SM2
1443
static void *sm2_load(const void *reference, size_t reference_sz)
1444
0
{
1445
0
    return common_load(reference, reference_sz, 1);
1446
0
}
1447
#endif
1448
#endif
1449
1450
static void *ec_dup(const void *keydata_from, int selection)
1451
0
{
1452
0
    if (ossl_prov_is_running())
1453
0
        return ossl_ec_key_dup(keydata_from, selection);
1454
0
    return NULL;
1455
0
}
1456
1457
const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
1458
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1459
    { OSSL_FUNC_KEYMGMT_NEW_EX, (void (*)(void))ec_newdata_ex },
1460
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1461
    { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1462
        (void (*)(void))ec_gen_set_template },
1463
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1464
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1465
        (void (*)(void))ec_gen_settable_params },
1466
    { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params },
1467
    { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
1468
        (void (*)(void))ec_gen_gettable_params },
1469
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
1470
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1471
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
1472
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1473
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))ec_get_params },
1474
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))ec_gettable_params },
1475
    { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))ec_set_params },
1476
    { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))ec_settable_params },
1477
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1478
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1479
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
1480
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
1481
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1482
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1483
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1484
    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1485
        (void (*)(void))ec_query_operation_name },
1486
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
1487
    OSSL_DISPATCH_END
1488
};
1489
1490
#ifndef FIPS_MODULE
1491
#ifndef OPENSSL_NO_SM2
1492
#define SM2_FUNCS(variant)                                                          \
1493
    const OSSL_DISPATCH ossl_##variant##_keymgmt_functions[] = {                    \
1494
        { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata },                     \
1495
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init },               \
1496
        { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,                                       \
1497
            (void (*)(void))ec_gen_set_template },                                  \
1498
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },    \
1499
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,                                    \
1500
            (void (*)(void))ec_gen_settable_params },                               \
1501
        { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen },                         \
1502
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },          \
1503
        { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load },                       \
1504
        { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },                    \
1505
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))sm2_get_params },           \
1506
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))sm2_gettable_params }, \
1507
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))ec_set_params },            \
1508
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))sm2_settable_params }, \
1509
        { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },                          \
1510
        { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },                      \
1511
        { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate },               \
1512
        { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import },                   \
1513
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },        \
1514
        { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },                    \
1515
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },        \
1516
        { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,                                   \
1517
            (void (*)(void))variant##_query_operation_name },                       \
1518
        { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },                          \
1519
        OSSL_DISPATCH_END                                                           \
1520
    }
1521
SM2_FUNCS(sm2);
1522
SM2_FUNCS(curve_sm2);
1523
#endif
1524
#endif