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