1 /* crypto/ec/ec2_smpl.c */
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-2003 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/err.h>
74 const EC_METHOD
*EC_GF2m_simple_method(void)
76 static const EC_METHOD ret
= {
77 NID_X9_62_characteristic_two_field
,
78 ec_GF2m_simple_group_init
,
79 ec_GF2m_simple_group_finish
,
80 ec_GF2m_simple_group_clear_finish
,
81 ec_GF2m_simple_group_copy
,
82 ec_GF2m_simple_group_set_curve
,
83 ec_GF2m_simple_group_get_curve
,
84 ec_GF2m_simple_group_get_degree
,
85 ec_GF2m_simple_group_check_discriminant
,
86 ec_GF2m_simple_point_init
,
87 ec_GF2m_simple_point_finish
,
88 ec_GF2m_simple_point_clear_finish
,
89 ec_GF2m_simple_point_copy
,
90 ec_GF2m_simple_point_set_to_infinity
,
91 0 /* set_Jprojective_coordinates_GFp */ ,
92 0 /* get_Jprojective_coordinates_GFp */ ,
93 ec_GF2m_simple_point_set_affine_coordinates
,
94 ec_GF2m_simple_point_get_affine_coordinates
,
95 ec_GF2m_simple_set_compressed_coordinates
,
96 ec_GF2m_simple_point2oct
,
97 ec_GF2m_simple_oct2point
,
100 ec_GF2m_simple_invert
,
101 ec_GF2m_simple_is_at_infinity
,
102 ec_GF2m_simple_is_on_curve
,
104 ec_GF2m_simple_make_affine
,
105 ec_GF2m_simple_points_make_affine
,
108 * the following three method functions are defined in ec2_mult.c
111 ec_GF2m_precompute_mult
,
112 ec_GF2m_have_precompute_mult
,
114 ec_GF2m_simple_field_mul
,
115 ec_GF2m_simple_field_sqr
,
116 ec_GF2m_simple_field_div
,
117 0 /* field_encode */ ,
118 0 /* field_decode */ ,
119 0 /* field_set_to_one */
126 * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
127 * are handled by EC_GROUP_new.
129 int ec_GF2m_simple_group_init(EC_GROUP
*group
)
131 BN_init(&group
->field
);
138 * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
139 * handled by EC_GROUP_free.
141 void ec_GF2m_simple_group_finish(EC_GROUP
*group
)
143 BN_free(&group
->field
);
149 * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
150 * members are handled by EC_GROUP_clear_free.
152 void ec_GF2m_simple_group_clear_finish(EC_GROUP
*group
)
154 BN_clear_free(&group
->field
);
155 BN_clear_free(&group
->a
);
156 BN_clear_free(&group
->b
);
165 * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
166 * handled by EC_GROUP_copy.
168 int ec_GF2m_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
171 if (!BN_copy(&dest
->field
, &src
->field
))
173 if (!BN_copy(&dest
->a
, &src
->a
))
175 if (!BN_copy(&dest
->b
, &src
->b
))
177 dest
->poly
[0] = src
->poly
[0];
178 dest
->poly
[1] = src
->poly
[1];
179 dest
->poly
[2] = src
->poly
[2];
180 dest
->poly
[3] = src
->poly
[3];
181 dest
->poly
[4] = src
->poly
[4];
182 if (bn_wexpand(&dest
->a
, (int)(dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
185 if (bn_wexpand(&dest
->b
, (int)(dest
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
188 for (i
= dest
->a
.top
; i
< dest
->a
.dmax
; i
++)
190 for (i
= dest
->b
.top
; i
< dest
->b
.dmax
; i
++)
195 /* Set the curve parameters of an EC_GROUP structure. */
196 int ec_GF2m_simple_group_set_curve(EC_GROUP
*group
,
197 const BIGNUM
*p
, const BIGNUM
*a
,
198 const BIGNUM
*b
, BN_CTX
*ctx
)
203 if (!BN_copy(&group
->field
, p
))
205 i
= BN_GF2m_poly2arr(&group
->field
, group
->poly
, 5);
206 if ((i
!= 5) && (i
!= 3)) {
207 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE
, EC_R_UNSUPPORTED_FIELD
);
212 if (!BN_GF2m_mod_arr(&group
->a
, a
, group
->poly
))
214 if (bn_wexpand(&group
->a
, (int)(group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
217 for (i
= group
->a
.top
; i
< group
->a
.dmax
; i
++)
221 if (!BN_GF2m_mod_arr(&group
->b
, b
, group
->poly
))
223 if (bn_wexpand(&group
->b
, (int)(group
->poly
[0] + BN_BITS2
- 1) / BN_BITS2
)
226 for (i
= group
->b
.top
; i
< group
->b
.dmax
; i
++)
235 * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
236 * then there values will not be set but the method will return with success.
238 int ec_GF2m_simple_group_get_curve(const EC_GROUP
*group
, BIGNUM
*p
,
239 BIGNUM
*a
, BIGNUM
*b
, BN_CTX
*ctx
)
244 if (!BN_copy(p
, &group
->field
))
249 if (!BN_copy(a
, &group
->a
))
254 if (!BN_copy(b
, &group
->b
))
265 * Gets the degree of the field. For a curve over GF(2^m) this is the value
268 int ec_GF2m_simple_group_get_degree(const EC_GROUP
*group
)
270 return BN_num_bits(&group
->field
) - 1;
274 * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
275 * elliptic curve <=> b != 0 (mod p)
277 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP
*group
,
282 BN_CTX
*new_ctx
= NULL
;
285 ctx
= new_ctx
= BN_CTX_new();
287 ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT
,
288 ERR_R_MALLOC_FAILURE
);
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)
313 BN_CTX_free(new_ctx
);
317 /* Initializes an EC_POINT. */
318 int ec_GF2m_simple_point_init(EC_POINT
*point
)
326 /* Frees an EC_POINT. */
327 void ec_GF2m_simple_point_finish(EC_POINT
*point
)
334 /* Clears and frees an EC_POINT. */
335 void ec_GF2m_simple_point_clear_finish(EC_POINT
*point
)
337 BN_clear_free(&point
->X
);
338 BN_clear_free(&point
->Y
);
339 BN_clear_free(&point
->Z
);
344 * Copy the contents of one EC_POINT into another. Assumes dest is
347 int ec_GF2m_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
349 if (!BN_copy(&dest
->X
, &src
->X
))
351 if (!BN_copy(&dest
->Y
, &src
->Y
))
353 if (!BN_copy(&dest
->Z
, &src
->Z
))
355 dest
->Z_is_one
= src
->Z_is_one
;
361 * Set an EC_POINT to the point at infinity. A point at infinity is
362 * represented by having Z=0.
364 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP
*group
,
373 * Set the coordinates of an EC_POINT using affine coordinates. Note that
374 * the simple implementation only uses affine coordinates.
376 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP
*group
,
379 const BIGNUM
*y
, BN_CTX
*ctx
)
382 if (x
== NULL
|| y
== NULL
) {
383 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES
,
384 ERR_R_PASSED_NULL_PARAMETER
);
388 if (!BN_copy(&point
->X
, x
))
390 BN_set_negative(&point
->X
, 0);
391 if (!BN_copy(&point
->Y
, y
))
393 BN_set_negative(&point
->Y
, 0);
394 if (!BN_copy(&point
->Z
, BN_value_one()))
396 BN_set_negative(&point
->Z
, 0);
405 * Gets the affine coordinates of an EC_POINT. Note that the simple
406 * implementation only uses affine coordinates.
408 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP
*group
,
409 const EC_POINT
*point
,
410 BIGNUM
*x
, BIGNUM
*y
,
415 if (EC_POINT_is_at_infinity(group
, point
)) {
416 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
417 EC_R_POINT_AT_INFINITY
);
421 if (BN_cmp(&point
->Z
, BN_value_one())) {
422 ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
423 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
427 if (!BN_copy(x
, &point
->X
))
429 BN_set_negative(x
, 0);
432 if (!BN_copy(y
, &point
->Y
))
434 BN_set_negative(y
, 0);
442 /* Include patented algorithms. */
443 #include "ec2_smpt.c"
446 * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
447 * length will be returned. If the length len of buf is smaller than required
448 * an error will be returned. The point compression section of this function
449 * is patented by Certicom Corp. under US Patent 6,141,420. Point
450 * compression is disabled by default and can be enabled by defining the
451 * preprocessor macro OPENSSL_EC_BIN_PT_COMP at Configure-time.
453 size_t ec_GF2m_simple_point2oct(const EC_GROUP
*group
, const EC_POINT
*point
,
454 point_conversion_form_t form
,
455 unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
458 BN_CTX
*new_ctx
= NULL
;
461 size_t field_len
, i
, skip
;
463 #ifndef OPENSSL_EC_BIN_PT_COMP
464 if ((form
== POINT_CONVERSION_COMPRESSED
)
465 || (form
== POINT_CONVERSION_HYBRID
)) {
466 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, ERR_R_DISABLED
);
471 if ((form
!= POINT_CONVERSION_COMPRESSED
)
472 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
473 && (form
!= POINT_CONVERSION_HYBRID
)) {
474 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, EC_R_INVALID_FORM
);
478 if (EC_POINT_is_at_infinity(group
, point
)) {
479 /* encodes to a single 0 octet */
482 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
490 /* ret := required output buffer length */
491 field_len
= (EC_GROUP_get_degree(group
) + 7) / 8;
494 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
496 /* if 'buf' is NULL, just return required length */
499 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
504 ctx
= new_ctx
= BN_CTX_new();
513 yxi
= BN_CTX_get(ctx
);
517 if (!EC_POINT_get_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
521 #ifdef OPENSSL_EC_BIN_PT_COMP
522 if ((form
!= POINT_CONVERSION_UNCOMPRESSED
) && !BN_is_zero(x
)) {
523 if (!group
->meth
->field_div(group
, yxi
, y
, x
, ctx
))
532 skip
= field_len
- BN_num_bytes(x
);
533 if (skip
> field_len
) {
534 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
541 skip
= BN_bn2bin(x
, buf
+ i
);
543 if (i
!= 1 + field_len
) {
544 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
548 if (form
== POINT_CONVERSION_UNCOMPRESSED
549 || form
== POINT_CONVERSION_HYBRID
) {
550 skip
= field_len
- BN_num_bytes(y
);
551 if (skip
> field_len
) {
552 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
559 skip
= BN_bn2bin(y
, buf
+ i
);
564 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
572 BN_CTX_free(new_ctx
);
579 BN_CTX_free(new_ctx
);
584 * Converts an octet string representation to an EC_POINT. Note that the
585 * simple implementation only uses affine coordinates.
587 int ec_GF2m_simple_oct2point(const EC_GROUP
*group
, EC_POINT
*point
,
588 const unsigned char *buf
, size_t len
,
591 point_conversion_form_t form
;
593 BN_CTX
*new_ctx
= NULL
;
595 size_t field_len
, enc_len
;
599 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_BUFFER_TOO_SMALL
);
605 if ((form
!= 0) && (form
!= POINT_CONVERSION_COMPRESSED
)
606 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
607 && (form
!= POINT_CONVERSION_HYBRID
)) {
608 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
611 if ((form
== 0 || form
== POINT_CONVERSION_UNCOMPRESSED
) && y_bit
) {
612 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
618 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
622 return EC_POINT_set_to_infinity(group
, point
);
625 field_len
= (EC_GROUP_get_degree(group
) + 7) / 8;
628 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
630 if (len
!= enc_len
) {
631 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
636 ctx
= new_ctx
= BN_CTX_new();
644 yxi
= BN_CTX_get(ctx
);
648 if (!BN_bin2bn(buf
+ 1, field_len
, x
))
650 if (BN_ucmp(x
, &group
->field
) >= 0) {
651 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
655 if (form
== POINT_CONVERSION_COMPRESSED
) {
656 if (!EC_POINT_set_compressed_coordinates_GF2m
657 (group
, point
, x
, y_bit
, ctx
))
660 if (!BN_bin2bn(buf
+ 1 + field_len
, field_len
, y
))
662 if (BN_ucmp(y
, &group
->field
) >= 0) {
663 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
666 if (form
== POINT_CONVERSION_HYBRID
) {
667 if (!group
->meth
->field_div(group
, yxi
, y
, x
, ctx
))
669 if (y_bit
!= BN_is_odd(yxi
)) {
670 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
675 if (!EC_POINT_set_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
679 /* test required by X9.62 */
680 if (!EC_POINT_is_on_curve(group
, point
, ctx
)) {
681 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT
, EC_R_POINT_IS_NOT_ON_CURVE
);
690 BN_CTX_free(new_ctx
);
695 * Computes a + b and stores the result in r. r could be a or b, a could be
696 * b. Uses algorithm A.10.2 of IEEE P1363.
698 int ec_GF2m_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
699 const EC_POINT
*b
, BN_CTX
*ctx
)
701 BN_CTX
*new_ctx
= NULL
;
702 BIGNUM
*x0
, *y0
, *x1
, *y1
, *x2
, *y2
, *s
, *t
;
705 if (EC_POINT_is_at_infinity(group
, a
)) {
706 if (!EC_POINT_copy(r
, b
))
711 if (EC_POINT_is_at_infinity(group
, b
)) {
712 if (!EC_POINT_copy(r
, a
))
718 ctx
= new_ctx
= BN_CTX_new();
724 x0
= BN_CTX_get(ctx
);
725 y0
= BN_CTX_get(ctx
);
726 x1
= BN_CTX_get(ctx
);
727 y1
= BN_CTX_get(ctx
);
728 x2
= BN_CTX_get(ctx
);
729 y2
= BN_CTX_get(ctx
);
736 if (!BN_copy(x0
, &a
->X
))
738 if (!BN_copy(y0
, &a
->Y
))
741 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, x0
, y0
, ctx
))
745 if (!BN_copy(x1
, &b
->X
))
747 if (!BN_copy(y1
, &b
->Y
))
750 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, x1
, y1
, ctx
))
754 if (BN_GF2m_cmp(x0
, x1
)) {
755 if (!BN_GF2m_add(t
, x0
, x1
))
757 if (!BN_GF2m_add(s
, y0
, y1
))
759 if (!group
->meth
->field_div(group
, s
, s
, t
, ctx
))
761 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
763 if (!BN_GF2m_add(x2
, x2
, &group
->a
))
765 if (!BN_GF2m_add(x2
, x2
, s
))
767 if (!BN_GF2m_add(x2
, x2
, t
))
770 if (BN_GF2m_cmp(y0
, y1
) || BN_is_zero(x1
)) {
771 if (!EC_POINT_set_to_infinity(group
, r
))
776 if (!group
->meth
->field_div(group
, s
, y1
, x1
, ctx
))
778 if (!BN_GF2m_add(s
, s
, x1
))
781 if (!group
->meth
->field_sqr(group
, x2
, s
, ctx
))
783 if (!BN_GF2m_add(x2
, x2
, s
))
785 if (!BN_GF2m_add(x2
, x2
, &group
->a
))
789 if (!BN_GF2m_add(y2
, x1
, x2
))
791 if (!group
->meth
->field_mul(group
, y2
, y2
, s
, ctx
))
793 if (!BN_GF2m_add(y2
, y2
, x2
))
795 if (!BN_GF2m_add(y2
, y2
, y1
))
798 if (!EC_POINT_set_affine_coordinates_GF2m(group
, r
, x2
, y2
, ctx
))
806 BN_CTX_free(new_ctx
);
811 * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
812 * A.10.2 of IEEE P1363.
814 int ec_GF2m_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
817 return ec_GF2m_simple_add(group
, r
, a
, a
, ctx
);
820 int ec_GF2m_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
822 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(&point
->Y
))
823 /* point is its own inverse */
826 if (!EC_POINT_make_affine(group
, point
, ctx
))
828 return BN_GF2m_add(&point
->Y
, &point
->X
, &point
->Y
);
831 /* Indicates whether the given point is the point at infinity. */
832 int ec_GF2m_simple_is_at_infinity(const EC_GROUP
*group
,
833 const EC_POINT
*point
)
835 return BN_is_zero(&point
->Z
);
839 * Determines whether the given EC_POINT is an actual point on the curve defined
840 * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
841 * y^2 + x*y = x^3 + a*x^2 + b.
843 int ec_GF2m_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
847 BN_CTX
*new_ctx
= NULL
;
849 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
850 const BIGNUM
*, BN_CTX
*);
851 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
853 if (EC_POINT_is_at_infinity(group
, point
))
856 field_mul
= group
->meth
->field_mul
;
857 field_sqr
= group
->meth
->field_sqr
;
859 /* only support affine coordinates */
860 if (!point
->Z_is_one
)
864 ctx
= new_ctx
= BN_CTX_new();
870 y2
= BN_CTX_get(ctx
);
871 lh
= BN_CTX_get(ctx
);
876 * We have a curve defined by a Weierstrass equation
877 * y^2 + x*y = x^3 + a*x^2 + b.
878 * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
879 * <=> ((x + a) * x + y ) * x + b + y^2 = 0
881 if (!BN_GF2m_add(lh
, &point
->X
, &group
->a
))
883 if (!field_mul(group
, lh
, lh
, &point
->X
, ctx
))
885 if (!BN_GF2m_add(lh
, lh
, &point
->Y
))
887 if (!field_mul(group
, lh
, lh
, &point
->X
, ctx
))
889 if (!BN_GF2m_add(lh
, lh
, &group
->b
))
891 if (!field_sqr(group
, y2
, &point
->Y
, ctx
))
893 if (!BN_GF2m_add(lh
, lh
, y2
))
895 ret
= BN_is_zero(lh
);
900 BN_CTX_free(new_ctx
);
905 * Indicates whether two points are equal.
908 * 0 equal (in affine coordinates)
911 int ec_GF2m_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
,
912 const EC_POINT
*b
, BN_CTX
*ctx
)
914 BIGNUM
*aX
, *aY
, *bX
, *bY
;
915 BN_CTX
*new_ctx
= NULL
;
918 if (EC_POINT_is_at_infinity(group
, a
)) {
919 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
922 if (EC_POINT_is_at_infinity(group
, b
))
925 if (a
->Z_is_one
&& b
->Z_is_one
) {
926 return ((BN_cmp(&a
->X
, &b
->X
) == 0)
927 && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
931 ctx
= new_ctx
= BN_CTX_new();
937 aX
= BN_CTX_get(ctx
);
938 aY
= BN_CTX_get(ctx
);
939 bX
= BN_CTX_get(ctx
);
940 bY
= BN_CTX_get(ctx
);
944 if (!EC_POINT_get_affine_coordinates_GF2m(group
, a
, aX
, aY
, ctx
))
946 if (!EC_POINT_get_affine_coordinates_GF2m(group
, b
, bX
, bY
, ctx
))
948 ret
= ((BN_cmp(aX
, bX
) == 0) && BN_cmp(aY
, bY
) == 0) ? 0 : 1;
954 BN_CTX_free(new_ctx
);
958 /* Forces the given EC_POINT to internally use affine coordinates. */
959 int ec_GF2m_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
,
962 BN_CTX
*new_ctx
= NULL
;
966 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
970 ctx
= new_ctx
= BN_CTX_new();
981 if (!EC_POINT_get_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
983 if (!BN_copy(&point
->X
, x
))
985 if (!BN_copy(&point
->Y
, y
))
987 if (!BN_one(&point
->Z
))
996 BN_CTX_free(new_ctx
);
1001 * Forces each of the EC_POINTs in the given array to use affine coordinates.
1003 int ec_GF2m_simple_points_make_affine(const EC_GROUP
*group
, size_t num
,
1004 EC_POINT
*points
[], BN_CTX
*ctx
)
1008 for (i
= 0; i
< num
; i
++) {
1009 if (!group
->meth
->make_affine(group
, points
[i
], ctx
))
1016 /* Wrapper to simple binary polynomial field multiplication implementation. */
1017 int ec_GF2m_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
,
1018 const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
1020 return BN_GF2m_mod_mul_arr(r
, a
, b
, group
->poly
, ctx
);
1023 /* Wrapper to simple binary polynomial field squaring implementation. */
1024 int ec_GF2m_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
,
1025 const BIGNUM
*a
, BN_CTX
*ctx
)
1027 return BN_GF2m_mod_sqr_arr(r
, a
, group
->poly
, ctx
);
1030 /* Wrapper to simple binary polynomial field division implementation. */
1031 int ec_GF2m_simple_field_div(const EC_GROUP
*group
, BIGNUM
*r
,
1032 const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
1034 return BN_GF2m_mod_div(r
, a
, b
, &group
->field
, ctx
);