xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ec_lib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ec/ec_lib.c */
2*2139Sjp161948 /*
3*2139Sjp161948  * Originally written by Bodo Moeller for the OpenSSL project.
4*2139Sjp161948  */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  * Binary polynomial ECC support in OpenSSL originally developed by
61*2139Sjp161948  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62*2139Sjp161948  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <string.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <openssl/err.h>
670Sstevel@tonic-gate #include <openssl/opensslv.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include "ec_lcl.h"
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* functions for EC_GROUP objects */
750Sstevel@tonic-gate 
EC_GROUP_new(const EC_METHOD * meth)760Sstevel@tonic-gate EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
770Sstevel@tonic-gate 	{
780Sstevel@tonic-gate 	EC_GROUP *ret;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (meth == NULL)
810Sstevel@tonic-gate 		{
820Sstevel@tonic-gate 		ECerr(EC_F_EC_GROUP_NEW, ERR_R_PASSED_NULL_PARAMETER);
830Sstevel@tonic-gate 		return NULL;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	if (meth->group_init == 0)
860Sstevel@tonic-gate 		{
870Sstevel@tonic-gate 		ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
880Sstevel@tonic-gate 		return NULL;
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	ret = OPENSSL_malloc(sizeof *ret);
920Sstevel@tonic-gate 	if (ret == NULL)
930Sstevel@tonic-gate 		{
940Sstevel@tonic-gate 		ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
950Sstevel@tonic-gate 		return NULL;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	ret->meth = meth;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	ret->extra_data = NULL;
101*2139Sjp161948 
102*2139Sjp161948 	ret->generator = NULL;
103*2139Sjp161948 	BN_init(&ret->order);
104*2139Sjp161948 	BN_init(&ret->cofactor);
105*2139Sjp161948 
106*2139Sjp161948 	ret->curve_name = 0;
107*2139Sjp161948 	ret->asn1_flag  = 0;
108*2139Sjp161948 	ret->asn1_form  = POINT_CONVERSION_UNCOMPRESSED;
109*2139Sjp161948 
110*2139Sjp161948 	ret->seed = NULL;
111*2139Sjp161948 	ret->seed_len = 0;
112*2139Sjp161948 
1130Sstevel@tonic-gate 	if (!meth->group_init(ret))
1140Sstevel@tonic-gate 		{
1150Sstevel@tonic-gate 		OPENSSL_free(ret);
1160Sstevel@tonic-gate 		return NULL;
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	return ret;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
EC_GROUP_free(EC_GROUP * group)1230Sstevel@tonic-gate void EC_GROUP_free(EC_GROUP *group)
1240Sstevel@tonic-gate 	{
1250Sstevel@tonic-gate 	if (!group) return;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (group->meth->group_finish != 0)
1280Sstevel@tonic-gate 		group->meth->group_finish(group);
1290Sstevel@tonic-gate 
130*2139Sjp161948 	EC_EX_DATA_free_all_data(&group->extra_data);
131*2139Sjp161948 
132*2139Sjp161948 	if (group->generator != NULL)
133*2139Sjp161948 		EC_POINT_free(group->generator);
134*2139Sjp161948 	BN_free(&group->order);
135*2139Sjp161948 	BN_free(&group->cofactor);
136*2139Sjp161948 
137*2139Sjp161948 	if (group->seed)
138*2139Sjp161948 		OPENSSL_free(group->seed);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	OPENSSL_free(group);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
EC_GROUP_clear_free(EC_GROUP * group)1440Sstevel@tonic-gate void EC_GROUP_clear_free(EC_GROUP *group)
1450Sstevel@tonic-gate 	{
1460Sstevel@tonic-gate 	if (!group) return;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (group->meth->group_clear_finish != 0)
1490Sstevel@tonic-gate 		group->meth->group_clear_finish(group);
1500Sstevel@tonic-gate 	else if (group->meth != NULL && group->meth->group_finish != 0)
1510Sstevel@tonic-gate 		group->meth->group_finish(group);
1520Sstevel@tonic-gate 
153*2139Sjp161948 	EC_EX_DATA_clear_free_all_data(&group->extra_data);
154*2139Sjp161948 
155*2139Sjp161948 	if (group->generator != NULL)
156*2139Sjp161948 		EC_POINT_clear_free(group->generator);
157*2139Sjp161948 	BN_clear_free(&group->order);
158*2139Sjp161948 	BN_clear_free(&group->cofactor);
159*2139Sjp161948 
160*2139Sjp161948 	if (group->seed)
161*2139Sjp161948 		{
162*2139Sjp161948 		OPENSSL_cleanse(group->seed, group->seed_len);
163*2139Sjp161948 		OPENSSL_free(group->seed);
164*2139Sjp161948 		}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	OPENSSL_cleanse(group, sizeof *group);
1670Sstevel@tonic-gate 	OPENSSL_free(group);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
EC_GROUP_copy(EC_GROUP * dest,const EC_GROUP * src)1710Sstevel@tonic-gate int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
1720Sstevel@tonic-gate 	{
173*2139Sjp161948 	EC_EXTRA_DATA *d;
174*2139Sjp161948 
1750Sstevel@tonic-gate 	if (dest->meth->group_copy == 0)
1760Sstevel@tonic-gate 		{
1770Sstevel@tonic-gate 		ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1780Sstevel@tonic-gate 		return 0;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	if (dest->meth != src->meth)
1810Sstevel@tonic-gate 		{
1820Sstevel@tonic-gate 		ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
1830Sstevel@tonic-gate 		return 0;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	if (dest == src)
1860Sstevel@tonic-gate 		return 1;
1870Sstevel@tonic-gate 
188*2139Sjp161948 	EC_EX_DATA_free_all_data(&dest->extra_data);
189*2139Sjp161948 
190*2139Sjp161948 	for (d = src->extra_data; d != NULL; d = d->next)
1910Sstevel@tonic-gate 		{
192*2139Sjp161948 		void *t = d->dup_func(d->data);
193*2139Sjp161948 
194*2139Sjp161948 		if (t == NULL)
195*2139Sjp161948 			return 0;
196*2139Sjp161948 		if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func, d->free_func, d->clear_free_func))
197*2139Sjp161948 			return 0;
198*2139Sjp161948 		}
199*2139Sjp161948 
200*2139Sjp161948 	if (src->generator != NULL)
201*2139Sjp161948 		{
202*2139Sjp161948 		if (dest->generator == NULL)
2030Sstevel@tonic-gate 			{
204*2139Sjp161948 			dest->generator = EC_POINT_new(dest);
205*2139Sjp161948 			if (dest->generator == NULL) return 0;
2060Sstevel@tonic-gate 			}
207*2139Sjp161948 		if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
208*2139Sjp161948 		}
209*2139Sjp161948 	else
210*2139Sjp161948 		{
211*2139Sjp161948 		/* src->generator == NULL */
212*2139Sjp161948 		if (dest->generator != NULL)
213*2139Sjp161948 			{
214*2139Sjp161948 			EC_POINT_clear_free(dest->generator);
215*2139Sjp161948 			dest->generator = NULL;
216*2139Sjp161948 			}
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 
219*2139Sjp161948 	if (!BN_copy(&dest->order, &src->order)) return 0;
220*2139Sjp161948 	if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
221*2139Sjp161948 
222*2139Sjp161948 	dest->curve_name = src->curve_name;
223*2139Sjp161948 	dest->asn1_flag  = src->asn1_flag;
224*2139Sjp161948 	dest->asn1_form  = src->asn1_form;
225*2139Sjp161948 
226*2139Sjp161948 	if (src->seed)
227*2139Sjp161948 		{
228*2139Sjp161948 		if (dest->seed)
229*2139Sjp161948 			OPENSSL_free(dest->seed);
230*2139Sjp161948 		dest->seed = OPENSSL_malloc(src->seed_len);
231*2139Sjp161948 		if (dest->seed == NULL)
232*2139Sjp161948 			return 0;
233*2139Sjp161948 		if (!memcpy(dest->seed, src->seed, src->seed_len))
234*2139Sjp161948 			return 0;
235*2139Sjp161948 		dest->seed_len = src->seed_len;
236*2139Sjp161948 		}
237*2139Sjp161948 	else
238*2139Sjp161948 		{
239*2139Sjp161948 		if (dest->seed)
240*2139Sjp161948 			OPENSSL_free(dest->seed);
241*2139Sjp161948 		dest->seed = NULL;
242*2139Sjp161948 		dest->seed_len = 0;
243*2139Sjp161948 		}
244*2139Sjp161948 
245*2139Sjp161948 
2460Sstevel@tonic-gate 	return dest->meth->group_copy(dest, src);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 
EC_GROUP_dup(const EC_GROUP * a)250*2139Sjp161948 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
251*2139Sjp161948 	{
252*2139Sjp161948 	EC_GROUP *t = NULL;
253*2139Sjp161948 	int ok = 0;
254*2139Sjp161948 
255*2139Sjp161948 	if (a == NULL) return NULL;
256*2139Sjp161948 
257*2139Sjp161948 	if ((t = EC_GROUP_new(a->meth)) == NULL) return(NULL);
258*2139Sjp161948 	if (!EC_GROUP_copy(t, a)) goto err;
259*2139Sjp161948 
260*2139Sjp161948 	ok = 1;
261*2139Sjp161948 
262*2139Sjp161948   err:
263*2139Sjp161948 	if (!ok)
264*2139Sjp161948 		{
265*2139Sjp161948 		if (t) EC_GROUP_free(t);
266*2139Sjp161948 		return NULL;
267*2139Sjp161948 		}
268*2139Sjp161948 	else return t;
269*2139Sjp161948 	}
270*2139Sjp161948 
271*2139Sjp161948 
EC_GROUP_method_of(const EC_GROUP * group)2720Sstevel@tonic-gate const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
2730Sstevel@tonic-gate 	{
2740Sstevel@tonic-gate 	return group->meth;
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
EC_METHOD_get_field_type(const EC_METHOD * meth)278*2139Sjp161948 int EC_METHOD_get_field_type(const EC_METHOD *meth)
279*2139Sjp161948         {
280*2139Sjp161948         return meth->field_type;
281*2139Sjp161948         }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 
EC_GROUP_set_generator(EC_GROUP * group,const EC_POINT * generator,const BIGNUM * order,const BIGNUM * cofactor)2840Sstevel@tonic-gate int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor)
2850Sstevel@tonic-gate 	{
286*2139Sjp161948 	if (generator == NULL)
287*2139Sjp161948 		{
288*2139Sjp161948 		ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
289*2139Sjp161948 		return 0   ;
290*2139Sjp161948 		}
291*2139Sjp161948 
292*2139Sjp161948 	if (group->generator == NULL)
2930Sstevel@tonic-gate 		{
294*2139Sjp161948 		group->generator = EC_POINT_new(group);
295*2139Sjp161948 		if (group->generator == NULL) return 0;
2960Sstevel@tonic-gate 		}
297*2139Sjp161948 	if (!EC_POINT_copy(group->generator, generator)) return 0;
298*2139Sjp161948 
299*2139Sjp161948 	if (order != NULL)
300*2139Sjp161948 		{ if (!BN_copy(&group->order, order)) return 0; }
301*2139Sjp161948 	else
302*2139Sjp161948 		BN_zero(&group->order);
303*2139Sjp161948 
304*2139Sjp161948 	if (cofactor != NULL)
305*2139Sjp161948 		{ if (!BN_copy(&group->cofactor, cofactor)) return 0; }
306*2139Sjp161948 	else
307*2139Sjp161948 		BN_zero(&group->cofactor);
308*2139Sjp161948 
309*2139Sjp161948 	return 1;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 
EC_GROUP_get0_generator(const EC_GROUP * group)313*2139Sjp161948 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
3140Sstevel@tonic-gate 	{
315*2139Sjp161948 	return group->generator;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
EC_GROUP_get_order(const EC_GROUP * group,BIGNUM * order,BN_CTX * ctx)3190Sstevel@tonic-gate int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
3200Sstevel@tonic-gate 	{
321*2139Sjp161948 	if (!BN_copy(order, &group->order))
3220Sstevel@tonic-gate 		return 0;
323*2139Sjp161948 
324*2139Sjp161948 	return !BN_is_zero(order);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
EC_GROUP_get_cofactor(const EC_GROUP * group,BIGNUM * cofactor,BN_CTX * ctx)3280Sstevel@tonic-gate int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
3290Sstevel@tonic-gate 	{
330*2139Sjp161948 	if (!BN_copy(cofactor, &group->cofactor))
331*2139Sjp161948 		return 0;
332*2139Sjp161948 
333*2139Sjp161948 	return !BN_is_zero(&group->cofactor);
334*2139Sjp161948 	}
335*2139Sjp161948 
336*2139Sjp161948 
EC_GROUP_set_curve_name(EC_GROUP * group,int nid)337*2139Sjp161948 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
338*2139Sjp161948 	{
339*2139Sjp161948 	group->curve_name = nid;
340*2139Sjp161948 	}
341*2139Sjp161948 
342*2139Sjp161948 
EC_GROUP_get_curve_name(const EC_GROUP * group)343*2139Sjp161948 int EC_GROUP_get_curve_name(const EC_GROUP *group)
344*2139Sjp161948 	{
345*2139Sjp161948 	return group->curve_name;
346*2139Sjp161948 	}
347*2139Sjp161948 
348*2139Sjp161948 
EC_GROUP_set_asn1_flag(EC_GROUP * group,int flag)349*2139Sjp161948 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
350*2139Sjp161948 	{
351*2139Sjp161948 	group->asn1_flag = flag;
352*2139Sjp161948 	}
353*2139Sjp161948 
354*2139Sjp161948 
EC_GROUP_get_asn1_flag(const EC_GROUP * group)355*2139Sjp161948 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
356*2139Sjp161948 	{
357*2139Sjp161948 	return group->asn1_flag;
358*2139Sjp161948 	}
359*2139Sjp161948 
360*2139Sjp161948 
EC_GROUP_set_point_conversion_form(EC_GROUP * group,point_conversion_form_t form)361*2139Sjp161948 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
362*2139Sjp161948                                         point_conversion_form_t form)
363*2139Sjp161948 	{
364*2139Sjp161948 	group->asn1_form = form;
365*2139Sjp161948 	}
366*2139Sjp161948 
367*2139Sjp161948 
EC_GROUP_get_point_conversion_form(const EC_GROUP * group)368*2139Sjp161948 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *group)
369*2139Sjp161948 	{
370*2139Sjp161948 	return group->asn1_form;
371*2139Sjp161948 	}
372*2139Sjp161948 
373*2139Sjp161948 
EC_GROUP_set_seed(EC_GROUP * group,const unsigned char * p,size_t len)374*2139Sjp161948 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
375*2139Sjp161948 	{
376*2139Sjp161948 	if (group->seed)
3770Sstevel@tonic-gate 		{
378*2139Sjp161948 		OPENSSL_free(group->seed);
379*2139Sjp161948 		group->seed = NULL;
380*2139Sjp161948 		group->seed_len = 0;
381*2139Sjp161948 		}
382*2139Sjp161948 
383*2139Sjp161948 	if (!len || !p)
384*2139Sjp161948 		return 1;
385*2139Sjp161948 
386*2139Sjp161948 	if ((group->seed = OPENSSL_malloc(len)) == NULL)
387*2139Sjp161948 		return 0;
388*2139Sjp161948 	memcpy(group->seed, p, len);
389*2139Sjp161948 	group->seed_len = len;
390*2139Sjp161948 
391*2139Sjp161948 	return len;
392*2139Sjp161948 	}
393*2139Sjp161948 
394*2139Sjp161948 
EC_GROUP_get0_seed(const EC_GROUP * group)395*2139Sjp161948 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
396*2139Sjp161948 	{
397*2139Sjp161948 	return group->seed;
398*2139Sjp161948 	}
399*2139Sjp161948 
400*2139Sjp161948 
EC_GROUP_get_seed_len(const EC_GROUP * group)401*2139Sjp161948 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
402*2139Sjp161948 	{
403*2139Sjp161948 	return group->seed_len;
404*2139Sjp161948 	}
405*2139Sjp161948 
406*2139Sjp161948 
EC_GROUP_set_curve_GFp(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)407*2139Sjp161948 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
408*2139Sjp161948 	{
409*2139Sjp161948 	if (group->meth->group_set_curve == 0)
410*2139Sjp161948 		{
411*2139Sjp161948 		ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4120Sstevel@tonic-gate 		return 0;
4130Sstevel@tonic-gate 		}
414*2139Sjp161948 	return group->meth->group_set_curve(group, p, a, b, ctx);
415*2139Sjp161948 	}
416*2139Sjp161948 
417*2139Sjp161948 
EC_GROUP_get_curve_GFp(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)418*2139Sjp161948 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
419*2139Sjp161948 	{
420*2139Sjp161948 	if (group->meth->group_get_curve == 0)
421*2139Sjp161948 		{
422*2139Sjp161948 		ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
423*2139Sjp161948 		return 0;
424*2139Sjp161948 		}
425*2139Sjp161948 	return group->meth->group_get_curve(group, p, a, b, ctx);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 
EC_GROUP_set_curve_GF2m(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)429*2139Sjp161948 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
430*2139Sjp161948 	{
431*2139Sjp161948 	if (group->meth->group_set_curve == 0)
432*2139Sjp161948 		{
433*2139Sjp161948 		ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
434*2139Sjp161948 		return 0;
435*2139Sjp161948 		}
436*2139Sjp161948 	return group->meth->group_set_curve(group, p, a, b, ctx);
437*2139Sjp161948 	}
438*2139Sjp161948 
439*2139Sjp161948 
EC_GROUP_get_curve_GF2m(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)440*2139Sjp161948 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
4410Sstevel@tonic-gate 	{
442*2139Sjp161948 	if (group->meth->group_get_curve == 0)
443*2139Sjp161948 		{
444*2139Sjp161948 		ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
445*2139Sjp161948 		return 0;
446*2139Sjp161948 		}
447*2139Sjp161948 	return group->meth->group_get_curve(group, p, a, b, ctx);
448*2139Sjp161948 	}
449*2139Sjp161948 
450*2139Sjp161948 
EC_GROUP_get_degree(const EC_GROUP * group)451*2139Sjp161948 int EC_GROUP_get_degree(const EC_GROUP *group)
452*2139Sjp161948 	{
453*2139Sjp161948 	if (group->meth->group_get_degree == 0)
4540Sstevel@tonic-gate 		{
455*2139Sjp161948 		ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
456*2139Sjp161948 		return 0;
457*2139Sjp161948 		}
458*2139Sjp161948 	return group->meth->group_get_degree(group);
459*2139Sjp161948 	}
460*2139Sjp161948 
461*2139Sjp161948 
EC_GROUP_check_discriminant(const EC_GROUP * group,BN_CTX * ctx)462*2139Sjp161948 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
463*2139Sjp161948 	{
464*2139Sjp161948 	if (group->meth->group_check_discriminant == 0)
465*2139Sjp161948 		{
466*2139Sjp161948 		ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4670Sstevel@tonic-gate 		return 0;
4680Sstevel@tonic-gate 		}
469*2139Sjp161948 	return group->meth->group_check_discriminant(group, ctx);
470*2139Sjp161948 	}
4710Sstevel@tonic-gate 
472*2139Sjp161948 
EC_GROUP_cmp(const EC_GROUP * a,const EC_GROUP * b,BN_CTX * ctx)473*2139Sjp161948 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
474*2139Sjp161948 	{
475*2139Sjp161948 	int    r = 0;
476*2139Sjp161948 	BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
477*2139Sjp161948 	BN_CTX *ctx_new = NULL;
478*2139Sjp161948 
479*2139Sjp161948 	/* compare the field types*/
480*2139Sjp161948 	if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
481*2139Sjp161948 	    EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
482*2139Sjp161948 		return 1;
483*2139Sjp161948 	/* compare the curve name (if present) */
484*2139Sjp161948 	if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
485*2139Sjp161948 	    EC_GROUP_get_curve_name(a) == EC_GROUP_get_curve_name(b))
486*2139Sjp161948 		return 0;
487*2139Sjp161948 
488*2139Sjp161948 	if (!ctx)
489*2139Sjp161948 		ctx_new = ctx = BN_CTX_new();
490*2139Sjp161948 	if (!ctx)
491*2139Sjp161948 		return -1;
492*2139Sjp161948 
493*2139Sjp161948 	BN_CTX_start(ctx);
494*2139Sjp161948 	a1 = BN_CTX_get(ctx);
495*2139Sjp161948 	a2 = BN_CTX_get(ctx);
496*2139Sjp161948 	a3 = BN_CTX_get(ctx);
497*2139Sjp161948 	b1 = BN_CTX_get(ctx);
498*2139Sjp161948 	b2 = BN_CTX_get(ctx);
499*2139Sjp161948 	b3 = BN_CTX_get(ctx);
500*2139Sjp161948 	if (!b3)
501*2139Sjp161948 		{
502*2139Sjp161948 		BN_CTX_end(ctx);
503*2139Sjp161948 		if (ctx_new)
504*2139Sjp161948 			BN_CTX_free(ctx);
505*2139Sjp161948 		return -1;
506*2139Sjp161948 		}
507*2139Sjp161948 
508*2139Sjp161948 	/* XXX This approach assumes that the external representation
509*2139Sjp161948 	 * of curves over the same field type is the same.
510*2139Sjp161948 	 */
511*2139Sjp161948 	if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
512*2139Sjp161948 	    !b->meth->group_get_curve(b, b1, b2, b3, ctx))
513*2139Sjp161948 		r = 1;
514*2139Sjp161948 
515*2139Sjp161948 	if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
516*2139Sjp161948 		r = 1;
517*2139Sjp161948 
518*2139Sjp161948 	/* XXX EC_POINT_cmp() assumes that the methods are equal */
519*2139Sjp161948 	if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
520*2139Sjp161948 	    EC_GROUP_get0_generator(b), ctx))
521*2139Sjp161948 		r = 1;
522*2139Sjp161948 
523*2139Sjp161948 	if (!r)
524*2139Sjp161948 		{
525*2139Sjp161948 		/* compare the order and cofactor */
526*2139Sjp161948 		if (!EC_GROUP_get_order(a, a1, ctx) ||
527*2139Sjp161948 		    !EC_GROUP_get_order(b, b1, ctx) ||
528*2139Sjp161948 		    !EC_GROUP_get_cofactor(a, a2, ctx) ||
529*2139Sjp161948 		    !EC_GROUP_get_cofactor(b, b2, ctx))
530*2139Sjp161948 			{
531*2139Sjp161948 			BN_CTX_end(ctx);
532*2139Sjp161948 			if (ctx_new)
533*2139Sjp161948 				BN_CTX_free(ctx);
534*2139Sjp161948 			return -1;
535*2139Sjp161948 			}
536*2139Sjp161948 		if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
537*2139Sjp161948 			r = 1;
538*2139Sjp161948 		}
539*2139Sjp161948 
540*2139Sjp161948 	BN_CTX_end(ctx);
541*2139Sjp161948 	if (ctx_new)
542*2139Sjp161948 		BN_CTX_free(ctx);
543*2139Sjp161948 
544*2139Sjp161948 	return r;
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /* this has 'package' visibility */
EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data,void * data,void * (* dup_func)(void *),void (* free_func)(void *),void (* clear_free_func)(void *))549*2139Sjp161948 int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
550*2139Sjp161948 	void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
5510Sstevel@tonic-gate 	{
552*2139Sjp161948 	EC_EXTRA_DATA *d;
553*2139Sjp161948 
554*2139Sjp161948 	if (ex_data == NULL)
555*2139Sjp161948 		return 0;
556*2139Sjp161948 
557*2139Sjp161948 	for (d = *ex_data; d != NULL; d = d->next)
5580Sstevel@tonic-gate 		{
559*2139Sjp161948 		if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
560*2139Sjp161948 			{
561*2139Sjp161948 			ECerr(EC_F_EC_GROUP_SET_EXTRA_DATA, EC_R_SLOT_FULL);
562*2139Sjp161948 			return 0;
563*2139Sjp161948 			}
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 
566*2139Sjp161948 	if (data == NULL)
567*2139Sjp161948 		/* no explicit entry needed */
568*2139Sjp161948 		return 1;
569*2139Sjp161948 
570*2139Sjp161948 	d = OPENSSL_malloc(sizeof *d);
571*2139Sjp161948 	if (d == NULL)
572*2139Sjp161948 		return 0;
573*2139Sjp161948 
574*2139Sjp161948 	d->data = data;
575*2139Sjp161948 	d->dup_func = dup_func;
576*2139Sjp161948 	d->free_func = free_func;
577*2139Sjp161948 	d->clear_free_func = clear_free_func;
578*2139Sjp161948 
579*2139Sjp161948 	d->next = *ex_data;
580*2139Sjp161948 	*ex_data = d;
581*2139Sjp161948 
582*2139Sjp161948 	return 1;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
585*2139Sjp161948 /* this has 'package' visibility */
EC_EX_DATA_get_data(const EC_EXTRA_DATA * ex_data,void * (* dup_func)(void *),void (* free_func)(void *),void (* clear_free_func)(void *))586*2139Sjp161948 void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
587*2139Sjp161948 	void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
588*2139Sjp161948 	{
589*2139Sjp161948 	const EC_EXTRA_DATA *d;
590*2139Sjp161948 
591*2139Sjp161948 	for (d = ex_data; d != NULL; d = d->next)
592*2139Sjp161948 		{
593*2139Sjp161948 		if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
594*2139Sjp161948 			return d->data;
595*2139Sjp161948 		}
596*2139Sjp161948 
597*2139Sjp161948 	return NULL;
598*2139Sjp161948 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /* this has 'package' visibility */
EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,void * (* dup_func)(void *),void (* free_func)(void *),void (* clear_free_func)(void *))601*2139Sjp161948 void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
602*2139Sjp161948 	void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
6030Sstevel@tonic-gate 	{
604*2139Sjp161948 	EC_EXTRA_DATA **p;
605*2139Sjp161948 
606*2139Sjp161948 	if (ex_data == NULL)
607*2139Sjp161948 		return;
608*2139Sjp161948 
609*2139Sjp161948 	for (p = ex_data; *p != NULL; p = &((*p)->next))
610*2139Sjp161948 		{
611*2139Sjp161948 		if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
612*2139Sjp161948 			{
613*2139Sjp161948 			EC_EXTRA_DATA *next = (*p)->next;
614*2139Sjp161948 
615*2139Sjp161948 			(*p)->free_func((*p)->data);
616*2139Sjp161948 			OPENSSL_free(*p);
617*2139Sjp161948 
618*2139Sjp161948 			*p = next;
619*2139Sjp161948 			return;
620*2139Sjp161948 			}
621*2139Sjp161948 		}
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /* this has 'package' visibility */
EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,void * (* dup_func)(void *),void (* free_func)(void *),void (* clear_free_func)(void *))625*2139Sjp161948 void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
626*2139Sjp161948 	void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
6270Sstevel@tonic-gate 	{
628*2139Sjp161948 	EC_EXTRA_DATA **p;
629*2139Sjp161948 
630*2139Sjp161948 	if (ex_data == NULL)
631*2139Sjp161948 		return;
632*2139Sjp161948 
633*2139Sjp161948 	for (p = ex_data; *p != NULL; p = &((*p)->next))
634*2139Sjp161948 		{
635*2139Sjp161948 		if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
636*2139Sjp161948 			{
637*2139Sjp161948 			EC_EXTRA_DATA *next = (*p)->next;
638*2139Sjp161948 
639*2139Sjp161948 			(*p)->clear_free_func((*p)->data);
640*2139Sjp161948 			OPENSSL_free(*p);
641*2139Sjp161948 
642*2139Sjp161948 			*p = next;
643*2139Sjp161948 			return;
644*2139Sjp161948 			}
645*2139Sjp161948 		}
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
648*2139Sjp161948 /* this has 'package' visibility */
EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)649*2139Sjp161948 void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
650*2139Sjp161948 	{
651*2139Sjp161948 	EC_EXTRA_DATA *d;
652*2139Sjp161948 
653*2139Sjp161948 	if (ex_data == NULL)
654*2139Sjp161948 		return;
655*2139Sjp161948 
656*2139Sjp161948 	d = *ex_data;
657*2139Sjp161948 	while (d)
658*2139Sjp161948 		{
659*2139Sjp161948 		EC_EXTRA_DATA *next = d->next;
660*2139Sjp161948 
661*2139Sjp161948 		d->free_func(d->data);
662*2139Sjp161948 		OPENSSL_free(d);
663*2139Sjp161948 
664*2139Sjp161948 		d = next;
665*2139Sjp161948 		}
666*2139Sjp161948 	*ex_data = NULL;
667*2139Sjp161948 	}
668*2139Sjp161948 
669*2139Sjp161948 /* this has 'package' visibility */
EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)670*2139Sjp161948 void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
671*2139Sjp161948 	{
672*2139Sjp161948 	EC_EXTRA_DATA *d;
673*2139Sjp161948 
674*2139Sjp161948 	if (ex_data == NULL)
675*2139Sjp161948 		return;
676*2139Sjp161948 
677*2139Sjp161948 	d = *ex_data;
678*2139Sjp161948 	while (d)
679*2139Sjp161948 		{
680*2139Sjp161948 		EC_EXTRA_DATA *next = d->next;
681*2139Sjp161948 
682*2139Sjp161948 		d->clear_free_func(d->data);
683*2139Sjp161948 		OPENSSL_free(d);
684*2139Sjp161948 
685*2139Sjp161948 		d = next;
686*2139Sjp161948 		}
687*2139Sjp161948 	*ex_data = NULL;
688*2139Sjp161948 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate /* functions for EC_POINT objects */
6920Sstevel@tonic-gate 
EC_POINT_new(const EC_GROUP * group)6930Sstevel@tonic-gate EC_POINT *EC_POINT_new(const EC_GROUP *group)
6940Sstevel@tonic-gate 	{
6950Sstevel@tonic-gate 	EC_POINT *ret;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (group == NULL)
6980Sstevel@tonic-gate 		{
6990Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
7000Sstevel@tonic-gate 		return NULL;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 	if (group->meth->point_init == 0)
7030Sstevel@tonic-gate 		{
7040Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
7050Sstevel@tonic-gate 		return NULL;
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	ret = OPENSSL_malloc(sizeof *ret);
7090Sstevel@tonic-gate 	if (ret == NULL)
7100Sstevel@tonic-gate 		{
7110Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
7120Sstevel@tonic-gate 		return NULL;
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	ret->meth = group->meth;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	if (!ret->meth->point_init(ret))
7180Sstevel@tonic-gate 		{
7190Sstevel@tonic-gate 		OPENSSL_free(ret);
7200Sstevel@tonic-gate 		return NULL;
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	return ret;
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 
EC_POINT_free(EC_POINT * point)7270Sstevel@tonic-gate void EC_POINT_free(EC_POINT *point)
7280Sstevel@tonic-gate 	{
7290Sstevel@tonic-gate 	if (!point) return;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (point->meth->point_finish != 0)
7320Sstevel@tonic-gate 		point->meth->point_finish(point);
7330Sstevel@tonic-gate 	OPENSSL_free(point);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 
EC_POINT_clear_free(EC_POINT * point)7370Sstevel@tonic-gate void EC_POINT_clear_free(EC_POINT *point)
7380Sstevel@tonic-gate 	{
7390Sstevel@tonic-gate 	if (!point) return;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if (point->meth->point_clear_finish != 0)
7420Sstevel@tonic-gate 		point->meth->point_clear_finish(point);
7430Sstevel@tonic-gate 	else if (point->meth != NULL && point->meth->point_finish != 0)
7440Sstevel@tonic-gate 		point->meth->point_finish(point);
7450Sstevel@tonic-gate 	OPENSSL_cleanse(point, sizeof *point);
7460Sstevel@tonic-gate 	OPENSSL_free(point);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 
EC_POINT_copy(EC_POINT * dest,const EC_POINT * src)7500Sstevel@tonic-gate int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
7510Sstevel@tonic-gate 	{
7520Sstevel@tonic-gate 	if (dest->meth->point_copy == 0)
7530Sstevel@tonic-gate 		{
7540Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
7550Sstevel@tonic-gate 		return 0;
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 	if (dest->meth != src->meth)
7580Sstevel@tonic-gate 		{
7590Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
7600Sstevel@tonic-gate 		return 0;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 	if (dest == src)
7630Sstevel@tonic-gate 		return 1;
7640Sstevel@tonic-gate 	return dest->meth->point_copy(dest, src);
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 
EC_POINT_dup(const EC_POINT * a,const EC_GROUP * group)768*2139Sjp161948 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
769*2139Sjp161948 	{
770*2139Sjp161948 	EC_POINT *t;
771*2139Sjp161948 	int r;
772*2139Sjp161948 
773*2139Sjp161948 	if (a == NULL) return NULL;
774*2139Sjp161948 
775*2139Sjp161948 	t = EC_POINT_new(group);
776*2139Sjp161948 	if (t == NULL) return(NULL);
777*2139Sjp161948 	r = EC_POINT_copy(t, a);
778*2139Sjp161948 	if (!r)
779*2139Sjp161948 		{
780*2139Sjp161948 		EC_POINT_free(t);
781*2139Sjp161948 		return NULL;
782*2139Sjp161948 		}
783*2139Sjp161948 	else return t;
784*2139Sjp161948 	}
785*2139Sjp161948 
786*2139Sjp161948 
EC_POINT_method_of(const EC_POINT * point)7870Sstevel@tonic-gate const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
7880Sstevel@tonic-gate 	{
7890Sstevel@tonic-gate 	return point->meth;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 
EC_POINT_set_to_infinity(const EC_GROUP * group,EC_POINT * point)7930Sstevel@tonic-gate int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
7940Sstevel@tonic-gate 	{
7950Sstevel@tonic-gate 	if (group->meth->point_set_to_infinity == 0)
7960Sstevel@tonic-gate 		{
7970Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
7980Sstevel@tonic-gate 		return 0;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 	if (group->meth != point->meth)
8010Sstevel@tonic-gate 		{
8020Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
8030Sstevel@tonic-gate 		return 0;
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 	return group->meth->point_set_to_infinity(group, point);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 
EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,const BIGNUM * z,BN_CTX * ctx)8090Sstevel@tonic-gate int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
8100Sstevel@tonic-gate 	const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
8110Sstevel@tonic-gate 	{
8120Sstevel@tonic-gate 	if (group->meth->point_set_Jprojective_coordinates_GFp == 0)
8130Sstevel@tonic-gate 		{
8140Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
8150Sstevel@tonic-gate 		return 0;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 	if (group->meth != point->meth)
8180Sstevel@tonic-gate 		{
8190Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
8200Sstevel@tonic-gate 		return 0;
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 	return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 
EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BIGNUM * z,BN_CTX * ctx)8260Sstevel@tonic-gate int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
8270Sstevel@tonic-gate 	BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
8280Sstevel@tonic-gate 	{
8290Sstevel@tonic-gate 	if (group->meth->point_get_Jprojective_coordinates_GFp == 0)
8300Sstevel@tonic-gate 		{
8310Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
8320Sstevel@tonic-gate 		return 0;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 	if (group->meth != point->meth)
8350Sstevel@tonic-gate 		{
8360Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
8370Sstevel@tonic-gate 		return 0;
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 	return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 
EC_POINT_set_affine_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)8430Sstevel@tonic-gate int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
8440Sstevel@tonic-gate 	const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
8450Sstevel@tonic-gate 	{
846*2139Sjp161948 	if (group->meth->point_set_affine_coordinates == 0)
8470Sstevel@tonic-gate 		{
8480Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
8490Sstevel@tonic-gate 		return 0;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	if (group->meth != point->meth)
8520Sstevel@tonic-gate 		{
8530Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
8540Sstevel@tonic-gate 		return 0;
8550Sstevel@tonic-gate 		}
856*2139Sjp161948 	return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
857*2139Sjp161948 	}
858*2139Sjp161948 
859*2139Sjp161948 
EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)860*2139Sjp161948 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
861*2139Sjp161948 	const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
862*2139Sjp161948 	{
863*2139Sjp161948 	if (group->meth->point_set_affine_coordinates == 0)
864*2139Sjp161948 		{
865*2139Sjp161948 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
866*2139Sjp161948 		return 0;
867*2139Sjp161948 		}
868*2139Sjp161948 	if (group->meth != point->meth)
869*2139Sjp161948 		{
870*2139Sjp161948 		ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
871*2139Sjp161948 		return 0;
872*2139Sjp161948 		}
873*2139Sjp161948 	return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 
EC_POINT_get_affine_coordinates_GFp(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)8770Sstevel@tonic-gate int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
8780Sstevel@tonic-gate 	BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
8790Sstevel@tonic-gate 	{
880*2139Sjp161948 	if (group->meth->point_get_affine_coordinates == 0)
8810Sstevel@tonic-gate 		{
8820Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
8830Sstevel@tonic-gate 		return 0;
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 	if (group->meth != point->meth)
8860Sstevel@tonic-gate 		{
8870Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
8880Sstevel@tonic-gate 		return 0;
8890Sstevel@tonic-gate 		}
890*2139Sjp161948 	return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
891*2139Sjp161948 	}
892*2139Sjp161948 
893*2139Sjp161948 
EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)894*2139Sjp161948 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
895*2139Sjp161948 	BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
896*2139Sjp161948 	{
897*2139Sjp161948 	if (group->meth->point_get_affine_coordinates == 0)
898*2139Sjp161948 		{
899*2139Sjp161948 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
900*2139Sjp161948 		return 0;
901*2139Sjp161948 		}
902*2139Sjp161948 	if (group->meth != point->meth)
903*2139Sjp161948 		{
904*2139Sjp161948 		ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
905*2139Sjp161948 		return 0;
906*2139Sjp161948 		}
907*2139Sjp161948 	return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 
EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)9110Sstevel@tonic-gate int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
9120Sstevel@tonic-gate 	const BIGNUM *x, int y_bit, BN_CTX *ctx)
9130Sstevel@tonic-gate 	{
914*2139Sjp161948 	if (group->meth->point_set_compressed_coordinates == 0)
9150Sstevel@tonic-gate 		{
9160Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
9170Sstevel@tonic-gate 		return 0;
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 	if (group->meth != point->meth)
9200Sstevel@tonic-gate 		{
9210Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
9220Sstevel@tonic-gate 		return 0;
9230Sstevel@tonic-gate 		}
924*2139Sjp161948 	return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
925*2139Sjp161948 	}
926*2139Sjp161948 
927*2139Sjp161948 
EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)928*2139Sjp161948 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
929*2139Sjp161948 	const BIGNUM *x, int y_bit, BN_CTX *ctx)
930*2139Sjp161948 	{
931*2139Sjp161948 	if (group->meth->point_set_compressed_coordinates == 0)
932*2139Sjp161948 		{
933*2139Sjp161948 		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
934*2139Sjp161948 		return 0;
935*2139Sjp161948 		}
936*2139Sjp161948 	if (group->meth != point->meth)
937*2139Sjp161948 		{
938*2139Sjp161948 		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
939*2139Sjp161948 		return 0;
940*2139Sjp161948 		}
941*2139Sjp161948 	return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 
EC_POINT_point2oct(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,unsigned char * buf,size_t len,BN_CTX * ctx)9450Sstevel@tonic-gate size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
9460Sstevel@tonic-gate         unsigned char *buf, size_t len, BN_CTX *ctx)
9470Sstevel@tonic-gate 	{
9480Sstevel@tonic-gate 	if (group->meth->point2oct == 0)
9490Sstevel@tonic-gate 		{
9500Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
9510Sstevel@tonic-gate 		return 0;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 	if (group->meth != point->meth)
9540Sstevel@tonic-gate 		{
9550Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
9560Sstevel@tonic-gate 		return 0;
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 	return group->meth->point2oct(group, point, form, buf, len, ctx);
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 
EC_POINT_oct2point(const EC_GROUP * group,EC_POINT * point,const unsigned char * buf,size_t len,BN_CTX * ctx)9620Sstevel@tonic-gate int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
9630Sstevel@tonic-gate         const unsigned char *buf, size_t len, BN_CTX *ctx)
9640Sstevel@tonic-gate 	{
9650Sstevel@tonic-gate 	if (group->meth->oct2point == 0)
9660Sstevel@tonic-gate 		{
9670Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
9680Sstevel@tonic-gate 		return 0;
9690Sstevel@tonic-gate 		}
9700Sstevel@tonic-gate 	if (group->meth != point->meth)
9710Sstevel@tonic-gate 		{
9720Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
9730Sstevel@tonic-gate 		return 0;
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 	return group->meth->oct2point(group, point, buf, len, ctx);
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 
EC_POINT_add(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)9790Sstevel@tonic-gate int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
9800Sstevel@tonic-gate 	{
9810Sstevel@tonic-gate 	if (group->meth->add == 0)
9820Sstevel@tonic-gate 		{
9830Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
9840Sstevel@tonic-gate 		return 0;
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 	if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth))
9870Sstevel@tonic-gate 		{
9880Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
9890Sstevel@tonic-gate 		return 0;
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 	return group->meth->add(group, r, a, b, ctx);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 
EC_POINT_dbl(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,BN_CTX * ctx)9950Sstevel@tonic-gate int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
9960Sstevel@tonic-gate 	{
9970Sstevel@tonic-gate 	if (group->meth->dbl == 0)
9980Sstevel@tonic-gate 		{
9990Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10000Sstevel@tonic-gate 		return 0;
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 	if ((group->meth != r->meth) || (r->meth != a->meth))
10030Sstevel@tonic-gate 		{
10040Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
10050Sstevel@tonic-gate 		return 0;
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 	return group->meth->dbl(group, r, a, ctx);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 
EC_POINT_invert(const EC_GROUP * group,EC_POINT * a,BN_CTX * ctx)10110Sstevel@tonic-gate int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
10120Sstevel@tonic-gate 	{
10130Sstevel@tonic-gate 	if (group->meth->dbl == 0)
10140Sstevel@tonic-gate 		{
1015*2139Sjp161948 		ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10160Sstevel@tonic-gate 		return 0;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 	if (group->meth != a->meth)
10190Sstevel@tonic-gate 		{
1020*2139Sjp161948 		ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
10210Sstevel@tonic-gate 		return 0;
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 	return group->meth->invert(group, a, ctx);
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 
EC_POINT_is_at_infinity(const EC_GROUP * group,const EC_POINT * point)10270Sstevel@tonic-gate int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
10280Sstevel@tonic-gate 	{
10290Sstevel@tonic-gate 	if (group->meth->is_at_infinity == 0)
10300Sstevel@tonic-gate 		{
10310Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10320Sstevel@tonic-gate 		return 0;
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 	if (group->meth != point->meth)
10350Sstevel@tonic-gate 		{
10360Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
10370Sstevel@tonic-gate 		return 0;
10380Sstevel@tonic-gate 		}
10390Sstevel@tonic-gate 	return group->meth->is_at_infinity(group, point);
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 
EC_POINT_is_on_curve(const EC_GROUP * group,const EC_POINT * point,BN_CTX * ctx)10430Sstevel@tonic-gate int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
10440Sstevel@tonic-gate 	{
10450Sstevel@tonic-gate 	if (group->meth->is_on_curve == 0)
10460Sstevel@tonic-gate 		{
10470Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10480Sstevel@tonic-gate 		return 0;
10490Sstevel@tonic-gate 		}
10500Sstevel@tonic-gate 	if (group->meth != point->meth)
10510Sstevel@tonic-gate 		{
10520Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
10530Sstevel@tonic-gate 		return 0;
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 	return group->meth->is_on_curve(group, point, ctx);
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 
EC_POINT_cmp(const EC_GROUP * group,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)10590Sstevel@tonic-gate int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
10600Sstevel@tonic-gate 	{
10610Sstevel@tonic-gate 	if (group->meth->point_cmp == 0)
10620Sstevel@tonic-gate 		{
10630Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10640Sstevel@tonic-gate 		return 0;
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 	if ((group->meth != a->meth) || (a->meth != b->meth))
10670Sstevel@tonic-gate 		{
10680Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
10690Sstevel@tonic-gate 		return 0;
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 	return group->meth->point_cmp(group, a, b, ctx);
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 
EC_POINT_make_affine(const EC_GROUP * group,EC_POINT * point,BN_CTX * ctx)10750Sstevel@tonic-gate int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
10760Sstevel@tonic-gate 	{
10770Sstevel@tonic-gate 	if (group->meth->make_affine == 0)
10780Sstevel@tonic-gate 		{
10790Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10800Sstevel@tonic-gate 		return 0;
10810Sstevel@tonic-gate 		}
10820Sstevel@tonic-gate 	if (group->meth != point->meth)
10830Sstevel@tonic-gate 		{
10840Sstevel@tonic-gate 		ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
10850Sstevel@tonic-gate 		return 0;
10860Sstevel@tonic-gate 		}
10870Sstevel@tonic-gate 	return group->meth->make_affine(group, point, ctx);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 
EC_POINTs_make_affine(const EC_GROUP * group,size_t num,EC_POINT * points[],BN_CTX * ctx)10910Sstevel@tonic-gate int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
10920Sstevel@tonic-gate 	{
10930Sstevel@tonic-gate 	size_t i;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (group->meth->points_make_affine == 0)
10960Sstevel@tonic-gate 		{
10970Sstevel@tonic-gate 		ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
10980Sstevel@tonic-gate 		return 0;
10990Sstevel@tonic-gate 		}
11000Sstevel@tonic-gate 	for (i = 0; i < num; i++)
11010Sstevel@tonic-gate 		{
11020Sstevel@tonic-gate 		if (group->meth != points[i]->meth)
11030Sstevel@tonic-gate 			{
11040Sstevel@tonic-gate 			ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
11050Sstevel@tonic-gate 			return 0;
11060Sstevel@tonic-gate 			}
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 	return group->meth->points_make_affine(group, num, points, ctx);
11090Sstevel@tonic-gate 	}
1110*2139Sjp161948 
1111*2139Sjp161948 
1112*2139Sjp161948 /* Functions for point multiplication.
1113*2139Sjp161948  *
1114*2139Sjp161948  * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1115*2139Sjp161948  * otherwise we dispatch through methods.
1116*2139Sjp161948  */
1117*2139Sjp161948 
EC_POINTs_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * scalar,size_t num,const EC_POINT * points[],const BIGNUM * scalars[],BN_CTX * ctx)1118*2139Sjp161948 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1119*2139Sjp161948 	size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1120*2139Sjp161948 	{
1121*2139Sjp161948 	if (group->meth->mul == 0)
1122*2139Sjp161948 		/* use default */
1123*2139Sjp161948 		return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1124*2139Sjp161948 
1125*2139Sjp161948 	return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1126*2139Sjp161948 	}
1127*2139Sjp161948 
EC_POINT_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * g_scalar,const EC_POINT * point,const BIGNUM * p_scalar,BN_CTX * ctx)1128*2139Sjp161948 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1129*2139Sjp161948 	const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1130*2139Sjp161948 	{
1131*2139Sjp161948 	/* just a convenient interface to EC_POINTs_mul() */
1132*2139Sjp161948 
1133*2139Sjp161948 	const EC_POINT *points[1];
1134*2139Sjp161948 	const BIGNUM *scalars[1];
1135*2139Sjp161948 
1136*2139Sjp161948 	points[0] = point;
1137*2139Sjp161948 	scalars[0] = p_scalar;
1138*2139Sjp161948 
1139*2139Sjp161948 	return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
1140*2139Sjp161948 	}
1141*2139Sjp161948 
EC_GROUP_precompute_mult(EC_GROUP * group,BN_CTX * ctx)1142*2139Sjp161948 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1143*2139Sjp161948 	{
1144*2139Sjp161948 	if (group->meth->mul == 0)
1145*2139Sjp161948 		/* use default */
1146*2139Sjp161948 		return ec_wNAF_precompute_mult(group, ctx);
1147*2139Sjp161948 
1148*2139Sjp161948 	if (group->meth->precompute_mult != 0)
1149*2139Sjp161948 		return group->meth->precompute_mult(group, ctx);
1150*2139Sjp161948 	else
1151*2139Sjp161948 		return 1; /* nothing to do, so report success */
1152*2139Sjp161948 	}
1153*2139Sjp161948 
EC_GROUP_have_precompute_mult(const EC_GROUP * group)1154*2139Sjp161948 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1155*2139Sjp161948 	{
1156*2139Sjp161948 	if (group->meth->mul == 0)
1157*2139Sjp161948 		/* use default */
1158*2139Sjp161948 		return ec_wNAF_have_precompute_mult(group);
1159*2139Sjp161948 
1160*2139Sjp161948 	if (group->meth->have_precompute_mult != 0)
1161*2139Sjp161948 		return group->meth->have_precompute_mult(group);
1162*2139Sjp161948 	else
1163*2139Sjp161948 		return 0; /* cannot tell whether precomputation has been performed */
1164*2139Sjp161948 	}
1165