Coverage Report

Created: 2025-12-31 06:58

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