Coverage Report

Created: 2024-11-21 07:03

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