Additional comment changes for reformat of 1.0.0
[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         /*-
667          * Recover y.  We have a Weierstrass equation
668          *     y^2 = x^3 + a*x + b,
669          * so  y  is one of the square roots of  x^3 + a*x + b.
670          */
671
672         /* tmp1 := x^3 */
673         if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
674         if (group->meth->field_decode == 0)
675                 {
676                 /* field_{sqr,mul} work on standard representation */
677                 if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
678                 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
679                 }
680         else
681                 {
682                 if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
683                 if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
684                 }
685         
686         /* tmp1 := tmp1 + a*x */
687         if (group->a_is_minus3)
688                 {
689                 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
690                 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
691                 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
692                 }
693         else
694                 {
695                 if (group->meth->field_decode)
696                         {
697                         if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
698                         if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
699                         }
700                 else
701                         {
702                         /* field_mul works on standard representation */
703                         if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
704                         }
705                 
706                 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
707                 }
708         
709         /* tmp1 := tmp1 + b */
710         if (group->meth->field_decode)
711                 {
712                 if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
713                 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
714                 }
715         else
716                 {
717                 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
718                 }
719         
720         if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
721                 {
722                 unsigned long err = ERR_peek_last_error();
723                 
724                 if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
725                         {
726                         ERR_clear_error();
727                         ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
728                         }
729                 else
730                         ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
731                 goto err;
732                 }
733
734         if (y_bit != BN_is_odd(y))
735                 {
736                 if (BN_is_zero(y))
737                         {
738                         int kron;
739
740                         kron = BN_kronecker(x, &group->field, ctx);
741                         if (kron == -2) goto err;
742
743                         if (kron == 1)
744                                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
745                         else
746                                 /* BN_mod_sqrt() should have cought this error (not a square) */
747                                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
748                         goto err;
749                         }
750                 if (!BN_usub(y, &group->field, y)) goto err;
751                 }
752         if (y_bit != BN_is_odd(y))
753                 {
754                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
755                 goto err;
756                 }
757
758         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
759
760         ret = 1;
761
762  err:
763         BN_CTX_end(ctx);
764         if (new_ctx != NULL)
765                 BN_CTX_free(new_ctx);
766         return ret;
767         }
768
769
770 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
771         unsigned char *buf, size_t len, BN_CTX *ctx)
772         {
773         size_t ret;
774         BN_CTX *new_ctx = NULL;
775         int used_ctx = 0;
776         BIGNUM *x, *y;
777         size_t field_len, i, skip;
778
779         if ((form != POINT_CONVERSION_COMPRESSED)
780                 && (form != POINT_CONVERSION_UNCOMPRESSED)
781                 && (form != POINT_CONVERSION_HYBRID))
782                 {
783                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
784                 goto err;
785                 }
786
787         if (EC_POINT_is_at_infinity(group, point))
788                 {
789                 /* encodes to a single 0 octet */
790                 if (buf != NULL)
791                         {
792                         if (len < 1)
793                                 {
794                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
795                                 return 0;
796                                 }
797                         buf[0] = 0;
798                         }
799                 return 1;
800                 }
801
802
803         /* ret := required output buffer length */
804         field_len = BN_num_bytes(&group->field);
805         ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
806
807         /* if 'buf' is NULL, just return required length */
808         if (buf != NULL)
809                 {
810                 if (len < ret)
811                         {
812                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
813                         goto err;
814                         }
815
816                 if (ctx == NULL)
817                         {
818                         ctx = new_ctx = BN_CTX_new();
819                         if (ctx == NULL)
820                                 return 0;
821                         }
822
823                 BN_CTX_start(ctx);
824                 used_ctx = 1;
825                 x = BN_CTX_get(ctx);
826                 y = BN_CTX_get(ctx);
827                 if (y == NULL) goto err;
828
829                 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
830
831                 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
832                         buf[0] = form + 1;
833                 else
834                         buf[0] = form;
835         
836                 i = 1;
837                 
838                 skip = field_len - BN_num_bytes(x);
839                 if (skip > field_len)
840                         {
841                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
842                         goto err;
843                         }
844                 while (skip > 0)
845                         {
846                         buf[i++] = 0;
847                         skip--;
848                         }
849                 skip = BN_bn2bin(x, buf + i);
850                 i += skip;
851                 if (i != 1 + field_len)
852                         {
853                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
854                         goto err;
855                         }
856
857                 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
858                         {
859                         skip = field_len - BN_num_bytes(y);
860                         if (skip > field_len)
861                                 {
862                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
863                                 goto err;
864                                 }
865                         while (skip > 0)
866                                 {
867                                 buf[i++] = 0;
868                                 skip--;
869                                 }
870                         skip = BN_bn2bin(y, buf + i);
871                         i += skip;
872                         }
873
874                 if (i != ret)
875                         {
876                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
877                         goto err;
878                         }
879                 }
880         
881         if (used_ctx)
882                 BN_CTX_end(ctx);
883         if (new_ctx != NULL)
884                 BN_CTX_free(new_ctx);
885         return ret;
886
887  err:
888         if (used_ctx)
889                 BN_CTX_end(ctx);
890         if (new_ctx != NULL)
891                 BN_CTX_free(new_ctx);
892         return 0;
893         }
894
895
896 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
897         const unsigned char *buf, size_t len, BN_CTX *ctx)
898         {
899         point_conversion_form_t form;
900         int y_bit;
901         BN_CTX *new_ctx = NULL;
902         BIGNUM *x, *y;
903         size_t field_len, enc_len;
904         int ret = 0;
905
906         if (len == 0)
907                 {
908                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
909                 return 0;
910                 }
911         form = buf[0];
912         y_bit = form & 1;
913         form = form & ~1U;
914         if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
915                 && (form != POINT_CONVERSION_UNCOMPRESSED)
916                 && (form != POINT_CONVERSION_HYBRID))
917                 {
918                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
919                 return 0;
920                 }
921         if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
922                 {
923                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
924                 return 0;
925                 }
926
927         if (form == 0)
928                 {
929                 if (len != 1)
930                         {
931                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
932                         return 0;
933                         }
934
935                 return EC_POINT_set_to_infinity(group, point);
936                 }
937         
938         field_len = BN_num_bytes(&group->field);
939         enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
940
941         if (len != enc_len)
942                 {
943                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
944                 return 0;
945                 }
946
947         if (ctx == NULL)
948                 {
949                 ctx = new_ctx = BN_CTX_new();
950                 if (ctx == NULL)
951                         return 0;
952                 }
953
954         BN_CTX_start(ctx);
955         x = BN_CTX_get(ctx);
956         y = BN_CTX_get(ctx);
957         if (y == NULL) goto err;
958
959         if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
960         if (BN_ucmp(x, &group->field) >= 0)
961                 {
962                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
963                 goto err;
964                 }
965
966         if (form == POINT_CONVERSION_COMPRESSED)
967                 {
968                 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
969                 }
970         else
971                 {
972                 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
973                 if (BN_ucmp(y, &group->field) >= 0)
974                         {
975                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
976                         goto err;
977                         }
978                 if (form == POINT_CONVERSION_HYBRID)
979                         {
980                         if (y_bit != BN_is_odd(y))
981                                 {
982                                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
983                                 goto err;
984                                 }
985                         }
986
987                 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
988                 }
989         
990         if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
991                 {
992                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
993                 goto err;
994                 }
995
996         ret = 1;
997         
998  err:
999         BN_CTX_end(ctx);
1000         if (new_ctx != NULL)
1001                 BN_CTX_free(new_ctx);
1002         return ret;
1003         }
1004
1005
1006 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1007         {
1008         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1009         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1010         const BIGNUM *p;
1011         BN_CTX *new_ctx = NULL;
1012         BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
1013         int ret = 0;
1014         
1015         if (a == b)
1016                 return EC_POINT_dbl(group, r, a, ctx);
1017         if (EC_POINT_is_at_infinity(group, a))
1018                 return EC_POINT_copy(r, b);
1019         if (EC_POINT_is_at_infinity(group, b))
1020                 return EC_POINT_copy(r, a);
1021         
1022         field_mul = group->meth->field_mul;
1023         field_sqr = group->meth->field_sqr;
1024         p = &group->field;
1025
1026         if (ctx == NULL)
1027                 {
1028                 ctx = new_ctx = BN_CTX_new();
1029                 if (ctx == NULL)
1030                         return 0;
1031                 }
1032
1033         BN_CTX_start(ctx);
1034         n0 = BN_CTX_get(ctx);
1035         n1 = BN_CTX_get(ctx);
1036         n2 = BN_CTX_get(ctx);
1037         n3 = BN_CTX_get(ctx);
1038         n4 = BN_CTX_get(ctx);
1039         n5 = BN_CTX_get(ctx);
1040         n6 = BN_CTX_get(ctx);
1041         if (n6 == NULL) goto end;
1042
1043         /* Note that in this function we must not read components of 'a' or 'b'
1044          * once we have written the corresponding components of 'r'.
1045          * ('r' might be one of 'a' or 'b'.)
1046          */
1047
1048         /* n1, n2 */
1049         if (b->Z_is_one)
1050                 {
1051                 if (!BN_copy(n1, &a->X)) goto end;
1052                 if (!BN_copy(n2, &a->Y)) goto end;
1053                 /* n1 = X_a */
1054                 /* n2 = Y_a */
1055                 }
1056         else
1057                 {
1058                 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
1059                 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
1060                 /* n1 = X_a * Z_b^2 */
1061
1062                 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
1063                 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
1064                 /* n2 = Y_a * Z_b^3 */
1065                 }
1066
1067         /* n3, n4 */
1068         if (a->Z_is_one)
1069                 {
1070                 if (!BN_copy(n3, &b->X)) goto end;
1071                 if (!BN_copy(n4, &b->Y)) goto end;
1072                 /* n3 = X_b */
1073                 /* n4 = Y_b */
1074                 }
1075         else
1076                 {
1077                 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
1078                 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
1079                 /* n3 = X_b * Z_a^2 */
1080
1081                 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
1082                 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
1083                 /* n4 = Y_b * Z_a^3 */
1084                 }
1085
1086         /* n5, n6 */
1087         if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
1088         if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
1089         /* n5 = n1 - n3 */
1090         /* n6 = n2 - n4 */
1091
1092         if (BN_is_zero(n5))
1093                 {
1094                 if (BN_is_zero(n6))
1095                         {
1096                         /* a is the same point as b */
1097                         BN_CTX_end(ctx);
1098                         ret = EC_POINT_dbl(group, r, a, ctx);
1099                         ctx = NULL;
1100                         goto end;
1101                         }
1102                 else
1103                         {
1104                         /* a is the inverse of b */
1105                         BN_zero(&r->Z);
1106                         r->Z_is_one = 0;
1107                         ret = 1;
1108                         goto end;
1109                         }
1110                 }
1111
1112         /* 'n7', 'n8' */
1113         if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
1114         if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
1115         /* 'n7' = n1 + n3 */
1116         /* 'n8' = n2 + n4 */
1117
1118         /* Z_r */
1119         if (a->Z_is_one && b->Z_is_one)
1120                 {
1121                 if (!BN_copy(&r->Z, n5)) goto end;
1122                 }
1123         else
1124                 {
1125                 if (a->Z_is_one)
1126                         { if (!BN_copy(n0, &b->Z)) goto end; }
1127                 else if (b->Z_is_one)
1128                         { if (!BN_copy(n0, &a->Z)) goto end; }
1129                 else
1130                         { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
1131                 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
1132                 }
1133         r->Z_is_one = 0;
1134         /* Z_r = Z_a * Z_b * n5 */
1135
1136         /* X_r */
1137         if (!field_sqr(group, n0, n6, ctx)) goto end;
1138         if (!field_sqr(group, n4, n5, ctx)) goto end;
1139         if (!field_mul(group, n3, n1, n4, ctx)) goto end;
1140         if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
1141         /* X_r = n6^2 - n5^2 * 'n7' */
1142         
1143         /* 'n9' */
1144         if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
1145         if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
1146         /* n9 = n5^2 * 'n7' - 2 * X_r */
1147
1148         /* Y_r */
1149         if (!field_mul(group, n0, n0, n6, ctx)) goto end;
1150         if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
1151         if (!field_mul(group, n1, n2, n5, ctx)) goto end;
1152         if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
1153         if (BN_is_odd(n0))
1154                 if (!BN_add(n0, n0, p)) goto end;
1155         /* now  0 <= n0 < 2*p,  and n0 is even */
1156         if (!BN_rshift1(&r->Y, n0)) goto end;
1157         /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
1158
1159         ret = 1;
1160
1161  end:
1162         if (ctx) /* otherwise we already called BN_CTX_end */
1163                 BN_CTX_end(ctx);
1164         if (new_ctx != NULL)
1165                 BN_CTX_free(new_ctx);
1166         return ret;
1167         }
1168
1169
1170 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
1171         {
1172         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1173         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1174         const BIGNUM *p;
1175         BN_CTX *new_ctx = NULL;
1176         BIGNUM *n0, *n1, *n2, *n3;
1177         int ret = 0;
1178         
1179         if (EC_POINT_is_at_infinity(group, a))
1180                 {
1181                 BN_zero(&r->Z);
1182                 r->Z_is_one = 0;
1183                 return 1;
1184                 }
1185
1186         field_mul = group->meth->field_mul;
1187         field_sqr = group->meth->field_sqr;
1188         p = &group->field;
1189
1190         if (ctx == NULL)
1191                 {
1192                 ctx = new_ctx = BN_CTX_new();
1193                 if (ctx == NULL)
1194                         return 0;
1195                 }
1196
1197         BN_CTX_start(ctx);
1198         n0 = BN_CTX_get(ctx);
1199         n1 = BN_CTX_get(ctx);
1200         n2 = BN_CTX_get(ctx);
1201         n3 = BN_CTX_get(ctx);
1202         if (n3 == NULL) goto err;
1203
1204         /* Note that in this function we must not read components of 'a'
1205          * once we have written the corresponding components of 'r'.
1206          * ('r' might the same as 'a'.)
1207          */
1208
1209         /* n1 */
1210         if (a->Z_is_one)
1211                 {
1212                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1213                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1214                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1215                 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
1216                 /* n1 = 3 * X_a^2 + a_curve */
1217                 }
1218         else if (group->a_is_minus3)
1219                 {
1220                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1221                 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
1222                 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
1223                 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
1224                 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
1225                 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
1226                 /*-
1227                  * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
1228                  *    = 3 * X_a^2 - 3 * Z_a^4
1229                  */
1230                 }
1231         else
1232                 {
1233                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1234                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1235                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1236                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1237                 if (!field_sqr(group, n1, n1, ctx)) goto err;
1238                 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
1239                 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
1240                 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
1241                 }
1242
1243         /* Z_r */
1244         if (a->Z_is_one)
1245                 {
1246                 if (!BN_copy(n0, &a->Y)) goto err;
1247                 }
1248         else
1249                 {
1250                 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
1251                 }
1252         if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
1253         r->Z_is_one = 0;
1254         /* Z_r = 2 * Y_a * Z_a */
1255
1256         /* n2 */
1257         if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
1258         if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
1259         if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
1260         /* n2 = 4 * X_a * Y_a^2 */
1261
1262         /* X_r */
1263         if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
1264         if (!field_sqr(group, &r->X, n1, ctx)) goto err;
1265         if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
1266         /* X_r = n1^2 - 2 * n2 */
1267         
1268         /* n3 */
1269         if (!field_sqr(group, n0, n3, ctx)) goto err;
1270         if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
1271         /* n3 = 8 * Y_a^4 */
1272         
1273         /* Y_r */
1274         if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
1275         if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1276         if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1277         /* Y_r = n1 * (n2 - X_r) - n3 */
1278
1279         ret = 1;
1280
1281  err:
1282         BN_CTX_end(ctx);
1283         if (new_ctx != NULL)
1284                 BN_CTX_free(new_ctx);
1285         return ret;
1286         }
1287
1288
1289 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1290         {
1291         if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
1292                 /* point is its own inverse */
1293                 return 1;
1294         
1295         return BN_usub(&point->Y, &group->field, &point->Y);
1296         }
1297
1298
1299 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1300         {
1301         return BN_is_zero(&point->Z);
1302         }
1303
1304
1305 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1306         {
1307         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1308         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1309         const BIGNUM *p;
1310         BN_CTX *new_ctx = NULL;
1311         BIGNUM *rh, *tmp, *Z4, *Z6;
1312         int ret = -1;
1313
1314         if (EC_POINT_is_at_infinity(group, point))
1315                 return 1;
1316         
1317         field_mul = group->meth->field_mul;
1318         field_sqr = group->meth->field_sqr;
1319         p = &group->field;
1320
1321         if (ctx == NULL)
1322                 {
1323                 ctx = new_ctx = BN_CTX_new();
1324                 if (ctx == NULL)
1325                         return -1;
1326                 }
1327
1328         BN_CTX_start(ctx);
1329         rh = BN_CTX_get(ctx);
1330         tmp = BN_CTX_get(ctx);
1331         Z4 = BN_CTX_get(ctx);
1332         Z6 = BN_CTX_get(ctx);
1333         if (Z6 == NULL) goto err;
1334
1335         /*-
1336          * We have a curve defined by a Weierstrass equation
1337          *      y^2 = x^3 + a*x + b.
1338          * The point to consider is given in Jacobian projective coordinates
1339          * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
1340          * Substituting this and multiplying by  Z^6  transforms the above equation into
1341          *      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1342          * To test this, we add up the right-hand side in 'rh'.
1343          */
1344
1345         /* rh := X^2 */
1346         if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1347
1348         if (!point->Z_is_one)
1349                 {
1350                 if (!field_sqr(group, tmp, &point->Z, ctx)) goto err;
1351                 if (!field_sqr(group, Z4, tmp, ctx)) goto err;
1352                 if (!field_mul(group, Z6, Z4, tmp, ctx)) goto err;
1353
1354                 /* rh := (rh + a*Z^4)*X */
1355                 if (group->a_is_minus3)
1356                         {
1357                         if (!BN_mod_lshift1_quick(tmp, Z4, p)) goto err;
1358                         if (!BN_mod_add_quick(tmp, tmp, Z4, p)) goto err;
1359                         if (!BN_mod_sub_quick(rh, rh, tmp, p)) goto err;
1360                         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1361                         }
1362                 else
1363                         {
1364                         if (!field_mul(group, tmp, Z4, &group->a, ctx)) goto err;
1365                         if (!BN_mod_add_quick(rh, rh, tmp, p)) goto err;
1366                         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1367                         }
1368
1369                 /* rh := rh + b*Z^6 */
1370                 if (!field_mul(group, tmp, &group->b, Z6, ctx)) goto err;
1371                 if (!BN_mod_add_quick(rh, rh, tmp, p)) goto err;
1372                 }
1373         else
1374                 {
1375                 /* point->Z_is_one */
1376
1377                 /* rh := (rh + a)*X */
1378                 if (!BN_mod_add_quick(rh, rh, &group->a, p)) goto err;
1379                 if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1380                 /* rh := rh + b */
1381                 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1382                 }
1383
1384         /* 'lh' := Y^2 */
1385         if (!field_sqr(group, tmp, &point->Y, ctx)) goto err;
1386
1387         ret = (0 == BN_ucmp(tmp, rh));
1388
1389  err:
1390         BN_CTX_end(ctx);
1391         if (new_ctx != NULL)
1392                 BN_CTX_free(new_ctx);
1393         return ret;
1394         }
1395
1396
1397 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1398         {
1399         /*-
1400          * return values:
1401          *  -1   error
1402          *   0   equal (in affine coordinates)
1403          *   1   not equal
1404          */
1405
1406         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1407         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1408         BN_CTX *new_ctx = NULL;
1409         BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1410         const BIGNUM *tmp1_, *tmp2_;
1411         int ret = -1;
1412         
1413         if (EC_POINT_is_at_infinity(group, a))
1414                 {
1415                 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1416                 }
1417
1418         if (EC_POINT_is_at_infinity(group, b))
1419                 return 1;
1420         
1421         if (a->Z_is_one && b->Z_is_one)
1422                 {
1423                 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
1424                 }
1425
1426         field_mul = group->meth->field_mul;
1427         field_sqr = group->meth->field_sqr;
1428
1429         if (ctx == NULL)
1430                 {
1431                 ctx = new_ctx = BN_CTX_new();
1432                 if (ctx == NULL)
1433                         return -1;
1434                 }
1435
1436         BN_CTX_start(ctx);
1437         tmp1 = BN_CTX_get(ctx);
1438         tmp2 = BN_CTX_get(ctx);
1439         Za23 = BN_CTX_get(ctx);
1440         Zb23 = BN_CTX_get(ctx);
1441         if (Zb23 == NULL) goto end;
1442
1443         /*-
1444          * We have to decide whether
1445          *     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1446          * or equivalently, whether
1447          *     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1448          */
1449
1450         if (!b->Z_is_one)
1451                 {
1452                 if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
1453                 if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
1454                 tmp1_ = tmp1;
1455                 }
1456         else
1457                 tmp1_ = &a->X;
1458         if (!a->Z_is_one)
1459                 {
1460                 if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
1461                 if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
1462                 tmp2_ = tmp2;
1463                 }
1464         else
1465                 tmp2_ = &b->X;
1466         
1467         /* compare  X_a*Z_b^2  with  X_b*Z_a^2 */
1468         if (BN_cmp(tmp1_, tmp2_) != 0)
1469                 {
1470                 ret = 1; /* points differ */
1471                 goto end;
1472                 }
1473
1474
1475         if (!b->Z_is_one)
1476                 {
1477                 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
1478                 if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
1479                 /* tmp1_ = tmp1 */
1480                 }
1481         else
1482                 tmp1_ = &a->Y;
1483         if (!a->Z_is_one)
1484                 {
1485                 if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
1486                 if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
1487                 /* tmp2_ = tmp2 */
1488                 }
1489         else
1490                 tmp2_ = &b->Y;
1491
1492         /* compare  Y_a*Z_b^3  with  Y_b*Z_a^3 */
1493         if (BN_cmp(tmp1_, tmp2_) != 0)
1494                 {
1495                 ret = 1; /* points differ */
1496                 goto end;
1497                 }
1498
1499         /* points are equal */
1500         ret = 0;
1501
1502  end:
1503         BN_CTX_end(ctx);
1504         if (new_ctx != NULL)
1505                 BN_CTX_free(new_ctx);
1506         return ret;
1507         }
1508
1509
1510 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1511         {
1512         BN_CTX *new_ctx = NULL;
1513         BIGNUM *x, *y;
1514         int ret = 0;
1515
1516         if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1517                 return 1;
1518
1519         if (ctx == NULL)
1520                 {
1521                 ctx = new_ctx = BN_CTX_new();
1522                 if (ctx == NULL)
1523                         return 0;
1524                 }
1525
1526         BN_CTX_start(ctx);
1527         x = BN_CTX_get(ctx);
1528         y = BN_CTX_get(ctx);
1529         if (y == NULL) goto err;
1530
1531         if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1532         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1533         if (!point->Z_is_one)
1534                 {
1535                 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1536                 goto err;
1537                 }
1538         
1539         ret = 1;
1540
1541  err:
1542         BN_CTX_end(ctx);
1543         if (new_ctx != NULL)
1544                 BN_CTX_free(new_ctx);
1545         return ret;
1546         }
1547
1548
1549 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1550         {
1551         BN_CTX *new_ctx = NULL;
1552         BIGNUM *tmp, *tmp_Z;
1553         BIGNUM **prod_Z = NULL;
1554         size_t i;
1555         int ret = 0;
1556
1557         if (num == 0)
1558                 return 1;
1559
1560         if (ctx == NULL)
1561                 {
1562                 ctx = new_ctx = BN_CTX_new();
1563                 if (ctx == NULL)
1564                         return 0;
1565                 }
1566
1567         BN_CTX_start(ctx);
1568         tmp = BN_CTX_get(ctx);
1569         tmp_Z = BN_CTX_get(ctx);
1570         if (tmp == NULL || tmp_Z == NULL) goto err;
1571
1572         prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);
1573         if (prod_Z == NULL) goto err;
1574         for (i = 0; i < num; i++)
1575                 {
1576                 prod_Z[i] = BN_new();
1577                 if (prod_Z[i] == NULL) goto err;
1578                 }
1579
1580         /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1581          * skipping any zero-valued inputs (pretend that they're 1). */
1582
1583         if (!BN_is_zero(&points[0]->Z))
1584                 {
1585                 if (!BN_copy(prod_Z[0], &points[0]->Z)) goto err;
1586                 }
1587         else
1588                 {
1589                 if (group->meth->field_set_to_one != 0)
1590                         {
1591                         if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) goto err;
1592                         }
1593                 else
1594                         {
1595                         if (!BN_one(prod_Z[0])) goto err;
1596                         }
1597                 }
1598
1599         for (i = 1; i < num; i++)
1600                 {
1601                 if (!BN_is_zero(&points[i]->Z))
1602                         {
1603                         if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1], &points[i]->Z, ctx)) goto err;
1604                         }
1605                 else
1606                         {
1607                         if (!BN_copy(prod_Z[i], prod_Z[i - 1])) goto err;
1608                         }
1609                 }
1610
1611         /* Now use a single explicit inversion to replace every
1612          * non-zero points[i]->Z by its inverse. */
1613
1614         if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx))
1615                 {
1616                 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1617                 goto err;
1618                 }
1619         if (group->meth->field_encode != 0)
1620                 {
1621                 /* In the Montgomery case, we just turned  R*H  (representing H)
1622                  * into  1/(R*H),  but we need  R*(1/H)  (representing 1/H);
1623                  * i.e. we need to multiply by the Montgomery factor twice. */
1624                 if (!group->meth->field_encode(group, tmp, tmp, ctx)) goto err;
1625                 if (!group->meth->field_encode(group, tmp, tmp, ctx)) goto err;
1626                 }
1627
1628         for (i = num - 1; i > 0; --i)
1629                 {
1630                 /* Loop invariant: tmp is the product of the inverses of
1631                  * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
1632                 if (!BN_is_zero(&points[i]->Z))
1633                         {
1634                         /* Set tmp_Z to the inverse of points[i]->Z (as product
1635                          * of Z inverses 0 .. i, Z values 0 .. i - 1). */
1636                         if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx)) goto err;
1637                         /* Update tmp to satisfy the loop invariant for i - 1. */
1638                         if (!group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx)) goto err;
1639                         /* Replace points[i]->Z by its inverse. */
1640                         if (!BN_copy(&points[i]->Z, tmp_Z)) goto err;
1641                         }
1642                 }
1643
1644         if (!BN_is_zero(&points[0]->Z))
1645                 {
1646                 /* Replace points[0]->Z by its inverse. */
1647                 if (!BN_copy(&points[0]->Z, tmp)) goto err;
1648                 }
1649
1650         /* Finally, fix up the X and Y coordinates for all points. */
1651
1652         for (i = 0; i < num; i++)
1653                 {
1654                 EC_POINT *p = points[i];
1655
1656                 if (!BN_is_zero(&p->Z))
1657                         {
1658                         /* turn  (X, Y, 1/Z)  into  (X/Z^2, Y/Z^3, 1) */
1659
1660                         if (!group->meth->field_sqr(group, tmp, &p->Z, ctx)) goto err;
1661                         if (!group->meth->field_mul(group, &p->X, &p->X, tmp, ctx)) goto err;
1662
1663                         if (!group->meth->field_mul(group, tmp, tmp, &p->Z, ctx)) goto err;
1664                         if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) goto err;
1665
1666                         if (group->meth->field_set_to_one != 0)
1667                                 {
1668                                 if (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;
1669                                 }
1670                         else
1671                                 {
1672                                 if (!BN_one(&p->Z)) goto err;
1673                                 }
1674                         p->Z_is_one = 1;
1675                         }
1676                 }
1677
1678         ret = 1;
1679
1680  err:
1681         BN_CTX_end(ctx);
1682         if (new_ctx != NULL)
1683                 BN_CTX_free(new_ctx);
1684         if (prod_Z != NULL)
1685                 {
1686                 for (i = 0; i < num; i++)
1687                         {
1688                         if (prod_Z[i] == NULL) break;
1689                         BN_clear_free(prod_Z[i]);
1690                         }
1691                 OPENSSL_free(prod_Z);
1692                 }
1693         return ret;
1694         }
1695
1696
1697 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1698         {
1699         return BN_mod_mul(r, a, b, &group->field, ctx);
1700         }
1701
1702
1703 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1704         {
1705         return BN_mod_sqr(r, a, &group->field, ctx);
1706         }