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