Coverage Report

Created: 2025-12-11 07:20

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_SECURITY_BITS)) != NULL) {
651
0
        int ecbits, sec_bits;
652
653
0
        ecbits = EC_GROUP_order_bits(ecg);
654
655
        /*
656
         * The following estimates are based on the values published
657
         * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
658
         * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
659
         *
660
         * Note that the above reference explicitly categorizes algorithms in a
661
         * discrete set of values {80, 112, 128, 192, 256}, and that it is
662
         * relevant only for NIST approved Elliptic Curves, while OpenSSL
663
         * applies the same logic also to other curves.
664
         *
665
         * Classifications produced by other standardazing bodies might differ,
666
         * so the results provided for "bits of security" by this provider are
667
         * to be considered merely indicative, and it is the users'
668
         * responsibility to compare these values against the normative
669
         * references that may be relevant for their intent and purposes.
670
         */
671
0
        if (ecbits >= 512)
672
0
            sec_bits = 256;
673
0
        else if (ecbits >= 384)
674
0
            sec_bits = 192;
675
0
        else if (ecbits >= 256)
676
0
            sec_bits = 128;
677
0
        else if (ecbits >= 224)
678
0
            sec_bits = 112;
679
0
        else if (ecbits >= 160)
680
0
            sec_bits = 80;
681
0
        else
682
0
            sec_bits = ecbits / 2;
683
684
0
        if (!OSSL_PARAM_set_int(p, sec_bits))
685
0
            goto err;
686
0
    }
687
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_CATEGORY)) != NULL)
688
0
        if (!OSSL_PARAM_set_int(p, 0))
689
0
            goto err;
690
691
0
    if ((p = OSSL_PARAM_locate(params,
692
0
             OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS))
693
0
        != NULL) {
694
0
        int explicitparams = EC_KEY_decoded_from_explicit_params(eck);
695
696
0
        if (explicitparams < 0
697
0
            || !OSSL_PARAM_set_int(p, explicitparams))
698
0
            goto err;
699
0
    }
700
701
0
    if (!sm2) {
702
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
703
0
            && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
704
0
            goto err;
705
0
    } else {
706
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
707
0
            && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
708
0
            goto err;
709
0
    }
710
711
    /* SM2 doesn't support this PARAM */
712
0
    if (!sm2) {
713
0
        p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
714
0
        if (p != NULL) {
715
0
            int ecdh_cofactor_mode = 0;
716
717
0
            ecdh_cofactor_mode = (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
718
719
0
            if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
720
0
                goto err;
721
0
        }
722
0
    }
723
0
    if ((p = OSSL_PARAM_locate(params,
724
0
             OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY))
725
0
        != NULL) {
726
0
        const EC_POINT *ecp = EC_KEY_get0_public_key(key);
727
728
0
        if (ecp == NULL) {
729
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
730
0
            goto err;
731
0
        }
732
0
        p->return_size = EC_POINT_point2oct(ecg, ecp,
733
0
            POINT_CONVERSION_UNCOMPRESSED,
734
0
            p->data, p->data_size, bnctx);
735
0
        if (p->return_size == 0)
736
0
            goto err;
737
0
    }
738
739
0
    ret = ec_get_ecm_params(ecg, params)
740
0
        && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx,
741
0
            &genbuf)
742
0
        && key_to_params(eck, NULL, params, 1, &pub_key)
743
0
        && otherparams_to_params(eck, NULL, params);
744
0
err:
745
0
    OPENSSL_free(genbuf);
746
0
    OPENSSL_free(pub_key);
747
0
    BN_CTX_end(bnctx);
748
0
    BN_CTX_free(bnctx);
749
0
    return ret;
750
0
}
751
752
static int ec_get_params(void *key, OSSL_PARAM params[])
753
0
{
754
0
    return common_get_params(key, params, 0);
755
0
}
756
757
#ifndef OPENSSL_NO_EC2M
758
#define EC2M_GETTABLE_DOM_PARAMS                                        \
759
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL),                   \
760
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
761
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL),        \
762
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL),           \
763
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL),           \
764
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
765
#else
766
#define EC2M_GETTABLE_DOM_PARAMS
767
#endif
768
769
static const OSSL_PARAM ec_known_gettable_params[] = {
770
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
771
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
772
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
773
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
774
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
775
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
776
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
777
    EC_IMEXPORTABLE_DOM_PARAMETERS,
778
    EC2M_GETTABLE_DOM_PARAMS
779
        EC_IMEXPORTABLE_PUBLIC_KEY,
780
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
781
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
782
    EC_IMEXPORTABLE_PRIVATE_KEY,
783
    EC_IMEXPORTABLE_OTHER_PARAMETERS,
784
    OSSL_PARAM_END
785
};
786
787
static const OSSL_PARAM *ec_gettable_params(void *provctx)
788
0
{
789
0
    return ec_known_gettable_params;
790
0
}
791
792
static const OSSL_PARAM ec_known_settable_params[] = {
793
    OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
794
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
795
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
796
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
797
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
798
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
799
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
800
    OSSL_PARAM_END
801
};
802
803
static const OSSL_PARAM *ec_settable_params(void *provctx)
804
0
{
805
0
    return ec_known_settable_params;
806
0
}
807
808
static int ec_set_params(void *key, const OSSL_PARAM params[])
809
0
{
810
0
    EC_KEY *eck = key;
811
0
    const OSSL_PARAM *p;
812
813
0
    if (key == NULL)
814
0
        return 0;
815
0
    if (ossl_param_is_empty(params))
816
0
        return 1;
817
818
0
    if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
819
0
        return 0;
820
821
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
822
0
    if (p != NULL) {
823
0
        BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
824
0
        int ret = 1;
825
826
0
        if (ctx == NULL
827
0
            || p->data_type != OSSL_PARAM_OCTET_STRING
828
0
            || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
829
0
            ret = 0;
830
0
        BN_CTX_free(ctx);
831
0
        if (!ret)
832
0
            return 0;
833
0
    }
834
835
0
    return ossl_ec_key_otherparams_fromdata(eck, params);
836
0
}
837
838
#ifndef FIPS_MODULE
839
#ifndef OPENSSL_NO_SM2
840
static int sm2_get_params(void *key, OSSL_PARAM params[])
841
0
{
842
0
    return common_get_params(key, params, 1);
843
0
}
844
845
static const OSSL_PARAM sm2_known_gettable_params[] = {
846
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
847
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
848
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
849
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
850
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
851
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
852
    EC_IMEXPORTABLE_DOM_PARAMETERS,
853
    EC_IMEXPORTABLE_PUBLIC_KEY,
854
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
855
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
856
    EC_IMEXPORTABLE_PRIVATE_KEY,
857
    OSSL_PARAM_END
858
};
859
860
static const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
861
0
{
862
0
    return sm2_known_gettable_params;
863
0
}
864
865
static const OSSL_PARAM sm2_known_settable_params[] = {
866
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
867
    OSSL_PARAM_END
868
};
869
870
static const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
871
0
{
872
0
    return sm2_known_settable_params;
873
0
}
874
875
static int sm2_validate(const void *keydata, int selection, int checktype)
876
0
{
877
0
    const EC_KEY *eck = keydata;
878
0
    int ok = 1;
879
0
    BN_CTX *ctx = NULL;
880
881
0
    if (!ossl_prov_is_running())
882
0
        return 0;
883
884
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
885
0
        return 1; /* nothing to validate */
886
887
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
888
0
    if (ctx == NULL)
889
0
        return 0;
890
891
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
892
0
        ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
893
894
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
895
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
896
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
897
0
        else
898
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
899
0
    }
900
901
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
902
0
        ok = ok && ossl_sm2_key_private_check(eck);
903
904
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
905
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
906
907
0
    BN_CTX_free(ctx);
908
0
    return ok;
909
0
}
910
#endif
911
#endif
912
913
static int ec_validate(const void *keydata, int selection, int checktype)
914
0
{
915
0
    const EC_KEY *eck = keydata;
916
0
    int ok = 1;
917
0
    BN_CTX *ctx = NULL;
918
919
0
    if (!ossl_prov_is_running())
920
0
        return 0;
921
922
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
923
0
        return 1; /* nothing to validate */
924
925
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
926
0
    if (ctx == NULL)
927
0
        return 0;
928
929
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
930
0
        int flags = EC_KEY_get_flags(eck);
931
932
0
        if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
933
0
            ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck), (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0;
934
0
        else
935
0
            ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
936
0
    }
937
938
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
939
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
940
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
941
0
        else
942
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
943
0
    }
944
945
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
946
0
        ok = ok && ossl_ec_key_private_check(eck);
947
948
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
949
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
950
951
0
    BN_CTX_free(ctx);
952
0
    return ok;
953
0
}
954
955
struct ec_gen_ctx {
956
    OSSL_LIB_CTX *libctx;
957
    char *group_name;
958
    char *encoding;
959
    char *pt_format;
960
    char *group_check;
961
    char *field_type;
962
    BIGNUM *p, *a, *b, *order, *cofactor;
963
    unsigned char *gen, *seed;
964
    size_t gen_len, seed_len;
965
    int selection;
966
    int ecdh_mode;
967
    EC_GROUP *gen_group;
968
    unsigned char *dhkem_ikm;
969
    size_t dhkem_ikmlen;
970
    OSSL_FIPS_IND_DECLARE
971
};
972
973
static void *ec_gen_init(void *provctx, int selection,
974
    const OSSL_PARAM params[])
975
0
{
976
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
977
0
    struct ec_gen_ctx *gctx = NULL;
978
979
0
    if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
980
0
        return NULL;
981
982
0
    if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
983
0
        gctx->libctx = libctx;
984
0
        gctx->selection = selection;
985
0
        gctx->ecdh_mode = 0;
986
0
        OSSL_FIPS_IND_INIT(gctx)
987
0
        if (!ec_gen_set_params(gctx, params)) {
988
0
            OPENSSL_free(gctx);
989
0
            gctx = NULL;
990
0
        }
991
0
    }
992
0
    return gctx;
993
0
}
994
995
#ifndef FIPS_MODULE
996
#ifndef OPENSSL_NO_SM2
997
static void *sm2_gen_init(void *provctx, int selection,
998
    const OSSL_PARAM params[])
999
0
{
1000
0
    struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
1001
1002
0
    if (gctx != NULL) {
1003
0
        if (gctx->group_name != NULL)
1004
0
            return gctx;
1005
0
        if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
1006
0
            return gctx;
1007
0
        ec_gen_cleanup(gctx);
1008
0
    }
1009
0
    return NULL;
1010
0
}
1011
#endif
1012
#endif
1013
1014
static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
1015
0
{
1016
0
    struct ec_gen_ctx *gctx = genctx;
1017
0
    EC_GROUP *group;
1018
1019
0
    group = EC_GROUP_dup(src);
1020
0
    if (group == NULL) {
1021
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
1022
0
        return 0;
1023
0
    }
1024
0
    EC_GROUP_free(gctx->gen_group);
1025
0
    gctx->gen_group = group;
1026
0
    return 1;
1027
0
}
1028
1029
static int ec_gen_set_template(void *genctx, void *templ)
1030
0
{
1031
0
    struct ec_gen_ctx *gctx = genctx;
1032
0
    EC_KEY *ec = templ;
1033
0
    const EC_GROUP *ec_group;
1034
1035
0
    if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
1036
0
        return 0;
1037
0
    if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
1038
0
        return 0;
1039
0
    return ec_gen_set_group(gctx, ec_group);
1040
0
}
1041
1042
#define COPY_INT_PARAM(params, key, val)           \
1043
0
    p = OSSL_PARAM_locate_const(params, key);      \
1044
0
    if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
1045
0
        goto err;
1046
1047
#define COPY_UTF8_PARAM(params, key, val)           \
1048
0
    p = OSSL_PARAM_locate_const(params, key);       \
1049
0
    if (p != NULL) {                                \
1050
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING) \
1051
0
            goto err;                               \
1052
0
        OPENSSL_free(val);                          \
1053
0
        val = OPENSSL_strdup(p->data);              \
1054
0
        if (val == NULL)                            \
1055
0
            goto err;                               \
1056
0
    }
1057
1058
#define COPY_OCTET_PARAM(params, key, val, len)      \
1059
0
    p = OSSL_PARAM_locate_const(params, key);        \
1060
0
    if (p != NULL) {                                 \
1061
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) \
1062
0
            goto err;                                \
1063
0
        OPENSSL_free(val);                           \
1064
0
        len = p->data_size;                          \
1065
0
        val = OPENSSL_memdup(p->data, p->data_size); \
1066
0
        if (val == NULL)                             \
1067
0
            goto err;                                \
1068
0
    }
1069
1070
#define COPY_BN_PARAM(params, key, bn)                \
1071
0
    p = OSSL_PARAM_locate_const(params, key);         \
1072
0
    if (p != NULL) {                                  \
1073
0
        if (bn == NULL)                               \
1074
0
            bn = BN_new();                            \
1075
0
        if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
1076
0
            goto err;                                 \
1077
0
    }
1078
1079
static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
1080
0
{
1081
0
    int ret = 0;
1082
0
    struct ec_gen_ctx *gctx = genctx;
1083
0
    const OSSL_PARAM *p;
1084
1085
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params,
1086
0
            OSSL_PKEY_PARAM_FIPS_KEY_CHECK))
1087
0
        goto err;
1088
1089
0
    COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
1090
1091
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
1092
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
1093
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
1094
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
1095
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
1096
1097
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
1098
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
1099
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
1100
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
1101
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
1102
1103
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
1104
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
1105
0
        gctx->gen_len);
1106
1107
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm,
1108
0
        gctx->dhkem_ikmlen);
1109
1110
0
    ret = 1;
1111
0
err:
1112
0
    return ret;
1113
0
}
1114
1115
static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
1116
0
{
1117
0
    int ret = 0;
1118
0
    OSSL_PARAM_BLD *bld;
1119
0
    OSSL_PARAM *params = NULL;
1120
0
    EC_GROUP *group = NULL;
1121
1122
0
    bld = OSSL_PARAM_BLD_new();
1123
0
    if (bld == NULL)
1124
0
        return 0;
1125
1126
0
    if (gctx->encoding != NULL
1127
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
1128
0
            gctx->encoding, 0))
1129
0
        goto err;
1130
1131
0
    if (gctx->pt_format != NULL
1132
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld,
1133
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
1134
0
            gctx->pt_format, 0))
1135
0
        goto err;
1136
1137
0
    if (gctx->group_name != NULL) {
1138
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1139
0
                gctx->group_name, 0))
1140
0
            goto err;
1141
        /* Ignore any other parameters if there is a group name */
1142
0
        goto build;
1143
0
    } else if (gctx->field_type != NULL) {
1144
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1145
0
                gctx->field_type, 0))
1146
0
            goto err;
1147
0
    } else {
1148
0
        goto err;
1149
0
    }
1150
0
    if (gctx->p == NULL
1151
0
        || gctx->a == NULL
1152
0
        || gctx->b == NULL
1153
0
        || gctx->order == NULL
1154
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
1155
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
1156
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
1157
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
1158
0
        goto err;
1159
1160
0
    if (gctx->cofactor != NULL
1161
0
        && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1162
0
            gctx->cofactor))
1163
0
        goto err;
1164
1165
0
    if (gctx->seed != NULL
1166
0
        && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
1167
0
            gctx->seed, gctx->seed_len))
1168
0
        goto err;
1169
1170
0
    if (gctx->gen == NULL
1171
0
        || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
1172
0
            gctx->gen, gctx->gen_len))
1173
0
        goto err;
1174
0
build:
1175
0
    params = OSSL_PARAM_BLD_to_param(bld);
1176
0
    if (params == NULL)
1177
0
        goto err;
1178
0
    group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
1179
0
    if (group == NULL)
1180
0
        goto err;
1181
1182
0
    EC_GROUP_free(gctx->gen_group);
1183
0
    gctx->gen_group = group;
1184
1185
0
    ret = 1;
1186
0
err:
1187
0
    OSSL_PARAM_free(params);
1188
0
    OSSL_PARAM_BLD_free(bld);
1189
0
    return ret;
1190
0
}
1191
1192
static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx,
1193
    ossl_unused void *provctx)
1194
0
{
1195
0
    static const OSSL_PARAM settable[] = {
1196
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
1197
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1198
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
1199
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
1200
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
1201
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
1202
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
1203
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
1204
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
1205
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
1206
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
1207
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
1208
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
1209
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_KEY_CHECK)
1210
0
            OSSL_PARAM_END
1211
0
    };
1212
0
    return settable;
1213
0
}
1214
1215
static const OSSL_PARAM *ec_gen_gettable_params(ossl_unused void *genctx,
1216
    ossl_unused void *provctx)
1217
0
{
1218
0
    static const OSSL_PARAM known_ec_gen_gettable_ctx_params[] = {
1219
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
1220
0
            OSSL_PARAM_END
1221
0
    };
1222
0
    return known_ec_gen_gettable_ctx_params;
1223
0
}
1224
1225
static int ec_gen_get_params(void *genctx, OSSL_PARAM *params)
1226
0
{
1227
0
    struct ec_gen_ctx *gctx = genctx;
1228
1229
0
    if (gctx == NULL)
1230
0
        return 0;
1231
1232
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params))
1233
0
        return 0;
1234
1235
0
    return 1;
1236
0
}
1237
1238
static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
1239
0
{
1240
0
    if (group == NULL) {
1241
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
1242
0
        return 0;
1243
0
    }
1244
0
    return EC_KEY_set_group(ec, group) > 0;
1245
0
}
1246
1247
/*
1248
 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1249
 */
1250
static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1251
0
{
1252
0
    struct ec_gen_ctx *gctx = genctx;
1253
0
    EC_KEY *ec = NULL;
1254
0
    int ret = 0;
1255
1256
0
    if (!ossl_prov_is_running()
1257
0
        || gctx == NULL
1258
0
        || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1259
0
        return NULL;
1260
1261
0
    if (gctx->gen_group == NULL) {
1262
0
        if (!ec_gen_set_group_from_params(gctx))
1263
0
            goto err;
1264
0
    } else {
1265
0
        if (gctx->encoding != NULL) {
1266
0
            int flags = ossl_ec_encoding_name2id(gctx->encoding);
1267
1268
0
            if (flags < 0)
1269
0
                goto err;
1270
0
            EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1271
0
        }
1272
0
        if (gctx->pt_format != NULL) {
1273
0
            int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1274
1275
0
            if (format < 0)
1276
0
                goto err;
1277
0
            EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1278
0
        }
1279
0
    }
1280
#ifdef FIPS_MODULE
1281
    if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(gctx),
1282
            OSSL_FIPS_IND_SETTABLE0, gctx->libctx,
1283
            gctx->gen_group, "EC KeyGen", 1))
1284
        goto err;
1285
#endif
1286
1287
    /* We must always assign a group, no matter what */
1288
0
    ret = ec_gen_assign_group(ec, gctx->gen_group);
1289
1290
    /* Whether you want it or not, you get a keypair, not just one half */
1291
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1292
0
#ifndef FIPS_MODULE
1293
0
        if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0)
1294
0
            ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm, gctx->dhkem_ikmlen);
1295
0
        else
1296
0
#endif
1297
0
            ret = ret && EC_KEY_generate_key(ec);
1298
0
    }
1299
1300
0
    if (gctx->ecdh_mode != -1)
1301
0
        ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
1302
1303
0
    if (gctx->group_check != NULL)
1304
0
        ret = ret && ossl_ec_set_check_group_type_from_name(ec, gctx->group_check);
1305
#ifdef FIPS_MODULE
1306
    if (ret > 0
1307
        && !ossl_fips_self_testing()
1308
        && EC_KEY_get0_public_key(ec) != NULL
1309
        && EC_KEY_get0_private_key(ec) != NULL
1310
        && EC_KEY_get0_group(ec) != NULL) {
1311
        BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
1312
1313
        ret = bnctx != NULL && ossl_ec_key_pairwise_check(ec, bnctx);
1314
        BN_CTX_free(bnctx);
1315
        if (ret <= 0)
1316
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
1317
    }
1318
#endif /* FIPS_MODULE */
1319
1320
0
    if (ret)
1321
0
        return ec;
1322
0
err:
1323
    /* Something went wrong, throw the key away */
1324
0
    EC_KEY_free(ec);
1325
0
    return NULL;
1326
0
}
1327
1328
#ifndef FIPS_MODULE
1329
#ifndef OPENSSL_NO_SM2
1330
/*
1331
 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1332
 */
1333
static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1334
0
{
1335
0
    struct ec_gen_ctx *gctx = genctx;
1336
0
    EC_KEY *ec = NULL;
1337
0
    int ret = 1;
1338
1339
0
    if (gctx == NULL
1340
0
        || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1341
0
        return NULL;
1342
1343
0
    if (gctx->gen_group == NULL) {
1344
0
        if (!ec_gen_set_group_from_params(gctx))
1345
0
            goto err;
1346
0
    } else {
1347
0
        if (gctx->encoding) {
1348
0
            int flags = ossl_ec_encoding_name2id(gctx->encoding);
1349
1350
0
            if (flags < 0)
1351
0
                goto err;
1352
0
            EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1353
0
        }
1354
0
        if (gctx->pt_format != NULL) {
1355
0
            int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1356
1357
0
            if (format < 0)
1358
0
                goto err;
1359
0
            EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1360
0
        }
1361
0
    }
1362
1363
    /* We must always assign a group, no matter what */
1364
0
    ret = ec_gen_assign_group(ec, gctx->gen_group);
1365
1366
    /* Whether you want it or not, you get a keypair, not just one half */
1367
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
1368
0
        ret = ret && EC_KEY_generate_key(ec);
1369
1370
0
    if (ret)
1371
0
        return ec;
1372
0
err:
1373
    /* Something went wrong, throw the key away */
1374
0
    EC_KEY_free(ec);
1375
0
    return NULL;
1376
0
}
1377
#endif
1378
#endif
1379
1380
static void ec_gen_cleanup(void *genctx)
1381
0
{
1382
0
    struct ec_gen_ctx *gctx = genctx;
1383
1384
0
    if (gctx == NULL)
1385
0
        return;
1386
1387
0
    OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen);
1388
0
    EC_GROUP_free(gctx->gen_group);
1389
0
    BN_free(gctx->p);
1390
0
    BN_free(gctx->a);
1391
0
    BN_free(gctx->b);
1392
0
    BN_free(gctx->order);
1393
0
    BN_free(gctx->cofactor);
1394
0
    OPENSSL_free(gctx->group_name);
1395
0
    OPENSSL_free(gctx->field_type);
1396
0
    OPENSSL_free(gctx->pt_format);
1397
0
    OPENSSL_free(gctx->encoding);
1398
0
    OPENSSL_free(gctx->seed);
1399
0
    OPENSSL_free(gctx->gen);
1400
0
    OPENSSL_free(gctx);
1401
0
}
1402
1403
static void *common_load(const void *reference, size_t reference_sz,
1404
    int sm2_wanted)
1405
0
{
1406
0
    EC_KEY *ec = NULL;
1407
1408
0
    if (ossl_prov_is_running() && reference_sz == sizeof(ec)) {
1409
        /* The contents of the reference is the address to our object */
1410
0
        ec = *(EC_KEY **)reference;
1411
1412
0
        if (!common_check_sm2(ec, sm2_wanted))
1413
0
            return NULL;
1414
1415
        /* We grabbed, so we detach it */
1416
0
        *(EC_KEY **)reference = NULL;
1417
0
        return ec;
1418
0
    }
1419
0
    return NULL;
1420
0
}
1421
1422
static void *ec_load(const void *reference, size_t reference_sz)
1423
0
{
1424
0
    return common_load(reference, reference_sz, 0);
1425
0
}
1426
1427
#ifndef FIPS_MODULE
1428
#ifndef OPENSSL_NO_SM2
1429
static void *sm2_load(const void *reference, size_t reference_sz)
1430
0
{
1431
0
    return common_load(reference, reference_sz, 1);
1432
0
}
1433
#endif
1434
#endif
1435
1436
static void *ec_dup(const void *keydata_from, int selection)
1437
0
{
1438
0
    if (ossl_prov_is_running())
1439
0
        return ossl_ec_key_dup(keydata_from, selection);
1440
0
    return NULL;
1441
0
}
1442
1443
const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
1444
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1445
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1446
    { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1447
        (void (*)(void))ec_gen_set_template },
1448
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1449
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1450
        (void (*)(void))ec_gen_settable_params },
1451
    { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params },
1452
    { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
1453
        (void (*)(void))ec_gen_gettable_params },
1454
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
1455
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1456
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
1457
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1458
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))ec_get_params },
1459
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))ec_gettable_params },
1460
    { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))ec_set_params },
1461
    { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))ec_settable_params },
1462
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1463
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1464
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
1465
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
1466
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1467
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1468
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1469
    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1470
        (void (*)(void))ec_query_operation_name },
1471
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
1472
    OSSL_DISPATCH_END
1473
};
1474
1475
#ifndef FIPS_MODULE
1476
#ifndef OPENSSL_NO_SM2
1477
const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = {
1478
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata },
1479
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init },
1480
    { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1481
        (void (*)(void))ec_gen_set_template },
1482
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1483
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1484
        (void (*)(void))ec_gen_settable_params },
1485
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen },
1486
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1487
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load },
1488
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1489
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))sm2_get_params },
1490
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))sm2_gettable_params },
1491
    { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))ec_set_params },
1492
    { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))sm2_settable_params },
1493
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1494
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1495
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate },
1496
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import },
1497
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1498
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1499
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1500
    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1501
        (void (*)(void))sm2_query_operation_name },
1502
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
1503
    OSSL_DISPATCH_END
1504
};
1505
#endif
1506
#endif