Coverage Report

Created: 2026-02-11 06:19

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