Coverage Report

Created: 2026-10-28 06:28

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