1 /* $OpenBSD: ec2_smpl.c,v 1.15 2017/01/29 17:49:23 beck 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
,
112 * the following three method functions are defined in
115 .mul
= ec_GF2m_simple_mul
,
116 .precompute_mult
= ec_GF2m_precompute_mult
,
117 .have_precompute_mult
= ec_GF2m_have_precompute_mult
,
119 .field_mul
= ec_GF2m_simple_field_mul
,
120 .field_sqr
= ec_GF2m_simple_field_sqr
,
121 .field_div
= ec_GF2m_simple_field_div
,
128 /* Initialize a GF(2^m)-based EC_GROUP structure.
129 * Note that all other members are handled by EC_GROUP_new.
132 ec_GF2m_simple_group_init(EC_GROUP
* group
)
134 BN_init(&group
->field
);
141 /* Free a GF(2^m)-based EC_GROUP structure.
142 * Note that all other members are handled by EC_GROUP_free.
145 ec_GF2m_simple_group_finish(EC_GROUP
* group
)
147 BN_free(&group
->field
);
153 /* Clear and free a GF(2^m)-based EC_GROUP structure.
154 * Note that all other members are handled by EC_GROUP_clear_free.
157 ec_GF2m_simple_group_clear_finish(EC_GROUP
* group
)
159 BN_clear_free(&group
->field
);
160 BN_clear_free(&group
->a
);
161 BN_clear_free(&group
->b
);
171 /* Copy a GF(2^m)-based EC_GROUP structure.
172 * Note that all other members are handled by EC_GROUP_copy.
175 ec_GF2m_simple_group_copy(EC_GROUP
* dest
, const EC_GROUP
* src
)
179 if (!BN_copy(&dest
->field
, &src
->field
))
181 if (!BN_copy(&dest
->a
, &src
->a
))
183 if (!BN_copy(&dest
->b
, &src
->b
))
185 dest
->poly
[0] = src
->poly
[0];
186 dest
->poly
[1] = src
->poly
[1];
187 dest
->poly
[2] = src
->poly
[2];
188 dest
->poly
[3] = src
->poly
[3];
189 dest
->poly
[4] = src
->poly
[4];
190 dest
->poly
[5] = src
->poly
[5];
191 if (bn_wexpand(&dest
->a
, (int) (dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) == NULL
)
193 if (bn_wexpand(&dest
->b
, (int) (dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) == NULL
)
195 for (i
= dest
->a
.top
; i
< dest
->a
.dmax
; i
++)
197 for (i
= dest
->b
.top
; i
< dest
->b
.dmax
; i
++)
203 /* Set the curve parameters of an EC_GROUP structure. */
205 ec_GF2m_simple_group_set_curve(EC_GROUP
* group
,
206 const BIGNUM
* p
, const BIGNUM
* a
, const BIGNUM
* b
, BN_CTX
* ctx
)
211 if (!BN_copy(&group
->field
, p
))
213 i
= BN_GF2m_poly2arr(&group
->field
, group
->poly
, 6) - 1;
214 if ((i
!= 5) && (i
!= 3)) {
215 ECerror(EC_R_UNSUPPORTED_FIELD
);
219 if (!BN_GF2m_mod_arr(&group
->a
, a
, group
->poly
))
221 if (bn_wexpand(&group
->a
, (int) (group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) == NULL
)
223 for (i
= group
->a
.top
; i
< group
->a
.dmax
; i
++)
227 if (!BN_GF2m_mod_arr(&group
->b
, b
, group
->poly
))
229 if (bn_wexpand(&group
->b
, (int) (group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
) == NULL
)
231 for (i
= group
->b
.top
; i
< group
->b
.dmax
; i
++)
240 /* Get the curve parameters of an EC_GROUP structure.
241 * If p, a, or b are NULL then there values will not be set but the method will return with success.
244 ec_GF2m_simple_group_get_curve(const EC_GROUP
*group
,
245 BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
, BN_CTX
*ctx
)
250 if (!BN_copy(p
, &group
->field
))
254 if (!BN_copy(a
, &group
->a
))
258 if (!BN_copy(b
, &group
->b
))
268 /* Gets the degree of the field. For a curve over GF(2^m) this is the value m. */
270 ec_GF2m_simple_group_get_degree(const EC_GROUP
* group
)
272 return BN_num_bits(&group
->field
) - 1;
276 /* Checks the discriminant of the curve.
277 * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
280 ec_GF2m_simple_group_check_discriminant(const EC_GROUP
* group
, BN_CTX
* ctx
)
284 BN_CTX
*new_ctx
= NULL
;
287 ctx
= new_ctx
= BN_CTX_new();
289 ECerror(ERR_R_MALLOC_FAILURE
);
294 if ((b
= BN_CTX_get(ctx
)) == NULL
)
297 if (!BN_GF2m_mod_arr(b
, &group
->b
, group
->poly
))
301 * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
302 * curve <=> b != 0 (mod p)
312 BN_CTX_free(new_ctx
);
317 /* Initializes an EC_POINT. */
319 ec_GF2m_simple_point_init(EC_POINT
* point
)
328 /* Frees an EC_POINT. */
330 ec_GF2m_simple_point_finish(EC_POINT
* point
)
338 /* Clears and frees an EC_POINT. */
340 ec_GF2m_simple_point_clear_finish(EC_POINT
* point
)
342 BN_clear_free(&point
->X
);
343 BN_clear_free(&point
->Y
);
344 BN_clear_free(&point
->Z
);
349 /* Copy the contents of one EC_POINT into another. Assumes dest is initialized. */
351 ec_GF2m_simple_point_copy(EC_POINT
* dest
, const EC_POINT
* src
)
353 if (!BN_copy(&dest
->X
, &src
->X
))
355 if (!BN_copy(&dest
->Y
, &src
->Y
))
357 if (!BN_copy(&dest
->Z
, &src
->Z
))
359 dest
->Z_is_one
= src
->Z_is_one
;
365 /* Set an EC_POINT to the point at infinity.
366 * A point at infinity is represented by having Z=0.
369 ec_GF2m_simple_point_set_to_infinity(const EC_GROUP
* group
, EC_POINT
* point
)
377 /* Set the coordinates of an EC_POINT using affine coordinates.
378 * Note that the simple implementation only uses affine coordinates.
381 ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP
* group
, EC_POINT
* point
,
382 const BIGNUM
* x
, const BIGNUM
* y
, BN_CTX
* ctx
)
385 if (x
== NULL
|| y
== NULL
) {
386 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
389 if (!BN_copy(&point
->X
, x
))
391 BN_set_negative(&point
->X
, 0);
392 if (!BN_copy(&point
->Y
, y
))
394 BN_set_negative(&point
->Y
, 0);
395 if (!BN_copy(&point
->Z
, BN_value_one()))
397 BN_set_negative(&point
->Z
, 0);
406 /* Gets the affine coordinates of an EC_POINT.
407 * Note that the simple implementation only uses affine coordinates.
410 ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP
*group
,
411 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
, BN_CTX
*ctx
)
415 if (EC_POINT_is_at_infinity(group
, point
) > 0) {
416 ECerror(EC_R_POINT_AT_INFINITY
);
419 if (BN_cmp(&point
->Z
, BN_value_one())) {
420 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
424 if (!BN_copy(x
, &point
->X
))
426 BN_set_negative(x
, 0);
429 if (!BN_copy(y
, &point
->Y
))
431 BN_set_negative(y
, 0);
439 /* Computes a + b and stores the result in r. r could be a or b, a could be b.
440 * Uses algorithm A.10.2 of IEEE P1363.
443 ec_GF2m_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
444 const EC_POINT
*b
, BN_CTX
*ctx
)
446 BN_CTX
*new_ctx
= NULL
;
447 BIGNUM
*x0
, *y0
, *x1
, *y1
, *x2
, *y2
, *s
, *t
;
450 if (EC_POINT_is_at_infinity(group
, a
) > 0) {
451 if (!EC_POINT_copy(r
, b
))
455 if (EC_POINT_is_at_infinity(group
, b
) > 0) {
456 if (!EC_POINT_copy(r
, a
))
461 ctx
= new_ctx
= BN_CTX_new();
466 if ((x0
= BN_CTX_get(ctx
)) == NULL
)
468 if ((y0
= BN_CTX_get(ctx
)) == NULL
)
470 if ((x1
= BN_CTX_get(ctx
)) == NULL
)
472 if ((y1
= BN_CTX_get(ctx
)) == NULL
)
474 if ((x2
= BN_CTX_get(ctx
)) == NULL
)
476 if ((y2
= BN_CTX_get(ctx
)) == NULL
)
478 if ((s
= BN_CTX_get(ctx
)) == NULL
)
480 if ((t
= BN_CTX_get(ctx
)) == NULL
)
484 if (!BN_copy(x0
, &a
->X
))
486 if (!BN_copy(y0
, &a
->Y
))
489 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, x0
, y0
, ctx
))
493 if (!BN_copy(x1
, &b
->X
))
495 if (!BN_copy(y1
, &b
->Y
))
498 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, x1
, y1
, ctx
))
503 if (BN_GF2m_cmp(x0
, x1
)) {
504 if (!BN_GF2m_add(t
, x0
, x1
))
506 if (!BN_GF2m_add(s
, y0
, y1
))
508 if (!group
->meth
->field_div(group
, s
, s
, t
, ctx
))
510 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
512 if (!BN_GF2m_add(x2
, x2
, &group
->a
))
514 if (!BN_GF2m_add(x2
, x2
, s
))
516 if (!BN_GF2m_add(x2
, x2
, t
))
519 if (BN_GF2m_cmp(y0
, y1
) || BN_is_zero(x1
)) {
520 if (!EC_POINT_set_to_infinity(group
, r
))
525 if (!group
->meth
->field_div(group
, s
, y1
, x1
, ctx
))
527 if (!BN_GF2m_add(s
, s
, x1
))
530 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
532 if (!BN_GF2m_add(x2
, x2
, s
))
534 if (!BN_GF2m_add(x2
, x2
, &group
->a
))
538 if (!BN_GF2m_add(y2
, x1
, x2
))
540 if (!group
->meth
->field_mul(group
, y2
, y2
, s
, ctx
))
542 if (!BN_GF2m_add(y2
, y2
, x2
))
544 if (!BN_GF2m_add(y2
, y2
, y1
))
547 if (!EC_POINT_set_affine_coordinates_GF2m(group
, r
, x2
, y2
, ctx
))
554 BN_CTX_free(new_ctx
);
559 /* Computes 2 * a and stores the result in r. r could be a.
560 * Uses algorithm A.10.2 of IEEE P1363.
563 ec_GF2m_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
566 return ec_GF2m_simple_add(group
, r
, a
, a
, ctx
);
570 ec_GF2m_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
572 if (EC_POINT_is_at_infinity(group
, point
) > 0 || BN_is_zero(&point
->Y
))
573 /* point is its own inverse */
576 if (!EC_POINT_make_affine(group
, point
, ctx
))
578 return BN_GF2m_add(&point
->Y
, &point
->X
, &point
->Y
);
582 /* Indicates whether the given point is the point at infinity. */
584 ec_GF2m_simple_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
586 return BN_is_zero(&point
->Z
);
590 /* Determines whether the given EC_POINT is an actual point on the curve defined
591 * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
592 * y^2 + x*y = x^3 + a*x^2 + b.
595 ec_GF2m_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
, BN_CTX
*ctx
)
598 BN_CTX
*new_ctx
= NULL
;
600 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
601 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
603 if (EC_POINT_is_at_infinity(group
, point
) > 0)
606 field_mul
= group
->meth
->field_mul
;
607 field_sqr
= group
->meth
->field_sqr
;
609 /* only support affine coordinates */
610 if (!point
->Z_is_one
)
614 ctx
= new_ctx
= BN_CTX_new();
619 if ((y2
= BN_CTX_get(ctx
)) == NULL
)
621 if ((lh
= BN_CTX_get(ctx
)) == NULL
)
625 * We have a curve defined by a Weierstrass equation y^2 + x*y = x^3
626 * + a*x^2 + b. <=> x^3 + a*x^2 + x*y + b + y^2 = 0 <=> ((x + a) * x
627 * + y ) * x + b + y^2 = 0
629 if (!BN_GF2m_add(lh
, &point
->X
, &group
->a
))
631 if (!field_mul(group
, lh
, lh
, &point
->X
, ctx
))
633 if (!BN_GF2m_add(lh
, lh
, &point
->Y
))
635 if (!field_mul(group
, lh
, lh
, &point
->X
, ctx
))
637 if (!BN_GF2m_add(lh
, lh
, &group
->b
))
639 if (!field_sqr(group
, y2
, &point
->Y
, ctx
))
641 if (!BN_GF2m_add(lh
, lh
, y2
))
643 ret
= BN_is_zero(lh
);
647 BN_CTX_free(new_ctx
);
652 /* Indicates whether two points are equal.
655 * 0 equal (in affine coordinates)
659 ec_GF2m_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
,
660 const EC_POINT
*b
, BN_CTX
*ctx
)
662 BIGNUM
*aX
, *aY
, *bX
, *bY
;
663 BN_CTX
*new_ctx
= NULL
;
666 if (EC_POINT_is_at_infinity(group
, a
) > 0) {
667 return EC_POINT_is_at_infinity(group
, b
) > 0 ? 0 : 1;
669 if (EC_POINT_is_at_infinity(group
, b
) > 0)
672 if (a
->Z_is_one
&& b
->Z_is_one
) {
673 return ((BN_cmp(&a
->X
, &b
->X
) == 0) && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
676 ctx
= new_ctx
= BN_CTX_new();
681 if ((aX
= BN_CTX_get(ctx
)) == NULL
)
683 if ((aY
= BN_CTX_get(ctx
)) == NULL
)
685 if ((bX
= BN_CTX_get(ctx
)) == NULL
)
687 if ((bY
= BN_CTX_get(ctx
)) == NULL
)
690 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, aX
, aY
, ctx
))
692 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, bX
, bY
, ctx
))
694 ret
= ((BN_cmp(aX
, bX
) == 0) && BN_cmp(aY
, bY
) == 0) ? 0 : 1;
699 BN_CTX_free(new_ctx
);
704 /* Forces the given EC_POINT to internally use affine coordinates. */
706 ec_GF2m_simple_make_affine(const EC_GROUP
* group
, EC_POINT
* point
, BN_CTX
* ctx
)
708 BN_CTX
*new_ctx
= NULL
;
712 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
) > 0)
716 ctx
= new_ctx
= BN_CTX_new();
721 if ((x
= BN_CTX_get(ctx
)) == NULL
)
723 if ((y
= BN_CTX_get(ctx
)) == NULL
)
726 if (!EC_POINT_get_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
728 if (!BN_copy(&point
->X
, x
))
730 if (!BN_copy(&point
->Y
, y
))
732 if (!BN_one(&point
->Z
))
740 BN_CTX_free(new_ctx
);
745 /* Forces each of the EC_POINTs in the given array to use affine coordinates. */
747 ec_GF2m_simple_points_make_affine(const EC_GROUP
*group
, size_t num
,
748 EC_POINT
*points
[], BN_CTX
*ctx
)
752 for (i
= 0; i
< num
; i
++) {
753 if (!group
->meth
->make_affine(group
, points
[i
], ctx
))
761 /* Wrapper to simple binary polynomial field multiplication implementation. */
763 ec_GF2m_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
764 const BIGNUM
*b
, BN_CTX
*ctx
)
766 return BN_GF2m_mod_mul_arr(r
, a
, b
, group
->poly
, ctx
);
770 /* Wrapper to simple binary polynomial field squaring implementation. */
772 ec_GF2m_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
775 return BN_GF2m_mod_sqr_arr(r
, a
, group
->poly
, ctx
);
779 /* Wrapper to simple binary polynomial field division implementation. */
781 ec_GF2m_simple_field_div(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
782 const BIGNUM
*b
, BN_CTX
*ctx
)
784 return BN_GF2m_mod_div(r
, a
, b
, &group
->field
, ctx
);