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