xref: /openbsd/lib/libcrypto/ec/ec_lib.c (revision a6445c1d)
1 /* $OpenBSD: ec_lib.c,v 1.15 2014/07/12 16:03:37 miod 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 		OPENSSL_cleanse(group->seed, group->seed_len);
156 		free(group->seed);
157 	}
158 	OPENSSL_cleanse(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 		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 
233 EC_GROUP *
234 EC_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 
249 err:
250 	if (!ok) {
251 		EC_GROUP_free(t);
252 		return NULL;
253 	} else
254 		return t;
255 }
256 
257 
258 const EC_METHOD *
259 EC_GROUP_method_of(const EC_GROUP *group)
260 {
261 	return group->meth;
262 }
263 
264 
265 int
266 EC_METHOD_get_field_type(const EC_METHOD *meth)
267 {
268 	return meth->field_type;
269 }
270 
271 
272 int
273 EC_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 
304 const EC_POINT *
305 EC_GROUP_get0_generator(const EC_GROUP *group)
306 {
307 	return group->generator;
308 }
309 
310 
311 int
312 EC_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 
321 int
322 EC_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 
331 void
332 EC_GROUP_set_curve_name(EC_GROUP * group, int nid)
333 {
334 	group->curve_name = nid;
335 }
336 
337 
338 int
339 EC_GROUP_get_curve_name(const EC_GROUP * group)
340 {
341 	return group->curve_name;
342 }
343 
344 
345 void
346 EC_GROUP_set_asn1_flag(EC_GROUP * group, int flag)
347 {
348 	group->asn1_flag = flag;
349 }
350 
351 
352 int
353 EC_GROUP_get_asn1_flag(const EC_GROUP * group)
354 {
355 	return group->asn1_flag;
356 }
357 
358 
359 void
360 EC_GROUP_set_point_conversion_form(EC_GROUP * group,
361     point_conversion_form_t form)
362 {
363 	group->asn1_form = form;
364 }
365 
366 
367 point_conversion_form_t
368 EC_GROUP_get_point_conversion_form(const EC_GROUP * group)
369 {
370 	return group->asn1_form;
371 }
372 
373 
374 size_t
375 EC_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 
394 unsigned char *
395 EC_GROUP_get0_seed(const EC_GROUP * group)
396 {
397 	return group->seed;
398 }
399 
400 
401 size_t
402 EC_GROUP_get_seed_len(const EC_GROUP * group)
403 {
404 	return group->seed_len;
405 }
406 
407 
408 int
409 EC_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 
420 int
421 EC_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
432 int
433 EC_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 
444 int
445 EC_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 
456 int
457 EC_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 
467 int
468 EC_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 
478 int
479 EC_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 	a1 = BN_CTX_get(ctx);
501 	a2 = BN_CTX_get(ctx);
502 	a3 = BN_CTX_get(ctx);
503 	b1 = BN_CTX_get(ctx);
504 	b2 = BN_CTX_get(ctx);
505 	b3 = BN_CTX_get(ctx);
506 	if (!b3) {
507 		BN_CTX_end(ctx);
508 		if (ctx_new)
509 			BN_CTX_free(ctx);
510 		return -1;
511 	}
512 	/*
513 	 * XXX This approach assumes that the external representation of
514 	 * curves over the same field type is the same.
515 	 */
516 	if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
517 	    !b->meth->group_get_curve(b, b1, b2, b3, ctx))
518 		r = 1;
519 
520 	if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
521 		r = 1;
522 
523 	/* XXX EC_POINT_cmp() assumes that the methods are equal */
524 	if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
525 		EC_GROUP_get0_generator(b), ctx))
526 		r = 1;
527 
528 	if (!r) {
529 		/* compare the order and cofactor */
530 		if (!EC_GROUP_get_order(a, a1, ctx) ||
531 		    !EC_GROUP_get_order(b, b1, ctx) ||
532 		    !EC_GROUP_get_cofactor(a, a2, ctx) ||
533 		    !EC_GROUP_get_cofactor(b, b2, ctx)) {
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 	BN_CTX_end(ctx);
543 	if (ctx_new)
544 		BN_CTX_free(ctx);
545 
546 	return r;
547 }
548 
549 
550 /* this has 'package' visibility */
551 int
552 EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data, void *data,
553     void *(*dup_func) (void *),
554     void (*free_func) (void *),
555     void (*clear_free_func) (void *))
556 {
557 	EC_EXTRA_DATA *d;
558 
559 	if (ex_data == NULL)
560 		return 0;
561 
562 	for (d = *ex_data; d != NULL; d = d->next) {
563 		if (d->dup_func == dup_func && d->free_func == free_func &&
564 		    d->clear_free_func == clear_free_func) {
565 			ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
566 			return 0;
567 		}
568 	}
569 
570 	if (data == NULL)
571 		/* no explicit entry needed */
572 		return 1;
573 
574 	d = malloc(sizeof *d);
575 	if (d == NULL)
576 		return 0;
577 
578 	d->data = data;
579 	d->dup_func = dup_func;
580 	d->free_func = free_func;
581 	d->clear_free_func = clear_free_func;
582 
583 	d->next = *ex_data;
584 	*ex_data = d;
585 
586 	return 1;
587 }
588 
589 /* this has 'package' visibility */
590 void *
591 EC_EX_DATA_get_data(const EC_EXTRA_DATA * ex_data,
592     void *(*dup_func) (void *),
593     void (*free_func) (void *),
594     void (*clear_free_func) (void *))
595 {
596 	const EC_EXTRA_DATA *d;
597 
598 	for (d = ex_data; d != NULL; d = d->next) {
599 		if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
600 			return d->data;
601 	}
602 
603 	return NULL;
604 }
605 
606 /* this has 'package' visibility */
607 void
608 EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,
609     void *(*dup_func) (void *),
610     void (*free_func) (void *),
611     void (*clear_free_func) (void *))
612 {
613 	EC_EXTRA_DATA **p;
614 
615 	if (ex_data == NULL)
616 		return;
617 
618 	for (p = ex_data; *p != NULL; p = &((*p)->next)) {
619 		if ((*p)->dup_func == dup_func &&
620 		    (*p)->free_func == free_func &&
621 		    (*p)->clear_free_func == clear_free_func) {
622 			EC_EXTRA_DATA *next = (*p)->next;
623 
624 			(*p)->free_func((*p)->data);
625 			free(*p);
626 
627 			*p = next;
628 			return;
629 		}
630 	}
631 }
632 
633 /* this has 'package' visibility */
634 void
635 EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,
636     void *(*dup_func) (void *),
637     void (*free_func) (void *),
638     void (*clear_free_func) (void *))
639 {
640 	EC_EXTRA_DATA **p;
641 
642 	if (ex_data == NULL)
643 		return;
644 
645 	for (p = ex_data; *p != NULL; p = &((*p)->next)) {
646 		if ((*p)->dup_func == dup_func &&
647 		    (*p)->free_func == free_func &&
648 		    (*p)->clear_free_func == clear_free_func) {
649 			EC_EXTRA_DATA *next = (*p)->next;
650 
651 			(*p)->clear_free_func((*p)->data);
652 			free(*p);
653 
654 			*p = next;
655 			return;
656 		}
657 	}
658 }
659 
660 /* this has 'package' visibility */
661 void
662 EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)
663 {
664 	EC_EXTRA_DATA *d;
665 
666 	if (ex_data == NULL)
667 		return;
668 
669 	d = *ex_data;
670 	while (d) {
671 		EC_EXTRA_DATA *next = d->next;
672 
673 		d->free_func(d->data);
674 		free(d);
675 
676 		d = next;
677 	}
678 	*ex_data = NULL;
679 }
680 
681 /* this has 'package' visibility */
682 void
683 EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)
684 {
685 	EC_EXTRA_DATA *d;
686 
687 	if (ex_data == NULL)
688 		return;
689 
690 	d = *ex_data;
691 	while (d) {
692 		EC_EXTRA_DATA *next = d->next;
693 
694 		d->clear_free_func(d->data);
695 		free(d);
696 
697 		d = next;
698 	}
699 	*ex_data = NULL;
700 }
701 
702 
703 /* functions for EC_POINT objects */
704 
705 EC_POINT *
706 EC_POINT_new(const EC_GROUP * group)
707 {
708 	EC_POINT *ret;
709 
710 	if (group == NULL) {
711 		ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
712 		return NULL;
713 	}
714 	if (group->meth->point_init == 0) {
715 		ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
716 		return NULL;
717 	}
718 	ret = malloc(sizeof *ret);
719 	if (ret == NULL) {
720 		ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
721 		return NULL;
722 	}
723 	ret->meth = group->meth;
724 
725 	if (!ret->meth->point_init(ret)) {
726 		free(ret);
727 		return NULL;
728 	}
729 	return ret;
730 }
731 
732 
733 void
734 EC_POINT_free(EC_POINT * point)
735 {
736 	if (!point)
737 		return;
738 
739 	if (point->meth->point_finish != 0)
740 		point->meth->point_finish(point);
741 	free(point);
742 }
743 
744 
745 void
746 EC_POINT_clear_free(EC_POINT * point)
747 {
748 	if (!point)
749 		return;
750 
751 	if (point->meth->point_clear_finish != 0)
752 		point->meth->point_clear_finish(point);
753 	else if (point->meth->point_finish != 0)
754 		point->meth->point_finish(point);
755 	OPENSSL_cleanse(point, sizeof *point);
756 	free(point);
757 }
758 
759 
760 int
761 EC_POINT_copy(EC_POINT * dest, const EC_POINT * src)
762 {
763 	if (dest->meth->point_copy == 0) {
764 		ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
765 		return 0;
766 	}
767 	if (dest->meth != src->meth) {
768 		ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
769 		return 0;
770 	}
771 	if (dest == src)
772 		return 1;
773 	return dest->meth->point_copy(dest, src);
774 }
775 
776 
777 EC_POINT *
778 EC_POINT_dup(const EC_POINT * a, const EC_GROUP * group)
779 {
780 	EC_POINT *t;
781 	int r;
782 
783 	if (a == NULL)
784 		return NULL;
785 
786 	t = EC_POINT_new(group);
787 	if (t == NULL)
788 		return (NULL);
789 	r = EC_POINT_copy(t, a);
790 	if (!r) {
791 		EC_POINT_free(t);
792 		return NULL;
793 	} else
794 		return t;
795 }
796 
797 
798 const EC_METHOD *
799 EC_POINT_method_of(const EC_POINT * point)
800 {
801 	return point->meth;
802 }
803 
804 
805 int
806 EC_POINT_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
807 {
808 	if (group->meth->point_set_to_infinity == 0) {
809 		ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
810 		return 0;
811 	}
812 	if (group->meth != point->meth) {
813 		ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
814 		return 0;
815 	}
816 	return group->meth->point_set_to_infinity(group, point);
817 }
818 
819 
820 int
821 EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
822     const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
823 {
824 	if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
825 		ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
826 		return 0;
827 	}
828 	if (group->meth != point->meth) {
829 		ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
830 		return 0;
831 	}
832 	return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
833 }
834 
835 
836 int
837 EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
838     const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
839 {
840 	if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
841 		ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
842 		return 0;
843 	}
844 	if (group->meth != point->meth) {
845 		ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
846 		return 0;
847 	}
848 	return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
849 }
850 
851 
852 int
853 EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
854     const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
855 {
856 	if (group->meth->point_set_affine_coordinates == 0) {
857 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
858 		return 0;
859 	}
860 	if (group->meth != point->meth) {
861 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
862 		return 0;
863 	}
864 	return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
865 }
866 
867 #ifndef OPENSSL_NO_EC2M
868 int
869 EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
870     const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
871 {
872 	if (group->meth->point_set_affine_coordinates == 0) {
873 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
874 		return 0;
875 	}
876 	if (group->meth != point->meth) {
877 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
878 		return 0;
879 	}
880 	return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
881 }
882 #endif
883 
884 int
885 EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
886     BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
887 {
888 	if (group->meth->point_get_affine_coordinates == 0) {
889 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
890 		return 0;
891 	}
892 	if (group->meth != point->meth) {
893 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
894 		return 0;
895 	}
896 	return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
897 }
898 
899 #ifndef OPENSSL_NO_EC2M
900 int
901 EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
902     BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
903 {
904 	if (group->meth->point_get_affine_coordinates == 0) {
905 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
906 		return 0;
907 	}
908 	if (group->meth != point->meth) {
909 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
910 		return 0;
911 	}
912 	return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
913 }
914 #endif
915 
916 int
917 EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
918     const EC_POINT *b, BN_CTX *ctx)
919 {
920 	if (group->meth->add == 0) {
921 		ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
922 		return 0;
923 	}
924 	if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth)) {
925 		ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
926 		return 0;
927 	}
928 	return group->meth->add(group, r, a, b, ctx);
929 }
930 
931 
932 int
933 EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
934 {
935 	if (group->meth->dbl == 0) {
936 		ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
937 		return 0;
938 	}
939 	if ((group->meth != r->meth) || (r->meth != a->meth)) {
940 		ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
941 		return 0;
942 	}
943 	return group->meth->dbl(group, r, a, ctx);
944 }
945 
946 
947 int
948 EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
949 {
950 	if (group->meth->invert == 0) {
951 		ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
952 		return 0;
953 	}
954 	if (group->meth != a->meth) {
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
963 EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
964 {
965 	if (group->meth->is_at_infinity == 0) {
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 		ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
971 		return 0;
972 	}
973 	return group->meth->is_at_infinity(group, point);
974 }
975 
976 
977 int
978 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 		ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
982 		return 0;
983 	}
984 	if (group->meth != point->meth) {
985 		ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
986 		return 0;
987 	}
988 	return group->meth->is_on_curve(group, point, ctx);
989 }
990 
991 
992 int
993 EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
994     BN_CTX * ctx)
995 {
996 	if (group->meth->point_cmp == 0) {
997 		ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
998 		return -1;
999 	}
1000 	if ((group->meth != a->meth) || (a->meth != b->meth)) {
1001 		ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1002 		return -1;
1003 	}
1004 	return group->meth->point_cmp(group, a, b, ctx);
1005 }
1006 
1007 
1008 int
1009 EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1010 {
1011 	if (group->meth->make_affine == 0) {
1012 		ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1013 		return 0;
1014 	}
1015 	if (group->meth != point->meth) {
1016 		ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1017 		return 0;
1018 	}
1019 	return group->meth->make_affine(group, point, ctx);
1020 }
1021 
1022 
1023 int
1024 EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
1025     BN_CTX *ctx)
1026 {
1027 	size_t i;
1028 
1029 	if (group->meth->points_make_affine == 0) {
1030 		ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1031 		return 0;
1032 	}
1033 	for (i = 0; i < num; i++) {
1034 		if (group->meth != points[i]->meth) {
1035 			ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1036 			return 0;
1037 		}
1038 	}
1039 	return group->meth->points_make_affine(group, num, points, ctx);
1040 }
1041 
1042 
1043 /* Functions for point multiplication.
1044  *
1045  * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1046  * otherwise we dispatch through methods.
1047  */
1048 
1049 int
1050 EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1051     size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1052 {
1053 	if (group->meth->mul == 0)
1054 		/* use default */
1055 		return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1056 
1057 	return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1058 }
1059 
1060 int
1061 EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1062     const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1063 {
1064 	/* just a convenient interface to EC_POINTs_mul() */
1065 
1066 	const EC_POINT *points[1];
1067 	const BIGNUM *scalars[1];
1068 
1069 	points[0] = point;
1070 	scalars[0] = p_scalar;
1071 
1072 	return EC_POINTs_mul(group, r, g_scalar,
1073 	    (point != NULL && p_scalar != NULL),
1074 	    points, scalars, ctx);
1075 }
1076 
1077 int
1078 EC_GROUP_precompute_mult(EC_GROUP * group, BN_CTX * ctx)
1079 {
1080 	if (group->meth->mul == 0)
1081 		/* use default */
1082 		return ec_wNAF_precompute_mult(group, ctx);
1083 
1084 	if (group->meth->precompute_mult != 0)
1085 		return group->meth->precompute_mult(group, ctx);
1086 	else
1087 		return 1;	/* nothing to do, so report success */
1088 }
1089 
1090 int
1091 EC_GROUP_have_precompute_mult(const EC_GROUP * group)
1092 {
1093 	if (group->meth->mul == 0)
1094 		/* use default */
1095 		return ec_wNAF_have_precompute_mult(group);
1096 
1097 	if (group->meth->have_precompute_mult != 0)
1098 		return group->meth->have_precompute_mult(group);
1099 	else
1100 		return 0;	/* cannot tell whether precomputation has
1101 				 * been performed */
1102 }
1103