Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/ec/ec_backend.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2026 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
 * Low level APIs related to EC_KEY are deprecated for public use,
12
 * but still ok for internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/core_names.h>
17
#include <openssl/objects.h>
18
#include <openssl/params.h>
19
#include <openssl/err.h>
20
#ifndef FIPS_MODULE
21
#include <openssl/x509.h>
22
#endif
23
#include "crypto/bn.h"
24
#include "crypto/ec.h"
25
#include "ec_local.h"
26
#include "internal/e_os.h"
27
#include "internal/nelem.h"
28
#include "internal/param_build_set.h"
29
30
#include <crypto/asn1.h>
31
32
/* Mapping between a flag and a name */
33
static const OSSL_ITEM encoding_nameid_map[] = {
34
    { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
35
    { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP },
36
};
37
38
static const OSSL_ITEM check_group_type_nameid_map[] = {
39
    { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT },
40
    { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED },
41
    { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST },
42
};
43
44
static const OSSL_ITEM format_nameid_map[] = {
45
    { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED },
46
    { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED },
47
    { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID },
48
};
49
50
int ossl_ec_encoding_name2id(const char *name)
51
60.1k
{
52
60.1k
    size_t i, sz;
53
54
    /* Return the default value if there is no name */
55
60.1k
    if (name == NULL)
56
0
        return OPENSSL_EC_NAMED_CURVE;
57
58
120k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
59
120k
        if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
60
60.1k
            return encoding_nameid_map[i].id;
61
120k
    }
62
0
    return -1;
63
60.1k
}
64
65
static char *ec_param_encoding_id2name(int id)
66
484k
{
67
484k
    size_t i, sz;
68
69
968k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
70
968k
        if (id == (int)encoding_nameid_map[i].id)
71
484k
            return encoding_nameid_map[i].ptr;
72
968k
    }
73
0
    return NULL;
74
484k
}
75
76
char *ossl_ec_check_group_type_id2name(int id)
77
458k
{
78
458k
    size_t i, sz;
79
80
458k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
81
458k
        if (id == (int)check_group_type_nameid_map[i].id)
82
458k
            return check_group_type_nameid_map[i].ptr;
83
458k
    }
84
0
    return NULL;
85
458k
}
86
87
static int ec_check_group_type_name2id(const char *name)
88
33.6k
{
89
33.6k
    size_t i, sz;
90
91
    /* Return the default value if there is no name */
92
33.6k
    if (name == NULL)
93
0
        return 0;
94
95
33.6k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
96
33.6k
        if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
97
33.6k
            return check_group_type_nameid_map[i].id;
98
33.6k
    }
99
0
    return -1;
100
33.6k
}
101
102
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
103
33.6k
{
104
33.6k
    int flags = ec_check_group_type_name2id(name);
105
106
33.6k
    if (flags == -1)
107
0
        return 0;
108
33.6k
    EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
109
33.6k
    EC_KEY_set_flags(ec, flags);
110
33.6k
    return 1;
111
33.6k
}
112
113
static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
114
33.6k
{
115
33.6k
    const char *name = NULL;
116
33.6k
    int status = 0;
117
118
33.6k
    switch (p->data_type) {
119
33.6k
    case OSSL_PARAM_UTF8_STRING:
120
33.6k
        name = p->data;
121
33.6k
        status = (name != NULL);
122
33.6k
        break;
123
0
    case OSSL_PARAM_UTF8_PTR:
124
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
125
0
        break;
126
33.6k
    }
127
33.6k
    if (status)
128
33.6k
        return ossl_ec_set_check_group_type_from_name(ec, name);
129
0
    return 0;
130
33.6k
}
131
132
int ossl_ec_pt_format_name2id(const char *name)
133
120k
{
134
120k
    size_t i, sz;
135
136
    /* Return the default value if there is no name */
137
120k
    if (name == NULL)
138
0
        return (int)POINT_CONVERSION_UNCOMPRESSED;
139
140
120k
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
141
120k
        if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
142
120k
            return format_nameid_map[i].id;
143
120k
    }
144
0
    return -1;
145
120k
}
146
147
char *ossl_ec_pt_format_id2name(int id)
148
800k
{
149
800k
    size_t i, sz;
150
151
1.09M
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
152
1.07M
        if (id == (int)format_nameid_map[i].id)
153
775k
            return format_nameid_map[i].ptr;
154
1.07M
    }
155
24.6k
    return NULL;
156
800k
}
157
158
static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
159
    OSSL_PARAM params[], BN_CTX *bnctx,
160
    unsigned char **genbuf)
161
277k
{
162
277k
    int ret = 0, fid;
163
277k
    const char *field_type;
164
277k
    const OSSL_PARAM *param = NULL;
165
277k
    const OSSL_PARAM *param_p = NULL;
166
277k
    const OSSL_PARAM *param_a = NULL;
167
277k
    const OSSL_PARAM *param_b = NULL;
168
169
277k
    fid = EC_GROUP_get_field_type(group);
170
171
277k
    if (fid == NID_X9_62_prime_field) {
172
208k
        field_type = SN_X9_62_prime_field;
173
208k
    } else if (fid == NID_X9_62_characteristic_two_field) {
174
#ifdef OPENSSL_NO_EC2M
175
        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
176
        goto err;
177
#else
178
69.6k
        field_type = SN_X9_62_characteristic_two_field;
179
69.6k
#endif
180
69.6k
    } else {
181
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
182
0
        return 0;
183
0
    }
184
185
277k
    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
186
277k
    param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
187
277k
    param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
188
277k
    if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL) {
189
0
        BIGNUM *p = BN_CTX_get(bnctx);
190
0
        BIGNUM *a = BN_CTX_get(bnctx);
191
0
        BIGNUM *b = BN_CTX_get(bnctx);
192
193
0
        if (b == NULL) {
194
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
195
0
            goto err;
196
0
        }
197
198
0
        if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) {
199
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
200
0
            goto err;
201
0
        }
202
0
        if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)
203
0
            || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)
204
0
            || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) {
205
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
206
0
            goto err;
207
0
        }
208
0
    }
209
210
277k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
211
277k
    if (tmpl != NULL || param != NULL) {
212
0
        const BIGNUM *order = EC_GROUP_get0_order(group);
213
214
0
        if (order == NULL) {
215
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
216
0
            goto err;
217
0
        }
218
0
        if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,
219
0
                order)) {
220
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
221
0
            goto err;
222
0
        }
223
0
    }
224
225
277k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
226
277k
    if (tmpl != NULL || param != NULL) {
227
0
        if (!ossl_param_build_set_utf8_string(tmpl, params,
228
0
                OSSL_PKEY_PARAM_EC_FIELD_TYPE,
229
0
                field_type)) {
230
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
231
0
            goto err;
232
0
        }
233
0
    }
234
235
277k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
236
277k
    if (tmpl != NULL || param != NULL) {
237
0
        size_t genbuf_len;
238
0
        const EC_POINT *genpt = EC_GROUP_get0_generator(group);
239
0
        point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group);
240
241
0
        if (genpt == NULL) {
242
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
243
0
            goto err;
244
0
        }
245
0
        genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);
246
0
        if (genbuf_len == 0) {
247
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
248
0
            goto err;
249
0
        }
250
0
        if (!ossl_param_build_set_octet_string(tmpl, params,
251
0
                OSSL_PKEY_PARAM_EC_GENERATOR,
252
0
                *genbuf, genbuf_len)) {
253
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
254
0
            goto err;
255
0
        }
256
0
    }
257
258
277k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
259
277k
    if (tmpl != NULL || param != NULL) {
260
0
        const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group);
261
262
0
        if (cofactor != NULL
263
0
            && !ossl_param_build_set_bn(tmpl, params,
264
0
                OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) {
265
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
266
0
            goto err;
267
0
        }
268
0
    }
269
270
277k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
271
277k
    if (tmpl != NULL || param != NULL) {
272
0
        unsigned char *seed = EC_GROUP_get0_seed(group);
273
0
        size_t seed_len = EC_GROUP_get_seed_len(group);
274
275
0
        if (seed != NULL
276
0
            && seed_len > 0
277
0
            && !ossl_param_build_set_octet_string(tmpl, params,
278
0
                OSSL_PKEY_PARAM_EC_SEED,
279
0
                seed, seed_len)) {
280
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
281
0
            goto err;
282
0
        }
283
0
    }
284
277k
    ret = 1;
285
277k
err:
286
277k
    return ret;
287
277k
}
288
289
int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
290
    OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
291
    const char *propq,
292
    BN_CTX *bnctx, unsigned char **genbuf)
293
337k
{
294
337k
    int ret = 0, curve_nid, encoding_flag;
295
337k
    const char *encoding_name, *pt_form_name;
296
337k
    point_conversion_form_t genform;
297
298
337k
    if (group == NULL) {
299
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
300
0
        return 0;
301
0
    }
302
303
337k
    genform = EC_GROUP_get_point_conversion_form(group);
304
337k
    pt_form_name = ossl_ec_pt_format_id2name(genform);
305
337k
    if (pt_form_name == NULL
306
337k
        || !ossl_param_build_set_utf8_string(
307
337k
            tmpl, params,
308
337k
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
309
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
310
0
        return 0;
311
0
    }
312
337k
    encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
313
337k
    encoding_name = ec_param_encoding_id2name(encoding_flag);
314
337k
    if (encoding_name == NULL
315
337k
        || !ossl_param_build_set_utf8_string(tmpl, params,
316
337k
            OSSL_PKEY_PARAM_EC_ENCODING,
317
337k
            encoding_name)) {
318
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
319
0
        return 0;
320
0
    }
321
322
337k
    if (!ossl_param_build_set_int(tmpl, params,
323
337k
            OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
324
337k
            group->decoded_from_explicit_params))
325
0
        return 0;
326
327
337k
    curve_nid = EC_GROUP_get_curve_name(group);
328
329
    /*
330
     * Get the explicit parameters in these two cases:
331
     * - We do not have a template, i.e. specific parameters are requested
332
     * - The curve is not a named curve
333
     */
334
337k
    if (tmpl == NULL || curve_nid == NID_undef)
335
277k
        if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
336
0
            goto err;
337
338
337k
    if (curve_nid != NID_undef) {
339
        /* Named curve */
340
336k
        const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
341
342
336k
        if (curve_name == NULL
343
336k
            || !ossl_param_build_set_utf8_string(tmpl, params,
344
336k
                OSSL_PKEY_PARAM_GROUP_NAME,
345
336k
                curve_name)) {
346
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
347
0
            goto err;
348
0
        }
349
336k
    }
350
337k
    ret = 1;
351
337k
err:
352
337k
    return ret;
353
337k
}
354
355
/*
356
 * The intention with the "backend" source file is to offer backend functions
357
 * for legacy backends (EVP_PKEY_ASN1_METHOD) and provider implementations
358
 * alike.
359
 */
360
int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
361
73.4k
{
362
73.4k
    const EC_GROUP *ecg = EC_KEY_get0_group(ec);
363
73.4k
    const BIGNUM *cofactor;
364
    /*
365
     * mode can be only 0 for disable, or 1 for enable here.
366
     *
367
     * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
368
     * also supports mode == -1 with the meaning of "reset to the default for
369
     * the associated key".
370
     */
371
73.4k
    if (mode < 0 || mode > 1)
372
0
        return 0;
373
374
73.4k
    if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL)
375
0
        return 0;
376
377
    /* ECDH cofactor mode has no effect if cofactor is 1 */
378
73.4k
    if (BN_is_one(cofactor))
379
73.4k
        return 1;
380
381
0
    if (mode == 1)
382
0
        EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
383
0
    else if (mode == 0)
384
0
        EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
385
386
0
    return 1;
387
73.4k
}
388
389
/*
390
 * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has
391
 * been called before!
392
 *
393
 * This function only gets the bare keypair, domain parameters and other
394
 * parameters are treated separately, and domain parameters are required to
395
 * define a keypair.
396
 */
397
int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
398
7.17k
{
399
7.17k
    const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
400
7.17k
    BN_CTX *ctx = NULL;
401
7.17k
    BIGNUM *priv_key = NULL;
402
7.17k
    unsigned char *pub_key = NULL;
403
7.17k
    size_t pub_key_len;
404
7.17k
    const EC_GROUP *ecg = NULL;
405
7.17k
    EC_POINT *pub_point = NULL;
406
7.17k
    int ok = 0;
407
408
7.17k
    ecg = EC_KEY_get0_group(ec);
409
7.17k
    if (ecg == NULL)
410
0
        return 0;
411
412
7.17k
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
413
7.17k
    if (include_private)
414
7.17k
        param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
415
416
7.17k
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
417
7.17k
    if (ctx == NULL)
418
0
        goto err;
419
420
7.17k
    if (param_pub_key != NULL)
421
7.17k
        if (!OSSL_PARAM_get_octet_string(param_pub_key,
422
7.17k
                (void **)&pub_key, 0, &pub_key_len)
423
7.17k
            || (pub_point = EC_POINT_new(ecg)) == NULL
424
7.17k
            || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
425
0
            goto err;
426
427
7.17k
    if (param_priv_key != NULL && include_private) {
428
7.17k
        int fixed_words;
429
7.17k
        const BIGNUM *order;
430
431
        /*
432
         * Key import/export should never leak the bit length of the secret
433
         * scalar in the key.
434
         *
435
         * For this reason, on export we use padded BIGNUMs with fixed length.
436
         *
437
         * When importing we also should make sure that, even if short lived,
438
         * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
439
         * soon as possible, so that any processing of this BIGNUM might opt for
440
         * constant time implementations in the backend.
441
         *
442
         * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
443
         * to preallocate the BIGNUM internal buffer to a fixed public size big
444
         * enough that operations performed during the processing never trigger
445
         * a realloc which would leak the size of the scalar through memory
446
         * accesses.
447
         *
448
         * Fixed Length
449
         * ------------
450
         *
451
         * The order of the large prime subgroup of the curve is our choice for
452
         * a fixed public size, as that is generally the upper bound for
453
         * generating a private key in EC cryptosystems and should fit all valid
454
         * secret scalars.
455
         *
456
         * For padding on export we just use the bit length of the order
457
         * converted to bytes (rounding up).
458
         *
459
         * For preallocating the BIGNUM storage we look at the number of "words"
460
         * required for the internal representation of the order, and we
461
         * preallocate 2 extra "words" in case any of the subsequent processing
462
         * might temporarily overflow the order length.
463
         */
464
7.17k
        order = EC_GROUP_get0_order(ecg);
465
7.17k
        if (order == NULL || BN_is_zero(order))
466
0
            goto err;
467
468
7.17k
        fixed_words = bn_get_top(order) + 2;
469
470
7.17k
        if ((priv_key = BN_secure_new()) == NULL)
471
0
            goto err;
472
7.17k
        if (bn_wexpand(priv_key, fixed_words) == NULL)
473
0
            goto err;
474
7.17k
        BN_set_flags(priv_key, BN_FLG_CONSTTIME);
475
476
7.17k
        if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
477
0
            goto err;
478
7.17k
    }
479
480
7.17k
    if (priv_key != NULL
481
7.17k
        && !EC_KEY_set_private_key(ec, priv_key))
482
0
        goto err;
483
484
7.17k
    if (pub_point != NULL
485
7.17k
        && !EC_KEY_set_public_key(ec, pub_point))
486
0
        goto err;
487
488
    /* Fallback computation of public key if not provided */
489
7.17k
    if (priv_key != NULL && pub_point == NULL) {
490
0
        if ((pub_point = EC_POINT_new(ecg)) == NULL
491
0
            || !EC_KEY_set_public_key(ec, pub_point))
492
0
            goto err;
493
0
        if (!ossl_ec_key_simple_generate_public_key(ec))
494
0
            goto err;
495
0
    }
496
497
7.17k
    ok = 1;
498
499
7.17k
err:
500
7.17k
    BN_CTX_free(ctx);
501
7.17k
    BN_clear_free(priv_key);
502
7.17k
    OPENSSL_free(pub_key);
503
7.17k
    EC_POINT_free(pub_point);
504
7.17k
    return ok;
505
7.17k
}
506
507
int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
508
60.1k
{
509
60.1k
    int ok = 0;
510
60.1k
    EC_GROUP *group = NULL;
511
512
60.1k
    if (ec == NULL)
513
0
        return 0;
514
515
60.1k
    group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
516
60.1k
        ossl_ec_key_get0_propq(ec));
517
518
60.1k
    if (!EC_KEY_set_group(ec, group))
519
0
        goto err;
520
60.1k
    ok = 1;
521
60.1k
err:
522
60.1k
    EC_GROUP_free(group);
523
60.1k
    return ok;
524
60.1k
}
525
526
static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
527
66.3k
{
528
66.3k
    const OSSL_PARAM *p;
529
66.3k
    int format = -1;
530
531
66.3k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
532
66.3k
    if (p != NULL) {
533
60.1k
        if (!ossl_ec_pt_format_param2id(p, &format)) {
534
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
535
0
            return 0;
536
0
        }
537
60.1k
        EC_KEY_set_conv_form(ec, format);
538
60.1k
    }
539
66.3k
    return 1;
540
66.3k
}
541
542
static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
543
66.3k
{
544
66.3k
    const OSSL_PARAM *p;
545
546
66.3k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
547
66.3k
    if (p != NULL)
548
33.6k
        return ec_set_check_group_type_from_param(ec, p);
549
32.6k
    return 1;
550
66.3k
}
551
552
static int ec_set_include_public(EC_KEY *ec, int include)
553
0
{
554
0
    int flags = EC_KEY_get_enc_flags(ec);
555
556
0
    if (!include)
557
0
        flags |= EC_PKEY_NO_PUBKEY;
558
0
    else
559
0
        flags &= ~EC_PKEY_NO_PUBKEY;
560
0
    EC_KEY_set_enc_flags(ec, flags);
561
0
    return 1;
562
0
}
563
564
int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
565
64.1k
{
566
64.1k
    const OSSL_PARAM *p;
567
568
64.1k
    if (ec == NULL)
569
0
        return 0;
570
571
64.1k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
572
64.1k
    if (p != NULL) {
573
60.1k
        int mode;
574
575
60.1k
        if (!OSSL_PARAM_get_int(p, &mode)
576
60.1k
            || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
577
0
            return 0;
578
60.1k
    }
579
580
64.1k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
581
64.1k
    if (p != NULL) {
582
0
        int include = 1;
583
584
0
        if (!OSSL_PARAM_get_int(p, &include)
585
0
            || !ec_set_include_public(ec, include))
586
0
            return 0;
587
0
    }
588
64.1k
    if (!ec_key_point_format_fromdata(ec, params))
589
0
        return 0;
590
64.1k
    if (!ec_key_group_check_fromdata(ec, params))
591
0
        return 0;
592
64.1k
    return 1;
593
64.1k
}
594
595
int ossl_ec_key_is_foreign(const EC_KEY *ec)
596
69.3k
{
597
69.3k
#ifndef FIPS_MODULE
598
69.3k
    if (EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
599
0
        return 1;
600
69.3k
#endif
601
69.3k
    return 0;
602
69.3k
}
603
604
EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
605
5.90k
{
606
5.90k
    EC_KEY *ret;
607
608
5.90k
    if (src == NULL) {
609
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
610
0
        return NULL;
611
0
    }
612
613
5.90k
    if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq)) == NULL)
614
0
        return NULL;
615
616
    /* copy the parameters */
617
5.90k
    if (src->group != NULL
618
5.90k
        && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
619
5.90k
        ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
620
5.90k
            src->group->meth);
621
5.90k
        if (ret->group == NULL
622
5.90k
            || !EC_GROUP_copy(ret->group, src->group))
623
0
            goto err;
624
625
5.90k
        if (src->meth != NULL)
626
5.90k
            ret->meth = src->meth;
627
5.90k
    }
628
629
    /*  copy the public key */
630
5.90k
    if (src->pub_key != NULL
631
5.42k
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
632
3.07k
        if (ret->group == NULL)
633
            /* no parameter-less keys allowed */
634
0
            goto err;
635
3.07k
        ret->pub_key = EC_POINT_new(ret->group);
636
3.07k
        if (ret->pub_key == NULL
637
3.07k
            || !EC_POINT_copy(ret->pub_key, src->pub_key))
638
0
            goto err;
639
3.07k
    }
640
641
    /* copy the private key */
642
5.90k
    if (src->priv_key != NULL
643
4.92k
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
644
2.58k
        if (ret->group == NULL)
645
            /* no parameter-less keys allowed */
646
0
            goto err;
647
2.58k
        ret->priv_key = BN_new();
648
2.58k
        if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
649
0
            goto err;
650
2.58k
        if (ret->group->meth->keycopy
651
0
            && ret->group->meth->keycopy(ret, src) == 0)
652
0
            goto err;
653
2.58k
    }
654
655
    /* copy the rest */
656
5.90k
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
657
3.56k
        ret->enc_flag = src->enc_flag;
658
3.56k
        ret->conv_form = src->conv_form;
659
3.56k
    }
660
661
5.90k
    ret->version = src->version;
662
5.90k
    ret->flags = src->flags;
663
664
5.90k
#ifndef FIPS_MODULE
665
5.90k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
666
5.90k
            &ret->ex_data, &src->ex_data))
667
0
        goto err;
668
5.90k
#endif
669
670
5.90k
    if (ret->meth != NULL && ret->meth->copy != NULL) {
671
0
        if ((selection
672
0
                & OSSL_KEYMGMT_SELECT_KEYPAIR)
673
0
            != OSSL_KEYMGMT_SELECT_KEYPAIR)
674
0
            goto err;
675
0
        if (ret->meth->copy(ret, src) == 0)
676
0
            goto err;
677
0
    }
678
679
5.90k
    return ret;
680
0
err:
681
0
    EC_KEY_free(ret);
682
0
    return NULL;
683
5.90k
}
684
685
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
686
60.1k
{
687
60.1k
    const char *name = NULL;
688
60.1k
    int status = 0;
689
690
60.1k
    switch (p->data_type) {
691
60.1k
    case OSSL_PARAM_UTF8_STRING:
692
        /* The OSSL_PARAM functions have no support for this */
693
60.1k
        name = p->data;
694
60.1k
        status = (name != NULL);
695
60.1k
        break;
696
0
    case OSSL_PARAM_UTF8_PTR:
697
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
698
0
        break;
699
60.1k
    }
700
60.1k
    if (status) {
701
60.1k
        int i = ossl_ec_encoding_name2id(name);
702
703
60.1k
        if (i >= 0) {
704
60.1k
            *id = i;
705
60.1k
            return 1;
706
60.1k
        }
707
60.1k
    }
708
0
    return 0;
709
60.1k
}
710
711
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
712
120k
{
713
120k
    const char *name = NULL;
714
120k
    int status = 0;
715
716
120k
    switch (p->data_type) {
717
120k
    case OSSL_PARAM_UTF8_STRING:
718
        /* The OSSL_PARAM functions have no support for this */
719
120k
        name = p->data;
720
120k
        status = (name != NULL);
721
120k
        break;
722
0
    case OSSL_PARAM_UTF8_PTR:
723
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
724
0
        break;
725
120k
    }
726
120k
    if (status) {
727
120k
        int i = ossl_ec_pt_format_name2id(name);
728
729
120k
        if (i >= 0) {
730
120k
            *id = i;
731
120k
            return 1;
732
120k
        }
733
120k
    }
734
0
    return 0;
735
120k
}
736
737
#ifndef FIPS_MODULE
738
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
739
664k
{
740
664k
    int ptype = 0;
741
664k
    const void *pval = NULL;
742
743
664k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
744
745
664k
    if (ptype == V_ASN1_OBJECT)
746
621k
        return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
747
748
42.8k
    if (ptype == V_ASN1_SEQUENCE) {
749
33.5k
        const ASN1_STRING *str = pval;
750
33.5k
        const unsigned char *der = str->data;
751
33.5k
        int derlen = str->length;
752
33.5k
        EC_GROUP *group;
753
33.5k
        int ret;
754
755
33.5k
        if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
756
32.9k
            ret = 0;
757
674
        else
758
674
            ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
759
760
33.5k
        EC_GROUP_free(group);
761
33.5k
        return ret;
762
33.5k
    }
763
764
9.28k
    return 0;
765
42.8k
}
766
767
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
768
    OSSL_LIB_CTX *libctx, const char *propq)
769
664k
{
770
664k
    int ptype = 0;
771
664k
    const void *pval = NULL;
772
664k
    EC_KEY *eckey = NULL;
773
664k
    EC_GROUP *group = NULL;
774
775
664k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
776
664k
    if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
777
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
778
0
        goto ecerr;
779
0
    }
780
781
664k
    if (ptype == V_ASN1_SEQUENCE) {
782
33.8k
        const ASN1_STRING *pstr = pval;
783
33.8k
        const unsigned char *pm = pstr->data;
784
33.8k
        int pmlen = pstr->length;
785
786
33.8k
        if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
787
33.0k
            ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
788
33.0k
            goto ecerr;
789
33.0k
        }
790
630k
    } else if (ptype == V_ASN1_OBJECT) {
791
621k
        const ASN1_OBJECT *poid = pval;
792
793
        /*
794
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
795
         */
796
797
621k
        group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
798
621k
        if (group == NULL)
799
50.4k
            goto ecerr;
800
570k
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
801
570k
        if (EC_KEY_set_group(eckey, group) == 0)
802
0
            goto ecerr;
803
570k
        EC_GROUP_free(group);
804
570k
    } else {
805
9.25k
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
806
9.25k
        goto ecerr;
807
9.25k
    }
808
809
571k
    return eckey;
810
811
92.7k
ecerr:
812
92.7k
    EC_KEY_free(eckey);
813
92.7k
    EC_GROUP_free(group);
814
92.7k
    return NULL;
815
664k
}
816
817
EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
818
    OSSL_LIB_CTX *libctx, const char *propq)
819
1.90k
{
820
1.90k
    const unsigned char *p = NULL;
821
1.90k
    int pklen;
822
1.90k
    EC_KEY *eckey = NULL;
823
1.90k
    const X509_ALGOR *palg;
824
825
1.90k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
826
0
        return 0;
827
1.90k
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
828
1.90k
    if (eckey == NULL)
829
390
        goto err;
830
831
    /* We have parameters now set private key */
832
1.51k
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
833
708
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
834
708
        goto err;
835
708
    }
836
837
808
    return eckey;
838
1.09k
err:
839
1.09k
    EC_KEY_free(eckey);
840
    return NULL;
841
1.51k
}
842
#endif