2 * Copyright 2002-2016 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
32 const EC_METHOD *EC_GF2m_simple_method(void)
34 static const EC_METHOD ret = {
36 NID_X9_62_characteristic_two_field,
37 ec_GF2m_simple_group_init,
38 ec_GF2m_simple_group_finish,
39 ec_GF2m_simple_group_clear_finish,
40 ec_GF2m_simple_group_copy,
41 ec_GF2m_simple_group_set_curve,
42 ec_GF2m_simple_group_get_curve,
43 ec_GF2m_simple_group_get_degree,
44 ec_group_simple_order_bits,
45 ec_GF2m_simple_group_check_discriminant,
46 ec_GF2m_simple_point_init,
47 ec_GF2m_simple_point_finish,
48 ec_GF2m_simple_point_clear_finish,
49 ec_GF2m_simple_point_copy,
50 ec_GF2m_simple_point_set_to_infinity,
51 0 /* set_Jprojective_coordinates_GFp */ ,
52 0 /* get_Jprojective_coordinates_GFp */ ,
53 ec_GF2m_simple_point_set_affine_coordinates,
54 ec_GF2m_simple_point_get_affine_coordinates,
58 ec_GF2m_simple_invert,
59 ec_GF2m_simple_is_at_infinity,
60 ec_GF2m_simple_is_on_curve,
62 ec_GF2m_simple_make_affine,
63 ec_GF2m_simple_points_make_affine,
66 * the following three method functions are defined in ec2_mult.c
69 ec_GF2m_precompute_mult,
70 ec_GF2m_have_precompute_mult,
72 ec_GF2m_simple_field_mul,
73 ec_GF2m_simple_field_sqr,
74 ec_GF2m_simple_field_div,
75 0 /* field_encode */ ,
76 0 /* field_decode */ ,
77 0, /* field_set_to_one */
78 ec_key_simple_priv2oct,
79 ec_key_simple_oct2priv,
81 ec_key_simple_generate_key,
82 ec_key_simple_check_key,
83 ec_key_simple_generate_public_key,
86 ecdh_simple_compute_key
93 * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
94 * are handled by EC_GROUP_new.
96 int ec_GF2m_simple_group_init(EC_GROUP *group)
98 group->field = BN_new();
102 if (group->field == NULL || group->a == NULL || group->b == NULL) {
103 BN_free(group->field);
112 * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
113 * handled by EC_GROUP_free.
115 void ec_GF2m_simple_group_finish(EC_GROUP *group)
117 BN_free(group->field);
123 * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
124 * members are handled by EC_GROUP_clear_free.
126 void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
128 BN_clear_free(group->field);
129 BN_clear_free(group->a);
130 BN_clear_free(group->b);
140 * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
141 * handled by EC_GROUP_copy.
143 int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
145 if (!BN_copy(dest->field, src->field))
147 if (!BN_copy(dest->a, src->a))
149 if (!BN_copy(dest->b, src->b))
151 dest->poly[0] = src->poly[0];
152 dest->poly[1] = src->poly[1];
153 dest->poly[2] = src->poly[2];
154 dest->poly[3] = src->poly[3];
155 dest->poly[4] = src->poly[4];
156 dest->poly[5] = src->poly[5];
157 if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
160 if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
163 bn_set_all_zero(dest->a);
164 bn_set_all_zero(dest->b);
168 /* Set the curve parameters of an EC_GROUP structure. */
169 int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
170 const BIGNUM *p, const BIGNUM *a,
171 const BIGNUM *b, BN_CTX *ctx)
176 if (!BN_copy(group->field, p))
178 i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
179 if ((i != 5) && (i != 3)) {
180 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
185 if (!BN_GF2m_mod_arr(group->a, a, group->poly))
187 if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
190 bn_set_all_zero(group->a);
193 if (!BN_GF2m_mod_arr(group->b, b, group->poly))
195 if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
198 bn_set_all_zero(group->b);
206 * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
207 * then there values will not be set but the method will return with success.
209 int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
210 BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
215 if (!BN_copy(p, group->field))
220 if (!BN_copy(a, group->a))
225 if (!BN_copy(b, group->b))
236 * Gets the degree of the field. For a curve over GF(2^m) this is the value
239 int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
241 return BN_num_bits(group->field) - 1;
245 * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
246 * elliptic curve <=> b != 0 (mod p)
248 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
253 BN_CTX *new_ctx = NULL;
256 ctx = new_ctx = BN_CTX_new();
258 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
259 ERR_R_MALLOC_FAILURE);
268 if (!BN_GF2m_mod_arr(b, group->b, group->poly))
272 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
273 * curve <=> b != 0 (mod p)
283 BN_CTX_free(new_ctx);
287 /* Initializes an EC_POINT. */
288 int ec_GF2m_simple_point_init(EC_POINT *point)
294 if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
303 /* Frees an EC_POINT. */
304 void ec_GF2m_simple_point_finish(EC_POINT *point)
311 /* Clears and frees an EC_POINT. */
312 void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
314 BN_clear_free(point->X);
315 BN_clear_free(point->Y);
316 BN_clear_free(point->Z);
321 * Copy the contents of one EC_POINT into another. Assumes dest is
324 int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
326 if (!BN_copy(dest->X, src->X))
328 if (!BN_copy(dest->Y, src->Y))
330 if (!BN_copy(dest->Z, src->Z))
332 dest->Z_is_one = src->Z_is_one;
338 * Set an EC_POINT to the point at infinity. A point at infinity is
339 * represented by having Z=0.
341 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
350 * Set the coordinates of an EC_POINT using affine coordinates. Note that
351 * the simple implementation only uses affine coordinates.
353 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
356 const BIGNUM *y, BN_CTX *ctx)
359 if (x == NULL || y == NULL) {
360 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
361 ERR_R_PASSED_NULL_PARAMETER);
365 if (!BN_copy(point->X, x))
367 BN_set_negative(point->X, 0);
368 if (!BN_copy(point->Y, y))
370 BN_set_negative(point->Y, 0);
371 if (!BN_copy(point->Z, BN_value_one()))
373 BN_set_negative(point->Z, 0);
382 * Gets the affine coordinates of an EC_POINT. Note that the simple
383 * implementation only uses affine coordinates.
385 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
386 const EC_POINT *point,
387 BIGNUM *x, BIGNUM *y,
392 if (EC_POINT_is_at_infinity(group, point)) {
393 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
394 EC_R_POINT_AT_INFINITY);
398 if (BN_cmp(point->Z, BN_value_one())) {
399 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
400 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
404 if (!BN_copy(x, point->X))
406 BN_set_negative(x, 0);
409 if (!BN_copy(y, point->Y))
411 BN_set_negative(y, 0);
420 * Computes a + b and stores the result in r. r could be a or b, a could be
421 * b. Uses algorithm A.10.2 of IEEE P1363.
423 int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
424 const EC_POINT *b, BN_CTX *ctx)
426 BN_CTX *new_ctx = NULL;
427 BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
430 if (EC_POINT_is_at_infinity(group, a)) {
431 if (!EC_POINT_copy(r, b))
436 if (EC_POINT_is_at_infinity(group, b)) {
437 if (!EC_POINT_copy(r, a))
443 ctx = new_ctx = BN_CTX_new();
449 x0 = BN_CTX_get(ctx);
450 y0 = BN_CTX_get(ctx);
451 x1 = BN_CTX_get(ctx);
452 y1 = BN_CTX_get(ctx);
453 x2 = BN_CTX_get(ctx);
454 y2 = BN_CTX_get(ctx);
461 if (!BN_copy(x0, a->X))
463 if (!BN_copy(y0, a->Y))
466 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
470 if (!BN_copy(x1, b->X))
472 if (!BN_copy(y1, b->Y))
475 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
479 if (BN_GF2m_cmp(x0, x1)) {
480 if (!BN_GF2m_add(t, x0, x1))
482 if (!BN_GF2m_add(s, y0, y1))
484 if (!group->meth->field_div(group, s, s, t, ctx))
486 if (!group->meth->field_sqr(group, x2, s, ctx))
488 if (!BN_GF2m_add(x2, x2, group->a))
490 if (!BN_GF2m_add(x2, x2, s))
492 if (!BN_GF2m_add(x2, x2, t))
495 if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
496 if (!EC_POINT_set_to_infinity(group, r))
501 if (!group->meth->field_div(group, s, y1, x1, ctx))
503 if (!BN_GF2m_add(s, s, x1))
506 if (!group->meth->field_sqr(group, x2, s, ctx))
508 if (!BN_GF2m_add(x2, x2, s))
510 if (!BN_GF2m_add(x2, x2, group->a))
514 if (!BN_GF2m_add(y2, x1, x2))
516 if (!group->meth->field_mul(group, y2, y2, s, ctx))
518 if (!BN_GF2m_add(y2, y2, x2))
520 if (!BN_GF2m_add(y2, y2, y1))
523 if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
530 BN_CTX_free(new_ctx);
535 * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
536 * A.10.2 of IEEE P1363.
538 int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
541 return ec_GF2m_simple_add(group, r, a, a, ctx);
544 int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
546 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
547 /* point is its own inverse */
550 if (!EC_POINT_make_affine(group, point, ctx))
552 return BN_GF2m_add(point->Y, point->X, point->Y);
555 /* Indicates whether the given point is the point at infinity. */
556 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
557 const EC_POINT *point)
559 return BN_is_zero(point->Z);
563 * Determines whether the given EC_POINT is an actual point on the curve defined
564 * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
565 * y^2 + x*y = x^3 + a*x^2 + b.
567 int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
571 BN_CTX *new_ctx = NULL;
573 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
574 const BIGNUM *, BN_CTX *);
575 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
577 if (EC_POINT_is_at_infinity(group, point))
580 field_mul = group->meth->field_mul;
581 field_sqr = group->meth->field_sqr;
583 /* only support affine coordinates */
584 if (!point->Z_is_one)
588 ctx = new_ctx = BN_CTX_new();
594 y2 = BN_CTX_get(ctx);
595 lh = BN_CTX_get(ctx);
600 * We have a curve defined by a Weierstrass equation
601 * y^2 + x*y = x^3 + a*x^2 + b.
602 * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
603 * <=> ((x + a) * x + y ) * x + b + y^2 = 0
605 if (!BN_GF2m_add(lh, point->X, group->a))
607 if (!field_mul(group, lh, lh, point->X, ctx))
609 if (!BN_GF2m_add(lh, lh, point->Y))
611 if (!field_mul(group, lh, lh, point->X, ctx))
613 if (!BN_GF2m_add(lh, lh, group->b))
615 if (!field_sqr(group, y2, point->Y, ctx))
617 if (!BN_GF2m_add(lh, lh, y2))
619 ret = BN_is_zero(lh);
623 BN_CTX_free(new_ctx);
628 * Indicates whether two points are equal.
631 * 0 equal (in affine coordinates)
634 int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
635 const EC_POINT *b, BN_CTX *ctx)
637 BIGNUM *aX, *aY, *bX, *bY;
638 BN_CTX *new_ctx = NULL;
641 if (EC_POINT_is_at_infinity(group, a)) {
642 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
645 if (EC_POINT_is_at_infinity(group, b))
648 if (a->Z_is_one && b->Z_is_one) {
649 return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
653 ctx = new_ctx = BN_CTX_new();
659 aX = BN_CTX_get(ctx);
660 aY = BN_CTX_get(ctx);
661 bX = BN_CTX_get(ctx);
662 bY = BN_CTX_get(ctx);
666 if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
668 if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
670 ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
675 BN_CTX_free(new_ctx);
679 /* Forces the given EC_POINT to internally use affine coordinates. */
680 int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
683 BN_CTX *new_ctx = NULL;
687 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
691 ctx = new_ctx = BN_CTX_new();
702 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
704 if (!BN_copy(point->X, x))
706 if (!BN_copy(point->Y, y))
708 if (!BN_one(point->Z))
717 BN_CTX_free(new_ctx);
722 * Forces each of the EC_POINTs in the given array to use affine coordinates.
724 int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
725 EC_POINT *points[], BN_CTX *ctx)
729 for (i = 0; i < num; i++) {
730 if (!group->meth->make_affine(group, points[i], ctx))
737 /* Wrapper to simple binary polynomial field multiplication implementation. */
738 int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
739 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
741 return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
744 /* Wrapper to simple binary polynomial field squaring implementation. */
745 int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
746 const BIGNUM *a, BN_CTX *ctx)
748 return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
751 /* Wrapper to simple binary polynomial field division implementation. */
752 int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
753 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
755 return BN_GF2m_mod_div(r, a, b, group->field, ctx);