2 * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
14 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
15 * to the OpenSSL project.
17 * The ECC Code is licensed pursuant to the OpenSSL open source
18 * license provided below.
20 * The software is originally written by Sheueling Chang Shantz and
21 * Douglas Stebila of Sun Microsystems Laboratories.
25 #include <openssl/err.h>
27 #include "internal/bn_int.h"
30 #ifndef OPENSSL_NO_EC2M
33 * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
34 * are handled by EC_GROUP_new.
36 int ec_GF2m_simple_group_init(EC_GROUP *group)
38 group->field = BN_new();
42 if (group->field == NULL || group->a == NULL || group->b == NULL) {
43 BN_free(group->field);
52 * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
53 * handled by EC_GROUP_free.
55 void ec_GF2m_simple_group_finish(EC_GROUP *group)
57 BN_free(group->field);
63 * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
64 * members are handled by EC_GROUP_clear_free.
66 void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
68 BN_clear_free(group->field);
69 BN_clear_free(group->a);
70 BN_clear_free(group->b);
80 * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
81 * handled by EC_GROUP_copy.
83 int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
85 if (!BN_copy(dest->field, src->field))
87 if (!BN_copy(dest->a, src->a))
89 if (!BN_copy(dest->b, src->b))
91 dest->poly[0] = src->poly[0];
92 dest->poly[1] = src->poly[1];
93 dest->poly[2] = src->poly[2];
94 dest->poly[3] = src->poly[3];
95 dest->poly[4] = src->poly[4];
96 dest->poly[5] = src->poly[5];
97 if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
100 if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
103 bn_set_all_zero(dest->a);
104 bn_set_all_zero(dest->b);
108 /* Set the curve parameters of an EC_GROUP structure. */
109 int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
110 const BIGNUM *p, const BIGNUM *a,
111 const BIGNUM *b, BN_CTX *ctx)
116 if (!BN_copy(group->field, p))
118 i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
119 if ((i != 5) && (i != 3)) {
120 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
125 if (!BN_GF2m_mod_arr(group->a, a, group->poly))
127 if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
130 bn_set_all_zero(group->a);
133 if (!BN_GF2m_mod_arr(group->b, b, group->poly))
135 if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
138 bn_set_all_zero(group->b);
146 * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
147 * then there values will not be set but the method will return with success.
149 int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
150 BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
155 if (!BN_copy(p, group->field))
160 if (!BN_copy(a, group->a))
165 if (!BN_copy(b, group->b))
176 * Gets the degree of the field. For a curve over GF(2^m) this is the value
179 int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
181 return BN_num_bits(group->field) - 1;
185 * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
186 * elliptic curve <=> b != 0 (mod p)
188 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
193 BN_CTX *new_ctx = NULL;
196 ctx = new_ctx = BN_CTX_new();
198 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
199 ERR_R_MALLOC_FAILURE);
208 if (!BN_GF2m_mod_arr(b, group->b, group->poly))
212 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
213 * curve <=> b != 0 (mod p)
223 BN_CTX_free(new_ctx);
227 /* Initializes an EC_POINT. */
228 int ec_GF2m_simple_point_init(EC_POINT *point)
234 if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
243 /* Frees an EC_POINT. */
244 void ec_GF2m_simple_point_finish(EC_POINT *point)
251 /* Clears and frees an EC_POINT. */
252 void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
254 BN_clear_free(point->X);
255 BN_clear_free(point->Y);
256 BN_clear_free(point->Z);
261 * Copy the contents of one EC_POINT into another. Assumes dest is
264 int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
266 if (!BN_copy(dest->X, src->X))
268 if (!BN_copy(dest->Y, src->Y))
270 if (!BN_copy(dest->Z, src->Z))
272 dest->Z_is_one = src->Z_is_one;
273 dest->curve_name = src->curve_name;
279 * Set an EC_POINT to the point at infinity. A point at infinity is
280 * represented by having Z=0.
282 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
291 * Set the coordinates of an EC_POINT using affine coordinates. Note that
292 * the simple implementation only uses affine coordinates.
294 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
297 const BIGNUM *y, BN_CTX *ctx)
300 if (x == NULL || y == NULL) {
301 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
302 ERR_R_PASSED_NULL_PARAMETER);
306 if (!BN_copy(point->X, x))
308 BN_set_negative(point->X, 0);
309 if (!BN_copy(point->Y, y))
311 BN_set_negative(point->Y, 0);
312 if (!BN_copy(point->Z, BN_value_one()))
314 BN_set_negative(point->Z, 0);
323 * Gets the affine coordinates of an EC_POINT. Note that the simple
324 * implementation only uses affine coordinates.
326 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
327 const EC_POINT *point,
328 BIGNUM *x, BIGNUM *y,
333 if (EC_POINT_is_at_infinity(group, point)) {
334 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
335 EC_R_POINT_AT_INFINITY);
339 if (BN_cmp(point->Z, BN_value_one())) {
340 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
341 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
345 if (!BN_copy(x, point->X))
347 BN_set_negative(x, 0);
350 if (!BN_copy(y, point->Y))
352 BN_set_negative(y, 0);
361 * Computes a + b and stores the result in r. r could be a or b, a could be
362 * b. Uses algorithm A.10.2 of IEEE P1363.
364 int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
365 const EC_POINT *b, BN_CTX *ctx)
367 BN_CTX *new_ctx = NULL;
368 BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
371 if (EC_POINT_is_at_infinity(group, a)) {
372 if (!EC_POINT_copy(r, b))
377 if (EC_POINT_is_at_infinity(group, b)) {
378 if (!EC_POINT_copy(r, a))
384 ctx = new_ctx = BN_CTX_new();
390 x0 = BN_CTX_get(ctx);
391 y0 = BN_CTX_get(ctx);
392 x1 = BN_CTX_get(ctx);
393 y1 = BN_CTX_get(ctx);
394 x2 = BN_CTX_get(ctx);
395 y2 = BN_CTX_get(ctx);
402 if (!BN_copy(x0, a->X))
404 if (!BN_copy(y0, a->Y))
407 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
411 if (!BN_copy(x1, b->X))
413 if (!BN_copy(y1, b->Y))
416 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
420 if (BN_GF2m_cmp(x0, x1)) {
421 if (!BN_GF2m_add(t, x0, x1))
423 if (!BN_GF2m_add(s, y0, y1))
425 if (!group->meth->field_div(group, s, s, t, ctx))
427 if (!group->meth->field_sqr(group, x2, s, ctx))
429 if (!BN_GF2m_add(x2, x2, group->a))
431 if (!BN_GF2m_add(x2, x2, s))
433 if (!BN_GF2m_add(x2, x2, t))
436 if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
437 if (!EC_POINT_set_to_infinity(group, r))
442 if (!group->meth->field_div(group, s, y1, x1, ctx))
444 if (!BN_GF2m_add(s, s, x1))
447 if (!group->meth->field_sqr(group, x2, s, ctx))
449 if (!BN_GF2m_add(x2, x2, s))
451 if (!BN_GF2m_add(x2, x2, group->a))
455 if (!BN_GF2m_add(y2, x1, x2))
457 if (!group->meth->field_mul(group, y2, y2, s, ctx))
459 if (!BN_GF2m_add(y2, y2, x2))
461 if (!BN_GF2m_add(y2, y2, y1))
464 if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
471 BN_CTX_free(new_ctx);
476 * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
477 * A.10.2 of IEEE P1363.
479 int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
482 return ec_GF2m_simple_add(group, r, a, a, ctx);
485 int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
487 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
488 /* point is its own inverse */
491 if (!EC_POINT_make_affine(group, point, ctx))
493 return BN_GF2m_add(point->Y, point->X, point->Y);
496 /* Indicates whether the given point is the point at infinity. */
497 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
498 const EC_POINT *point)
500 return BN_is_zero(point->Z);
504 * Determines whether the given EC_POINT is an actual point on the curve defined
505 * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
506 * y^2 + x*y = x^3 + a*x^2 + b.
508 int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
512 BN_CTX *new_ctx = NULL;
514 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
515 const BIGNUM *, BN_CTX *);
516 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
518 if (EC_POINT_is_at_infinity(group, point))
521 field_mul = group->meth->field_mul;
522 field_sqr = group->meth->field_sqr;
524 /* only support affine coordinates */
525 if (!point->Z_is_one)
529 ctx = new_ctx = BN_CTX_new();
535 y2 = BN_CTX_get(ctx);
536 lh = BN_CTX_get(ctx);
541 * We have a curve defined by a Weierstrass equation
542 * y^2 + x*y = x^3 + a*x^2 + b.
543 * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
544 * <=> ((x + a) * x + y ) * x + b + y^2 = 0
546 if (!BN_GF2m_add(lh, point->X, group->a))
548 if (!field_mul(group, lh, lh, point->X, ctx))
550 if (!BN_GF2m_add(lh, lh, point->Y))
552 if (!field_mul(group, lh, lh, point->X, ctx))
554 if (!BN_GF2m_add(lh, lh, group->b))
556 if (!field_sqr(group, y2, point->Y, ctx))
558 if (!BN_GF2m_add(lh, lh, y2))
560 ret = BN_is_zero(lh);
564 BN_CTX_free(new_ctx);
569 * Indicates whether two points are equal.
572 * 0 equal (in affine coordinates)
575 int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
576 const EC_POINT *b, BN_CTX *ctx)
578 BIGNUM *aX, *aY, *bX, *bY;
579 BN_CTX *new_ctx = NULL;
582 if (EC_POINT_is_at_infinity(group, a)) {
583 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
586 if (EC_POINT_is_at_infinity(group, b))
589 if (a->Z_is_one && b->Z_is_one) {
590 return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
594 ctx = new_ctx = BN_CTX_new();
600 aX = BN_CTX_get(ctx);
601 aY = BN_CTX_get(ctx);
602 bX = BN_CTX_get(ctx);
603 bY = BN_CTX_get(ctx);
607 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
609 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
611 ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
616 BN_CTX_free(new_ctx);
620 /* Forces the given EC_POINT to internally use affine coordinates. */
621 int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
624 BN_CTX *new_ctx = NULL;
628 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
632 ctx = new_ctx = BN_CTX_new();
643 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
645 if (!BN_copy(point->X, x))
647 if (!BN_copy(point->Y, y))
649 if (!BN_one(point->Z))
658 BN_CTX_free(new_ctx);
663 * Forces each of the EC_POINTs in the given array to use affine coordinates.
665 int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
666 EC_POINT *points[], BN_CTX *ctx)
670 for (i = 0; i < num; i++) {
671 if (!group->meth->make_affine(group, points[i], ctx))
678 /* Wrapper to simple binary polynomial field multiplication implementation. */
679 int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
680 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
682 return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
685 /* Wrapper to simple binary polynomial field squaring implementation. */
686 int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
687 const BIGNUM *a, BN_CTX *ctx)
689 return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
692 /* Wrapper to simple binary polynomial field division implementation. */
693 int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
694 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
696 return BN_GF2m_mod_div(r, a, b, group->field, ctx);
700 * Computes the multiplicative inverse of a in GF(2^m), storing the result in r.
701 * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
702 * SCA hardening is with blinding: BN_GF2m_mod_inv does that.
704 static int ec_GF2m_simple_field_inv(const EC_GROUP *group, BIGNUM *r,
705 const BIGNUM *a, BN_CTX *ctx)
709 if (!(ret = BN_GF2m_mod_inv(r, a, group->field, ctx)))
710 ECerr(EC_F_EC_GF2M_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
714 const EC_METHOD *EC_GF2m_simple_method(void)
716 static const EC_METHOD ret = {
717 EC_FLAGS_DEFAULT_OCT,
718 NID_X9_62_characteristic_two_field,
719 ec_GF2m_simple_group_init,
720 ec_GF2m_simple_group_finish,
721 ec_GF2m_simple_group_clear_finish,
722 ec_GF2m_simple_group_copy,
723 ec_GF2m_simple_group_set_curve,
724 ec_GF2m_simple_group_get_curve,
725 ec_GF2m_simple_group_get_degree,
726 ec_group_simple_order_bits,
727 ec_GF2m_simple_group_check_discriminant,
728 ec_GF2m_simple_point_init,
729 ec_GF2m_simple_point_finish,
730 ec_GF2m_simple_point_clear_finish,
731 ec_GF2m_simple_point_copy,
732 ec_GF2m_simple_point_set_to_infinity,
733 0 /* set_Jprojective_coordinates_GFp */ ,
734 0 /* get_Jprojective_coordinates_GFp */ ,
735 ec_GF2m_simple_point_set_affine_coordinates,
736 ec_GF2m_simple_point_get_affine_coordinates,
740 ec_GF2m_simple_invert,
741 ec_GF2m_simple_is_at_infinity,
742 ec_GF2m_simple_is_on_curve,
744 ec_GF2m_simple_make_affine,
745 ec_GF2m_simple_points_make_affine,
748 * the following three method functions are defined in ec2_mult.c
751 ec_GF2m_precompute_mult,
752 ec_GF2m_have_precompute_mult,
754 ec_GF2m_simple_field_mul,
755 ec_GF2m_simple_field_sqr,
756 ec_GF2m_simple_field_div,
757 ec_GF2m_simple_field_inv,
758 0 /* field_encode */ ,
759 0 /* field_decode */ ,
760 0, /* field_set_to_one */
761 ec_key_simple_priv2oct,
762 ec_key_simple_oct2priv,
764 ec_key_simple_generate_key,
765 ec_key_simple_check_key,
766 ec_key_simple_generate_public_key,
769 ecdh_simple_compute_key,
770 0 /* blind_coordinates */