Coverage Report

Created: 2025-06-13 06:58

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