xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ec2_smpl.c (revision 2139:6243c3338933)
1*2139Sjp161948 /* crypto/ec/ec2_smpl.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4*2139Sjp161948  *
5*2139Sjp161948  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6*2139Sjp161948  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7*2139Sjp161948  * to the OpenSSL project.
8*2139Sjp161948  *
9*2139Sjp161948  * The ECC Code is licensed pursuant to the OpenSSL open source
10*2139Sjp161948  * license provided below.
11*2139Sjp161948  *
12*2139Sjp161948  * The software is originally written by Sheueling Chang Shantz and
13*2139Sjp161948  * Douglas Stebila of Sun Microsystems Laboratories.
14*2139Sjp161948  *
15*2139Sjp161948  */
16*2139Sjp161948 /* ====================================================================
17*2139Sjp161948  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
18*2139Sjp161948  *
19*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
20*2139Sjp161948  * modification, are permitted provided that the following conditions
21*2139Sjp161948  * are met:
22*2139Sjp161948  *
23*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
24*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
25*2139Sjp161948  *
26*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
27*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in
28*2139Sjp161948  *    the documentation and/or other materials provided with the
29*2139Sjp161948  *    distribution.
30*2139Sjp161948  *
31*2139Sjp161948  * 3. All advertising materials mentioning features or use of this
32*2139Sjp161948  *    software must display the following acknowledgment:
33*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
34*2139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35*2139Sjp161948  *
36*2139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37*2139Sjp161948  *    endorse or promote products derived from this software without
38*2139Sjp161948  *    prior written permission. For written permission, please contact
39*2139Sjp161948  *    openssl-core@openssl.org.
40*2139Sjp161948  *
41*2139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
42*2139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
43*2139Sjp161948  *    permission of the OpenSSL Project.
44*2139Sjp161948  *
45*2139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
46*2139Sjp161948  *    acknowledgment:
47*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
48*2139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49*2139Sjp161948  *
50*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51*2139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52*2139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53*2139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54*2139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56*2139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57*2139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*2139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59*2139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60*2139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61*2139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
62*2139Sjp161948  * ====================================================================
63*2139Sjp161948  *
64*2139Sjp161948  * This product includes cryptographic software written by Eric Young
65*2139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
66*2139Sjp161948  * Hudson (tjh@cryptsoft.com).
67*2139Sjp161948  *
68*2139Sjp161948  */
69*2139Sjp161948 
70*2139Sjp161948 #include <openssl/err.h>
71*2139Sjp161948 
72*2139Sjp161948 #include "ec_lcl.h"
73*2139Sjp161948 
74*2139Sjp161948 
EC_GF2m_simple_method(void)75*2139Sjp161948 const EC_METHOD *EC_GF2m_simple_method(void)
76*2139Sjp161948 	{
77*2139Sjp161948 	static const EC_METHOD ret = {
78*2139Sjp161948 		NID_X9_62_characteristic_two_field,
79*2139Sjp161948 		ec_GF2m_simple_group_init,
80*2139Sjp161948 		ec_GF2m_simple_group_finish,
81*2139Sjp161948 		ec_GF2m_simple_group_clear_finish,
82*2139Sjp161948 		ec_GF2m_simple_group_copy,
83*2139Sjp161948 		ec_GF2m_simple_group_set_curve,
84*2139Sjp161948 		ec_GF2m_simple_group_get_curve,
85*2139Sjp161948 		ec_GF2m_simple_group_get_degree,
86*2139Sjp161948 		ec_GF2m_simple_group_check_discriminant,
87*2139Sjp161948 		ec_GF2m_simple_point_init,
88*2139Sjp161948 		ec_GF2m_simple_point_finish,
89*2139Sjp161948 		ec_GF2m_simple_point_clear_finish,
90*2139Sjp161948 		ec_GF2m_simple_point_copy,
91*2139Sjp161948 		ec_GF2m_simple_point_set_to_infinity,
92*2139Sjp161948 		0 /* set_Jprojective_coordinates_GFp */,
93*2139Sjp161948 		0 /* get_Jprojective_coordinates_GFp */,
94*2139Sjp161948 		ec_GF2m_simple_point_set_affine_coordinates,
95*2139Sjp161948 		ec_GF2m_simple_point_get_affine_coordinates,
96*2139Sjp161948 		ec_GF2m_simple_set_compressed_coordinates,
97*2139Sjp161948 		ec_GF2m_simple_point2oct,
98*2139Sjp161948 		ec_GF2m_simple_oct2point,
99*2139Sjp161948 		ec_GF2m_simple_add,
100*2139Sjp161948 		ec_GF2m_simple_dbl,
101*2139Sjp161948 		ec_GF2m_simple_invert,
102*2139Sjp161948 		ec_GF2m_simple_is_at_infinity,
103*2139Sjp161948 		ec_GF2m_simple_is_on_curve,
104*2139Sjp161948 		ec_GF2m_simple_cmp,
105*2139Sjp161948 		ec_GF2m_simple_make_affine,
106*2139Sjp161948 		ec_GF2m_simple_points_make_affine,
107*2139Sjp161948 
108*2139Sjp161948 		/* the following three method functions are defined in ec2_mult.c */
109*2139Sjp161948 		ec_GF2m_simple_mul,
110*2139Sjp161948 		ec_GF2m_precompute_mult,
111*2139Sjp161948 		ec_GF2m_have_precompute_mult,
112*2139Sjp161948 
113*2139Sjp161948 		ec_GF2m_simple_field_mul,
114*2139Sjp161948 		ec_GF2m_simple_field_sqr,
115*2139Sjp161948 		ec_GF2m_simple_field_div,
116*2139Sjp161948 		0 /* field_encode */,
117*2139Sjp161948 		0 /* field_decode */,
118*2139Sjp161948 		0 /* field_set_to_one */ };
119*2139Sjp161948 
120*2139Sjp161948 	return &ret;
121*2139Sjp161948 	}
122*2139Sjp161948 
123*2139Sjp161948 
124*2139Sjp161948 /* Initialize a GF(2^m)-based EC_GROUP structure.
125*2139Sjp161948  * Note that all other members are handled by EC_GROUP_new.
126*2139Sjp161948  */
ec_GF2m_simple_group_init(EC_GROUP * group)127*2139Sjp161948 int ec_GF2m_simple_group_init(EC_GROUP *group)
128*2139Sjp161948 	{
129*2139Sjp161948 	BN_init(&group->field);
130*2139Sjp161948 	BN_init(&group->a);
131*2139Sjp161948 	BN_init(&group->b);
132*2139Sjp161948 	return 1;
133*2139Sjp161948 	}
134*2139Sjp161948 
135*2139Sjp161948 
136*2139Sjp161948 /* Free a GF(2^m)-based EC_GROUP structure.
137*2139Sjp161948  * Note that all other members are handled by EC_GROUP_free.
138*2139Sjp161948  */
ec_GF2m_simple_group_finish(EC_GROUP * group)139*2139Sjp161948 void ec_GF2m_simple_group_finish(EC_GROUP *group)
140*2139Sjp161948 	{
141*2139Sjp161948 	BN_free(&group->field);
142*2139Sjp161948 	BN_free(&group->a);
143*2139Sjp161948 	BN_free(&group->b);
144*2139Sjp161948 	}
145*2139Sjp161948 
146*2139Sjp161948 
147*2139Sjp161948 /* Clear and free a GF(2^m)-based EC_GROUP structure.
148*2139Sjp161948  * Note that all other members are handled by EC_GROUP_clear_free.
149*2139Sjp161948  */
ec_GF2m_simple_group_clear_finish(EC_GROUP * group)150*2139Sjp161948 void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
151*2139Sjp161948 	{
152*2139Sjp161948 	BN_clear_free(&group->field);
153*2139Sjp161948 	BN_clear_free(&group->a);
154*2139Sjp161948 	BN_clear_free(&group->b);
155*2139Sjp161948 	group->poly[0] = 0;
156*2139Sjp161948 	group->poly[1] = 0;
157*2139Sjp161948 	group->poly[2] = 0;
158*2139Sjp161948 	group->poly[3] = 0;
159*2139Sjp161948 	group->poly[4] = 0;
160*2139Sjp161948 	}
161*2139Sjp161948 
162*2139Sjp161948 
163*2139Sjp161948 /* Copy a GF(2^m)-based EC_GROUP structure.
164*2139Sjp161948  * Note that all other members are handled by EC_GROUP_copy.
165*2139Sjp161948  */
ec_GF2m_simple_group_copy(EC_GROUP * dest,const EC_GROUP * src)166*2139Sjp161948 int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
167*2139Sjp161948 	{
168*2139Sjp161948 	int i;
169*2139Sjp161948 	if (!BN_copy(&dest->field, &src->field)) return 0;
170*2139Sjp161948 	if (!BN_copy(&dest->a, &src->a)) return 0;
171*2139Sjp161948 	if (!BN_copy(&dest->b, &src->b)) return 0;
172*2139Sjp161948 	dest->poly[0] = src->poly[0];
173*2139Sjp161948 	dest->poly[1] = src->poly[1];
174*2139Sjp161948 	dest->poly[2] = src->poly[2];
175*2139Sjp161948 	dest->poly[3] = src->poly[3];
176*2139Sjp161948 	dest->poly[4] = src->poly[4];
177*2139Sjp161948 	bn_wexpand(&dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2);
178*2139Sjp161948 	bn_wexpand(&dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2);
179*2139Sjp161948 	for (i = dest->a.top; i < dest->a.dmax; i++) dest->a.d[i] = 0;
180*2139Sjp161948 	for (i = dest->b.top; i < dest->b.dmax; i++) dest->b.d[i] = 0;
181*2139Sjp161948 	return 1;
182*2139Sjp161948 	}
183*2139Sjp161948 
184*2139Sjp161948 
185*2139Sjp161948 /* Set the curve parameters of an EC_GROUP structure. */
ec_GF2m_simple_group_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)186*2139Sjp161948 int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
187*2139Sjp161948 	const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
188*2139Sjp161948 	{
189*2139Sjp161948 	int ret = 0, i;
190*2139Sjp161948 
191*2139Sjp161948 	/* group->field */
192*2139Sjp161948 	if (!BN_copy(&group->field, p)) goto err;
193*2139Sjp161948 	i = BN_GF2m_poly2arr(&group->field, group->poly, 5);
194*2139Sjp161948 	if ((i != 5) && (i != 3))
195*2139Sjp161948 		{
196*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
197*2139Sjp161948 		goto err;
198*2139Sjp161948 		}
199*2139Sjp161948 
200*2139Sjp161948 	/* group->a */
201*2139Sjp161948 	if (!BN_GF2m_mod_arr(&group->a, a, group->poly)) goto err;
202*2139Sjp161948 	bn_wexpand(&group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2);
203*2139Sjp161948 	for (i = group->a.top; i < group->a.dmax; i++) group->a.d[i] = 0;
204*2139Sjp161948 
205*2139Sjp161948 	/* group->b */
206*2139Sjp161948 	if (!BN_GF2m_mod_arr(&group->b, b, group->poly)) goto err;
207*2139Sjp161948 	bn_wexpand(&group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2);
208*2139Sjp161948 	for (i = group->b.top; i < group->b.dmax; i++) group->b.d[i] = 0;
209*2139Sjp161948 
210*2139Sjp161948 	ret = 1;
211*2139Sjp161948   err:
212*2139Sjp161948 	return ret;
213*2139Sjp161948 	}
214*2139Sjp161948 
215*2139Sjp161948 
216*2139Sjp161948 /* Get the curve parameters of an EC_GROUP structure.
217*2139Sjp161948  * If p, a, or b are NULL then there values will not be set but the method will return with success.
218*2139Sjp161948  */
ec_GF2m_simple_group_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)219*2139Sjp161948 int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
220*2139Sjp161948 	{
221*2139Sjp161948 	int ret = 0;
222*2139Sjp161948 
223*2139Sjp161948 	if (p != NULL)
224*2139Sjp161948 		{
225*2139Sjp161948 		if (!BN_copy(p, &group->field)) return 0;
226*2139Sjp161948 		}
227*2139Sjp161948 
228*2139Sjp161948 	if (a != NULL)
229*2139Sjp161948 		{
230*2139Sjp161948 		if (!BN_copy(a, &group->a)) goto err;
231*2139Sjp161948 		}
232*2139Sjp161948 
233*2139Sjp161948 	if (b != NULL)
234*2139Sjp161948 		{
235*2139Sjp161948 		if (!BN_copy(b, &group->b)) goto err;
236*2139Sjp161948 		}
237*2139Sjp161948 
238*2139Sjp161948 	ret = 1;
239*2139Sjp161948 
240*2139Sjp161948   err:
241*2139Sjp161948 	return ret;
242*2139Sjp161948 	}
243*2139Sjp161948 
244*2139Sjp161948 
245*2139Sjp161948 /* Gets the degree of the field.  For a curve over GF(2^m) this is the value m. */
ec_GF2m_simple_group_get_degree(const EC_GROUP * group)246*2139Sjp161948 int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
247*2139Sjp161948 	{
248*2139Sjp161948 	return BN_num_bits(&group->field)-1;
249*2139Sjp161948 	}
250*2139Sjp161948 
251*2139Sjp161948 
252*2139Sjp161948 /* Checks the discriminant of the curve.
253*2139Sjp161948  * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
254*2139Sjp161948  */
ec_GF2m_simple_group_check_discriminant(const EC_GROUP * group,BN_CTX * ctx)255*2139Sjp161948 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
256*2139Sjp161948 	{
257*2139Sjp161948 	int ret = 0;
258*2139Sjp161948 	BIGNUM *b;
259*2139Sjp161948 	BN_CTX *new_ctx = NULL;
260*2139Sjp161948 
261*2139Sjp161948 	if (ctx == NULL)
262*2139Sjp161948 		{
263*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
264*2139Sjp161948 		if (ctx == NULL)
265*2139Sjp161948 			{
266*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
267*2139Sjp161948 			goto err;
268*2139Sjp161948 			}
269*2139Sjp161948 		}
270*2139Sjp161948 	BN_CTX_start(ctx);
271*2139Sjp161948 	b = BN_CTX_get(ctx);
272*2139Sjp161948 	if (b == NULL) goto err;
273*2139Sjp161948 
274*2139Sjp161948 	if (!BN_GF2m_mod_arr(b, &group->b, group->poly)) goto err;
275*2139Sjp161948 
276*2139Sjp161948 	/* check the discriminant:
277*2139Sjp161948 	 * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
278*2139Sjp161948 	 */
279*2139Sjp161948 	if (BN_is_zero(b)) goto err;
280*2139Sjp161948 
281*2139Sjp161948 	ret = 1;
282*2139Sjp161948 
283*2139Sjp161948 err:
284*2139Sjp161948 	BN_CTX_end(ctx);
285*2139Sjp161948 	if (new_ctx != NULL)
286*2139Sjp161948 		BN_CTX_free(new_ctx);
287*2139Sjp161948 	return ret;
288*2139Sjp161948 	}
289*2139Sjp161948 
290*2139Sjp161948 
291*2139Sjp161948 /* Initializes an EC_POINT. */
ec_GF2m_simple_point_init(EC_POINT * point)292*2139Sjp161948 int ec_GF2m_simple_point_init(EC_POINT *point)
293*2139Sjp161948 	{
294*2139Sjp161948 	BN_init(&point->X);
295*2139Sjp161948 	BN_init(&point->Y);
296*2139Sjp161948 	BN_init(&point->Z);
297*2139Sjp161948 	return 1;
298*2139Sjp161948 	}
299*2139Sjp161948 
300*2139Sjp161948 
301*2139Sjp161948 /* Frees an EC_POINT. */
ec_GF2m_simple_point_finish(EC_POINT * point)302*2139Sjp161948 void ec_GF2m_simple_point_finish(EC_POINT *point)
303*2139Sjp161948 	{
304*2139Sjp161948 	BN_free(&point->X);
305*2139Sjp161948 	BN_free(&point->Y);
306*2139Sjp161948 	BN_free(&point->Z);
307*2139Sjp161948 	}
308*2139Sjp161948 
309*2139Sjp161948 
310*2139Sjp161948 /* Clears and frees an EC_POINT. */
ec_GF2m_simple_point_clear_finish(EC_POINT * point)311*2139Sjp161948 void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
312*2139Sjp161948 	{
313*2139Sjp161948 	BN_clear_free(&point->X);
314*2139Sjp161948 	BN_clear_free(&point->Y);
315*2139Sjp161948 	BN_clear_free(&point->Z);
316*2139Sjp161948 	point->Z_is_one = 0;
317*2139Sjp161948 	}
318*2139Sjp161948 
319*2139Sjp161948 
320*2139Sjp161948 /* Copy the contents of one EC_POINT into another.  Assumes dest is initialized. */
ec_GF2m_simple_point_copy(EC_POINT * dest,const EC_POINT * src)321*2139Sjp161948 int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
322*2139Sjp161948 	{
323*2139Sjp161948 	if (!BN_copy(&dest->X, &src->X)) return 0;
324*2139Sjp161948 	if (!BN_copy(&dest->Y, &src->Y)) return 0;
325*2139Sjp161948 	if (!BN_copy(&dest->Z, &src->Z)) return 0;
326*2139Sjp161948 	dest->Z_is_one = src->Z_is_one;
327*2139Sjp161948 
328*2139Sjp161948 	return 1;
329*2139Sjp161948 	}
330*2139Sjp161948 
331*2139Sjp161948 
332*2139Sjp161948 /* Set an EC_POINT to the point at infinity.
333*2139Sjp161948  * A point at infinity is represented by having Z=0.
334*2139Sjp161948  */
ec_GF2m_simple_point_set_to_infinity(const EC_GROUP * group,EC_POINT * point)335*2139Sjp161948 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
336*2139Sjp161948 	{
337*2139Sjp161948 	point->Z_is_one = 0;
338*2139Sjp161948 	BN_zero(&point->Z);
339*2139Sjp161948 	return 1;
340*2139Sjp161948 	}
341*2139Sjp161948 
342*2139Sjp161948 
343*2139Sjp161948 /* Set the coordinates of an EC_POINT using affine coordinates.
344*2139Sjp161948  * Note that the simple implementation only uses affine coordinates.
345*2139Sjp161948  */
ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)346*2139Sjp161948 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
347*2139Sjp161948 	const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
348*2139Sjp161948 	{
349*2139Sjp161948 	int ret = 0;
350*2139Sjp161948 	if (x == NULL || y == NULL)
351*2139Sjp161948 		{
352*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
353*2139Sjp161948 		return 0;
354*2139Sjp161948 		}
355*2139Sjp161948 
356*2139Sjp161948 	if (!BN_copy(&point->X, x)) goto err;
357*2139Sjp161948 	BN_set_negative(&point->X, 0);
358*2139Sjp161948 	if (!BN_copy(&point->Y, y)) goto err;
359*2139Sjp161948 	BN_set_negative(&point->Y, 0);
360*2139Sjp161948 	if (!BN_copy(&point->Z, BN_value_one())) goto err;
361*2139Sjp161948 	BN_set_negative(&point->Z, 0);
362*2139Sjp161948 	point->Z_is_one = 1;
363*2139Sjp161948 	ret = 1;
364*2139Sjp161948 
365*2139Sjp161948   err:
366*2139Sjp161948 	return ret;
367*2139Sjp161948 	}
368*2139Sjp161948 
369*2139Sjp161948 
370*2139Sjp161948 /* Gets the affine coordinates of an EC_POINT.
371*2139Sjp161948  * Note that the simple implementation only uses affine coordinates.
372*2139Sjp161948  */
ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)373*2139Sjp161948 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
374*2139Sjp161948 	BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
375*2139Sjp161948 	{
376*2139Sjp161948 	int ret = 0;
377*2139Sjp161948 
378*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, point))
379*2139Sjp161948 		{
380*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
381*2139Sjp161948 		return 0;
382*2139Sjp161948 		}
383*2139Sjp161948 
384*2139Sjp161948 	if (BN_cmp(&point->Z, BN_value_one()))
385*2139Sjp161948 		{
386*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
387*2139Sjp161948 		return 0;
388*2139Sjp161948 		}
389*2139Sjp161948 	if (x != NULL)
390*2139Sjp161948 		{
391*2139Sjp161948 		if (!BN_copy(x, &point->X)) goto err;
392*2139Sjp161948 		BN_set_negative(x, 0);
393*2139Sjp161948 		}
394*2139Sjp161948 	if (y != NULL)
395*2139Sjp161948 		{
396*2139Sjp161948 		if (!BN_copy(y, &point->Y)) goto err;
397*2139Sjp161948 		BN_set_negative(y, 0);
398*2139Sjp161948 		}
399*2139Sjp161948 	ret = 1;
400*2139Sjp161948 
401*2139Sjp161948  err:
402*2139Sjp161948 	return ret;
403*2139Sjp161948 	}
404*2139Sjp161948 
405*2139Sjp161948 
406*2139Sjp161948 /* Include patented algorithms. */
407*2139Sjp161948 #include "ec2_smpt.c"
408*2139Sjp161948 
409*2139Sjp161948 
410*2139Sjp161948 /* Converts an EC_POINT to an octet string.
411*2139Sjp161948  * If buf is NULL, the encoded length will be returned.
412*2139Sjp161948  * If the length len of buf is smaller than required an error will be returned.
413*2139Sjp161948  *
414*2139Sjp161948  * The point compression section of this function is patented by Certicom Corp.
415*2139Sjp161948  * under US Patent 6,141,420.  Point compression is disabled by default and can
416*2139Sjp161948  * be enabled by defining the preprocessor macro OPENSSL_EC_BIN_PT_COMP at
417*2139Sjp161948  * Configure-time.
418*2139Sjp161948  */
ec_GF2m_simple_point2oct(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,unsigned char * buf,size_t len,BN_CTX * ctx)419*2139Sjp161948 size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
420*2139Sjp161948 	unsigned char *buf, size_t len, BN_CTX *ctx)
421*2139Sjp161948 	{
422*2139Sjp161948 	size_t ret;
423*2139Sjp161948 	BN_CTX *new_ctx = NULL;
424*2139Sjp161948 	int used_ctx = 0;
425*2139Sjp161948 	BIGNUM *x, *y, *yxi;
426*2139Sjp161948 	size_t field_len, i, skip;
427*2139Sjp161948 
428*2139Sjp161948 #ifndef OPENSSL_EC_BIN_PT_COMP
429*2139Sjp161948 	if ((form == POINT_CONVERSION_COMPRESSED) || (form == POINT_CONVERSION_HYBRID))
430*2139Sjp161948 		{
431*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_DISABLED);
432*2139Sjp161948 		goto err;
433*2139Sjp161948 		}
434*2139Sjp161948 #endif
435*2139Sjp161948 
436*2139Sjp161948 	if ((form != POINT_CONVERSION_COMPRESSED)
437*2139Sjp161948 		&& (form != POINT_CONVERSION_UNCOMPRESSED)
438*2139Sjp161948 		&& (form != POINT_CONVERSION_HYBRID))
439*2139Sjp161948 		{
440*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
441*2139Sjp161948 		goto err;
442*2139Sjp161948 		}
443*2139Sjp161948 
444*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, point))
445*2139Sjp161948 		{
446*2139Sjp161948 		/* encodes to a single 0 octet */
447*2139Sjp161948 		if (buf != NULL)
448*2139Sjp161948 			{
449*2139Sjp161948 			if (len < 1)
450*2139Sjp161948 				{
451*2139Sjp161948 				ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
452*2139Sjp161948 				return 0;
453*2139Sjp161948 				}
454*2139Sjp161948 			buf[0] = 0;
455*2139Sjp161948 			}
456*2139Sjp161948 		return 1;
457*2139Sjp161948 		}
458*2139Sjp161948 
459*2139Sjp161948 
460*2139Sjp161948 	/* ret := required output buffer length */
461*2139Sjp161948 	field_len = (EC_GROUP_get_degree(group) + 7) / 8;
462*2139Sjp161948 	ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
463*2139Sjp161948 
464*2139Sjp161948 	/* if 'buf' is NULL, just return required length */
465*2139Sjp161948 	if (buf != NULL)
466*2139Sjp161948 		{
467*2139Sjp161948 		if (len < ret)
468*2139Sjp161948 			{
469*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
470*2139Sjp161948 			goto err;
471*2139Sjp161948 			}
472*2139Sjp161948 
473*2139Sjp161948 		if (ctx == NULL)
474*2139Sjp161948 			{
475*2139Sjp161948 			ctx = new_ctx = BN_CTX_new();
476*2139Sjp161948 			if (ctx == NULL)
477*2139Sjp161948 				return 0;
478*2139Sjp161948 			}
479*2139Sjp161948 
480*2139Sjp161948 		BN_CTX_start(ctx);
481*2139Sjp161948 		used_ctx = 1;
482*2139Sjp161948 		x = BN_CTX_get(ctx);
483*2139Sjp161948 		y = BN_CTX_get(ctx);
484*2139Sjp161948 		yxi = BN_CTX_get(ctx);
485*2139Sjp161948 		if (yxi == NULL) goto err;
486*2139Sjp161948 
487*2139Sjp161948 		if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
488*2139Sjp161948 
489*2139Sjp161948 		buf[0] = form;
490*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
491*2139Sjp161948 		if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x))
492*2139Sjp161948 			{
493*2139Sjp161948 			if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
494*2139Sjp161948 			if (BN_is_odd(yxi)) buf[0]++;
495*2139Sjp161948 			}
496*2139Sjp161948 #endif
497*2139Sjp161948 
498*2139Sjp161948 		i = 1;
499*2139Sjp161948 
500*2139Sjp161948 		skip = field_len - BN_num_bytes(x);
501*2139Sjp161948 		if (skip > field_len)
502*2139Sjp161948 			{
503*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
504*2139Sjp161948 			goto err;
505*2139Sjp161948 			}
506*2139Sjp161948 		while (skip > 0)
507*2139Sjp161948 			{
508*2139Sjp161948 			buf[i++] = 0;
509*2139Sjp161948 			skip--;
510*2139Sjp161948 			}
511*2139Sjp161948 		skip = BN_bn2bin(x, buf + i);
512*2139Sjp161948 		i += skip;
513*2139Sjp161948 		if (i != 1 + field_len)
514*2139Sjp161948 			{
515*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
516*2139Sjp161948 			goto err;
517*2139Sjp161948 			}
518*2139Sjp161948 
519*2139Sjp161948 		if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
520*2139Sjp161948 			{
521*2139Sjp161948 			skip = field_len - BN_num_bytes(y);
522*2139Sjp161948 			if (skip > field_len)
523*2139Sjp161948 				{
524*2139Sjp161948 				ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
525*2139Sjp161948 				goto err;
526*2139Sjp161948 				}
527*2139Sjp161948 			while (skip > 0)
528*2139Sjp161948 				{
529*2139Sjp161948 				buf[i++] = 0;
530*2139Sjp161948 				skip--;
531*2139Sjp161948 				}
532*2139Sjp161948 			skip = BN_bn2bin(y, buf + i);
533*2139Sjp161948 			i += skip;
534*2139Sjp161948 			}
535*2139Sjp161948 
536*2139Sjp161948 		if (i != ret)
537*2139Sjp161948 			{
538*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
539*2139Sjp161948 			goto err;
540*2139Sjp161948 			}
541*2139Sjp161948 		}
542*2139Sjp161948 
543*2139Sjp161948 	if (used_ctx)
544*2139Sjp161948 		BN_CTX_end(ctx);
545*2139Sjp161948 	if (new_ctx != NULL)
546*2139Sjp161948 		BN_CTX_free(new_ctx);
547*2139Sjp161948 	return ret;
548*2139Sjp161948 
549*2139Sjp161948  err:
550*2139Sjp161948 	if (used_ctx)
551*2139Sjp161948 		BN_CTX_end(ctx);
552*2139Sjp161948 	if (new_ctx != NULL)
553*2139Sjp161948 		BN_CTX_free(new_ctx);
554*2139Sjp161948 	return 0;
555*2139Sjp161948 	}
556*2139Sjp161948 
557*2139Sjp161948 
558*2139Sjp161948 /* Converts an octet string representation to an EC_POINT.
559*2139Sjp161948  * Note that the simple implementation only uses affine coordinates.
560*2139Sjp161948  */
ec_GF2m_simple_oct2point(const EC_GROUP * group,EC_POINT * point,const unsigned char * buf,size_t len,BN_CTX * ctx)561*2139Sjp161948 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
562*2139Sjp161948 	const unsigned char *buf, size_t len, BN_CTX *ctx)
563*2139Sjp161948 	{
564*2139Sjp161948 	point_conversion_form_t form;
565*2139Sjp161948 	int y_bit;
566*2139Sjp161948 	BN_CTX *new_ctx = NULL;
567*2139Sjp161948 	BIGNUM *x, *y, *yxi;
568*2139Sjp161948 	size_t field_len, enc_len;
569*2139Sjp161948 	int ret = 0;
570*2139Sjp161948 
571*2139Sjp161948 	if (len == 0)
572*2139Sjp161948 		{
573*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
574*2139Sjp161948 		return 0;
575*2139Sjp161948 		}
576*2139Sjp161948 	form = buf[0];
577*2139Sjp161948 	y_bit = form & 1;
578*2139Sjp161948 	form = form & ~1U;
579*2139Sjp161948 	if ((form != 0)	&& (form != POINT_CONVERSION_COMPRESSED)
580*2139Sjp161948 		&& (form != POINT_CONVERSION_UNCOMPRESSED)
581*2139Sjp161948 		&& (form != POINT_CONVERSION_HYBRID))
582*2139Sjp161948 		{
583*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
584*2139Sjp161948 		return 0;
585*2139Sjp161948 		}
586*2139Sjp161948 	if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
587*2139Sjp161948 		{
588*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
589*2139Sjp161948 		return 0;
590*2139Sjp161948 		}
591*2139Sjp161948 
592*2139Sjp161948 	if (form == 0)
593*2139Sjp161948 		{
594*2139Sjp161948 		if (len != 1)
595*2139Sjp161948 			{
596*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
597*2139Sjp161948 			return 0;
598*2139Sjp161948 			}
599*2139Sjp161948 
600*2139Sjp161948 		return EC_POINT_set_to_infinity(group, point);
601*2139Sjp161948 		}
602*2139Sjp161948 
603*2139Sjp161948 	field_len = (EC_GROUP_get_degree(group) + 7) / 8;
604*2139Sjp161948 	enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
605*2139Sjp161948 
606*2139Sjp161948 	if (len != enc_len)
607*2139Sjp161948 		{
608*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
609*2139Sjp161948 		return 0;
610*2139Sjp161948 		}
611*2139Sjp161948 
612*2139Sjp161948 	if (ctx == NULL)
613*2139Sjp161948 		{
614*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
615*2139Sjp161948 		if (ctx == NULL)
616*2139Sjp161948 			return 0;
617*2139Sjp161948 		}
618*2139Sjp161948 
619*2139Sjp161948 	BN_CTX_start(ctx);
620*2139Sjp161948 	x = BN_CTX_get(ctx);
621*2139Sjp161948 	y = BN_CTX_get(ctx);
622*2139Sjp161948 	yxi = BN_CTX_get(ctx);
623*2139Sjp161948 	if (yxi == NULL) goto err;
624*2139Sjp161948 
625*2139Sjp161948 	if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
626*2139Sjp161948 	if (BN_ucmp(x, &group->field) >= 0)
627*2139Sjp161948 		{
628*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
629*2139Sjp161948 		goto err;
630*2139Sjp161948 		}
631*2139Sjp161948 
632*2139Sjp161948 	if (form == POINT_CONVERSION_COMPRESSED)
633*2139Sjp161948 		{
634*2139Sjp161948 		if (!EC_POINT_set_compressed_coordinates_GF2m(group, point, x, y_bit, ctx)) goto err;
635*2139Sjp161948 		}
636*2139Sjp161948 	else
637*2139Sjp161948 		{
638*2139Sjp161948 		if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
639*2139Sjp161948 		if (BN_ucmp(y, &group->field) >= 0)
640*2139Sjp161948 			{
641*2139Sjp161948 			ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
642*2139Sjp161948 			goto err;
643*2139Sjp161948 			}
644*2139Sjp161948 		if (form == POINT_CONVERSION_HYBRID)
645*2139Sjp161948 			{
646*2139Sjp161948 			if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
647*2139Sjp161948 			if (y_bit != BN_is_odd(yxi))
648*2139Sjp161948 				{
649*2139Sjp161948 				ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
650*2139Sjp161948 				goto err;
651*2139Sjp161948 				}
652*2139Sjp161948 			}
653*2139Sjp161948 
654*2139Sjp161948 		if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
655*2139Sjp161948 		}
656*2139Sjp161948 
657*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
658*2139Sjp161948 		{
659*2139Sjp161948 		ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
660*2139Sjp161948 		goto err;
661*2139Sjp161948 		}
662*2139Sjp161948 
663*2139Sjp161948 	ret = 1;
664*2139Sjp161948 
665*2139Sjp161948  err:
666*2139Sjp161948 	BN_CTX_end(ctx);
667*2139Sjp161948 	if (new_ctx != NULL)
668*2139Sjp161948 		BN_CTX_free(new_ctx);
669*2139Sjp161948 	return ret;
670*2139Sjp161948 	}
671*2139Sjp161948 
672*2139Sjp161948 
673*2139Sjp161948 /* Computes a + b and stores the result in r.  r could be a or b, a could be b.
674*2139Sjp161948  * Uses algorithm A.10.2 of IEEE P1363.
675*2139Sjp161948  */
ec_GF2m_simple_add(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)676*2139Sjp161948 int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
677*2139Sjp161948 	{
678*2139Sjp161948 	BN_CTX *new_ctx = NULL;
679*2139Sjp161948 	BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
680*2139Sjp161948 	int ret = 0;
681*2139Sjp161948 
682*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, a))
683*2139Sjp161948 		{
684*2139Sjp161948 		if (!EC_POINT_copy(r, b)) return 0;
685*2139Sjp161948 		return 1;
686*2139Sjp161948 		}
687*2139Sjp161948 
688*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, b))
689*2139Sjp161948 		{
690*2139Sjp161948 		if (!EC_POINT_copy(r, a)) return 0;
691*2139Sjp161948 		return 1;
692*2139Sjp161948 		}
693*2139Sjp161948 
694*2139Sjp161948 	if (ctx == NULL)
695*2139Sjp161948 		{
696*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
697*2139Sjp161948 		if (ctx == NULL)
698*2139Sjp161948 			return 0;
699*2139Sjp161948 		}
700*2139Sjp161948 
701*2139Sjp161948 	BN_CTX_start(ctx);
702*2139Sjp161948 	x0 = BN_CTX_get(ctx);
703*2139Sjp161948 	y0 = BN_CTX_get(ctx);
704*2139Sjp161948 	x1 = BN_CTX_get(ctx);
705*2139Sjp161948 	y1 = BN_CTX_get(ctx);
706*2139Sjp161948 	x2 = BN_CTX_get(ctx);
707*2139Sjp161948 	y2 = BN_CTX_get(ctx);
708*2139Sjp161948 	s = BN_CTX_get(ctx);
709*2139Sjp161948 	t = BN_CTX_get(ctx);
710*2139Sjp161948 	if (t == NULL) goto err;
711*2139Sjp161948 
712*2139Sjp161948 	if (a->Z_is_one)
713*2139Sjp161948 		{
714*2139Sjp161948 		if (!BN_copy(x0, &a->X)) goto err;
715*2139Sjp161948 		if (!BN_copy(y0, &a->Y)) goto err;
716*2139Sjp161948 		}
717*2139Sjp161948 	else
718*2139Sjp161948 		{
719*2139Sjp161948 		if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx)) goto err;
720*2139Sjp161948 		}
721*2139Sjp161948 	if (b->Z_is_one)
722*2139Sjp161948 		{
723*2139Sjp161948 		if (!BN_copy(x1, &b->X)) goto err;
724*2139Sjp161948 		if (!BN_copy(y1, &b->Y)) goto err;
725*2139Sjp161948 		}
726*2139Sjp161948 	else
727*2139Sjp161948 		{
728*2139Sjp161948 		if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx)) goto err;
729*2139Sjp161948 		}
730*2139Sjp161948 
731*2139Sjp161948 
732*2139Sjp161948 	if (BN_GF2m_cmp(x0, x1))
733*2139Sjp161948 		{
734*2139Sjp161948 		if (!BN_GF2m_add(t, x0, x1)) goto err;
735*2139Sjp161948 		if (!BN_GF2m_add(s, y0, y1)) goto err;
736*2139Sjp161948 		if (!group->meth->field_div(group, s, s, t, ctx)) goto err;
737*2139Sjp161948 		if (!group->meth->field_sqr(group, x2, s, ctx)) goto err;
738*2139Sjp161948 		if (!BN_GF2m_add(x2, x2, &group->a)) goto err;
739*2139Sjp161948 		if (!BN_GF2m_add(x2, x2, s)) goto err;
740*2139Sjp161948 		if (!BN_GF2m_add(x2, x2, t)) goto err;
741*2139Sjp161948 		}
742*2139Sjp161948 	else
743*2139Sjp161948 		{
744*2139Sjp161948 		if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1))
745*2139Sjp161948 			{
746*2139Sjp161948 			if (!EC_POINT_set_to_infinity(group, r)) goto err;
747*2139Sjp161948 			ret = 1;
748*2139Sjp161948 			goto err;
749*2139Sjp161948 			}
750*2139Sjp161948 		if (!group->meth->field_div(group, s, y1, x1, ctx)) goto err;
751*2139Sjp161948 		if (!BN_GF2m_add(s, s, x1)) goto err;
752*2139Sjp161948 
753*2139Sjp161948 		if (!group->meth->field_sqr(group, x2, s, ctx)) goto err;
754*2139Sjp161948 		if (!BN_GF2m_add(x2, x2, s)) goto err;
755*2139Sjp161948 		if (!BN_GF2m_add(x2, x2, &group->a)) goto err;
756*2139Sjp161948 		}
757*2139Sjp161948 
758*2139Sjp161948 	if (!BN_GF2m_add(y2, x1, x2)) goto err;
759*2139Sjp161948 	if (!group->meth->field_mul(group, y2, y2, s, ctx)) goto err;
760*2139Sjp161948 	if (!BN_GF2m_add(y2, y2, x2)) goto err;
761*2139Sjp161948 	if (!BN_GF2m_add(y2, y2, y1)) goto err;
762*2139Sjp161948 
763*2139Sjp161948 	if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx)) goto err;
764*2139Sjp161948 
765*2139Sjp161948 	ret = 1;
766*2139Sjp161948 
767*2139Sjp161948  err:
768*2139Sjp161948 	BN_CTX_end(ctx);
769*2139Sjp161948 	if (new_ctx != NULL)
770*2139Sjp161948 		BN_CTX_free(new_ctx);
771*2139Sjp161948 	return ret;
772*2139Sjp161948 	}
773*2139Sjp161948 
774*2139Sjp161948 
775*2139Sjp161948 /* Computes 2 * a and stores the result in r.  r could be a.
776*2139Sjp161948  * Uses algorithm A.10.2 of IEEE P1363.
777*2139Sjp161948  */
ec_GF2m_simple_dbl(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,BN_CTX * ctx)778*2139Sjp161948 int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
779*2139Sjp161948 	{
780*2139Sjp161948 	return ec_GF2m_simple_add(group, r, a, a, ctx);
781*2139Sjp161948 	}
782*2139Sjp161948 
783*2139Sjp161948 
ec_GF2m_simple_invert(const EC_GROUP * group,EC_POINT * point,BN_CTX * ctx)784*2139Sjp161948 int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
785*2139Sjp161948 	{
786*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
787*2139Sjp161948 		/* point is its own inverse */
788*2139Sjp161948 		return 1;
789*2139Sjp161948 
790*2139Sjp161948 	if (!EC_POINT_make_affine(group, point, ctx)) return 0;
791*2139Sjp161948 	return BN_GF2m_add(&point->Y, &point->X, &point->Y);
792*2139Sjp161948 	}
793*2139Sjp161948 
794*2139Sjp161948 
795*2139Sjp161948 /* Indicates whether the given point is the point at infinity. */
ec_GF2m_simple_is_at_infinity(const EC_GROUP * group,const EC_POINT * point)796*2139Sjp161948 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
797*2139Sjp161948 	{
798*2139Sjp161948 	return BN_is_zero(&point->Z);
799*2139Sjp161948 	}
800*2139Sjp161948 
801*2139Sjp161948 
802*2139Sjp161948 /* Determines whether the given EC_POINT is an actual point on the curve defined
803*2139Sjp161948  * in the EC_GROUP.  A point is valid if it satisfies the Weierstrass equation:
804*2139Sjp161948  *      y^2 + x*y = x^3 + a*x^2 + b.
805*2139Sjp161948  */
ec_GF2m_simple_is_on_curve(const EC_GROUP * group,const EC_POINT * point,BN_CTX * ctx)806*2139Sjp161948 int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
807*2139Sjp161948 	{
808*2139Sjp161948 	int ret = -1;
809*2139Sjp161948 	BN_CTX *new_ctx = NULL;
810*2139Sjp161948 	BIGNUM *lh, *y2;
811*2139Sjp161948 	int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
812*2139Sjp161948 	int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
813*2139Sjp161948 
814*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, point))
815*2139Sjp161948 		return 1;
816*2139Sjp161948 
817*2139Sjp161948 	field_mul = group->meth->field_mul;
818*2139Sjp161948 	field_sqr = group->meth->field_sqr;
819*2139Sjp161948 
820*2139Sjp161948 	/* only support affine coordinates */
821*2139Sjp161948 	if (!point->Z_is_one) goto err;
822*2139Sjp161948 
823*2139Sjp161948 	if (ctx == NULL)
824*2139Sjp161948 		{
825*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
826*2139Sjp161948 		if (ctx == NULL)
827*2139Sjp161948 			return -1;
828*2139Sjp161948 		}
829*2139Sjp161948 
830*2139Sjp161948 	BN_CTX_start(ctx);
831*2139Sjp161948 	y2 = BN_CTX_get(ctx);
832*2139Sjp161948 	lh = BN_CTX_get(ctx);
833*2139Sjp161948 	if (lh == NULL) goto err;
834*2139Sjp161948 
835*2139Sjp161948 	/* We have a curve defined by a Weierstrass equation
836*2139Sjp161948 	 *      y^2 + x*y = x^3 + a*x^2 + b.
837*2139Sjp161948 	 *  <=> x^3 + a*x^2 + x*y + b + y^2 = 0
838*2139Sjp161948 	 *  <=> ((x + a) * x + y ) * x + b + y^2 = 0
839*2139Sjp161948 	 */
840*2139Sjp161948 	if (!BN_GF2m_add(lh, &point->X, &group->a)) goto err;
841*2139Sjp161948 	if (!field_mul(group, lh, lh, &point->X, ctx)) goto err;
842*2139Sjp161948 	if (!BN_GF2m_add(lh, lh, &point->Y)) goto err;
843*2139Sjp161948 	if (!field_mul(group, lh, lh, &point->X, ctx)) goto err;
844*2139Sjp161948 	if (!BN_GF2m_add(lh, lh, &group->b)) goto err;
845*2139Sjp161948 	if (!field_sqr(group, y2, &point->Y, ctx)) goto err;
846*2139Sjp161948 	if (!BN_GF2m_add(lh, lh, y2)) goto err;
847*2139Sjp161948 	ret = BN_is_zero(lh);
848*2139Sjp161948  err:
849*2139Sjp161948 	if (ctx) BN_CTX_end(ctx);
850*2139Sjp161948 	if (new_ctx) BN_CTX_free(new_ctx);
851*2139Sjp161948 	return ret;
852*2139Sjp161948 	}
853*2139Sjp161948 
854*2139Sjp161948 
855*2139Sjp161948 /* Indicates whether two points are equal.
856*2139Sjp161948  * Return values:
857*2139Sjp161948  *  -1   error
858*2139Sjp161948  *   0   equal (in affine coordinates)
859*2139Sjp161948  *   1   not equal
860*2139Sjp161948  */
ec_GF2m_simple_cmp(const EC_GROUP * group,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)861*2139Sjp161948 int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
862*2139Sjp161948 	{
863*2139Sjp161948 	BIGNUM *aX, *aY, *bX, *bY;
864*2139Sjp161948 	BN_CTX *new_ctx = NULL;
865*2139Sjp161948 	int ret = -1;
866*2139Sjp161948 
867*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, a))
868*2139Sjp161948 		{
869*2139Sjp161948 		return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
870*2139Sjp161948 		}
871*2139Sjp161948 
872*2139Sjp161948 	if (a->Z_is_one && b->Z_is_one)
873*2139Sjp161948 		{
874*2139Sjp161948 		return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
875*2139Sjp161948 		}
876*2139Sjp161948 
877*2139Sjp161948 	if (ctx == NULL)
878*2139Sjp161948 		{
879*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
880*2139Sjp161948 		if (ctx == NULL)
881*2139Sjp161948 			return -1;
882*2139Sjp161948 		}
883*2139Sjp161948 
884*2139Sjp161948 	BN_CTX_start(ctx);
885*2139Sjp161948 	aX = BN_CTX_get(ctx);
886*2139Sjp161948 	aY = BN_CTX_get(ctx);
887*2139Sjp161948 	bX = BN_CTX_get(ctx);
888*2139Sjp161948 	bY = BN_CTX_get(ctx);
889*2139Sjp161948 	if (bY == NULL) goto err;
890*2139Sjp161948 
891*2139Sjp161948 	if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx)) goto err;
892*2139Sjp161948 	if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx)) goto err;
893*2139Sjp161948 	ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
894*2139Sjp161948 
895*2139Sjp161948   err:
896*2139Sjp161948 	if (ctx) BN_CTX_end(ctx);
897*2139Sjp161948 	if (new_ctx) BN_CTX_free(new_ctx);
898*2139Sjp161948 	return ret;
899*2139Sjp161948 	}
900*2139Sjp161948 
901*2139Sjp161948 
902*2139Sjp161948 /* Forces the given EC_POINT to internally use affine coordinates. */
ec_GF2m_simple_make_affine(const EC_GROUP * group,EC_POINT * point,BN_CTX * ctx)903*2139Sjp161948 int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
904*2139Sjp161948 	{
905*2139Sjp161948 	BN_CTX *new_ctx = NULL;
906*2139Sjp161948 	BIGNUM *x, *y;
907*2139Sjp161948 	int ret = 0;
908*2139Sjp161948 
909*2139Sjp161948 	if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
910*2139Sjp161948 		return 1;
911*2139Sjp161948 
912*2139Sjp161948 	if (ctx == NULL)
913*2139Sjp161948 		{
914*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
915*2139Sjp161948 		if (ctx == NULL)
916*2139Sjp161948 			return 0;
917*2139Sjp161948 		}
918*2139Sjp161948 
919*2139Sjp161948 	BN_CTX_start(ctx);
920*2139Sjp161948 	x = BN_CTX_get(ctx);
921*2139Sjp161948 	y = BN_CTX_get(ctx);
922*2139Sjp161948 	if (y == NULL) goto err;
923*2139Sjp161948 
924*2139Sjp161948 	if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
925*2139Sjp161948 	if (!BN_copy(&point->X, x)) goto err;
926*2139Sjp161948 	if (!BN_copy(&point->Y, y)) goto err;
927*2139Sjp161948 	if (!BN_one(&point->Z)) goto err;
928*2139Sjp161948 
929*2139Sjp161948 	ret = 1;
930*2139Sjp161948 
931*2139Sjp161948   err:
932*2139Sjp161948 	if (ctx) BN_CTX_end(ctx);
933*2139Sjp161948 	if (new_ctx) BN_CTX_free(new_ctx);
934*2139Sjp161948 	return ret;
935*2139Sjp161948 	}
936*2139Sjp161948 
937*2139Sjp161948 
938*2139Sjp161948 /* Forces each of the EC_POINTs in the given array to use affine coordinates. */
ec_GF2m_simple_points_make_affine(const EC_GROUP * group,size_t num,EC_POINT * points[],BN_CTX * ctx)939*2139Sjp161948 int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
940*2139Sjp161948 	{
941*2139Sjp161948 	size_t i;
942*2139Sjp161948 
943*2139Sjp161948 	for (i = 0; i < num; i++)
944*2139Sjp161948 		{
945*2139Sjp161948 		if (!group->meth->make_affine(group, points[i], ctx)) return 0;
946*2139Sjp161948 		}
947*2139Sjp161948 
948*2139Sjp161948 	return 1;
949*2139Sjp161948 	}
950*2139Sjp161948 
951*2139Sjp161948 
952*2139Sjp161948 /* Wrapper to simple binary polynomial field multiplication implementation. */
ec_GF2m_simple_field_mul(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)953*2139Sjp161948 int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
954*2139Sjp161948 	{
955*2139Sjp161948 	return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
956*2139Sjp161948 	}
957*2139Sjp161948 
958*2139Sjp161948 
959*2139Sjp161948 /* Wrapper to simple binary polynomial field squaring implementation. */
ec_GF2m_simple_field_sqr(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,BN_CTX * ctx)960*2139Sjp161948 int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
961*2139Sjp161948 	{
962*2139Sjp161948 	return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
963*2139Sjp161948 	}
964*2139Sjp161948 
965*2139Sjp161948 
966*2139Sjp161948 /* Wrapper to simple binary polynomial field division implementation. */
ec_GF2m_simple_field_div(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)967*2139Sjp161948 int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
968*2139Sjp161948 	{
969*2139Sjp161948 	return BN_GF2m_mod_div(r, a, b, &group->field, ctx);
970*2139Sjp161948 	}
971