Coverage Report

Created: 2023-06-08 06:40

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