Coverage Report

Created: 2025-12-31 06:58

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