Coverage Report

Created: 2026-04-01 06:39

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