Coverage Report

Created: 2026-02-14 07:20

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