Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ec/ecp_smpl.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 <openssl/err.h>
12
#include <openssl/symhacks.h>
13
14
#include "ec_lcl.h"
15
16
const EC_METHOD *EC_GFp_simple_method(void)
17
0
{
18
0
    static const EC_METHOD ret = {
19
0
        EC_FLAGS_DEFAULT_OCT,
20
0
        NID_X9_62_prime_field,
21
0
        ec_GFp_simple_group_init,
22
0
        ec_GFp_simple_group_finish,
23
0
        ec_GFp_simple_group_clear_finish,
24
0
        ec_GFp_simple_group_copy,
25
0
        ec_GFp_simple_group_set_curve,
26
0
        ec_GFp_simple_group_get_curve,
27
0
        ec_GFp_simple_group_get_degree,
28
0
        ec_group_simple_order_bits,
29
0
        ec_GFp_simple_group_check_discriminant,
30
0
        ec_GFp_simple_point_init,
31
0
        ec_GFp_simple_point_finish,
32
0
        ec_GFp_simple_point_clear_finish,
33
0
        ec_GFp_simple_point_copy,
34
0
        ec_GFp_simple_point_set_to_infinity,
35
0
        ec_GFp_simple_set_Jprojective_coordinates_GFp,
36
0
        ec_GFp_simple_get_Jprojective_coordinates_GFp,
37
0
        ec_GFp_simple_point_set_affine_coordinates,
38
0
        ec_GFp_simple_point_get_affine_coordinates,
39
0
        0, 0, 0,
40
0
        ec_GFp_simple_add,
41
0
        ec_GFp_simple_dbl,
42
0
        ec_GFp_simple_invert,
43
0
        ec_GFp_simple_is_at_infinity,
44
0
        ec_GFp_simple_is_on_curve,
45
0
        ec_GFp_simple_cmp,
46
0
        ec_GFp_simple_make_affine,
47
0
        ec_GFp_simple_points_make_affine,
48
0
        0 /* mul */ ,
49
0
        0 /* precompute_mult */ ,
50
0
        0 /* have_precompute_mult */ ,
51
0
        ec_GFp_simple_field_mul,
52
0
        ec_GFp_simple_field_sqr,
53
0
        0 /* field_div */ ,
54
0
        0 /* field_encode */ ,
55
0
        0 /* field_decode */ ,
56
0
        0,                      /* field_set_to_one */
57
0
        ec_key_simple_priv2oct,
58
0
        ec_key_simple_oct2priv,
59
0
        0, /* set private */
60
0
        ec_key_simple_generate_key,
61
0
        ec_key_simple_check_key,
62
0
        ec_key_simple_generate_public_key,
63
0
        0, /* keycopy */
64
0
        0, /* keyfinish */
65
0
        ecdh_simple_compute_key,
66
0
        0, /* field_inverse_mod_ord */
67
0
        ec_GFp_simple_blind_coordinates,
68
0
        ec_GFp_simple_ladder_pre,
69
0
        ec_GFp_simple_ladder_step,
70
0
        ec_GFp_simple_ladder_post
71
0
    };
72
0
73
0
    return &ret;
74
0
}
75
76
/*
77
 * Most method functions in this file are designed to work with
78
 * non-trivial representations of field elements if necessary
79
 * (see ecp_mont.c): while standard modular addition and subtraction
80
 * are used, the field_mul and field_sqr methods will be used for
81
 * multiplication, and field_encode and field_decode (if defined)
82
 * will be used for converting between representations.
83
 *
84
 * Functions ec_GFp_simple_points_make_affine() and
85
 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
86
 * that if a non-trivial representation is used, it is a Montgomery
87
 * representation (i.e. 'encoding' means multiplying by some factor R).
88
 */
89
90
int ec_GFp_simple_group_init(EC_GROUP *group)
91
0
{
92
0
    group->field = BN_new();
93
0
    group->a = BN_new();
94
0
    group->b = BN_new();
95
0
    if (group->field == NULL || group->a == NULL || group->b == NULL) {
96
0
        BN_free(group->field);
97
0
        BN_free(group->a);
98
0
        BN_free(group->b);
99
0
        return 0;
100
0
    }
101
0
    group->a_is_minus3 = 0;
102
0
    return 1;
103
0
}
104
105
void ec_GFp_simple_group_finish(EC_GROUP *group)
106
0
{
107
0
    BN_free(group->field);
108
0
    BN_free(group->a);
109
0
    BN_free(group->b);
110
0
}
111
112
void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
113
0
{
114
0
    BN_clear_free(group->field);
115
0
    BN_clear_free(group->a);
116
0
    BN_clear_free(group->b);
117
0
}
118
119
int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
120
0
{
121
0
    if (!BN_copy(dest->field, src->field))
122
0
        return 0;
123
0
    if (!BN_copy(dest->a, src->a))
124
0
        return 0;
125
0
    if (!BN_copy(dest->b, src->b))
126
0
        return 0;
127
0
128
0
    dest->a_is_minus3 = src->a_is_minus3;
129
0
130
0
    return 1;
131
0
}
132
133
int ec_GFp_simple_group_set_curve(EC_GROUP *group,
134
                                  const BIGNUM *p, const BIGNUM *a,
135
                                  const BIGNUM *b, BN_CTX *ctx)
136
0
{
137
0
    int ret = 0;
138
0
    BN_CTX *new_ctx = NULL;
139
0
    BIGNUM *tmp_a;
140
0
141
0
    /* p must be a prime > 3 */
142
0
    if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
143
0
        ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
144
0
        return 0;
145
0
    }
146
0
147
0
    if (ctx == NULL) {
148
0
        ctx = new_ctx = BN_CTX_new();
149
0
        if (ctx == NULL)
150
0
            return 0;
151
0
    }
152
0
153
0
    BN_CTX_start(ctx);
154
0
    tmp_a = BN_CTX_get(ctx);
155
0
    if (tmp_a == NULL)
156
0
        goto err;
157
0
158
0
    /* group->field */
159
0
    if (!BN_copy(group->field, p))
160
0
        goto err;
161
0
    BN_set_negative(group->field, 0);
162
0
163
0
    /* group->a */
164
0
    if (!BN_nnmod(tmp_a, a, p, ctx))
165
0
        goto err;
166
0
    if (group->meth->field_encode) {
167
0
        if (!group->meth->field_encode(group, group->a, tmp_a, ctx))
168
0
            goto err;
169
0
    } else if (!BN_copy(group->a, tmp_a))
170
0
        goto err;
171
0
172
0
    /* group->b */
173
0
    if (!BN_nnmod(group->b, b, p, ctx))
174
0
        goto err;
175
0
    if (group->meth->field_encode)
176
0
        if (!group->meth->field_encode(group, group->b, group->b, ctx))
177
0
            goto err;
178
0
179
0
    /* group->a_is_minus3 */
180
0
    if (!BN_add_word(tmp_a, 3))
181
0
        goto err;
182
0
    group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));
183
0
184
0
    ret = 1;
185
0
186
0
 err:
187
0
    BN_CTX_end(ctx);
188
0
    BN_CTX_free(new_ctx);
189
0
    return ret;
190
0
}
191
192
int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
193
                                  BIGNUM *b, BN_CTX *ctx)
194
0
{
195
0
    int ret = 0;
196
0
    BN_CTX *new_ctx = NULL;
197
0
198
0
    if (p != NULL) {
199
0
        if (!BN_copy(p, group->field))
200
0
            return 0;
201
0
    }
202
0
203
0
    if (a != NULL || b != NULL) {
204
0
        if (group->meth->field_decode) {
205
0
            if (ctx == NULL) {
206
0
                ctx = new_ctx = BN_CTX_new();
207
0
                if (ctx == NULL)
208
0
                    return 0;
209
0
            }
210
0
            if (a != NULL) {
211
0
                if (!group->meth->field_decode(group, a, group->a, ctx))
212
0
                    goto err;
213
0
            }
214
0
            if (b != NULL) {
215
0
                if (!group->meth->field_decode(group, b, group->b, ctx))
216
0
                    goto err;
217
0
            }
218
0
        } else {
219
0
            if (a != NULL) {
220
0
                if (!BN_copy(a, group->a))
221
0
                    goto err;
222
0
            }
223
0
            if (b != NULL) {
224
0
                if (!BN_copy(b, group->b))
225
0
                    goto err;
226
0
            }
227
0
        }
228
0
    }
229
0
230
0
    ret = 1;
231
0
232
0
 err:
233
0
    BN_CTX_free(new_ctx);
234
0
    return ret;
235
0
}
236
237
int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
238
0
{
239
0
    return BN_num_bits(group->field);
240
0
}
241
242
int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
243
0
{
244
0
    int ret = 0;
245
0
    BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
246
0
    const BIGNUM *p = group->field;
247
0
    BN_CTX *new_ctx = NULL;
248
0
249
0
    if (ctx == NULL) {
250
0
        ctx = new_ctx = BN_CTX_new();
251
0
        if (ctx == NULL) {
252
0
            ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
253
0
                  ERR_R_MALLOC_FAILURE);
254
0
            goto err;
255
0
        }
256
0
    }
257
0
    BN_CTX_start(ctx);
258
0
    a = BN_CTX_get(ctx);
259
0
    b = BN_CTX_get(ctx);
260
0
    tmp_1 = BN_CTX_get(ctx);
261
0
    tmp_2 = BN_CTX_get(ctx);
262
0
    order = BN_CTX_get(ctx);
263
0
    if (order == NULL)
264
0
        goto err;
265
0
266
0
    if (group->meth->field_decode) {
267
0
        if (!group->meth->field_decode(group, a, group->a, ctx))
268
0
            goto err;
269
0
        if (!group->meth->field_decode(group, b, group->b, ctx))
270
0
            goto err;
271
0
    } else {
272
0
        if (!BN_copy(a, group->a))
273
0
            goto err;
274
0
        if (!BN_copy(b, group->b))
275
0
            goto err;
276
0
    }
277
0
278
0
    /*-
279
0
     * check the discriminant:
280
0
     * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
281
0
     * 0 =< a, b < p
282
0
     */
283
0
    if (BN_is_zero(a)) {
284
0
        if (BN_is_zero(b))
285
0
            goto err;
286
0
    } else if (!BN_is_zero(b)) {
287
0
        if (!BN_mod_sqr(tmp_1, a, p, ctx))
288
0
            goto err;
289
0
        if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
290
0
            goto err;
291
0
        if (!BN_lshift(tmp_1, tmp_2, 2))
292
0
            goto err;
293
0
        /* tmp_1 = 4*a^3 */
294
0
295
0
        if (!BN_mod_sqr(tmp_2, b, p, ctx))
296
0
            goto err;
297
0
        if (!BN_mul_word(tmp_2, 27))
298
0
            goto err;
299
0
        /* tmp_2 = 27*b^2 */
300
0
301
0
        if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
302
0
            goto err;
303
0
        if (BN_is_zero(a))
304
0
            goto err;
305
0
    }
306
0
    ret = 1;
307
0
308
0
 err:
309
0
    if (ctx != NULL)
310
0
        BN_CTX_end(ctx);
311
0
    BN_CTX_free(new_ctx);
312
0
    return ret;
313
0
}
314
315
int ec_GFp_simple_point_init(EC_POINT *point)
316
0
{
317
0
    point->X = BN_new();
318
0
    point->Y = BN_new();
319
0
    point->Z = BN_new();
320
0
    point->Z_is_one = 0;
321
0
322
0
    if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
323
0
        BN_free(point->X);
324
0
        BN_free(point->Y);
325
0
        BN_free(point->Z);
326
0
        return 0;
327
0
    }
328
0
    return 1;
329
0
}
330
331
void ec_GFp_simple_point_finish(EC_POINT *point)
332
0
{
333
0
    BN_free(point->X);
334
0
    BN_free(point->Y);
335
0
    BN_free(point->Z);
336
0
}
337
338
void ec_GFp_simple_point_clear_finish(EC_POINT *point)
339
0
{
340
0
    BN_clear_free(point->X);
341
0
    BN_clear_free(point->Y);
342
0
    BN_clear_free(point->Z);
343
0
    point->Z_is_one = 0;
344
0
}
345
346
int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
347
0
{
348
0
    if (!BN_copy(dest->X, src->X))
349
0
        return 0;
350
0
    if (!BN_copy(dest->Y, src->Y))
351
0
        return 0;
352
0
    if (!BN_copy(dest->Z, src->Z))
353
0
        return 0;
354
0
    dest->Z_is_one = src->Z_is_one;
355
0
    dest->curve_name = src->curve_name;
356
0
357
0
    return 1;
358
0
}
359
360
int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
361
                                        EC_POINT *point)
362
0
{
363
0
    point->Z_is_one = 0;
364
0
    BN_zero(point->Z);
365
0
    return 1;
366
0
}
367
368
int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
369
                                                  EC_POINT *point,
370
                                                  const BIGNUM *x,
371
                                                  const BIGNUM *y,
372
                                                  const BIGNUM *z,
373
                                                  BN_CTX *ctx)
374
0
{
375
0
    BN_CTX *new_ctx = NULL;
376
0
    int ret = 0;
377
0
378
0
    if (ctx == NULL) {
379
0
        ctx = new_ctx = BN_CTX_new();
380
0
        if (ctx == NULL)
381
0
            return 0;
382
0
    }
383
0
384
0
    if (x != NULL) {
385
0
        if (!BN_nnmod(point->X, x, group->field, ctx))
386
0
            goto err;
387
0
        if (group->meth->field_encode) {
388
0
            if (!group->meth->field_encode(group, point->X, point->X, ctx))
389
0
                goto err;
390
0
        }
391
0
    }
392
0
393
0
    if (y != NULL) {
394
0
        if (!BN_nnmod(point->Y, y, group->field, ctx))
395
0
            goto err;
396
0
        if (group->meth->field_encode) {
397
0
            if (!group->meth->field_encode(group, point->Y, point->Y, ctx))
398
0
                goto err;
399
0
        }
400
0
    }
401
0
402
0
    if (z != NULL) {
403
0
        int Z_is_one;
404
0
405
0
        if (!BN_nnmod(point->Z, z, group->field, ctx))
406
0
            goto err;
407
0
        Z_is_one = BN_is_one(point->Z);
408
0
        if (group->meth->field_encode) {
409
0
            if (Z_is_one && (group->meth->field_set_to_one != 0)) {
410
0
                if (!group->meth->field_set_to_one(group, point->Z, ctx))
411
0
                    goto err;
412
0
            } else {
413
0
                if (!group->
414
0
                    meth->field_encode(group, point->Z, point->Z, ctx))
415
0
                    goto err;
416
0
            }
417
0
        }
418
0
        point->Z_is_one = Z_is_one;
419
0
    }
420
0
421
0
    ret = 1;
422
0
423
0
 err:
424
0
    BN_CTX_free(new_ctx);
425
0
    return ret;
426
0
}
427
428
int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
429
                                                  const EC_POINT *point,
430
                                                  BIGNUM *x, BIGNUM *y,
431
                                                  BIGNUM *z, BN_CTX *ctx)
432
0
{
433
0
    BN_CTX *new_ctx = NULL;
434
0
    int ret = 0;
435
0
436
0
    if (group->meth->field_decode != 0) {
437
0
        if (ctx == NULL) {
438
0
            ctx = new_ctx = BN_CTX_new();
439
0
            if (ctx == NULL)
440
0
                return 0;
441
0
        }
442
0
443
0
        if (x != NULL) {
444
0
            if (!group->meth->field_decode(group, x, point->X, ctx))
445
0
                goto err;
446
0
        }
447
0
        if (y != NULL) {
448
0
            if (!group->meth->field_decode(group, y, point->Y, ctx))
449
0
                goto err;
450
0
        }
451
0
        if (z != NULL) {
452
0
            if (!group->meth->field_decode(group, z, point->Z, ctx))
453
0
                goto err;
454
0
        }
455
0
    } else {
456
0
        if (x != NULL) {
457
0
            if (!BN_copy(x, point->X))
458
0
                goto err;
459
0
        }
460
0
        if (y != NULL) {
461
0
            if (!BN_copy(y, point->Y))
462
0
                goto err;
463
0
        }
464
0
        if (z != NULL) {
465
0
            if (!BN_copy(z, point->Z))
466
0
                goto err;
467
0
        }
468
0
    }
469
0
470
0
    ret = 1;
471
0
472
0
 err:
473
0
    BN_CTX_free(new_ctx);
474
0
    return ret;
475
0
}
476
477
int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
478
                                               EC_POINT *point,
479
                                               const BIGNUM *x,
480
                                               const BIGNUM *y, BN_CTX *ctx)
481
0
{
482
0
    if (x == NULL || y == NULL) {
483
0
        /*
484
0
         * unlike for projective coordinates, we do not tolerate this
485
0
         */
486
0
        ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
487
0
              ERR_R_PASSED_NULL_PARAMETER);
488
0
        return 0;
489
0
    }
490
0
491
0
    return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y,
492
0
                                                    BN_value_one(), ctx);
493
0
}
494
495
int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
496
                                               const EC_POINT *point,
497
                                               BIGNUM *x, BIGNUM *y,
498
                                               BN_CTX *ctx)
499
0
{
500
0
    BN_CTX *new_ctx = NULL;
501
0
    BIGNUM *Z, *Z_1, *Z_2, *Z_3;
502
0
    const BIGNUM *Z_;
503
0
    int ret = 0;
504
0
505
0
    if (EC_POINT_is_at_infinity(group, point)) {
506
0
        ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
507
0
              EC_R_POINT_AT_INFINITY);
508
0
        return 0;
509
0
    }
510
0
511
0
    if (ctx == NULL) {
512
0
        ctx = new_ctx = BN_CTX_new();
513
0
        if (ctx == NULL)
514
0
            return 0;
515
0
    }
516
0
517
0
    BN_CTX_start(ctx);
518
0
    Z = BN_CTX_get(ctx);
519
0
    Z_1 = BN_CTX_get(ctx);
520
0
    Z_2 = BN_CTX_get(ctx);
521
0
    Z_3 = BN_CTX_get(ctx);
522
0
    if (Z_3 == NULL)
523
0
        goto err;
524
0
525
0
    /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */
526
0
527
0
    if (group->meth->field_decode) {
528
0
        if (!group->meth->field_decode(group, Z, point->Z, ctx))
529
0
            goto err;
530
0
        Z_ = Z;
531
0
    } else {
532
0
        Z_ = point->Z;
533
0
    }
534
0
535
0
    if (BN_is_one(Z_)) {
536
0
        if (group->meth->field_decode) {
537
0
            if (x != NULL) {
538
0
                if (!group->meth->field_decode(group, x, point->X, ctx))
539
0
                    goto err;
540
0
            }
541
0
            if (y != NULL) {
542
0
                if (!group->meth->field_decode(group, y, point->Y, ctx))
543
0
                    goto err;
544
0
            }
545
0
        } else {
546
0
            if (x != NULL) {
547
0
                if (!BN_copy(x, point->X))
548
0
                    goto err;
549
0
            }
550
0
            if (y != NULL) {
551
0
                if (!BN_copy(y, point->Y))
552
0
                    goto err;
553
0
            }
554
0
        }
555
0
    } else {
556
0
        if (!BN_mod_inverse(Z_1, Z_, group->field, ctx)) {
557
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
558
0
                  ERR_R_BN_LIB);
559
0
            goto err;
560
0
        }
561
0
562
0
        if (group->meth->field_encode == 0) {
563
0
            /* field_sqr works on standard representation */
564
0
            if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
565
0
                goto err;
566
0
        } else {
567
0
            if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx))
568
0
                goto err;
569
0
        }
570
0
571
0
        if (x != NULL) {
572
0
            /*
573
0
             * in the Montgomery case, field_mul will cancel out Montgomery
574
0
             * factor in X:
575
0
             */
576
0
            if (!group->meth->field_mul(group, x, point->X, Z_2, ctx))
577
0
                goto err;
578
0
        }
579
0
580
0
        if (y != NULL) {
581
0
            if (group->meth->field_encode == 0) {
582
0
                /*
583
0
                 * field_mul works on standard representation
584
0
                 */
585
0
                if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
586
0
                    goto err;
587
0
            } else {
588
0
                if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx))
589
0
                    goto err;
590
0
            }
591
0
592
0
            /*
593
0
             * in the Montgomery case, field_mul will cancel out Montgomery
594
0
             * factor in Y:
595
0
             */
596
0
            if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx))
597
0
                goto err;
598
0
        }
599
0
    }
600
0
601
0
    ret = 1;
602
0
603
0
 err:
604
0
    BN_CTX_end(ctx);
605
0
    BN_CTX_free(new_ctx);
606
0
    return ret;
607
0
}
608
609
int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
610
                      const EC_POINT *b, BN_CTX *ctx)
611
0
{
612
0
    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
613
0
                      const BIGNUM *, BN_CTX *);
614
0
    int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
615
0
    const BIGNUM *p;
616
0
    BN_CTX *new_ctx = NULL;
617
0
    BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
618
0
    int ret = 0;
619
0
620
0
    if (a == b)
621
0
        return EC_POINT_dbl(group, r, a, ctx);
622
0
    if (EC_POINT_is_at_infinity(group, a))
623
0
        return EC_POINT_copy(r, b);
624
0
    if (EC_POINT_is_at_infinity(group, b))
625
0
        return EC_POINT_copy(r, a);
626
0
627
0
    field_mul = group->meth->field_mul;
628
0
    field_sqr = group->meth->field_sqr;
629
0
    p = group->field;
630
0
631
0
    if (ctx == NULL) {
632
0
        ctx = new_ctx = BN_CTX_new();
633
0
        if (ctx == NULL)
634
0
            return 0;
635
0
    }
636
0
637
0
    BN_CTX_start(ctx);
638
0
    n0 = BN_CTX_get(ctx);
639
0
    n1 = BN_CTX_get(ctx);
640
0
    n2 = BN_CTX_get(ctx);
641
0
    n3 = BN_CTX_get(ctx);
642
0
    n4 = BN_CTX_get(ctx);
643
0
    n5 = BN_CTX_get(ctx);
644
0
    n6 = BN_CTX_get(ctx);
645
0
    if (n6 == NULL)
646
0
        goto end;
647
0
648
0
    /*
649
0
     * Note that in this function we must not read components of 'a' or 'b'
650
0
     * once we have written the corresponding components of 'r'. ('r' might
651
0
     * be one of 'a' or 'b'.)
652
0
     */
653
0
654
0
    /* n1, n2 */
655
0
    if (b->Z_is_one) {
656
0
        if (!BN_copy(n1, a->X))
657
0
            goto end;
658
0
        if (!BN_copy(n2, a->Y))
659
0
            goto end;
660
0
        /* n1 = X_a */
661
0
        /* n2 = Y_a */
662
0
    } else {
663
0
        if (!field_sqr(group, n0, b->Z, ctx))
664
0
            goto end;
665
0
        if (!field_mul(group, n1, a->X, n0, ctx))
666
0
            goto end;
667
0
        /* n1 = X_a * Z_b^2 */
668
0
669
0
        if (!field_mul(group, n0, n0, b->Z, ctx))
670
0
            goto end;
671
0
        if (!field_mul(group, n2, a->Y, n0, ctx))
672
0
            goto end;
673
0
        /* n2 = Y_a * Z_b^3 */
674
0
    }
675
0
676
0
    /* n3, n4 */
677
0
    if (a->Z_is_one) {
678
0
        if (!BN_copy(n3, b->X))
679
0
            goto end;
680
0
        if (!BN_copy(n4, b->Y))
681
0
            goto end;
682
0
        /* n3 = X_b */
683
0
        /* n4 = Y_b */
684
0
    } else {
685
0
        if (!field_sqr(group, n0, a->Z, ctx))
686
0
            goto end;
687
0
        if (!field_mul(group, n3, b->X, n0, ctx))
688
0
            goto end;
689
0
        /* n3 = X_b * Z_a^2 */
690
0
691
0
        if (!field_mul(group, n0, n0, a->Z, ctx))
692
0
            goto end;
693
0
        if (!field_mul(group, n4, b->Y, n0, ctx))
694
0
            goto end;
695
0
        /* n4 = Y_b * Z_a^3 */
696
0
    }
697
0
698
0
    /* n5, n6 */
699
0
    if (!BN_mod_sub_quick(n5, n1, n3, p))
700
0
        goto end;
701
0
    if (!BN_mod_sub_quick(n6, n2, n4, p))
702
0
        goto end;
703
0
    /* n5 = n1 - n3 */
704
0
    /* n6 = n2 - n4 */
705
0
706
0
    if (BN_is_zero(n5)) {
707
0
        if (BN_is_zero(n6)) {
708
0
            /* a is the same point as b */
709
0
            BN_CTX_end(ctx);
710
0
            ret = EC_POINT_dbl(group, r, a, ctx);
711
0
            ctx = NULL;
712
0
            goto end;
713
0
        } else {
714
0
            /* a is the inverse of b */
715
0
            BN_zero(r->Z);
716
0
            r->Z_is_one = 0;
717
0
            ret = 1;
718
0
            goto end;
719
0
        }
720
0
    }
721
0
722
0
    /* 'n7', 'n8' */
723
0
    if (!BN_mod_add_quick(n1, n1, n3, p))
724
0
        goto end;
725
0
    if (!BN_mod_add_quick(n2, n2, n4, p))
726
0
        goto end;
727
0
    /* 'n7' = n1 + n3 */
728
0
    /* 'n8' = n2 + n4 */
729
0
730
0
    /* Z_r */
731
0
    if (a->Z_is_one && b->Z_is_one) {
732
0
        if (!BN_copy(r->Z, n5))
733
0
            goto end;
734
0
    } else {
735
0
        if (a->Z_is_one) {
736
0
            if (!BN_copy(n0, b->Z))
737
0
                goto end;
738
0
        } else if (b->Z_is_one) {
739
0
            if (!BN_copy(n0, a->Z))
740
0
                goto end;
741
0
        } else {
742
0
            if (!field_mul(group, n0, a->Z, b->Z, ctx))
743
0
                goto end;
744
0
        }
745
0
        if (!field_mul(group, r->Z, n0, n5, ctx))
746
0
            goto end;
747
0
    }
748
0
    r->Z_is_one = 0;
749
0
    /* Z_r = Z_a * Z_b * n5 */
750
0
751
0
    /* X_r */
752
0
    if (!field_sqr(group, n0, n6, ctx))
753
0
        goto end;
754
0
    if (!field_sqr(group, n4, n5, ctx))
755
0
        goto end;
756
0
    if (!field_mul(group, n3, n1, n4, ctx))
757
0
        goto end;
758
0
    if (!BN_mod_sub_quick(r->X, n0, n3, p))
759
0
        goto end;
760
0
    /* X_r = n6^2 - n5^2 * 'n7' */
761
0
762
0
    /* 'n9' */
763
0
    if (!BN_mod_lshift1_quick(n0, r->X, p))
764
0
        goto end;
765
0
    if (!BN_mod_sub_quick(n0, n3, n0, p))
766
0
        goto end;
767
0
    /* n9 = n5^2 * 'n7' - 2 * X_r */
768
0
769
0
    /* Y_r */
770
0
    if (!field_mul(group, n0, n0, n6, ctx))
771
0
        goto end;
772
0
    if (!field_mul(group, n5, n4, n5, ctx))
773
0
        goto end;               /* now n5 is n5^3 */
774
0
    if (!field_mul(group, n1, n2, n5, ctx))
775
0
        goto end;
776
0
    if (!BN_mod_sub_quick(n0, n0, n1, p))
777
0
        goto end;
778
0
    if (BN_is_odd(n0))
779
0
        if (!BN_add(n0, n0, p))
780
0
            goto end;
781
0
    /* now  0 <= n0 < 2*p,  and n0 is even */
782
0
    if (!BN_rshift1(r->Y, n0))
783
0
        goto end;
784
0
    /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
785
0
786
0
    ret = 1;
787
0
788
0
 end:
789
0
    if (ctx)                    /* otherwise we already called BN_CTX_end */
790
0
        BN_CTX_end(ctx);
791
0
    BN_CTX_free(new_ctx);
792
0
    return ret;
793
0
}
794
795
int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
796
                      BN_CTX *ctx)
797
0
{
798
0
    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
799
0
                      const BIGNUM *, BN_CTX *);
800
0
    int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
801
0
    const BIGNUM *p;
802
0
    BN_CTX *new_ctx = NULL;
803
0
    BIGNUM *n0, *n1, *n2, *n3;
804
0
    int ret = 0;
805
0
806
0
    if (EC_POINT_is_at_infinity(group, a)) {
807
0
        BN_zero(r->Z);
808
0
        r->Z_is_one = 0;
809
0
        return 1;
810
0
    }
811
0
812
0
    field_mul = group->meth->field_mul;
813
0
    field_sqr = group->meth->field_sqr;
814
0
    p = group->field;
815
0
816
0
    if (ctx == NULL) {
817
0
        ctx = new_ctx = BN_CTX_new();
818
0
        if (ctx == NULL)
819
0
            return 0;
820
0
    }
821
0
822
0
    BN_CTX_start(ctx);
823
0
    n0 = BN_CTX_get(ctx);
824
0
    n1 = BN_CTX_get(ctx);
825
0
    n2 = BN_CTX_get(ctx);
826
0
    n3 = BN_CTX_get(ctx);
827
0
    if (n3 == NULL)
828
0
        goto err;
829
0
830
0
    /*
831
0
     * Note that in this function we must not read components of 'a' once we
832
0
     * have written the corresponding components of 'r'. ('r' might the same
833
0
     * as 'a'.)
834
0
     */
835
0
836
0
    /* n1 */
837
0
    if (a->Z_is_one) {
838
0
        if (!field_sqr(group, n0, a->X, ctx))
839
0
            goto err;
840
0
        if (!BN_mod_lshift1_quick(n1, n0, p))
841
0
            goto err;
842
0
        if (!BN_mod_add_quick(n0, n0, n1, p))
843
0
            goto err;
844
0
        if (!BN_mod_add_quick(n1, n0, group->a, p))
845
0
            goto err;
846
0
        /* n1 = 3 * X_a^2 + a_curve */
847
0
    } else if (group->a_is_minus3) {
848
0
        if (!field_sqr(group, n1, a->Z, ctx))
849
0
            goto err;
850
0
        if (!BN_mod_add_quick(n0, a->X, n1, p))
851
0
            goto err;
852
0
        if (!BN_mod_sub_quick(n2, a->X, n1, p))
853
0
            goto err;
854
0
        if (!field_mul(group, n1, n0, n2, ctx))
855
0
            goto err;
856
0
        if (!BN_mod_lshift1_quick(n0, n1, p))
857
0
            goto err;
858
0
        if (!BN_mod_add_quick(n1, n0, n1, p))
859
0
            goto err;
860
0
        /*-
861
0
         * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
862
0
         *    = 3 * X_a^2 - 3 * Z_a^4
863
0
         */
864
0
    } else {
865
0
        if (!field_sqr(group, n0, a->X, ctx))
866
0
            goto err;
867
0
        if (!BN_mod_lshift1_quick(n1, n0, p))
868
0
            goto err;
869
0
        if (!BN_mod_add_quick(n0, n0, n1, p))
870
0
            goto err;
871
0
        if (!field_sqr(group, n1, a->Z, ctx))
872
0
            goto err;
873
0
        if (!field_sqr(group, n1, n1, ctx))
874
0
            goto err;
875
0
        if (!field_mul(group, n1, n1, group->a, ctx))
876
0
            goto err;
877
0
        if (!BN_mod_add_quick(n1, n1, n0, p))
878
0
            goto err;
879
0
        /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
880
0
    }
881
0
882
0
    /* Z_r */
883
0
    if (a->Z_is_one) {
884
0
        if (!BN_copy(n0, a->Y))
885
0
            goto err;
886
0
    } else {
887
0
        if (!field_mul(group, n0, a->Y, a->Z, ctx))
888
0
            goto err;
889
0
    }
890
0
    if (!BN_mod_lshift1_quick(r->Z, n0, p))
891
0
        goto err;
892
0
    r->Z_is_one = 0;
893
0
    /* Z_r = 2 * Y_a * Z_a */
894
0
895
0
    /* n2 */
896
0
    if (!field_sqr(group, n3, a->Y, ctx))
897
0
        goto err;
898
0
    if (!field_mul(group, n2, a->X, n3, ctx))
899
0
        goto err;
900
0
    if (!BN_mod_lshift_quick(n2, n2, 2, p))
901
0
        goto err;
902
0
    /* n2 = 4 * X_a * Y_a^2 */
903
0
904
0
    /* X_r */
905
0
    if (!BN_mod_lshift1_quick(n0, n2, p))
906
0
        goto err;
907
0
    if (!field_sqr(group, r->X, n1, ctx))
908
0
        goto err;
909
0
    if (!BN_mod_sub_quick(r->X, r->X, n0, p))
910
0
        goto err;
911
0
    /* X_r = n1^2 - 2 * n2 */
912
0
913
0
    /* n3 */
914
0
    if (!field_sqr(group, n0, n3, ctx))
915
0
        goto err;
916
0
    if (!BN_mod_lshift_quick(n3, n0, 3, p))
917
0
        goto err;
918
0
    /* n3 = 8 * Y_a^4 */
919
0
920
0
    /* Y_r */
921
0
    if (!BN_mod_sub_quick(n0, n2, r->X, p))
922
0
        goto err;
923
0
    if (!field_mul(group, n0, n1, n0, ctx))
924
0
        goto err;
925
0
    if (!BN_mod_sub_quick(r->Y, n0, n3, p))
926
0
        goto err;
927
0
    /* Y_r = n1 * (n2 - X_r) - n3 */
928
0
929
0
    ret = 1;
930
0
931
0
 err:
932
0
    BN_CTX_end(ctx);
933
0
    BN_CTX_free(new_ctx);
934
0
    return ret;
935
0
}
936
937
int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
938
0
{
939
0
    if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
940
0
        /* point is its own inverse */
941
0
        return 1;
942
0
943
0
    return BN_usub(point->Y, group->field, point->Y);
944
0
}
945
946
int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
947
0
{
948
0
    return BN_is_zero(point->Z);
949
0
}
950
951
int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
952
                              BN_CTX *ctx)
953
0
{
954
0
    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
955
0
                      const BIGNUM *, BN_CTX *);
956
0
    int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
957
0
    const BIGNUM *p;
958
0
    BN_CTX *new_ctx = NULL;
959
0
    BIGNUM *rh, *tmp, *Z4, *Z6;
960
0
    int ret = -1;
961
0
962
0
    if (EC_POINT_is_at_infinity(group, point))
963
0
        return 1;
964
0
965
0
    field_mul = group->meth->field_mul;
966
0
    field_sqr = group->meth->field_sqr;
967
0
    p = group->field;
968
0
969
0
    if (ctx == NULL) {
970
0
        ctx = new_ctx = BN_CTX_new();
971
0
        if (ctx == NULL)
972
0
            return -1;
973
0
    }
974
0
975
0
    BN_CTX_start(ctx);
976
0
    rh = BN_CTX_get(ctx);
977
0
    tmp = BN_CTX_get(ctx);
978
0
    Z4 = BN_CTX_get(ctx);
979
0
    Z6 = BN_CTX_get(ctx);
980
0
    if (Z6 == NULL)
981
0
        goto err;
982
0
983
0
    /*-
984
0
     * We have a curve defined by a Weierstrass equation
985
0
     *      y^2 = x^3 + a*x + b.
986
0
     * The point to consider is given in Jacobian projective coordinates
987
0
     * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
988
0
     * Substituting this and multiplying by  Z^6  transforms the above equation into
989
0
     *      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
990
0
     * To test this, we add up the right-hand side in 'rh'.
991
0
     */
992
0
993
0
    /* rh := X^2 */
994
0
    if (!field_sqr(group, rh, point->X, ctx))
995
0
        goto err;
996
0
997
0
    if (!point->Z_is_one) {
998
0
        if (!field_sqr(group, tmp, point->Z, ctx))
999
0
            goto err;
1000
0
        if (!field_sqr(group, Z4, tmp, ctx))
1001
0
            goto err;
1002
0
        if (!field_mul(group, Z6, Z4, tmp, ctx))
1003
0
            goto err;
1004
0
1005
0
        /* rh := (rh + a*Z^4)*X */
1006
0
        if (group->a_is_minus3) {
1007
0
            if (!BN_mod_lshift1_quick(tmp, Z4, p))
1008
0
                goto err;
1009
0
            if (!BN_mod_add_quick(tmp, tmp, Z4, p))
1010
0
                goto err;
1011
0
            if (!BN_mod_sub_quick(rh, rh, tmp, p))
1012
0
                goto err;
1013
0
            if (!field_mul(group, rh, rh, point->X, ctx))
1014
0
                goto err;
1015
0
        } else {
1016
0
            if (!field_mul(group, tmp, Z4, group->a, ctx))
1017
0
                goto err;
1018
0
            if (!BN_mod_add_quick(rh, rh, tmp, p))
1019
0
                goto err;
1020
0
            if (!field_mul(group, rh, rh, point->X, ctx))
1021
0
                goto err;
1022
0
        }
1023
0
1024
0
        /* rh := rh + b*Z^6 */
1025
0
        if (!field_mul(group, tmp, group->b, Z6, ctx))
1026
0
            goto err;
1027
0
        if (!BN_mod_add_quick(rh, rh, tmp, p))
1028
0
            goto err;
1029
0
    } else {
1030
0
        /* point->Z_is_one */
1031
0
1032
0
        /* rh := (rh + a)*X */
1033
0
        if (!BN_mod_add_quick(rh, rh, group->a, p))
1034
0
            goto err;
1035
0
        if (!field_mul(group, rh, rh, point->X, ctx))
1036
0
            goto err;
1037
0
        /* rh := rh + b */
1038
0
        if (!BN_mod_add_quick(rh, rh, group->b, p))
1039
0
            goto err;
1040
0
    }
1041
0
1042
0
    /* 'lh' := Y^2 */
1043
0
    if (!field_sqr(group, tmp, point->Y, ctx))
1044
0
        goto err;
1045
0
1046
0
    ret = (0 == BN_ucmp(tmp, rh));
1047
0
1048
0
 err:
1049
0
    BN_CTX_end(ctx);
1050
0
    BN_CTX_free(new_ctx);
1051
0
    return ret;
1052
0
}
1053
1054
int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
1055
                      const EC_POINT *b, BN_CTX *ctx)
1056
0
{
1057
0
    /*-
1058
0
     * return values:
1059
0
     *  -1   error
1060
0
     *   0   equal (in affine coordinates)
1061
0
     *   1   not equal
1062
0
     */
1063
0
1064
0
    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
1065
0
                      const BIGNUM *, BN_CTX *);
1066
0
    int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1067
0
    BN_CTX *new_ctx = NULL;
1068
0
    BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1069
0
    const BIGNUM *tmp1_, *tmp2_;
1070
0
    int ret = -1;
1071
0
1072
0
    if (EC_POINT_is_at_infinity(group, a)) {
1073
0
        return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1074
0
    }
1075
0
1076
0
    if (EC_POINT_is_at_infinity(group, b))
1077
0
        return 1;
1078
0
1079
0
    if (a->Z_is_one && b->Z_is_one) {
1080
0
        return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
1081
0
    }
1082
0
1083
0
    field_mul = group->meth->field_mul;
1084
0
    field_sqr = group->meth->field_sqr;
1085
0
1086
0
    if (ctx == NULL) {
1087
0
        ctx = new_ctx = BN_CTX_new();
1088
0
        if (ctx == NULL)
1089
0
            return -1;
1090
0
    }
1091
0
1092
0
    BN_CTX_start(ctx);
1093
0
    tmp1 = BN_CTX_get(ctx);
1094
0
    tmp2 = BN_CTX_get(ctx);
1095
0
    Za23 = BN_CTX_get(ctx);
1096
0
    Zb23 = BN_CTX_get(ctx);
1097
0
    if (Zb23 == NULL)
1098
0
        goto end;
1099
0
1100
0
    /*-
1101
0
     * We have to decide whether
1102
0
     *     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1103
0
     * or equivalently, whether
1104
0
     *     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1105
0
     */
1106
0
1107
0
    if (!b->Z_is_one) {
1108
0
        if (!field_sqr(group, Zb23, b->Z, ctx))
1109
0
            goto end;
1110
0
        if (!field_mul(group, tmp1, a->X, Zb23, ctx))
1111
0
            goto end;
1112
0
        tmp1_ = tmp1;
1113
0
    } else
1114
0
        tmp1_ = a->X;
1115
0
    if (!a->Z_is_one) {
1116
0
        if (!field_sqr(group, Za23, a->Z, ctx))
1117
0
            goto end;
1118
0
        if (!field_mul(group, tmp2, b->X, Za23, ctx))
1119
0
            goto end;
1120
0
        tmp2_ = tmp2;
1121
0
    } else
1122
0
        tmp2_ = b->X;
1123
0
1124
0
    /* compare  X_a*Z_b^2  with  X_b*Z_a^2 */
1125
0
    if (BN_cmp(tmp1_, tmp2_) != 0) {
1126
0
        ret = 1;                /* points differ */
1127
0
        goto end;
1128
0
    }
1129
0
1130
0
    if (!b->Z_is_one) {
1131
0
        if (!field_mul(group, Zb23, Zb23, b->Z, ctx))
1132
0
            goto end;
1133
0
        if (!field_mul(group, tmp1, a->Y, Zb23, ctx))
1134
0
            goto end;
1135
0
        /* tmp1_ = tmp1 */
1136
0
    } else
1137
0
        tmp1_ = a->Y;
1138
0
    if (!a->Z_is_one) {
1139
0
        if (!field_mul(group, Za23, Za23, a->Z, ctx))
1140
0
            goto end;
1141
0
        if (!field_mul(group, tmp2, b->Y, Za23, ctx))
1142
0
            goto end;
1143
0
        /* tmp2_ = tmp2 */
1144
0
    } else
1145
0
        tmp2_ = b->Y;
1146
0
1147
0
    /* compare  Y_a*Z_b^3  with  Y_b*Z_a^3 */
1148
0
    if (BN_cmp(tmp1_, tmp2_) != 0) {
1149
0
        ret = 1;                /* points differ */
1150
0
        goto end;
1151
0
    }
1152
0
1153
0
    /* points are equal */
1154
0
    ret = 0;
1155
0
1156
0
 end:
1157
0
    BN_CTX_end(ctx);
1158
0
    BN_CTX_free(new_ctx);
1159
0
    return ret;
1160
0
}
1161
1162
int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
1163
                              BN_CTX *ctx)
1164
0
{
1165
0
    BN_CTX *new_ctx = NULL;
1166
0
    BIGNUM *x, *y;
1167
0
    int ret = 0;
1168
0
1169
0
    if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1170
0
        return 1;
1171
0
1172
0
    if (ctx == NULL) {
1173
0
        ctx = new_ctx = BN_CTX_new();
1174
0
        if (ctx == NULL)
1175
0
            return 0;
1176
0
    }
1177
0
1178
0
    BN_CTX_start(ctx);
1179
0
    x = BN_CTX_get(ctx);
1180
0
    y = BN_CTX_get(ctx);
1181
0
    if (y == NULL)
1182
0
        goto err;
1183
0
1184
0
    if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
1185
0
        goto err;
1186
0
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
1187
0
        goto err;
1188
0
    if (!point->Z_is_one) {
1189
0
        ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1190
0
        goto err;
1191
0
    }
1192
0
1193
0
    ret = 1;
1194
0
1195
0
 err:
1196
0
    BN_CTX_end(ctx);
1197
0
    BN_CTX_free(new_ctx);
1198
0
    return ret;
1199
0
}
1200
1201
int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
1202
                                     EC_POINT *points[], BN_CTX *ctx)
1203
0
{
1204
0
    BN_CTX *new_ctx = NULL;
1205
0
    BIGNUM *tmp, *tmp_Z;
1206
0
    BIGNUM **prod_Z = NULL;
1207
0
    size_t i;
1208
0
    int ret = 0;
1209
0
1210
0
    if (num == 0)
1211
0
        return 1;
1212
0
1213
0
    if (ctx == NULL) {
1214
0
        ctx = new_ctx = BN_CTX_new();
1215
0
        if (ctx == NULL)
1216
0
            return 0;
1217
0
    }
1218
0
1219
0
    BN_CTX_start(ctx);
1220
0
    tmp = BN_CTX_get(ctx);
1221
0
    tmp_Z = BN_CTX_get(ctx);
1222
0
    if (tmp_Z == NULL)
1223
0
        goto err;
1224
0
1225
0
    prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
1226
0
    if (prod_Z == NULL)
1227
0
        goto err;
1228
0
    for (i = 0; i < num; i++) {
1229
0
        prod_Z[i] = BN_new();
1230
0
        if (prod_Z[i] == NULL)
1231
0
            goto err;
1232
0
    }
1233
0
1234
0
    /*
1235
0
     * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1236
0
     * skipping any zero-valued inputs (pretend that they're 1).
1237
0
     */
1238
0
1239
0
    if (!BN_is_zero(points[0]->Z)) {
1240
0
        if (!BN_copy(prod_Z[0], points[0]->Z))
1241
0
            goto err;
1242
0
    } else {
1243
0
        if (group->meth->field_set_to_one != 0) {
1244
0
            if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))
1245
0
                goto err;
1246
0
        } else {
1247
0
            if (!BN_one(prod_Z[0]))
1248
0
                goto err;
1249
0
        }
1250
0
    }
1251
0
1252
0
    for (i = 1; i < num; i++) {
1253
0
        if (!BN_is_zero(points[i]->Z)) {
1254
0
            if (!group->
1255
0
                meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,
1256
0
                                ctx))
1257
0
                goto err;
1258
0
        } else {
1259
0
            if (!BN_copy(prod_Z[i], prod_Z[i - 1]))
1260
0
                goto err;
1261
0
        }
1262
0
    }
1263
0
1264
0
    /*
1265
0
     * Now use a single explicit inversion to replace every non-zero
1266
0
     * points[i]->Z by its inverse.
1267
0
     */
1268
0
1269
0
    if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {
1270
0
        ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1271
0
        goto err;
1272
0
    }
1273
0
    if (group->meth->field_encode != 0) {
1274
0
        /*
1275
0
         * In the Montgomery case, we just turned R*H (representing H) into
1276
0
         * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
1277
0
         * multiply by the Montgomery factor twice.
1278
0
         */
1279
0
        if (!group->meth->field_encode(group, tmp, tmp, ctx))
1280
0
            goto err;
1281
0
        if (!group->meth->field_encode(group, tmp, tmp, ctx))
1282
0
            goto err;
1283
0
    }
1284
0
1285
0
    for (i = num - 1; i > 0; --i) {
1286
0
        /*
1287
0
         * Loop invariant: tmp is the product of the inverses of points[0]->Z
1288
0
         * .. points[i]->Z (zero-valued inputs skipped).
1289
0
         */
1290
0
        if (!BN_is_zero(points[i]->Z)) {
1291
0
            /*
1292
0
             * Set tmp_Z to the inverse of points[i]->Z (as product of Z
1293
0
             * inverses 0 .. i, Z values 0 .. i - 1).
1294
0
             */
1295
0
            if (!group->
1296
0
                meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))
1297
0
                goto err;
1298
0
            /*
1299
0
             * Update tmp to satisfy the loop invariant for i - 1.
1300
0
             */
1301
0
            if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))
1302
0
                goto err;
1303
0
            /* Replace points[i]->Z by its inverse. */
1304
0
            if (!BN_copy(points[i]->Z, tmp_Z))
1305
0
                goto err;
1306
0
        }
1307
0
    }
1308
0
1309
0
    if (!BN_is_zero(points[0]->Z)) {
1310
0
        /* Replace points[0]->Z by its inverse. */
1311
0
        if (!BN_copy(points[0]->Z, tmp))
1312
0
            goto err;
1313
0
    }
1314
0
1315
0
    /* Finally, fix up the X and Y coordinates for all points. */
1316
0
1317
0
    for (i = 0; i < num; i++) {
1318
0
        EC_POINT *p = points[i];
1319
0
1320
0
        if (!BN_is_zero(p->Z)) {
1321
0
            /* turn  (X, Y, 1/Z)  into  (X/Z^2, Y/Z^3, 1) */
1322
0
1323
0
            if (!group->meth->field_sqr(group, tmp, p->Z, ctx))
1324
0
                goto err;
1325
0
            if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))
1326
0
                goto err;
1327
0
1328
0
            if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))
1329
0
                goto err;
1330
0
            if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))
1331
0
                goto err;
1332
0
1333
0
            if (group->meth->field_set_to_one != 0) {
1334
0
                if (!group->meth->field_set_to_one(group, p->Z, ctx))
1335
0
                    goto err;
1336
0
            } else {
1337
0
                if (!BN_one(p->Z))
1338
0
                    goto err;
1339
0
            }
1340
0
            p->Z_is_one = 1;
1341
0
        }
1342
0
    }
1343
0
1344
0
    ret = 1;
1345
0
1346
0
 err:
1347
0
    BN_CTX_end(ctx);
1348
0
    BN_CTX_free(new_ctx);
1349
0
    if (prod_Z != NULL) {
1350
0
        for (i = 0; i < num; i++) {
1351
0
            if (prod_Z[i] == NULL)
1352
0
                break;
1353
0
            BN_clear_free(prod_Z[i]);
1354
0
        }
1355
0
        OPENSSL_free(prod_Z);
1356
0
    }
1357
0
    return ret;
1358
0
}
1359
1360
int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1361
                            const BIGNUM *b, BN_CTX *ctx)
1362
0
{
1363
0
    return BN_mod_mul(r, a, b, group->field, ctx);
1364
0
}
1365
1366
int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1367
                            BN_CTX *ctx)
1368
0
{
1369
0
    return BN_mod_sqr(r, a, group->field, ctx);
1370
0
}
1371
1372
/*-
1373
 * Apply randomization of EC point projective coordinates:
1374
 *
1375
 *   (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z)
1376
 *   lambda = [1,group->field)
1377
 *
1378
 */
1379
int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1380
                                    BN_CTX *ctx)
1381
0
{
1382
0
    int ret = 0;
1383
0
    BIGNUM *lambda = NULL;
1384
0
    BIGNUM *temp = NULL;
1385
0
1386
0
    BN_CTX_start(ctx);
1387
0
    lambda = BN_CTX_get(ctx);
1388
0
    temp = BN_CTX_get(ctx);
1389
0
    if (temp == NULL) {
1390
0
        ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
1391
0
        goto err;
1392
0
    }
1393
0
1394
0
    /* make sure lambda is not zero */
1395
0
    do {
1396
0
        if (!BN_priv_rand_range(lambda, group->field)) {
1397
0
            ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_BN_LIB);
1398
0
            goto err;
1399
0
        }
1400
0
    } while (BN_is_zero(lambda));
1401
0
1402
0
    /* if field_encode defined convert between representations */
1403
0
    if (group->meth->field_encode != NULL
1404
0
        && !group->meth->field_encode(group, lambda, lambda, ctx))
1405
0
        goto err;
1406
0
    if (!group->meth->field_mul(group, p->Z, p->Z, lambda, ctx))
1407
0
        goto err;
1408
0
    if (!group->meth->field_sqr(group, temp, lambda, ctx))
1409
0
        goto err;
1410
0
    if (!group->meth->field_mul(group, p->X, p->X, temp, ctx))
1411
0
        goto err;
1412
0
    if (!group->meth->field_mul(group, temp, temp, lambda, ctx))
1413
0
        goto err;
1414
0
    if (!group->meth->field_mul(group, p->Y, p->Y, temp, ctx))
1415
0
        goto err;
1416
0
    p->Z_is_one = 0;
1417
0
1418
0
    ret = 1;
1419
0
1420
0
 err:
1421
0
    BN_CTX_end(ctx);
1422
0
    return ret;
1423
0
}
1424
1425
/*-
1426
 * Set s := p, r := 2p.
1427
 *
1428
 * For doubling we use Formula 3 from Izu-Takagi "A fast parallel elliptic curve
1429
 * multiplication resistant against side channel attacks" appendix, as described
1430
 * at
1431
 * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#doubling-dbl-2002-it-2
1432
 *
1433
 * The input point p will be in randomized Jacobian projective coords:
1434
 *      x = X/Z**2, y=Y/Z**3
1435
 *
1436
 * The output points p, s, and r are converted to standard (homogeneous)
1437
 * projective coords:
1438
 *      x = X/Z, y=Y/Z
1439
 */
1440
int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
1441
                             EC_POINT *r, EC_POINT *s,
1442
                             EC_POINT *p, BN_CTX *ctx)
1443
0
{
1444
0
    BIGNUM *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
1445
0
1446
0
    t1 = r->Z;
1447
0
    t2 = r->Y;
1448
0
    t3 = s->X;
1449
0
    t4 = r->X;
1450
0
    t5 = s->Y;
1451
0
    t6 = s->Z;
1452
0
1453
0
    /* convert p: (X,Y,Z) -> (XZ,Y,Z**3) */
1454
0
    if (!group->meth->field_mul(group, p->X, p->X, p->Z, ctx)
1455
0
        || !group->meth->field_sqr(group, t1, p->Z, ctx)
1456
0
        || !group->meth->field_mul(group, p->Z, p->Z, t1, ctx)
1457
0
        /* r := 2p */
1458
0
        || !group->meth->field_sqr(group, t2, p->X, ctx)
1459
0
        || !group->meth->field_sqr(group, t3, p->Z, ctx)
1460
0
        || !group->meth->field_mul(group, t4, t3, group->a, ctx)
1461
0
        || !BN_mod_sub_quick(t5, t2, t4, group->field)
1462
0
        || !BN_mod_add_quick(t2, t2, t4, group->field)
1463
0
        || !group->meth->field_sqr(group, t5, t5, ctx)
1464
0
        || !group->meth->field_mul(group, t6, t3, group->b, ctx)
1465
0
        || !group->meth->field_mul(group, t1, p->X, p->Z, ctx)
1466
0
        || !group->meth->field_mul(group, t4, t1, t6, ctx)
1467
0
        || !BN_mod_lshift_quick(t4, t4, 3, group->field)
1468
0
        /* r->X coord output */
1469
0
        || !BN_mod_sub_quick(r->X, t5, t4, group->field)
1470
0
        || !group->meth->field_mul(group, t1, t1, t2, ctx)
1471
0
        || !group->meth->field_mul(group, t2, t3, t6, ctx)
1472
0
        || !BN_mod_add_quick(t1, t1, t2, group->field)
1473
0
        /* r->Z coord output */
1474
0
        || !BN_mod_lshift_quick(r->Z, t1, 2, group->field)
1475
0
        || !EC_POINT_copy(s, p))
1476
0
        return 0;
1477
0
1478
0
    r->Z_is_one = 0;
1479
0
    s->Z_is_one = 0;
1480
0
    p->Z_is_one = 0;
1481
0
1482
0
    return 1;
1483
0
}
1484
1485
/*-
1486
 * Differential addition-and-doubling using  Eq. (9) and (10) from Izu-Takagi
1487
 * "A fast parallel elliptic curve multiplication resistant against side channel
1488
 * attacks", as described at
1489
 * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-ladd-2002-it-4
1490
 */
1491
int ec_GFp_simple_ladder_step(const EC_GROUP *group,
1492
                              EC_POINT *r, EC_POINT *s,
1493
                              EC_POINT *p, BN_CTX *ctx)
1494
0
{
1495
0
    int ret = 0;
1496
0
    BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6, *t7 = NULL;
1497
0
1498
0
    BN_CTX_start(ctx);
1499
0
    t0 = BN_CTX_get(ctx);
1500
0
    t1 = BN_CTX_get(ctx);
1501
0
    t2 = BN_CTX_get(ctx);
1502
0
    t3 = BN_CTX_get(ctx);
1503
0
    t4 = BN_CTX_get(ctx);
1504
0
    t5 = BN_CTX_get(ctx);
1505
0
    t6 = BN_CTX_get(ctx);
1506
0
    t7 = BN_CTX_get(ctx);
1507
0
1508
0
    if (t7 == NULL
1509
0
        || !group->meth->field_mul(group, t0, r->X, s->X, ctx)
1510
0
        || !group->meth->field_mul(group, t1, r->Z, s->Z, ctx)
1511
0
        || !group->meth->field_mul(group, t2, r->X, s->Z, ctx)
1512
0
        || !group->meth->field_mul(group, t3, r->Z, s->X, ctx)
1513
0
        || !group->meth->field_mul(group, t4, group->a, t1, ctx)
1514
0
        || !BN_mod_add_quick(t0, t0, t4, group->field)
1515
0
        || !BN_mod_add_quick(t4, t3, t2, group->field)
1516
0
        || !group->meth->field_mul(group, t0, t4, t0, ctx)
1517
0
        || !group->meth->field_sqr(group, t1, t1, ctx)
1518
0
        || !BN_mod_lshift_quick(t7, group->b, 2, group->field)
1519
0
        || !group->meth->field_mul(group, t1, t7, t1, ctx)
1520
0
        || !BN_mod_lshift1_quick(t0, t0, group->field)
1521
0
        || !BN_mod_add_quick(t0, t1, t0, group->field)
1522
0
        || !BN_mod_sub_quick(t1, t2, t3, group->field)
1523
0
        || !group->meth->field_sqr(group, t1, t1, ctx)
1524
0
        || !group->meth->field_mul(group, t3, t1, p->X, ctx)
1525
0
        || !group->meth->field_mul(group, t0, p->Z, t0, ctx)
1526
0
        /* s->X coord output */
1527
0
        || !BN_mod_sub_quick(s->X, t0, t3, group->field)
1528
0
        /* s->Z coord output */
1529
0
        || !group->meth->field_mul(group, s->Z, p->Z, t1, ctx)
1530
0
        || !group->meth->field_sqr(group, t3, r->X, ctx)
1531
0
        || !group->meth->field_sqr(group, t2, r->Z, ctx)
1532
0
        || !group->meth->field_mul(group, t4, t2, group->a, ctx)
1533
0
        || !BN_mod_add_quick(t5, r->X, r->Z, group->field)
1534
0
        || !group->meth->field_sqr(group, t5, t5, ctx)
1535
0
        || !BN_mod_sub_quick(t5, t5, t3, group->field)
1536
0
        || !BN_mod_sub_quick(t5, t5, t2, group->field)
1537
0
        || !BN_mod_sub_quick(t6, t3, t4, group->field)
1538
0
        || !group->meth->field_sqr(group, t6, t6, ctx)
1539
0
        || !group->meth->field_mul(group, t0, t2, t5, ctx)
1540
0
        || !group->meth->field_mul(group, t0, t7, t0, ctx)
1541
0
        /* r->X coord output */
1542
0
        || !BN_mod_sub_quick(r->X, t6, t0, group->field)
1543
0
        || !BN_mod_add_quick(t6, t3, t4, group->field)
1544
0
        || !group->meth->field_sqr(group, t3, t2, ctx)
1545
0
        || !group->meth->field_mul(group, t7, t3, t7, ctx)
1546
0
        || !group->meth->field_mul(group, t5, t5, t6, ctx)
1547
0
        || !BN_mod_lshift1_quick(t5, t5, group->field)
1548
0
        /* r->Z coord output */
1549
0
        || !BN_mod_add_quick(r->Z, t7, t5, group->field))
1550
0
        goto err;
1551
0
1552
0
    ret = 1;
1553
0
1554
0
 err:
1555
0
    BN_CTX_end(ctx);
1556
0
    return ret;
1557
0
}
1558
1559
/*-
1560
 * Recovers the y-coordinate of r using Eq. (8) from Brier-Joye, "Weierstrass
1561
 * Elliptic Curves and Side-Channel Attacks", modified to work in projective
1562
 * coordinates and return r in Jacobian projective coordinates.
1563
 *
1564
 * X4 = two*Y1*X2*Z3*Z2*Z1;
1565
 * Y4 = two*b*Z3*SQR(Z2*Z1) + Z3*(a*Z2*Z1+X1*X2)*(X1*Z2+X2*Z1) - X3*SQR(X1*Z2-X2*Z1);
1566
 * Z4 = two*Y1*Z3*SQR(Z2)*Z1;
1567
 *
1568
 * Z4 != 0 because:
1569
 *  - Z1==0 implies p is at infinity, which would have caused an early exit in
1570
 *    the caller;
1571
 *  - Z2==0 implies r is at infinity (handled by the BN_is_zero(r->Z) branch);
1572
 *  - Z3==0 implies s is at infinity (handled by the BN_is_zero(s->Z) branch);
1573
 *  - Y1==0 implies p has order 2, so either r or s are infinity and handled by
1574
 *    one of the BN_is_zero(...) branches.
1575
 */
1576
int ec_GFp_simple_ladder_post(const EC_GROUP *group,
1577
                              EC_POINT *r, EC_POINT *s,
1578
                              EC_POINT *p, BN_CTX *ctx)
1579
0
{
1580
0
    int ret = 0;
1581
0
    BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
1582
0
1583
0
    if (BN_is_zero(r->Z))
1584
0
        return EC_POINT_set_to_infinity(group, r);
1585
0
1586
0
    if (BN_is_zero(s->Z)) {
1587
0
        /* (X,Y,Z) -> (XZ,YZ**2,Z) */
1588
0
        if (!group->meth->field_mul(group, r->X, p->X, p->Z, ctx)
1589
0
            || !group->meth->field_sqr(group, r->Z, p->Z, ctx)
1590
0
            || !group->meth->field_mul(group, r->Y, p->Y, r->Z, ctx)
1591
0
            || !BN_copy(r->Z, p->Z)
1592
0
            || !EC_POINT_invert(group, r, ctx))
1593
0
            return 0;
1594
0
        return 1;
1595
0
    }
1596
0
1597
0
    BN_CTX_start(ctx);
1598
0
    t0 = BN_CTX_get(ctx);
1599
0
    t1 = BN_CTX_get(ctx);
1600
0
    t2 = BN_CTX_get(ctx);
1601
0
    t3 = BN_CTX_get(ctx);
1602
0
    t4 = BN_CTX_get(ctx);
1603
0
    t5 = BN_CTX_get(ctx);
1604
0
    t6 = BN_CTX_get(ctx);
1605
0
1606
0
    if (t6 == NULL
1607
0
        || !BN_mod_lshift1_quick(t0, p->Y, group->field)
1608
0
        || !group->meth->field_mul(group, t1, r->X, p->Z, ctx)
1609
0
        || !group->meth->field_mul(group, t2, r->Z, s->Z, ctx)
1610
0
        || !group->meth->field_mul(group, t2, t1, t2, ctx)
1611
0
        || !group->meth->field_mul(group, t3, t2, t0, ctx)
1612
0
        || !group->meth->field_mul(group, t2, r->Z, p->Z, ctx)
1613
0
        || !group->meth->field_sqr(group, t4, t2, ctx)
1614
0
        || !BN_mod_lshift1_quick(t5, group->b, group->field)
1615
0
        || !group->meth->field_mul(group, t4, t4, t5, ctx)
1616
0
        || !group->meth->field_mul(group, t6, t2, group->a, ctx)
1617
0
        || !group->meth->field_mul(group, t5, r->X, p->X, ctx)
1618
0
        || !BN_mod_add_quick(t5, t6, t5, group->field)
1619
0
        || !group->meth->field_mul(group, t6, r->Z, p->X, ctx)
1620
0
        || !BN_mod_add_quick(t2, t6, t1, group->field)
1621
0
        || !group->meth->field_mul(group, t5, t5, t2, ctx)
1622
0
        || !BN_mod_sub_quick(t6, t6, t1, group->field)
1623
0
        || !group->meth->field_sqr(group, t6, t6, ctx)
1624
0
        || !group->meth->field_mul(group, t6, t6, s->X, ctx)
1625
0
        || !BN_mod_add_quick(t4, t5, t4, group->field)
1626
0
        || !group->meth->field_mul(group, t4, t4, s->Z, ctx)
1627
0
        || !BN_mod_sub_quick(t4, t4, t6, group->field)
1628
0
        || !group->meth->field_sqr(group, t5, r->Z, ctx)
1629
0
        || !group->meth->field_mul(group, r->Z, p->Z, s->Z, ctx)
1630
0
        || !group->meth->field_mul(group, r->Z, t5, r->Z, ctx)
1631
0
        || !group->meth->field_mul(group, r->Z, r->Z, t0, ctx)
1632
0
        /* t3 := X, t4 := Y */
1633
0
        /* (X,Y,Z) -> (XZ,YZ**2,Z) */
1634
0
        || !group->meth->field_mul(group, r->X, t3, r->Z, ctx)
1635
0
        || !group->meth->field_sqr(group, t3, r->Z, ctx)
1636
0
        || !group->meth->field_mul(group, r->Y, t4, t3, ctx))
1637
0
        goto err;
1638
0
1639
0
    ret = 1;
1640
0
1641
0
 err:
1642
0
    BN_CTX_end(ctx);
1643
0
    return ret;
1644
0
}