Coverage Report

Created: 2026-04-11 06:29

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