2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
11 #include <openssl/err.h>
13 #include "internal/bn_int.h"
16 #ifndef OPENSSL_NO_EC2M
18 const EC_METHOD
*EC_GF2m_simple_method(void)
20 static const EC_METHOD ret
= {
22 NID_X9_62_characteristic_two_field
,
23 ec_GF2m_simple_group_init
,
24 ec_GF2m_simple_group_finish
,
25 ec_GF2m_simple_group_clear_finish
,
26 ec_GF2m_simple_group_copy
,
27 ec_GF2m_simple_group_set_curve
,
28 ec_GF2m_simple_group_get_curve
,
29 ec_GF2m_simple_group_get_degree
,
30 ec_group_simple_order_bits
,
31 ec_GF2m_simple_group_check_discriminant
,
32 ec_GF2m_simple_point_init
,
33 ec_GF2m_simple_point_finish
,
34 ec_GF2m_simple_point_clear_finish
,
35 ec_GF2m_simple_point_copy
,
36 ec_GF2m_simple_point_set_to_infinity
,
37 0 /* set_Jprojective_coordinates_GFp */ ,
38 0 /* get_Jprojective_coordinates_GFp */ ,
39 ec_GF2m_simple_point_set_affine_coordinates
,
40 ec_GF2m_simple_point_get_affine_coordinates
,
44 ec_GF2m_simple_invert
,
45 ec_GF2m_simple_is_at_infinity
,
46 ec_GF2m_simple_is_on_curve
,
48 ec_GF2m_simple_make_affine
,
49 ec_GF2m_simple_points_make_affine
,
51 0 /* precompute_mul */,
52 0 /* have_precompute_mul */,
53 ec_GF2m_simple_field_mul
,
54 ec_GF2m_simple_field_sqr
,
55 ec_GF2m_simple_field_div
,
56 0 /* field_encode */ ,
57 0 /* field_decode */ ,
58 0, /* field_set_to_one */
59 ec_key_simple_priv2oct
,
60 ec_key_simple_oct2priv
,
62 ec_key_simple_generate_key
,
63 ec_key_simple_check_key
,
64 ec_key_simple_generate_public_key
,
67 ecdh_simple_compute_key
74 * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
75 * are handled by EC_GROUP_new.
77 int ec_GF2m_simple_group_init(EC_GROUP
*group
)
79 group
->field
= BN_new();
83 if (group
->field
== NULL
|| group
->a
== NULL
|| group
->b
== NULL
) {
84 BN_free(group
->field
);
93 * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
94 * handled by EC_GROUP_free.
96 void ec_GF2m_simple_group_finish(EC_GROUP
*group
)
98 BN_free(group
->field
);
104 * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
105 * members are handled by EC_GROUP_clear_free.
107 void ec_GF2m_simple_group_clear_finish(EC_GROUP
*group
)
109 BN_clear_free(group
->field
);
110 BN_clear_free(group
->a
);
111 BN_clear_free(group
->b
);
121 * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
122 * handled by EC_GROUP_copy.
124 int ec_GF2m_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
126 if (!BN_copy(dest
->field
, src
->field
))
128 if (!BN_copy(dest
->a
, src
->a
))
130 if (!BN_copy(dest
->b
, src
->b
))
132 dest
->poly
[0] = src
->poly
[0];
133 dest
->poly
[1] = src
->poly
[1];
134 dest
->poly
[2] = src
->poly
[2];
135 dest
->poly
[3] = src
->poly
[3];
136 dest
->poly
[4] = src
->poly
[4];
137 dest
->poly
[5] = src
->poly
[5];
138 if (bn_wexpand(dest
->a
, (int)(dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) ==
141 if (bn_wexpand(dest
->b
, (int)(dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) ==
144 bn_set_all_zero(dest
->a
);
145 bn_set_all_zero(dest
->b
);
149 /* Set the curve parameters of an EC_GROUP structure. */
150 int ec_GF2m_simple_group_set_curve(EC_GROUP
*group
,
151 const BIGNUM
*p
, const BIGNUM
*a
,
152 const BIGNUM
*b
, BN_CTX
*ctx
)
157 if (!BN_copy(group
->field
, p
))
159 i
= BN_GF2m_poly2arr(group
->field
, group
->poly
, 6) - 1;
160 if ((i
!= 5) && (i
!= 3)) {
161 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE
, EC_R_UNSUPPORTED_FIELD
);
166 if (!BN_GF2m_mod_arr(group
->a
, a
, group
->poly
))
168 if (bn_wexpand(group
->a
, (int)(group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
171 bn_set_all_zero(group
->a
);
174 if (!BN_GF2m_mod_arr(group
->b
, b
, group
->poly
))
176 if (bn_wexpand(group
->b
, (int)(group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
179 bn_set_all_zero(group
->b
);
187 * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
188 * then there values will not be set but the method will return with success.
190 int ec_GF2m_simple_group_get_curve(const EC_GROUP
*group
, BIGNUM
*p
,
191 BIGNUM
*a
, BIGNUM
*b
, BN_CTX
*ctx
)
196 if (!BN_copy(p
, group
->field
))
201 if (!BN_copy(a
, group
->a
))
206 if (!BN_copy(b
, group
->b
))
217 * Gets the degree of the field. For a curve over GF(2^m) this is the value
220 int ec_GF2m_simple_group_get_degree(const EC_GROUP
*group
)
222 return BN_num_bits(group
->field
) - 1;
226 * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
227 * elliptic curve <=> b != 0 (mod p)
229 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP
*group
,
234 BN_CTX
*new_ctx
= NULL
;
237 ctx
= new_ctx
= BN_CTX_new();
239 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT
,
240 ERR_R_MALLOC_FAILURE
);
249 if (!BN_GF2m_mod_arr(b
, group
->b
, group
->poly
))
253 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
254 * curve <=> b != 0 (mod p)
264 BN_CTX_free(new_ctx
);
268 /* Initializes an EC_POINT. */
269 int ec_GF2m_simple_point_init(EC_POINT
*point
)
275 if (point
->X
== NULL
|| point
->Y
== NULL
|| point
->Z
== NULL
) {
284 /* Frees an EC_POINT. */
285 void ec_GF2m_simple_point_finish(EC_POINT
*point
)
292 /* Clears and frees an EC_POINT. */
293 void ec_GF2m_simple_point_clear_finish(EC_POINT
*point
)
295 BN_clear_free(point
->X
);
296 BN_clear_free(point
->Y
);
297 BN_clear_free(point
->Z
);
302 * Copy the contents of one EC_POINT into another. Assumes dest is
305 int ec_GF2m_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
307 if (!BN_copy(dest
->X
, src
->X
))
309 if (!BN_copy(dest
->Y
, src
->Y
))
311 if (!BN_copy(dest
->Z
, src
->Z
))
313 dest
->Z_is_one
= src
->Z_is_one
;
314 dest
->curve_name
= src
->curve_name
;
320 * Set an EC_POINT to the point at infinity. A point at infinity is
321 * represented by having Z=0.
323 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP
*group
,
332 * Set the coordinates of an EC_POINT using affine coordinates. Note that
333 * the simple implementation only uses affine coordinates.
335 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP
*group
,
338 const BIGNUM
*y
, BN_CTX
*ctx
)
341 if (x
== NULL
|| y
== NULL
) {
342 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES
,
343 ERR_R_PASSED_NULL_PARAMETER
);
347 if (!BN_copy(point
->X
, x
))
349 BN_set_negative(point
->X
, 0);
350 if (!BN_copy(point
->Y
, y
))
352 BN_set_negative(point
->Y
, 0);
353 if (!BN_copy(point
->Z
, BN_value_one()))
355 BN_set_negative(point
->Z
, 0);
364 * Gets the affine coordinates of an EC_POINT. Note that the simple
365 * implementation only uses affine coordinates.
367 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP
*group
,
368 const EC_POINT
*point
,
369 BIGNUM
*x
, BIGNUM
*y
,
374 if (EC_POINT_is_at_infinity(group
, point
)) {
375 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
376 EC_R_POINT_AT_INFINITY
);
380 if (BN_cmp(point
->Z
, BN_value_one())) {
381 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
382 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
386 if (!BN_copy(x
, point
->X
))
388 BN_set_negative(x
, 0);
391 if (!BN_copy(y
, point
->Y
))
393 BN_set_negative(y
, 0);
402 * Computes a + b and stores the result in r. r could be a or b, a could be
403 * b. Uses algorithm A.10.2 of IEEE P1363.
405 int ec_GF2m_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
406 const EC_POINT
*b
, BN_CTX
*ctx
)
408 BN_CTX
*new_ctx
= NULL
;
409 BIGNUM
*x0
, *y0
, *x1
, *y1
, *x2
, *y2
, *s
, *t
;
412 if (EC_POINT_is_at_infinity(group
, a
)) {
413 if (!EC_POINT_copy(r
, b
))
418 if (EC_POINT_is_at_infinity(group
, b
)) {
419 if (!EC_POINT_copy(r
, a
))
425 ctx
= new_ctx
= BN_CTX_new();
431 x0
= BN_CTX_get(ctx
);
432 y0
= BN_CTX_get(ctx
);
433 x1
= BN_CTX_get(ctx
);
434 y1
= BN_CTX_get(ctx
);
435 x2
= BN_CTX_get(ctx
);
436 y2
= BN_CTX_get(ctx
);
443 if (!BN_copy(x0
, a
->X
))
445 if (!BN_copy(y0
, a
->Y
))
448 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, x0
, y0
, ctx
))
452 if (!BN_copy(x1
, b
->X
))
454 if (!BN_copy(y1
, b
->Y
))
457 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, x1
, y1
, ctx
))
461 if (BN_GF2m_cmp(x0
, x1
)) {
462 if (!BN_GF2m_add(t
, x0
, x1
))
464 if (!BN_GF2m_add(s
, y0
, y1
))
466 if (!group
->meth
->field_div(group
, s
, s
, t
, ctx
))
468 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
470 if (!BN_GF2m_add(x2
, x2
, group
->a
))
472 if (!BN_GF2m_add(x2
, x2
, s
))
474 if (!BN_GF2m_add(x2
, x2
, t
))
477 if (BN_GF2m_cmp(y0
, y1
) || BN_is_zero(x1
)) {
478 if (!EC_POINT_set_to_infinity(group
, r
))
483 if (!group
->meth
->field_div(group
, s
, y1
, x1
, ctx
))
485 if (!BN_GF2m_add(s
, s
, x1
))
488 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
490 if (!BN_GF2m_add(x2
, x2
, s
))
492 if (!BN_GF2m_add(x2
, x2
, group
->a
))
496 if (!BN_GF2m_add(y2
, x1
, x2
))
498 if (!group
->meth
->field_mul(group
, y2
, y2
, s
, ctx
))
500 if (!BN_GF2m_add(y2
, y2
, x2
))
502 if (!BN_GF2m_add(y2
, y2
, y1
))
505 if (!EC_POINT_set_affine_coordinates_GF2m(group
, r
, x2
, y2
, ctx
))
512 BN_CTX_free(new_ctx
);
517 * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
518 * A.10.2 of IEEE P1363.
520 int ec_GF2m_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
523 return ec_GF2m_simple_add(group
, r
, a
, a
, ctx
);
526 int ec_GF2m_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
528 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(point
->Y
))
529 /* point is its own inverse */
532 if (!EC_POINT_make_affine(group
, point
, ctx
))
534 return BN_GF2m_add(point
->Y
, point
->X
, point
->Y
);
537 /* Indicates whether the given point is the point at infinity. */
538 int ec_GF2m_simple_is_at_infinity(const EC_GROUP
*group
,
539 const EC_POINT
*point
)
541 return BN_is_zero(point
->Z
);
545 * Determines whether the given EC_POINT is an actual point on the curve defined
546 * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
547 * y^2 + x*y = x^3 + a*x^2 + b.
549 int ec_GF2m_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
553 BN_CTX
*new_ctx
= NULL
;
555 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
556 const BIGNUM
*, BN_CTX
*);
557 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
559 if (EC_POINT_is_at_infinity(group
, point
))
562 field_mul
= group
->meth
->field_mul
;
563 field_sqr
= group
->meth
->field_sqr
;
565 /* only support affine coordinates */
566 if (!point
->Z_is_one
)
570 ctx
= new_ctx
= BN_CTX_new();
576 y2
= BN_CTX_get(ctx
);
577 lh
= BN_CTX_get(ctx
);
582 * We have a curve defined by a Weierstrass equation
583 * y^2 + x*y = x^3 + a*x^2 + b.
584 * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
585 * <=> ((x + a) * x + y ) * x + b + y^2 = 0
587 if (!BN_GF2m_add(lh
, point
->X
, group
->a
))
589 if (!field_mul(group
, lh
, lh
, point
->X
, ctx
))
591 if (!BN_GF2m_add(lh
, lh
, point
->Y
))
593 if (!field_mul(group
, lh
, lh
, point
->X
, ctx
))
595 if (!BN_GF2m_add(lh
, lh
, group
->b
))
597 if (!field_sqr(group
, y2
, point
->Y
, ctx
))
599 if (!BN_GF2m_add(lh
, lh
, y2
))
601 ret
= BN_is_zero(lh
);
605 BN_CTX_free(new_ctx
);
610 * Indicates whether two points are equal.
613 * 0 equal (in affine coordinates)
616 int ec_GF2m_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
,
617 const EC_POINT
*b
, BN_CTX
*ctx
)
619 BIGNUM
*aX
, *aY
, *bX
, *bY
;
620 BN_CTX
*new_ctx
= NULL
;
623 if (EC_POINT_is_at_infinity(group
, a
)) {
624 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
627 if (EC_POINT_is_at_infinity(group
, b
))
630 if (a
->Z_is_one
&& b
->Z_is_one
) {
631 return ((BN_cmp(a
->X
, b
->X
) == 0) && BN_cmp(a
->Y
, b
->Y
) == 0) ? 0 : 1;
635 ctx
= new_ctx
= BN_CTX_new();
641 aX
= BN_CTX_get(ctx
);
642 aY
= BN_CTX_get(ctx
);
643 bX
= BN_CTX_get(ctx
);
644 bY
= BN_CTX_get(ctx
);
648 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, aX
, aY
, ctx
))
650 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, bX
, bY
, ctx
))
652 ret
= ((BN_cmp(aX
, bX
) == 0) && BN_cmp(aY
, bY
) == 0) ? 0 : 1;
656 BN_CTX_free(new_ctx
);
660 /* Forces the given EC_POINT to internally use affine coordinates. */
661 int ec_GF2m_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
,
664 BN_CTX
*new_ctx
= NULL
;
668 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
672 ctx
= new_ctx
= BN_CTX_new();
683 if (!EC_POINT_get_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
685 if (!BN_copy(point
->X
, x
))
687 if (!BN_copy(point
->Y
, y
))
689 if (!BN_one(point
->Z
))
697 BN_CTX_free(new_ctx
);
702 * Forces each of the EC_POINTs in the given array to use affine coordinates.
704 int ec_GF2m_simple_points_make_affine(const EC_GROUP
*group
, size_t num
,
705 EC_POINT
*points
[], BN_CTX
*ctx
)
709 for (i
= 0; i
< num
; i
++) {
710 if (!group
->meth
->make_affine(group
, points
[i
], ctx
))
717 /* Wrapper to simple binary polynomial field multiplication implementation. */
718 int ec_GF2m_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
,
719 const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
721 return BN_GF2m_mod_mul_arr(r
, a
, b
, group
->poly
, ctx
);
724 /* Wrapper to simple binary polynomial field squaring implementation. */
725 int ec_GF2m_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
,
726 const BIGNUM
*a
, BN_CTX
*ctx
)
728 return BN_GF2m_mod_sqr_arr(r
, a
, group
->poly
, ctx
);
731 /* Wrapper to simple binary polynomial field division implementation. */
732 int ec_GF2m_simple_field_div(const EC_GROUP
*group
, BIGNUM
*r
,
733 const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
735 return BN_GF2m_mod_div(r
, a
, b
, group
->field
, ctx
);