Coverage Report

Created: 2023-09-25 06:41

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