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.
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.
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
12 * The software is originally written by Sheueling Chang Shantz and
13 * Douglas Stebila of Sun Microsystems Laboratories.
16 /* ====================================================================
17 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
45 * 6. Redistributions of any form whatsoever must retain the following
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
70 #include <openssl/opensslconf.h>
72 #include <openssl/err.h>
76 #ifndef OPENSSL_NO_EC2M
79 EC_GF2m_simple_method(void)
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,
125 /* Initialize a GF(2^m)-based EC_GROUP structure.
126 * Note that all other members are handled by EC_GROUP_new.
129 ec_GF2m_simple_group_init(EC_GROUP * group)
131 BN_init(&group->field);
138 /* Free a GF(2^m)-based EC_GROUP structure.
139 * Note that all other members are handled by EC_GROUP_free.
142 ec_GF2m_simple_group_finish(EC_GROUP * group)
144 BN_free(&group->field);
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.
154 ec_GF2m_simple_group_clear_finish(EC_GROUP * group)
156 BN_clear_free(&group->field);
157 BN_clear_free(&group->a);
158 BN_clear_free(&group->b);
168 /* Copy a GF(2^m)-based EC_GROUP structure.
169 * Note that all other members are handled by EC_GROUP_copy.
172 ec_GF2m_simple_group_copy(EC_GROUP * dest, const EC_GROUP * src)
176 if (!BN_copy(&dest->field, &src->field))
178 if (!BN_copy(&dest->a, &src->a))
180 if (!BN_copy(&dest->b, &src->b))
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)
190 if (bn_wexpand(&dest->b, (int) (dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
192 for (i = dest->a.top; i < dest->a.dmax; i++)
194 for (i = dest->b.top; i < dest->b.dmax; i++)
200 /* Set the curve parameters of an EC_GROUP structure. */
202 ec_GF2m_simple_group_set_curve(EC_GROUP * group,
203 const BIGNUM * p, const BIGNUM * a, const BIGNUM * b, BN_CTX * ctx)
208 if (!BN_copy(&group->field, p))
210 i = BN_GF2m_poly2arr(&group->field, group->poly, 6) - 1;
211 if ((i != 5) && (i != 3)) {
212 ECerror(EC_R_UNSUPPORTED_FIELD);
216 if (!BN_GF2m_mod_arr(&group->a, a, group->poly))
218 if (bn_wexpand(&group->a, (int) (group->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
220 for (i = group->a.top; i < group->a.dmax; i++)
224 if (!BN_GF2m_mod_arr(&group->b, b, group->poly))
226 if (bn_wexpand(&group->b, (int) (group->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
228 for (i = group->b.top; i < group->b.dmax; i++)
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.
241 ec_GF2m_simple_group_get_curve(const EC_GROUP *group,
242 BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
247 if (!BN_copy(p, &group->field))
251 if (!BN_copy(a, &group->a))
255 if (!BN_copy(b, &group->b))
265 /* Gets the degree of the field. For a curve over GF(2^m) this is the value m. */
267 ec_GF2m_simple_group_get_degree(const EC_GROUP * group)
269 return BN_num_bits(&group->field) - 1;
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)
277 ec_GF2m_simple_group_check_discriminant(const EC_GROUP * group, BN_CTX * ctx)
281 BN_CTX *new_ctx = NULL;
284 ctx = new_ctx = BN_CTX_new();
286 ECerror(ERR_R_MALLOC_FAILURE);
291 if ((b = BN_CTX_get(ctx)) == NULL)
294 if (!BN_GF2m_mod_arr(b, &group->b, group->poly))
298 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
299 * curve <=> b != 0 (mod p)
309 BN_CTX_free(new_ctx);
314 /* Initializes an EC_POINT. */
316 ec_GF2m_simple_point_init(EC_POINT * point)
325 /* Frees an EC_POINT. */
327 ec_GF2m_simple_point_finish(EC_POINT * point)
335 /* Clears and frees an EC_POINT. */
337 ec_GF2m_simple_point_clear_finish(EC_POINT * point)
339 BN_clear_free(&point->X);
340 BN_clear_free(&point->Y);
341 BN_clear_free(&point->Z);
346 /* Copy the contents of one EC_POINT into another. Assumes dest is initialized. */
348 ec_GF2m_simple_point_copy(EC_POINT * dest, const EC_POINT * src)
350 if (!BN_copy(&dest->X, &src->X))
352 if (!BN_copy(&dest->Y, &src->Y))
354 if (!BN_copy(&dest->Z, &src->Z))
356 dest->Z_is_one = src->Z_is_one;
362 /* Set an EC_POINT to the point at infinity.
363 * A point at infinity is represented by having Z=0.
366 ec_GF2m_simple_point_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
374 /* Set the coordinates of an EC_POINT using affine coordinates.
375 * Note that the simple implementation only uses affine coordinates.
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)
382 if (x == NULL || y == NULL) {
383 ECerror(ERR_R_PASSED_NULL_PARAMETER);
386 if (!BN_copy(&point->X, x))
388 BN_set_negative(&point->X, 0);
389 if (!BN_copy(&point->Y, y))
391 BN_set_negative(&point->Y, 0);
392 if (!BN_copy(&point->Z, BN_value_one()))
394 BN_set_negative(&point->Z, 0);
403 /* Gets the affine coordinates of an EC_POINT.
404 * Note that the simple implementation only uses affine coordinates.
407 ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
408 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
412 if (EC_POINT_is_at_infinity(group, point) > 0) {
413 ECerror(EC_R_POINT_AT_INFINITY);
416 if (BN_cmp(&point->Z, BN_value_one())) {
417 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
421 if (!BN_copy(x, &point->X))
423 BN_set_negative(x, 0);
426 if (!BN_copy(y, &point->Y))
428 BN_set_negative(y, 0);
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.
440 ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
441 const EC_POINT *b, BN_CTX *ctx)
443 BN_CTX *new_ctx = NULL;
444 BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
447 if (EC_POINT_is_at_infinity(group, a) > 0) {
448 if (!EC_POINT_copy(r, b))
452 if (EC_POINT_is_at_infinity(group, b) > 0) {
453 if (!EC_POINT_copy(r, a))
458 ctx = new_ctx = BN_CTX_new();
463 if ((x0 = BN_CTX_get(ctx)) == NULL)
465 if ((y0 = BN_CTX_get(ctx)) == NULL)
467 if ((x1 = BN_CTX_get(ctx)) == NULL)
469 if ((y1 = BN_CTX_get(ctx)) == NULL)
471 if ((x2 = BN_CTX_get(ctx)) == NULL)
473 if ((y2 = BN_CTX_get(ctx)) == NULL)
475 if ((s = BN_CTX_get(ctx)) == NULL)
477 if ((t = BN_CTX_get(ctx)) == NULL)
481 if (!BN_copy(x0, &a->X))
483 if (!BN_copy(y0, &a->Y))
486 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
490 if (!BN_copy(x1, &b->X))
492 if (!BN_copy(y1, &b->Y))
495 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
500 if (BN_GF2m_cmp(x0, x1)) {
501 if (!BN_GF2m_add(t, x0, x1))
503 if (!BN_GF2m_add(s, y0, y1))
505 if (!group->meth->field_div(group, s, s, t, ctx))
507 if (!group->meth->field_sqr(group, x2, s, ctx))
509 if (!BN_GF2m_add(x2, x2, &group->a))
511 if (!BN_GF2m_add(x2, x2, s))
513 if (!BN_GF2m_add(x2, x2, t))
516 if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
517 if (!EC_POINT_set_to_infinity(group, r))
522 if (!group->meth->field_div(group, s, y1, x1, ctx))
524 if (!BN_GF2m_add(s, s, x1))
527 if (!group->meth->field_sqr(group, x2, s, ctx))
529 if (!BN_GF2m_add(x2, x2, s))
531 if (!BN_GF2m_add(x2, x2, &group->a))
535 if (!BN_GF2m_add(y2, x1, x2))
537 if (!group->meth->field_mul(group, y2, y2, s, ctx))
539 if (!BN_GF2m_add(y2, y2, x2))
541 if (!BN_GF2m_add(y2, y2, y1))
544 if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
551 BN_CTX_free(new_ctx);
556 /* Computes 2 * a and stores the result in r. r could be a.
557 * Uses algorithm A.10.2 of IEEE P1363.
560 ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
563 return ec_GF2m_simple_add(group, r, a, a, ctx);
567 ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
569 if (EC_POINT_is_at_infinity(group, point) > 0 || BN_is_zero(&point->Y))
570 /* point is its own inverse */
573 if (!EC_POINT_make_affine(group, point, ctx))
575 return BN_GF2m_add(&point->Y, &point->X, &point->Y);
579 /* Indicates whether the given point is the point at infinity. */
581 ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
583 return BN_is_zero(&point->Z);
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.
592 ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
595 BN_CTX *new_ctx = NULL;
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 *);
600 if (EC_POINT_is_at_infinity(group, point) > 0)
603 field_mul = group->meth->field_mul;
604 field_sqr = group->meth->field_sqr;
606 /* only support affine coordinates */
607 if (!point->Z_is_one)
611 ctx = new_ctx = BN_CTX_new();
616 if ((y2 = BN_CTX_get(ctx)) == NULL)
618 if ((lh = BN_CTX_get(ctx)) == NULL)
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
626 if (!BN_GF2m_add(lh, &point->X, &group->a))
628 if (!field_mul(group, lh, lh, &point->X, ctx))
630 if (!BN_GF2m_add(lh, lh, &point->Y))
632 if (!field_mul(group, lh, lh, &point->X, ctx))
634 if (!BN_GF2m_add(lh, lh, &group->b))
636 if (!field_sqr(group, y2, &point->Y, ctx))
638 if (!BN_GF2m_add(lh, lh, y2))
640 ret = BN_is_zero(lh);
644 BN_CTX_free(new_ctx);
649 /* Indicates whether two points are equal.
652 * 0 equal (in affine coordinates)
656 ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
657 const EC_POINT *b, BN_CTX *ctx)
659 BIGNUM *aX, *aY, *bX, *bY;
660 BN_CTX *new_ctx = NULL;
663 if (EC_POINT_is_at_infinity(group, a) > 0) {
664 return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1;
666 if (EC_POINT_is_at_infinity(group, b) > 0)
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;
673 ctx = new_ctx = BN_CTX_new();
678 if ((aX = BN_CTX_get(ctx)) == NULL)
680 if ((aY = BN_CTX_get(ctx)) == NULL)
682 if ((bX = BN_CTX_get(ctx)) == NULL)
684 if ((bY = BN_CTX_get(ctx)) == NULL)
687 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
689 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
691 ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
696 BN_CTX_free(new_ctx);
701 /* Forces the given EC_POINT to internally use affine coordinates. */
703 ec_GF2m_simple_make_affine(const EC_GROUP * group, EC_POINT * point, BN_CTX * ctx)
705 BN_CTX *new_ctx = NULL;
709 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0)
713 ctx = new_ctx = BN_CTX_new();
718 if ((x = BN_CTX_get(ctx)) == NULL)
720 if ((y = BN_CTX_get(ctx)) == NULL)
723 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
725 if (!BN_copy(&point->X, x))
727 if (!BN_copy(&point->Y, y))
729 if (!BN_one(&point->Z))
737 BN_CTX_free(new_ctx);
742 /* Forces each of the EC_POINTs in the given array to use affine coordinates. */
744 ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
745 EC_POINT *points[], BN_CTX *ctx)
749 for (i = 0; i < num; i++) {
750 if (!group->meth->make_affine(group, points[i], ctx))
758 /* Wrapper to simple binary polynomial field multiplication implementation. */
760 ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
761 const BIGNUM *b, BN_CTX *ctx)
763 return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
767 /* Wrapper to simple binary polynomial field squaring implementation. */
769 ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
772 return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
776 /* Wrapper to simple binary polynomial field division implementation. */
778 ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
779 const BIGNUM *b, BN_CTX *ctx)
781 return BN_GF2m_mod_div(r, a, b, &group->field, ctx);