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