Some actual method functions (not enough yet to use the EC library, though),
[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                 /* TODO: 'set' and 'get' functions for EC_POINTs */
78                 ec_GFp_simple_point2oct,
79                 ec_GFp_simple_oct2point,
80                 ec_GFp_simple_add,
81                 ec_GFp_simple_dbl,
82                 ec_GFp_simple_is_at_infinity,
83                 ec_GFp_simple_is_on_curve,
84                 ec_GFp_simple_make_affine,
85                 ec_GFp_simple_field_mul,
86                 ec_GFp_simple_field_sqr,
87                 0 /* field_encode */,
88                 0 /* field_decode */ };
89
90         return &ret;
91         }
92
93
94 int ec_GFp_simple_group_init(EC_GROUP *group)
95         {
96         BN_init(&group->field);
97         BN_init(&group->a);
98         BN_init(&group->b);
99         group->a_is_minus3 = 0;
100         group->generator = NULL;
101         BN_init(&group->order);
102         BN_init(&group->cofactor);
103         return 1;
104         }
105
106
107 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
108         const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
109         {
110         int ret = 0;
111         BN_CTX *new_ctx = NULL;
112         BIGNUM *tmp_a;
113         
114         if (ctx == NULL)
115                 {
116                 ctx = new_ctx = BN_CTX_new();
117                 if (ctx == NULL)
118                         return 0;
119                 }
120         BN_CTX_start(ctx);
121
122         tmp_a = BN_CTX_get(ctx);
123         if (tmp_a == NULL) goto err;
124
125         /* group->field */
126         if (!BN_copy(&group->field, p)) goto err;
127         group->field.neg = 0;
128
129         /* group->a */
130         if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
131         if (group->meth->field_encode)
132                 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }     
133         else
134                 if (!BN_copy(&group->a, tmp_a)) goto err;
135         
136         /* group->b */
137         if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
138         if (group->meth->field_encode)
139                 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
140         
141         /* group->a_is_minus3 */
142         if (!BN_add_word(tmp_a, 3)) goto err;
143         group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
144
145         ret = 1;
146
147  err:
148         BN_CTX_end(ctx);
149         if (new_ctx != NULL)
150                 BN_CTX_free(new_ctx);
151         return ret;
152         }
153
154
155 void ec_GFp_simple_group_finish(EC_GROUP *group)
156         {
157         BN_free(&group->field);
158         BN_free(&group->a);
159         BN_free(&group->b);
160         if (group->generator != NULL)
161                 EC_POINT_free(group->generator);
162         BN_free(&group->order);
163         BN_free(&group->cofactor);
164         }
165
166
167 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
168         {
169         BN_clear_free(&group->field);
170         BN_clear_free(&group->a);
171         BN_clear_free(&group->b);
172         if (group->generator != NULL)
173                 {
174                 EC_POINT_clear_free(group->generator);
175                 group->generator = NULL;
176                 }
177         BN_clear_free(&group->order);
178         BN_clear_free(&group->cofactor);
179         }
180
181
182 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
183         {
184         if (!BN_copy(&dest->field, &src->field)) return 0;
185         if (!BN_copy(&dest->a, &src->a)) return 0;
186         if (!BN_copy(&dest->b, &src->b)) return 0;
187
188         dest->a_is_minus3 = src->a_is_minus3;
189
190         if (src->generator != NULL)
191                 {
192                 if (dest->generator == NULL)
193                         {
194                         dest->generator = EC_POINT_new(dest);
195                         if (dest->generator == NULL) return 0;
196                         }
197                 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
198                 }
199         else
200                 {
201                 /* src->generator == NULL */
202                 if (dest->generator != NULL)
203                         {
204                         EC_POINT_clear_free(dest->generator);
205                         dest->generator = NULL;
206                         }
207                 }
208
209         if (!BN_copy(&dest->order, &src->order)) return 0;
210         if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
211
212         return 1;
213         }
214
215
216 int ec_GFp_simple_group_set_generator(EC_GROUP *group, const EC_POINT *generator,
217         const BIGNUM *order, const BIGNUM *cofactor)
218         {
219         if (generator)
220                 {
221                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
222                 return 0   ;
223                 }
224
225         if (group->generator == NULL)
226                 {
227                 group->generator = EC_POINT_new(group);
228                 if (group->generator == NULL) return 0;
229                 }
230         if (!EC_POINT_copy(group->generator, generator)) return 0;
231
232         if (order != NULL)
233                 { if (!BN_copy(&group->order, order)) return 0; }       
234         else
235                 { if (!BN_zero(&group->order)) return 0; }      
236
237         if (cofactor != NULL)
238                 { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 
239         else
240                 { if (!BN_zero(&group->cofactor)) return 0; }   
241
242         return 1;
243         }
244
245
246 /* TODO: 'set' and 'get' functions for EC_GROUPs */
247
248
249 int ec_GFp_simple_point_init(EC_POINT *point)
250         {
251         BN_init(&point->X);
252         BN_init(&point->Y);
253         BN_init(&point->Z);
254         point->Z_is_one = 0;
255
256         return 1;
257         }
258
259
260 void ec_GFp_simple_point_finish(EC_POINT *point)
261         {
262         BN_free(&point->X);
263         BN_free(&point->Y);
264         BN_free(&point->Z);
265         }
266
267
268 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
269         {
270         BN_clear_free(&point->X);
271         BN_clear_free(&point->Y);
272         BN_clear_free(&point->Z);
273         point->Z_is_one = 0;
274         }
275
276
277 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
278         {
279         if (!BN_copy(&dest->X, &src->X)) return 0;
280         if (!BN_copy(&dest->Y, &src->Y)) return 0;
281         if (!BN_copy(&dest->Z, &src->Z)) return 0;
282         dest->Z_is_one = src->Z_is_one;
283
284         return 1;
285         }
286
287
288 /* TODO: 'set' and 'get' functions for EC_POINTs */
289
290
291 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
292         unsigned char *buf, size_t len, BN_CTX *ctx);
293 /* TODO */
294
295
296 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
297         const unsigned char *buf, size_t len, BN_CTX *);
298 /* TODO */
299
300
301 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
302         {
303         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
304         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
305         const BIGNUM *p;
306         BN_CTX *new_ctx = NULL;
307         BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
308         int ret = 0;
309         
310         if (a == b)
311                 return EC_POINT_dbl(group, r, a, ctx);
312         if (EC_POINT_is_at_infinity(group, a))
313                 return EC_POINT_copy(r, b);
314         if (EC_POINT_is_at_infinity(group, b))
315                 return EC_POINT_copy(r, a);
316         
317         field_mul = group->meth->field_mul;
318         field_sqr = group->meth->field_sqr;
319         p = &group->field;
320
321         if (ctx == NULL)
322                 {
323                 ctx = new_ctx = BN_CTX_new();
324                 if (ctx == NULL)
325                         return 0;
326                 }
327         BN_CTX_start(ctx);
328
329         n0 = BN_CTX_get(ctx);
330         n1 = BN_CTX_get(ctx);
331         n2 = BN_CTX_get(ctx);
332         n3 = BN_CTX_get(ctx);
333         n4 = BN_CTX_get(ctx);
334         n5 = BN_CTX_get(ctx);
335         n6 = BN_CTX_get(ctx);
336         if (n6 == NULL) goto end;
337
338         /* n1, n2 */
339         if (b->Z_is_one)
340                 {
341                 if (!BN_copy(n1, &a->X)) goto end;
342                 if (!BN_copy(n2, &a->Y)) goto end;
343                 /* n1 = X_a */
344                 /* n2 = Y_a */
345                 }
346         else
347                 {
348                 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
349                 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
350                 /* n1 = X_a * Z_b^2 */
351
352                 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
353                 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
354                 /* n2 = Y_a * Z_b^3 */
355                 }
356
357         /* n3, n4 */
358         if (a->Z_is_one)
359                 {
360                 if (!BN_copy(n3, &b->X)) goto end;
361                 if (!BN_copy(n4, &b->Y)) goto end;
362                 /* n3 = X_b */
363                 /* n4 = Y_b */
364                 }
365         else
366                 {
367                 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
368                 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
369                 /* n3 = X_b * Z_a^2 */
370
371                 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
372                 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
373                 /* n4 = Y_b * Z_a^3 */
374                 }
375
376         /* n5, n6 */
377         if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
378         if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
379         /* n5 = n1 - n3 */
380         /* n6 = n2 - n4 */
381
382         if (BN_is_zero(n5))
383                 {
384                 if (BN_is_zero(n6))
385                         {
386                         /* a is the same point as b */
387                         BN_CTX_end(ctx);
388                         ctx = NULL;
389                         ret = EC_POINT_dbl(group, r, a, ctx);
390                         goto end;
391                         }
392                 else
393                         {
394                         /* a is the inverse of b */
395                         if (!BN_zero(&r->Z)) goto end;
396                         r->Z_is_one = 0;
397                         ret = 1;
398                         goto end;
399                         }
400                 }
401
402         /* 'n7', 'n8' */
403         if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
404         if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
405         /* 'n7' = n1 + n3 */
406         /* 'n8' = n2 + n4 */
407
408         /* Z_r */
409         if (a->Z_is_one && b->Z_is_one)
410                 {
411                 if (!BN_copy(&r->Z, n5)) goto end;
412                 }
413         else
414                 {
415                 if (a->Z_is_one)
416                         { if (!BN_copy(n0, &b->Z)) goto end; }
417                 else if (b->Z_is_one)
418                         { if (!BN_copy(n0, &a->Z)) goto end; }
419                 else
420                         { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
421                 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
422                 }
423         r->Z_is_one = 0;
424         /* Z_r = Z_a * Z_b * n5 */
425
426         /* X_r */
427         if (!field_sqr(group, n0, n6, ctx)) goto end;
428         if (!field_sqr(group, n4, n5, ctx)) goto end;
429         if (!field_mul(group, n3, n1, n4, ctx)) goto end;
430         if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
431         /* X_r = n6^2 - n5^2 * 'n7' */
432         
433         /* 'n9' */
434         if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
435         if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
436         /* n9 = n5^2 * 'n7' - 2 * X_r */
437
438         /* Y_r */
439         if (!field_mul(group, n0, n0, n6, ctx)) goto end;
440         if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
441         if (!field_mul(group, n1, n2, n5, ctx)) goto end;
442         if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
443         if (BN_is_odd(n0))
444                 if (!BN_add(n0, n0, p)) goto end;
445         /* now  0 <= n0 < 2*p,  and n0 is even */
446         if (!BN_rshift1(&r->Y, n0)) goto end;
447         /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
448
449         ret = 1;
450
451  end:
452         if (ctx) /* otherwise we already called BN_CTX_end */
453                 BN_CTX_end(ctx);
454         if (new_ctx != NULL)
455                 BN_CTX_free(new_ctx);
456         return ret;
457         }
458
459
460 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
461         {
462         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
463         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
464         const BIGNUM *p;
465         BN_CTX *new_ctx = NULL;
466         BIGNUM *n0, *n1, *n2, *n3;
467         int ret = 0;
468         
469         if (EC_POINT_is_at_infinity(group, a))
470                 {
471                 if (!BN_zero(&r->Z)) return 0;
472                 r->Z_is_one = 0;
473                 return 1;
474                 }
475
476         field_mul = group->meth->field_mul;
477         field_sqr = group->meth->field_sqr;
478         p = &group->field;
479
480         if (ctx == NULL)
481                 {
482                 ctx = new_ctx = BN_CTX_new();
483                 if (ctx == NULL)
484                         return 0;
485                 }
486         BN_CTX_start(ctx);
487
488         n0 = BN_CTX_get(ctx);
489         n1 = BN_CTX_get(ctx);
490         n2 = BN_CTX_get(ctx);
491         n3 = BN_CTX_get(ctx);
492         if (n3 == NULL) goto err;
493
494         /* TODO: optimization for group->a_is_minus3 */
495
496         /* n1 */
497         if (a->Z_is_one)
498                 {
499                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
500                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
501                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
502                 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
503                 /* n1 = 3 * X_a^2 + a_curve */
504                 }
505         else if (group->a_is_minus3)
506                 {
507                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
508                 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
509                 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
510                 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
511                 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
512                 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
513                 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
514                  *    = 3 * X_a^2 - 3 * Z_a^4 */
515                 }
516         else
517                 {
518                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
519                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
520                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
521                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
522                 if (!field_sqr(group, n1, n1, ctx)) goto err;
523                 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
524                 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
525                 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
526                 }
527
528         /* Z_r */
529         if (a->Z_is_one)
530                 {
531                 if (!BN_copy(n0, &a->Y)) goto err;
532                 }
533         else
534                 {
535                 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
536                 }
537         if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
538         r->Z_is_one = 0;
539         /* Z_r = 2 * Y_a * Z_a */
540
541         /* n2 */
542         if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
543         if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
544         if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
545         /* n2 = 4 * X_a * Y_a^2 */
546
547         /* X_r */
548         if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
549         if (!field_sqr(group, &r->X, n1, ctx)) goto err;
550         if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
551         /* X_r = n1^2 - 2 * n2 */
552         
553         /* n3 */
554         if (!field_sqr(group, n0, n3, ctx)) goto err;
555         if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
556         /* n3 = 8 * Y_a^4 */
557         
558         /* Y_r */
559         if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
560         if (!field_mul(group, n0, n1, n0, ctx)) goto err;
561         if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
562         /* Y_r = n1 * (n2 - X_r) - n3 */
563
564         ret = 1;
565
566  err:
567         BN_CTX_end(ctx);
568         if (new_ctx != NULL)
569                 BN_CTX_free(new_ctx);
570         return ret;
571         }
572
573
574 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
575         {
576         return BN_is_zero(&point->Z);
577         }
578
579
580 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx);
581 /* TODO */
582
583
584 int ec_GFp_simple_make_affine(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx);
585 /* TODO */
586
587
588 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
589         {
590         return BN_mod_mul(r, a, b, &group->field, ctx);
591         }
592
593
594 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
595         {
596         return BN_mod_sqr(r, a, &group->field, ctx);
597         }