Coverage Report

Created: 2025-10-10 07:01

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
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
0
    return ok;
435
0
}
436
437
static
438
int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
439
0
{
440
0
    return common_import(keydata, selection, params, 0);
441
0
}
442
443
#ifndef FIPS_MODULE
444
# ifndef OPENSSL_NO_SM2
445
static
446
int sm2_import(void *keydata, int selection, const OSSL_PARAM params[])
447
0
{
448
0
    return common_import(keydata, selection, params, 1);
449
0
}
450
# endif
451
#endif
452
453
static
454
int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
455
              void *cbarg)
456
0
{
457
0
    EC_KEY *ec = keydata;
458
0
    OSSL_PARAM_BLD *tmpl = NULL;
459
0
    OSSL_PARAM *params = NULL;
460
0
    unsigned char *pub_key = NULL, *genbuf = NULL;
461
0
    BN_CTX *bnctx = NULL;
462
0
    int ok = 1;
463
464
0
    if (!ossl_prov_is_running() || ec == NULL)
465
0
        return 0;
466
467
    /*
468
     * In this implementation, we can export/import only keydata in the
469
     * following combinations:
470
     *   - domain parameters (+optional other params)
471
     *   - public key with associated domain parameters (+optional other params)
472
     *   - private key with associated public key and domain parameters
473
     *         (+optional other params)
474
     *
475
     * This means:
476
     *   - domain parameters must always be requested
477
     *   - private key must be requested alongside public key
478
     *   - other parameters are always optional
479
     */
480
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
481
0
        return 0;
482
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
483
0
            && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
484
0
        return 0;
485
486
0
    tmpl = OSSL_PARAM_BLD_new();
487
0
    if (tmpl == NULL)
488
0
        return 0;
489
490
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
491
0
        bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
492
0
        if (bnctx == NULL) {
493
0
            ok = 0;
494
0
            goto end;
495
0
        }
496
0
        BN_CTX_start(bnctx);
497
0
        ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL,
498
0
                                        ossl_ec_key_get_libctx(ec),
499
0
                                        ossl_ec_key_get0_propq(ec),
500
0
                                        bnctx, &genbuf);
501
0
    }
502
503
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
504
0
        int include_private =
505
0
            selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
506
507
0
        ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
508
0
    }
509
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
510
0
        ok = ok && otherparams_to_params(ec, tmpl, NULL);
511
512
0
    if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
513
0
        ok = 0;
514
0
        goto end;
515
0
    }
516
517
0
    ok = param_cb(params, cbarg);
518
0
    OSSL_PARAM_free(params);
519
0
end:
520
0
    OSSL_PARAM_BLD_free(tmpl);
521
0
    OPENSSL_free(pub_key);
522
0
    OPENSSL_free(genbuf);
523
0
    BN_CTX_end(bnctx);
524
0
    BN_CTX_free(bnctx);
525
0
    return ok;
526
0
}
527
528
/* IMEXPORT = IMPORT + EXPORT */
529
530
# define EC_IMEXPORTABLE_DOM_PARAMETERS                                        \
531
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),               \
532
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),              \
533
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
534
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),            \
535
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),                              \
536
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),                              \
537
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),                              \
538
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),            \
539
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),                          \
540
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),                       \
541
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),                 \
542
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL)
543
544
# define EC_IMEXPORTABLE_PUBLIC_KEY                                            \
545
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
546
# define EC_IMEXPORTABLE_PRIVATE_KEY                                           \
547
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
548
# define EC_IMEXPORTABLE_OTHER_PARAMETERS                                      \
549
    OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),                   \
550
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
551
552
/*
553
 * Include all the possible combinations of OSSL_PARAM arrays for
554
 * ec_imexport_types().
555
 *
556
 * They are in a separate file as it is ~100 lines of unreadable and
557
 * uninteresting machine generated stuff.
558
 */
559
#include "ec_kmgmt_imexport.inc"
560
561
static ossl_inline
562
const OSSL_PARAM *ec_imexport_types(int selection)
563
0
{
564
0
    int type_select = 0;
565
566
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
567
0
        type_select += 1;
568
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
569
0
        type_select += 2;
570
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
571
0
        type_select += 4;
572
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
573
0
        type_select += 8;
574
0
    return ec_types[type_select];
575
0
}
576
577
static
578
const OSSL_PARAM *ec_import_types(int selection)
579
0
{
580
0
    return ec_imexport_types(selection);
581
0
}
582
583
static
584
const OSSL_PARAM *ec_export_types(int selection)
585
0
{
586
0
    return ec_imexport_types(selection);
587
0
}
588
589
static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
590
0
{
591
#ifdef OPENSSL_NO_EC2M
592
    return 1;
593
#else
594
0
    int ret = 0, m;
595
0
    unsigned int k1 = 0, k2 = 0, k3 = 0;
596
0
    int basis_nid;
597
0
    const char *basis_name = NULL;
598
0
    int fid = EC_GROUP_get_field_type(group);
599
600
0
    if (fid != NID_X9_62_characteristic_two_field)
601
0
        return 1;
602
603
0
    basis_nid = EC_GROUP_get_basis_type(group);
604
0
    if (basis_nid == NID_X9_62_tpBasis)
605
0
        basis_name = SN_X9_62_tpBasis;
606
0
    else if (basis_nid == NID_X9_62_ppBasis)
607
0
        basis_name = SN_X9_62_ppBasis;
608
0
    else
609
0
        goto err;
610
611
0
    m = EC_GROUP_get_degree(group);
612
0
    if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
613
0
        || !ossl_param_build_set_utf8_string(NULL, params,
614
0
                                             OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
615
0
                                             basis_name))
616
0
        goto err;
617
618
0
    if (basis_nid == NID_X9_62_tpBasis) {
619
0
        if (!EC_GROUP_get_trinomial_basis(group, &k1)
620
0
            || !ossl_param_build_set_int(NULL, params,
621
0
                                         OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
622
0
                                         (int)k1))
623
0
            goto err;
624
0
    } else {
625
0
        if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
626
0
            || !ossl_param_build_set_int(NULL, params,
627
0
                                         OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
628
0
            || !ossl_param_build_set_int(NULL, params,
629
0
                                         OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
630
0
            || !ossl_param_build_set_int(NULL, params,
631
0
                                         OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
632
0
            goto err;
633
0
    }
634
0
    ret = 1;
635
0
err:
636
0
    return ret;
637
0
#endif /* OPENSSL_NO_EC2M */
638
0
}
639
640
static
641
int common_get_params(void *key, OSSL_PARAM params[], int sm2)
642
0
{
643
0
    int ret = 0;
644
0
    EC_KEY *eck = key;
645
0
    const EC_GROUP *ecg = NULL;
646
0
    OSSL_PARAM *p;
647
0
    unsigned char *pub_key = NULL, *genbuf = NULL;
648
0
    OSSL_LIB_CTX *libctx;
649
0
    const char *propq;
650
0
    BN_CTX *bnctx = NULL;
651
652
0
    ecg = EC_KEY_get0_group(eck);
653
0
    if (ecg == NULL) {
654
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
655
0
        return 0;
656
0
    }
657
658
0
    libctx = ossl_ec_key_get_libctx(eck);
659
0
    propq = ossl_ec_key_get0_propq(eck);
660
661
0
    bnctx = BN_CTX_new_ex(libctx);
662
0
    if (bnctx == NULL)
663
0
        return 0;
664
0
    BN_CTX_start(bnctx);
665
666
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
667
0
        && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
668
0
        goto err;
669
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
670
0
        && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
671
0
        goto err;
672
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
673
0
        int ecbits, sec_bits;
674
675
0
        ecbits = EC_GROUP_order_bits(ecg);
676
677
        /*
678
         * The following estimates are based on the values published
679
         * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
680
         * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
681
         *
682
         * Note that the above reference explicitly categorizes algorithms in a
683
         * discrete set of values {80, 112, 128, 192, 256}, and that it is
684
         * relevant only for NIST approved Elliptic Curves, while OpenSSL
685
         * applies the same logic also to other curves.
686
         *
687
         * Classifications produced by other standardazing bodies might differ,
688
         * so the results provided for "bits of security" by this provider are
689
         * to be considered merely indicative, and it is the users'
690
         * responsibility to compare these values against the normative
691
         * references that may be relevant for their intent and purposes.
692
         */
693
0
        if (ecbits >= 512)
694
0
            sec_bits = 256;
695
0
        else if (ecbits >= 384)
696
0
            sec_bits = 192;
697
0
        else if (ecbits >= 256)
698
0
            sec_bits = 128;
699
0
        else if (ecbits >= 224)
700
0
            sec_bits = 112;
701
0
        else if (ecbits >= 160)
702
0
            sec_bits = 80;
703
0
        else
704
0
            sec_bits = ecbits / 2;
705
706
0
        if (!OSSL_PARAM_set_int(p, sec_bits))
707
0
            goto err;
708
0
    }
709
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_CATEGORY)) != NULL)
710
0
        if (!OSSL_PARAM_set_int(p, 0))
711
0
            goto err;
712
713
0
    if ((p = OSSL_PARAM_locate(params,
714
0
                               OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS))
715
0
            != NULL) {
716
0
        int explicitparams = EC_KEY_decoded_from_explicit_params(eck);
717
718
0
        if (explicitparams < 0
719
0
             || !OSSL_PARAM_set_int(p, explicitparams))
720
0
            goto err;
721
0
    }
722
723
0
    if (!sm2) {
724
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
725
0
                && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
726
0
            goto err;
727
0
    } else {
728
0
        if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
729
0
                && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
730
0
            goto err;
731
0
    }
732
733
    /* SM2 doesn't support this PARAM */
734
0
    if (!sm2) {
735
0
        p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
736
0
        if (p != NULL) {
737
0
            int ecdh_cofactor_mode = 0;
738
739
0
            ecdh_cofactor_mode =
740
0
                (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
741
742
0
            if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
743
0
                goto err;
744
0
        }
745
0
    }
746
0
    if ((p = OSSL_PARAM_locate(params,
747
0
                               OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
748
0
        const EC_POINT *ecp = EC_KEY_get0_public_key(key);
749
750
0
        if (ecp == NULL) {
751
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
752
0
            goto err;
753
0
        }
754
0
        p->return_size = EC_POINT_point2oct(ecg, ecp,
755
0
                                            POINT_CONVERSION_UNCOMPRESSED,
756
0
                                            p->data, p->data_size, bnctx);
757
0
        if (p->return_size == 0)
758
0
            goto err;
759
0
    }
760
761
0
    ret = ec_get_ecm_params(ecg, params)
762
0
          && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx,
763
0
                                  &genbuf)
764
0
          && key_to_params(eck, NULL, params, 1, &pub_key)
765
0
          && otherparams_to_params(eck, NULL, params);
766
0
err:
767
0
    OPENSSL_free(genbuf);
768
0
    OPENSSL_free(pub_key);
769
0
    BN_CTX_end(bnctx);
770
0
    BN_CTX_free(bnctx);
771
0
    return ret;
772
0
}
773
774
static
775
int ec_get_params(void *key, OSSL_PARAM params[])
776
0
{
777
0
    return common_get_params(key, params, 0);
778
0
}
779
780
#ifndef OPENSSL_NO_EC2M
781
# define EC2M_GETTABLE_DOM_PARAMS                                              \
782
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL),                      \
783
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0),        \
784
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL),               \
785
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL),                  \
786
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL),                  \
787
        OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
788
#else
789
# define EC2M_GETTABLE_DOM_PARAMS
790
#endif
791
792
static const OSSL_PARAM ec_known_gettable_params[] = {
793
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
794
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
795
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
796
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
797
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
798
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
799
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
800
    EC_IMEXPORTABLE_DOM_PARAMETERS,
801
    EC2M_GETTABLE_DOM_PARAMS
802
    EC_IMEXPORTABLE_PUBLIC_KEY,
803
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
804
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
805
    EC_IMEXPORTABLE_PRIVATE_KEY,
806
    EC_IMEXPORTABLE_OTHER_PARAMETERS,
807
    OSSL_PARAM_END
808
};
809
810
static
811
const OSSL_PARAM *ec_gettable_params(void *provctx)
812
0
{
813
0
    return ec_known_gettable_params;
814
0
}
815
816
static const OSSL_PARAM ec_known_settable_params[] = {
817
    OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
818
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
819
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
820
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
821
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
822
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
823
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
824
    OSSL_PARAM_END
825
};
826
827
static
828
const OSSL_PARAM *ec_settable_params(void *provctx)
829
0
{
830
0
    return ec_known_settable_params;
831
0
}
832
833
static
834
int ec_set_params(void *key, const OSSL_PARAM params[])
835
0
{
836
0
    EC_KEY *eck = key;
837
0
    const OSSL_PARAM *p;
838
839
0
    if (key == NULL)
840
0
        return 0;
841
0
    if (ossl_param_is_empty(params))
842
0
        return 1;
843
844
0
    if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
845
0
        return 0;
846
847
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
848
0
    if (p != NULL) {
849
0
        BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
850
0
        int ret = 1;
851
852
0
        if (ctx == NULL
853
0
                || p->data_type != OSSL_PARAM_OCTET_STRING
854
0
                || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
855
0
            ret = 0;
856
0
        BN_CTX_free(ctx);
857
0
        if (!ret)
858
0
            return 0;
859
0
    }
860
861
0
    return ossl_ec_key_otherparams_fromdata(eck, params);
862
0
}
863
864
#ifndef FIPS_MODULE
865
# ifndef OPENSSL_NO_SM2
866
static
867
int sm2_get_params(void *key, OSSL_PARAM params[])
868
0
{
869
0
    return common_get_params(key, params, 1);
870
0
}
871
872
static const OSSL_PARAM sm2_known_gettable_params[] = {
873
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
874
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
875
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
876
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
877
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
878
    OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
879
    EC_IMEXPORTABLE_DOM_PARAMETERS,
880
    EC_IMEXPORTABLE_PUBLIC_KEY,
881
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
882
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
883
    EC_IMEXPORTABLE_PRIVATE_KEY,
884
    OSSL_PARAM_END
885
};
886
887
static
888
const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
889
0
{
890
0
    return sm2_known_gettable_params;
891
0
}
892
893
static const OSSL_PARAM sm2_known_settable_params[] = {
894
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
895
    OSSL_PARAM_END
896
};
897
898
static
899
const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
900
0
{
901
0
    return sm2_known_settable_params;
902
0
}
903
904
static
905
int sm2_validate(const void *keydata, int selection, int checktype)
906
0
{
907
0
    const EC_KEY *eck = keydata;
908
0
    int ok = 1;
909
0
    BN_CTX *ctx = NULL;
910
911
0
    if (!ossl_prov_is_running())
912
0
        return 0;
913
914
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
915
0
        return 1; /* nothing to validate */
916
917
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
918
0
    if  (ctx == NULL)
919
0
        return 0;
920
921
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
922
0
        ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
923
924
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
925
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
926
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
927
0
        else
928
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
929
0
    }
930
931
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
932
0
        ok = ok && ossl_sm2_key_private_check(eck);
933
934
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
935
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
936
937
0
    BN_CTX_free(ctx);
938
0
    return ok;
939
0
}
940
# endif
941
#endif
942
943
static
944
int ec_validate(const void *keydata, int selection, int checktype)
945
0
{
946
0
    const EC_KEY *eck = keydata;
947
0
    int ok = 1;
948
0
    BN_CTX *ctx = NULL;
949
950
0
    if (!ossl_prov_is_running())
951
0
        return 0;
952
953
0
    if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
954
0
        return 1; /* nothing to validate */
955
956
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
957
0
    if  (ctx == NULL)
958
0
        return 0;
959
960
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
961
0
        int flags = EC_KEY_get_flags(eck);
962
963
0
        if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
964
0
            ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck),
965
0
                           (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0;
966
0
        else
967
0
            ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
968
0
    }
969
970
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
971
0
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
972
0
            ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
973
0
        else
974
0
            ok = ok && ossl_ec_key_public_check(eck, ctx);
975
0
    }
976
977
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
978
0
        ok = ok && ossl_ec_key_private_check(eck);
979
980
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
981
0
        ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
982
983
0
    BN_CTX_free(ctx);
984
0
    return ok;
985
0
}
986
987
struct ec_gen_ctx {
988
    OSSL_LIB_CTX *libctx;
989
    char *group_name;
990
    char *encoding;
991
    char *pt_format;
992
    char *group_check;
993
    char *field_type;
994
    BIGNUM *p, *a, *b, *order, *cofactor;
995
    unsigned char *gen, *seed;
996
    size_t gen_len, seed_len;
997
    int selection;
998
    int ecdh_mode;
999
    EC_GROUP *gen_group;
1000
    unsigned char *dhkem_ikm;
1001
    size_t dhkem_ikmlen;
1002
    OSSL_FIPS_IND_DECLARE
1003
};
1004
1005
static void *ec_gen_init(void *provctx, int selection,
1006
                         const OSSL_PARAM params[])
1007
0
{
1008
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
1009
0
    struct ec_gen_ctx *gctx = NULL;
1010
1011
0
    if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
1012
0
        return NULL;
1013
1014
0
    if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
1015
0
        gctx->libctx = libctx;
1016
0
        gctx->selection = selection;
1017
0
        gctx->ecdh_mode = 0;
1018
0
        OSSL_FIPS_IND_INIT(gctx)
1019
0
        if (!ec_gen_set_params(gctx, params)) {
1020
0
            OPENSSL_free(gctx);
1021
0
            gctx = NULL;
1022
0
        }
1023
0
    }
1024
0
    return gctx;
1025
0
}
1026
1027
#ifndef FIPS_MODULE
1028
# ifndef OPENSSL_NO_SM2
1029
static void *sm2_gen_init(void *provctx, int selection,
1030
                         const OSSL_PARAM params[])
1031
0
{
1032
0
    struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
1033
1034
0
    if (gctx != NULL) {
1035
0
        if (gctx->group_name != NULL)
1036
0
            return gctx;
1037
0
        if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
1038
0
            return gctx;
1039
0
        ec_gen_cleanup(gctx);
1040
0
    }
1041
0
    return NULL;
1042
0
}
1043
# endif
1044
#endif
1045
1046
static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
1047
0
{
1048
0
    struct ec_gen_ctx *gctx = genctx;
1049
0
    EC_GROUP *group;
1050
1051
0
    group = EC_GROUP_dup(src);
1052
0
    if (group == NULL) {
1053
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
1054
0
        return 0;
1055
0
    }
1056
0
    EC_GROUP_free(gctx->gen_group);
1057
0
    gctx->gen_group = group;
1058
0
    return 1;
1059
0
}
1060
1061
static int ec_gen_set_template(void *genctx, void *templ)
1062
0
{
1063
0
    struct ec_gen_ctx *gctx = genctx;
1064
0
    EC_KEY *ec = templ;
1065
0
    const EC_GROUP *ec_group;
1066
1067
0
    if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
1068
0
        return 0;
1069
0
    if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
1070
0
        return 0;
1071
0
    return ec_gen_set_group(gctx, ec_group);
1072
0
}
1073
1074
0
#define COPY_INT_PARAM(params, key, val)                                       \
1075
0
p = OSSL_PARAM_locate_const(params, key);                                      \
1076
0
if (p != NULL && !OSSL_PARAM_get_int(p, &val))                                 \
1077
0
    goto err;
1078
1079
0
#define COPY_UTF8_PARAM(params, key, val)                                      \
1080
0
p = OSSL_PARAM_locate_const(params, key);                                      \
1081
0
if (p != NULL) {                                                               \
1082
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)                                \
1083
0
        goto err;                                                              \
1084
0
    OPENSSL_free(val);                                                         \
1085
0
    val = OPENSSL_strdup(p->data);                                             \
1086
0
    if (val == NULL)                                                           \
1087
0
        goto err;                                                              \
1088
0
}
1089
1090
0
#define COPY_OCTET_PARAM(params, key, val, len)                                \
1091
0
p = OSSL_PARAM_locate_const(params, key);                                      \
1092
0
if (p != NULL) {                                                               \
1093
0
    if (p->data_type != OSSL_PARAM_OCTET_STRING)                               \
1094
0
        goto err;                                                              \
1095
0
    OPENSSL_free(val);                                                         \
1096
0
    len = p->data_size;                                                        \
1097
0
    val = OPENSSL_memdup(p->data, p->data_size);                               \
1098
0
    if (val == NULL)                                                           \
1099
0
        goto err;                                                              \
1100
0
}
1101
1102
0
#define COPY_BN_PARAM(params, key, bn)                                         \
1103
0
p = OSSL_PARAM_locate_const(params, key);                                      \
1104
0
if (p != NULL) {                                                               \
1105
0
    if (bn == NULL)                                                            \
1106
0
        bn = BN_new();                                                         \
1107
0
    if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn))                              \
1108
0
        goto err;                                                              \
1109
0
}
1110
1111
static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
1112
0
{
1113
0
    int ret = 0;
1114
0
    struct ec_gen_ctx *gctx = genctx;
1115
0
    const OSSL_PARAM *p;
1116
1117
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params,
1118
0
                                     OSSL_PKEY_PARAM_FIPS_KEY_CHECK))
1119
0
        goto err;
1120
1121
0
    COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
1122
1123
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
1124
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
1125
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
1126
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
1127
0
    COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
1128
1129
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
1130
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
1131
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
1132
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
1133
0
    COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
1134
1135
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
1136
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
1137
0
                     gctx->gen_len);
1138
1139
0
    COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm,
1140
0
                     gctx->dhkem_ikmlen);
1141
1142
0
    ret = 1;
1143
0
err:
1144
0
    return ret;
1145
0
}
1146
1147
static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
1148
0
{
1149
0
    int ret = 0;
1150
0
    OSSL_PARAM_BLD *bld;
1151
0
    OSSL_PARAM *params = NULL;
1152
0
    EC_GROUP *group = NULL;
1153
1154
0
    bld = OSSL_PARAM_BLD_new();
1155
0
    if (bld == NULL)
1156
0
        return 0;
1157
1158
0
    if (gctx->encoding != NULL
1159
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
1160
0
                                            gctx->encoding, 0))
1161
0
        goto err;
1162
1163
0
    if (gctx->pt_format != NULL
1164
0
        && !OSSL_PARAM_BLD_push_utf8_string(bld,
1165
0
                                            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
1166
0
                                            gctx->pt_format, 0))
1167
0
        goto err;
1168
1169
0
    if (gctx->group_name != NULL) {
1170
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1171
0
                                             gctx->group_name, 0))
1172
0
            goto err;
1173
        /* Ignore any other parameters if there is a group name */
1174
0
        goto build;
1175
0
    } else if (gctx->field_type != NULL) {
1176
0
        if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1177
0
                                             gctx->field_type, 0))
1178
0
            goto err;
1179
0
    } else {
1180
0
        goto err;
1181
0
    }
1182
0
    if (gctx->p == NULL
1183
0
        || gctx->a == NULL
1184
0
        || gctx->b == NULL
1185
0
        || gctx->order == NULL
1186
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
1187
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
1188
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
1189
0
        || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
1190
0
        goto err;
1191
1192
0
    if (gctx->cofactor != NULL
1193
0
        && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1194
0
                                   gctx->cofactor))
1195
0
        goto err;
1196
1197
0
    if (gctx->seed != NULL
1198
0
        && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
1199
0
                                             gctx->seed, gctx->seed_len))
1200
0
        goto err;
1201
1202
0
    if (gctx->gen == NULL
1203
0
        || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
1204
0
                                             gctx->gen, gctx->gen_len))
1205
0
        goto err;
1206
0
build:
1207
0
    params = OSSL_PARAM_BLD_to_param(bld);
1208
0
    if (params == NULL)
1209
0
        goto err;
1210
0
    group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
1211
0
    if (group == NULL)
1212
0
        goto err;
1213
1214
0
    EC_GROUP_free(gctx->gen_group);
1215
0
    gctx->gen_group = group;
1216
1217
0
    ret = 1;
1218
0
err:
1219
0
    OSSL_PARAM_free(params);
1220
0
    OSSL_PARAM_BLD_free(bld);
1221
0
    return ret;
1222
0
}
1223
1224
static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx,
1225
                                                ossl_unused void *provctx)
1226
0
{
1227
0
    static OSSL_PARAM settable[] = {
1228
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
1229
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1230
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
1231
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
1232
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
1233
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
1234
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
1235
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
1236
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
1237
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
1238
0
        OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
1239
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
1240
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
1241
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_KEY_CHECK)
1242
0
        OSSL_PARAM_END
1243
0
    };
1244
0
    return settable;
1245
0
}
1246
1247
static const OSSL_PARAM *ec_gen_gettable_params(ossl_unused void *genctx,
1248
                                                ossl_unused void *provctx)
1249
0
{
1250
0
    static const OSSL_PARAM known_ec_gen_gettable_ctx_params[] = {
1251
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
1252
0
        OSSL_PARAM_END
1253
0
    };
1254
0
    return known_ec_gen_gettable_ctx_params;
1255
0
}
1256
1257
static int ec_gen_get_params(void *genctx, OSSL_PARAM *params)
1258
0
{
1259
0
    struct ec_gen_ctx *gctx = genctx;
1260
1261
0
    if (gctx == NULL)
1262
0
        return 0;
1263
1264
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params))
1265
0
        return 0;
1266
1267
0
    return 1;
1268
0
}
1269
1270
static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
1271
0
{
1272
0
    if (group == NULL) {
1273
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
1274
0
        return 0;
1275
0
    }
1276
0
    return EC_KEY_set_group(ec, group) > 0;
1277
0
}
1278
1279
/*
1280
 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1281
 */
1282
static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1283
0
{
1284
0
    struct ec_gen_ctx *gctx = genctx;
1285
0
    EC_KEY *ec = NULL;
1286
0
    int ret = 0;
1287
1288
0
    if (!ossl_prov_is_running()
1289
0
        || gctx == NULL
1290
0
        || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1291
0
        return NULL;
1292
1293
0
    if (gctx->gen_group == NULL) {
1294
0
        if (!ec_gen_set_group_from_params(gctx))
1295
0
            goto err;
1296
0
    } else {
1297
0
        if (gctx->encoding != NULL) {
1298
0
            int flags = ossl_ec_encoding_name2id(gctx->encoding);
1299
1300
0
            if (flags < 0)
1301
0
                goto err;
1302
0
            EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1303
0
        }
1304
0
        if (gctx->pt_format != NULL) {
1305
0
            int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1306
1307
0
            if (format < 0)
1308
0
                goto err;
1309
0
            EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1310
0
        }
1311
0
    }
1312
#ifdef FIPS_MODULE
1313
    if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(gctx),
1314
                                    OSSL_FIPS_IND_SETTABLE0, gctx->libctx,
1315
                                    gctx->gen_group, "EC KeyGen", 1))
1316
        goto err;
1317
#endif
1318
1319
    /* We must always assign a group, no matter what */
1320
0
    ret = ec_gen_assign_group(ec, gctx->gen_group);
1321
1322
    /* Whether you want it or not, you get a keypair, not just one half */
1323
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1324
0
#ifndef FIPS_MODULE
1325
0
        if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0)
1326
0
            ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm,
1327
0
                                                    gctx->dhkem_ikmlen);
1328
0
        else
1329
0
#endif
1330
0
            ret = ret && EC_KEY_generate_key(ec);
1331
0
    }
1332
1333
0
    if (gctx->ecdh_mode != -1)
1334
0
        ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
1335
1336
0
    if (gctx->group_check != NULL)
1337
0
        ret = ret && ossl_ec_set_check_group_type_from_name(ec,
1338
0
                                                            gctx->group_check);
1339
#ifdef FIPS_MODULE
1340
    if (ret > 0
1341
            && !ossl_fips_self_testing()
1342
            && EC_KEY_get0_public_key(ec) != NULL
1343
            && EC_KEY_get0_private_key(ec) != NULL
1344
            && EC_KEY_get0_group(ec) != NULL) {
1345
        BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
1346
1347
        ret = bnctx != NULL && ossl_ec_key_pairwise_check(ec, bnctx);
1348
        BN_CTX_free(bnctx);
1349
        if (ret <= 0)
1350
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
1351
    }
1352
#endif  /* FIPS_MODULE */
1353
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