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