Coverage Report

Created: 2026-11-16 07:12

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