1 /* crypto/ec/ecp_smpl.c */
3 * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
4 * for the OpenSSL project. Includes code written by Bodo Moeller for the
7 /* ====================================================================
8 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * openssl-core@openssl.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
60 /* ====================================================================
61 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
62 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
63 * and contributed to the OpenSSL project.
66 #include <openssl/err.h>
67 #include <openssl/symhacks.h>
70 # include <openssl/fips.h>
75 const EC_METHOD
*EC_GFp_simple_method(void)
77 static const EC_METHOD ret
= {
79 NID_X9_62_prime_field
,
80 ec_GFp_simple_group_init
,
81 ec_GFp_simple_group_finish
,
82 ec_GFp_simple_group_clear_finish
,
83 ec_GFp_simple_group_copy
,
84 ec_GFp_simple_group_set_curve
,
85 ec_GFp_simple_group_get_curve
,
86 ec_GFp_simple_group_get_degree
,
87 ec_GFp_simple_group_check_discriminant
,
88 ec_GFp_simple_point_init
,
89 ec_GFp_simple_point_finish
,
90 ec_GFp_simple_point_clear_finish
,
91 ec_GFp_simple_point_copy
,
92 ec_GFp_simple_point_set_to_infinity
,
93 ec_GFp_simple_set_Jprojective_coordinates_GFp
,
94 ec_GFp_simple_get_Jprojective_coordinates_GFp
,
95 ec_GFp_simple_point_set_affine_coordinates
,
96 ec_GFp_simple_point_get_affine_coordinates
,
100 ec_GFp_simple_invert
,
101 ec_GFp_simple_is_at_infinity
,
102 ec_GFp_simple_is_on_curve
,
104 ec_GFp_simple_make_affine
,
105 ec_GFp_simple_points_make_affine
,
107 0 /* precompute_mult */ ,
108 0 /* have_precompute_mult */ ,
109 ec_GFp_simple_field_mul
,
110 ec_GFp_simple_field_sqr
,
112 0 /* field_encode */ ,
113 0 /* field_decode */ ,
114 0 /* field_set_to_one */
119 return fips_ec_gfp_simple_method();
126 * Most method functions in this file are designed to work with
127 * non-trivial representations of field elements if necessary
128 * (see ecp_mont.c): while standard modular addition and subtraction
129 * are used, the field_mul and field_sqr methods will be used for
130 * multiplication, and field_encode and field_decode (if defined)
131 * will be used for converting between representations.
133 * Functions ec_GFp_simple_points_make_affine() and
134 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
135 * that if a non-trivial representation is used, it is a Montgomery
136 * representation (i.e. 'encoding' means multiplying by some factor R).
139 int ec_GFp_simple_group_init(EC_GROUP
*group
)
141 BN_init(&group
->field
);
144 group
->a_is_minus3
= 0;
148 void ec_GFp_simple_group_finish(EC_GROUP
*group
)
150 BN_free(&group
->field
);
155 void ec_GFp_simple_group_clear_finish(EC_GROUP
*group
)
157 BN_clear_free(&group
->field
);
158 BN_clear_free(&group
->a
);
159 BN_clear_free(&group
->b
);
162 int ec_GFp_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
164 if (!BN_copy(&dest
->field
, &src
->field
))
166 if (!BN_copy(&dest
->a
, &src
->a
))
168 if (!BN_copy(&dest
->b
, &src
->b
))
171 dest
->a_is_minus3
= src
->a_is_minus3
;
176 int ec_GFp_simple_group_set_curve(EC_GROUP
*group
,
177 const BIGNUM
*p
, const BIGNUM
*a
,
178 const BIGNUM
*b
, BN_CTX
*ctx
)
181 BN_CTX
*new_ctx
= NULL
;
184 /* p must be a prime > 3 */
185 if (BN_num_bits(p
) <= 2 || !BN_is_odd(p
)) {
186 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE
, EC_R_INVALID_FIELD
);
191 ctx
= new_ctx
= BN_CTX_new();
197 tmp_a
= BN_CTX_get(ctx
);
202 if (!BN_copy(&group
->field
, p
))
204 BN_set_negative(&group
->field
, 0);
207 if (!BN_nnmod(tmp_a
, a
, p
, ctx
))
209 if (group
->meth
->field_encode
) {
210 if (!group
->meth
->field_encode(group
, &group
->a
, tmp_a
, ctx
))
212 } else if (!BN_copy(&group
->a
, tmp_a
))
216 if (!BN_nnmod(&group
->b
, b
, p
, ctx
))
218 if (group
->meth
->field_encode
)
219 if (!group
->meth
->field_encode(group
, &group
->b
, &group
->b
, ctx
))
222 /* group->a_is_minus3 */
223 if (!BN_add_word(tmp_a
, 3))
225 group
->a_is_minus3
= (0 == BN_cmp(tmp_a
, &group
->field
));
232 BN_CTX_free(new_ctx
);
236 int ec_GFp_simple_group_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
237 BIGNUM
*b
, BN_CTX
*ctx
)
240 BN_CTX
*new_ctx
= NULL
;
243 if (!BN_copy(p
, &group
->field
))
247 if (a
!= NULL
|| b
!= NULL
) {
248 if (group
->meth
->field_decode
) {
250 ctx
= new_ctx
= BN_CTX_new();
255 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
))
259 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
))
264 if (!BN_copy(a
, &group
->a
))
268 if (!BN_copy(b
, &group
->b
))
278 BN_CTX_free(new_ctx
);
282 int ec_GFp_simple_group_get_degree(const EC_GROUP
*group
)
284 return BN_num_bits(&group
->field
);
287 int ec_GFp_simple_group_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
290 BIGNUM
*a
, *b
, *order
, *tmp_1
, *tmp_2
;
291 const BIGNUM
*p
= &group
->field
;
292 BN_CTX
*new_ctx
= NULL
;
295 ctx
= new_ctx
= BN_CTX_new();
297 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT
,
298 ERR_R_MALLOC_FAILURE
);
305 tmp_1
= BN_CTX_get(ctx
);
306 tmp_2
= BN_CTX_get(ctx
);
307 order
= BN_CTX_get(ctx
);
311 if (group
->meth
->field_decode
) {
312 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
))
314 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
))
317 if (!BN_copy(a
, &group
->a
))
319 if (!BN_copy(b
, &group
->b
))
324 * check the discriminant:
325 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
331 } else if (!BN_is_zero(b
)) {
332 if (!BN_mod_sqr(tmp_1
, a
, p
, ctx
))
334 if (!BN_mod_mul(tmp_2
, tmp_1
, a
, p
, ctx
))
336 if (!BN_lshift(tmp_1
, tmp_2
, 2))
340 if (!BN_mod_sqr(tmp_2
, b
, p
, ctx
))
342 if (!BN_mul_word(tmp_2
, 27))
346 if (!BN_mod_add(a
, tmp_1
, tmp_2
, p
, ctx
))
357 BN_CTX_free(new_ctx
);
361 int ec_GFp_simple_point_init(EC_POINT
*point
)
371 void ec_GFp_simple_point_finish(EC_POINT
*point
)
378 void ec_GFp_simple_point_clear_finish(EC_POINT
*point
)
380 BN_clear_free(&point
->X
);
381 BN_clear_free(&point
->Y
);
382 BN_clear_free(&point
->Z
);
386 int ec_GFp_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
388 if (!BN_copy(&dest
->X
, &src
->X
))
390 if (!BN_copy(&dest
->Y
, &src
->Y
))
392 if (!BN_copy(&dest
->Z
, &src
->Z
))
394 dest
->Z_is_one
= src
->Z_is_one
;
399 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP
*group
,
407 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
414 BN_CTX
*new_ctx
= NULL
;
418 ctx
= new_ctx
= BN_CTX_new();
424 if (!BN_nnmod(&point
->X
, x
, &group
->field
, ctx
))
426 if (group
->meth
->field_encode
) {
427 if (!group
->meth
->field_encode(group
, &point
->X
, &point
->X
, ctx
))
433 if (!BN_nnmod(&point
->Y
, y
, &group
->field
, ctx
))
435 if (group
->meth
->field_encode
) {
436 if (!group
->meth
->field_encode(group
, &point
->Y
, &point
->Y
, ctx
))
444 if (!BN_nnmod(&point
->Z
, z
, &group
->field
, ctx
))
446 Z_is_one
= BN_is_one(&point
->Z
);
447 if (group
->meth
->field_encode
) {
448 if (Z_is_one
&& (group
->meth
->field_set_to_one
!= 0)) {
449 if (!group
->meth
->field_set_to_one(group
, &point
->Z
, ctx
))
453 meth
->field_encode(group
, &point
->Z
, &point
->Z
, ctx
))
457 point
->Z_is_one
= Z_is_one
;
464 BN_CTX_free(new_ctx
);
468 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
469 const EC_POINT
*point
,
470 BIGNUM
*x
, BIGNUM
*y
,
471 BIGNUM
*z
, BN_CTX
*ctx
)
473 BN_CTX
*new_ctx
= NULL
;
476 if (group
->meth
->field_decode
!= 0) {
478 ctx
= new_ctx
= BN_CTX_new();
484 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
))
488 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
))
492 if (!group
->meth
->field_decode(group
, z
, &point
->Z
, ctx
))
497 if (!BN_copy(x
, &point
->X
))
501 if (!BN_copy(y
, &point
->Y
))
505 if (!BN_copy(z
, &point
->Z
))
514 BN_CTX_free(new_ctx
);
518 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP
*group
,
521 const BIGNUM
*y
, BN_CTX
*ctx
)
523 if (x
== NULL
|| y
== NULL
) {
525 * unlike for projective coordinates, we do not tolerate this
527 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES
,
528 ERR_R_PASSED_NULL_PARAMETER
);
532 return EC_POINT_set_Jprojective_coordinates_GFp(group
, point
, x
, y
,
533 BN_value_one(), ctx
);
536 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP
*group
,
537 const EC_POINT
*point
,
538 BIGNUM
*x
, BIGNUM
*y
,
541 BN_CTX
*new_ctx
= NULL
;
542 BIGNUM
*Z
, *Z_1
, *Z_2
, *Z_3
;
546 if (EC_POINT_is_at_infinity(group
, point
)) {
547 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
548 EC_R_POINT_AT_INFINITY
);
553 ctx
= new_ctx
= BN_CTX_new();
560 Z_1
= BN_CTX_get(ctx
);
561 Z_2
= BN_CTX_get(ctx
);
562 Z_3
= BN_CTX_get(ctx
);
566 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
568 if (group
->meth
->field_decode
) {
569 if (!group
->meth
->field_decode(group
, Z
, &point
->Z
, ctx
))
577 if (group
->meth
->field_decode
) {
579 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
))
583 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
))
588 if (!BN_copy(x
, &point
->X
))
592 if (!BN_copy(y
, &point
->Y
))
597 if (!BN_mod_inverse(Z_1
, Z_
, &group
->field
, ctx
)) {
598 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
,
603 if (group
->meth
->field_encode
== 0) {
604 /* field_sqr works on standard representation */
605 if (!group
->meth
->field_sqr(group
, Z_2
, Z_1
, ctx
))
608 if (!BN_mod_sqr(Z_2
, Z_1
, &group
->field
, ctx
))
614 * in the Montgomery case, field_mul will cancel out Montgomery
617 if (!group
->meth
->field_mul(group
, x
, &point
->X
, Z_2
, ctx
))
622 if (group
->meth
->field_encode
== 0) {
624 * field_mul works on standard representation
626 if (!group
->meth
->field_mul(group
, Z_3
, Z_2
, Z_1
, ctx
))
629 if (!BN_mod_mul(Z_3
, Z_2
, Z_1
, &group
->field
, ctx
))
634 * in the Montgomery case, field_mul will cancel out Montgomery
637 if (!group
->meth
->field_mul(group
, y
, &point
->Y
, Z_3
, ctx
))
647 BN_CTX_free(new_ctx
);
651 int ec_GFp_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
652 const EC_POINT
*b
, BN_CTX
*ctx
)
654 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
655 const BIGNUM
*, BN_CTX
*);
656 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
658 BN_CTX
*new_ctx
= NULL
;
659 BIGNUM
*n0
, *n1
, *n2
, *n3
, *n4
, *n5
, *n6
;
663 return EC_POINT_dbl(group
, r
, a
, ctx
);
664 if (EC_POINT_is_at_infinity(group
, a
))
665 return EC_POINT_copy(r
, b
);
666 if (EC_POINT_is_at_infinity(group
, b
))
667 return EC_POINT_copy(r
, a
);
669 field_mul
= group
->meth
->field_mul
;
670 field_sqr
= group
->meth
->field_sqr
;
674 ctx
= new_ctx
= BN_CTX_new();
680 n0
= BN_CTX_get(ctx
);
681 n1
= BN_CTX_get(ctx
);
682 n2
= BN_CTX_get(ctx
);
683 n3
= BN_CTX_get(ctx
);
684 n4
= BN_CTX_get(ctx
);
685 n5
= BN_CTX_get(ctx
);
686 n6
= BN_CTX_get(ctx
);
691 * Note that in this function we must not read components of 'a' or 'b'
692 * once we have written the corresponding components of 'r'. ('r' might
693 * be one of 'a' or 'b'.)
698 if (!BN_copy(n1
, &a
->X
))
700 if (!BN_copy(n2
, &a
->Y
))
705 if (!field_sqr(group
, n0
, &b
->Z
, ctx
))
707 if (!field_mul(group
, n1
, &a
->X
, n0
, ctx
))
709 /* n1 = X_a * Z_b^2 */
711 if (!field_mul(group
, n0
, n0
, &b
->Z
, ctx
))
713 if (!field_mul(group
, n2
, &a
->Y
, n0
, ctx
))
715 /* n2 = Y_a * Z_b^3 */
720 if (!BN_copy(n3
, &b
->X
))
722 if (!BN_copy(n4
, &b
->Y
))
727 if (!field_sqr(group
, n0
, &a
->Z
, ctx
))
729 if (!field_mul(group
, n3
, &b
->X
, n0
, ctx
))
731 /* n3 = X_b * Z_a^2 */
733 if (!field_mul(group
, n0
, n0
, &a
->Z
, ctx
))
735 if (!field_mul(group
, n4
, &b
->Y
, n0
, ctx
))
737 /* n4 = Y_b * Z_a^3 */
741 if (!BN_mod_sub_quick(n5
, n1
, n3
, p
))
743 if (!BN_mod_sub_quick(n6
, n2
, n4
, p
))
748 if (BN_is_zero(n5
)) {
749 if (BN_is_zero(n6
)) {
750 /* a is the same point as b */
752 ret
= EC_POINT_dbl(group
, r
, a
, ctx
);
756 /* a is the inverse of b */
765 if (!BN_mod_add_quick(n1
, n1
, n3
, p
))
767 if (!BN_mod_add_quick(n2
, n2
, n4
, p
))
773 if (a
->Z_is_one
&& b
->Z_is_one
) {
774 if (!BN_copy(&r
->Z
, n5
))
778 if (!BN_copy(n0
, &b
->Z
))
780 } else if (b
->Z_is_one
) {
781 if (!BN_copy(n0
, &a
->Z
))
784 if (!field_mul(group
, n0
, &a
->Z
, &b
->Z
, ctx
))
787 if (!field_mul(group
, &r
->Z
, n0
, n5
, ctx
))
791 /* Z_r = Z_a * Z_b * n5 */
794 if (!field_sqr(group
, n0
, n6
, ctx
))
796 if (!field_sqr(group
, n4
, n5
, ctx
))
798 if (!field_mul(group
, n3
, n1
, n4
, ctx
))
800 if (!BN_mod_sub_quick(&r
->X
, n0
, n3
, p
))
802 /* X_r = n6^2 - n5^2 * 'n7' */
805 if (!BN_mod_lshift1_quick(n0
, &r
->X
, p
))
807 if (!BN_mod_sub_quick(n0
, n3
, n0
, p
))
809 /* n9 = n5^2 * 'n7' - 2 * X_r */
812 if (!field_mul(group
, n0
, n0
, n6
, ctx
))
814 if (!field_mul(group
, n5
, n4
, n5
, ctx
))
815 goto end
; /* now n5 is n5^3 */
816 if (!field_mul(group
, n1
, n2
, n5
, ctx
))
818 if (!BN_mod_sub_quick(n0
, n0
, n1
, p
))
821 if (!BN_add(n0
, n0
, p
))
823 /* now 0 <= n0 < 2*p, and n0 is even */
824 if (!BN_rshift1(&r
->Y
, n0
))
826 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
831 if (ctx
) /* otherwise we already called BN_CTX_end */
834 BN_CTX_free(new_ctx
);
838 int ec_GFp_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
841 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
842 const BIGNUM
*, BN_CTX
*);
843 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
845 BN_CTX
*new_ctx
= NULL
;
846 BIGNUM
*n0
, *n1
, *n2
, *n3
;
849 if (EC_POINT_is_at_infinity(group
, a
)) {
855 field_mul
= group
->meth
->field_mul
;
856 field_sqr
= group
->meth
->field_sqr
;
860 ctx
= new_ctx
= BN_CTX_new();
866 n0
= BN_CTX_get(ctx
);
867 n1
= BN_CTX_get(ctx
);
868 n2
= BN_CTX_get(ctx
);
869 n3
= BN_CTX_get(ctx
);
874 * Note that in this function we must not read components of 'a' once we
875 * have written the corresponding components of 'r'. ('r' might the same
881 if (!field_sqr(group
, n0
, &a
->X
, ctx
))
883 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
885 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
887 if (!BN_mod_add_quick(n1
, n0
, &group
->a
, p
))
889 /* n1 = 3 * X_a^2 + a_curve */
890 } else if (group
->a_is_minus3
) {
891 if (!field_sqr(group
, n1
, &a
->Z
, ctx
))
893 if (!BN_mod_add_quick(n0
, &a
->X
, n1
, p
))
895 if (!BN_mod_sub_quick(n2
, &a
->X
, n1
, p
))
897 if (!field_mul(group
, n1
, n0
, n2
, ctx
))
899 if (!BN_mod_lshift1_quick(n0
, n1
, p
))
901 if (!BN_mod_add_quick(n1
, n0
, n1
, p
))
904 * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
905 * = 3 * X_a^2 - 3 * Z_a^4
908 if (!field_sqr(group
, n0
, &a
->X
, ctx
))
910 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
912 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
914 if (!field_sqr(group
, n1
, &a
->Z
, ctx
))
916 if (!field_sqr(group
, n1
, n1
, ctx
))
918 if (!field_mul(group
, n1
, n1
, &group
->a
, ctx
))
920 if (!BN_mod_add_quick(n1
, n1
, n0
, p
))
922 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
927 if (!BN_copy(n0
, &a
->Y
))
930 if (!field_mul(group
, n0
, &a
->Y
, &a
->Z
, ctx
))
933 if (!BN_mod_lshift1_quick(&r
->Z
, n0
, p
))
936 /* Z_r = 2 * Y_a * Z_a */
939 if (!field_sqr(group
, n3
, &a
->Y
, ctx
))
941 if (!field_mul(group
, n2
, &a
->X
, n3
, ctx
))
943 if (!BN_mod_lshift_quick(n2
, n2
, 2, p
))
945 /* n2 = 4 * X_a * Y_a^2 */
948 if (!BN_mod_lshift1_quick(n0
, n2
, p
))
950 if (!field_sqr(group
, &r
->X
, n1
, ctx
))
952 if (!BN_mod_sub_quick(&r
->X
, &r
->X
, n0
, p
))
954 /* X_r = n1^2 - 2 * n2 */
957 if (!field_sqr(group
, n0
, n3
, ctx
))
959 if (!BN_mod_lshift_quick(n3
, n0
, 3, p
))
964 if (!BN_mod_sub_quick(n0
, n2
, &r
->X
, p
))
966 if (!field_mul(group
, n0
, n1
, n0
, ctx
))
968 if (!BN_mod_sub_quick(&r
->Y
, n0
, n3
, p
))
970 /* Y_r = n1 * (n2 - X_r) - n3 */
977 BN_CTX_free(new_ctx
);
981 int ec_GFp_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
983 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(&point
->Y
))
984 /* point is its own inverse */
987 return BN_usub(&point
->Y
, &group
->field
, &point
->Y
);
990 int ec_GFp_simple_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
992 return BN_is_zero(&point
->Z
);
995 int ec_GFp_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
998 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
999 const BIGNUM
*, BN_CTX
*);
1000 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1002 BN_CTX
*new_ctx
= NULL
;
1003 BIGNUM
*rh
, *tmp
, *Z4
, *Z6
;
1006 if (EC_POINT_is_at_infinity(group
, point
))
1009 field_mul
= group
->meth
->field_mul
;
1010 field_sqr
= group
->meth
->field_sqr
;
1014 ctx
= new_ctx
= BN_CTX_new();
1020 rh
= BN_CTX_get(ctx
);
1021 tmp
= BN_CTX_get(ctx
);
1022 Z4
= BN_CTX_get(ctx
);
1023 Z6
= BN_CTX_get(ctx
);
1028 * We have a curve defined by a Weierstrass equation
1029 * y^2 = x^3 + a*x + b.
1030 * The point to consider is given in Jacobian projective coordinates
1031 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1032 * Substituting this and multiplying by Z^6 transforms the above equation into
1033 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1034 * To test this, we add up the right-hand side in 'rh'.
1038 if (!field_sqr(group
, rh
, &point
->X
, ctx
))
1041 if (!point
->Z_is_one
) {
1042 if (!field_sqr(group
, tmp
, &point
->Z
, ctx
))
1044 if (!field_sqr(group
, Z4
, tmp
, ctx
))
1046 if (!field_mul(group
, Z6
, Z4
, tmp
, ctx
))
1049 /* rh := (rh + a*Z^4)*X */
1050 if (group
->a_is_minus3
) {
1051 if (!BN_mod_lshift1_quick(tmp
, Z4
, p
))
1053 if (!BN_mod_add_quick(tmp
, tmp
, Z4
, p
))
1055 if (!BN_mod_sub_quick(rh
, rh
, tmp
, p
))
1057 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1060 if (!field_mul(group
, tmp
, Z4
, &group
->a
, ctx
))
1062 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1064 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1068 /* rh := rh + b*Z^6 */
1069 if (!field_mul(group
, tmp
, &group
->b
, Z6
, ctx
))
1071 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1074 /* point->Z_is_one */
1076 /* rh := (rh + a)*X */
1077 if (!BN_mod_add_quick(rh
, rh
, &group
->a
, p
))
1079 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1082 if (!BN_mod_add_quick(rh
, rh
, &group
->b
, p
))
1087 if (!field_sqr(group
, tmp
, &point
->Y
, ctx
))
1090 ret
= (0 == BN_ucmp(tmp
, rh
));
1094 if (new_ctx
!= NULL
)
1095 BN_CTX_free(new_ctx
);
1099 int ec_GFp_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
,
1100 const EC_POINT
*b
, BN_CTX
*ctx
)
1105 * 0 equal (in affine coordinates)
1109 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*,
1110 const BIGNUM
*, BN_CTX
*);
1111 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1112 BN_CTX
*new_ctx
= NULL
;
1113 BIGNUM
*tmp1
, *tmp2
, *Za23
, *Zb23
;
1114 const BIGNUM
*tmp1_
, *tmp2_
;
1117 if (EC_POINT_is_at_infinity(group
, a
)) {
1118 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
1121 if (EC_POINT_is_at_infinity(group
, b
))
1124 if (a
->Z_is_one
&& b
->Z_is_one
) {
1125 return ((BN_cmp(&a
->X
, &b
->X
) == 0)
1126 && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
1129 field_mul
= group
->meth
->field_mul
;
1130 field_sqr
= group
->meth
->field_sqr
;
1133 ctx
= new_ctx
= BN_CTX_new();
1139 tmp1
= BN_CTX_get(ctx
);
1140 tmp2
= BN_CTX_get(ctx
);
1141 Za23
= BN_CTX_get(ctx
);
1142 Zb23
= BN_CTX_get(ctx
);
1147 * We have to decide whether
1148 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1149 * or equivalently, whether
1150 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1154 if (!field_sqr(group
, Zb23
, &b
->Z
, ctx
))
1156 if (!field_mul(group
, tmp1
, &a
->X
, Zb23
, ctx
))
1162 if (!field_sqr(group
, Za23
, &a
->Z
, ctx
))
1164 if (!field_mul(group
, tmp2
, &b
->X
, Za23
, ctx
))
1170 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1171 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1172 ret
= 1; /* points differ */
1177 if (!field_mul(group
, Zb23
, Zb23
, &b
->Z
, ctx
))
1179 if (!field_mul(group
, tmp1
, &a
->Y
, Zb23
, ctx
))
1185 if (!field_mul(group
, Za23
, Za23
, &a
->Z
, ctx
))
1187 if (!field_mul(group
, tmp2
, &b
->Y
, Za23
, ctx
))
1193 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1194 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1195 ret
= 1; /* points differ */
1199 /* points are equal */
1204 if (new_ctx
!= NULL
)
1205 BN_CTX_free(new_ctx
);
1209 int ec_GFp_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
,
1212 BN_CTX
*new_ctx
= NULL
;
1216 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
1220 ctx
= new_ctx
= BN_CTX_new();
1226 x
= BN_CTX_get(ctx
);
1227 y
= BN_CTX_get(ctx
);
1231 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1233 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1235 if (!point
->Z_is_one
) {
1236 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE
, ERR_R_INTERNAL_ERROR
);
1244 if (new_ctx
!= NULL
)
1245 BN_CTX_free(new_ctx
);
1249 int ec_GFp_simple_points_make_affine(const EC_GROUP
*group
, size_t num
,
1250 EC_POINT
*points
[], BN_CTX
*ctx
)
1252 BN_CTX
*new_ctx
= NULL
;
1253 BIGNUM
*tmp
, *tmp_Z
;
1254 BIGNUM
**prod_Z
= NULL
;
1262 ctx
= new_ctx
= BN_CTX_new();
1268 tmp
= BN_CTX_get(ctx
);
1269 tmp_Z
= BN_CTX_get(ctx
);
1270 if (tmp
== NULL
|| tmp_Z
== NULL
)
1273 prod_Z
= OPENSSL_malloc(num
* sizeof prod_Z
[0]);
1276 for (i
= 0; i
< num
; i
++) {
1277 prod_Z
[i
] = BN_new();
1278 if (prod_Z
[i
] == NULL
)
1283 * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1284 * skipping any zero-valued inputs (pretend that they're 1).
1287 if (!BN_is_zero(&points
[0]->Z
)) {
1288 if (!BN_copy(prod_Z
[0], &points
[0]->Z
))
1291 if (group
->meth
->field_set_to_one
!= 0) {
1292 if (!group
->meth
->field_set_to_one(group
, prod_Z
[0], ctx
))
1295 if (!BN_one(prod_Z
[0]))
1300 for (i
= 1; i
< num
; i
++) {
1301 if (!BN_is_zero(&points
[i
]->Z
)) {
1302 if (!group
->meth
->field_mul(group
, prod_Z
[i
], prod_Z
[i
- 1],
1303 &points
[i
]->Z
, ctx
))
1306 if (!BN_copy(prod_Z
[i
], prod_Z
[i
- 1]))
1312 * Now use a single explicit inversion to replace every non-zero
1313 * points[i]->Z by its inverse.
1316 if (!BN_mod_inverse(tmp
, prod_Z
[num
- 1], &group
->field
, ctx
)) {
1317 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE
, ERR_R_BN_LIB
);
1320 if (group
->meth
->field_encode
!= 0) {
1322 * In the Montgomery case, we just turned R*H (representing H) into
1323 * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
1324 * multiply by the Montgomery factor twice.
1326 if (!group
->meth
->field_encode(group
, tmp
, tmp
, ctx
))
1328 if (!group
->meth
->field_encode(group
, tmp
, tmp
, ctx
))
1332 for (i
= num
- 1; i
> 0; --i
) {
1334 * Loop invariant: tmp is the product of the inverses of points[0]->Z
1335 * .. points[i]->Z (zero-valued inputs skipped).
1337 if (!BN_is_zero(&points
[i
]->Z
)) {
1339 * Set tmp_Z to the inverse of points[i]->Z (as product of Z
1340 * inverses 0 .. i, Z values 0 .. i - 1).
1343 meth
->field_mul(group
, tmp_Z
, prod_Z
[i
- 1], tmp
, ctx
))
1346 * Update tmp to satisfy the loop invariant for i - 1.
1348 if (!group
->meth
->field_mul(group
, tmp
, tmp
, &points
[i
]->Z
, ctx
))
1350 /* Replace points[i]->Z by its inverse. */
1351 if (!BN_copy(&points
[i
]->Z
, tmp_Z
))
1356 if (!BN_is_zero(&points
[0]->Z
)) {
1357 /* Replace points[0]->Z by its inverse. */
1358 if (!BN_copy(&points
[0]->Z
, tmp
))
1362 /* Finally, fix up the X and Y coordinates for all points. */
1364 for (i
= 0; i
< num
; i
++) {
1365 EC_POINT
*p
= points
[i
];
1367 if (!BN_is_zero(&p
->Z
)) {
1368 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1370 if (!group
->meth
->field_sqr(group
, tmp
, &p
->Z
, ctx
))
1372 if (!group
->meth
->field_mul(group
, &p
->X
, &p
->X
, tmp
, ctx
))
1375 if (!group
->meth
->field_mul(group
, tmp
, tmp
, &p
->Z
, ctx
))
1377 if (!group
->meth
->field_mul(group
, &p
->Y
, &p
->Y
, tmp
, ctx
))
1380 if (group
->meth
->field_set_to_one
!= 0) {
1381 if (!group
->meth
->field_set_to_one(group
, &p
->Z
, ctx
))
1395 if (new_ctx
!= NULL
)
1396 BN_CTX_free(new_ctx
);
1397 if (prod_Z
!= NULL
) {
1398 for (i
= 0; i
< num
; i
++) {
1399 if (prod_Z
[i
] == NULL
)
1401 BN_clear_free(prod_Z
[i
]);
1403 OPENSSL_free(prod_Z
);
1408 int ec_GFp_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
1409 const BIGNUM
*b
, BN_CTX
*ctx
)
1411 return BN_mod_mul(r
, a
, b
, &group
->field
, ctx
);
1414 int ec_GFp_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
1417 return BN_mod_sqr(r
, a
, &group
->field
, ctx
);