summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ec_lib.c')
-rw-r--r--src/lib/libcrypto/ec/ec_lib.c1109
1 files changed, 0 insertions, 1109 deletions
diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c
deleted file mode 100644
index 8cf0f2241e..0000000000
--- a/src/lib/libcrypto/ec/ec_lib.c
+++ /dev/null
@@ -1,1109 +0,0 @@
1/* $OpenBSD: ec_lib.c,v 1.16 2015/02/09 15:49:22 jsing Exp $ */
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/opensslconf.h>
67
68#include <openssl/err.h>
69#include <openssl/opensslv.h>
70
71#include "ec_lcl.h"
72
73/* functions for EC_GROUP objects */
74
75EC_GROUP *
76EC_GROUP_new(const EC_METHOD * meth)
77{
78 EC_GROUP *ret;
79
80 if (meth == NULL) {
81 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
82 return NULL;
83 }
84 if (meth->group_init == 0) {
85 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
86 return NULL;
87 }
88 ret = malloc(sizeof *ret);
89 if (ret == NULL) {
90 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
91 return NULL;
92 }
93 ret->meth = meth;
94
95 ret->extra_data = NULL;
96
97 ret->generator = NULL;
98 BN_init(&ret->order);
99 BN_init(&ret->cofactor);
100
101 ret->curve_name = 0;
102 ret->asn1_flag = 0;
103 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
104
105 ret->seed = NULL;
106 ret->seed_len = 0;
107
108 if (!meth->group_init(ret)) {
109 free(ret);
110 return NULL;
111 }
112 return ret;
113}
114
115
116void
117EC_GROUP_free(EC_GROUP * group)
118{
119 if (!group)
120 return;
121
122 if (group->meth->group_finish != 0)
123 group->meth->group_finish(group);
124
125 EC_EX_DATA_free_all_data(&group->extra_data);
126
127 EC_POINT_free(group->generator);
128 BN_free(&group->order);
129 BN_free(&group->cofactor);
130
131 free(group->seed);
132
133 free(group);
134}
135
136
137void
138EC_GROUP_clear_free(EC_GROUP * group)
139{
140 if (!group)
141 return;
142
143 if (group->meth->group_clear_finish != 0)
144 group->meth->group_clear_finish(group);
145 else if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
147
148 EC_EX_DATA_clear_free_all_data(&group->extra_data);
149
150 EC_POINT_clear_free(group->generator);
151 BN_clear_free(&group->order);
152 BN_clear_free(&group->cofactor);
153
154 if (group->seed) {
155 OPENSSL_cleanse(group->seed, group->seed_len);
156 free(group->seed);
157 }
158 OPENSSL_cleanse(group, sizeof *group);
159 free(group);
160}
161
162
163int
164EC_GROUP_copy(EC_GROUP * dest, const EC_GROUP * src)
165{
166 EC_EXTRA_DATA *d;
167
168 if (dest->meth->group_copy == 0) {
169 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
170 return 0;
171 }
172 if (dest->meth != src->meth) {
173 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
174 return 0;
175 }
176 if (dest == src)
177 return 1;
178
179 EC_EX_DATA_free_all_data(&dest->extra_data);
180
181 for (d = src->extra_data; d != NULL; d = d->next) {
182 void *t = d->dup_func(d->data);
183
184 if (t == NULL)
185 return 0;
186 if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func,
187 d->free_func, d->clear_free_func))
188 return 0;
189 }
190
191 if (src->generator != NULL) {
192 if (dest->generator == NULL) {
193 dest->generator = EC_POINT_new(dest);
194 if (dest->generator == NULL)
195 return 0;
196 }
197 if (!EC_POINT_copy(dest->generator, src->generator))
198 return 0;
199 } else {
200 /* src->generator == NULL */
201 EC_POINT_clear_free(dest->generator);
202 dest->generator = NULL;
203 }
204
205 if (!BN_copy(&dest->order, &src->order))
206 return 0;
207 if (!BN_copy(&dest->cofactor, &src->cofactor))
208 return 0;
209
210 dest->curve_name = src->curve_name;
211 dest->asn1_flag = src->asn1_flag;
212 dest->asn1_form = src->asn1_form;
213
214 if (src->seed) {
215 free(dest->seed);
216 dest->seed = malloc(src->seed_len);
217 if (dest->seed == NULL)
218 return 0;
219 if (!memcpy(dest->seed, src->seed, src->seed_len))
220 return 0;
221 dest->seed_len = src->seed_len;
222 } else {
223 free(dest->seed);
224 dest->seed = NULL;
225 dest->seed_len = 0;
226 }
227
228
229 return dest->meth->group_copy(dest, src);
230}
231
232
233EC_GROUP *
234EC_GROUP_dup(const EC_GROUP * a)
235{
236 EC_GROUP *t = NULL;
237 int ok = 0;
238
239 if (a == NULL)
240 return NULL;
241
242 if ((t = EC_GROUP_new(a->meth)) == NULL)
243 return (NULL);
244 if (!EC_GROUP_copy(t, a))
245 goto err;
246
247 ok = 1;
248
249err:
250 if (!ok) {
251 EC_GROUP_free(t);
252 return NULL;
253 } else
254 return t;
255}
256
257
258const EC_METHOD *
259EC_GROUP_method_of(const EC_GROUP *group)
260{
261 return group->meth;
262}
263
264
265int
266EC_METHOD_get_field_type(const EC_METHOD *meth)
267{
268 return meth->field_type;
269}
270
271
272int
273EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
274 const BIGNUM *order, const BIGNUM *cofactor)
275{
276 if (generator == NULL) {
277 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
278 return 0;
279 }
280 if (group->generator == NULL) {
281 group->generator = EC_POINT_new(group);
282 if (group->generator == NULL)
283 return 0;
284 }
285 if (!EC_POINT_copy(group->generator, generator))
286 return 0;
287
288 if (order != NULL) {
289 if (!BN_copy(&group->order, order))
290 return 0;
291 } else
292 BN_zero(&group->order);
293
294 if (cofactor != NULL) {
295 if (!BN_copy(&group->cofactor, cofactor))
296 return 0;
297 } else
298 BN_zero(&group->cofactor);
299
300 return 1;
301}
302
303
304const EC_POINT *
305EC_GROUP_get0_generator(const EC_GROUP *group)
306{
307 return group->generator;
308}
309
310
311int
312EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
313{
314 if (!BN_copy(order, &group->order))
315 return 0;
316
317 return !BN_is_zero(order);
318}
319
320
321int
322EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
323{
324 if (!BN_copy(cofactor, &group->cofactor))
325 return 0;
326
327 return !BN_is_zero(&group->cofactor);
328}
329
330
331void
332EC_GROUP_set_curve_name(EC_GROUP * group, int nid)
333{
334 group->curve_name = nid;
335}
336
337
338int
339EC_GROUP_get_curve_name(const EC_GROUP * group)
340{
341 return group->curve_name;
342}
343
344
345void
346EC_GROUP_set_asn1_flag(EC_GROUP * group, int flag)
347{
348 group->asn1_flag = flag;
349}
350
351
352int
353EC_GROUP_get_asn1_flag(const EC_GROUP * group)
354{
355 return group->asn1_flag;
356}
357
358
359void
360EC_GROUP_set_point_conversion_form(EC_GROUP * group,
361 point_conversion_form_t form)
362{
363 group->asn1_form = form;
364}
365
366
367point_conversion_form_t
368EC_GROUP_get_point_conversion_form(const EC_GROUP * group)
369{
370 return group->asn1_form;
371}
372
373
374size_t
375EC_GROUP_set_seed(EC_GROUP * group, const unsigned char *p, size_t len)
376{
377 if (group->seed) {
378 free(group->seed);
379 group->seed = NULL;
380 group->seed_len = 0;
381 }
382 if (!len || !p)
383 return 1;
384
385 if ((group->seed = malloc(len)) == NULL)
386 return 0;
387 memcpy(group->seed, p, len);
388 group->seed_len = len;
389
390 return len;
391}
392
393
394unsigned char *
395EC_GROUP_get0_seed(const EC_GROUP * group)
396{
397 return group->seed;
398}
399
400
401size_t
402EC_GROUP_get_seed_len(const EC_GROUP * group)
403{
404 return group->seed_len;
405}
406
407
408int
409EC_GROUP_set_curve_GFp(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
410 const BIGNUM * b, BN_CTX * ctx)
411{
412 if (group->meth->group_set_curve == 0) {
413 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
414 return 0;
415 }
416 return group->meth->group_set_curve(group, p, a, b, ctx);
417}
418
419
420int
421EC_GROUP_get_curve_GFp(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
422 BIGNUM * b, BN_CTX * ctx)
423{
424 if (group->meth->group_get_curve == 0) {
425 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
426 return 0;
427 }
428 return group->meth->group_get_curve(group, p, a, b, ctx);
429}
430
431#ifndef OPENSSL_NO_EC2M
432int
433EC_GROUP_set_curve_GF2m(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
434 const BIGNUM * b, BN_CTX * ctx)
435{
436 if (group->meth->group_set_curve == 0) {
437 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
438 return 0;
439 }
440 return group->meth->group_set_curve(group, p, a, b, ctx);
441}
442
443
444int
445EC_GROUP_get_curve_GF2m(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
446 BIGNUM * b, BN_CTX * ctx)
447{
448 if (group->meth->group_get_curve == 0) {
449 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
450 return 0;
451 }
452 return group->meth->group_get_curve(group, p, a, b, ctx);
453}
454#endif
455
456int
457EC_GROUP_get_degree(const EC_GROUP * group)
458{
459 if (group->meth->group_get_degree == 0) {
460 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
461 return 0;
462 }
463 return group->meth->group_get_degree(group);
464}
465
466
467int
468EC_GROUP_check_discriminant(const EC_GROUP * group, BN_CTX * ctx)
469{
470 if (group->meth->group_check_discriminant == 0) {
471 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
472 return 0;
473 }
474 return group->meth->group_check_discriminant(group, ctx);
475}
476
477
478int
479EC_GROUP_cmp(const EC_GROUP * a, const EC_GROUP * b, BN_CTX * ctx)
480{
481 int r = 0;
482 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
483 BN_CTX *ctx_new = NULL;
484
485 /* compare the field types */
486 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
487 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
488 return 1;
489 /* compare the curve name (if present in both) */
490 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
491 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
492 return 1;
493
494 if (!ctx)
495 ctx_new = ctx = BN_CTX_new();
496 if (!ctx)
497 return -1;
498
499 BN_CTX_start(ctx);
500 if ((a1 = BN_CTX_get(ctx)) == NULL)
501 goto err;
502 if ((a2 = BN_CTX_get(ctx)) == NULL)
503 goto err;
504 if ((a3 = BN_CTX_get(ctx)) == NULL)
505 goto err;
506 if ((b1 = BN_CTX_get(ctx)) == NULL)
507 goto err;
508 if ((b2 = BN_CTX_get(ctx)) == NULL)
509 goto err;
510 if ((b3 = BN_CTX_get(ctx)) == NULL)
511 goto err;
512
513 /*
514 * XXX This approach assumes that the external representation of
515 * curves over the same field type is the same.
516 */
517 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
518 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
519 r = 1;
520
521 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
522 r = 1;
523
524 /* XXX EC_POINT_cmp() assumes that the methods are equal */
525 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
526 EC_GROUP_get0_generator(b), ctx))
527 r = 1;
528
529 if (!r) {
530 /* compare the order and cofactor */
531 if (!EC_GROUP_get_order(a, a1, ctx) ||
532 !EC_GROUP_get_order(b, b1, ctx) ||
533 !EC_GROUP_get_cofactor(a, a2, ctx) ||
534 !EC_GROUP_get_cofactor(b, b2, ctx)) {
535 BN_CTX_end(ctx);
536 if (ctx_new)
537 BN_CTX_free(ctx);
538 return -1;
539 }
540 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
541 r = 1;
542 }
543 BN_CTX_end(ctx);
544 if (ctx_new)
545 BN_CTX_free(ctx);
546
547 return r;
548
549err:
550 BN_CTX_end(ctx);
551 if (ctx_new)
552 BN_CTX_free(ctx);
553 return -1;
554}
555
556
557/* this has 'package' visibility */
558int
559EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data, void *data,
560 void *(*dup_func) (void *),
561 void (*free_func) (void *),
562 void (*clear_free_func) (void *))
563{
564 EC_EXTRA_DATA *d;
565
566 if (ex_data == NULL)
567 return 0;
568
569 for (d = *ex_data; d != NULL; d = d->next) {
570 if (d->dup_func == dup_func && d->free_func == free_func &&
571 d->clear_free_func == clear_free_func) {
572 ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
573 return 0;
574 }
575 }
576
577 if (data == NULL)
578 /* no explicit entry needed */
579 return 1;
580
581 d = malloc(sizeof *d);
582 if (d == NULL)
583 return 0;
584
585 d->data = data;
586 d->dup_func = dup_func;
587 d->free_func = free_func;
588 d->clear_free_func = clear_free_func;
589
590 d->next = *ex_data;
591 *ex_data = d;
592
593 return 1;
594}
595
596/* this has 'package' visibility */
597void *
598EC_EX_DATA_get_data(const EC_EXTRA_DATA * ex_data,
599 void *(*dup_func) (void *),
600 void (*free_func) (void *),
601 void (*clear_free_func) (void *))
602{
603 const EC_EXTRA_DATA *d;
604
605 for (d = ex_data; d != NULL; d = d->next) {
606 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
607 return d->data;
608 }
609
610 return NULL;
611}
612
613/* this has 'package' visibility */
614void
615EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,
616 void *(*dup_func) (void *),
617 void (*free_func) (void *),
618 void (*clear_free_func) (void *))
619{
620 EC_EXTRA_DATA **p;
621
622 if (ex_data == NULL)
623 return;
624
625 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
626 if ((*p)->dup_func == dup_func &&
627 (*p)->free_func == free_func &&
628 (*p)->clear_free_func == clear_free_func) {
629 EC_EXTRA_DATA *next = (*p)->next;
630
631 (*p)->free_func((*p)->data);
632 free(*p);
633
634 *p = next;
635 return;
636 }
637 }
638}
639
640/* this has 'package' visibility */
641void
642EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,
643 void *(*dup_func) (void *),
644 void (*free_func) (void *),
645 void (*clear_free_func) (void *))
646{
647 EC_EXTRA_DATA **p;
648
649 if (ex_data == NULL)
650 return;
651
652 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
653 if ((*p)->dup_func == dup_func &&
654 (*p)->free_func == free_func &&
655 (*p)->clear_free_func == clear_free_func) {
656 EC_EXTRA_DATA *next = (*p)->next;
657
658 (*p)->clear_free_func((*p)->data);
659 free(*p);
660
661 *p = next;
662 return;
663 }
664 }
665}
666
667/* this has 'package' visibility */
668void
669EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)
670{
671 EC_EXTRA_DATA *d;
672
673 if (ex_data == NULL)
674 return;
675
676 d = *ex_data;
677 while (d) {
678 EC_EXTRA_DATA *next = d->next;
679
680 d->free_func(d->data);
681 free(d);
682
683 d = next;
684 }
685 *ex_data = NULL;
686}
687
688/* this has 'package' visibility */
689void
690EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)
691{
692 EC_EXTRA_DATA *d;
693
694 if (ex_data == NULL)
695 return;
696
697 d = *ex_data;
698 while (d) {
699 EC_EXTRA_DATA *next = d->next;
700
701 d->clear_free_func(d->data);
702 free(d);
703
704 d = next;
705 }
706 *ex_data = NULL;
707}
708
709
710/* functions for EC_POINT objects */
711
712EC_POINT *
713EC_POINT_new(const EC_GROUP * group)
714{
715 EC_POINT *ret;
716
717 if (group == NULL) {
718 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
719 return NULL;
720 }
721 if (group->meth->point_init == 0) {
722 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
723 return NULL;
724 }
725 ret = malloc(sizeof *ret);
726 if (ret == NULL) {
727 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
728 return NULL;
729 }
730 ret->meth = group->meth;
731
732 if (!ret->meth->point_init(ret)) {
733 free(ret);
734 return NULL;
735 }
736 return ret;
737}
738
739
740void
741EC_POINT_free(EC_POINT * point)
742{
743 if (!point)
744 return;
745
746 if (point->meth->point_finish != 0)
747 point->meth->point_finish(point);
748 free(point);
749}
750
751
752void
753EC_POINT_clear_free(EC_POINT * point)
754{
755 if (!point)
756 return;
757
758 if (point->meth->point_clear_finish != 0)
759 point->meth->point_clear_finish(point);
760 else if (point->meth->point_finish != 0)
761 point->meth->point_finish(point);
762 OPENSSL_cleanse(point, sizeof *point);
763 free(point);
764}
765
766
767int
768EC_POINT_copy(EC_POINT * dest, const EC_POINT * src)
769{
770 if (dest->meth->point_copy == 0) {
771 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
772 return 0;
773 }
774 if (dest->meth != src->meth) {
775 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
776 return 0;
777 }
778 if (dest == src)
779 return 1;
780 return dest->meth->point_copy(dest, src);
781}
782
783
784EC_POINT *
785EC_POINT_dup(const EC_POINT * a, const EC_GROUP * group)
786{
787 EC_POINT *t;
788 int r;
789
790 if (a == NULL)
791 return NULL;
792
793 t = EC_POINT_new(group);
794 if (t == NULL)
795 return (NULL);
796 r = EC_POINT_copy(t, a);
797 if (!r) {
798 EC_POINT_free(t);
799 return NULL;
800 } else
801 return t;
802}
803
804
805const EC_METHOD *
806EC_POINT_method_of(const EC_POINT * point)
807{
808 return point->meth;
809}
810
811
812int
813EC_POINT_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
814{
815 if (group->meth->point_set_to_infinity == 0) {
816 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
817 return 0;
818 }
819 if (group->meth != point->meth) {
820 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
821 return 0;
822 }
823 return group->meth->point_set_to_infinity(group, point);
824}
825
826
827int
828EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
829 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
830{
831 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
832 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
833 return 0;
834 }
835 if (group->meth != point->meth) {
836 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
837 return 0;
838 }
839 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
840}
841
842
843int
844EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
845 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
846{
847 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
848 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
849 return 0;
850 }
851 if (group->meth != point->meth) {
852 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
853 return 0;
854 }
855 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
856}
857
858
859int
860EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
861 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
862{
863 if (group->meth->point_set_affine_coordinates == 0) {
864 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
865 return 0;
866 }
867 if (group->meth != point->meth) {
868 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
869 return 0;
870 }
871 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
872}
873
874#ifndef OPENSSL_NO_EC2M
875int
876EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
877 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
878{
879 if (group->meth->point_set_affine_coordinates == 0) {
880 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
881 return 0;
882 }
883 if (group->meth != point->meth) {
884 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
885 return 0;
886 }
887 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
888}
889#endif
890
891int
892EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
893 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
894{
895 if (group->meth->point_get_affine_coordinates == 0) {
896 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
897 return 0;
898 }
899 if (group->meth != point->meth) {
900 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
901 return 0;
902 }
903 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
904}
905
906#ifndef OPENSSL_NO_EC2M
907int
908EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
909 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
910{
911 if (group->meth->point_get_affine_coordinates == 0) {
912 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
913 return 0;
914 }
915 if (group->meth != point->meth) {
916 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
917 return 0;
918 }
919 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
920}
921#endif
922
923int
924EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
925 const EC_POINT *b, BN_CTX *ctx)
926{
927 if (group->meth->add == 0) {
928 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
929 return 0;
930 }
931 if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth)) {
932 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
933 return 0;
934 }
935 return group->meth->add(group, r, a, b, ctx);
936}
937
938
939int
940EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
941{
942 if (group->meth->dbl == 0) {
943 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
944 return 0;
945 }
946 if ((group->meth != r->meth) || (r->meth != a->meth)) {
947 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
948 return 0;
949 }
950 return group->meth->dbl(group, r, a, ctx);
951}
952
953
954int
955EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
956{
957 if (group->meth->invert == 0) {
958 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
959 return 0;
960 }
961 if (group->meth != a->meth) {
962 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
963 return 0;
964 }
965 return group->meth->invert(group, a, ctx);
966}
967
968
969int
970EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
971{
972 if (group->meth->is_at_infinity == 0) {
973 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
974 return 0;
975 }
976 if (group->meth != point->meth) {
977 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
978 return 0;
979 }
980 return group->meth->is_at_infinity(group, point);
981}
982
983
984int
985EC_POINT_is_on_curve(const EC_GROUP * group, const EC_POINT * point, BN_CTX * ctx)
986{
987 if (group->meth->is_on_curve == 0) {
988 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
989 return 0;
990 }
991 if (group->meth != point->meth) {
992 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
993 return 0;
994 }
995 return group->meth->is_on_curve(group, point, ctx);
996}
997
998
999int
1000EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1001 BN_CTX * ctx)
1002{
1003 if (group->meth->point_cmp == 0) {
1004 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1005 return -1;
1006 }
1007 if ((group->meth != a->meth) || (a->meth != b->meth)) {
1008 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1009 return -1;
1010 }
1011 return group->meth->point_cmp(group, a, b, ctx);
1012}
1013
1014
1015int
1016EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1017{
1018 if (group->meth->make_affine == 0) {
1019 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1020 return 0;
1021 }
1022 if (group->meth != point->meth) {
1023 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1024 return 0;
1025 }
1026 return group->meth->make_affine(group, point, ctx);
1027}
1028
1029
1030int
1031EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
1032 BN_CTX *ctx)
1033{
1034 size_t i;
1035
1036 if (group->meth->points_make_affine == 0) {
1037 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1038 return 0;
1039 }
1040 for (i = 0; i < num; i++) {
1041 if (group->meth != points[i]->meth) {
1042 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1043 return 0;
1044 }
1045 }
1046 return group->meth->points_make_affine(group, num, points, ctx);
1047}
1048
1049
1050/* Functions for point multiplication.
1051 *
1052 * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1053 * otherwise we dispatch through methods.
1054 */
1055
1056int
1057EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1058 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1059{
1060 if (group->meth->mul == 0)
1061 /* use default */
1062 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1063
1064 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1065}
1066
1067int
1068EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1069 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1070{
1071 /* just a convenient interface to EC_POINTs_mul() */
1072
1073 const EC_POINT *points[1];
1074 const BIGNUM *scalars[1];
1075
1076 points[0] = point;
1077 scalars[0] = p_scalar;
1078
1079 return EC_POINTs_mul(group, r, g_scalar,
1080 (point != NULL && p_scalar != NULL),
1081 points, scalars, ctx);
1082}
1083
1084int
1085EC_GROUP_precompute_mult(EC_GROUP * group, BN_CTX * ctx)
1086{
1087 if (group->meth->mul == 0)
1088 /* use default */
1089 return ec_wNAF_precompute_mult(group, ctx);
1090
1091 if (group->meth->precompute_mult != 0)
1092 return group->meth->precompute_mult(group, ctx);
1093 else
1094 return 1; /* nothing to do, so report success */
1095}
1096
1097int
1098EC_GROUP_have_precompute_mult(const EC_GROUP * group)
1099{
1100 if (group->meth->mul == 0)
1101 /* use default */
1102 return ec_wNAF_have_precompute_mult(group);
1103
1104 if (group->meth->have_precompute_mult != 0)
1105 return group->meth->have_precompute_mult(group);
1106 else
1107 return 0; /* cannot tell whether precomputation has
1108 * been performed */
1109}