Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/ec/ec_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the OpenSSL license (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
#include <string.h>
12
13
#include <openssl/err.h>
14
#include <openssl/opensslv.h>
15
16
#include "ec_local.h"
17
18
/* functions for EC_GROUP objects */
19
20
EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
21
36.7k
{
22
36.7k
    EC_GROUP *ret;
23
24
36.7k
    if (meth == NULL) {
25
0
        ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
26
0
        return NULL;
27
0
    }
28
36.7k
    if (meth->group_init == 0) {
29
0
        ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
30
0
        return NULL;
31
0
    }
32
33
36.7k
    ret = OPENSSL_zalloc(sizeof(*ret));
34
36.7k
    if (ret == NULL) {
35
0
        ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
36
0
        return NULL;
37
0
    }
38
39
36.7k
    ret->meth = meth;
40
36.7k
    if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
41
36.7k
        ret->order = BN_new();
42
36.7k
        if (ret->order == NULL)
43
0
            goto err;
44
36.7k
        ret->cofactor = BN_new();
45
36.7k
        if (ret->cofactor == NULL)
46
0
            goto err;
47
36.7k
    }
48
36.7k
    ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
49
36.7k
    ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
50
36.7k
    if (!meth->group_init(ret))
51
0
        goto err;
52
36.7k
    return ret;
53
54
0
 err:
55
0
    BN_free(ret->order);
56
0
    BN_free(ret->cofactor);
57
0
    OPENSSL_free(ret);
58
0
    return NULL;
59
36.7k
}
60
61
void EC_pre_comp_free(EC_GROUP *group)
62
36.7k
{
63
36.7k
    switch (group->pre_comp_type) {
64
36.7k
    case PCT_none:
65
36.7k
        break;
66
0
    case PCT_nistz256:
67
0
#ifdef ECP_NISTZ256_ASM
68
0
        EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
69
0
#endif
70
0
        break;
71
0
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
72
0
    case PCT_nistp224:
73
0
        EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
74
0
        break;
75
0
    case PCT_nistp256:
76
0
        EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
77
0
        break;
78
0
    case PCT_nistp521:
79
0
        EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
80
0
        break;
81
#else
82
    case PCT_nistp224:
83
    case PCT_nistp256:
84
    case PCT_nistp521:
85
        break;
86
#endif
87
0
    case PCT_ec:
88
0
        EC_ec_pre_comp_free(group->pre_comp.ec);
89
0
        break;
90
36.7k
    }
91
36.7k
    group->pre_comp.ec = NULL;
92
36.7k
}
93
94
void EC_GROUP_free(EC_GROUP *group)
95
76.0k
{
96
76.0k
    if (!group)
97
39.9k
        return;
98
99
36.0k
    if (group->meth->group_finish != 0)
100
36.0k
        group->meth->group_finish(group);
101
102
36.0k
    EC_pre_comp_free(group);
103
36.0k
    BN_MONT_CTX_free(group->mont_data);
104
36.0k
    EC_POINT_free(group->generator);
105
36.0k
    BN_free(group->order);
106
36.0k
    BN_free(group->cofactor);
107
36.0k
    OPENSSL_free(group->seed);
108
36.0k
    OPENSSL_free(group);
109
36.0k
}
110
111
void EC_GROUP_clear_free(EC_GROUP *group)
112
630
{
113
630
    if (!group)
114
0
        return;
115
116
630
    if (group->meth->group_clear_finish != 0)
117
630
        group->meth->group_clear_finish(group);
118
0
    else if (group->meth->group_finish != 0)
119
0
        group->meth->group_finish(group);
120
121
630
    EC_pre_comp_free(group);
122
630
    BN_MONT_CTX_free(group->mont_data);
123
630
    EC_POINT_clear_free(group->generator);
124
630
    BN_clear_free(group->order);
125
630
    BN_clear_free(group->cofactor);
126
630
    OPENSSL_clear_free(group->seed, group->seed_len);
127
630
    OPENSSL_clear_free(group, sizeof(*group));
128
630
}
129
130
int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
131
17.3k
{
132
17.3k
    if (dest->meth->group_copy == 0) {
133
0
        ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
134
0
        return 0;
135
0
    }
136
17.3k
    if (dest->meth != src->meth) {
137
0
        ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
138
0
        return 0;
139
0
    }
140
17.3k
    if (dest == src)
141
0
        return 1;
142
143
17.3k
    dest->curve_name = src->curve_name;
144
145
    /* Copy precomputed */
146
17.3k
    dest->pre_comp_type = src->pre_comp_type;
147
17.3k
    switch (src->pre_comp_type) {
148
17.3k
    case PCT_none:
149
17.3k
        dest->pre_comp.ec = NULL;
150
17.3k
        break;
151
0
    case PCT_nistz256:
152
0
#ifdef ECP_NISTZ256_ASM
153
0
        dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
154
0
#endif
155
0
        break;
156
0
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
157
0
    case PCT_nistp224:
158
0
        dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
159
0
        break;
160
0
    case PCT_nistp256:
161
0
        dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
162
0
        break;
163
0
    case PCT_nistp521:
164
0
        dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
165
0
        break;
166
#else
167
    case PCT_nistp224:
168
    case PCT_nistp256:
169
    case PCT_nistp521:
170
        break;
171
#endif
172
0
    case PCT_ec:
173
0
        dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
174
0
        break;
175
17.3k
    }
176
177
17.3k
    if (src->mont_data != NULL) {
178
15.4k
        if (dest->mont_data == NULL) {
179
15.4k
            dest->mont_data = BN_MONT_CTX_new();
180
15.4k
            if (dest->mont_data == NULL)
181
0
                return 0;
182
15.4k
        }
183
15.4k
        if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
184
0
            return 0;
185
15.4k
    } else {
186
        /* src->generator == NULL */
187
1.93k
        BN_MONT_CTX_free(dest->mont_data);
188
1.93k
        dest->mont_data = NULL;
189
1.93k
    }
190
191
17.3k
    if (src->generator != NULL) {
192
17.3k
        if (dest->generator == NULL) {
193
17.3k
            dest->generator = EC_POINT_new(dest);
194
17.3k
            if (dest->generator == NULL)
195
0
                return 0;
196
17.3k
        }
197
17.3k
        if (!EC_POINT_copy(dest->generator, src->generator))
198
0
            return 0;
199
17.3k
    } else {
200
        /* src->generator == NULL */
201
0
        EC_POINT_clear_free(dest->generator);
202
0
        dest->generator = NULL;
203
0
    }
204
205
17.3k
    if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
206
17.3k
        if (!BN_copy(dest->order, src->order))
207
0
            return 0;
208
17.3k
        if (!BN_copy(dest->cofactor, src->cofactor))
209
0
            return 0;
210
17.3k
    }
211
212
17.3k
    dest->asn1_flag = src->asn1_flag;
213
17.3k
    dest->asn1_form = src->asn1_form;
214
17.3k
    dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
215
216
17.3k
    if (src->seed) {
217
7.84k
        OPENSSL_free(dest->seed);
218
7.84k
        if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
219
0
            ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
220
0
            return 0;
221
0
        }
222
7.84k
        if (!memcpy(dest->seed, src->seed, src->seed_len))
223
0
            return 0;
224
7.84k
        dest->seed_len = src->seed_len;
225
9.52k
    } else {
226
9.52k
        OPENSSL_free(dest->seed);
227
9.52k
        dest->seed = NULL;
228
9.52k
        dest->seed_len = 0;
229
9.52k
    }
230
231
17.3k
    return dest->meth->group_copy(dest, src);
232
17.3k
}
233
234
EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
235
17.3k
{
236
17.3k
    EC_GROUP *t = NULL;
237
17.3k
    int ok = 0;
238
239
17.3k
    if (a == NULL)
240
0
        return NULL;
241
242
17.3k
    if ((t = EC_GROUP_new(a->meth)) == NULL)
243
0
        return NULL;
244
17.3k
    if (!EC_GROUP_copy(t, a))
245
0
        goto err;
246
247
17.3k
    ok = 1;
248
249
17.3k
 err:
250
17.3k
    if (!ok) {
251
0
        EC_GROUP_free(t);
252
0
        return NULL;
253
0
    }
254
17.3k
        return t;
255
17.3k
}
256
257
const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
258
2.19k
{
259
2.19k
    return group->meth;
260
2.19k
}
261
262
int EC_METHOD_get_field_type(const EC_METHOD *meth)
263
2.19k
{
264
2.19k
    return meth->field_type;
265
2.19k
}
266
267
static int ec_precompute_mont_data(EC_GROUP *);
268
269
/*-
270
 * Try computing cofactor from the generator order (n) and field cardinality (q).
271
 * This works for all curves of cryptographic interest.
272
 *
273
 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
274
 * h_min = (q + 1 - 2*sqrt(q))/n
275
 * h_max = (q + 1 + 2*sqrt(q))/n
276
 * h_max - h_min = 4*sqrt(q)/n
277
 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
278
 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
279
 *
280
 * Otherwise, zero cofactor and return success.
281
 */
282
2.13k
static int ec_guess_cofactor(EC_GROUP *group) {
283
2.13k
    int ret = 0;
284
2.13k
    BN_CTX *ctx = NULL;
285
2.13k
    BIGNUM *q = NULL;
286
287
    /*-
288
     * If the cofactor is too large, we cannot guess it.
289
     * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
290
     */
291
2.13k
    if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
292
        /* default to 0 */
293
243
        BN_zero(group->cofactor);
294
        /* return success */
295
243
        return 1;
296
243
    }
297
298
1.88k
    if ((ctx = BN_CTX_new()) == NULL)
299
0
        return 0;
300
301
1.88k
    BN_CTX_start(ctx);
302
1.88k
    if ((q = BN_CTX_get(ctx)) == NULL)
303
0
        goto err;
304
305
    /* set q = 2**m for binary fields; q = p otherwise */
306
1.88k
    if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
307
0
        BN_zero(q);
308
0
        if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
309
0
            goto err;
310
1.88k
    } else {
311
1.88k
        if (!BN_copy(q, group->field))
312
0
            goto err;
313
1.88k
    }
314
315
    /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
316
1.88k
    if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
317
1.88k
        || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
318
        /* q + 1 + n/2 */
319
1.88k
        || !BN_add(group->cofactor, group->cofactor, BN_value_one())
320
        /* (q + 1 + n/2)/n */
321
1.88k
        || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
322
0
        goto err;
323
1.88k
    ret = 1;
324
1.88k
 err:
325
1.88k
    BN_CTX_end(ctx);
326
1.88k
    BN_CTX_free(ctx);
327
1.88k
    return ret;
328
1.88k
}
329
330
int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
331
                           const BIGNUM *order, const BIGNUM *cofactor)
332
19.9k
{
333
19.9k
    if (generator == NULL) {
334
0
        ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
335
0
        return 0;
336
0
    }
337
338
    /* require group->field >= 1 */
339
19.9k
    if (group->field == NULL || BN_is_zero(group->field)
340
19.9k
        || BN_is_negative(group->field)) {
341
0
        ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
342
0
        return 0;
343
0
    }
344
345
    /*-
346
     * - require order >= 1
347
     * - enforce upper bound due to Hasse thm: order can be no more than one bit
348
     *   longer than field cardinality
349
     */
350
19.9k
    if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
351
19.9k
        || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
352
0
        ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
353
0
        return 0;
354
0
    }
355
356
    /*-
357
     * Unfortunately the cofactor is an optional field in many standards.
358
     * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
359
     * So accept cofactor == NULL or cofactor >= 0.
360
     */
361
19.9k
    if (cofactor != NULL && BN_is_negative(cofactor)) {
362
3
        ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
363
3
        return 0;
364
3
    }
365
366
19.9k
    if (group->generator == NULL) {
367
17.9k
        group->generator = EC_POINT_new(group);
368
17.9k
        if (group->generator == NULL)
369
0
            return 0;
370
17.9k
    }
371
19.9k
    if (!EC_POINT_copy(group->generator, generator))
372
0
        return 0;
373
374
19.9k
    if (!BN_copy(group->order, order))
375
0
        return 0;
376
377
    /* Either take the provided positive cofactor, or try to compute it */
378
19.9k
    if (cofactor != NULL && !BN_is_zero(cofactor)) {
379
17.8k
        if (!BN_copy(group->cofactor, cofactor))
380
0
            return 0;
381
17.8k
    } else if (!ec_guess_cofactor(group)) {
382
0
        BN_zero(group->cofactor);
383
0
        return 0;
384
0
    }
385
386
    /*
387
     * Some groups have an order with
388
     * factors of two, which makes the Montgomery setup fail.
389
     * |group->mont_data| will be NULL in this case.
390
     */
391
19.9k
    if (BN_is_odd(group->order)) {
392
16.1k
        return ec_precompute_mont_data(group);
393
16.1k
    }
394
395
3.86k
    BN_MONT_CTX_free(group->mont_data);
396
3.86k
    group->mont_data = NULL;
397
3.86k
    return 1;
398
19.9k
}
399
400
const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
401
2.27k
{
402
2.27k
    return group->generator;
403
2.27k
}
404
405
BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
406
0
{
407
0
    return group->mont_data;
408
0
}
409
410
int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
411
1.97k
{
412
1.97k
    if (group->order == NULL)
413
0
        return 0;
414
1.97k
    if (!BN_copy(order, group->order))
415
0
        return 0;
416
417
1.97k
    return !BN_is_zero(order);
418
1.97k
}
419
420
const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
421
206
{
422
206
    return group->order;
423
206
}
424
425
int EC_GROUP_order_bits(const EC_GROUP *group)
426
2.74k
{
427
2.74k
    return group->meth->group_order_bits(group);
428
2.74k
}
429
430
int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
431
                          BN_CTX *ctx)
432
0
{
433
434
0
    if (group->cofactor == NULL)
435
0
        return 0;
436
0
    if (!BN_copy(cofactor, group->cofactor))
437
0
        return 0;
438
439
0
    return !BN_is_zero(group->cofactor);
440
0
}
441
442
const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
443
2.19k
{
444
2.19k
    return group->cofactor;
445
2.19k
}
446
447
void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
448
16.0k
{
449
16.0k
    group->curve_name = nid;
450
16.0k
}
451
452
int EC_GROUP_get_curve_name(const EC_GROUP *group)
453
2.82k
{
454
2.82k
    return group->curve_name;
455
2.82k
}
456
457
void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
458
17.9k
{
459
17.9k
    group->asn1_flag = flag;
460
17.9k
}
461
462
int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
463
17.0k
{
464
17.0k
    return group->asn1_flag;
465
17.0k
}
466
467
void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
468
                                        point_conversion_form_t form)
469
2.47k
{
470
2.47k
    group->asn1_form = form;
471
2.47k
}
472
473
point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
474
                                                           *group)
475
206
{
476
206
    return group->asn1_form;
477
206
}
478
479
size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
480
8.30k
{
481
8.30k
    OPENSSL_free(group->seed);
482
8.30k
    group->seed = NULL;
483
8.30k
    group->seed_len = 0;
484
485
8.30k
    if (!len || !p)
486
1.99k
        return 1;
487
488
6.31k
    if ((group->seed = OPENSSL_malloc(len)) == NULL) {
489
0
        ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
490
0
        return 0;
491
0
    }
492
6.31k
    memcpy(group->seed, p, len);
493
6.31k
    group->seed_len = len;
494
495
6.31k
    return len;
496
6.31k
}
497
498
unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
499
2.09k
{
500
2.09k
    return group->seed;
501
2.09k
}
502
503
size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
504
2.03k
{
505
2.03k
    return group->seed_len;
506
2.03k
}
507
508
int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
509
                       const BIGNUM *b, BN_CTX *ctx)
510
15.7k
{
511
15.7k
    if (group->meth->group_set_curve == 0) {
512
0
        ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
513
0
        return 0;
514
0
    }
515
15.7k
    return group->meth->group_set_curve(group, p, a, b, ctx);
516
15.7k
}
517
518
int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
519
                       BN_CTX *ctx)
520
2.29k
{
521
2.29k
    if (group->meth->group_get_curve == NULL) {
522
0
        ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
523
0
        return 0;
524
0
    }
525
2.29k
    return group->meth->group_get_curve(group, p, a, b, ctx);
526
2.29k
}
527
528
#if OPENSSL_API_COMPAT < 0x10200000L
529
int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
530
                           const BIGNUM *b, BN_CTX *ctx)
531
0
{
532
0
    return EC_GROUP_set_curve(group, p, a, b, ctx);
533
0
}
534
535
int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
536
                           BIGNUM *b, BN_CTX *ctx)
537
0
{
538
0
    return EC_GROUP_get_curve(group, p, a, b, ctx);
539
0
}
540
541
# ifndef OPENSSL_NO_EC2M
542
int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
543
                            const BIGNUM *b, BN_CTX *ctx)
544
0
{
545
0
    return EC_GROUP_set_curve(group, p, a, b, ctx);
546
0
}
547
548
int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
549
                            BIGNUM *b, BN_CTX *ctx)
550
0
{
551
0
    return EC_GROUP_get_curve(group, p, a, b, ctx);
552
0
}
553
# endif
554
#endif
555
556
int EC_GROUP_get_degree(const EC_GROUP *group)
557
7.23k
{
558
7.23k
    if (group->meth->group_get_degree == 0) {
559
0
        ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
560
0
        return 0;
561
0
    }
562
7.23k
    return group->meth->group_get_degree(group);
563
7.23k
}
564
565
int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
566
0
{
567
0
    if (group->meth->group_check_discriminant == 0) {
568
0
        ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
569
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
570
0
        return 0;
571
0
    }
572
0
    return group->meth->group_check_discriminant(group, ctx);
573
0
}
574
575
int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
576
0
{
577
0
    int r = 0;
578
0
    BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
579
0
    BN_CTX *ctx_new = NULL;
580
581
    /* compare the field types */
582
0
    if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
583
0
        EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
584
0
        return 1;
585
    /* compare the curve name (if present in both) */
586
0
    if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
587
0
        EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
588
0
        return 1;
589
0
    if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
590
0
        return 0;
591
592
0
    if (ctx == NULL)
593
0
        ctx_new = ctx = BN_CTX_new();
594
0
    if (ctx == NULL)
595
0
        return -1;
596
597
0
    BN_CTX_start(ctx);
598
0
    a1 = BN_CTX_get(ctx);
599
0
    a2 = BN_CTX_get(ctx);
600
0
    a3 = BN_CTX_get(ctx);
601
0
    b1 = BN_CTX_get(ctx);
602
0
    b2 = BN_CTX_get(ctx);
603
0
    b3 = BN_CTX_get(ctx);
604
0
    if (b3 == NULL) {
605
0
        BN_CTX_end(ctx);
606
0
        BN_CTX_free(ctx_new);
607
0
        return -1;
608
0
    }
609
610
    /*
611
     * XXX This approach assumes that the external representation of curves
612
     * over the same field type is the same.
613
     */
614
0
    if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
615
0
        !b->meth->group_get_curve(b, b1, b2, b3, ctx))
616
0
        r = 1;
617
618
0
    if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
619
0
        r = 1;
620
621
    /* XXX EC_POINT_cmp() assumes that the methods are equal */
622
0
    if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
623
0
                          EC_GROUP_get0_generator(b), ctx))
624
0
        r = 1;
625
626
0
    if (!r) {
627
0
        const BIGNUM *ao, *bo, *ac, *bc;
628
        /* compare the order and cofactor */
629
0
        ao = EC_GROUP_get0_order(a);
630
0
        bo = EC_GROUP_get0_order(b);
631
0
        ac = EC_GROUP_get0_cofactor(a);
632
0
        bc = EC_GROUP_get0_cofactor(b);
633
0
        if (ao == NULL || bo == NULL) {
634
0
            BN_CTX_end(ctx);
635
0
            BN_CTX_free(ctx_new);
636
0
            return -1;
637
0
        }
638
0
        if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
639
0
            r = 1;
640
0
    }
641
642
0
    BN_CTX_end(ctx);
643
0
    BN_CTX_free(ctx_new);
644
645
0
    return r;
646
0
}
647
648
/* functions for EC_POINT objects */
649
650
EC_POINT *EC_POINT_new(const EC_GROUP *group)
651
72.9k
{
652
72.9k
    EC_POINT *ret;
653
654
72.9k
    if (group == NULL) {
655
0
        ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
656
0
        return NULL;
657
0
    }
658
72.9k
    if (group->meth->point_init == NULL) {
659
0
        ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
660
0
        return NULL;
661
0
    }
662
663
72.9k
    ret = OPENSSL_zalloc(sizeof(*ret));
664
72.9k
    if (ret == NULL) {
665
0
        ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
666
0
        return NULL;
667
0
    }
668
669
72.9k
    ret->meth = group->meth;
670
72.9k
    ret->curve_name = group->curve_name;
671
672
72.9k
    if (!ret->meth->point_init(ret)) {
673
0
        OPENSSL_free(ret);
674
0
        return NULL;
675
0
    }
676
677
72.9k
    return ret;
678
72.9k
}
679
680
void EC_POINT_free(EC_POINT *point)
681
90.1k
{
682
90.1k
    if (!point)
683
17.9k
        return;
684
685
72.2k
    if (point->meth->point_finish != 0)
686
72.2k
        point->meth->point_finish(point);
687
72.2k
    OPENSSL_free(point);
688
72.2k
}
689
690
void EC_POINT_clear_free(EC_POINT *point)
691
1.93k
{
692
1.93k
    if (!point)
693
1.22k
        return;
694
695
710
    if (point->meth->point_clear_finish != 0)
696
710
        point->meth->point_clear_finish(point);
697
0
    else if (point->meth->point_finish != 0)
698
0
        point->meth->point_finish(point);
699
710
    OPENSSL_clear_free(point, sizeof(*point));
700
710
}
701
702
int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
703
37.9k
{
704
37.9k
    if (dest->meth->point_copy == 0) {
705
0
        ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
706
0
        return 0;
707
0
    }
708
37.9k
    if (dest->meth != src->meth
709
37.9k
            || (dest->curve_name != src->curve_name
710
37.9k
                && dest->curve_name != 0
711
37.9k
                && src->curve_name != 0)) {
712
0
        ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
713
0
        return 0;
714
0
    }
715
37.9k
    if (dest == src)
716
0
        return 1;
717
37.9k
    return dest->meth->point_copy(dest, src);
718
37.9k
}
719
720
EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
721
0
{
722
0
    EC_POINT *t;
723
0
    int r;
724
725
0
    if (a == NULL)
726
0
        return NULL;
727
728
0
    t = EC_POINT_new(group);
729
0
    if (t == NULL)
730
0
        return NULL;
731
0
    r = EC_POINT_copy(t, a);
732
0
    if (!r) {
733
0
        EC_POINT_free(t);
734
0
        return NULL;
735
0
    }
736
0
    return t;
737
0
}
738
739
const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
740
0
{
741
0
    return point->meth;
742
0
}
743
744
int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
745
584
{
746
584
    if (group->meth->point_set_to_infinity == 0) {
747
0
        ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
748
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
749
0
        return 0;
750
0
    }
751
584
    if (group->meth != point->meth) {
752
0
        ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
753
0
        return 0;
754
0
    }
755
584
    return group->meth->point_set_to_infinity(group, point);
756
584
}
757
758
int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
759
                                             EC_POINT *point, const BIGNUM *x,
760
                                             const BIGNUM *y, const BIGNUM *z,
761
                                             BN_CTX *ctx)
762
16.2k
{
763
16.2k
    if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
764
0
        ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
765
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
766
0
        return 0;
767
0
    }
768
16.2k
    if (!ec_point_is_compat(point, group)) {
769
0
        ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
770
0
              EC_R_INCOMPATIBLE_OBJECTS);
771
0
        return 0;
772
0
    }
773
16.2k
    return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
774
16.2k
                                                              y, z, ctx);
775
16.2k
}
776
777
int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
778
                                             const EC_POINT *point, BIGNUM *x,
779
                                             BIGNUM *y, BIGNUM *z,
780
                                             BN_CTX *ctx)
781
0
{
782
0
    if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
783
0
        ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
784
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
785
0
        return 0;
786
0
    }
787
0
    if (!ec_point_is_compat(point, group)) {
788
0
        ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
789
0
              EC_R_INCOMPATIBLE_OBJECTS);
790
0
        return 0;
791
0
    }
792
0
    return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
793
0
                                                              y, z, ctx);
794
0
}
795
796
int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
797
                                    const BIGNUM *x, const BIGNUM *y,
798
                                    BN_CTX *ctx)
799
28.5k
{
800
28.5k
    if (group->meth->point_set_affine_coordinates == NULL) {
801
0
        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
802
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
803
0
        return 0;
804
0
    }
805
28.5k
    if (!ec_point_is_compat(point, group)) {
806
0
        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
807
0
        return 0;
808
0
    }
809
28.5k
    if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
810
0
        return 0;
811
812
28.5k
    if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
813
2.87k
        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
814
2.87k
        return 0;
815
2.87k
    }
816
25.6k
    return 1;
817
28.5k
}
818
819
#if OPENSSL_API_COMPAT < 0x10200000L
820
int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
821
                                        EC_POINT *point, const BIGNUM *x,
822
                                        const BIGNUM *y, BN_CTX *ctx)
823
0
{
824
0
    return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
825
0
}
826
827
# ifndef OPENSSL_NO_EC2M
828
int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
829
                                         EC_POINT *point, const BIGNUM *x,
830
                                         const BIGNUM *y, BN_CTX *ctx)
831
0
{
832
0
    return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
833
0
}
834
# endif
835
#endif
836
837
int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
838
                                    const EC_POINT *point, BIGNUM *x, BIGNUM *y,
839
                                    BN_CTX *ctx)
840
2.74k
{
841
2.74k
    if (group->meth->point_get_affine_coordinates == NULL) {
842
0
        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
843
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
844
0
        return 0;
845
0
    }
846
2.74k
    if (!ec_point_is_compat(point, group)) {
847
0
        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
848
0
        return 0;
849
0
    }
850
2.74k
    if (EC_POINT_is_at_infinity(group, point)) {
851
15
        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
852
15
        return 0;
853
15
    }
854
2.72k
    return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
855
2.74k
}
856
857
#if OPENSSL_API_COMPAT < 0x10200000L
858
int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
859
                                        const EC_POINT *point, BIGNUM *x,
860
                                        BIGNUM *y, BN_CTX *ctx)
861
0
{
862
0
    return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
863
0
}
864
865
# ifndef OPENSSL_NO_EC2M
866
int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
867
                                         const EC_POINT *point, BIGNUM *x,
868
                                         BIGNUM *y, BN_CTX *ctx)
869
0
{
870
0
    return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
871
0
}
872
# endif
873
#endif
874
875
int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
876
                 const EC_POINT *b, BN_CTX *ctx)
877
1.60k
{
878
1.60k
    if (group->meth->add == 0) {
879
0
        ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
880
0
        return 0;
881
0
    }
882
1.60k
    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
883
1.60k
        || !ec_point_is_compat(b, group)) {
884
0
        ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
885
0
        return 0;
886
0
    }
887
1.60k
    return group->meth->add(group, r, a, b, ctx);
888
1.60k
}
889
890
int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
891
                 BN_CTX *ctx)
892
9.73k
{
893
9.73k
    if (group->meth->dbl == 0) {
894
0
        ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
895
0
        return 0;
896
0
    }
897
9.73k
    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
898
0
        ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
899
0
        return 0;
900
0
    }
901
9.73k
    return group->meth->dbl(group, r, a, ctx);
902
9.73k
}
903
904
int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
905
486
{
906
486
    if (group->meth->invert == 0) {
907
0
        ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
908
0
        return 0;
909
0
    }
910
486
    if (!ec_point_is_compat(a, group)) {
911
0
        ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
912
0
        return 0;
913
0
    }
914
486
    return group->meth->invert(group, a, ctx);
915
486
}
916
917
int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
918
49.0k
{
919
49.0k
    if (group->meth->is_at_infinity == 0) {
920
0
        ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
921
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
922
0
        return 0;
923
0
    }
924
49.0k
    if (!ec_point_is_compat(point, group)) {
925
0
        ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
926
0
        return 0;
927
0
    }
928
49.0k
    return group->meth->is_at_infinity(group, point);
929
49.0k
}
930
931
/*
932
 * Check whether an EC_POINT is on the curve or not. Note that the return
933
 * value for this function should NOT be treated as a boolean. Return values:
934
 *  1: The point is on the curve
935
 *  0: The point is not on the curve
936
 * -1: An error occurred
937
 */
938
int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
939
                         BN_CTX *ctx)
940
28.5k
{
941
28.5k
    if (group->meth->is_on_curve == 0) {
942
0
        ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943
0
        return 0;
944
0
    }
945
28.5k
    if (!ec_point_is_compat(point, group)) {
946
0
        ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
947
0
        return 0;
948
0
    }
949
28.5k
    return group->meth->is_on_curve(group, point, ctx);
950
28.5k
}
951
952
int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
953
                 BN_CTX *ctx)
954
28
{
955
28
    if (group->meth->point_cmp == 0) {
956
0
        ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
957
0
        return -1;
958
0
    }
959
28
    if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
960
0
        ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
961
0
        return -1;
962
0
    }
963
28
    return group->meth->point_cmp(group, a, b, ctx);
964
28
}
965
966
int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
967
0
{
968
0
    if (group->meth->make_affine == 0) {
969
0
        ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
970
0
        return 0;
971
0
    }
972
0
    if (!ec_point_is_compat(point, group)) {
973
0
        ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
974
0
        return 0;
975
0
    }
976
0
    return group->meth->make_affine(group, point, ctx);
977
0
}
978
979
int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
980
                          EC_POINT *points[], BN_CTX *ctx)
981
69
{
982
69
    size_t i;
983
984
69
    if (group->meth->points_make_affine == 0) {
985
0
        ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
986
0
        return 0;
987
0
    }
988
309
    for (i = 0; i < num; i++) {
989
240
        if (!ec_point_is_compat(points[i], group)) {
990
0
            ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
991
0
            return 0;
992
0
        }
993
240
    }
994
69
    return group->meth->points_make_affine(group, num, points, ctx);
995
69
}
996
997
/*
998
 * Functions for point multiplication. If group->meth->mul is 0, we use the
999
 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1000
 * methods.
1001
 */
1002
1003
int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1004
                  size_t num, const EC_POINT *points[],
1005
                  const BIGNUM *scalars[], BN_CTX *ctx)
1006
578
{
1007
578
    int ret = 0;
1008
578
    size_t i = 0;
1009
578
    BN_CTX *new_ctx = NULL;
1010
1011
578
    if (!ec_point_is_compat(r, group)) {
1012
0
        ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1013
0
        return 0;
1014
0
    }
1015
1016
578
    if (scalar == NULL && num == 0)
1017
0
        return EC_POINT_set_to_infinity(group, r);
1018
1019
578
    for (i = 0; i < num; i++) {
1020
0
        if (!ec_point_is_compat(points[i], group)) {
1021
0
            ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1022
0
            return 0;
1023
0
        }
1024
0
    }
1025
1026
578
    if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
1027
0
        ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1028
0
        return 0;
1029
0
    }
1030
1031
578
    if (group->meth->mul != NULL)
1032
282
        ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1033
296
    else
1034
        /* use default */
1035
296
        ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1036
1037
578
    BN_CTX_free(new_ctx);
1038
578
    return ret;
1039
578
}
1040
1041
int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1042
                 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1043
578
{
1044
    /* just a convenient interface to EC_POINTs_mul() */
1045
1046
578
    const EC_POINT *points[1];
1047
578
    const BIGNUM *scalars[1];
1048
1049
578
    points[0] = point;
1050
578
    scalars[0] = p_scalar;
1051
1052
578
    return EC_POINTs_mul(group, r, g_scalar,
1053
578
                         (point != NULL
1054
578
                          && p_scalar != NULL), points, scalars, ctx);
1055
578
}
1056
1057
int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1058
0
{
1059
0
    if (group->meth->mul == 0)
1060
        /* use default */
1061
0
        return ec_wNAF_precompute_mult(group, ctx);
1062
1063
0
    if (group->meth->precompute_mult != 0)
1064
0
        return group->meth->precompute_mult(group, ctx);
1065
0
    else
1066
0
        return 1;               /* nothing to do, so report success */
1067
0
}
1068
1069
int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1070
0
{
1071
0
    if (group->meth->mul == 0)
1072
        /* use default */
1073
0
        return ec_wNAF_have_precompute_mult(group);
1074
1075
0
    if (group->meth->have_precompute_mult != 0)
1076
0
        return group->meth->have_precompute_mult(group);
1077
0
    else
1078
0
        return 0;               /* cannot tell whether precomputation has
1079
                                 * been performed */
1080
0
}
1081
1082
/*
1083
 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1084
 * returns one on success. On error it returns zero.
1085
 */
1086
static int ec_precompute_mont_data(EC_GROUP *group)
1087
16.1k
{
1088
16.1k
    BN_CTX *ctx = BN_CTX_new();
1089
16.1k
    int ret = 0;
1090
1091
16.1k
    BN_MONT_CTX_free(group->mont_data);
1092
16.1k
    group->mont_data = NULL;
1093
1094
16.1k
    if (ctx == NULL)
1095
0
        goto err;
1096
1097
16.1k
    group->mont_data = BN_MONT_CTX_new();
1098
16.1k
    if (group->mont_data == NULL)
1099
0
        goto err;
1100
1101
16.1k
    if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1102
0
        BN_MONT_CTX_free(group->mont_data);
1103
0
        group->mont_data = NULL;
1104
0
        goto err;
1105
0
    }
1106
1107
16.1k
    ret = 1;
1108
1109
16.1k
 err:
1110
1111
16.1k
    BN_CTX_free(ctx);
1112
16.1k
    return ret;
1113
16.1k
}
1114
1115
int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1116
0
{
1117
0
    return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1118
0
}
1119
1120
void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1121
0
{
1122
0
    return CRYPTO_get_ex_data(&key->ex_data, idx);
1123
0
}
1124
1125
int ec_group_simple_order_bits(const EC_GROUP *group)
1126
2.74k
{
1127
2.74k
    if (group->order == NULL)
1128
0
        return 0;
1129
2.74k
    return BN_num_bits(group->order);
1130
2.74k
}
1131
1132
static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1133
                                    const BIGNUM *x, BN_CTX *ctx)
1134
0
{
1135
0
    BIGNUM *e = NULL;
1136
0
    BN_CTX *new_ctx = NULL;
1137
0
    int ret = 0;
1138
1139
0
    if (group->mont_data == NULL)
1140
0
        return 0;
1141
1142
0
    if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
1143
0
        return 0;
1144
1145
0
    BN_CTX_start(ctx);
1146
0
    if ((e = BN_CTX_get(ctx)) == NULL)
1147
0
        goto err;
1148
1149
    /*-
1150
     * We want inverse in constant time, therefore we utilize the fact
1151
     * order must be prime and use Fermats Little Theorem instead.
1152
     */
1153
0
    if (!BN_set_word(e, 2))
1154
0
        goto err;
1155
0
    if (!BN_sub(e, group->order, e))
1156
0
        goto err;
1157
    /*-
1158
     * Exponent e is public.
1159
     * No need for scatter-gather or BN_FLG_CONSTTIME.
1160
     */
1161
0
    if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1162
0
        goto err;
1163
1164
0
    ret = 1;
1165
1166
0
 err:
1167
0
    BN_CTX_end(ctx);
1168
0
    BN_CTX_free(new_ctx);
1169
0
    return ret;
1170
0
}
1171
1172
/*-
1173
 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1174
 * - When group->order is even, this function returns an error.
1175
 * - When group->order is otherwise composite, the correctness
1176
 *   of the output is not guaranteed.
1177
 * - When x is outside the range [1, group->order), the correctness
1178
 *   of the output is not guaranteed.
1179
 * - Otherwise, this function returns the multiplicative inverse in the
1180
 *   range [1, group->order).
1181
 *
1182
 * EC_METHODs must implement their own field_inverse_mod_ord for
1183
 * other functionality.
1184
 */
1185
int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1186
                            const BIGNUM *x, BN_CTX *ctx)
1187
0
{
1188
0
    if (group->meth->field_inverse_mod_ord != NULL)
1189
0
        return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1190
0
    else
1191
0
        return ec_field_inverse_mod_ord(group, res, x, ctx);
1192
0
}
1193
1194
/*-
1195
 * Coordinate blinding for EC_POINT.
1196
 *
1197
 * The underlying EC_METHOD can optionally implement this function:
1198
 * underlying implementations should return 0 on errors, or 1 on
1199
 * success.
1200
 *
1201
 * This wrapper returns 1 in case the underlying EC_METHOD does not
1202
 * support coordinate blinding.
1203
 */
1204
int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1205
68
{
1206
68
    if (group->meth->blind_coordinates == NULL)
1207
0
        return 1; /* ignore if not implemented */
1208
1209
68
    return group->meth->blind_coordinates(group, p, ctx);
1210
68
}