2 * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. Includes code written by Bodo Moeller for the
6 /* ====================================================================
7 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* ====================================================================
60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
62 * and contributed to the OpenSSL project.
65 #include <openssl/err.h>
66 #include <openssl/symhacks.h>
70 const EC_METHOD
*EC_GFp_simple_method(void)
72 static const EC_METHOD ret
= {
74 NID_X9_62_prime_field
,
75 ec_GFp_simple_group_init
,
76 ec_GFp_simple_group_finish
,
77 ec_GFp_simple_group_clear_finish
,
78 ec_GFp_simple_group_copy
,
79 ec_GFp_simple_group_set_curve
,
80 ec_GFp_simple_group_get_curve
,
81 ec_GFp_simple_group_get_degree
,
82 ec_GFp_simple_group_check_discriminant
,
83 ec_GFp_simple_point_init
,
84 ec_GFp_simple_point_finish
,
85 ec_GFp_simple_point_clear_finish
,
86 ec_GFp_simple_point_copy
,
87 ec_GFp_simple_point_set_to_infinity
,
88 ec_GFp_simple_set_Jprojective_coordinates_GFp
,
89 ec_GFp_simple_get_Jprojective_coordinates_GFp
,
90 ec_GFp_simple_point_set_affine_coordinates
,
91 ec_GFp_simple_point_get_affine_coordinates
,
96 ec_GFp_simple_is_at_infinity
,
97 ec_GFp_simple_is_on_curve
,
99 ec_GFp_simple_make_affine
,
100 ec_GFp_simple_points_make_affine
,
102 0 /* precompute_mult */ ,
103 0 /* have_precompute_mult */ ,
104 ec_GFp_simple_field_mul
,
105 ec_GFp_simple_field_sqr
,
107 0 /* field_encode */ ,
108 0 /* field_decode */ ,
109 0 /* field_set_to_one */
116 * Most method functions in this file are designed to work with
117 * non-trivial representations of field elements if necessary
118 * (see ecp_mont.c): while standard modular addition and subtraction
119 * are used, the field_mul and field_sqr methods will be used for
120 * multiplication, and field_encode and field_decode (if defined)
121 * will be used for converting between representations.
123 * Functions ec_GFp_simple_points_make_affine() and
124 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
125 * that if a non-trivial representation is used, it is a Montgomery
126 * representation (i.e. 'encoding' means multiplying by some factor R).
129 int ec_GFp_simple_group_init(EC_GROUP
*group
)
131 group
->field
= BN_new();
134 if (group
->field
== NULL
|| group
->a
== NULL
|| group
->b
== NULL
) {
135 BN_free(group
->field
);
140 group
->a_is_minus3
= 0;
144 void ec_GFp_simple_group_finish(EC_GROUP
*group
)
146 BN_free(group
->field
);
151 void ec_GFp_simple_group_clear_finish(EC_GROUP
*group
)
153 BN_clear_free(group
->field
);
154 BN_clear_free(group
->a
);
155 BN_clear_free(group
->b
);
158 int ec_GFp_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
160 if (!BN_copy(dest
->field
, src
->field
))
162 if (!BN_copy(dest
->a
, src
->a
))
164 if (!BN_copy(dest
->b
, src
->b
))
167 dest
->a_is_minus3
= src
->a_is_minus3
;
172 int ec_GFp_simple_group_set_curve(EC_GROUP
*group
,
173 const BIGNUM
*p
, const BIGNUM
*a
,
174 const BIGNUM
*b
, BN_CTX
*ctx
)
177 BN_CTX
*new_ctx
= NULL
;
180 /* p must be a prime > 3 */
181 if (BN_num_bits(p
) <= 2 || !BN_is_odd(p
)) {
182 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE
, EC_R_INVALID_FIELD
);
187 ctx
= new_ctx
= BN_CTX_new();
193 tmp_a
= BN_CTX_get(ctx
);
198 if (!BN_copy(group
->field
, p
))
200 BN_set_negative(group
->field
, 0);
203 if (!BN_nnmod(tmp_a
, a
, p
, ctx
))
205 if (group
->meth
->field_encode
) {
206 if (!group
->meth
->field_encode(group
, group
->a
, tmp_a
, ctx
))
208 } else if (!BN_copy(group
->a
, tmp_a
))
212 if (!BN_nnmod(group
->b
, b
, p
, ctx
))
214 if (group
->meth
->field_encode
)
215 if (!group
->meth
->field_encode(group
, group
->b
, group
->b
, ctx
))
218 /* group->a_is_minus3 */
219 if (!BN_add_word(tmp_a
, 3))
221 group
->a_is_minus3
= (0 == BN_cmp(tmp_a
, group
->field
));
227 BN_CTX_free(new_ctx
);
231 int ec_GFp_simple_group_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
232 BIGNUM
*b
, BN_CTX
*ctx
)
235 BN_CTX
*new_ctx
= NULL
;
238 if (!BN_copy(p
, group
->field
))
242 if (a
!= NULL
|| b
!= NULL
) {
243 if (group
->meth
->field_decode
) {
245 ctx
= new_ctx
= BN_CTX_new();
250 if (!group
->meth
->field_decode(group
, a
, group
->a
, ctx
))
254 if (!group
->meth
->field_decode(group
, b
, group
->b
, ctx
))
259 if (!BN_copy(a
, group
->a
))
263 if (!BN_copy(b
, group
->b
))
272 BN_CTX_free(new_ctx
);
276 int ec_GFp_simple_group_get_degree(const EC_GROUP
*group
)
278 return BN_num_bits(group
->field
);
281 int ec_GFp_simple_group_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
284 BIGNUM
*a
, *b
, *order
, *tmp_1
, *tmp_2
;
285 const BIGNUM
*p
= group
->field
;
286 BN_CTX
*new_ctx
= NULL
;
289 ctx
= new_ctx
= BN_CTX_new();
291 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT
,
292 ERR_R_MALLOC_FAILURE
);
299 tmp_1
= BN_CTX_get(ctx
);
300 tmp_2
= BN_CTX_get(ctx
);
301 order
= BN_CTX_get(ctx
);
305 if (group
->meth
->field_decode
) {
306 if (!group
->meth
->field_decode(group
, a
, group
->a
, ctx
))
308 if (!group
->meth
->field_decode(group
, b
, group
->b
, ctx
))
311 if (!BN_copy(a
, group
->a
))
313 if (!BN_copy(b
, group
->b
))
318 * check the discriminant:
319 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
325 } else if (!BN_is_zero(b
)) {
326 if (!BN_mod_sqr(tmp_1
, a
, p
, ctx
))
328 if (!BN_mod_mul(tmp_2
, tmp_1
, a
, p
, ctx
))
330 if (!BN_lshift(tmp_1
, tmp_2
, 2))
334 if (!BN_mod_sqr(tmp_2
, b
, p
, ctx
))
336 if (!BN_mul_word(tmp_2
, 27))
340 if (!BN_mod_add(a
, tmp_1
, tmp_2
, p
, ctx
))
350 BN_CTX_free(new_ctx
);
354 int ec_GFp_simple_point_init(EC_POINT
*point
)
361 if (point
->X
== NULL
|| point
->Y
== NULL
|| point
->Z
== NULL
) {
370 void ec_GFp_simple_point_finish(EC_POINT
*point
)
377 void ec_GFp_simple_point_clear_finish(EC_POINT
*point
)
379 BN_clear_free(point
->X
);
380 BN_clear_free(point
->Y
);
381 BN_clear_free(point
->Z
);
385 int ec_GFp_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
387 if (!BN_copy(dest
->X
, src
->X
))
389 if (!BN_copy(dest
->Y
, src
->Y
))
391 if (!BN_copy(dest
->Z
, src
->Z
))
393 dest
->Z_is_one
= src
->Z_is_one
;
398 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP
*group
,
406 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
413 BN_CTX
*new_ctx
= NULL
;
417 ctx
= new_ctx
= BN_CTX_new();
423 if (!BN_nnmod(point
->X
, x
, group
->field
, ctx
))
425 if (group
->meth
->field_encode
) {
426 if (!group
->meth
->field_encode(group
, point
->X
, point
->X
, ctx
))
432 if (!BN_nnmod(point
->Y
, y
, group
->field
, ctx
))
434 if (group
->meth
->field_encode
) {
435 if (!group
->meth
->field_encode(group
, point
->Y
, point
->Y
, ctx
))
443 if (!BN_nnmod(point
->Z
, z
, group
->field
, ctx
))
445 Z_is_one
= BN_is_one(point
->Z
);
446 if (group
->meth
->field_encode
) {
447 if (Z_is_one
&& (group
->meth
->field_set_to_one
!= 0)) {
448 if (!group
->meth
->field_set_to_one(group
, point
->Z
, ctx
))
452 meth
->field_encode(group
, point
->Z
, point
->Z
, ctx
))
456 point
->Z_is_one
= Z_is_one
;
462 BN_CTX_free(new_ctx
);
466 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
467 const EC_POINT
*point
,
468 BIGNUM
*x
, BIGNUM
*y
,
469 BIGNUM
*z
, BN_CTX
*ctx
)
471 BN_CTX
*new_ctx
= NULL
;
474 if (group
->meth
->field_decode
!= 0) {
476 ctx
= new_ctx
= BN_CTX_new();
482 if (!group
->meth
->field_decode(group
, x
, point
->X
, ctx
))
486 if (!group
->meth
->field_decode(group
, y
, point
->Y
, ctx
))
490 if (!group
->meth
->field_decode(group
, z
, point
->Z
, ctx
))
495 if (!BN_copy(x
, point
->X
))
499 if (!BN_copy(y
, point
->Y
))
503 if (!BN_copy(z
, point
->Z
))
511 BN_CTX_free(new_ctx
);
515 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP
*group
,
518 const BIGNUM
*y
, BN_CTX
*ctx
)
520 if (x
== NULL
|| y
== NULL
) {
522 * unlike for projective coordinates, we do not tolerate this
524 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES
,
525 ERR_R_PASSED_NULL_PARAMETER
);
529 return EC_POINT_set_Jprojective_coordinates_GFp(group
, point
, x
, y
,
530 BN_value_one(), ctx
);
533 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP
*group
,
534 const EC_POINT
*point
,
535 BIGNUM
*x
, BIGNUM
*y
,
538 BN_CTX
*new_ctx
= NULL
;
539 BIGNUM
*Z
, *Z_1
, *Z_2
, *Z_3
;
543 if (EC_POINT_is_at_infinity(group
, point
)) {
544 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
545 EC_R_POINT_AT_INFINITY
);
550 ctx
= new_ctx
= BN_CTX_new();
557 Z_1
= BN_CTX_get(ctx
);
558 Z_2
= BN_CTX_get(ctx
);
559 Z_3
= BN_CTX_get(ctx
);
563 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
565 if (group
->meth
->field_decode
) {
566 if (!group
->meth
->field_decode(group
, Z
, point
->Z
, ctx
))
574 if (group
->meth
->field_decode
) {
576 if (!group
->meth
->field_decode(group
, x
, point
->X
, ctx
))
580 if (!group
->meth
->field_decode(group
, y
, point
->Y
, ctx
))
585 if (!BN_copy(x
, point
->X
))
589 if (!BN_copy(y
, point
->Y
))
594 if (!BN_mod_inverse(Z_1
, Z_
, group
->field
, ctx
)) {
595 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
600 if (group
->meth
->field_encode
== 0) {
601 /* field_sqr works on standard representation */
602 if (!group
->meth
->field_sqr(group
, Z_2
, Z_1
, ctx
))
605 if (!BN_mod_sqr(Z_2
, Z_1
, group
->field
, ctx
))
611 * in the Montgomery case, field_mul will cancel out Montgomery
614 if (!group
->meth
->field_mul(group
, x
, point
->X
, Z_2
, ctx
))
619 if (group
->meth
->field_encode
== 0) {
621 * field_mul works on standard representation
623 if (!group
->meth
->field_mul(group
, Z_3
, Z_2
, Z_1
, ctx
))
626 if (!BN_mod_mul(Z_3
, Z_2
, Z_1
, group
->field
, ctx
))
631 * in the Montgomery case, field_mul will cancel out Montgomery
634 if (!group
->meth
->field_mul(group
, y
, point
->Y
, Z_3
, ctx
))
643 BN_CTX_free(new_ctx
);
647 int ec_GFp_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
648 const EC_POINT
*b
, BN_CTX
*ctx
)
650 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
651 const BIGNUM
*, BN_CTX
*);
652 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
654 BN_CTX
*new_ctx
= NULL
;
655 BIGNUM
*n0
, *n1
, *n2
, *n3
, *n4
, *n5
, *n6
;
659 return EC_POINT_dbl(group
, r
, a
, ctx
);
660 if (EC_POINT_is_at_infinity(group
, a
))
661 return EC_POINT_copy(r
, b
);
662 if (EC_POINT_is_at_infinity(group
, b
))
663 return EC_POINT_copy(r
, a
);
665 field_mul
= group
->meth
->field_mul
;
666 field_sqr
= group
->meth
->field_sqr
;
670 ctx
= new_ctx
= BN_CTX_new();
676 n0
= BN_CTX_get(ctx
);
677 n1
= BN_CTX_get(ctx
);
678 n2
= BN_CTX_get(ctx
);
679 n3
= BN_CTX_get(ctx
);
680 n4
= BN_CTX_get(ctx
);
681 n5
= BN_CTX_get(ctx
);
682 n6
= BN_CTX_get(ctx
);
687 * Note that in this function we must not read components of 'a' or 'b'
688 * once we have written the corresponding components of 'r'. ('r' might
689 * be one of 'a' or 'b'.)
694 if (!BN_copy(n1
, a
->X
))
696 if (!BN_copy(n2
, a
->Y
))
701 if (!field_sqr(group
, n0
, b
->Z
, ctx
))
703 if (!field_mul(group
, n1
, a
->X
, n0
, ctx
))
705 /* n1 = X_a * Z_b^2 */
707 if (!field_mul(group
, n0
, n0
, b
->Z
, ctx
))
709 if (!field_mul(group
, n2
, a
->Y
, n0
, ctx
))
711 /* n2 = Y_a * Z_b^3 */
716 if (!BN_copy(n3
, b
->X
))
718 if (!BN_copy(n4
, b
->Y
))
723 if (!field_sqr(group
, n0
, a
->Z
, ctx
))
725 if (!field_mul(group
, n3
, b
->X
, n0
, ctx
))
727 /* n3 = X_b * Z_a^2 */
729 if (!field_mul(group
, n0
, n0
, a
->Z
, ctx
))
731 if (!field_mul(group
, n4
, b
->Y
, n0
, ctx
))
733 /* n4 = Y_b * Z_a^3 */
737 if (!BN_mod_sub_quick(n5
, n1
, n3
, p
))
739 if (!BN_mod_sub_quick(n6
, n2
, n4
, p
))
744 if (BN_is_zero(n5
)) {
745 if (BN_is_zero(n6
)) {
746 /* a is the same point as b */
748 ret
= EC_POINT_dbl(group
, r
, a
, ctx
);
752 /* a is the inverse of b */
761 if (!BN_mod_add_quick(n1
, n1
, n3
, p
))
763 if (!BN_mod_add_quick(n2
, n2
, n4
, p
))
769 if (a
->Z_is_one
&& b
->Z_is_one
) {
770 if (!BN_copy(r
->Z
, n5
))
774 if (!BN_copy(n0
, b
->Z
))
776 } else if (b
->Z_is_one
) {
777 if (!BN_copy(n0
, a
->Z
))
780 if (!field_mul(group
, n0
, a
->Z
, b
->Z
, ctx
))
783 if (!field_mul(group
, r
->Z
, n0
, n5
, ctx
))
787 /* Z_r = Z_a * Z_b * n5 */
790 if (!field_sqr(group
, n0
, n6
, ctx
))
792 if (!field_sqr(group
, n4
, n5
, ctx
))
794 if (!field_mul(group
, n3
, n1
, n4
, ctx
))
796 if (!BN_mod_sub_quick(r
->X
, n0
, n3
, p
))
798 /* X_r = n6^2 - n5^2 * 'n7' */
801 if (!BN_mod_lshift1_quick(n0
, r
->X
, p
))
803 if (!BN_mod_sub_quick(n0
, n3
, n0
, p
))
805 /* n9 = n5^2 * 'n7' - 2 * X_r */
808 if (!field_mul(group
, n0
, n0
, n6
, ctx
))
810 if (!field_mul(group
, n5
, n4
, n5
, ctx
))
811 goto end
; /* now n5 is n5^3 */
812 if (!field_mul(group
, n1
, n2
, n5
, ctx
))
814 if (!BN_mod_sub_quick(n0
, n0
, n1
, p
))
817 if (!BN_add(n0
, n0
, p
))
819 /* now 0 <= n0 < 2*p, and n0 is even */
820 if (!BN_rshift1(r
->Y
, n0
))
822 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
827 if (ctx
) /* otherwise we already called BN_CTX_end */
829 BN_CTX_free(new_ctx
);
833 int ec_GFp_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
836 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
837 const BIGNUM
*, BN_CTX
*);
838 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
840 BN_CTX
*new_ctx
= NULL
;
841 BIGNUM
*n0
, *n1
, *n2
, *n3
;
844 if (EC_POINT_is_at_infinity(group
, a
)) {
850 field_mul
= group
->meth
->field_mul
;
851 field_sqr
= group
->meth
->field_sqr
;
855 ctx
= new_ctx
= BN_CTX_new();
861 n0
= BN_CTX_get(ctx
);
862 n1
= BN_CTX_get(ctx
);
863 n2
= BN_CTX_get(ctx
);
864 n3
= BN_CTX_get(ctx
);
869 * Note that in this function we must not read components of 'a' once we
870 * have written the corresponding components of 'r'. ('r' might the same
876 if (!field_sqr(group
, n0
, a
->X
, ctx
))
878 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
880 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
882 if (!BN_mod_add_quick(n1
, n0
, group
->a
, p
))
884 /* n1 = 3 * X_a^2 + a_curve */
885 } else if (group
->a_is_minus3
) {
886 if (!field_sqr(group
, n1
, a
->Z
, ctx
))
888 if (!BN_mod_add_quick(n0
, a
->X
, n1
, p
))
890 if (!BN_mod_sub_quick(n2
, a
->X
, n1
, p
))
892 if (!field_mul(group
, n1
, n0
, n2
, ctx
))
894 if (!BN_mod_lshift1_quick(n0
, n1
, p
))
896 if (!BN_mod_add_quick(n1
, n0
, n1
, p
))
899 * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
900 * = 3 * X_a^2 - 3 * Z_a^4
903 if (!field_sqr(group
, n0
, a
->X
, ctx
))
905 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
907 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
909 if (!field_sqr(group
, n1
, a
->Z
, ctx
))
911 if (!field_sqr(group
, n1
, n1
, ctx
))
913 if (!field_mul(group
, n1
, n1
, group
->a
, ctx
))
915 if (!BN_mod_add_quick(n1
, n1
, n0
, p
))
917 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
922 if (!BN_copy(n0
, a
->Y
))
925 if (!field_mul(group
, n0
, a
->Y
, a
->Z
, ctx
))
928 if (!BN_mod_lshift1_quick(r
->Z
, n0
, p
))
931 /* Z_r = 2 * Y_a * Z_a */
934 if (!field_sqr(group
, n3
, a
->Y
, ctx
))
936 if (!field_mul(group
, n2
, a
->X
, n3
, ctx
))
938 if (!BN_mod_lshift_quick(n2
, n2
, 2, p
))
940 /* n2 = 4 * X_a * Y_a^2 */
943 if (!BN_mod_lshift1_quick(n0
, n2
, p
))
945 if (!field_sqr(group
, r
->X
, n1
, ctx
))
947 if (!BN_mod_sub_quick(r
->X
, r
->X
, n0
, p
))
949 /* X_r = n1^2 - 2 * n2 */
952 if (!field_sqr(group
, n0
, n3
, ctx
))
954 if (!BN_mod_lshift_quick(n3
, n0
, 3, p
))
959 if (!BN_mod_sub_quick(n0
, n2
, r
->X
, p
))
961 if (!field_mul(group
, n0
, n1
, n0
, ctx
))
963 if (!BN_mod_sub_quick(r
->Y
, n0
, n3
, p
))
965 /* Y_r = n1 * (n2 - X_r) - n3 */
971 BN_CTX_free(new_ctx
);
975 int ec_GFp_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
977 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(point
->Y
))
978 /* point is its own inverse */
981 return BN_usub(point
->Y
, group
->field
, point
->Y
);
984 int ec_GFp_simple_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
986 return BN_is_zero(point
->Z
);
989 int ec_GFp_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
992 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
993 const BIGNUM
*, BN_CTX
*);
994 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
996 BN_CTX
*new_ctx
= NULL
;
997 BIGNUM
*rh
, *tmp
, *Z4
, *Z6
;
1000 if (EC_POINT_is_at_infinity(group
, point
))
1003 field_mul
= group
->meth
->field_mul
;
1004 field_sqr
= group
->meth
->field_sqr
;
1008 ctx
= new_ctx
= BN_CTX_new();
1014 rh
= BN_CTX_get(ctx
);
1015 tmp
= BN_CTX_get(ctx
);
1016 Z4
= BN_CTX_get(ctx
);
1017 Z6
= BN_CTX_get(ctx
);
1022 * We have a curve defined by a Weierstrass equation
1023 * y^2 = x^3 + a*x + b.
1024 * The point to consider is given in Jacobian projective coordinates
1025 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1026 * Substituting this and multiplying by Z^6 transforms the above equation into
1027 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1028 * To test this, we add up the right-hand side in 'rh'.
1032 if (!field_sqr(group
, rh
, point
->X
, ctx
))
1035 if (!point
->Z_is_one
) {
1036 if (!field_sqr(group
, tmp
, point
->Z
, ctx
))
1038 if (!field_sqr(group
, Z4
, tmp
, ctx
))
1040 if (!field_mul(group
, Z6
, Z4
, tmp
, ctx
))
1043 /* rh := (rh + a*Z^4)*X */
1044 if (group
->a_is_minus3
) {
1045 if (!BN_mod_lshift1_quick(tmp
, Z4
, p
))
1047 if (!BN_mod_add_quick(tmp
, tmp
, Z4
, p
))
1049 if (!BN_mod_sub_quick(rh
, rh
, tmp
, p
))
1051 if (!field_mul(group
, rh
, rh
, point
->X
, ctx
))
1054 if (!field_mul(group
, tmp
, Z4
, group
->a
, ctx
))
1056 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1058 if (!field_mul(group
, rh
, rh
, point
->X
, ctx
))
1062 /* rh := rh + b*Z^6 */
1063 if (!field_mul(group
, tmp
, group
->b
, Z6
, ctx
))
1065 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1068 /* point->Z_is_one */
1070 /* rh := (rh + a)*X */
1071 if (!BN_mod_add_quick(rh
, rh
, group
->a
, p
))
1073 if (!field_mul(group
, rh
, rh
, point
->X
, ctx
))
1076 if (!BN_mod_add_quick(rh
, rh
, group
->b
, p
))
1081 if (!field_sqr(group
, tmp
, point
->Y
, ctx
))
1084 ret
= (0 == BN_ucmp(tmp
, rh
));
1088 BN_CTX_free(new_ctx
);
1092 int ec_GFp_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
,
1093 const EC_POINT
*b
, BN_CTX
*ctx
)
1098 * 0 equal (in affine coordinates)
1102 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
1103 const BIGNUM
*, BN_CTX
*);
1104 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1105 BN_CTX
*new_ctx
= NULL
;
1106 BIGNUM
*tmp1
, *tmp2
, *Za23
, *Zb23
;
1107 const BIGNUM
*tmp1_
, *tmp2_
;
1110 if (EC_POINT_is_at_infinity(group
, a
)) {
1111 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
1114 if (EC_POINT_is_at_infinity(group
, b
))
1117 if (a
->Z_is_one
&& b
->Z_is_one
) {
1118 return ((BN_cmp(a
->X
, b
->X
) == 0) && BN_cmp(a
->Y
, b
->Y
) == 0) ? 0 : 1;
1121 field_mul
= group
->meth
->field_mul
;
1122 field_sqr
= group
->meth
->field_sqr
;
1125 ctx
= new_ctx
= BN_CTX_new();
1131 tmp1
= BN_CTX_get(ctx
);
1132 tmp2
= BN_CTX_get(ctx
);
1133 Za23
= BN_CTX_get(ctx
);
1134 Zb23
= BN_CTX_get(ctx
);
1139 * We have to decide whether
1140 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1141 * or equivalently, whether
1142 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1146 if (!field_sqr(group
, Zb23
, b
->Z
, ctx
))
1148 if (!field_mul(group
, tmp1
, a
->X
, Zb23
, ctx
))
1154 if (!field_sqr(group
, Za23
, a
->Z
, ctx
))
1156 if (!field_mul(group
, tmp2
, b
->X
, Za23
, ctx
))
1162 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1163 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1164 ret
= 1; /* points differ */
1169 if (!field_mul(group
, Zb23
, Zb23
, b
->Z
, ctx
))
1171 if (!field_mul(group
, tmp1
, a
->Y
, Zb23
, ctx
))
1177 if (!field_mul(group
, Za23
, Za23
, a
->Z
, ctx
))
1179 if (!field_mul(group
, tmp2
, b
->Y
, Za23
, ctx
))
1185 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1186 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1187 ret
= 1; /* points differ */
1191 /* points are equal */
1196 BN_CTX_free(new_ctx
);
1200 int ec_GFp_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
,
1203 BN_CTX
*new_ctx
= NULL
;
1207 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
1211 ctx
= new_ctx
= BN_CTX_new();
1217 x
= BN_CTX_get(ctx
);
1218 y
= BN_CTX_get(ctx
);
1222 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1224 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1226 if (!point
->Z_is_one
) {
1227 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE
, ERR_R_INTERNAL_ERROR
);
1235 BN_CTX_free(new_ctx
);
1239 int ec_GFp_simple_points_make_affine(const EC_GROUP
*group
, size_t num
,
1240 EC_POINT
*points
[], BN_CTX
*ctx
)
1242 BN_CTX
*new_ctx
= NULL
;
1243 BIGNUM
*tmp
, *tmp_Z
;
1244 BIGNUM
**prod_Z
= NULL
;
1252 ctx
= new_ctx
= BN_CTX_new();
1258 tmp
= BN_CTX_get(ctx
);
1259 tmp_Z
= BN_CTX_get(ctx
);
1260 if (tmp
== NULL
|| tmp_Z
== NULL
)
1263 prod_Z
= OPENSSL_malloc(num
* sizeof prod_Z
[0]);
1266 for (i
= 0; i
< num
; i
++) {
1267 prod_Z
[i
] = BN_new();
1268 if (prod_Z
[i
] == NULL
)
1273 * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1274 * skipping any zero-valued inputs (pretend that they're 1).
1277 if (!BN_is_zero(points
[0]->Z
)) {
1278 if (!BN_copy(prod_Z
[0], points
[0]->Z
))
1281 if (group
->meth
->field_set_to_one
!= 0) {
1282 if (!group
->meth
->field_set_to_one(group
, prod_Z
[0], ctx
))
1285 if (!BN_one(prod_Z
[0]))
1290 for (i
= 1; i
< num
; i
++) {
1291 if (!BN_is_zero(points
[i
]->Z
)) {
1293 meth
->field_mul(group
, prod_Z
[i
], prod_Z
[i
- 1], points
[i
]->Z
,
1297 if (!BN_copy(prod_Z
[i
], prod_Z
[i
- 1]))
1303 * Now use a single explicit inversion to replace every non-zero
1304 * points[i]->Z by its inverse.
1307 if (!BN_mod_inverse(tmp
, prod_Z
[num
- 1], group
->field
, ctx
)) {
1308 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE
, ERR_R_BN_LIB
);
1311 if (group
->meth
->field_encode
!= 0) {
1313 * In the Montgomery case, we just turned R*H (representing H) into
1314 * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
1315 * multiply by the Montgomery factor twice.
1317 if (!group
->meth
->field_encode(group
, tmp
, tmp
, ctx
))
1319 if (!group
->meth
->field_encode(group
, tmp
, tmp
, ctx
))
1323 for (i
= num
- 1; i
> 0; --i
) {
1325 * Loop invariant: tmp is the product of the inverses of points[0]->Z
1326 * .. points[i]->Z (zero-valued inputs skipped).
1328 if (!BN_is_zero(points
[i
]->Z
)) {
1330 * Set tmp_Z to the inverse of points[i]->Z (as product of Z
1331 * inverses 0 .. i, Z values 0 .. i - 1).
1334 meth
->field_mul(group
, tmp_Z
, prod_Z
[i
- 1], tmp
, ctx
))
1337 * Update tmp to satisfy the loop invariant for i - 1.
1339 if (!group
->meth
->field_mul(group
, tmp
, tmp
, points
[i
]->Z
, ctx
))
1341 /* Replace points[i]->Z by its inverse. */
1342 if (!BN_copy(points
[i
]->Z
, tmp_Z
))
1347 if (!BN_is_zero(points
[0]->Z
)) {
1348 /* Replace points[0]->Z by its inverse. */
1349 if (!BN_copy(points
[0]->Z
, tmp
))
1353 /* Finally, fix up the X and Y coordinates for all points. */
1355 for (i
= 0; i
< num
; i
++) {
1356 EC_POINT
*p
= points
[i
];
1358 if (!BN_is_zero(p
->Z
)) {
1359 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1361 if (!group
->meth
->field_sqr(group
, tmp
, p
->Z
, ctx
))
1363 if (!group
->meth
->field_mul(group
, p
->X
, p
->X
, tmp
, ctx
))
1366 if (!group
->meth
->field_mul(group
, tmp
, tmp
, p
->Z
, ctx
))
1368 if (!group
->meth
->field_mul(group
, p
->Y
, p
->Y
, tmp
, ctx
))
1371 if (group
->meth
->field_set_to_one
!= 0) {
1372 if (!group
->meth
->field_set_to_one(group
, p
->Z
, ctx
))
1386 BN_CTX_free(new_ctx
);
1387 if (prod_Z
!= NULL
) {
1388 for (i
= 0; i
< num
; i
++) {
1389 if (prod_Z
[i
] == NULL
)
1391 BN_clear_free(prod_Z
[i
]);
1393 OPENSSL_free(prod_Z
);
1398 int ec_GFp_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
1399 const BIGNUM
*b
, BN_CTX
*ctx
)
1401 return BN_mod_mul(r
, a
, b
, group
->field
, ctx
);
1404 int ec_GFp_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
1407 return BN_mod_sqr(r
, a
, group
->field
, ctx
);