Coverage Report

Created: 2018-08-29 13:53

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