Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/ec/ec_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2001-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
/*
12
 * EC_GROUP low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <string.h>
18
#include <openssl/params.h>
19
#include <openssl/core_names.h>
20
#include <openssl/err.h>
21
#include <openssl/opensslv.h>
22
#include <openssl/param_build.h>
23
#include "crypto/ec.h"
24
#include "crypto/bn.h"
25
#include "internal/nelem.h"
26
#include "ec_local.h"
27
28
/* functions for EC_GROUP objects */
29
30
EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
31
    const EC_METHOD *meth)
32
1.19M
{
33
1.19M
    EC_GROUP *ret;
34
35
1.19M
    if (meth == NULL) {
36
0
        ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
37
0
        return NULL;
38
0
    }
39
1.19M
    if (meth->group_init == 0) {
40
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
41
0
        return NULL;
42
0
    }
43
44
1.19M
    ret = OPENSSL_zalloc(sizeof(*ret));
45
1.19M
    if (ret == NULL)
46
0
        return NULL;
47
48
1.19M
    ret->libctx = libctx;
49
1.19M
    if (propq != NULL) {
50
1.20k
        ret->propq = OPENSSL_strdup(propq);
51
1.20k
        if (ret->propq == NULL)
52
0
            goto err;
53
1.20k
    }
54
1.19M
    ret->meth = meth;
55
1.19M
    if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
56
1.19M
        ret->order = BN_new();
57
1.19M
        if (ret->order == NULL)
58
0
            goto err;
59
1.19M
        ret->cofactor = BN_new();
60
1.19M
        if (ret->cofactor == NULL)
61
0
            goto err;
62
1.19M
    }
63
1.19M
    ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
64
1.19M
    ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
65
1.19M
    if (!meth->group_init(ret))
66
0
        goto err;
67
1.19M
    return ret;
68
69
0
err:
70
0
    BN_free(ret->order);
71
0
    BN_free(ret->cofactor);
72
0
    OPENSSL_free(ret->propq);
73
0
    OPENSSL_free(ret);
74
0
    return NULL;
75
1.19M
}
76
77
#ifndef OPENSSL_NO_DEPRECATED_3_0
78
#ifndef FIPS_MODULE
79
EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
80
0
{
81
0
    return ossl_ec_group_new_ex(NULL, NULL, meth);
82
0
}
83
#endif
84
#endif
85
86
void EC_pre_comp_free(EC_GROUP *group)
87
1.77M
{
88
1.77M
    switch (group->pre_comp_type) {
89
1.77M
    case PCT_none:
90
1.77M
        break;
91
0
    case PCT_nistz256:
92
0
#ifdef ECP_NISTZ256_ASM
93
0
        EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
94
0
#endif
95
0
        break;
96
0
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
97
0
    case PCT_nistp224:
98
0
        EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
99
0
        break;
100
0
    case PCT_nistp256:
101
0
        EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
102
0
        break;
103
0
    case PCT_nistp384:
104
0
        ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);
105
0
        break;
106
0
    case PCT_nistp521:
107
0
        EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
108
0
        break;
109
#else
110
    case PCT_nistp224:
111
    case PCT_nistp256:
112
    case PCT_nistp384:
113
    case PCT_nistp521:
114
        break;
115
#endif
116
0
    case PCT_ec:
117
0
        EC_ec_pre_comp_free(group->pre_comp.ec);
118
0
        break;
119
1.77M
    }
120
1.77M
    group->pre_comp.ec = NULL;
121
1.77M
}
122
123
void EC_GROUP_free(EC_GROUP *group)
124
2.10M
{
125
2.10M
    if (!group)
126
903k
        return;
127
128
1.19M
    if (group->meth->group_finish != 0)
129
1.19M
        group->meth->group_finish(group);
130
131
1.19M
    EC_pre_comp_free(group);
132
1.19M
    BN_MONT_CTX_free(group->mont_data);
133
1.19M
    EC_POINT_free(group->generator);
134
1.19M
    BN_free(group->order);
135
1.19M
    BN_free(group->cofactor);
136
1.19M
    OPENSSL_free(group->seed);
137
1.19M
    OPENSSL_free(group->propq);
138
1.19M
    OPENSSL_free(group);
139
1.19M
}
140
141
#ifndef OPENSSL_NO_DEPRECATED_3_0
142
void EC_GROUP_clear_free(EC_GROUP *group)
143
0
{
144
0
    if (!group)
145
0
        return;
146
147
0
    if (group->meth->group_clear_finish != 0)
148
0
        group->meth->group_clear_finish(group);
149
0
    else if (group->meth->group_finish != 0)
150
0
        group->meth->group_finish(group);
151
152
0
    EC_pre_comp_free(group);
153
0
    BN_MONT_CTX_free(group->mont_data);
154
0
    EC_POINT_clear_free(group->generator);
155
0
    BN_clear_free(group->order);
156
0
    BN_clear_free(group->cofactor);
157
0
    OPENSSL_clear_free(group->seed, group->seed_len);
158
0
    OPENSSL_clear_free(group, sizeof(*group));
159
0
}
160
#endif
161
162
int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
163
395k
{
164
395k
    if (dest->meth->group_copy == 0) {
165
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
166
0
        return 0;
167
0
    }
168
395k
    if (dest->meth != src->meth) {
169
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
170
0
        return 0;
171
0
    }
172
395k
    if (dest == src)
173
0
        return 1;
174
175
395k
    dest->libctx = src->libctx;
176
395k
    dest->curve_name = src->curve_name;
177
178
395k
    EC_pre_comp_free(dest);
179
180
    /* Copy precomputed */
181
395k
    dest->pre_comp_type = src->pre_comp_type;
182
395k
    switch (src->pre_comp_type) {
183
395k
    case PCT_none:
184
395k
        dest->pre_comp.ec = NULL;
185
395k
        break;
186
0
    case PCT_nistz256:
187
0
#ifdef ECP_NISTZ256_ASM
188
0
        dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
189
0
#endif
190
0
        break;
191
0
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
192
0
    case PCT_nistp224:
193
0
        dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
194
0
        break;
195
0
    case PCT_nistp256:
196
0
        dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
197
0
        break;
198
0
    case PCT_nistp384:
199
0
        dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);
200
0
        break;
201
0
    case PCT_nistp521:
202
0
        dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
203
0
        break;
204
#else
205
    case PCT_nistp224:
206
    case PCT_nistp256:
207
    case PCT_nistp384:
208
    case PCT_nistp521:
209
        break;
210
#endif
211
0
    case PCT_ec:
212
0
        dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
213
0
        break;
214
395k
    }
215
216
395k
    if (src->mont_data != NULL) {
217
393k
        if (dest->mont_data == NULL) {
218
393k
            dest->mont_data = BN_MONT_CTX_new();
219
393k
            if (dest->mont_data == NULL)
220
0
                return 0;
221
393k
        }
222
393k
        if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
223
0
            return 0;
224
393k
    } else {
225
        /* src->generator == NULL */
226
2.02k
        BN_MONT_CTX_free(dest->mont_data);
227
2.02k
        dest->mont_data = NULL;
228
2.02k
    }
229
230
395k
    if (src->generator != NULL) {
231
395k
        if (dest->generator == NULL) {
232
395k
            dest->generator = EC_POINT_new(dest);
233
395k
            if (dest->generator == NULL)
234
0
                return 0;
235
395k
        }
236
395k
        if (!EC_POINT_copy(dest->generator, src->generator))
237
0
            return 0;
238
395k
    } else {
239
        /* src->generator == NULL */
240
0
        EC_POINT_clear_free(dest->generator);
241
0
        dest->generator = NULL;
242
0
    }
243
244
395k
    if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
245
395k
        if (!BN_copy(dest->order, src->order))
246
0
            return 0;
247
395k
        if (!BN_copy(dest->cofactor, src->cofactor))
248
0
            return 0;
249
395k
    }
250
251
395k
    dest->asn1_flag = src->asn1_flag;
252
395k
    dest->asn1_form = src->asn1_form;
253
395k
    dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
254
255
395k
    if (src->seed) {
256
309k
        OPENSSL_free(dest->seed);
257
309k
        if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)
258
0
            return 0;
259
309k
        if (!memcpy(dest->seed, src->seed, src->seed_len))
260
0
            return 0;
261
309k
        dest->seed_len = src->seed_len;
262
309k
    } else {
263
86.2k
        OPENSSL_free(dest->seed);
264
86.2k
        dest->seed = NULL;
265
86.2k
        dest->seed_len = 0;
266
86.2k
    }
267
268
395k
    return dest->meth->group_copy(dest, src);
269
395k
}
270
271
EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
272
565k
{
273
565k
    EC_GROUP *t = NULL;
274
565k
    int ok = 0;
275
276
565k
    if (a == NULL)
277
0
        return NULL;
278
279
565k
    if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
280
0
        return NULL;
281
565k
    if (!EC_GROUP_copy(t, a))
282
0
        goto err;
283
284
565k
    ok = 1;
285
286
565k
err:
287
565k
    if (!ok) {
288
0
        EC_GROUP_free(t);
289
0
        return NULL;
290
0
    }
291
565k
    return t;
292
565k
}
293
294
#ifndef OPENSSL_NO_DEPRECATED_3_0
295
const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
296
0
{
297
0
    return group->meth;
298
0
}
299
300
int EC_METHOD_get_field_type(const EC_METHOD *meth)
301
0
{
302
0
    return meth->field_type;
303
0
}
304
#endif
305
306
static int ec_precompute_mont_data(EC_GROUP *);
307
308
/*-
309
 * Try computing cofactor from the generator order (n) and field cardinality (q).
310
 * This works for all curves of cryptographic interest.
311
 *
312
 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
313
 * h_min = (q + 1 - 2*sqrt(q))/n
314
 * h_max = (q + 1 + 2*sqrt(q))/n
315
 * h_max - h_min = 4*sqrt(q)/n
316
 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
317
 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
318
 *
319
 * Otherwise, zero cofactor and return success.
320
 */
321
static int ec_guess_cofactor(EC_GROUP *group)
322
8.20k
{
323
8.20k
    int ret = 0;
324
8.20k
    BN_CTX *ctx = NULL;
325
8.20k
    BIGNUM *q = NULL;
326
327
    /*-
328
     * If the cofactor is too large, we cannot guess it.
329
     * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
330
     */
331
8.20k
    if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
332
        /* default to 0 */
333
5.39k
        BN_zero(group->cofactor);
334
        /* return success */
335
5.39k
        return 1;
336
5.39k
    }
337
338
2.81k
    if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
339
0
        return 0;
340
341
2.81k
    BN_CTX_start(ctx);
342
2.81k
    if ((q = BN_CTX_get(ctx)) == NULL)
343
0
        goto err;
344
345
    /* set q = 2**m for binary fields; q = p otherwise */
346
2.81k
    if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
347
0
        BN_zero(q);
348
0
        if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
349
0
            goto err;
350
2.81k
    } else {
351
2.81k
        if (!BN_copy(q, group->field))
352
0
            goto err;
353
2.81k
    }
354
355
    /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
356
2.81k
    if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
357
2.81k
        || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
358
        /* q + 1 + n/2 */
359
2.81k
        || !BN_add(group->cofactor, group->cofactor, BN_value_one())
360
        /* (q + 1 + n/2)/n */
361
2.81k
        || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
362
0
        goto err;
363
2.81k
    ret = 1;
364
2.81k
err:
365
2.81k
    BN_CTX_end(ctx);
366
2.81k
    BN_CTX_free(ctx);
367
2.81k
    return ret;
368
2.81k
}
369
370
int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
371
    const BIGNUM *order, const BIGNUM *cofactor)
372
292k
{
373
292k
    if (generator == NULL) {
374
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
375
0
        return 0;
376
0
    }
377
378
    /* require group->field >= 1 */
379
292k
    if (group->field == NULL || BN_is_zero(group->field)
380
292k
        || BN_is_negative(group->field)) {
381
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
382
0
        return 0;
383
0
    }
384
385
    /*-
386
     * - require order >= 1
387
     * - enforce upper bound due to Hasse thm: order can be no more than one bit
388
     *   longer than field cardinality
389
     */
390
292k
    if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
391
292k
        || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
392
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
393
0
        return 0;
394
0
    }
395
396
    /*-
397
     * Unfortunately the cofactor is an optional field in many standards.
398
     * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
399
     * So accept cofactor == NULL or cofactor >= 0.
400
     */
401
292k
    if (cofactor != NULL && BN_is_negative(cofactor)) {
402
92
        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
403
92
        return 0;
404
92
    }
405
406
292k
    if (group->generator == NULL) {
407
287k
        group->generator = EC_POINT_new(group);
408
287k
        if (group->generator == NULL)
409
0
            return 0;
410
287k
    }
411
292k
    if (!EC_POINT_copy(group->generator, generator))
412
0
        return 0;
413
414
292k
    if (!BN_copy(group->order, order))
415
0
        return 0;
416
417
    /* Either take the provided positive cofactor, or try to compute it */
418
292k
    if (cofactor != NULL && !BN_is_zero(cofactor)) {
419
284k
        if (!BN_copy(group->cofactor, cofactor))
420
0
            return 0;
421
284k
    } else if (!ec_guess_cofactor(group)) {
422
0
        BN_zero(group->cofactor);
423
0
        return 0;
424
0
    }
425
426
    /*
427
     * Some groups have an order with
428
     * factors of two, which makes the Montgomery setup fail.
429
     * |group->mont_data| will be NULL in this case.
430
     */
431
292k
    if (BN_is_odd(group->order)) {
432
289k
        return ec_precompute_mont_data(group);
433
289k
    }
434
435
3.59k
    BN_MONT_CTX_free(group->mont_data);
436
3.59k
    group->mont_data = NULL;
437
3.59k
    return 1;
438
292k
}
439
440
const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
441
234k
{
442
234k
    return group->generator;
443
234k
}
444
445
BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
446
0
{
447
0
    return group->mont_data;
448
0
}
449
450
int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
451
20.0k
{
452
20.0k
    if (group->order == NULL)
453
0
        return 0;
454
20.0k
    if (!BN_copy(order, group->order))
455
0
        return 0;
456
457
20.0k
    return !BN_is_zero(order);
458
20.0k
}
459
460
const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
461
658k
{
462
658k
    return group->order;
463
658k
}
464
465
int EC_GROUP_order_bits(const EC_GROUP *group)
466
690k
{
467
690k
    return group->meth->group_order_bits(group);
468
690k
}
469
470
int EC_GROUP_security_bits(const EC_GROUP *group)
471
139k
{
472
139k
    int ecbits = group->meth->group_order_bits(group);
473
474
    /*
475
     * The following estimates are based on the values published in Table 2 of
476
     * "NIST Special Publication 800-57 Part 1 Revision 4" at
477
     * http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
478
     *
479
     * Note that the above reference explicitly categorizes algorithms in a
480
     * discrete set of values {80, 112, 128, 192, 256}, and that it is relevant
481
     * only for NIST approved Elliptic Curves, while OpenSSL applies the same
482
     * logic also to other curves.
483
     */
484
139k
    if (ecbits >= 512)
485
2.63k
        return 256;
486
137k
    if (ecbits >= 384)
487
4.66k
        return 192;
488
132k
    if (ecbits >= 256)
489
41.7k
        return 128;
490
90.7k
    if (ecbits >= 224)
491
19.5k
        return 112;
492
71.2k
    if (ecbits >= 160)
493
18.5k
        return 80;
494
52.6k
    return ecbits / 2;
495
71.2k
}
496
497
int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
498
    BN_CTX *ctx)
499
0
{
500
0
    if (group->cofactor == NULL)
501
0
        return 0;
502
0
    if (!BN_copy(cofactor, group->cofactor))
503
0
        return 0;
504
505
0
    return !BN_is_zero(group->cofactor);
506
0
}
507
508
const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
509
304k
{
510
304k
    return group->cofactor;
511
304k
}
512
513
void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
514
614k
{
515
614k
    group->curve_name = nid;
516
614k
    group->asn1_flag = (nid != NID_undef)
517
614k
        ? OPENSSL_EC_NAMED_CURVE
518
614k
        : OPENSSL_EC_EXPLICIT_CURVE;
519
614k
}
520
521
int EC_GROUP_get_curve_name(const EC_GROUP *group)
522
2.11M
{
523
2.11M
    return group->curve_name;
524
2.11M
}
525
526
const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
527
0
{
528
0
    return group->field;
529
0
}
530
531
int EC_GROUP_get_field_type(const EC_GROUP *group)
532
1.19M
{
533
1.19M
    return group->meth->field_type;
534
1.19M
}
535
536
void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
537
609k
{
538
609k
    group->asn1_flag = flag;
539
609k
}
540
541
int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
542
887k
{
543
887k
    return group->asn1_flag;
544
887k
}
545
546
void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
547
    point_conversion_form_t form)
548
235k
{
549
235k
    group->asn1_form = form;
550
235k
}
551
552
point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
553
        *group)
554
426k
{
555
426k
    return group->asn1_form;
556
426k
}
557
558
size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
559
490k
{
560
490k
    OPENSSL_free(group->seed);
561
490k
    group->seed = NULL;
562
490k
    group->seed_len = 0;
563
564
490k
    if (!len || !p)
565
5.67k
        return 1;
566
567
484k
    if ((group->seed = OPENSSL_malloc(len)) == NULL)
568
0
        return 0;
569
484k
    memcpy(group->seed, p, len);
570
484k
    group->seed_len = len;
571
572
484k
    return len;
573
484k
}
574
575
unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
576
36.4k
{
577
36.4k
    return group->seed;
578
36.4k
}
579
580
size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
581
35.5k
{
582
35.5k
    return group->seed_len;
583
35.5k
}
584
585
int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
586
    const BIGNUM *b, BN_CTX *ctx)
587
357k
{
588
357k
    if (group->meth->group_set_curve == 0) {
589
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
590
0
        return 0;
591
0
    }
592
357k
    return group->meth->group_set_curve(group, p, a, b, ctx);
593
357k
}
594
595
int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
596
    BN_CTX *ctx)
597
38.3k
{
598
38.3k
    if (group->meth->group_get_curve == NULL) {
599
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
600
0
        return 0;
601
0
    }
602
38.3k
    return group->meth->group_get_curve(group, p, a, b, ctx);
603
38.3k
}
604
605
#ifndef OPENSSL_NO_DEPRECATED_3_0
606
int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
607
    const BIGNUM *b, BN_CTX *ctx)
608
0
{
609
0
    return EC_GROUP_set_curve(group, p, a, b, ctx);
610
0
}
611
612
int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
613
    BIGNUM *b, BN_CTX *ctx)
614
0
{
615
0
    return EC_GROUP_get_curve(group, p, a, b, ctx);
616
0
}
617
618
#ifndef OPENSSL_NO_EC2M
619
int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
620
    const BIGNUM *b, BN_CTX *ctx)
621
0
{
622
0
    return EC_GROUP_set_curve(group, p, a, b, ctx);
623
0
}
624
625
int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
626
    BIGNUM *b, BN_CTX *ctx)
627
0
{
628
0
    return EC_GROUP_get_curve(group, p, a, b, ctx);
629
0
}
630
#endif
631
#endif
632
633
int EC_GROUP_get_degree(const EC_GROUP *group)
634
345k
{
635
345k
    if (group->meth->group_get_degree == 0) {
636
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
637
0
        return 0;
638
0
    }
639
345k
    return group->meth->group_get_degree(group);
640
345k
}
641
642
int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
643
3.56k
{
644
3.56k
    if (group->meth->group_check_discriminant == 0) {
645
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
646
0
        return 0;
647
0
    }
648
3.56k
    return group->meth->group_check_discriminant(group, ctx);
649
3.56k
}
650
651
int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
652
90.9k
{
653
90.9k
    int r = 0;
654
90.9k
    BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
655
90.9k
#ifndef FIPS_MODULE
656
90.9k
    BN_CTX *ctx_new = NULL;
657
90.9k
#endif
658
659
    /* compare the field types */
660
90.9k
    if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
661
0
        return 1;
662
    /* compare the curve name (if present in both) */
663
90.9k
    if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
664
0
        return 1;
665
90.9k
    if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
666
0
        return 0;
667
668
90.9k
#ifndef FIPS_MODULE
669
90.9k
    if (ctx == NULL)
670
0
        ctx_new = ctx = BN_CTX_new();
671
90.9k
#endif
672
90.9k
    if (ctx == NULL)
673
0
        return -1;
674
675
90.9k
    BN_CTX_start(ctx);
676
90.9k
    a1 = BN_CTX_get(ctx);
677
90.9k
    a2 = BN_CTX_get(ctx);
678
90.9k
    a3 = BN_CTX_get(ctx);
679
90.9k
    b1 = BN_CTX_get(ctx);
680
90.9k
    b2 = BN_CTX_get(ctx);
681
90.9k
    b3 = BN_CTX_get(ctx);
682
90.9k
    if (b3 == NULL) {
683
0
        BN_CTX_end(ctx);
684
0
#ifndef FIPS_MODULE
685
0
        BN_CTX_free(ctx_new);
686
0
#endif
687
0
        return -1;
688
0
    }
689
690
    /*
691
     * XXX This approach assumes that the external representation of curves
692
     * over the same field type is the same.
693
     */
694
90.9k
    if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || !b->meth->group_get_curve(b, b1, b2, b3, ctx))
695
0
        r = 1;
696
697
    /* return 1 if the curve parameters are different */
698
90.9k
    if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
699
0
        r = 1;
700
701
    /* XXX EC_POINT_cmp() assumes that the methods are equal */
702
    /* return 1 if the generators are different */
703
90.9k
    if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), EC_GROUP_get0_generator(b), ctx) != 0)
704
0
        r = 1;
705
706
90.9k
    if (!r) {
707
90.9k
        const BIGNUM *ao, *bo, *ac, *bc;
708
        /* compare the orders */
709
90.9k
        ao = EC_GROUP_get0_order(a);
710
90.9k
        bo = EC_GROUP_get0_order(b);
711
90.9k
        if (ao == NULL || bo == NULL) {
712
            /* return an error if either order is NULL */
713
0
            r = -1;
714
0
            goto end;
715
0
        }
716
90.9k
        if (BN_cmp(ao, bo) != 0) {
717
            /* return 1 if orders are different */
718
0
            r = 1;
719
0
            goto end;
720
0
        }
721
        /*
722
         * It gets here if the curve parameters and generator matched.
723
         * Now check the optional cofactors (if both are present).
724
         */
725
90.9k
        ac = EC_GROUP_get0_cofactor(a);
726
90.9k
        bc = EC_GROUP_get0_cofactor(b);
727
        /* Returns 1 (mismatch) if both cofactors are specified and different */
728
90.9k
        if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
729
0
            r = 1;
730
        /* Returns 0 if the parameters matched */
731
90.9k
    }
732
90.9k
end:
733
90.9k
    BN_CTX_end(ctx);
734
90.9k
#ifndef FIPS_MODULE
735
90.9k
    BN_CTX_free(ctx_new);
736
90.9k
#endif
737
90.9k
    return r;
738
90.9k
}
739
740
/* functions for EC_POINT objects */
741
742
EC_POINT *EC_POINT_new(const EC_GROUP *group)
743
2.56M
{
744
2.56M
    EC_POINT *ret;
745
746
2.56M
    if (group == NULL) {
747
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
748
0
        return NULL;
749
0
    }
750
2.56M
    if (group->meth->point_init == NULL) {
751
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
752
0
        return NULL;
753
0
    }
754
755
2.56M
    ret = OPENSSL_zalloc(sizeof(*ret));
756
2.56M
    if (ret == NULL)
757
0
        return NULL;
758
759
2.56M
    ret->meth = group->meth;
760
2.56M
    ret->curve_name = group->curve_name;
761
762
2.56M
    if (!ret->meth->point_init(ret)) {
763
0
        OPENSSL_free(ret);
764
0
        return NULL;
765
0
    }
766
767
2.56M
    return ret;
768
2.56M
}
769
770
void EC_POINT_free(EC_POINT *point)
771
2.78M
{
772
2.78M
    if (point == NULL)
773
243k
        return;
774
775
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
776
    EC_POINT_clear_free(point);
777
#else
778
2.53M
    if (point->meth->point_finish != 0)
779
2.53M
        point->meth->point_finish(point);
780
2.53M
    OPENSSL_free(point);
781
2.53M
#endif
782
2.53M
}
783
784
void EC_POINT_clear_free(EC_POINT *point)
785
88.0k
{
786
88.0k
    if (point == NULL)
787
60.1k
        return;
788
789
27.8k
    if (point->meth->point_clear_finish != 0)
790
27.8k
        point->meth->point_clear_finish(point);
791
0
    else if (point->meth->point_finish != 0)
792
0
        point->meth->point_finish(point);
793
27.8k
    OPENSSL_clear_free(point, sizeof(*point));
794
27.8k
}
795
796
int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
797
1.29M
{
798
1.29M
    if (dest->meth->point_copy == 0) {
799
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
800
0
        return 0;
801
0
    }
802
1.29M
    if (dest->meth != src->meth
803
1.29M
        || (dest->curve_name != src->curve_name
804
129k
            && dest->curve_name != 0
805
129k
            && src->curve_name != 0)) {
806
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
807
0
        return 0;
808
0
    }
809
1.29M
    if (dest == src)
810
1.41k
        return 1;
811
1.29M
    return dest->meth->point_copy(dest, src);
812
1.29M
}
813
814
EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
815
58.4k
{
816
58.4k
    EC_POINT *t;
817
58.4k
    int r;
818
819
58.4k
    if (a == NULL)
820
0
        return NULL;
821
822
58.4k
    t = EC_POINT_new(group);
823
58.4k
    if (t == NULL)
824
0
        return NULL;
825
58.4k
    r = EC_POINT_copy(t, a);
826
58.4k
    if (!r) {
827
0
        EC_POINT_free(t);
828
0
        return NULL;
829
0
    }
830
58.4k
    return t;
831
58.4k
}
832
833
#ifndef OPENSSL_NO_DEPRECATED_3_0
834
const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
835
0
{
836
0
    return point->meth;
837
0
}
838
#endif
839
840
int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
841
25.7k
{
842
25.7k
    if (group->meth->point_set_to_infinity == 0) {
843
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
844
0
        return 0;
845
0
    }
846
25.7k
    if (group->meth != point->meth) {
847
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
848
0
        return 0;
849
0
    }
850
25.7k
    return group->meth->point_set_to_infinity(group, point);
851
25.7k
}
852
853
#ifndef OPENSSL_NO_DEPRECATED_3_0
854
int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
855
    EC_POINT *point, const BIGNUM *x,
856
    const BIGNUM *y, const BIGNUM *z,
857
    BN_CTX *ctx)
858
637k
{
859
637k
    if (group->meth->field_type != NID_X9_62_prime_field) {
860
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
861
0
        return 0;
862
0
    }
863
637k
    if (!ec_point_is_compat(point, group)) {
864
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
865
0
        return 0;
866
0
    }
867
637k
    return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
868
637k
        x, y, z, ctx);
869
637k
}
870
871
int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
872
    const EC_POINT *point, BIGNUM *x,
873
    BIGNUM *y, BIGNUM *z,
874
    BN_CTX *ctx)
875
0
{
876
0
    if (group->meth->field_type != NID_X9_62_prime_field) {
877
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
878
0
        return 0;
879
0
    }
880
0
    if (!ec_point_is_compat(point, group)) {
881
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
882
0
        return 0;
883
0
    }
884
0
    return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
885
0
        x, y, z, ctx);
886
0
}
887
#endif
888
889
int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
890
    const BIGNUM *x, const BIGNUM *y,
891
    BN_CTX *ctx)
892
992k
{
893
992k
    if (group->meth->point_set_affine_coordinates == NULL) {
894
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
895
0
        return 0;
896
0
    }
897
992k
    if (!ec_point_is_compat(point, group)) {
898
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
899
0
        return 0;
900
0
    }
901
992k
    if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
902
0
        return 0;
903
904
992k
    if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
905
27.5k
        ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
906
27.5k
        return 0;
907
27.5k
    }
908
965k
    return 1;
909
992k
}
910
911
#ifndef OPENSSL_NO_DEPRECATED_3_0
912
int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
913
    EC_POINT *point, const BIGNUM *x,
914
    const BIGNUM *y, BN_CTX *ctx)
915
0
{
916
0
    return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
917
0
}
918
919
#ifndef OPENSSL_NO_EC2M
920
int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
921
    EC_POINT *point, const BIGNUM *x,
922
    const BIGNUM *y, BN_CTX *ctx)
923
0
{
924
0
    return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
925
0
}
926
#endif
927
#endif
928
929
int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
930
    const EC_POINT *point, BIGNUM *x, BIGNUM *y,
931
    BN_CTX *ctx)
932
113k
{
933
113k
    if (group->meth->point_get_affine_coordinates == NULL) {
934
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
935
0
        return 0;
936
0
    }
937
113k
    if (!ec_point_is_compat(point, group)) {
938
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
939
0
        return 0;
940
0
    }
941
113k
    if (EC_POINT_is_at_infinity(group, point)) {
942
961
        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
943
961
        return 0;
944
961
    }
945
112k
    return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
946
113k
}
947
948
#ifndef OPENSSL_NO_DEPRECATED_3_0
949
int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
950
    const EC_POINT *point, BIGNUM *x,
951
    BIGNUM *y, BN_CTX *ctx)
952
0
{
953
0
    return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
954
0
}
955
956
#ifndef OPENSSL_NO_EC2M
957
int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
958
    const EC_POINT *point, BIGNUM *x,
959
    BIGNUM *y, BN_CTX *ctx)
960
0
{
961
0
    return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
962
0
}
963
#endif
964
#endif
965
966
int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
967
    const EC_POINT *b, BN_CTX *ctx)
968
69.3k
{
969
69.3k
    if (group->meth->add == 0) {
970
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971
0
        return 0;
972
0
    }
973
69.3k
    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
974
69.3k
        || !ec_point_is_compat(b, group)) {
975
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
976
0
        return 0;
977
0
    }
978
69.3k
    return group->meth->add(group, r, a, b, ctx);
979
69.3k
}
980
981
int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
982
    BN_CTX *ctx)
983
464k
{
984
464k
    if (group->meth->dbl == 0) {
985
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
986
0
        return 0;
987
0
    }
988
464k
    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
989
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
990
0
        return 0;
991
0
    }
992
464k
    return group->meth->dbl(group, r, a, ctx);
993
464k
}
994
995
int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
996
29.7k
{
997
29.7k
    if (group->meth->invert == 0) {
998
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
999
0
        return 0;
1000
0
    }
1001
29.7k
    if (!ec_point_is_compat(a, group)) {
1002
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1003
0
        return 0;
1004
0
    }
1005
29.7k
    return group->meth->invert(group, a, ctx);
1006
29.7k
}
1007
1008
int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1009
2.35M
{
1010
2.35M
    if (group->meth->is_at_infinity == 0) {
1011
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1012
0
        return 0;
1013
0
    }
1014
2.35M
    if (!ec_point_is_compat(point, group)) {
1015
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1016
0
        return 0;
1017
0
    }
1018
2.35M
    return group->meth->is_at_infinity(group, point);
1019
2.35M
}
1020
1021
/*
1022
 * Check whether an EC_POINT is on the curve or not. Note that the return
1023
 * value for this function should NOT be treated as a boolean. Return values:
1024
 *  1: The point is on the curve
1025
 *  0: The point is not on the curve
1026
 * -1: An error occurred
1027
 */
1028
int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1029
    BN_CTX *ctx)
1030
1.00M
{
1031
1.00M
    if (group->meth->is_on_curve == 0) {
1032
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1033
0
        return 0;
1034
0
    }
1035
1.00M
    if (!ec_point_is_compat(point, group)) {
1036
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1037
0
        return 0;
1038
0
    }
1039
1.00M
    return group->meth->is_on_curve(group, point, ctx);
1040
1.00M
}
1041
1042
int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1043
    BN_CTX *ctx)
1044
143k
{
1045
143k
    if (group->meth->point_cmp == 0) {
1046
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1047
0
        return -1;
1048
0
    }
1049
143k
    if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1050
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1051
0
        return -1;
1052
0
    }
1053
143k
    return group->meth->point_cmp(group, a, b, ctx);
1054
143k
}
1055
1056
#ifndef OPENSSL_NO_DEPRECATED_3_0
1057
int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1058
0
{
1059
0
    if (group->meth->make_affine == 0) {
1060
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1061
0
        return 0;
1062
0
    }
1063
0
    if (!ec_point_is_compat(point, group)) {
1064
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1065
0
        return 0;
1066
0
    }
1067
0
    return group->meth->make_affine(group, point, ctx);
1068
0
}
1069
1070
int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1071
    EC_POINT *points[], BN_CTX *ctx)
1072
0
{
1073
0
    size_t i;
1074
1075
0
    if (group->meth->points_make_affine == 0) {
1076
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1077
0
        return 0;
1078
0
    }
1079
0
    for (i = 0; i < num; i++) {
1080
0
        if (!ec_point_is_compat(points[i], group)) {
1081
0
            ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1082
0
            return 0;
1083
0
        }
1084
0
    }
1085
0
    return group->meth->points_make_affine(group, num, points, ctx);
1086
0
}
1087
#endif
1088
1089
/*
1090
 * Functions for point multiplication. If group->meth->mul is 0, we use the
1091
 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1092
 * methods.
1093
 */
1094
1095
#ifndef OPENSSL_NO_DEPRECATED_3_0
1096
int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1097
    size_t num, const EC_POINT *points[],
1098
    const BIGNUM *scalars[], BN_CTX *ctx)
1099
0
{
1100
0
    int ret = 0;
1101
0
    size_t i = 0;
1102
0
#ifndef FIPS_MODULE
1103
0
    BN_CTX *new_ctx = NULL;
1104
0
#endif
1105
1106
0
    if (!ec_point_is_compat(r, group)) {
1107
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1108
0
        return 0;
1109
0
    }
1110
1111
0
    if (scalar == NULL && num == 0)
1112
0
        return EC_POINT_set_to_infinity(group, r);
1113
1114
0
    for (i = 0; i < num; i++) {
1115
0
        if (!ec_point_is_compat(points[i], group)) {
1116
0
            ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1117
0
            return 0;
1118
0
        }
1119
0
    }
1120
1121
0
#ifndef FIPS_MODULE
1122
0
    if (ctx == NULL)
1123
0
        ctx = new_ctx = BN_CTX_secure_new();
1124
0
#endif
1125
0
    if (ctx == NULL) {
1126
0
        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1127
0
        return 0;
1128
0
    }
1129
1130
0
    if (group->meth->mul != NULL)
1131
0
        ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1132
0
    else
1133
        /* use default */
1134
0
        ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1135
1136
0
#ifndef FIPS_MODULE
1137
0
    BN_CTX_free(new_ctx);
1138
0
#endif
1139
0
    return ret;
1140
0
}
1141
#endif
1142
1143
int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1144
    const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1145
38.2k
{
1146
38.2k
    int ret = 0;
1147
38.2k
    size_t num;
1148
38.2k
#ifndef FIPS_MODULE
1149
38.2k
    BN_CTX *new_ctx = NULL;
1150
38.2k
#endif
1151
1152
38.2k
    if (!ec_point_is_compat(r, group)
1153
38.2k
        || (point != NULL && !ec_point_is_compat(point, group))) {
1154
0
        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1155
0
        return 0;
1156
0
    }
1157
1158
38.2k
    if (g_scalar == NULL && p_scalar == NULL)
1159
0
        return EC_POINT_set_to_infinity(group, r);
1160
1161
38.2k
#ifndef FIPS_MODULE
1162
38.2k
    if (ctx == NULL)
1163
0
        ctx = new_ctx = BN_CTX_secure_new();
1164
38.2k
#endif
1165
38.2k
    if (ctx == NULL) {
1166
0
        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1167
0
        return 0;
1168
0
    }
1169
1170
38.2k
    num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1171
38.2k
    if (group->meth->mul != NULL)
1172
31.3k
        ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1173
6.91k
    else
1174
        /* use default */
1175
6.91k
        ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1176
1177
38.2k
#ifndef FIPS_MODULE
1178
38.2k
    BN_CTX_free(new_ctx);
1179
38.2k
#endif
1180
38.2k
    return ret;
1181
38.2k
}
1182
1183
#ifndef OPENSSL_NO_DEPRECATED_3_0
1184
int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1185
0
{
1186
0
    if (group->meth->mul == 0)
1187
        /* use default */
1188
0
        return ossl_ec_wNAF_precompute_mult(group, ctx);
1189
1190
0
    if (group->meth->precompute_mult != 0)
1191
0
        return group->meth->precompute_mult(group, ctx);
1192
0
    else
1193
0
        return 1; /* nothing to do, so report success */
1194
0
}
1195
1196
int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1197
0
{
1198
0
    if (group->meth->mul == 0)
1199
        /* use default */
1200
0
        return ossl_ec_wNAF_have_precompute_mult(group);
1201
1202
0
    if (group->meth->have_precompute_mult != 0)
1203
0
        return group->meth->have_precompute_mult(group);
1204
0
    else
1205
0
        return 0; /* cannot tell whether precomputation has
1206
                   * been performed */
1207
0
}
1208
#endif
1209
1210
/*
1211
 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1212
 * returns one on success. On error it returns zero.
1213
 */
1214
static int ec_precompute_mont_data(EC_GROUP *group)
1215
454k
{
1216
454k
    BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1217
454k
    int ret = 0;
1218
1219
454k
    BN_MONT_CTX_free(group->mont_data);
1220
454k
    group->mont_data = NULL;
1221
1222
454k
    if (ctx == NULL)
1223
0
        goto err;
1224
1225
454k
    group->mont_data = BN_MONT_CTX_new();
1226
454k
    if (group->mont_data == NULL)
1227
0
        goto err;
1228
1229
454k
    if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1230
0
        BN_MONT_CTX_free(group->mont_data);
1231
0
        group->mont_data = NULL;
1232
0
        goto err;
1233
0
    }
1234
1235
454k
    ret = 1;
1236
1237
454k
err:
1238
1239
454k
    BN_CTX_free(ctx);
1240
454k
    return ret;
1241
454k
}
1242
1243
#ifndef FIPS_MODULE
1244
int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1245
0
{
1246
0
    return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1247
0
}
1248
1249
void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1250
0
{
1251
0
    return CRYPTO_get_ex_data(&key->ex_data, idx);
1252
0
}
1253
#endif
1254
1255
int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
1256
830k
{
1257
830k
    if (group->order == NULL)
1258
0
        return 0;
1259
830k
    return BN_num_bits(group->order);
1260
830k
}
1261
1262
static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1263
    const BIGNUM *x, BN_CTX *ctx)
1264
426
{
1265
426
    BIGNUM *e = NULL;
1266
426
    int ret = 0;
1267
426
#ifndef FIPS_MODULE
1268
426
    BN_CTX *new_ctx = NULL;
1269
426
#endif
1270
1271
426
    if (group->mont_data == NULL)
1272
0
        return 0;
1273
1274
426
#ifndef FIPS_MODULE
1275
426
    if (ctx == NULL)
1276
0
        ctx = new_ctx = BN_CTX_secure_new();
1277
426
#endif
1278
426
    if (ctx == NULL)
1279
0
        return 0;
1280
1281
426
    BN_CTX_start(ctx);
1282
426
    if ((e = BN_CTX_get(ctx)) == NULL)
1283
0
        goto err;
1284
1285
    /*-
1286
     * We want inverse in constant time, therefore we utilize the fact
1287
     * order must be prime and use Fermats Little Theorem instead.
1288
     */
1289
426
    if (!BN_set_word(e, 2))
1290
0
        goto err;
1291
426
    if (!BN_sub(e, group->order, e))
1292
0
        goto err;
1293
    /*-
1294
     * Although the exponent is public we want the result to be
1295
     * fixed top.
1296
     */
1297
426
    if (!bn_mod_exp_mont_fixed_top(r, x, e, group->order, ctx, group->mont_data))
1298
0
        goto err;
1299
1300
426
    ret = 1;
1301
1302
426
err:
1303
426
    BN_CTX_end(ctx);
1304
426
#ifndef FIPS_MODULE
1305
426
    BN_CTX_free(new_ctx);
1306
426
#endif
1307
426
    return ret;
1308
426
}
1309
1310
/*-
1311
 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1312
 * - When group->order is even, this function returns an error.
1313
 * - When group->order is otherwise composite, the correctness
1314
 *   of the output is not guaranteed.
1315
 * - When x is outside the range [1, group->order), the correctness
1316
 *   of the output is not guaranteed.
1317
 * - Otherwise, this function returns the multiplicative inverse in the
1318
 *   range [1, group->order).
1319
 *
1320
 * EC_METHODs must implement their own field_inverse_mod_ord for
1321
 * other functionality.
1322
 */
1323
int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1324
    const BIGNUM *x, BN_CTX *ctx)
1325
6.37k
{
1326
6.37k
    if (group->meth->field_inverse_mod_ord != NULL)
1327
5.94k
        return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1328
426
    else
1329
426
        return ec_field_inverse_mod_ord(group, res, x, ctx);
1330
6.37k
}
1331
1332
/*-
1333
 * Coordinate blinding for EC_POINT.
1334
 *
1335
 * The underlying EC_METHOD can optionally implement this function:
1336
 * underlying implementations should return 0 on errors, or 1 on
1337
 * success.
1338
 *
1339
 * This wrapper returns 1 in case the underlying EC_METHOD does not
1340
 * support coordinate blinding.
1341
 */
1342
int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1343
    BN_CTX *ctx)
1344
2.91k
{
1345
2.91k
    if (group->meth->blind_coordinates == NULL)
1346
0
        return 1; /* ignore if not implemented */
1347
1348
2.91k
    return group->meth->blind_coordinates(group, p, ctx);
1349
2.91k
}
1350
1351
int EC_GROUP_get_basis_type(const EC_GROUP *group)
1352
119k
{
1353
119k
    int i;
1354
1355
119k
    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
1356
        /* everything else is currently not supported */
1357
0
        return 0;
1358
1359
    /* Find the last non-zero element of group->poly[] */
1360
119k
    for (i = 0;
1361
542k
        i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
1362
423k
        i++)
1363
423k
        continue;
1364
1365
119k
    if (i == 4)
1366
92.6k
        return NID_X9_62_ppBasis;
1367
26.4k
    else if (i == 2)
1368
26.4k
        return NID_X9_62_tpBasis;
1369
0
    else
1370
        /* everything else is currently not supported */
1371
0
        return 0;
1372
119k
}
1373
1374
#ifndef OPENSSL_NO_EC2M
1375
int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
1376
26.4k
{
1377
26.4k
    if (group == NULL)
1378
0
        return 0;
1379
1380
26.4k
    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1381
26.4k
        || !((group->poly[0] != 0) && (group->poly[1] != 0)
1382
26.4k
            && (group->poly[2] == 0))) {
1383
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1384
0
        return 0;
1385
0
    }
1386
1387
26.4k
    if (k)
1388
26.4k
        *k = group->poly[1];
1389
1390
26.4k
    return 1;
1391
26.4k
}
1392
1393
int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1394
    unsigned int *k2, unsigned int *k3)
1395
92.6k
{
1396
92.6k
    if (group == NULL)
1397
0
        return 0;
1398
1399
92.6k
    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1400
92.6k
        || !((group->poly[0] != 0) && (group->poly[1] != 0)
1401
92.6k
            && (group->poly[2] != 0) && (group->poly[3] != 0)
1402
92.6k
            && (group->poly[4] == 0))) {
1403
0
        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1404
0
        return 0;
1405
0
    }
1406
1407
92.6k
    if (k1)
1408
92.6k
        *k1 = group->poly[3];
1409
92.6k
    if (k2)
1410
92.6k
        *k2 = group->poly[2];
1411
92.6k
    if (k3)
1412
92.6k
        *k3 = group->poly[1];
1413
1414
92.6k
    return 1;
1415
92.6k
}
1416
#endif
1417
1418
#ifndef FIPS_MODULE
1419
/*
1420
 * Check if the explicit parameters group matches any built-in curves.
1421
 *
1422
 * We create a copy of the group just built, so that we can remove optional
1423
 * fields for the lookup: we do this to avoid the possibility that one of
1424
 * the optional parameters is used to force the library into using a less
1425
 * performant and less secure EC_METHOD instead of the specialized one.
1426
 * In any case, `seed` is not really used in any computation, while a
1427
 * cofactor different from the one in the built-in table is just
1428
 * mathematically wrong anyway and should not be used.
1429
 */
1430
static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1431
    OSSL_LIB_CTX *libctx,
1432
    const char *propq,
1433
    BN_CTX *ctx)
1434
0
{
1435
0
    EC_GROUP *ret_group = NULL, *dup = NULL;
1436
0
    int curve_name_nid;
1437
1438
0
    const EC_POINT *point = EC_GROUP_get0_generator(group);
1439
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
1440
0
    int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1441
1442
0
    if ((dup = EC_GROUP_dup(group)) == NULL
1443
0
        || EC_GROUP_set_seed(dup, NULL, 0) != 1
1444
0
        || !EC_GROUP_set_generator(dup, point, order, NULL))
1445
0
        goto err;
1446
0
    if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
1447
        /*
1448
         * The input explicit parameters successfully matched one of the
1449
         * built-in curves: often for built-in curves we have specialized
1450
         * methods with better performance and hardening.
1451
         *
1452
         * In this case we replace the `EC_GROUP` created through explicit
1453
         * parameters with one created from a named group.
1454
         */
1455
1456
0
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1457
        /*
1458
         * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1459
         * the same curve, we prefer the SECP nid when matching explicit
1460
         * parameters as that is associated with a specialized EC_METHOD.
1461
         */
1462
0
        if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1463
0
            curve_name_nid = NID_secp224r1;
1464
0
#endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1465
1466
0
        ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
1467
0
        if (ret_group == NULL)
1468
0
            goto err;
1469
1470
        /*
1471
         * Set the flag so that EC_GROUPs created from explicit parameters are
1472
         * serialized using explicit parameters by default.
1473
         */
1474
0
        EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1475
1476
        /*
1477
         * If the input params do not contain the optional seed field we make
1478
         * sure it is not added to the returned group.
1479
         *
1480
         * The seed field is not really used inside libcrypto anyway, and
1481
         * adding it to parsed explicit parameter keys would alter their DER
1482
         * encoding output (because of the extra field) which could impact
1483
         * applications fingerprinting keys by their DER encoding.
1484
         */
1485
0
        if (no_seed) {
1486
0
            if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1487
0
                goto err;
1488
0
        }
1489
0
    } else {
1490
0
        ret_group = (EC_GROUP *)group;
1491
0
    }
1492
0
    EC_GROUP_free(dup);
1493
0
    return ret_group;
1494
0
err:
1495
0
    EC_GROUP_free(dup);
1496
0
    EC_GROUP_free(ret_group);
1497
0
    return NULL;
1498
0
}
1499
#endif /* FIPS_MODULE */
1500
1501
static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
1502
    OSSL_LIB_CTX *libctx, const char *propq)
1503
68.2k
{
1504
68.2k
    int ok = 0, nid;
1505
68.2k
    const char *curve_name = NULL;
1506
1507
68.2k
    switch (p->data_type) {
1508
68.2k
    case OSSL_PARAM_UTF8_STRING:
1509
        /* The OSSL_PARAM functions have no support for this */
1510
68.2k
        curve_name = p->data;
1511
68.2k
        ok = (curve_name != NULL);
1512
68.2k
        break;
1513
0
    case OSSL_PARAM_UTF8_PTR:
1514
0
        ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1515
0
        break;
1516
68.2k
    }
1517
1518
68.2k
    if (ok) {
1519
68.2k
        nid = ossl_ec_curve_name2nid(curve_name);
1520
68.2k
        if (nid == NID_undef) {
1521
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
1522
0
            return NULL;
1523
68.2k
        } else {
1524
68.2k
            return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
1525
68.2k
        }
1526
68.2k
    }
1527
0
    return NULL;
1528
68.2k
}
1529
1530
/* These parameters can be set directly into an EC_GROUP */
1531
int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
1532
70.9k
{
1533
70.9k
    int encoding_flag = -1, format = -1;
1534
70.9k
    const OSSL_PARAM *p;
1535
1536
70.9k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
1537
70.9k
    if (p != NULL) {
1538
58.4k
        if (!ossl_ec_pt_format_param2id(p, &format)) {
1539
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1540
0
            return 0;
1541
0
        }
1542
58.4k
        EC_GROUP_set_point_conversion_form(group, format);
1543
58.4k
    }
1544
1545
70.9k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1546
70.9k
    if (p != NULL) {
1547
58.4k
        if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
1548
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1549
0
            return 0;
1550
0
        }
1551
58.4k
        EC_GROUP_set_asn1_flag(group, encoding_flag);
1552
58.4k
    }
1553
    /* Optional seed */
1554
70.9k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1555
70.9k
    if (p != NULL) {
1556
        /* The seed is allowed to be NULL */
1557
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING
1558
0
            || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
1559
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1560
0
            return 0;
1561
0
        }
1562
0
    }
1563
70.9k
    return 1;
1564
70.9k
}
1565
1566
EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
1567
    OSSL_LIB_CTX *libctx, const char *propq)
1568
8.18k
{
1569
8.18k
    const OSSL_PARAM *ptmp;
1570
8.18k
    EC_GROUP *group = NULL;
1571
1572
8.18k
#ifndef FIPS_MODULE
1573
8.18k
    const OSSL_PARAM *pa, *pb;
1574
8.18k
    int ok = 0;
1575
8.18k
    EC_GROUP *named_group = NULL;
1576
8.18k
    BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1577
8.18k
    EC_POINT *point = NULL;
1578
8.18k
    int field_bits = 0;
1579
8.18k
    int is_prime_field = 1;
1580
8.18k
    BN_CTX *bnctx = NULL;
1581
8.18k
    const unsigned char *buf = NULL;
1582
#ifndef OPENSSL_NO_EC_EXPLICIT_CURVES
1583
    int encoding_flag = -1;
1584
#endif
1585
8.18k
#endif
1586
1587
    /* This is the simple named group case */
1588
8.18k
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1589
8.18k
    if (ptmp != NULL) {
1590
8.18k
        int decoded = 0;
1591
1592
8.18k
        if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
1593
0
            return NULL;
1594
8.18k
        if (!ossl_ec_group_set_params(group, params)) {
1595
0
            EC_GROUP_free(group);
1596
0
            return NULL;
1597
0
        }
1598
1599
8.18k
        ptmp = OSSL_PARAM_locate_const(params,
1600
8.18k
            OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
1601
8.18k
        if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
1602
0
            ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
1603
0
            EC_GROUP_free(group);
1604
0
            return NULL;
1605
0
        }
1606
8.18k
        group->decoded_from_explicit_params = decoded > 0;
1607
8.18k
        return group;
1608
8.18k
    }
1609
#ifdef FIPS_MODULE
1610
    ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
1611
    return NULL;
1612
#else
1613
    /* If it gets here then we are trying explicit parameters */
1614
0
    bnctx = BN_CTX_new_ex(libctx);
1615
0
    if (bnctx == NULL) {
1616
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1617
0
        return 0;
1618
0
    }
1619
0
    BN_CTX_start(bnctx);
1620
1621
0
    p = BN_CTX_get(bnctx);
1622
0
    a = BN_CTX_get(bnctx);
1623
0
    b = BN_CTX_get(bnctx);
1624
0
    order = BN_CTX_get(bnctx);
1625
0
    if (order == NULL) {
1626
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1627
0
        goto err;
1628
0
    }
1629
1630
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1631
0
    if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
1632
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
1633
0
        goto err;
1634
0
    }
1635
0
    if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
1636
0
        is_prime_field = 1;
1637
0
    } else if (OPENSSL_strcasecmp(ptmp->data,
1638
0
                   SN_X9_62_characteristic_two_field)
1639
0
        == 0) {
1640
0
        is_prime_field = 0;
1641
0
    } else {
1642
        /* Invalid field */
1643
0
        ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
1644
0
        goto err;
1645
0
    }
1646
1647
0
    pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1648
0
    if (!OSSL_PARAM_get_BN(pa, &a)) {
1649
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
1650
0
        goto err;
1651
0
    }
1652
0
    pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1653
0
    if (!OSSL_PARAM_get_BN(pb, &b)) {
1654
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
1655
0
        goto err;
1656
0
    }
1657
1658
    /* extract the prime number or irreducible polynomial */
1659
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1660
0
    if (!OSSL_PARAM_get_BN(ptmp, &p)) {
1661
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1662
0
        goto err;
1663
0
    }
1664
1665
0
    if (is_prime_field) {
1666
0
        if (BN_is_negative(p) || BN_is_zero(p)) {
1667
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1668
0
            goto err;
1669
0
        }
1670
0
        field_bits = BN_num_bits(p);
1671
0
        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1672
0
            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1673
0
            goto err;
1674
0
        }
1675
1676
        /* create the EC_GROUP structure */
1677
0
        group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1678
0
    } else {
1679
#ifdef OPENSSL_NO_EC2M
1680
        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
1681
        goto err;
1682
#else
1683
        /* create the EC_GROUP structure */
1684
0
        group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1685
0
        if (group != NULL) {
1686
0
            field_bits = EC_GROUP_get_degree(group);
1687
0
            if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1688
0
                ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1689
0
                goto err;
1690
0
            }
1691
0
        }
1692
0
#endif /* OPENSSL_NO_EC2M */
1693
0
    }
1694
1695
0
    if (group == NULL) {
1696
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1697
0
        goto err;
1698
0
    }
1699
1700
    /* Optional seed */
1701
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1702
0
    if (ptmp != NULL) {
1703
0
        if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1704
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1705
0
            goto err;
1706
0
        }
1707
0
        if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1708
0
            goto err;
1709
0
    }
1710
1711
    /* generator base point */
1712
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1713
0
    if (ptmp == NULL
1714
0
        || ptmp->data_type != OSSL_PARAM_OCTET_STRING
1715
0
        || ptmp->data_size == 0) {
1716
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1717
0
        goto err;
1718
0
    }
1719
0
    buf = (const unsigned char *)(ptmp->data);
1720
0
    if ((point = EC_POINT_new(group)) == NULL)
1721
0
        goto err;
1722
0
    EC_GROUP_set_point_conversion_form(group,
1723
0
        (point_conversion_form_t)buf[0] & ~0x01);
1724
0
    if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1725
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1726
0
        goto err;
1727
0
    }
1728
1729
    /* order */
1730
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1731
0
    if (!OSSL_PARAM_get_BN(ptmp, &order)
1732
0
        || (BN_is_negative(order) || BN_is_zero(order))
1733
0
        || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
1734
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
1735
0
        goto err;
1736
0
    }
1737
1738
    /* Optional cofactor */
1739
0
    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1740
0
    if (ptmp != NULL) {
1741
0
        cofactor = BN_CTX_get(bnctx);
1742
0
        if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
1743
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
1744
0
            goto err;
1745
0
        }
1746
0
    }
1747
1748
    /* set the generator, order and cofactor (if present) */
1749
0
    if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1750
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1751
0
        goto err;
1752
0
    }
1753
1754
0
    named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1755
0
    if (named_group == NULL) {
1756
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
1757
0
        goto err;
1758
0
    }
1759
0
    if (named_group == group) {
1760
0
#ifdef OPENSSL_NO_EC_EXPLICIT_CURVES
1761
0
        if (EC_GROUP_check_named_curve(group, 0, NULL) == NID_undef) {
1762
0
            ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_GROUP);
1763
0
            goto err;
1764
0
        }
1765
#else
1766
        /*
1767
         * If we did not find a named group then the encoding should be explicit
1768
         * if it was specified
1769
         */
1770
        ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1771
        if (ptmp != NULL
1772
            && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
1773
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1774
            goto err;
1775
        }
1776
        if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
1777
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1778
            goto err;
1779
        }
1780
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1781
#endif
1782
0
    } else {
1783
0
        EC_GROUP_free(group);
1784
0
        group = named_group;
1785
0
    }
1786
    /* We've imported the group from explicit parameters, set it so. */
1787
0
    group->decoded_from_explicit_params = 1;
1788
0
    ok = 1;
1789
0
err:
1790
0
    if (!ok) {
1791
0
        EC_GROUP_free(group);
1792
0
        group = NULL;
1793
0
    }
1794
0
    EC_POINT_free(point);
1795
0
    BN_CTX_end(bnctx);
1796
0
    BN_CTX_free(bnctx);
1797
1798
0
    return group;
1799
0
#endif /* FIPS_MODULE */
1800
0
}
1801
1802
OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
1803
    const char *propq, BN_CTX *bnctx)
1804
0
{
1805
0
    OSSL_PARAM_BLD *tmpl = NULL;
1806
0
    BN_CTX *new_bnctx = NULL;
1807
0
    unsigned char *gen_buf = NULL;
1808
0
    OSSL_PARAM *params = NULL;
1809
1810
0
    if (group == NULL)
1811
0
        goto err;
1812
1813
0
    tmpl = OSSL_PARAM_BLD_new();
1814
0
    if (tmpl == NULL)
1815
0
        goto err;
1816
1817
0
    if (bnctx == NULL)
1818
0
        bnctx = new_bnctx = BN_CTX_new_ex(libctx);
1819
0
    if (bnctx == NULL)
1820
0
        goto err;
1821
0
    BN_CTX_start(bnctx);
1822
1823
0
    if (!ossl_ec_group_todata(
1824
0
            group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
1825
0
        goto err;
1826
1827
0
    params = OSSL_PARAM_BLD_to_param(tmpl);
1828
1829
0
err:
1830
0
    OSSL_PARAM_BLD_free(tmpl);
1831
0
    OPENSSL_free(gen_buf);
1832
0
    BN_CTX_end(bnctx);
1833
0
    BN_CTX_free(new_bnctx);
1834
0
    return params;
1835
0
}