xref: /dragonfly/crypto/libressl/crypto/ec/ec2_smpl.c (revision 92fe556d)
1 /* $OpenBSD: ec2_smpl.c,v 1.21 2018/11/05 20:18:21 tb Exp $ */
2 /* ====================================================================
3  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4  *
5  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7  * to the OpenSSL project.
8  *
9  * The ECC Code is licensed pursuant to the OpenSSL open source
10  * license provided below.
11  *
12  * The software is originally written by Sheueling Chang Shantz and
13  * Douglas Stebila of Sun Microsystems Laboratories.
14  *
15  */
16 /* ====================================================================
17  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  *
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *
31  * 3. All advertising materials mentioning features or use of this
32  *    software must display the following acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35  *
36  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37  *    endorse or promote products derived from this software without
38  *    prior written permission. For written permission, please contact
39  *    openssl-core@openssl.org.
40  *
41  * 5. Products derived from this software may not be called "OpenSSL"
42  *    nor may "OpenSSL" appear in their names without prior written
43  *    permission of the OpenSSL Project.
44  *
45  * 6. Redistributions of any form whatsoever must retain the following
46  *    acknowledgment:
47  *    "This product includes software developed by the OpenSSL Project
48  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61  * OF THE POSSIBILITY OF SUCH DAMAGE.
62  * ====================================================================
63  *
64  * This product includes cryptographic software written by Eric Young
65  * (eay@cryptsoft.com).  This product includes software written by Tim
66  * Hudson (tjh@cryptsoft.com).
67  *
68  */
69 
70 #include <openssl/opensslconf.h>
71 
72 #include <openssl/err.h>
73 
74 #include "ec_lcl.h"
75 
76 #ifndef OPENSSL_NO_EC2M
77 
78 const EC_METHOD *
79 EC_GF2m_simple_method(void)
80 {
81 	static const EC_METHOD ret = {
82 		.flags = EC_FLAGS_DEFAULT_OCT,
83 		.field_type = NID_X9_62_characteristic_two_field,
84 		.group_init = ec_GF2m_simple_group_init,
85 		.group_finish = ec_GF2m_simple_group_finish,
86 		.group_clear_finish = ec_GF2m_simple_group_clear_finish,
87 		.group_copy = ec_GF2m_simple_group_copy,
88 		.group_set_curve = ec_GF2m_simple_group_set_curve,
89 		.group_get_curve = ec_GF2m_simple_group_get_curve,
90 		.group_get_degree = ec_GF2m_simple_group_get_degree,
91 		.group_check_discriminant =
92 		ec_GF2m_simple_group_check_discriminant,
93 		.point_init = ec_GF2m_simple_point_init,
94 		.point_finish = ec_GF2m_simple_point_finish,
95 		.point_clear_finish = ec_GF2m_simple_point_clear_finish,
96 		.point_copy = ec_GF2m_simple_point_copy,
97 		.point_set_to_infinity = ec_GF2m_simple_point_set_to_infinity,
98 		.point_set_affine_coordinates =
99 		ec_GF2m_simple_point_set_affine_coordinates,
100 		.point_get_affine_coordinates =
101 		ec_GF2m_simple_point_get_affine_coordinates,
102 		.add = ec_GF2m_simple_add,
103 		.dbl = ec_GF2m_simple_dbl,
104 		.invert = ec_GF2m_simple_invert,
105 		.is_at_infinity = ec_GF2m_simple_is_at_infinity,
106 		.is_on_curve = ec_GF2m_simple_is_on_curve,
107 		.point_cmp = ec_GF2m_simple_cmp,
108 		.make_affine = ec_GF2m_simple_make_affine,
109 		.points_make_affine = ec_GF2m_simple_points_make_affine,
110 		.mul_generator_ct = ec_GFp_simple_mul_generator_ct,
111 		.mul_single_ct = ec_GFp_simple_mul_single_ct,
112 		.mul_double_nonct = ec_GFp_simple_mul_double_nonct,
113 		.precompute_mult = ec_GF2m_precompute_mult,
114 		.have_precompute_mult = ec_GF2m_have_precompute_mult,
115 		.field_mul = ec_GF2m_simple_field_mul,
116 		.field_sqr = ec_GF2m_simple_field_sqr,
117 		.field_div = ec_GF2m_simple_field_div,
118 		.blind_coordinates = NULL,
119 	};
120 
121 	return &ret;
122 }
123 
124 
125 /* Initialize a GF(2^m)-based EC_GROUP structure.
126  * Note that all other members are handled by EC_GROUP_new.
127  */
128 int
129 ec_GF2m_simple_group_init(EC_GROUP * group)
130 {
131 	BN_init(&group->field);
132 	BN_init(&group->a);
133 	BN_init(&group->b);
134 	return 1;
135 }
136 
137 
138 /* Free a GF(2^m)-based EC_GROUP structure.
139  * Note that all other members are handled by EC_GROUP_free.
140  */
141 void
142 ec_GF2m_simple_group_finish(EC_GROUP * group)
143 {
144 	BN_free(&group->field);
145 	BN_free(&group->a);
146 	BN_free(&group->b);
147 }
148 
149 
150 /* Clear and free a GF(2^m)-based EC_GROUP structure.
151  * Note that all other members are handled by EC_GROUP_clear_free.
152  */
153 void
154 ec_GF2m_simple_group_clear_finish(EC_GROUP * group)
155 {
156 	BN_clear_free(&group->field);
157 	BN_clear_free(&group->a);
158 	BN_clear_free(&group->b);
159 	group->poly[0] = 0;
160 	group->poly[1] = 0;
161 	group->poly[2] = 0;
162 	group->poly[3] = 0;
163 	group->poly[4] = 0;
164 	group->poly[5] = -1;
165 }
166 
167 
168 /* Copy a GF(2^m)-based EC_GROUP structure.
169  * Note that all other members are handled by EC_GROUP_copy.
170  */
171 int
172 ec_GF2m_simple_group_copy(EC_GROUP * dest, const EC_GROUP * src)
173 {
174 	int i;
175 
176 	if (!BN_copy(&dest->field, &src->field))
177 		return 0;
178 	if (!BN_copy(&dest->a, &src->a))
179 		return 0;
180 	if (!BN_copy(&dest->b, &src->b))
181 		return 0;
182 	dest->poly[0] = src->poly[0];
183 	dest->poly[1] = src->poly[1];
184 	dest->poly[2] = src->poly[2];
185 	dest->poly[3] = src->poly[3];
186 	dest->poly[4] = src->poly[4];
187 	dest->poly[5] = src->poly[5];
188 	if (bn_wexpand(&dest->a, (int) (dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
189 		return 0;
190 	if (bn_wexpand(&dest->b, (int) (dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
191 		return 0;
192 	for (i = dest->a.top; i < dest->a.dmax; i++)
193 		dest->a.d[i] = 0;
194 	for (i = dest->b.top; i < dest->b.dmax; i++)
195 		dest->b.d[i] = 0;
196 	return 1;
197 }
198 
199 
200 /* Set the curve parameters of an EC_GROUP structure. */
201 int
202 ec_GF2m_simple_group_set_curve(EC_GROUP * group,
203     const BIGNUM * p, const BIGNUM * a, const BIGNUM * b, BN_CTX * ctx)
204 {
205 	int ret = 0, i;
206 
207 	/* group->field */
208 	if (!BN_copy(&group->field, p))
209 		goto err;
210 	i = BN_GF2m_poly2arr(&group->field, group->poly, 6) - 1;
211 	if ((i != 5) && (i != 3)) {
212 		ECerror(EC_R_UNSUPPORTED_FIELD);
213 		goto err;
214 	}
215 	/* group->a */
216 	if (!BN_GF2m_mod_arr(&group->a, a, group->poly))
217 		goto err;
218 	if (bn_wexpand(&group->a, (int) (group->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
219 		goto err;
220 	for (i = group->a.top; i < group->a.dmax; i++)
221 		group->a.d[i] = 0;
222 
223 	/* group->b */
224 	if (!BN_GF2m_mod_arr(&group->b, b, group->poly))
225 		goto err;
226 	if (bn_wexpand(&group->b, (int) (group->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
227 		goto err;
228 	for (i = group->b.top; i < group->b.dmax; i++)
229 		group->b.d[i] = 0;
230 
231 	ret = 1;
232  err:
233 	return ret;
234 }
235 
236 
237 /* Get the curve parameters of an EC_GROUP structure.
238  * If p, a, or b are NULL then there values will not be set but the method will return with success.
239  */
240 int
241 ec_GF2m_simple_group_get_curve(const EC_GROUP *group,
242     BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
243 {
244 	int ret = 0;
245 
246 	if (p != NULL) {
247 		if (!BN_copy(p, &group->field))
248 			return 0;
249 	}
250 	if (a != NULL) {
251 		if (!BN_copy(a, &group->a))
252 			goto err;
253 	}
254 	if (b != NULL) {
255 		if (!BN_copy(b, &group->b))
256 			goto err;
257 	}
258 	ret = 1;
259 
260  err:
261 	return ret;
262 }
263 
264 
265 /* Gets the degree of the field.  For a curve over GF(2^m) this is the value m. */
266 int
267 ec_GF2m_simple_group_get_degree(const EC_GROUP * group)
268 {
269 	return BN_num_bits(&group->field) - 1;
270 }
271 
272 
273 /* Checks the discriminant of the curve.
274  * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
275  */
276 int
277 ec_GF2m_simple_group_check_discriminant(const EC_GROUP * group, BN_CTX * ctx)
278 {
279 	int ret = 0;
280 	BIGNUM *b;
281 	BN_CTX *new_ctx = NULL;
282 
283 	if (ctx == NULL) {
284 		ctx = new_ctx = BN_CTX_new();
285 		if (ctx == NULL) {
286 			ECerror(ERR_R_MALLOC_FAILURE);
287 			goto err;
288 		}
289 	}
290 	BN_CTX_start(ctx);
291 	if ((b = BN_CTX_get(ctx)) == NULL)
292 		goto err;
293 
294 	if (!BN_GF2m_mod_arr(b, &group->b, group->poly))
295 		goto err;
296 
297 	/*
298 	 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
299 	 * curve <=> b != 0 (mod p)
300 	 */
301 	if (BN_is_zero(b))
302 		goto err;
303 
304 	ret = 1;
305 
306  err:
307 	if (ctx != NULL)
308 		BN_CTX_end(ctx);
309 	BN_CTX_free(new_ctx);
310 	return ret;
311 }
312 
313 
314 /* Initializes an EC_POINT. */
315 int
316 ec_GF2m_simple_point_init(EC_POINT * point)
317 {
318 	BN_init(&point->X);
319 	BN_init(&point->Y);
320 	BN_init(&point->Z);
321 	return 1;
322 }
323 
324 
325 /* Frees an EC_POINT. */
326 void
327 ec_GF2m_simple_point_finish(EC_POINT * point)
328 {
329 	BN_free(&point->X);
330 	BN_free(&point->Y);
331 	BN_free(&point->Z);
332 }
333 
334 
335 /* Clears and frees an EC_POINT. */
336 void
337 ec_GF2m_simple_point_clear_finish(EC_POINT * point)
338 {
339 	BN_clear_free(&point->X);
340 	BN_clear_free(&point->Y);
341 	BN_clear_free(&point->Z);
342 	point->Z_is_one = 0;
343 }
344 
345 
346 /* Copy the contents of one EC_POINT into another.  Assumes dest is initialized. */
347 int
348 ec_GF2m_simple_point_copy(EC_POINT * dest, const EC_POINT * src)
349 {
350 	if (!BN_copy(&dest->X, &src->X))
351 		return 0;
352 	if (!BN_copy(&dest->Y, &src->Y))
353 		return 0;
354 	if (!BN_copy(&dest->Z, &src->Z))
355 		return 0;
356 	dest->Z_is_one = src->Z_is_one;
357 
358 	return 1;
359 }
360 
361 
362 /* Set an EC_POINT to the point at infinity.
363  * A point at infinity is represented by having Z=0.
364  */
365 int
366 ec_GF2m_simple_point_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
367 {
368 	point->Z_is_one = 0;
369 	BN_zero(&point->Z);
370 	return 1;
371 }
372 
373 
374 /* Set the coordinates of an EC_POINT using affine coordinates.
375  * Note that the simple implementation only uses affine coordinates.
376  */
377 int
378 ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP * group, EC_POINT * point,
379     const BIGNUM * x, const BIGNUM * y, BN_CTX * ctx)
380 {
381 	int ret = 0;
382 	if (x == NULL || y == NULL) {
383 		ECerror(ERR_R_PASSED_NULL_PARAMETER);
384 		return 0;
385 	}
386 	if (!BN_copy(&point->X, x))
387 		goto err;
388 	BN_set_negative(&point->X, 0);
389 	if (!BN_copy(&point->Y, y))
390 		goto err;
391 	BN_set_negative(&point->Y, 0);
392 	if (!BN_copy(&point->Z, BN_value_one()))
393 		goto err;
394 	BN_set_negative(&point->Z, 0);
395 	point->Z_is_one = 1;
396 	ret = 1;
397 
398  err:
399 	return ret;
400 }
401 
402 
403 /* Gets the affine coordinates of an EC_POINT.
404  * Note that the simple implementation only uses affine coordinates.
405  */
406 int
407 ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
408     const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
409 {
410 	int ret = 0;
411 
412 	if (EC_POINT_is_at_infinity(group, point) > 0) {
413 		ECerror(EC_R_POINT_AT_INFINITY);
414 		return 0;
415 	}
416 	if (BN_cmp(&point->Z, BN_value_one())) {
417 		ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
418 		return 0;
419 	}
420 	if (x != NULL) {
421 		if (!BN_copy(x, &point->X))
422 			goto err;
423 		BN_set_negative(x, 0);
424 	}
425 	if (y != NULL) {
426 		if (!BN_copy(y, &point->Y))
427 			goto err;
428 		BN_set_negative(y, 0);
429 	}
430 	ret = 1;
431 
432  err:
433 	return ret;
434 }
435 
436 /* Computes a + b and stores the result in r.  r could be a or b, a could be b.
437  * Uses algorithm A.10.2 of IEEE P1363.
438  */
439 int
440 ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
441     const EC_POINT *b, BN_CTX *ctx)
442 {
443 	BN_CTX *new_ctx = NULL;
444 	BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
445 	int ret = 0;
446 
447 	if (EC_POINT_is_at_infinity(group, a) > 0) {
448 		if (!EC_POINT_copy(r, b))
449 			return 0;
450 		return 1;
451 	}
452 	if (EC_POINT_is_at_infinity(group, b) > 0) {
453 		if (!EC_POINT_copy(r, a))
454 			return 0;
455 		return 1;
456 	}
457 	if (ctx == NULL) {
458 		ctx = new_ctx = BN_CTX_new();
459 		if (ctx == NULL)
460 			return 0;
461 	}
462 	BN_CTX_start(ctx);
463 	if ((x0 = BN_CTX_get(ctx)) == NULL)
464 		goto err;
465 	if ((y0 = BN_CTX_get(ctx)) == NULL)
466 		goto err;
467 	if ((x1 = BN_CTX_get(ctx)) == NULL)
468 		goto err;
469 	if ((y1 = BN_CTX_get(ctx)) == NULL)
470 		goto err;
471 	if ((x2 = BN_CTX_get(ctx)) == NULL)
472 		goto err;
473 	if ((y2 = BN_CTX_get(ctx)) == NULL)
474 		goto err;
475 	if ((s = BN_CTX_get(ctx)) == NULL)
476 		goto err;
477 	if ((t = BN_CTX_get(ctx)) == NULL)
478 		goto err;
479 
480 	if (a->Z_is_one) {
481 		if (!BN_copy(x0, &a->X))
482 			goto err;
483 		if (!BN_copy(y0, &a->Y))
484 			goto err;
485 	} else {
486 		if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
487 			goto err;
488 	}
489 	if (b->Z_is_one) {
490 		if (!BN_copy(x1, &b->X))
491 			goto err;
492 		if (!BN_copy(y1, &b->Y))
493 			goto err;
494 	} else {
495 		if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
496 			goto err;
497 	}
498 
499 
500 	if (BN_GF2m_cmp(x0, x1)) {
501 		if (!BN_GF2m_add(t, x0, x1))
502 			goto err;
503 		if (!BN_GF2m_add(s, y0, y1))
504 			goto err;
505 		if (!group->meth->field_div(group, s, s, t, ctx))
506 			goto err;
507 		if (!group->meth->field_sqr(group, x2, s, ctx))
508 			goto err;
509 		if (!BN_GF2m_add(x2, x2, &group->a))
510 			goto err;
511 		if (!BN_GF2m_add(x2, x2, s))
512 			goto err;
513 		if (!BN_GF2m_add(x2, x2, t))
514 			goto err;
515 	} else {
516 		if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
517 			if (!EC_POINT_set_to_infinity(group, r))
518 				goto err;
519 			ret = 1;
520 			goto err;
521 		}
522 		if (!group->meth->field_div(group, s, y1, x1, ctx))
523 			goto err;
524 		if (!BN_GF2m_add(s, s, x1))
525 			goto err;
526 
527 		if (!group->meth->field_sqr(group, x2, s, ctx))
528 			goto err;
529 		if (!BN_GF2m_add(x2, x2, s))
530 			goto err;
531 		if (!BN_GF2m_add(x2, x2, &group->a))
532 			goto err;
533 	}
534 
535 	if (!BN_GF2m_add(y2, x1, x2))
536 		goto err;
537 	if (!group->meth->field_mul(group, y2, y2, s, ctx))
538 		goto err;
539 	if (!BN_GF2m_add(y2, y2, x2))
540 		goto err;
541 	if (!BN_GF2m_add(y2, y2, y1))
542 		goto err;
543 
544 	if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
545 		goto err;
546 
547 	ret = 1;
548 
549  err:
550 	BN_CTX_end(ctx);
551 	BN_CTX_free(new_ctx);
552 	return ret;
553 }
554 
555 
556 /* Computes 2 * a and stores the result in r.  r could be a.
557  * Uses algorithm A.10.2 of IEEE P1363.
558  */
559 int
560 ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
561     BN_CTX *ctx)
562 {
563 	return ec_GF2m_simple_add(group, r, a, a, ctx);
564 }
565 
566 int
567 ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
568 {
569 	if (EC_POINT_is_at_infinity(group, point) > 0 || BN_is_zero(&point->Y))
570 		/* point is its own inverse */
571 		return 1;
572 
573 	if (!EC_POINT_make_affine(group, point, ctx))
574 		return 0;
575 	return BN_GF2m_add(&point->Y, &point->X, &point->Y);
576 }
577 
578 
579 /* Indicates whether the given point is the point at infinity. */
580 int
581 ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
582 {
583 	return BN_is_zero(&point->Z);
584 }
585 
586 
587 /* Determines whether the given EC_POINT is an actual point on the curve defined
588  * in the EC_GROUP.  A point is valid if it satisfies the Weierstrass equation:
589  *      y^2 + x*y = x^3 + a*x^2 + b.
590  */
591 int
592 ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
593 {
594 	int ret = -1;
595 	BN_CTX *new_ctx = NULL;
596 	BIGNUM *lh, *y2;
597 	int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
598 	int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
599 
600 	if (EC_POINT_is_at_infinity(group, point) > 0)
601 		return 1;
602 
603 	field_mul = group->meth->field_mul;
604 	field_sqr = group->meth->field_sqr;
605 
606 	/* only support affine coordinates */
607 	if (!point->Z_is_one)
608 		return -1;
609 
610 	if (ctx == NULL) {
611 		ctx = new_ctx = BN_CTX_new();
612 		if (ctx == NULL)
613 			return -1;
614 	}
615 	BN_CTX_start(ctx);
616 	if ((y2 = BN_CTX_get(ctx)) == NULL)
617 		goto err;
618 	if ((lh = BN_CTX_get(ctx)) == NULL)
619 		goto err;
620 
621 	/*
622 	 * We have a curve defined by a Weierstrass equation y^2 + x*y = x^3
623 	 * + a*x^2 + b. <=> x^3 + a*x^2 + x*y + b + y^2 = 0 <=> ((x + a) * x
624 	 * + y ) * x + b + y^2 = 0
625 	 */
626 	if (!BN_GF2m_add(lh, &point->X, &group->a))
627 		goto err;
628 	if (!field_mul(group, lh, lh, &point->X, ctx))
629 		goto err;
630 	if (!BN_GF2m_add(lh, lh, &point->Y))
631 		goto err;
632 	if (!field_mul(group, lh, lh, &point->X, ctx))
633 		goto err;
634 	if (!BN_GF2m_add(lh, lh, &group->b))
635 		goto err;
636 	if (!field_sqr(group, y2, &point->Y, ctx))
637 		goto err;
638 	if (!BN_GF2m_add(lh, lh, y2))
639 		goto err;
640 	ret = BN_is_zero(lh);
641  err:
642 	if (ctx)
643 		BN_CTX_end(ctx);
644 	BN_CTX_free(new_ctx);
645 	return ret;
646 }
647 
648 
649 /* Indicates whether two points are equal.
650  * Return values:
651  *  -1   error
652  *   0   equal (in affine coordinates)
653  *   1   not equal
654  */
655 int
656 ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
657     const EC_POINT *b, BN_CTX *ctx)
658 {
659 	BIGNUM *aX, *aY, *bX, *bY;
660 	BN_CTX *new_ctx = NULL;
661 	int ret = -1;
662 
663 	if (EC_POINT_is_at_infinity(group, a) > 0) {
664 		return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1;
665 	}
666 	if (EC_POINT_is_at_infinity(group, b) > 0)
667 		return 1;
668 
669 	if (a->Z_is_one && b->Z_is_one) {
670 		return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
671 	}
672 	if (ctx == NULL) {
673 		ctx = new_ctx = BN_CTX_new();
674 		if (ctx == NULL)
675 			return -1;
676 	}
677 	BN_CTX_start(ctx);
678 	if ((aX = BN_CTX_get(ctx)) == NULL)
679 		goto err;
680 	if ((aY = BN_CTX_get(ctx)) == NULL)
681 		goto err;
682 	if ((bX = BN_CTX_get(ctx)) == NULL)
683 		goto err;
684 	if ((bY = BN_CTX_get(ctx)) == NULL)
685 		goto err;
686 
687 	if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
688 		goto err;
689 	if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
690 		goto err;
691 	ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
692 
693  err:
694 	if (ctx)
695 		BN_CTX_end(ctx);
696 	BN_CTX_free(new_ctx);
697 	return ret;
698 }
699 
700 
701 /* Forces the given EC_POINT to internally use affine coordinates. */
702 int
703 ec_GF2m_simple_make_affine(const EC_GROUP * group, EC_POINT * point, BN_CTX * ctx)
704 {
705 	BN_CTX *new_ctx = NULL;
706 	BIGNUM *x, *y;
707 	int ret = 0;
708 
709 	if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0)
710 		return 1;
711 
712 	if (ctx == NULL) {
713 		ctx = new_ctx = BN_CTX_new();
714 		if (ctx == NULL)
715 			return 0;
716 	}
717 	BN_CTX_start(ctx);
718 	if ((x = BN_CTX_get(ctx)) == NULL)
719 		goto err;
720 	if ((y = BN_CTX_get(ctx)) == NULL)
721 		goto err;
722 
723 	if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
724 		goto err;
725 	if (!BN_copy(&point->X, x))
726 		goto err;
727 	if (!BN_copy(&point->Y, y))
728 		goto err;
729 	if (!BN_one(&point->Z))
730 		goto err;
731 
732 	ret = 1;
733 
734  err:
735 	if (ctx)
736 		BN_CTX_end(ctx);
737 	BN_CTX_free(new_ctx);
738 	return ret;
739 }
740 
741 
742 /* Forces each of the EC_POINTs in the given array to use affine coordinates. */
743 int
744 ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
745     EC_POINT *points[], BN_CTX *ctx)
746 {
747 	size_t i;
748 
749 	for (i = 0; i < num; i++) {
750 		if (!group->meth->make_affine(group, points[i], ctx))
751 			return 0;
752 	}
753 
754 	return 1;
755 }
756 
757 
758 /* Wrapper to simple binary polynomial field multiplication implementation. */
759 int
760 ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
761     const BIGNUM *b, BN_CTX *ctx)
762 {
763 	return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
764 }
765 
766 
767 /* Wrapper to simple binary polynomial field squaring implementation. */
768 int
769 ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
770     BN_CTX *ctx)
771 {
772 	return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
773 }
774 
775 
776 /* Wrapper to simple binary polynomial field division implementation. */
777 int
778 ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
779     const BIGNUM *b, BN_CTX *ctx)
780 {
781 	return BN_GF2m_mod_div(r, a, b, &group->field, ctx);
782 }
783 
784 #endif
785