Fix ec_GFp_simple_cmp.
[openssl.git] / crypto / ec / ecp_smpl.c
1 /* crypto/ec/ecp_smpl.c */
2 /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3  * for the OpenSSL project. */
4 /* ====================================================================
5  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer. 
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <openssl/err.h>
59
60 #include "ec_lcl.h"
61
62
63 const EC_METHOD *EC_GFp_simple_method(void)
64         {
65         static const EC_METHOD ret = {
66                 ec_GFp_simple_group_init,
67                 ec_GFp_simple_group_finish,
68                 ec_GFp_simple_group_clear_finish,
69                 ec_GFp_simple_group_copy,
70                 ec_GFp_simple_group_set_curve_GFp,
71                 ec_GFp_simple_group_get_curve_GFp,
72                 ec_GFp_simple_group_set_generator,
73                 ec_GFp_simple_group_get0_generator,
74                 ec_GFp_simple_group_get_order,
75                 ec_GFp_simple_group_get_cofactor,
76                 ec_GFp_simple_point_init,
77                 ec_GFp_simple_point_finish,
78                 ec_GFp_simple_point_clear_finish,
79                 ec_GFp_simple_point_copy,
80                 ec_GFp_simple_point_set_to_infinity,
81                 ec_GFp_simple_set_Jprojective_coordinates_GFp,
82                 ec_GFp_simple_get_Jprojective_coordinates_GFp,
83                 ec_GFp_simple_point_set_affine_coordinates_GFp,
84                 ec_GFp_simple_point_get_affine_coordinates_GFp,
85                 ec_GFp_simple_set_compressed_coordinates_GFp,
86                 ec_GFp_simple_point2oct,
87                 ec_GFp_simple_oct2point,
88                 ec_GFp_simple_add,
89                 ec_GFp_simple_dbl,
90                 ec_GFp_simple_invert,
91                 ec_GFp_simple_is_at_infinity,
92                 ec_GFp_simple_is_on_curve,
93                 ec_GFp_simple_cmp,
94                 ec_GFp_simple_make_affine,
95                 ec_GFp_simple_field_mul,
96                 ec_GFp_simple_field_sqr,
97                 0 /* field_encode */,
98                 0 /* field_decode */ };
99
100         return &ret;
101         }
102
103
104 int ec_GFp_simple_group_init(EC_GROUP *group)
105         {
106         BN_init(&group->field);
107         BN_init(&group->a);
108         BN_init(&group->b);
109         group->a_is_minus3 = 0;
110         group->generator = NULL;
111         BN_init(&group->order);
112         BN_init(&group->cofactor);
113         return 1;
114         }
115
116
117 void ec_GFp_simple_group_finish(EC_GROUP *group)
118         {
119         BN_free(&group->field);
120         BN_free(&group->a);
121         BN_free(&group->b);
122         if (group->generator != NULL)
123                 EC_POINT_free(group->generator);
124         BN_free(&group->order);
125         BN_free(&group->cofactor);
126         }
127
128
129 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
130         {
131         BN_clear_free(&group->field);
132         BN_clear_free(&group->a);
133         BN_clear_free(&group->b);
134         if (group->generator != NULL)
135                 {
136                 EC_POINT_clear_free(group->generator);
137                 group->generator = NULL;
138                 }
139         BN_clear_free(&group->order);
140         BN_clear_free(&group->cofactor);
141         }
142
143
144 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
145         {
146         if (!BN_copy(&dest->field, &src->field)) return 0;
147         if (!BN_copy(&dest->a, &src->a)) return 0;
148         if (!BN_copy(&dest->b, &src->b)) return 0;
149
150         dest->a_is_minus3 = src->a_is_minus3;
151
152         if (src->generator != NULL)
153                 {
154                 if (dest->generator == NULL)
155                         {
156                         dest->generator = EC_POINT_new(dest);
157                         if (dest->generator == NULL) return 0;
158                         }
159                 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
160                 }
161         else
162                 {
163                 /* src->generator == NULL */
164                 if (dest->generator != NULL)
165                         {
166                         EC_POINT_clear_free(dest->generator);
167                         dest->generator = NULL;
168                         }
169                 }
170
171         if (!BN_copy(&dest->order, &src->order)) return 0;
172         if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
173
174         return 1;
175         }
176
177
178 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
179         const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
180         {
181         int ret = 0;
182         BN_CTX *new_ctx = NULL;
183         BIGNUM *tmp_a;
184         
185         /* p must be a prime > 3 */
186         if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
187                 {
188                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP, EC_R_INVALID_FIELD);
189                 return 0;
190                 }
191
192         if (ctx == NULL)
193                 {
194                 ctx = new_ctx = BN_CTX_new();
195                 if (ctx == NULL)
196                         return 0;
197                 }
198
199         BN_CTX_start(ctx);
200         tmp_a = BN_CTX_get(ctx);
201         if (tmp_a == NULL) goto err;
202
203         /* group->field */
204         if (!BN_copy(&group->field, p)) goto err;
205         group->field.neg = 0;
206
207         /* group->a */
208         if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
209         if (group->meth->field_encode)
210                 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }     
211         else
212                 if (!BN_copy(&group->a, tmp_a)) goto err;
213         
214         /* group->b */
215         if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
216         if (group->meth->field_encode)
217                 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
218         
219         /* group->a_is_minus3 */
220         if (!BN_add_word(tmp_a, 3)) goto err;
221         group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
222
223         ret = 1;
224
225  err:
226         BN_CTX_end(ctx);
227         if (new_ctx != NULL)
228                 BN_CTX_free(new_ctx);
229         return ret;
230         }
231
232
233 int ec_GFp_simple_group_get_curve_GFp(EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
234         {
235         int ret = 0;
236         BN_CTX *new_ctx = NULL;
237         
238         if (p != NULL)
239                 {
240                 if (!BN_copy(p, &group->field)) return 0;
241                 }
242
243         if (a != NULL || b != NULL)
244                 {
245                 if (group->meth->field_decode)
246                         {
247                         if (ctx == NULL)
248                                 {
249                                 ctx = new_ctx = BN_CTX_new();
250                                 if (ctx == NULL)
251                                         return 0;
252                                 }
253                         if (a != NULL)
254                                 {
255                                 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
256                                 }
257                         if (b != NULL)
258                                 {
259                                 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
260                                 }
261                         }
262                 else
263                         {
264                         if (a != NULL)
265                                 {
266                                 if (!BN_copy(a, &group->a)) goto err;
267                                 }
268                         if (b != NULL)
269                                 {
270                                 if (!BN_copy(b, &group->b)) goto err;
271                                 }
272                         }
273                 }
274         
275         ret = 1;
276         
277  err:
278         if (new_ctx)
279                 BN_CTX_free(new_ctx);
280         return ret;
281         }
282
283
284
285 int ec_GFp_simple_group_set_generator(EC_GROUP *group, const EC_POINT *generator,
286         const BIGNUM *order, const BIGNUM *cofactor)
287         {
288         if (generator)
289                 {
290                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
291                 return 0   ;
292                 }
293
294         if (group->generator == NULL)
295                 {
296                 group->generator = EC_POINT_new(group);
297                 if (group->generator == NULL) return 0;
298                 }
299         if (!EC_POINT_copy(group->generator, generator)) return 0;
300
301         if (order != NULL)
302                 { if (!BN_copy(&group->order, order)) return 0; }       
303         else
304                 { if (!BN_zero(&group->order)) return 0; }      
305
306         if (cofactor != NULL)
307                 { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 
308         else
309                 { if (!BN_zero(&group->cofactor)) return 0; }   
310
311         return 1;
312         }
313
314
315 EC_POINT *ec_GFp_simple_group_get0_generator(EC_GROUP *group)
316         {
317         return group->generator;
318         }
319
320
321 int ec_GFp_simple_group_get_order(EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
322         {
323         if (!BN_copy(order, &group->order))
324                 return 0;
325
326         return !BN_is_zero(&group->order);
327         }
328
329
330 int ec_GFp_simple_group_get_cofactor(EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
331         {
332         if (!BN_copy(cofactor, &group->cofactor))
333                 return 0;
334
335         return !BN_is_zero(&group->cofactor);
336         }
337
338
339 int ec_GFp_simple_point_init(EC_POINT *point)
340         {
341         BN_init(&point->X);
342         BN_init(&point->Y);
343         BN_init(&point->Z);
344         point->Z_is_one = 0;
345
346         return 1;
347         }
348
349
350 void ec_GFp_simple_point_finish(EC_POINT *point)
351         {
352         BN_free(&point->X);
353         BN_free(&point->Y);
354         BN_free(&point->Z);
355         }
356
357
358 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
359         {
360         BN_clear_free(&point->X);
361         BN_clear_free(&point->Y);
362         BN_clear_free(&point->Z);
363         point->Z_is_one = 0;
364         }
365
366
367 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
368         {
369         if (!BN_copy(&dest->X, &src->X)) return 0;
370         if (!BN_copy(&dest->Y, &src->Y)) return 0;
371         if (!BN_copy(&dest->Z, &src->Z)) return 0;
372         dest->Z_is_one = src->Z_is_one;
373
374         return 1;
375         }
376
377
378 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
379         {
380         point->Z_is_one = 0;
381         return (BN_zero(&point->Z));
382         }
383
384
385 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
386         const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
387         {
388         BN_CTX *new_ctx = NULL;
389         int ret = 0;
390         
391         if (ctx == NULL)
392                 {
393                 ctx = new_ctx = BN_CTX_new();
394                 if (ctx == NULL)
395                         return 0;
396                 }
397
398         if (x != NULL)
399                 {
400                 if (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;
401                 if (group->meth->field_encode)
402                         {
403                         if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
404                         }
405                 }
406         
407         if (y != NULL)
408                 {
409                 if (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;
410                 if (group->meth->field_encode)
411                         {
412                         if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
413                         }
414                 }
415         
416         if (z != NULL)
417                 {
418                 int Z_is_one;
419
420                 if (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;
421                 Z_is_one = BN_is_one(&point->Z);
422                 if (group->meth->field_encode)
423                         {
424                         if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
425                         }
426                 point->Z_is_one = Z_is_one;
427                 }
428         
429         ret = 1;
430         
431  err:
432         if (new_ctx != NULL)
433                 BN_CTX_free(new_ctx);
434         return ret;
435         }
436
437
438 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
439         BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
440         {
441         BN_CTX *new_ctx = NULL;
442         int ret = 0;
443         
444         if (group->meth->field_decode != 0)
445                 {
446                 if (ctx == NULL)
447                         {
448                         ctx = new_ctx = BN_CTX_new();
449                         if (ctx == NULL)
450                                 return 0;
451                         }
452
453                 if (x != NULL)
454                         {
455                         if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
456                         }
457                 if (y != NULL)
458                         {
459                         if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
460                         }
461                 if (z != NULL)
462                         {
463                         if (!group->meth->field_decode(group, z, &point->Z, ctx)) goto err;
464                         }
465                 }
466         else    
467                 {
468                 if (x != NULL)
469                         {
470                         if (!BN_copy(x, &point->X)) goto err;
471                         }
472                 if (y != NULL)
473                         {
474                         if (!BN_copy(y, &point->Y)) goto err;
475                         }
476                 if (z != NULL)
477                         {
478                         if (!BN_copy(z, &point->Z)) goto err;
479                         }
480                 }
481         
482         ret = 1;
483
484  err:
485         if (new_ctx != NULL)
486                 BN_CTX_free(new_ctx);
487         return ret;
488         }
489
490
491 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
492         const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
493         {
494         if (x == NULL || y == NULL)
495                 {
496                 /* unlike for projective coordinates, we do not tolerate this */
497                 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_PASSED_NULL_PARAMETER);
498                 return 0;
499                 }
500
501         return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);
502         }
503
504
505 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
506         BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
507         {
508         BN_CTX *new_ctx = NULL;
509         BIGNUM *X, *Y, *Z, *Z_1, *Z_2, *Z_3;
510         const BIGNUM *X_, *Y_, *Z_;
511         int ret = 0;
512
513         if (EC_POINT_is_at_infinity(group, point))
514                 {
515                 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_POINT_AT_INFINITY);
516                 return 0;
517                 }
518
519         if (ctx == NULL)
520                 {
521                 ctx = new_ctx = BN_CTX_new();
522                 if (ctx == NULL)
523                         return 0;
524                 }
525
526         BN_CTX_start(ctx);
527         X = BN_CTX_get(ctx);
528         Y = BN_CTX_get(ctx);
529         Z = BN_CTX_get(ctx);
530         Z_1 = BN_CTX_get(ctx);
531         Z_2 = BN_CTX_get(ctx);
532         Z_3 = BN_CTX_get(ctx);
533         if (Z_3 == NULL) goto err;
534
535         /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */
536         
537         if (group->meth->field_decode)
538                 {
539                 if (!group->meth->field_decode(group, X, &point->X, ctx)) goto err;
540                 if (!group->meth->field_decode(group, Y, &point->Y, ctx)) goto err;
541                 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
542                 X_ = X; Y_ = Y; Z_ = Z;
543                 }
544         else
545                 {
546                 X_ = &point->X;
547                 Y_ = &point->Y;
548                 Z_ = &point->Z;
549                 }
550         
551         if (BN_is_one(Z_))
552                 {
553                 if (x != NULL)
554                         {
555                         if (!BN_copy(x, X_)) goto err;
556                         }
557                 if (y != NULL)
558                         {
559                         if (!BN_copy(y, Y_)) goto err;
560                         }
561                 }
562         else
563                 {
564                 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
565                         {
566                         ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_BN_LIB);
567                         goto err;
568                         }
569                 if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
570         
571                 if (x != NULL)
572                         {
573                         if (!BN_mod_mul(x, X_, Z_2, &group->field, ctx)) goto err;
574                         }
575
576                 if (y != NULL)
577                         {
578                         if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
579                         if (!BN_mod_mul(y, Y_, Z_3, &group->field, ctx)) goto err;
580                         }
581                 }
582
583         ret = 1;
584
585  err:
586         BN_CTX_end(ctx);
587         if (new_ctx != NULL)
588                 BN_CTX_free(new_ctx);
589         return ret;
590         }
591
592
593 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
594         const BIGNUM *x, int y_bit, BN_CTX *ctx)
595         {
596         BN_CTX *new_ctx = NULL;
597         BIGNUM *tmp1, *tmp2, *y;
598         int ret = 0;
599
600         if (ctx == NULL)
601                 {
602                 ctx = new_ctx = BN_CTX_new();
603                 if (ctx == NULL)
604                         return 0;
605                 }
606
607         y_bit = (y_bit != 0);
608
609         BN_CTX_start(ctx);
610         tmp1 = BN_CTX_get(ctx);
611         tmp2 = BN_CTX_get(ctx);
612         y = BN_CTX_get(ctx);
613         if (y == NULL) goto err;
614
615         /* Recover y.  We have a Weierstrass equation
616          *     y^2 = x^3 + a*x + b,
617          * so  y  is one of the square roots of  x^3 + a*x + b.
618          */
619
620         /* tmp1 := x^3 */
621         if (!BN_mod_sqr(tmp2, x, &group->field, ctx)) goto err;
622         if (!BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) goto err;
623         
624         /* tmp1 := tmp1 + a*x */
625         if (group->a_is_minus3)
626                 {
627                 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
628                 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
629                 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
630                 }
631         else
632                 {
633                 if (group->meth->field_decode)
634                         {
635                         if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
636                         if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
637                         }
638                 else
639                         {
640                         if (!BN_mod_mul(tmp2, &group->a, x, &group->field, ctx)) goto err;
641                         }
642                 
643                 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
644                 }
645         
646         /* tmp1 := tmp1 + b */
647         if (group->meth->field_decode)
648                 {
649                 if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
650                 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
651                 }
652         else
653                 {
654                 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
655                 }
656         
657         if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
658                 {
659                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_BN_LIB);
660                 goto err;
661                 }
662         /* If tmp1 is not a square (i.e. there is no point on the curve with
663          * our x), then y now is a nonsense value too */
664
665         if (y_bit != BN_is_odd(y))
666                 {
667                 if (BN_is_zero(y))
668                         {
669                         int kron;
670
671                         kron = BN_kronecker(x, &group->field, ctx);
672                         if (kron == -2) goto err;
673
674                         if (kron == 1)
675                                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSION_BIT);
676                         else
677                                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSED_POINT);
678                         goto err;
679                         }
680                 if (!BN_usub(y, &group->field, y)) goto err;
681                 }
682         if (y_bit != BN_is_odd(y))
683                 {
684                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_INTERNAL_ERROR);
685                 goto err;
686                 }
687
688         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
689
690         ret = 1;
691
692  err:
693         BN_CTX_end(ctx);
694         if (new_ctx != NULL)
695                 BN_CTX_free(new_ctx);
696         return ret;
697         }
698
699
700 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
701         unsigned char *buf, size_t len, BN_CTX *ctx)
702         {
703         size_t ret;
704         BN_CTX *new_ctx = NULL;
705         int used_ctx = 0;
706         BIGNUM *x, *y;
707         size_t field_len, i, skip;
708
709         if ((form != POINT_CONVERSION_COMPRESSED)
710                 && (form != POINT_CONVERSION_UNCOMPRESSED)
711                 && (form != POINT_CONVERSION_HYBRID))
712                 {
713                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
714                 goto err;
715                 }
716
717         if (EC_POINT_is_at_infinity(group, point))
718                 {
719                 /* encodes to a single 0 octet */
720                 if (buf != NULL)
721                         {
722                         if (len < 1)
723                                 {
724                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
725                                 return 0;
726                                 }
727                         buf[0] = 0;
728                         }
729                 return 1;
730                 }
731
732
733         /* ret := required output buffer length */
734         field_len = BN_num_bytes(&group->field);
735         ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
736
737         /* if 'buf' is NULL, just return required length */
738         if (buf != NULL)
739                 {
740                 if (len < ret)
741                         {
742                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
743                         goto err;
744                         }
745
746                 if (ctx == NULL)
747                         {
748                         ctx = new_ctx = BN_CTX_new();
749                         if (ctx == NULL)
750                                 return 0;
751                         }
752
753                 BN_CTX_start(ctx);
754                 used_ctx = 1;
755                 x = BN_CTX_get(ctx);
756                 y = BN_CTX_get(ctx);
757                 if (y == NULL) goto err;
758
759                 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
760
761                 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
762                         buf[0] = form + 1;
763                 else
764                         buf[0] = form;
765         
766                 i = 1;
767                 
768                 skip = field_len - BN_num_bytes(x);
769                 if (skip > field_len)
770                         {
771                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
772                         goto err;
773                         }
774                 while (skip > 0)
775                         {
776                         buf[i++] = 0;
777                         skip--;
778                         }
779                 skip = BN_bn2bin(x, buf + i);
780                 i += skip;
781                 if (i != 1 + field_len)
782                         {
783                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
784                         goto err;
785                         }
786
787                 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
788                         {
789                         skip = field_len - BN_num_bytes(y);
790                         if (skip > field_len)
791                                 {
792                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
793                                 goto err;
794                                 }
795                         while (skip > 0)
796                                 {
797                                 buf[i++] = 0;
798                                 skip--;
799                                 }
800                         skip = BN_bn2bin(y, buf + i);
801                         i += skip;
802                         }
803
804                 if (i != ret)
805                         {
806                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
807                         goto err;
808                         }
809                 }
810         
811         if (used_ctx)
812                 BN_CTX_end(ctx);
813         if (new_ctx != NULL)
814                 BN_CTX_free(new_ctx);
815         return ret;
816
817  err:
818         if (used_ctx)
819                 BN_CTX_end(ctx);
820         if (new_ctx != NULL)
821                 BN_CTX_free(new_ctx);
822         return 0;
823         }
824
825
826 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
827         const unsigned char *buf, size_t len, BN_CTX *ctx)
828         {
829         point_conversion_form_t form;
830         int y_bit;
831         BN_CTX *new_ctx = NULL;
832         BIGNUM *x, *y;
833         size_t field_len, enc_len;
834         int ret = 0;
835
836         if (len <= 0)
837                 {
838                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
839                 return 0;
840                 }
841         form = buf[0];
842         y_bit = form & 1;
843         form = form & ~1;
844         if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
845                 && (form != POINT_CONVERSION_UNCOMPRESSED)
846                 && (form != POINT_CONVERSION_HYBRID))
847                 {
848                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
849                 return 0;
850                 }
851         if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
852                 {
853                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
854                 return 0;
855                 }
856
857         if (form == 0)
858                 {
859                 if (len != 1)
860                         {
861                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
862                         return 0;
863                         }
864
865                 return EC_POINT_set_to_infinity(group, point);
866                 }
867         
868         field_len = BN_num_bytes(&group->field);
869         enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
870
871         if (len != enc_len)
872                 {
873                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
874                 return 0;
875                 }
876
877         if (ctx == NULL)
878                 {
879                 ctx = new_ctx = BN_CTX_new();
880                 if (ctx == NULL)
881                         return 0;
882                 }
883
884         BN_CTX_start(ctx);
885         x = BN_CTX_get(ctx);
886         y = BN_CTX_get(ctx);
887         if (y == NULL) goto err;
888
889         if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
890         if (BN_ucmp(x, &group->field) >= 0)
891                 {
892                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
893                 goto err;
894                 }
895
896         if (form == POINT_CONVERSION_COMPRESSED)
897                 {
898                 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
899                 }
900         else
901                 {
902                 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
903                 if (BN_ucmp(y, &group->field) >= 0)
904                         {
905                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
906                         goto err;
907                         }
908                 if (form == POINT_CONVERSION_HYBRID)
909                         {
910                         if (y_bit != BN_is_odd(y))
911                                 {
912                                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
913                                 goto err;
914                                 }
915                         }
916
917                 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
918                 }
919         
920         if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
921                 {
922                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
923                 goto err;
924                 }
925
926         ret = 1;
927         
928  err:
929         BN_CTX_end(ctx);
930         if (new_ctx != NULL)
931                 BN_CTX_free(new_ctx);
932         return ret;
933         }
934
935
936 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
937         {
938         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
939         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
940         const BIGNUM *p;
941         BN_CTX *new_ctx = NULL;
942         BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
943         int ret = 0;
944         
945         if (a == b)
946                 return EC_POINT_dbl(group, r, a, ctx);
947         if (EC_POINT_is_at_infinity(group, a))
948                 return EC_POINT_copy(r, b);
949         if (EC_POINT_is_at_infinity(group, b))
950                 return EC_POINT_copy(r, a);
951         
952         field_mul = group->meth->field_mul;
953         field_sqr = group->meth->field_sqr;
954         p = &group->field;
955
956         if (ctx == NULL)
957                 {
958                 ctx = new_ctx = BN_CTX_new();
959                 if (ctx == NULL)
960                         return 0;
961                 }
962
963         BN_CTX_start(ctx);
964         n0 = BN_CTX_get(ctx);
965         n1 = BN_CTX_get(ctx);
966         n2 = BN_CTX_get(ctx);
967         n3 = BN_CTX_get(ctx);
968         n4 = BN_CTX_get(ctx);
969         n5 = BN_CTX_get(ctx);
970         n6 = BN_CTX_get(ctx);
971         if (n6 == NULL) goto end;
972
973         /* Note that in this function we must not read components of 'a' or 'b'
974          * once we have written the corresponding components of 'r'.
975          * ('r' might be one of 'a' or 'b'.)
976          */
977
978         /* n1, n2 */
979         if (b->Z_is_one)
980                 {
981                 if (!BN_copy(n1, &a->X)) goto end;
982                 if (!BN_copy(n2, &a->Y)) goto end;
983                 /* n1 = X_a */
984                 /* n2 = Y_a */
985                 }
986         else
987                 {
988                 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
989                 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
990                 /* n1 = X_a * Z_b^2 */
991
992                 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
993                 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
994                 /* n2 = Y_a * Z_b^3 */
995                 }
996
997         /* n3, n4 */
998         if (a->Z_is_one)
999                 {
1000                 if (!BN_copy(n3, &b->X)) goto end;
1001                 if (!BN_copy(n4, &b->Y)) goto end;
1002                 /* n3 = X_b */
1003                 /* n4 = Y_b */
1004                 }
1005         else
1006                 {
1007                 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
1008                 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
1009                 /* n3 = X_b * Z_a^2 */
1010
1011                 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
1012                 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
1013                 /* n4 = Y_b * Z_a^3 */
1014                 }
1015
1016         /* n5, n6 */
1017         if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
1018         if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
1019         /* n5 = n1 - n3 */
1020         /* n6 = n2 - n4 */
1021
1022         if (BN_is_zero(n5))
1023                 {
1024                 if (BN_is_zero(n6))
1025                         {
1026                         /* a is the same point as b */
1027                         BN_CTX_end(ctx);
1028                         ret = EC_POINT_dbl(group, r, a, ctx);
1029                         ctx = NULL;
1030                         goto end;
1031                         }
1032                 else
1033                         {
1034                         /* a is the inverse of b */
1035                         if (!BN_zero(&r->Z)) goto end;
1036                         r->Z_is_one = 0;
1037                         ret = 1;
1038                         goto end;
1039                         }
1040                 }
1041
1042         /* 'n7', 'n8' */
1043         if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
1044         if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
1045         /* 'n7' = n1 + n3 */
1046         /* 'n8' = n2 + n4 */
1047
1048         /* Z_r */
1049         if (a->Z_is_one && b->Z_is_one)
1050                 {
1051                 if (!BN_copy(&r->Z, n5)) goto end;
1052                 }
1053         else
1054                 {
1055                 if (a->Z_is_one)
1056                         { if (!BN_copy(n0, &b->Z)) goto end; }
1057                 else if (b->Z_is_one)
1058                         { if (!BN_copy(n0, &a->Z)) goto end; }
1059                 else
1060                         { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
1061                 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
1062                 }
1063         r->Z_is_one = 0;
1064         /* Z_r = Z_a * Z_b * n5 */
1065
1066         /* X_r */
1067         if (!field_sqr(group, n0, n6, ctx)) goto end;
1068         if (!field_sqr(group, n4, n5, ctx)) goto end;
1069         if (!field_mul(group, n3, n1, n4, ctx)) goto end;
1070         if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
1071         /* X_r = n6^2 - n5^2 * 'n7' */
1072         
1073         /* 'n9' */
1074         if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
1075         if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
1076         /* n9 = n5^2 * 'n7' - 2 * X_r */
1077
1078         /* Y_r */
1079         if (!field_mul(group, n0, n0, n6, ctx)) goto end;
1080         if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
1081         if (!field_mul(group, n1, n2, n5, ctx)) goto end;
1082         if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
1083         if (BN_is_odd(n0))
1084                 if (!BN_add(n0, n0, p)) goto end;
1085         /* now  0 <= n0 < 2*p,  and n0 is even */
1086         if (!BN_rshift1(&r->Y, n0)) goto end;
1087         /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
1088
1089         ret = 1;
1090
1091  end:
1092         if (ctx) /* otherwise we already called BN_CTX_end */
1093                 BN_CTX_end(ctx);
1094         if (new_ctx != NULL)
1095                 BN_CTX_free(new_ctx);
1096         return ret;
1097         }
1098
1099
1100 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
1101         {
1102         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1103         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1104         const BIGNUM *p;
1105         BN_CTX *new_ctx = NULL;
1106         BIGNUM *n0, *n1, *n2, *n3;
1107         int ret = 0;
1108         
1109         if (EC_POINT_is_at_infinity(group, a))
1110                 {
1111                 if (!BN_zero(&r->Z)) return 0;
1112                 r->Z_is_one = 0;
1113                 return 1;
1114                 }
1115
1116         field_mul = group->meth->field_mul;
1117         field_sqr = group->meth->field_sqr;
1118         p = &group->field;
1119
1120         if (ctx == NULL)
1121                 {
1122                 ctx = new_ctx = BN_CTX_new();
1123                 if (ctx == NULL)
1124                         return 0;
1125                 }
1126
1127         BN_CTX_start(ctx);
1128         n0 = BN_CTX_get(ctx);
1129         n1 = BN_CTX_get(ctx);
1130         n2 = BN_CTX_get(ctx);
1131         n3 = BN_CTX_get(ctx);
1132         if (n3 == NULL) goto err;
1133
1134         /* Note that in this function we must not read components of 'a'
1135          * once we have written the corresponding components of 'r'.
1136          * ('r' might the same as 'a'.)
1137          */
1138
1139         /* n1 */
1140         if (a->Z_is_one)
1141                 {
1142                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1143                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1144                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1145                 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
1146                 /* n1 = 3 * X_a^2 + a_curve */
1147                 }
1148         else if (group->a_is_minus3)
1149                 {
1150                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1151                 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
1152                 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
1153                 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
1154                 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
1155                 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
1156                 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
1157                  *    = 3 * X_a^2 - 3 * Z_a^4 */
1158                 }
1159         else
1160                 {
1161                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1162                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1163                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1164                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1165                 if (!field_sqr(group, n1, n1, ctx)) goto err;
1166                 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
1167                 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
1168                 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
1169                 }
1170
1171         /* Z_r */
1172         if (a->Z_is_one)
1173                 {
1174                 if (!BN_copy(n0, &a->Y)) goto err;
1175                 }
1176         else
1177                 {
1178                 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
1179                 }
1180         if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
1181         r->Z_is_one = 0;
1182         /* Z_r = 2 * Y_a * Z_a */
1183
1184         /* n2 */
1185         if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
1186         if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
1187         if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
1188         /* n2 = 4 * X_a * Y_a^2 */
1189
1190         /* X_r */
1191         if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
1192         if (!field_sqr(group, &r->X, n1, ctx)) goto err;
1193         if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
1194         /* X_r = n1^2 - 2 * n2 */
1195         
1196         /* n3 */
1197         if (!field_sqr(group, n0, n3, ctx)) goto err;
1198         if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
1199         /* n3 = 8 * Y_a^4 */
1200         
1201         /* Y_r */
1202         if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
1203         if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1204         if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1205         /* Y_r = n1 * (n2 - X_r) - n3 */
1206
1207         ret = 1;
1208
1209  err:
1210         BN_CTX_end(ctx);
1211         if (new_ctx != NULL)
1212                 BN_CTX_free(new_ctx);
1213         return ret;
1214         }
1215
1216
1217 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1218         {
1219         if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
1220                 /* point is its own inverse */
1221                 return 1;
1222         
1223         return BN_usub(&point->Y, &group->field, &point->Y);
1224         }
1225
1226
1227 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1228         {
1229         return BN_is_zero(&point->Z);
1230         }
1231
1232
1233 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1234         {
1235         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1236         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1237         const BIGNUM *p;
1238         BN_CTX *new_ctx = NULL;
1239         BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
1240         int ret = -1;
1241
1242         if (EC_POINT_is_at_infinity(group, point))
1243                 return 1;
1244         
1245         field_mul = group->meth->field_mul;
1246         field_sqr = group->meth->field_sqr;
1247         p = &group->field;
1248
1249         if (ctx == NULL)
1250                 {
1251                 ctx = new_ctx = BN_CTX_new();
1252                 if (ctx == NULL)
1253                         return -1;
1254                 }
1255
1256         BN_CTX_start(ctx);
1257         rh = BN_CTX_get(ctx);
1258         tmp1 = BN_CTX_get(ctx);
1259         tmp2 = BN_CTX_get(ctx);
1260         Z4 = BN_CTX_get(ctx);
1261         Z6 = BN_CTX_get(ctx);
1262         if (Z6 == NULL) goto err;
1263
1264         /* We have a curve defined by a Weierstrass equation
1265          *      y^2 = x^3 + a*x + b.
1266          * The point to consider is given in Jacobian projective coordinates
1267          * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
1268          * Substituting this and multiplying by  Z^6  transforms the above equation into
1269          *      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1270          * To test this, we add up the right-hand side in 'rh'.
1271          */
1272
1273         /* rh := X^3 */
1274         if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1275         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1276
1277         if (!point->Z_is_one)
1278                 {
1279                 if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
1280                 if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
1281                 if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
1282
1283                 /* rh := rh + a*X*Z^4 */
1284                 if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
1285                 if (group->a_is_minus3)
1286                         {
1287                         if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
1288                         if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
1289                         if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1290                         }
1291                 else
1292                         {
1293                         if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
1294                         if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1295                         }
1296
1297                 /* rh := rh + b*Z^6 */
1298                 if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
1299                 if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
1300                 }
1301         else
1302                 {
1303                 /* point->Z_is_one */
1304
1305                 /* rh := rh + a*X */
1306                 if (group->a_is_minus3)
1307                         {
1308                         if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
1309                         if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
1310                         if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1311                         }
1312                 else
1313                         {
1314                         if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
1315                         if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1316                         }
1317
1318                 /* rh := rh + b */
1319                 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1320                 }
1321
1322         /* 'lh' := Y^2 */
1323         if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
1324
1325         ret = (0 == BN_cmp(tmp1, rh));
1326
1327  err:
1328         BN_CTX_end(ctx);
1329         if (new_ctx != NULL)
1330                 BN_CTX_free(new_ctx);
1331         return ret;
1332         }
1333
1334
1335 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1336         {
1337         /* return values:
1338          *  -1   error
1339          *   0   equal (in affine coordinates)
1340          *   1   not equal
1341          */
1342
1343         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1344         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1345         BN_CTX *new_ctx = NULL;
1346         BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1347         const BIGNUM *tmp1_, *tmp2_;
1348         int ret = -1;
1349         
1350         if (EC_POINT_is_at_infinity(group, a))
1351                 {
1352                 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1353                 }
1354         
1355         if (a->Z_is_one && b->Z_is_one)
1356                 {
1357                 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
1358                 }
1359
1360         field_mul = group->meth->field_mul;
1361         field_sqr = group->meth->field_sqr;
1362
1363         if (ctx == NULL)
1364                 {
1365                 ctx = new_ctx = BN_CTX_new();
1366                 if (ctx == NULL)
1367                         return -1;
1368                 }
1369
1370         BN_CTX_start(ctx);
1371         tmp1 = BN_CTX_get(ctx);
1372         tmp2 = BN_CTX_get(ctx);
1373         Za23 = BN_CTX_get(ctx);
1374         Zb23 = BN_CTX_get(ctx);
1375         if (Zb23 == NULL) goto end;
1376
1377         /* We have to decide whether
1378          *     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1379          * or equivalently, whether
1380          *     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1381          */
1382
1383         if (!b->Z_is_one)
1384                 {
1385                 if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
1386                 if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
1387                 tmp1_ = tmp1;
1388                 }
1389         else
1390                 tmp1_ = &a->X;
1391         if (!a->Z_is_one)
1392                 {
1393                 if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
1394                 if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
1395                 tmp2_ = tmp2;
1396                 }
1397         else
1398                 tmp2_ = &b->X;
1399         
1400         /* compare  X_a*Z_b^2  with  X_b*Z_a^2 */
1401         if (BN_cmp(tmp1_, tmp2_) != 0)
1402                 {
1403                 ret = 1; /* points differ */
1404                 goto end;
1405                 }
1406
1407
1408         if (!b->Z_is_one)
1409                 {
1410                 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
1411                 if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
1412                 /* tmp1_ = tmp1 */
1413                 }
1414         else
1415                 tmp1_ = &a->Y;
1416         if (!a->Z_is_one)
1417                 {
1418                 if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
1419                 if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
1420                 /* tmp2_ = tmp2 */
1421                 }
1422         else
1423                 tmp2_ = &b->Y;
1424
1425         /* compare  Y_a*Z_b^3  with  Y_b*Z_a^3 */
1426         if (BN_cmp(tmp1_, tmp2_) != 0)
1427                 {
1428                 ret = 1; /* points differ */
1429                 goto end;
1430                 }
1431
1432         /* points are equal */
1433         ret = 0;
1434
1435  end:
1436         BN_CTX_end(ctx);
1437         if (new_ctx != NULL)
1438                 BN_CTX_free(new_ctx);
1439         return ret;
1440         }
1441
1442
1443 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1444         {
1445         BN_CTX *new_ctx = NULL;
1446         BIGNUM *x, *y;
1447         int ret = 0;
1448
1449         if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1450                 return 1;
1451
1452         if (ctx == NULL)
1453                 {
1454                 ctx = new_ctx = BN_CTX_new();
1455                 if (ctx == NULL)
1456                         return 0;
1457                 }
1458
1459         BN_CTX_start(ctx);
1460         x = BN_CTX_get(ctx);
1461         y = BN_CTX_get(ctx);
1462         if (y == NULL) goto err;
1463
1464         if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1465         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1466         if (!point->Z_is_one)
1467                 {
1468                 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1469                 goto err;
1470                 }
1471         
1472         ret = 1;
1473
1474  err:
1475         BN_CTX_end(ctx);
1476         if (new_ctx != NULL)
1477                 BN_CTX_free(new_ctx);
1478         return ret;
1479         }
1480
1481
1482 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1483         {
1484         return BN_mod_mul(r, a, b, &group->field, ctx);
1485         }
1486
1487
1488 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1489         {
1490         return BN_mod_sqr(r, a, &group->field, ctx);
1491         }