SCA hardening for mod. field inversion in EC_GROUP
[openssl.git] / crypto / ec / ecp_smpl.c
1 /*
2  * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  * Portions of this software developed by SUN MICROSYSTEMS, INC.,
13  * and contributed to the OpenSSL project.
14  */
15
16 #include <openssl/err.h>
17 #include <openssl/symhacks.h>
18
19 #include "ec_lcl.h"
20
21 const EC_METHOD *EC_GFp_simple_method(void)
22 {
23     static const EC_METHOD ret = {
24         EC_FLAGS_DEFAULT_OCT,
25         NID_X9_62_prime_field,
26         ec_GFp_simple_group_init,
27         ec_GFp_simple_group_finish,
28         ec_GFp_simple_group_clear_finish,
29         ec_GFp_simple_group_copy,
30         ec_GFp_simple_group_set_curve,
31         ec_GFp_simple_group_get_curve,
32         ec_GFp_simple_group_get_degree,
33         ec_group_simple_order_bits,
34         ec_GFp_simple_group_check_discriminant,
35         ec_GFp_simple_point_init,
36         ec_GFp_simple_point_finish,
37         ec_GFp_simple_point_clear_finish,
38         ec_GFp_simple_point_copy,
39         ec_GFp_simple_point_set_to_infinity,
40         ec_GFp_simple_set_Jprojective_coordinates_GFp,
41         ec_GFp_simple_get_Jprojective_coordinates_GFp,
42         ec_GFp_simple_point_set_affine_coordinates,
43         ec_GFp_simple_point_get_affine_coordinates,
44         0, 0, 0,
45         ec_GFp_simple_add,
46         ec_GFp_simple_dbl,
47         ec_GFp_simple_invert,
48         ec_GFp_simple_is_at_infinity,
49         ec_GFp_simple_is_on_curve,
50         ec_GFp_simple_cmp,
51         ec_GFp_simple_make_affine,
52         ec_GFp_simple_points_make_affine,
53         0 /* mul */ ,
54         0 /* precompute_mult */ ,
55         0 /* have_precompute_mult */ ,
56         ec_GFp_simple_field_mul,
57         ec_GFp_simple_field_sqr,
58         0 /* field_div */ ,
59         ec_GFp_simple_field_inv,
60         0 /* field_encode */ ,
61         0 /* field_decode */ ,
62         0,                      /* field_set_to_one */
63         ec_key_simple_priv2oct,
64         ec_key_simple_oct2priv,
65         0, /* set private */
66         ec_key_simple_generate_key,
67         ec_key_simple_check_key,
68         ec_key_simple_generate_public_key,
69         0, /* keycopy */
70         0, /* keyfinish */
71         ecdh_simple_compute_key,
72         ec_GFp_simple_blind_coordinates
73     };
74
75     return &ret;
76 }
77
78 /*
79  * Most method functions in this file are designed to work with
80  * non-trivial representations of field elements if necessary
81  * (see ecp_mont.c): while standard modular addition and subtraction
82  * are used, the field_mul and field_sqr methods will be used for
83  * multiplication, and field_encode and field_decode (if defined)
84  * will be used for converting between representations.
85  *
86  * Functions ec_GFp_simple_points_make_affine() and
87  * ec_GFp_simple_point_get_affine_coordinates() specifically assume
88  * that if a non-trivial representation is used, it is a Montgomery
89  * representation (i.e. 'encoding' means multiplying by some factor R).
90  */
91
92 int ec_GFp_simple_group_init(EC_GROUP *group)
93 {
94     group->field = BN_new();
95     group->a = BN_new();
96     group->b = BN_new();
97     if (group->field == NULL || group->a == NULL || group->b == NULL) {
98         BN_free(group->field);
99         BN_free(group->a);
100         BN_free(group->b);
101         return 0;
102     }
103     group->a_is_minus3 = 0;
104     return 1;
105 }
106
107 void ec_GFp_simple_group_finish(EC_GROUP *group)
108 {
109     BN_free(group->field);
110     BN_free(group->a);
111     BN_free(group->b);
112 }
113
114 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
115 {
116     BN_clear_free(group->field);
117     BN_clear_free(group->a);
118     BN_clear_free(group->b);
119 }
120
121 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
122 {
123     if (!BN_copy(dest->field, src->field))
124         return 0;
125     if (!BN_copy(dest->a, src->a))
126         return 0;
127     if (!BN_copy(dest->b, src->b))
128         return 0;
129
130     dest->a_is_minus3 = src->a_is_minus3;
131
132     return 1;
133 }
134
135 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
136                                   const BIGNUM *p, const BIGNUM *a,
137                                   const BIGNUM *b, BN_CTX *ctx)
138 {
139     int ret = 0;
140     BN_CTX *new_ctx = NULL;
141     BIGNUM *tmp_a;
142
143     /* p must be a prime > 3 */
144     if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
145         ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
146         return 0;
147     }
148
149     if (ctx == NULL) {
150         ctx = new_ctx = BN_CTX_new();
151         if (ctx == NULL)
152             return 0;
153     }
154
155     BN_CTX_start(ctx);
156     tmp_a = BN_CTX_get(ctx);
157     if (tmp_a == NULL)
158         goto err;
159
160     /* group->field */
161     if (!BN_copy(group->field, p))
162         goto err;
163     BN_set_negative(group->field, 0);
164
165     /* group->a */
166     if (!BN_nnmod(tmp_a, a, p, ctx))
167         goto err;
168     if (group->meth->field_encode) {
169         if (!group->meth->field_encode(group, group->a, tmp_a, ctx))
170             goto err;
171     } else if (!BN_copy(group->a, tmp_a))
172         goto err;
173
174     /* group->b */
175     if (!BN_nnmod(group->b, b, p, ctx))
176         goto err;
177     if (group->meth->field_encode)
178         if (!group->meth->field_encode(group, group->b, group->b, ctx))
179             goto err;
180
181     /* group->a_is_minus3 */
182     if (!BN_add_word(tmp_a, 3))
183         goto err;
184     group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));
185
186     ret = 1;
187
188  err:
189     BN_CTX_end(ctx);
190     BN_CTX_free(new_ctx);
191     return ret;
192 }
193
194 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
195                                   BIGNUM *b, BN_CTX *ctx)
196 {
197     int ret = 0;
198     BN_CTX *new_ctx = NULL;
199
200     if (p != NULL) {
201         if (!BN_copy(p, group->field))
202             return 0;
203     }
204
205     if (a != NULL || b != NULL) {
206         if (group->meth->field_decode) {
207             if (ctx == NULL) {
208                 ctx = new_ctx = BN_CTX_new();
209                 if (ctx == NULL)
210                     return 0;
211             }
212             if (a != NULL) {
213                 if (!group->meth->field_decode(group, a, group->a, ctx))
214                     goto err;
215             }
216             if (b != NULL) {
217                 if (!group->meth->field_decode(group, b, group->b, ctx))
218                     goto err;
219             }
220         } else {
221             if (a != NULL) {
222                 if (!BN_copy(a, group->a))
223                     goto err;
224             }
225             if (b != NULL) {
226                 if (!BN_copy(b, group->b))
227                     goto err;
228             }
229         }
230     }
231
232     ret = 1;
233
234  err:
235     BN_CTX_free(new_ctx);
236     return ret;
237 }
238
239 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
240 {
241     return BN_num_bits(group->field);
242 }
243
244 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
245 {
246     int ret = 0;
247     BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
248     const BIGNUM *p = group->field;
249     BN_CTX *new_ctx = NULL;
250
251     if (ctx == NULL) {
252         ctx = new_ctx = BN_CTX_new();
253         if (ctx == NULL) {
254             ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
255                   ERR_R_MALLOC_FAILURE);
256             goto err;
257         }
258     }
259     BN_CTX_start(ctx);
260     a = BN_CTX_get(ctx);
261     b = BN_CTX_get(ctx);
262     tmp_1 = BN_CTX_get(ctx);
263     tmp_2 = BN_CTX_get(ctx);
264     order = BN_CTX_get(ctx);
265     if (order == NULL)
266         goto err;
267
268     if (group->meth->field_decode) {
269         if (!group->meth->field_decode(group, a, group->a, ctx))
270             goto err;
271         if (!group->meth->field_decode(group, b, group->b, ctx))
272             goto err;
273     } else {
274         if (!BN_copy(a, group->a))
275             goto err;
276         if (!BN_copy(b, group->b))
277             goto err;
278     }
279
280     /*-
281      * check the discriminant:
282      * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
283      * 0 =< a, b < p
284      */
285     if (BN_is_zero(a)) {
286         if (BN_is_zero(b))
287             goto err;
288     } else if (!BN_is_zero(b)) {
289         if (!BN_mod_sqr(tmp_1, a, p, ctx))
290             goto err;
291         if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
292             goto err;
293         if (!BN_lshift(tmp_1, tmp_2, 2))
294             goto err;
295         /* tmp_1 = 4*a^3 */
296
297         if (!BN_mod_sqr(tmp_2, b, p, ctx))
298             goto err;
299         if (!BN_mul_word(tmp_2, 27))
300             goto err;
301         /* tmp_2 = 27*b^2 */
302
303         if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
304             goto err;
305         if (BN_is_zero(a))
306             goto err;
307     }
308     ret = 1;
309
310  err:
311     if (ctx != NULL)
312         BN_CTX_end(ctx);
313     BN_CTX_free(new_ctx);
314     return ret;
315 }
316
317 int ec_GFp_simple_point_init(EC_POINT *point)
318 {
319     point->X = BN_new();
320     point->Y = BN_new();
321     point->Z = BN_new();
322     point->Z_is_one = 0;
323
324     if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
325         BN_free(point->X);
326         BN_free(point->Y);
327         BN_free(point->Z);
328         return 0;
329     }
330     return 1;
331 }
332
333 void ec_GFp_simple_point_finish(EC_POINT *point)
334 {
335     BN_free(point->X);
336     BN_free(point->Y);
337     BN_free(point->Z);
338 }
339
340 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
341 {
342     BN_clear_free(point->X);
343     BN_clear_free(point->Y);
344     BN_clear_free(point->Z);
345     point->Z_is_one = 0;
346 }
347
348 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
349 {
350     if (!BN_copy(dest->X, src->X))
351         return 0;
352     if (!BN_copy(dest->Y, src->Y))
353         return 0;
354     if (!BN_copy(dest->Z, src->Z))
355         return 0;
356     dest->Z_is_one = src->Z_is_one;
357     dest->curve_name = src->curve_name;
358
359     return 1;
360 }
361
362 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
363                                         EC_POINT *point)
364 {
365     point->Z_is_one = 0;
366     BN_zero(point->Z);
367     return 1;
368 }
369
370 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
371                                                   EC_POINT *point,
372                                                   const BIGNUM *x,
373                                                   const BIGNUM *y,
374                                                   const BIGNUM *z,
375                                                   BN_CTX *ctx)
376 {
377     BN_CTX *new_ctx = NULL;
378     int ret = 0;
379
380     if (ctx == NULL) {
381         ctx = new_ctx = BN_CTX_new();
382         if (ctx == NULL)
383             return 0;
384     }
385
386     if (x != NULL) {
387         if (!BN_nnmod(point->X, x, group->field, ctx))
388             goto err;
389         if (group->meth->field_encode) {
390             if (!group->meth->field_encode(group, point->X, point->X, ctx))
391                 goto err;
392         }
393     }
394
395     if (y != NULL) {
396         if (!BN_nnmod(point->Y, y, group->field, ctx))
397             goto err;
398         if (group->meth->field_encode) {
399             if (!group->meth->field_encode(group, point->Y, point->Y, ctx))
400                 goto err;
401         }
402     }
403
404     if (z != NULL) {
405         int Z_is_one;
406
407         if (!BN_nnmod(point->Z, z, group->field, ctx))
408             goto err;
409         Z_is_one = BN_is_one(point->Z);
410         if (group->meth->field_encode) {
411             if (Z_is_one && (group->meth->field_set_to_one != 0)) {
412                 if (!group->meth->field_set_to_one(group, point->Z, ctx))
413                     goto err;
414             } else {
415                 if (!group->
416                     meth->field_encode(group, point->Z, point->Z, ctx))
417                     goto err;
418             }
419         }
420         point->Z_is_one = Z_is_one;
421     }
422
423     ret = 1;
424
425  err:
426     BN_CTX_free(new_ctx);
427     return ret;
428 }
429
430 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
431                                                   const EC_POINT *point,
432                                                   BIGNUM *x, BIGNUM *y,
433                                                   BIGNUM *z, BN_CTX *ctx)
434 {
435     BN_CTX *new_ctx = NULL;
436     int ret = 0;
437
438     if (group->meth->field_decode != 0) {
439         if (ctx == NULL) {
440             ctx = new_ctx = BN_CTX_new();
441             if (ctx == NULL)
442                 return 0;
443         }
444
445         if (x != NULL) {
446             if (!group->meth->field_decode(group, x, point->X, ctx))
447                 goto err;
448         }
449         if (y != NULL) {
450             if (!group->meth->field_decode(group, y, point->Y, ctx))
451                 goto err;
452         }
453         if (z != NULL) {
454             if (!group->meth->field_decode(group, z, point->Z, ctx))
455                 goto err;
456         }
457     } else {
458         if (x != NULL) {
459             if (!BN_copy(x, point->X))
460                 goto err;
461         }
462         if (y != NULL) {
463             if (!BN_copy(y, point->Y))
464                 goto err;
465         }
466         if (z != NULL) {
467             if (!BN_copy(z, point->Z))
468                 goto err;
469         }
470     }
471
472     ret = 1;
473
474  err:
475     BN_CTX_free(new_ctx);
476     return ret;
477 }
478
479 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
480                                                EC_POINT *point,
481                                                const BIGNUM *x,
482                                                const BIGNUM *y, BN_CTX *ctx)
483 {
484     if (x == NULL || y == NULL) {
485         /*
486          * unlike for projective coordinates, we do not tolerate this
487          */
488         ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
489               ERR_R_PASSED_NULL_PARAMETER);
490         return 0;
491     }
492
493     return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y,
494                                                     BN_value_one(), ctx);
495 }
496
497 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
498                                                const EC_POINT *point,
499                                                BIGNUM *x, BIGNUM *y,
500                                                BN_CTX *ctx)
501 {
502     BN_CTX *new_ctx = NULL;
503     BIGNUM *Z, *Z_1, *Z_2, *Z_3;
504     const BIGNUM *Z_;
505     int ret = 0;
506
507     if (EC_POINT_is_at_infinity(group, point)) {
508         ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
509               EC_R_POINT_AT_INFINITY);
510         return 0;
511     }
512
513     if (ctx == NULL) {
514         ctx = new_ctx = BN_CTX_new();
515         if (ctx == NULL)
516             return 0;
517     }
518
519     BN_CTX_start(ctx);
520     Z = BN_CTX_get(ctx);
521     Z_1 = BN_CTX_get(ctx);
522     Z_2 = BN_CTX_get(ctx);
523     Z_3 = BN_CTX_get(ctx);
524     if (Z_3 == NULL)
525         goto err;
526
527     /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */
528
529     if (group->meth->field_decode) {
530         if (!group->meth->field_decode(group, Z, point->Z, ctx))
531             goto err;
532         Z_ = Z;
533     } else {
534         Z_ = point->Z;
535     }
536
537     if (BN_is_one(Z_)) {
538         if (group->meth->field_decode) {
539             if (x != NULL) {
540                 if (!group->meth->field_decode(group, x, point->X, ctx))
541                     goto err;
542             }
543             if (y != NULL) {
544                 if (!group->meth->field_decode(group, y, point->Y, ctx))
545                     goto err;
546             }
547         } else {
548             if (x != NULL) {
549                 if (!BN_copy(x, point->X))
550                     goto err;
551             }
552             if (y != NULL) {
553                 if (!BN_copy(y, point->Y))
554                     goto err;
555             }
556         }
557     } else {
558         if (!group->meth->field_inv(group, Z_1, Z_, ctx)) {
559             ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
560                   ERR_R_BN_LIB);
561             goto err;
562         }
563
564         if (group->meth->field_encode == 0) {
565             /* field_sqr works on standard representation */
566             if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
567                 goto err;
568         } else {
569             if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx))
570                 goto err;
571         }
572
573         if (x != NULL) {
574             /*
575              * in the Montgomery case, field_mul will cancel out Montgomery
576              * factor in X:
577              */
578             if (!group->meth->field_mul(group, x, point->X, Z_2, ctx))
579                 goto err;
580         }
581
582         if (y != NULL) {
583             if (group->meth->field_encode == 0) {
584                 /*
585                  * field_mul works on standard representation
586                  */
587                 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
588                     goto err;
589             } else {
590                 if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx))
591                     goto err;
592             }
593
594             /*
595              * in the Montgomery case, field_mul will cancel out Montgomery
596              * factor in Y:
597              */
598             if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx))
599                 goto err;
600         }
601     }
602
603     ret = 1;
604
605  err:
606     BN_CTX_end(ctx);
607     BN_CTX_free(new_ctx);
608     return ret;
609 }
610
611 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
612                       const EC_POINT *b, BN_CTX *ctx)
613 {
614     int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
615                       const BIGNUM *, BN_CTX *);
616     int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
617     const BIGNUM *p;
618     BN_CTX *new_ctx = NULL;
619     BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
620     int ret = 0;
621
622     if (a == b)
623         return EC_POINT_dbl(group, r, a, ctx);
624     if (EC_POINT_is_at_infinity(group, a))
625         return EC_POINT_copy(r, b);
626     if (EC_POINT_is_at_infinity(group, b))
627         return EC_POINT_copy(r, a);
628
629     field_mul = group->meth->field_mul;
630     field_sqr = group->meth->field_sqr;
631     p = group->field;
632
633     if (ctx == NULL) {
634         ctx = new_ctx = BN_CTX_new();
635         if (ctx == NULL)
636             return 0;
637     }
638
639     BN_CTX_start(ctx);
640     n0 = BN_CTX_get(ctx);
641     n1 = BN_CTX_get(ctx);
642     n2 = BN_CTX_get(ctx);
643     n3 = BN_CTX_get(ctx);
644     n4 = BN_CTX_get(ctx);
645     n5 = BN_CTX_get(ctx);
646     n6 = BN_CTX_get(ctx);
647     if (n6 == NULL)
648         goto end;
649
650     /*
651      * Note that in this function we must not read components of 'a' or 'b'
652      * once we have written the corresponding components of 'r'. ('r' might
653      * be one of 'a' or 'b'.)
654      */
655
656     /* n1, n2 */
657     if (b->Z_is_one) {
658         if (!BN_copy(n1, a->X))
659             goto end;
660         if (!BN_copy(n2, a->Y))
661             goto end;
662         /* n1 = X_a */
663         /* n2 = Y_a */
664     } else {
665         if (!field_sqr(group, n0, b->Z, ctx))
666             goto end;
667         if (!field_mul(group, n1, a->X, n0, ctx))
668             goto end;
669         /* n1 = X_a * Z_b^2 */
670
671         if (!field_mul(group, n0, n0, b->Z, ctx))
672             goto end;
673         if (!field_mul(group, n2, a->Y, n0, ctx))
674             goto end;
675         /* n2 = Y_a * Z_b^3 */
676     }
677
678     /* n3, n4 */
679     if (a->Z_is_one) {
680         if (!BN_copy(n3, b->X))
681             goto end;
682         if (!BN_copy(n4, b->Y))
683             goto end;
684         /* n3 = X_b */
685         /* n4 = Y_b */
686     } else {
687         if (!field_sqr(group, n0, a->Z, ctx))
688             goto end;
689         if (!field_mul(group, n3, b->X, n0, ctx))
690             goto end;
691         /* n3 = X_b * Z_a^2 */
692
693         if (!field_mul(group, n0, n0, a->Z, ctx))
694             goto end;
695         if (!field_mul(group, n4, b->Y, n0, ctx))
696             goto end;
697         /* n4 = Y_b * Z_a^3 */
698     }
699
700     /* n5, n6 */
701     if (!BN_mod_sub_quick(n5, n1, n3, p))
702         goto end;
703     if (!BN_mod_sub_quick(n6, n2, n4, p))
704         goto end;
705     /* n5 = n1 - n3 */
706     /* n6 = n2 - n4 */
707
708     if (BN_is_zero(n5)) {
709         if (BN_is_zero(n6)) {
710             /* a is the same point as b */
711             BN_CTX_end(ctx);
712             ret = EC_POINT_dbl(group, r, a, ctx);
713             ctx = NULL;
714             goto end;
715         } else {
716             /* a is the inverse of b */
717             BN_zero(r->Z);
718             r->Z_is_one = 0;
719             ret = 1;
720             goto end;
721         }
722     }
723
724     /* 'n7', 'n8' */
725     if (!BN_mod_add_quick(n1, n1, n3, p))
726         goto end;
727     if (!BN_mod_add_quick(n2, n2, n4, p))
728         goto end;
729     /* 'n7' = n1 + n3 */
730     /* 'n8' = n2 + n4 */
731
732     /* Z_r */
733     if (a->Z_is_one && b->Z_is_one) {
734         if (!BN_copy(r->Z, n5))
735             goto end;
736     } else {
737         if (a->Z_is_one) {
738             if (!BN_copy(n0, b->Z))
739                 goto end;
740         } else if (b->Z_is_one) {
741             if (!BN_copy(n0, a->Z))
742                 goto end;
743         } else {
744             if (!field_mul(group, n0, a->Z, b->Z, ctx))
745                 goto end;
746         }
747         if (!field_mul(group, r->Z, n0, n5, ctx))
748             goto end;
749     }
750     r->Z_is_one = 0;
751     /* Z_r = Z_a * Z_b * n5 */
752
753     /* X_r */
754     if (!field_sqr(group, n0, n6, ctx))
755         goto end;
756     if (!field_sqr(group, n4, n5, ctx))
757         goto end;
758     if (!field_mul(group, n3, n1, n4, ctx))
759         goto end;
760     if (!BN_mod_sub_quick(r->X, n0, n3, p))
761         goto end;
762     /* X_r = n6^2 - n5^2 * 'n7' */
763
764     /* 'n9' */
765     if (!BN_mod_lshift1_quick(n0, r->X, p))
766         goto end;
767     if (!BN_mod_sub_quick(n0, n3, n0, p))
768         goto end;
769     /* n9 = n5^2 * 'n7' - 2 * X_r */
770
771     /* Y_r */
772     if (!field_mul(group, n0, n0, n6, ctx))
773         goto end;
774     if (!field_mul(group, n5, n4, n5, ctx))
775         goto end;               /* now n5 is n5^3 */
776     if (!field_mul(group, n1, n2, n5, ctx))
777         goto end;
778     if (!BN_mod_sub_quick(n0, n0, n1, p))
779         goto end;
780     if (BN_is_odd(n0))
781         if (!BN_add(n0, n0, p))
782             goto end;
783     /* now  0 <= n0 < 2*p,  and n0 is even */
784     if (!BN_rshift1(r->Y, n0))
785         goto end;
786     /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
787
788     ret = 1;
789
790  end:
791     if (ctx)                    /* otherwise we already called BN_CTX_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();
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();
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();
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();
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_GFp(group, point, x, y, ctx))
1187         goto err;
1188     if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
1189         goto err;
1190     if (!point->Z_is_one) {
1191         ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, 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();
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 == NULL || 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         ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, 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  */
1379 int ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1380                             BN_CTX *ctx)
1381 {
1382     BIGNUM *e = NULL;
1383     BN_CTX *new_ctx = NULL;
1384     int ret = 0;
1385
1386     if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
1387         return 0;
1388
1389     BN_CTX_start(ctx);
1390     if ((e = BN_CTX_get(ctx)) == NULL)
1391         goto err;
1392
1393     do {
1394         if (!BN_rand_range(e, group->field))
1395         goto err;
1396     } while (BN_is_zero(e));
1397
1398     /* r := a * e */
1399     if (!group->meth->field_mul(group, r, a, e, ctx))
1400         goto err;
1401     /* r := 1/(a * e) */
1402     if (!BN_mod_inverse(r, r, group->field, ctx)) {
1403         ECerr(EC_F_EC_GFP_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
1404         goto err;
1405     }
1406     /* r := e/(a * e) = 1/a */
1407     if (!group->meth->field_mul(group, r, r, e, ctx))
1408         goto err;
1409
1410     ret = 1;
1411
1412  err:
1413     BN_CTX_end(ctx);
1414     BN_CTX_free(new_ctx);
1415     return ret;
1416 }
1417
1418 /*-
1419  * Apply randomization of EC point projective coordinates:
1420  *
1421  *   (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z)
1422  *   lambda = [1,group->field)
1423  *
1424  */
1425 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1426                                     BN_CTX *ctx)
1427 {
1428     int ret = 0;
1429     BIGNUM *lambda = NULL;
1430     BIGNUM *temp = NULL;
1431
1432     BN_CTX_start(ctx);
1433     lambda = BN_CTX_get(ctx);
1434     temp = BN_CTX_get(ctx);
1435     if (temp == NULL) {
1436         ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
1437         goto err;
1438     }
1439
1440     /* make sure lambda is not zero */
1441     do {
1442         if (!BN_rand_range(lambda, group->field)) {
1443             ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_BN_LIB);
1444             goto err;
1445         }
1446     } while (BN_is_zero(lambda));
1447
1448     /* if field_encode defined convert between representations */
1449     if (group->meth->field_encode != NULL
1450         && !group->meth->field_encode(group, lambda, lambda, ctx))
1451         goto err;
1452     if (!group->meth->field_mul(group, p->Z, p->Z, lambda, ctx))
1453         goto err;
1454     if (!group->meth->field_sqr(group, temp, lambda, ctx))
1455         goto err;
1456     if (!group->meth->field_mul(group, p->X, p->X, temp, ctx))
1457         goto err;
1458     if (!group->meth->field_mul(group, temp, temp, lambda, ctx))
1459         goto err;
1460     if (!group->meth->field_mul(group, p->Y, p->Y, temp, ctx))
1461         goto err;
1462     p->Z_is_one = 0;
1463
1464     ret = 1;
1465
1466  err:
1467      BN_CTX_end(ctx);
1468      return ret;
1469 }
1470