Coverage Report

Created: 2025-12-10 06:24

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