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