Coverage Report

Created: 2025-08-25 06:30

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