]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_lib.c
Call single parent free_comp routine.
[thirdparty/openssl.git] / crypto / ec / ec_lib.c
1 /* crypto/ec/ec_lib.c */
2 /*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5 /* ====================================================================
6 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Binary polynomial ECC support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */
63
64 #include <string.h>
65
66 #include <openssl/err.h>
67 #include <openssl/opensslv.h>
68
69 #include "ec_lcl.h"
70
71 /* functions for EC_GROUP objects */
72
73 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
74 {
75 EC_GROUP *ret;
76
77 if (meth == NULL) {
78 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
79 return NULL;
80 }
81 if (meth->group_init == 0) {
82 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
83 return NULL;
84 }
85
86 ret = OPENSSL_zalloc(sizeof(*ret));
87 if (ret == NULL) {
88 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
89 return NULL;
90 }
91
92 ret->meth = meth;
93 ret->order = BN_new();
94 if (ret->order == NULL)
95 goto err;
96 ret->cofactor = BN_new();
97 if (ret->cofactor == NULL)
98 goto err;
99 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
100 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
101 if (!meth->group_init(ret))
102 goto err;
103 return ret;
104
105 err:
106 BN_free(ret->order);
107 BN_free(ret->cofactor);
108 OPENSSL_free(ret);
109 return NULL;
110 }
111
112 void EC_pre_comp_free(EC_GROUP *group)
113 {
114 switch (group->pre_comp_type) {
115 default:
116 break;
117 #ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
118 case pct_nistz256:
119 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
120 break;
121 #endif
122 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
123 case pct_nistp224:
124 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
125 break;
126 case pct_nistp256:
127 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
128 break;
129 case pct_nistp521:
130 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
131 break;
132 #endif
133 case pct_ec:
134 EC_ec_pre_comp_free(group->pre_comp.ec);
135 break;
136 }
137 group->pre_comp.ec = NULL;
138 }
139
140 void EC_GROUP_free(EC_GROUP *group)
141 {
142 if (!group)
143 return;
144
145 if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
147
148 EC_pre_comp_free(group);
149 BN_MONT_CTX_free(group->mont_data);
150 EC_POINT_free(group->generator);
151 BN_free(group->order);
152 BN_free(group->cofactor);
153 OPENSSL_free(group->seed);
154 OPENSSL_free(group);
155 }
156
157 void EC_GROUP_clear_free(EC_GROUP *group)
158 {
159 if (!group)
160 return;
161
162 if (group->meth->group_clear_finish != 0)
163 group->meth->group_clear_finish(group);
164 else if (group->meth->group_finish != 0)
165 group->meth->group_finish(group);
166
167 EC_pre_comp_free(group);
168 BN_MONT_CTX_free(group->mont_data);
169 EC_POINT_clear_free(group->generator);
170 BN_clear_free(group->order);
171 BN_clear_free(group->cofactor);
172 OPENSSL_clear_free(group->seed, group->seed_len);
173 OPENSSL_clear_free(group, sizeof(*group));
174 }
175
176 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
177 {
178 if (dest->meth->group_copy == 0) {
179 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
180 return 0;
181 }
182 if (dest->meth != src->meth) {
183 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
184 return 0;
185 }
186 if (dest == src)
187 return 1;
188
189 /* Copy precomputed */
190 dest->pre_comp_type = src->pre_comp_type;
191 switch (src->pre_comp_type) {
192 default:
193 dest->pre_comp.ec = NULL;
194 break;
195 #ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
196 case pct_nistz256:
197 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
198 break;
199 #endif
200 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
201 case pct_nistp224:
202 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
203 break;
204 case pct_nistp256:
205 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
206 break;
207 case pct_nistp521:
208 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
209 break;
210 #endif
211 case pct_ec:
212 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
213 break;
214 }
215
216 if (src->mont_data != NULL) {
217 if (dest->mont_data == NULL) {
218 dest->mont_data = BN_MONT_CTX_new();
219 if (dest->mont_data == NULL)
220 return 0;
221 }
222 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
223 return 0;
224 } else {
225 /* src->generator == NULL */
226 BN_MONT_CTX_free(dest->mont_data);
227 dest->mont_data = NULL;
228 }
229
230 if (src->generator != NULL) {
231 if (dest->generator == NULL) {
232 dest->generator = EC_POINT_new(dest);
233 if (dest->generator == NULL)
234 return 0;
235 }
236 if (!EC_POINT_copy(dest->generator, src->generator))
237 return 0;
238 } else {
239 /* src->generator == NULL */
240 EC_POINT_clear_free(dest->generator);
241 dest->generator = NULL;
242 }
243
244 if (!BN_copy(dest->order, src->order))
245 return 0;
246 if (!BN_copy(dest->cofactor, src->cofactor))
247 return 0;
248
249 dest->curve_name = src->curve_name;
250 dest->asn1_flag = src->asn1_flag;
251 dest->asn1_form = src->asn1_form;
252
253 if (src->seed) {
254 OPENSSL_free(dest->seed);
255 dest->seed = OPENSSL_malloc(src->seed_len);
256 if (dest->seed == NULL)
257 return 0;
258 if (!memcpy(dest->seed, src->seed, src->seed_len))
259 return 0;
260 dest->seed_len = src->seed_len;
261 } else {
262 OPENSSL_free(dest->seed);
263 dest->seed = NULL;
264 dest->seed_len = 0;
265 }
266
267 return dest->meth->group_copy(dest, src);
268 }
269
270 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
271 {
272 EC_GROUP *t = NULL;
273 int ok = 0;
274
275 if (a == NULL)
276 return NULL;
277
278 if ((t = EC_GROUP_new(a->meth)) == NULL)
279 return (NULL);
280 if (!EC_GROUP_copy(t, a))
281 goto err;
282
283 ok = 1;
284
285 err:
286 if (!ok) {
287 EC_GROUP_free(t);
288 return NULL;
289 }
290 return t;
291 }
292
293 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
294 {
295 return group->meth;
296 }
297
298 int EC_METHOD_get_field_type(const EC_METHOD *meth)
299 {
300 return meth->field_type;
301 }
302
303 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
304 const BIGNUM *order, const BIGNUM *cofactor)
305 {
306 if (generator == NULL) {
307 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
308 return 0;
309 }
310
311 if (group->generator == NULL) {
312 group->generator = EC_POINT_new(group);
313 if (group->generator == NULL)
314 return 0;
315 }
316 if (!EC_POINT_copy(group->generator, generator))
317 return 0;
318
319 if (order != NULL) {
320 if (!BN_copy(group->order, order))
321 return 0;
322 } else
323 BN_zero(group->order);
324
325 if (cofactor != NULL) {
326 if (!BN_copy(group->cofactor, cofactor))
327 return 0;
328 } else
329 BN_zero(group->cofactor);
330
331 /*
332 * We ignore the return value because some groups have an order with
333 * factors of two, which makes the Montgomery setup fail.
334 * |group->mont_data| will be NULL in this case.
335 */
336 ec_precompute_mont_data(group);
337
338 return 1;
339 }
340
341 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
342 {
343 return group->generator;
344 }
345
346 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
347 {
348 return group->mont_data;
349 }
350
351 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
352 {
353 if (!BN_copy(order, group->order))
354 return 0;
355
356 return !BN_is_zero(order);
357 }
358
359 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
360 BN_CTX *ctx)
361 {
362 if (!BN_copy(cofactor, group->cofactor))
363 return 0;
364
365 return !BN_is_zero(group->cofactor);
366 }
367
368 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
369 {
370 group->curve_name = nid;
371 }
372
373 int EC_GROUP_get_curve_name(const EC_GROUP *group)
374 {
375 return group->curve_name;
376 }
377
378 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
379 {
380 group->asn1_flag = flag;
381 }
382
383 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
384 {
385 return group->asn1_flag;
386 }
387
388 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
389 point_conversion_form_t form)
390 {
391 group->asn1_form = form;
392 }
393
394 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
395 *group)
396 {
397 return group->asn1_form;
398 }
399
400 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
401 {
402 OPENSSL_free(group->seed);
403 group->seed = NULL;
404 group->seed_len = 0;
405
406 if (!len || !p)
407 return 1;
408
409 if ((group->seed = OPENSSL_malloc(len)) == NULL)
410 return 0;
411 memcpy(group->seed, p, len);
412 group->seed_len = len;
413
414 return len;
415 }
416
417 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
418 {
419 return group->seed;
420 }
421
422 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
423 {
424 return group->seed_len;
425 }
426
427 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
428 const BIGNUM *b, BN_CTX *ctx)
429 {
430 if (group->meth->group_set_curve == 0) {
431 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
432 return 0;
433 }
434 return group->meth->group_set_curve(group, p, a, b, ctx);
435 }
436
437 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
438 BIGNUM *b, BN_CTX *ctx)
439 {
440 if (group->meth->group_get_curve == 0) {
441 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
442 return 0;
443 }
444 return group->meth->group_get_curve(group, p, a, b, ctx);
445 }
446
447 #ifndef OPENSSL_NO_EC2M
448 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
449 const BIGNUM *b, BN_CTX *ctx)
450 {
451 if (group->meth->group_set_curve == 0) {
452 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
453 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
454 return 0;
455 }
456 return group->meth->group_set_curve(group, p, a, b, ctx);
457 }
458
459 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
460 BIGNUM *b, BN_CTX *ctx)
461 {
462 if (group->meth->group_get_curve == 0) {
463 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
464 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
465 return 0;
466 }
467 return group->meth->group_get_curve(group, p, a, b, ctx);
468 }
469 #endif
470
471 int EC_GROUP_get_degree(const EC_GROUP *group)
472 {
473 if (group->meth->group_get_degree == 0) {
474 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
475 return 0;
476 }
477 return group->meth->group_get_degree(group);
478 }
479
480 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
481 {
482 if (group->meth->group_check_discriminant == 0) {
483 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
484 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
485 return 0;
486 }
487 return group->meth->group_check_discriminant(group, ctx);
488 }
489
490 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
491 {
492 int r = 0;
493 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
494 BN_CTX *ctx_new = NULL;
495
496 /* compare the field types */
497 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
498 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
499 return 1;
500 /* compare the curve name (if present in both) */
501 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
502 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
503 return 1;
504
505 if (ctx == NULL)
506 ctx_new = ctx = BN_CTX_new();
507 if (ctx == NULL)
508 return -1;
509
510 BN_CTX_start(ctx);
511 a1 = BN_CTX_get(ctx);
512 a2 = BN_CTX_get(ctx);
513 a3 = BN_CTX_get(ctx);
514 b1 = BN_CTX_get(ctx);
515 b2 = BN_CTX_get(ctx);
516 b3 = BN_CTX_get(ctx);
517 if (b3 == NULL) {
518 BN_CTX_end(ctx);
519 BN_CTX_free(ctx_new);
520 return -1;
521 }
522
523 /*
524 * XXX This approach assumes that the external representation of curves
525 * over the same field type is the same.
526 */
527 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
528 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
529 r = 1;
530
531 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
532 r = 1;
533
534 /* XXX EC_POINT_cmp() assumes that the methods are equal */
535 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
536 EC_GROUP_get0_generator(b), ctx))
537 r = 1;
538
539 if (!r) {
540 /* compare the order and cofactor */
541 if (!EC_GROUP_get_order(a, a1, ctx) ||
542 !EC_GROUP_get_order(b, b1, ctx) ||
543 !EC_GROUP_get_cofactor(a, a2, ctx) ||
544 !EC_GROUP_get_cofactor(b, b2, ctx)) {
545 BN_CTX_end(ctx);
546 BN_CTX_free(ctx_new);
547 return -1;
548 }
549 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
550 r = 1;
551 }
552
553 BN_CTX_end(ctx);
554 BN_CTX_free(ctx_new);
555
556 return r;
557 }
558
559 /* functions for EC_POINT objects */
560
561 EC_POINT *EC_POINT_new(const EC_GROUP *group)
562 {
563 EC_POINT *ret;
564
565 if (group == NULL) {
566 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
567 return NULL;
568 }
569 if (group->meth->point_init == 0) {
570 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
571 return NULL;
572 }
573
574 ret = OPENSSL_malloc(sizeof(*ret));
575 if (ret == NULL) {
576 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
577 return NULL;
578 }
579
580 ret->meth = group->meth;
581
582 if (!ret->meth->point_init(ret)) {
583 OPENSSL_free(ret);
584 return NULL;
585 }
586
587 return ret;
588 }
589
590 void EC_POINT_free(EC_POINT *point)
591 {
592 if (!point)
593 return;
594
595 if (point->meth->point_finish != 0)
596 point->meth->point_finish(point);
597 OPENSSL_free(point);
598 }
599
600 void EC_POINT_clear_free(EC_POINT *point)
601 {
602 if (!point)
603 return;
604
605 if (point->meth->point_clear_finish != 0)
606 point->meth->point_clear_finish(point);
607 else if (point->meth->point_finish != 0)
608 point->meth->point_finish(point);
609 OPENSSL_clear_free(point, sizeof(*point));
610 }
611
612 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
613 {
614 if (dest->meth->point_copy == 0) {
615 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
616 return 0;
617 }
618 if (dest->meth != src->meth) {
619 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
620 return 0;
621 }
622 if (dest == src)
623 return 1;
624 return dest->meth->point_copy(dest, src);
625 }
626
627 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
628 {
629 EC_POINT *t;
630 int r;
631
632 if (a == NULL)
633 return NULL;
634
635 t = EC_POINT_new(group);
636 if (t == NULL)
637 return (NULL);
638 r = EC_POINT_copy(t, a);
639 if (!r) {
640 EC_POINT_free(t);
641 return NULL;
642 }
643 return t;
644 }
645
646 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
647 {
648 return point->meth;
649 }
650
651 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
652 {
653 if (group->meth->point_set_to_infinity == 0) {
654 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
655 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
656 return 0;
657 }
658 if (group->meth != point->meth) {
659 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
660 return 0;
661 }
662 return group->meth->point_set_to_infinity(group, point);
663 }
664
665 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
666 EC_POINT *point, const BIGNUM *x,
667 const BIGNUM *y, const BIGNUM *z,
668 BN_CTX *ctx)
669 {
670 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
671 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
672 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
673 return 0;
674 }
675 if (group->meth != point->meth) {
676 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
677 EC_R_INCOMPATIBLE_OBJECTS);
678 return 0;
679 }
680 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
681 y, z, ctx);
682 }
683
684 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
685 const EC_POINT *point, BIGNUM *x,
686 BIGNUM *y, BIGNUM *z,
687 BN_CTX *ctx)
688 {
689 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
690 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
691 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
692 return 0;
693 }
694 if (group->meth != point->meth) {
695 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
696 EC_R_INCOMPATIBLE_OBJECTS);
697 return 0;
698 }
699 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
700 y, z, ctx);
701 }
702
703 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
704 EC_POINT *point, const BIGNUM *x,
705 const BIGNUM *y, BN_CTX *ctx)
706 {
707 if (group->meth->point_set_affine_coordinates == 0) {
708 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
709 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
710 return 0;
711 }
712 if (group->meth != point->meth) {
713 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
714 EC_R_INCOMPATIBLE_OBJECTS);
715 return 0;
716 }
717 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
718 }
719
720 #ifndef OPENSSL_NO_EC2M
721 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
722 EC_POINT *point, const BIGNUM *x,
723 const BIGNUM *y, BN_CTX *ctx)
724 {
725 if (group->meth->point_set_affine_coordinates == 0) {
726 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
727 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
728 return 0;
729 }
730 if (group->meth != point->meth) {
731 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
732 EC_R_INCOMPATIBLE_OBJECTS);
733 return 0;
734 }
735 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
736 }
737 #endif
738
739 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
740 const EC_POINT *point, BIGNUM *x,
741 BIGNUM *y, BN_CTX *ctx)
742 {
743 if (group->meth->point_get_affine_coordinates == 0) {
744 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
745 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
746 return 0;
747 }
748 if (group->meth != point->meth) {
749 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
750 EC_R_INCOMPATIBLE_OBJECTS);
751 return 0;
752 }
753 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
754 }
755
756 #ifndef OPENSSL_NO_EC2M
757 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
758 const EC_POINT *point, BIGNUM *x,
759 BIGNUM *y, BN_CTX *ctx)
760 {
761 if (group->meth->point_get_affine_coordinates == 0) {
762 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
763 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
764 return 0;
765 }
766 if (group->meth != point->meth) {
767 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
768 EC_R_INCOMPATIBLE_OBJECTS);
769 return 0;
770 }
771 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
772 }
773 #endif
774
775 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
776 const EC_POINT *b, BN_CTX *ctx)
777 {
778 if (group->meth->add == 0) {
779 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
780 return 0;
781 }
782 if ((group->meth != r->meth) || (r->meth != a->meth)
783 || (a->meth != b->meth)) {
784 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
785 return 0;
786 }
787 return group->meth->add(group, r, a, b, ctx);
788 }
789
790 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
791 BN_CTX *ctx)
792 {
793 if (group->meth->dbl == 0) {
794 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
795 return 0;
796 }
797 if ((group->meth != r->meth) || (r->meth != a->meth)) {
798 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
799 return 0;
800 }
801 return group->meth->dbl(group, r, a, ctx);
802 }
803
804 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
805 {
806 if (group->meth->invert == 0) {
807 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
808 return 0;
809 }
810 if (group->meth != a->meth) {
811 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
812 return 0;
813 }
814 return group->meth->invert(group, a, ctx);
815 }
816
817 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
818 {
819 if (group->meth->is_at_infinity == 0) {
820 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
821 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
822 return 0;
823 }
824 if (group->meth != point->meth) {
825 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
826 return 0;
827 }
828 return group->meth->is_at_infinity(group, point);
829 }
830
831 /*
832 * Check whether an EC_POINT is on the curve or not. Note that the return
833 * value for this function should NOT be treated as a boolean. Return values:
834 * 1: The point is on the curve
835 * 0: The point is not on the curve
836 * -1: An error occurred
837 */
838 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
839 BN_CTX *ctx)
840 {
841 if (group->meth->is_on_curve == 0) {
842 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
843 return 0;
844 }
845 if (group->meth != point->meth) {
846 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
847 return 0;
848 }
849 return group->meth->is_on_curve(group, point, ctx);
850 }
851
852 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
853 BN_CTX *ctx)
854 {
855 if (group->meth->point_cmp == 0) {
856 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
857 return -1;
858 }
859 if ((group->meth != a->meth) || (a->meth != b->meth)) {
860 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
861 return -1;
862 }
863 return group->meth->point_cmp(group, a, b, ctx);
864 }
865
866 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
867 {
868 if (group->meth->make_affine == 0) {
869 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
870 return 0;
871 }
872 if (group->meth != point->meth) {
873 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
874 return 0;
875 }
876 return group->meth->make_affine(group, point, ctx);
877 }
878
879 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
880 EC_POINT *points[], BN_CTX *ctx)
881 {
882 size_t i;
883
884 if (group->meth->points_make_affine == 0) {
885 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
886 return 0;
887 }
888 for (i = 0; i < num; i++) {
889 if (group->meth != points[i]->meth) {
890 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
891 return 0;
892 }
893 }
894 return group->meth->points_make_affine(group, num, points, ctx);
895 }
896
897 /*
898 * Functions for point multiplication. If group->meth->mul is 0, we use the
899 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
900 * methods.
901 */
902
903 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
904 size_t num, const EC_POINT *points[],
905 const BIGNUM *scalars[], BN_CTX *ctx)
906 {
907 if (group->meth->mul == 0)
908 /* use default */
909 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
910
911 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
912 }
913
914 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
915 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
916 {
917 /* just a convenient interface to EC_POINTs_mul() */
918
919 const EC_POINT *points[1];
920 const BIGNUM *scalars[1];
921
922 points[0] = point;
923 scalars[0] = p_scalar;
924
925 return EC_POINTs_mul(group, r, g_scalar,
926 (point != NULL
927 && p_scalar != NULL), points, scalars, ctx);
928 }
929
930 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
931 {
932 if (group->meth->mul == 0)
933 /* use default */
934 return ec_wNAF_precompute_mult(group, ctx);
935
936 if (group->meth->precompute_mult != 0)
937 return group->meth->precompute_mult(group, ctx);
938 else
939 return 1; /* nothing to do, so report success */
940 }
941
942 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
943 {
944 if (group->meth->mul == 0)
945 /* use default */
946 return ec_wNAF_have_precompute_mult(group);
947
948 if (group->meth->have_precompute_mult != 0)
949 return group->meth->have_precompute_mult(group);
950 else
951 return 0; /* cannot tell whether precomputation has
952 * been performed */
953 }
954
955 /*
956 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
957 * returns one on success. On error it returns zero.
958 */
959 int ec_precompute_mont_data(EC_GROUP *group)
960 {
961 BN_CTX *ctx = BN_CTX_new();
962 int ret = 0;
963
964 BN_MONT_CTX_free(group->mont_data);
965 group->mont_data = NULL;
966
967 if (ctx == NULL)
968 goto err;
969
970 group->mont_data = BN_MONT_CTX_new();
971 if (group->mont_data == NULL)
972 goto err;
973
974 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
975 BN_MONT_CTX_free(group->mont_data);
976 group->mont_data = NULL;
977 goto err;
978 }
979
980 ret = 1;
981
982 err:
983
984 BN_CTX_free(ctx);
985 return ret;
986 }
987
988 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
989 {
990 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
991 }
992
993 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
994 {
995 return CRYPTO_get_ex_data(&key->ex_data, idx);
996 }