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