d6c3f635e5621a87e9d9d1918ea9964e74e2227e
[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 /* ====================================================================
5  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer. 
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <openssl/err.h>
59
60 #include "ec_lcl.h"
61
62
63 const EC_METHOD *EC_GFp_simple_method(void)
64         {
65         static const EC_METHOD ret = {
66                 ec_GFp_simple_group_init,
67                 ec_GFp_simple_group_set_curve_GFp,
68                 ec_GFp_simple_group_finish,
69                 ec_GFp_simple_group_clear_finish,
70                 ec_GFp_simple_group_copy,
71                 ec_GFp_simple_group_set_generator,
72                 /* TODO: 'set' and 'get' functions for EC_GROUPs */
73                 ec_GFp_simple_point_init,
74                 ec_GFp_simple_point_finish,
75                 ec_GFp_simple_point_clear_finish,
76                 ec_GFp_simple_point_copy,
77                 ec_GFp_simple_point_set_to_infinity,
78                 ec_GFp_simple_set_Jprojective_coordinates_GFp,
79                 ec_GFp_simple_get_Jprojective_coordinates_GFp,
80                 ec_GFp_simple_point_set_affine_coordinates_GFp,
81                 ec_GFp_simple_point_get_affine_coordinates_GFp,
82                 ec_GFp_simple_set_compressed_coordinates_GFp,
83                 ec_GFp_simple_point2oct,
84                 ec_GFp_simple_oct2point,
85                 ec_GFp_simple_add,
86                 ec_GFp_simple_dbl,
87                 ec_GFp_simple_invert,
88                 ec_GFp_simple_is_at_infinity,
89                 ec_GFp_simple_is_on_curve,
90                 ec_GFp_simple_cmp,
91                 ec_GFp_simple_make_affine,
92                 ec_GFp_simple_field_mul,
93                 ec_GFp_simple_field_sqr,
94                 0 /* field_encode */,
95                 0 /* field_decode */ };
96
97         return &ret;
98         }
99
100
101 int ec_GFp_simple_group_init(EC_GROUP *group)
102         {
103         BN_init(&group->field);
104         BN_init(&group->a);
105         BN_init(&group->b);
106         group->a_is_minus3 = 0;
107         group->generator = NULL;
108         BN_init(&group->order);
109         BN_init(&group->cofactor);
110         return 1;
111         }
112
113
114 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
115         const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
116         {
117         int ret = 0;
118         BN_CTX *new_ctx = NULL;
119         BIGNUM *tmp_a;
120         
121         /* p must be a prime > 3 */
122         if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
123                 {
124                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP, EC_R_INVALID_FIELD);
125                 return 0;
126                 }
127
128         if (ctx == NULL)
129                 {
130                 ctx = new_ctx = BN_CTX_new();
131                 if (ctx == NULL)
132                         return 0;
133                 }
134
135         BN_CTX_start(ctx);
136         tmp_a = BN_CTX_get(ctx);
137         if (tmp_a == NULL) goto err;
138
139         /* group->field */
140         if (!BN_copy(&group->field, p)) goto err;
141         group->field.neg = 0;
142
143         /* group->a */
144         if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
145         if (group->meth->field_encode)
146                 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }     
147         else
148                 if (!BN_copy(&group->a, tmp_a)) goto err;
149         
150         /* group->b */
151         if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
152         if (group->meth->field_encode)
153                 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
154         
155         /* group->a_is_minus3 */
156         if (!BN_add_word(tmp_a, 3)) goto err;
157         group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
158
159         ret = 1;
160
161  err:
162         BN_CTX_end(ctx);
163         if (new_ctx != NULL)
164                 BN_CTX_free(new_ctx);
165         return ret;
166         }
167
168
169 void ec_GFp_simple_group_finish(EC_GROUP *group)
170         {
171         BN_free(&group->field);
172         BN_free(&group->a);
173         BN_free(&group->b);
174         if (group->generator != NULL)
175                 EC_POINT_free(group->generator);
176         BN_free(&group->order);
177         BN_free(&group->cofactor);
178         }
179
180
181 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
182         {
183         BN_clear_free(&group->field);
184         BN_clear_free(&group->a);
185         BN_clear_free(&group->b);
186         if (group->generator != NULL)
187                 {
188                 EC_POINT_clear_free(group->generator);
189                 group->generator = NULL;
190                 }
191         BN_clear_free(&group->order);
192         BN_clear_free(&group->cofactor);
193         }
194
195
196 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
197         {
198         if (!BN_copy(&dest->field, &src->field)) return 0;
199         if (!BN_copy(&dest->a, &src->a)) return 0;
200         if (!BN_copy(&dest->b, &src->b)) return 0;
201
202         dest->a_is_minus3 = src->a_is_minus3;
203
204         if (src->generator != NULL)
205                 {
206                 if (dest->generator == NULL)
207                         {
208                         dest->generator = EC_POINT_new(dest);
209                         if (dest->generator == NULL) return 0;
210                         }
211                 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
212                 }
213         else
214                 {
215                 /* src->generator == NULL */
216                 if (dest->generator != NULL)
217                         {
218                         EC_POINT_clear_free(dest->generator);
219                         dest->generator = NULL;
220                         }
221                 }
222
223         if (!BN_copy(&dest->order, &src->order)) return 0;
224         if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
225
226         return 1;
227         }
228
229
230 int ec_GFp_simple_group_set_generator(EC_GROUP *group, const EC_POINT *generator,
231         const BIGNUM *order, const BIGNUM *cofactor)
232         {
233         if (generator)
234                 {
235                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
236                 return 0   ;
237                 }
238
239         if (group->generator == NULL)
240                 {
241                 group->generator = EC_POINT_new(group);
242                 if (group->generator == NULL) return 0;
243                 }
244         if (!EC_POINT_copy(group->generator, generator)) return 0;
245
246         if (order != NULL)
247                 { if (!BN_copy(&group->order, order)) return 0; }       
248         else
249                 { if (!BN_zero(&group->order)) return 0; }      
250
251         if (cofactor != NULL)
252                 { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 
253         else
254                 { if (!BN_zero(&group->cofactor)) return 0; }   
255
256         return 1;
257         }
258
259
260 /* TODO: 'set' and 'get' functions for EC_GROUPs */
261
262
263 int ec_GFp_simple_point_init(EC_POINT *point)
264         {
265         BN_init(&point->X);
266         BN_init(&point->Y);
267         BN_init(&point->Z);
268         point->Z_is_one = 0;
269
270         return 1;
271         }
272
273
274 void ec_GFp_simple_point_finish(EC_POINT *point)
275         {
276         BN_free(&point->X);
277         BN_free(&point->Y);
278         BN_free(&point->Z);
279         }
280
281
282 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
283         {
284         BN_clear_free(&point->X);
285         BN_clear_free(&point->Y);
286         BN_clear_free(&point->Z);
287         point->Z_is_one = 0;
288         }
289
290
291 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
292         {
293         if (!BN_copy(&dest->X, &src->X)) return 0;
294         if (!BN_copy(&dest->Y, &src->Y)) return 0;
295         if (!BN_copy(&dest->Z, &src->Z)) return 0;
296         dest->Z_is_one = src->Z_is_one;
297
298         return 1;
299         }
300
301
302 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
303         {
304         point->Z_is_one = 0;
305         return (BN_zero(&point->Z));
306         }
307
308
309 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
310         const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx);
311 /* TODO */
312
313
314 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
315         BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx);
316 /* TODO */
317
318
319 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
320         const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
321         {
322         BN_CTX *new_ctx = NULL;
323         int ret = 0;
324
325         if (!BN_copy(&point->X, x)) goto err;
326         if (!BN_copy(&point->Y, y)) goto err;
327         if (!BN_one(&point->Z)) goto err;
328
329         if (group->meth->field_encode)
330                 {
331                 if (ctx == NULL)
332                         {
333                         ctx = new_ctx = BN_CTX_new();
334                         if (ctx == NULL)
335                                 return 0;
336                         }
337                 
338                 if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
339                 if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
340                 if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
341                 }
342
343         point->Z_is_one = 1;
344         
345  err:
346         if (new_ctx != NULL)
347                 BN_CTX_free(new_ctx);
348         return ret;
349         }
350
351
352 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
353         BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
354         {
355         BN_CTX *new_ctx = NULL;
356         BIGNUM *X, *Y, *Z, *Z_1, *Z_2, *Z_3;
357         const BIGNUM *X_, *Y_, *Z_;
358         int ret = 0;
359
360         if (EC_POINT_is_at_infinity(group, point))
361                 {
362                 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_POINT_AT_INFINITY);
363                 return 0;
364                 }
365
366         if (ctx == NULL)
367                 {
368                 ctx = new_ctx = BN_CTX_new();
369                 if (ctx == NULL)
370                         return 0;
371                 }
372
373         BN_CTX_start(ctx);
374         X = BN_CTX_get(ctx);
375         Y = BN_CTX_get(ctx);
376         Z = BN_CTX_get(ctx);
377         Z_1 = BN_CTX_get(ctx);
378         Z_2 = BN_CTX_get(ctx);
379         Z_3 = BN_CTX_get(ctx);
380         if (Z_3 == NULL) goto err;
381
382         /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */
383         
384         if (group->meth->field_decode)
385                 {
386                 if (!group->meth->field_decode(group, X, &point->X, ctx)) goto err;
387                 if (!group->meth->field_decode(group, Y, &point->Y, ctx)) goto err;
388                 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
389                 X_ = X; Y_ = Y; Z_ = Z;
390                 }
391         else
392                 {
393                 X_ = &point->X;
394                 Y_ = &point->Y;
395                 Z_ = &point->Z;
396                 }
397         
398         if (BN_is_one(Z_))
399                 {
400                 if (x != NULL)
401                         {
402                         if (!BN_copy(x, X_)) goto err;
403                         }
404                 if (y != NULL)
405                         {
406                         if (!BN_copy(y, Y_)) goto err;
407                         }
408                 }
409         else
410                 {
411                 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
412                         {
413                         ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_BN_LIB);
414                         goto err;
415                         }
416                 if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
417         
418                 if (x != NULL)
419                         {
420                         if (!BN_mod_mul(x, X_, Z_2, &group->field, ctx)) goto err;
421                         }
422
423                 if (y != NULL)
424                         {
425                         if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
426                         if (!BN_mod_mul(y, Y_, Z_3, &group->field, ctx)) goto err;
427                         }
428                 }
429
430         ret = 1;
431
432  err:
433         BN_CTX_end(ctx);
434         if (new_ctx != NULL)
435                 BN_CTX_free(new_ctx);
436         return ret;
437         }
438
439
440 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
441         const BIGNUM *x, int y_bit, BN_CTX *);
442 /* TODO */
443
444
445 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
446         unsigned char *buf, size_t len, BN_CTX *ctx)
447         {
448         size_t ret;
449         BN_CTX *new_ctx = NULL;
450         int used_ctx = 0;
451         BIGNUM *x, *y;
452         size_t field_len, i, skip;
453
454         if ((form != POINT_CONVERSION_COMPRESSED)
455                 && (form != POINT_CONVERSION_UNCOMPRESSED)
456                 && (form != POINT_CONVERSION_HYBRID))
457                 {
458                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
459                 goto err;
460                 }
461
462         if (EC_POINT_is_at_infinity(group, point))
463                 {
464                 /* encodes to a single 0 octet */
465                 if (buf != NULL)
466                         {
467                         if (len < 1)
468                                 {
469                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
470                                 return 0;
471                                 }
472                         buf[0] = 0;
473                         }
474                 return 1;
475                 }
476
477
478         /* ret := required output buffer length */
479         field_len = BN_num_bytes(&group->field);
480         ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
481
482         /* if 'buf' is NULL, just return required length */
483         if (buf != NULL)
484                 {
485                 if (len < ret)
486                         {
487                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
488                         goto err;
489                         }
490
491                 if (ctx == NULL)
492                         {
493                         ctx = new_ctx = BN_CTX_new();
494                         if (ctx == NULL)
495                                 return 0;
496                         }
497
498                 BN_CTX_start(ctx);
499                 used_ctx = 1;
500                 x = BN_CTX_get(ctx);
501                 y = BN_CTX_get(ctx);
502                 if (y == NULL) goto err;
503
504                 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
505
506                 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
507                         buf[0] = form + 1;
508                 else
509                         buf[0] = form;
510         
511                 i = 1;
512                 
513                 skip = field_len - BN_num_bytes(x);
514                 if (skip > field_len)
515                         {
516                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
517                         goto err;
518                         }
519                 while (skip > 0)
520                         {
521                         buf[i++] = 0;
522                         skip--;
523                         }
524                 skip = BN_bn2bin(x, buf + i);
525                 i += skip;
526                 if (i != 1 + field_len)
527                         {
528                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
529                         goto err;
530                         }
531
532                 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
533                         {
534                         skip = field_len - BN_num_bytes(y);
535                         if (skip > field_len)
536                                 {
537                                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
538                                 goto err;
539                                 }
540                         while (skip > 0)
541                                 {
542                                 buf[i++] = 0;
543                                 skip--;
544                                 }
545                         skip = BN_bn2bin(y, buf + i);
546                         i += skip;
547                         }
548
549                 if (i != ret)
550                         {
551                         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
552                         goto err;
553                         }
554                 }
555         
556         if (used_ctx)
557                 BN_CTX_end(ctx);
558         if (new_ctx != NULL)
559                 BN_CTX_free(new_ctx);
560         return ret;
561
562  err:
563         if (used_ctx)
564                 BN_CTX_end(ctx);
565         if (new_ctx != NULL)
566                 BN_CTX_free(new_ctx);
567         return 0;
568         }
569
570
571 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
572         const unsigned char *buf, size_t len, BN_CTX *ctx)
573         {
574         point_conversion_form_t form;
575         int y_bit;
576         BN_CTX *new_ctx = NULL;
577         BIGNUM *x, *y;
578         size_t field_len, enc_len;
579         int ret = 0;
580
581         if (len <= 0)
582                 {
583                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
584                 return 0;
585                 }
586         form = buf[0];
587         y_bit = form & 1;
588         form = form & ~1;
589         if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
590                 && (form != POINT_CONVERSION_UNCOMPRESSED)
591                 && (form != POINT_CONVERSION_HYBRID))
592                 {
593                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
594                 return 0;
595                 }
596         if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
597                 {
598                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
599                 return 0;
600                 }
601
602         if (form == 0)
603                 {
604                 if (len != 1)
605                         {
606                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
607                         return 0;
608                         }
609
610                 return EC_POINT_set_to_infinity(group, point);
611                 }
612         
613         field_len = BN_num_bytes(&group->field);
614         enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
615
616         if (len != enc_len)
617                 {
618                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
619                 return 0;
620                 }
621
622         if (ctx == NULL)
623                 {
624                 ctx = new_ctx = BN_CTX_new();
625                 if (ctx == NULL)
626                         return 0;
627                 }
628
629         BN_CTX_start(ctx);
630         x = BN_CTX_get(ctx);
631         y = BN_CTX_get(ctx);
632         if (y == NULL) goto err;
633
634         if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
635         if (BN_ucmp(x, &group->field) >= 0)
636                 {
637                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
638                 goto err;
639                 }
640
641         if (form != POINT_CONVERSION_COMPRESSED)
642                 {
643                 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
644                 if (BN_ucmp(y, &group->field) >= 0)
645                         {
646                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
647                         goto err;
648                         }
649                 if (form == POINT_CONVERSION_HYBRID)
650                         {
651                         if (y_bit != BN_is_odd(y))
652                                 {
653                                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
654                                 goto err;
655                                 }
656                         }
657                 }
658
659         if (form == POINT_CONVERSION_COMPRESSED)
660                 {
661                 /* Recover y.  We have a Weierstrass equation
662                  *     y^2 = x^3 + a*x + b,
663                  * so  y  is one of the square roots of  x^3 + a*x + b.
664                  */
665
666                 BIGNUM *tmp1, *tmp2;
667                 
668                 tmp1 = BN_CTX_get(ctx);
669                 tmp2 = BN_CTX_get(ctx);
670                 if (tmp2 == NULL) goto err;
671                 
672                 /* tmp1 := x^3 */
673                 if (!BN_mod_sqr(tmp2, x, &group->field, ctx)) goto err;
674                 if (!BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) goto err;
675
676                 /* tmp1 := tmp1 + a*x */
677                 if (group->a_is_minus3)
678                         {
679                         if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
680                         if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
681                         if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
682                         }
683                 else
684                         {
685                         if (!BN_mod_mul(tmp2, &group->a, x, &group->field, ctx)) goto err;
686                         if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
687                         }
688                 
689                 /* tmp1 := tmp1 + b */
690                 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
691
692                 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
693                         {
694                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, ERR_R_BN_LIB);
695                         goto err;
696                         }
697
698                 if (y_bit != BN_is_odd(y))
699                         {
700                         if (BN_is_zero(y))
701                                 {
702                                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
703                                 goto err;
704                                 }
705                         if (!BN_usub(y, &group->field, y)) goto err;
706                         }
707                 if (y_bit != BN_is_odd(y))
708                         {
709                         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, ERR_R_INTERNAL_ERROR);
710                         goto err;
711                         }
712                 }
713         
714         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
715
716         if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
717                 {
718                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
719                 goto err;
720                 }
721
722         ret = 1;
723         
724  err:
725         BN_CTX_end(ctx);
726         if (new_ctx != NULL)
727                 BN_CTX_free(new_ctx);
728         return ret;
729         }
730
731
732 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
733         {
734         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
735         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
736         const BIGNUM *p;
737         BN_CTX *new_ctx = NULL;
738         BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
739         int ret = 0;
740         
741         if (a == b)
742                 return EC_POINT_dbl(group, r, a, ctx);
743         if (EC_POINT_is_at_infinity(group, a))
744                 return EC_POINT_copy(r, b);
745         if (EC_POINT_is_at_infinity(group, b))
746                 return EC_POINT_copy(r, a);
747         
748         field_mul = group->meth->field_mul;
749         field_sqr = group->meth->field_sqr;
750         p = &group->field;
751
752         if (ctx == NULL)
753                 {
754                 ctx = new_ctx = BN_CTX_new();
755                 if (ctx == NULL)
756                         return 0;
757                 }
758
759         BN_CTX_start(ctx);
760         n0 = BN_CTX_get(ctx);
761         n1 = BN_CTX_get(ctx);
762         n2 = BN_CTX_get(ctx);
763         n3 = BN_CTX_get(ctx);
764         n4 = BN_CTX_get(ctx);
765         n5 = BN_CTX_get(ctx);
766         n6 = BN_CTX_get(ctx);
767         if (n6 == NULL) goto end;
768
769         /* Note that in this function we must not read components of 'a' or 'b'
770          * once we have written the corresponding components of 'r'.
771          * ('r' might be one of 'a' or 'b'.)
772          */
773
774         /* n1, n2 */
775         if (b->Z_is_one)
776                 {
777                 if (!BN_copy(n1, &a->X)) goto end;
778                 if (!BN_copy(n2, &a->Y)) goto end;
779                 /* n1 = X_a */
780                 /* n2 = Y_a */
781                 }
782         else
783                 {
784                 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
785                 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
786                 /* n1 = X_a * Z_b^2 */
787
788                 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
789                 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
790                 /* n2 = Y_a * Z_b^3 */
791                 }
792
793         /* n3, n4 */
794         if (a->Z_is_one)
795                 {
796                 if (!BN_copy(n3, &b->X)) goto end;
797                 if (!BN_copy(n4, &b->Y)) goto end;
798                 /* n3 = X_b */
799                 /* n4 = Y_b */
800                 }
801         else
802                 {
803                 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
804                 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
805                 /* n3 = X_b * Z_a^2 */
806
807                 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
808                 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
809                 /* n4 = Y_b * Z_a^3 */
810                 }
811
812         /* n5, n6 */
813         if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
814         if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
815         /* n5 = n1 - n3 */
816         /* n6 = n2 - n4 */
817
818         if (BN_is_zero(n5))
819                 {
820                 if (BN_is_zero(n6))
821                         {
822                         /* a is the same point as b */
823                         BN_CTX_end(ctx);
824                         ret = EC_POINT_dbl(group, r, a, ctx);
825                         ctx = NULL;
826                         goto end;
827                         }
828                 else
829                         {
830                         /* a is the inverse of b */
831                         if (!BN_zero(&r->Z)) goto end;
832                         r->Z_is_one = 0;
833                         ret = 1;
834                         goto end;
835                         }
836                 }
837
838         /* 'n7', 'n8' */
839         if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
840         if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
841         /* 'n7' = n1 + n3 */
842         /* 'n8' = n2 + n4 */
843
844         /* Z_r */
845         if (a->Z_is_one && b->Z_is_one)
846                 {
847                 if (!BN_copy(&r->Z, n5)) goto end;
848                 }
849         else
850                 {
851                 if (a->Z_is_one)
852                         { if (!BN_copy(n0, &b->Z)) goto end; }
853                 else if (b->Z_is_one)
854                         { if (!BN_copy(n0, &a->Z)) goto end; }
855                 else
856                         { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
857                 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
858                 }
859         r->Z_is_one = 0;
860         /* Z_r = Z_a * Z_b * n5 */
861
862         /* X_r */
863         if (!field_sqr(group, n0, n6, ctx)) goto end;
864         if (!field_sqr(group, n4, n5, ctx)) goto end;
865         if (!field_mul(group, n3, n1, n4, ctx)) goto end;
866         if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
867         /* X_r = n6^2 - n5^2 * 'n7' */
868         
869         /* 'n9' */
870         if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
871         if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
872         /* n9 = n5^2 * 'n7' - 2 * X_r */
873
874         /* Y_r */
875         if (!field_mul(group, n0, n0, n6, ctx)) goto end;
876         if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
877         if (!field_mul(group, n1, n2, n5, ctx)) goto end;
878         if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
879         if (BN_is_odd(n0))
880                 if (!BN_add(n0, n0, p)) goto end;
881         /* now  0 <= n0 < 2*p,  and n0 is even */
882         if (!BN_rshift1(&r->Y, n0)) goto end;
883         /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
884
885         ret = 1;
886
887  end:
888         if (ctx) /* otherwise we already called BN_CTX_end */
889                 BN_CTX_end(ctx);
890         if (new_ctx != NULL)
891                 BN_CTX_free(new_ctx);
892         return ret;
893         }
894
895
896 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
897         {
898         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
899         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
900         const BIGNUM *p;
901         BN_CTX *new_ctx = NULL;
902         BIGNUM *n0, *n1, *n2, *n3;
903         int ret = 0;
904         
905         if (EC_POINT_is_at_infinity(group, a))
906                 {
907                 if (!BN_zero(&r->Z)) return 0;
908                 r->Z_is_one = 0;
909                 return 1;
910                 }
911
912         field_mul = group->meth->field_mul;
913         field_sqr = group->meth->field_sqr;
914         p = &group->field;
915
916         if (ctx == NULL)
917                 {
918                 ctx = new_ctx = BN_CTX_new();
919                 if (ctx == NULL)
920                         return 0;
921                 }
922
923         BN_CTX_start(ctx);
924         n0 = BN_CTX_get(ctx);
925         n1 = BN_CTX_get(ctx);
926         n2 = BN_CTX_get(ctx);
927         n3 = BN_CTX_get(ctx);
928         if (n3 == NULL) goto err;
929
930         /* Note that in this function we must not read components of 'a'
931          * once we have written the corresponding components of 'r'.
932          * ('r' might the same as 'a'.)
933          */
934
935         /* n1 */
936         if (a->Z_is_one)
937                 {
938                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
939                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
940                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
941                 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
942                 /* n1 = 3 * X_a^2 + a_curve */
943                 }
944         else if (group->a_is_minus3)
945                 {
946                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
947                 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
948                 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
949                 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
950                 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
951                 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
952                 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
953                  *    = 3 * X_a^2 - 3 * Z_a^4 */
954                 }
955         else
956                 {
957                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
958                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
959                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
960                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
961                 if (!field_sqr(group, n1, n1, ctx)) goto err;
962                 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
963                 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
964                 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
965                 }
966
967         /* Z_r */
968         if (a->Z_is_one)
969                 {
970                 if (!BN_copy(n0, &a->Y)) goto err;
971                 }
972         else
973                 {
974                 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
975                 }
976         if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
977         r->Z_is_one = 0;
978         /* Z_r = 2 * Y_a * Z_a */
979
980         /* n2 */
981         if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
982         if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
983         if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
984         /* n2 = 4 * X_a * Y_a^2 */
985
986         /* X_r */
987         if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
988         if (!field_sqr(group, &r->X, n1, ctx)) goto err;
989         if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
990         /* X_r = n1^2 - 2 * n2 */
991         
992         /* n3 */
993         if (!field_sqr(group, n0, n3, ctx)) goto err;
994         if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
995         /* n3 = 8 * Y_a^4 */
996         
997         /* Y_r */
998         if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
999         if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1000         if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1001         /* Y_r = n1 * (n2 - X_r) - n3 */
1002
1003         ret = 1;
1004
1005  err:
1006         BN_CTX_end(ctx);
1007         if (new_ctx != NULL)
1008                 BN_CTX_free(new_ctx);
1009         return ret;
1010         }
1011
1012
1013 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
1014 /* TODO */
1015
1016
1017 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1018         {
1019         return BN_is_zero(&point->Z);
1020         }
1021
1022
1023 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1024         {
1025         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1026         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1027         const BIGNUM *p;
1028         BN_CTX *new_ctx = NULL;
1029         BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
1030         int ret = -1;
1031
1032         if (EC_POINT_is_at_infinity(group, point))
1033                 return 1;
1034         
1035         field_mul = group->meth->field_mul;
1036         field_sqr = group->meth->field_sqr;
1037         p = &group->field;
1038
1039         if (ctx == NULL)
1040                 {
1041                 ctx = new_ctx = BN_CTX_new();
1042                 if (ctx == NULL)
1043                         return -1;
1044                 }
1045
1046         BN_CTX_start(ctx);
1047         rh = BN_CTX_get(ctx);
1048         tmp1 = BN_CTX_get(ctx);
1049         tmp2 = BN_CTX_get(ctx);
1050         Z4 = BN_CTX_get(ctx);
1051         Z6 = BN_CTX_get(ctx);
1052         if (Z6 == NULL) goto err;
1053
1054         /* We have a curve defined by a Weierstrass equation
1055          *      y^2 = x^3 + a*x + b.
1056          * The point to consider is given in Jacobian projective coordinates
1057          * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
1058          * Substituting this and multiplying by  Z^6  transforms the above equation into
1059          *      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1060          * To test this, we add up the right-hand side in 'rh'.
1061          */
1062
1063         /* rh := X^3 */
1064         if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1065         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1066
1067         if (!point->Z_is_one)
1068                 {
1069                 if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
1070                 if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
1071                 if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
1072
1073                 /* rh := rh + a*X*Z^4 */
1074                 if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
1075                 if (&group->a_is_minus3)
1076                         {
1077                         if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
1078                         if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
1079                         if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1080                         }
1081                 else
1082                         {
1083                         if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
1084                         if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1085                         }
1086
1087                 /* rh := rh + b*Z^6 */
1088                 if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
1089                 if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
1090                 }
1091         else
1092                 {
1093                 /* point->Z_is_one */
1094
1095                 /* rh := rh + a*X */
1096                 if (&group->a_is_minus3)
1097                         {
1098                         if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
1099                         if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
1100                         if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1101                         }
1102                 else
1103                         {
1104                         if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
1105                         if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1106                         }
1107
1108                 /* rh := rh + b */
1109                 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1110                 }
1111
1112         /* 'lh' := Y^2 */
1113         if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
1114
1115         ret = (0 == BN_cmp(tmp1, rh));
1116
1117  err:
1118         BN_CTX_end(ctx);
1119         if (new_ctx != NULL)
1120                 BN_CTX_free(new_ctx);
1121         return ret;
1122         }
1123
1124
1125 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
1126 /* TODO */
1127
1128
1129 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1130         {
1131         BN_CTX *new_ctx = NULL;
1132         BIGNUM *x, *y;
1133         int ret = 0;
1134
1135         if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1136                 return 1;
1137
1138         if (ctx == NULL)
1139                 {
1140                 ctx = new_ctx = BN_CTX_new();
1141                 if (ctx == NULL)
1142                         return 0;
1143                 }
1144
1145         BN_CTX_start(ctx);
1146         x = BN_CTX_get(ctx);
1147         y = BN_CTX_get(ctx);
1148         if (y == NULL) goto err;
1149
1150         if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1151         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1152         if (!point->Z_is_one)
1153                 {
1154                 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1155                 goto err;
1156                 }
1157         
1158         ret = 1;
1159
1160  err:
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_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1169         {
1170         return BN_mod_mul(r, a, b, &group->field, ctx);
1171         }
1172
1173
1174 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1175         {
1176         return BN_mod_sqr(r, a, &group->field, ctx);
1177         }