1 /* $OpenBSD: ecp_smpl.c,v 1.17 2017/01/29 17:49:23 beck Exp $ */
2 /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project.
4 * Includes code written by Bodo Moeller for the OpenSSL project.
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>
71 EC_GFp_simple_method(void)
73 static const EC_METHOD ret
= {
74 .flags
= EC_FLAGS_DEFAULT_OCT
,
75 .field_type
= NID_X9_62_prime_field
,
76 .group_init
= ec_GFp_simple_group_init
,
77 .group_finish
= ec_GFp_simple_group_finish
,
78 .group_clear_finish
= ec_GFp_simple_group_clear_finish
,
79 .group_copy
= ec_GFp_simple_group_copy
,
80 .group_set_curve
= ec_GFp_simple_group_set_curve
,
81 .group_get_curve
= ec_GFp_simple_group_get_curve
,
82 .group_get_degree
= ec_GFp_simple_group_get_degree
,
83 .group_check_discriminant
=
84 ec_GFp_simple_group_check_discriminant
,
85 .point_init
= ec_GFp_simple_point_init
,
86 .point_finish
= ec_GFp_simple_point_finish
,
87 .point_clear_finish
= ec_GFp_simple_point_clear_finish
,
88 .point_copy
= ec_GFp_simple_point_copy
,
89 .point_set_to_infinity
= ec_GFp_simple_point_set_to_infinity
,
90 .point_set_Jprojective_coordinates_GFp
=
91 ec_GFp_simple_set_Jprojective_coordinates_GFp
,
92 .point_get_Jprojective_coordinates_GFp
=
93 ec_GFp_simple_get_Jprojective_coordinates_GFp
,
94 .point_set_affine_coordinates
=
95 ec_GFp_simple_point_set_affine_coordinates
,
96 .point_get_affine_coordinates
=
97 ec_GFp_simple_point_get_affine_coordinates
,
98 .add
= ec_GFp_simple_add
,
99 .dbl
= ec_GFp_simple_dbl
,
100 .invert
= ec_GFp_simple_invert
,
101 .is_at_infinity
= ec_GFp_simple_is_at_infinity
,
102 .is_on_curve
= ec_GFp_simple_is_on_curve
,
103 .point_cmp
= ec_GFp_simple_cmp
,
104 .make_affine
= ec_GFp_simple_make_affine
,
105 .points_make_affine
= ec_GFp_simple_points_make_affine
,
106 .field_mul
= ec_GFp_simple_field_mul
,
107 .field_sqr
= ec_GFp_simple_field_sqr
114 /* Most method functions in this file are designed to work with
115 * non-trivial representations of field elements if necessary
116 * (see ecp_mont.c): while standard modular addition and subtraction
117 * are used, the field_mul and field_sqr methods will be used for
118 * multiplication, and field_encode and field_decode (if defined)
119 * will be used for converting between representations.
121 * Functions ec_GFp_simple_points_make_affine() and
122 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
123 * that if a non-trivial representation is used, it is a Montgomery
124 * representation (i.e. 'encoding' means multiplying by some factor R).
129 ec_GFp_simple_group_init(EC_GROUP
* group
)
131 BN_init(&group
->field
);
134 group
->a_is_minus3
= 0;
140 ec_GFp_simple_group_finish(EC_GROUP
* group
)
142 BN_free(&group
->field
);
149 ec_GFp_simple_group_clear_finish(EC_GROUP
* group
)
151 BN_clear_free(&group
->field
);
152 BN_clear_free(&group
->a
);
153 BN_clear_free(&group
->b
);
158 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
;
174 ec_GFp_simple_group_set_curve(EC_GROUP
* group
,
175 const BIGNUM
* p
, const BIGNUM
* a
, const BIGNUM
* b
, BN_CTX
* ctx
)
178 BN_CTX
*new_ctx
= NULL
;
181 /* p must be a prime > 3 */
182 if (BN_num_bits(p
) <= 2 || !BN_is_odd(p
)) {
183 ECerror(EC_R_INVALID_FIELD
);
187 ctx
= new_ctx
= BN_CTX_new();
192 if ((tmp_a
= BN_CTX_get(ctx
)) == NULL
)
196 if (!BN_copy(&group
->field
, p
))
198 BN_set_negative(&group
->field
, 0);
201 if (!BN_nnmod(tmp_a
, a
, p
, ctx
))
203 if (group
->meth
->field_encode
) {
204 if (!group
->meth
->field_encode(group
, &group
->a
, tmp_a
, ctx
))
206 } else if (!BN_copy(&group
->a
, tmp_a
))
210 if (!BN_nnmod(&group
->b
, b
, p
, ctx
))
212 if (group
->meth
->field_encode
)
213 if (!group
->meth
->field_encode(group
, &group
->b
, &group
->b
, ctx
))
216 /* group->a_is_minus3 */
217 if (!BN_add_word(tmp_a
, 3))
219 group
->a_is_minus3
= (0 == BN_cmp(tmp_a
, &group
->field
));
225 BN_CTX_free(new_ctx
);
231 ec_GFp_simple_group_get_curve(const EC_GROUP
* group
, BIGNUM
* p
, BIGNUM
* a
, BIGNUM
* b
, BN_CTX
* ctx
)
234 BN_CTX
*new_ctx
= NULL
;
237 if (!BN_copy(p
, &group
->field
))
240 if (a
!= NULL
|| b
!= NULL
) {
241 if (group
->meth
->field_decode
) {
243 ctx
= new_ctx
= BN_CTX_new();
248 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
))
252 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
))
257 if (!BN_copy(a
, &group
->a
))
261 if (!BN_copy(b
, &group
->b
))
269 BN_CTX_free(new_ctx
);
275 ec_GFp_simple_group_get_degree(const EC_GROUP
* group
)
277 return BN_num_bits(&group
->field
);
282 ec_GFp_simple_group_check_discriminant(const EC_GROUP
* group
, BN_CTX
* ctx
)
285 BIGNUM
*a
, *b
, *order
, *tmp_1
, *tmp_2
;
286 const BIGNUM
*p
= &group
->field
;
287 BN_CTX
*new_ctx
= NULL
;
290 ctx
= new_ctx
= BN_CTX_new();
292 ECerror(ERR_R_MALLOC_FAILURE
);
297 if ((a
= BN_CTX_get(ctx
)) == NULL
)
299 if ((b
= BN_CTX_get(ctx
)) == NULL
)
301 if ((tmp_1
= BN_CTX_get(ctx
)) == NULL
)
303 if ((tmp_2
= BN_CTX_get(ctx
)) == NULL
)
305 if ((order
= BN_CTX_get(ctx
)) == NULL
)
308 if (group
->meth
->field_decode
) {
309 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
))
311 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
))
314 if (!BN_copy(a
, &group
->a
))
316 if (!BN_copy(b
, &group
->b
))
321 * check the discriminant: y^2 = x^3 + a*x + b is an elliptic curve
322 * <=> 4*a^3 + 27*b^2 != 0 (mod p) 0 =< a, b < p
327 } else if (!BN_is_zero(b
)) {
328 if (!BN_mod_sqr(tmp_1
, a
, p
, ctx
))
330 if (!BN_mod_mul(tmp_2
, tmp_1
, a
, p
, ctx
))
332 if (!BN_lshift(tmp_1
, tmp_2
, 2))
336 if (!BN_mod_sqr(tmp_2
, b
, p
, ctx
))
338 if (!BN_mul_word(tmp_2
, 27))
342 if (!BN_mod_add(a
, tmp_1
, tmp_2
, p
, ctx
))
352 BN_CTX_free(new_ctx
);
358 ec_GFp_simple_point_init(EC_POINT
* point
)
370 ec_GFp_simple_point_finish(EC_POINT
* point
)
379 ec_GFp_simple_point_clear_finish(EC_POINT
* point
)
381 BN_clear_free(&point
->X
);
382 BN_clear_free(&point
->Y
);
383 BN_clear_free(&point
->Z
);
389 ec_GFp_simple_point_copy(EC_POINT
* dest
, const EC_POINT
* src
)
391 if (!BN_copy(&dest
->X
, &src
->X
))
393 if (!BN_copy(&dest
->Y
, &src
->Y
))
395 if (!BN_copy(&dest
->Z
, &src
->Z
))
397 dest
->Z_is_one
= src
->Z_is_one
;
404 ec_GFp_simple_point_set_to_infinity(const EC_GROUP
* group
, EC_POINT
* point
)
413 ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP
* group
, EC_POINT
* point
,
414 const BIGNUM
* x
, const BIGNUM
* y
, const BIGNUM
* z
, BN_CTX
* ctx
)
416 BN_CTX
*new_ctx
= NULL
;
420 ctx
= new_ctx
= BN_CTX_new();
425 if (!BN_nnmod(&point
->X
, x
, &group
->field
, ctx
))
427 if (group
->meth
->field_encode
) {
428 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
))
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
))
451 if (!group
->meth
->field_encode(group
, &point
->Z
, &point
->Z
, ctx
))
455 point
->Z_is_one
= Z_is_one
;
460 BN_CTX_free(new_ctx
);
466 ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP
* group
, const EC_POINT
* point
,
467 BIGNUM
* x
, BIGNUM
* y
, BIGNUM
* z
, BN_CTX
* ctx
)
469 BN_CTX
*new_ctx
= NULL
;
472 if (group
->meth
->field_decode
!= 0) {
474 ctx
= new_ctx
= BN_CTX_new();
479 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
))
483 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
))
487 if (!group
->meth
->field_decode(group
, z
, &point
->Z
, ctx
))
492 if (!BN_copy(x
, &point
->X
))
496 if (!BN_copy(y
, &point
->Y
))
500 if (!BN_copy(z
, &point
->Z
))
508 BN_CTX_free(new_ctx
);
514 ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP
* group
, EC_POINT
* point
,
515 const BIGNUM
* x
, const BIGNUM
* y
, BN_CTX
* ctx
)
517 if (x
== NULL
|| y
== NULL
) {
518 /* unlike for projective coordinates, we do not tolerate this */
519 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
522 return EC_POINT_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, BN_value_one(), ctx
);
527 ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP
* group
, const EC_POINT
* point
,
528 BIGNUM
* x
, BIGNUM
* y
, BN_CTX
* ctx
)
530 BN_CTX
*new_ctx
= NULL
;
531 BIGNUM
*Z
, *Z_1
, *Z_2
, *Z_3
;
535 if (EC_POINT_is_at_infinity(group
, point
) > 0) {
536 ECerror(EC_R_POINT_AT_INFINITY
);
540 ctx
= new_ctx
= BN_CTX_new();
545 if ((Z
= BN_CTX_get(ctx
)) == NULL
)
547 if ((Z_1
= BN_CTX_get(ctx
)) == NULL
)
549 if ((Z_2
= BN_CTX_get(ctx
)) == NULL
)
551 if ((Z_3
= BN_CTX_get(ctx
)) == NULL
)
554 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
556 if (group
->meth
->field_decode
) {
557 if (!group
->meth
->field_decode(group
, Z
, &point
->Z
, ctx
))
565 if (group
->meth
->field_decode
) {
567 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
))
571 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
))
576 if (!BN_copy(x
, &point
->X
))
580 if (!BN_copy(y
, &point
->Y
))
585 if (!BN_mod_inverse_ct(Z_1
, Z_
, &group
->field
, ctx
)) {
586 ECerror(ERR_R_BN_LIB
);
589 if (group
->meth
->field_encode
== 0) {
590 /* field_sqr works on standard representation */
591 if (!group
->meth
->field_sqr(group
, Z_2
, Z_1
, ctx
))
594 if (!BN_mod_sqr(Z_2
, Z_1
, &group
->field
, ctx
))
600 * in the Montgomery case, field_mul will cancel out
601 * Montgomery factor in X:
603 if (!group
->meth
->field_mul(group
, x
, &point
->X
, Z_2
, ctx
))
607 if (group
->meth
->field_encode
== 0) {
608 /* field_mul works on standard representation */
609 if (!group
->meth
->field_mul(group
, Z_3
, Z_2
, Z_1
, ctx
))
612 if (!BN_mod_mul(Z_3
, Z_2
, Z_1
, &group
->field
, ctx
))
617 * in the Montgomery case, field_mul will cancel out
618 * Montgomery factor in Y:
620 if (!group
->meth
->field_mul(group
, y
, &point
->Y
, Z_3
, ctx
))
629 BN_CTX_free(new_ctx
);
634 ec_GFp_simple_add(const EC_GROUP
* group
, EC_POINT
* r
, const EC_POINT
* a
, const EC_POINT
* b
, BN_CTX
* ctx
)
636 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
637 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
639 BN_CTX
*new_ctx
= NULL
;
640 BIGNUM
*n0
, *n1
, *n2
, *n3
, *n4
, *n5
, *n6
;
644 return EC_POINT_dbl(group
, r
, a
, ctx
);
645 if (EC_POINT_is_at_infinity(group
, a
) > 0)
646 return EC_POINT_copy(r
, b
);
647 if (EC_POINT_is_at_infinity(group
, b
) > 0)
648 return EC_POINT_copy(r
, a
);
650 field_mul
= group
->meth
->field_mul
;
651 field_sqr
= group
->meth
->field_sqr
;
655 ctx
= new_ctx
= BN_CTX_new();
660 if ((n0
= BN_CTX_get(ctx
)) == NULL
)
662 if ((n1
= BN_CTX_get(ctx
)) == NULL
)
664 if ((n2
= BN_CTX_get(ctx
)) == NULL
)
666 if ((n3
= BN_CTX_get(ctx
)) == NULL
)
668 if ((n4
= BN_CTX_get(ctx
)) == NULL
)
670 if ((n5
= BN_CTX_get(ctx
)) == NULL
)
672 if ((n6
= BN_CTX_get(ctx
)) == NULL
)
676 * Note that in this function we must not read components of 'a' or
677 * 'b' once we have written the corresponding components of 'r'. ('r'
678 * might be one of 'a' or 'b'.)
683 if (!BN_copy(n1
, &a
->X
))
685 if (!BN_copy(n2
, &a
->Y
))
690 if (!field_sqr(group
, n0
, &b
->Z
, ctx
))
692 if (!field_mul(group
, n1
, &a
->X
, n0
, ctx
))
694 /* n1 = X_a * Z_b^2 */
696 if (!field_mul(group
, n0
, n0
, &b
->Z
, ctx
))
698 if (!field_mul(group
, n2
, &a
->Y
, n0
, ctx
))
700 /* n2 = Y_a * Z_b^3 */
705 if (!BN_copy(n3
, &b
->X
))
707 if (!BN_copy(n4
, &b
->Y
))
712 if (!field_sqr(group
, n0
, &a
->Z
, ctx
))
714 if (!field_mul(group
, n3
, &b
->X
, n0
, ctx
))
716 /* n3 = X_b * Z_a^2 */
718 if (!field_mul(group
, n0
, n0
, &a
->Z
, ctx
))
720 if (!field_mul(group
, n4
, &b
->Y
, n0
, ctx
))
722 /* n4 = Y_b * Z_a^3 */
726 if (!BN_mod_sub_quick(n5
, n1
, n3
, p
))
728 if (!BN_mod_sub_quick(n6
, n2
, n4
, p
))
733 if (BN_is_zero(n5
)) {
734 if (BN_is_zero(n6
)) {
735 /* a is the same point as b */
737 ret
= EC_POINT_dbl(group
, r
, a
, ctx
);
741 /* a is the inverse of b */
749 if (!BN_mod_add_quick(n1
, n1
, n3
, p
))
751 if (!BN_mod_add_quick(n2
, n2
, n4
, p
))
757 if (a
->Z_is_one
&& b
->Z_is_one
) {
758 if (!BN_copy(&r
->Z
, n5
))
762 if (!BN_copy(n0
, &b
->Z
))
764 } else if (b
->Z_is_one
) {
765 if (!BN_copy(n0
, &a
->Z
))
768 if (!field_mul(group
, n0
, &a
->Z
, &b
->Z
, ctx
))
771 if (!field_mul(group
, &r
->Z
, n0
, n5
, ctx
))
775 /* Z_r = Z_a * Z_b * n5 */
778 if (!field_sqr(group
, n0
, n6
, ctx
))
780 if (!field_sqr(group
, n4
, n5
, ctx
))
782 if (!field_mul(group
, n3
, n1
, n4
, ctx
))
784 if (!BN_mod_sub_quick(&r
->X
, n0
, n3
, p
))
786 /* X_r = n6^2 - n5^2 * 'n7' */
789 if (!BN_mod_lshift1_quick(n0
, &r
->X
, p
))
791 if (!BN_mod_sub_quick(n0
, n3
, n0
, p
))
793 /* n9 = n5^2 * 'n7' - 2 * X_r */
796 if (!field_mul(group
, n0
, n0
, n6
, ctx
))
798 if (!field_mul(group
, n5
, n4
, n5
, ctx
))
799 goto end
; /* now n5 is n5^3 */
800 if (!field_mul(group
, n1
, n2
, n5
, ctx
))
802 if (!BN_mod_sub_quick(n0
, n0
, n1
, p
))
805 if (!BN_add(n0
, n0
, p
))
807 /* now 0 <= n0 < 2*p, and n0 is even */
808 if (!BN_rshift1(&r
->Y
, n0
))
810 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
815 if (ctx
) /* otherwise we already called BN_CTX_end */
817 BN_CTX_free(new_ctx
);
823 ec_GFp_simple_dbl(const EC_GROUP
* group
, EC_POINT
* r
, const EC_POINT
* a
, BN_CTX
* ctx
)
825 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
826 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
828 BN_CTX
*new_ctx
= NULL
;
829 BIGNUM
*n0
, *n1
, *n2
, *n3
;
832 if (EC_POINT_is_at_infinity(group
, a
) > 0) {
837 field_mul
= group
->meth
->field_mul
;
838 field_sqr
= group
->meth
->field_sqr
;
842 ctx
= new_ctx
= BN_CTX_new();
847 if ((n0
= BN_CTX_get(ctx
)) == NULL
)
849 if ((n1
= BN_CTX_get(ctx
)) == NULL
)
851 if ((n2
= BN_CTX_get(ctx
)) == NULL
)
853 if ((n3
= BN_CTX_get(ctx
)) == NULL
)
857 * Note that in this function we must not read components of 'a' once
858 * we have written the corresponding components of 'r'. ('r' might
864 if (!field_sqr(group
, n0
, &a
->X
, ctx
))
866 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
868 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
870 if (!BN_mod_add_quick(n1
, n0
, &group
->a
, p
))
872 /* n1 = 3 * X_a^2 + a_curve */
873 } else if (group
->a_is_minus3
) {
874 if (!field_sqr(group
, n1
, &a
->Z
, ctx
))
876 if (!BN_mod_add_quick(n0
, &a
->X
, n1
, p
))
878 if (!BN_mod_sub_quick(n2
, &a
->X
, n1
, p
))
880 if (!field_mul(group
, n1
, n0
, n2
, ctx
))
882 if (!BN_mod_lshift1_quick(n0
, n1
, p
))
884 if (!BN_mod_add_quick(n1
, n0
, n1
, p
))
887 * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2) = 3 * X_a^2 - 3 *
891 if (!field_sqr(group
, n0
, &a
->X
, ctx
))
893 if (!BN_mod_lshift1_quick(n1
, n0
, p
))
895 if (!BN_mod_add_quick(n0
, n0
, n1
, p
))
897 if (!field_sqr(group
, n1
, &a
->Z
, ctx
))
899 if (!field_sqr(group
, n1
, n1
, ctx
))
901 if (!field_mul(group
, n1
, n1
, &group
->a
, ctx
))
903 if (!BN_mod_add_quick(n1
, n1
, n0
, p
))
905 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
910 if (!BN_copy(n0
, &a
->Y
))
913 if (!field_mul(group
, n0
, &a
->Y
, &a
->Z
, ctx
))
916 if (!BN_mod_lshift1_quick(&r
->Z
, n0
, p
))
919 /* Z_r = 2 * Y_a * Z_a */
922 if (!field_sqr(group
, n3
, &a
->Y
, ctx
))
924 if (!field_mul(group
, n2
, &a
->X
, n3
, ctx
))
926 if (!BN_mod_lshift_quick(n2
, n2
, 2, p
))
928 /* n2 = 4 * X_a * Y_a^2 */
931 if (!BN_mod_lshift1_quick(n0
, n2
, p
))
933 if (!field_sqr(group
, &r
->X
, n1
, ctx
))
935 if (!BN_mod_sub_quick(&r
->X
, &r
->X
, n0
, p
))
937 /* X_r = n1^2 - 2 * n2 */
940 if (!field_sqr(group
, n0
, n3
, ctx
))
942 if (!BN_mod_lshift_quick(n3
, n0
, 3, p
))
947 if (!BN_mod_sub_quick(n0
, n2
, &r
->X
, p
))
949 if (!field_mul(group
, n0
, n1
, n0
, ctx
))
951 if (!BN_mod_sub_quick(&r
->Y
, n0
, n3
, p
))
953 /* Y_r = n1 * (n2 - X_r) - n3 */
959 BN_CTX_free(new_ctx
);
965 ec_GFp_simple_invert(const EC_GROUP
* group
, EC_POINT
* point
, BN_CTX
* ctx
)
967 if (EC_POINT_is_at_infinity(group
, point
) > 0 || BN_is_zero(&point
->Y
))
968 /* point is its own inverse */
971 return BN_usub(&point
->Y
, &group
->field
, &point
->Y
);
976 ec_GFp_simple_is_at_infinity(const EC_GROUP
* group
, const EC_POINT
* point
)
978 return BN_is_zero(&point
->Z
);
983 ec_GFp_simple_is_on_curve(const EC_GROUP
* group
, const EC_POINT
* point
, BN_CTX
* ctx
)
985 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
986 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
988 BN_CTX
*new_ctx
= NULL
;
989 BIGNUM
*rh
, *tmp
, *Z4
, *Z6
;
992 if (EC_POINT_is_at_infinity(group
, point
) > 0)
995 field_mul
= group
->meth
->field_mul
;
996 field_sqr
= group
->meth
->field_sqr
;
1000 ctx
= new_ctx
= BN_CTX_new();
1005 if ((rh
= BN_CTX_get(ctx
)) == NULL
)
1007 if ((tmp
= BN_CTX_get(ctx
)) == NULL
)
1009 if ((Z4
= BN_CTX_get(ctx
)) == NULL
)
1011 if ((Z6
= BN_CTX_get(ctx
)) == NULL
)
1015 * We have a curve defined by a Weierstrass equation y^2 = x^3 + a*x
1016 * + b. The point to consider is given in Jacobian projective
1017 * coordinates where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1018 * Substituting this and multiplying by Z^6 transforms the above
1019 * equation into Y^2 = X^3 + a*X*Z^4 + b*Z^6. To test this, we add up
1020 * the right-hand side in 'rh'.
1024 if (!field_sqr(group
, rh
, &point
->X
, ctx
))
1027 if (!point
->Z_is_one
) {
1028 if (!field_sqr(group
, tmp
, &point
->Z
, ctx
))
1030 if (!field_sqr(group
, Z4
, tmp
, ctx
))
1032 if (!field_mul(group
, Z6
, Z4
, tmp
, ctx
))
1035 /* rh := (rh + a*Z^4)*X */
1036 if (group
->a_is_minus3
) {
1037 if (!BN_mod_lshift1_quick(tmp
, Z4
, p
))
1039 if (!BN_mod_add_quick(tmp
, tmp
, Z4
, p
))
1041 if (!BN_mod_sub_quick(rh
, rh
, tmp
, p
))
1043 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1046 if (!field_mul(group
, tmp
, Z4
, &group
->a
, ctx
))
1048 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1050 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1054 /* rh := rh + b*Z^6 */
1055 if (!field_mul(group
, tmp
, &group
->b
, Z6
, ctx
))
1057 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
))
1060 /* point->Z_is_one */
1062 /* rh := (rh + a)*X */
1063 if (!BN_mod_add_quick(rh
, rh
, &group
->a
, p
))
1065 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
))
1068 if (!BN_mod_add_quick(rh
, rh
, &group
->b
, p
))
1073 if (!field_sqr(group
, tmp
, &point
->Y
, ctx
))
1076 ret
= (0 == BN_ucmp(tmp
, rh
));
1080 BN_CTX_free(new_ctx
);
1086 ec_GFp_simple_cmp(const EC_GROUP
* group
, const EC_POINT
* a
, const EC_POINT
* b
, BN_CTX
* ctx
)
1089 * return values: -1 error 0 equal (in affine coordinates) 1
1093 int (*field_mul
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1094 int (*field_sqr
) (const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1095 BN_CTX
*new_ctx
= NULL
;
1096 BIGNUM
*tmp1
, *tmp2
, *Za23
, *Zb23
;
1097 const BIGNUM
*tmp1_
, *tmp2_
;
1100 if (EC_POINT_is_at_infinity(group
, a
) > 0) {
1101 return EC_POINT_is_at_infinity(group
, b
) > 0 ? 0 : 1;
1103 if (EC_POINT_is_at_infinity(group
, b
) > 0)
1106 if (a
->Z_is_one
&& b
->Z_is_one
) {
1107 return ((BN_cmp(&a
->X
, &b
->X
) == 0) && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
1109 field_mul
= group
->meth
->field_mul
;
1110 field_sqr
= group
->meth
->field_sqr
;
1113 ctx
= new_ctx
= BN_CTX_new();
1118 if ((tmp1
= BN_CTX_get(ctx
)) == NULL
)
1120 if ((tmp2
= BN_CTX_get(ctx
)) == NULL
)
1122 if ((Za23
= BN_CTX_get(ctx
)) == NULL
)
1124 if ((Zb23
= BN_CTX_get(ctx
)) == NULL
)
1128 * We have to decide whether (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2,
1129 * Y_b/Z_b^3), or equivalently, whether (X_a*Z_b^2, Y_a*Z_b^3) =
1130 * (X_b*Z_a^2, Y_b*Z_a^3).
1134 if (!field_sqr(group
, Zb23
, &b
->Z
, ctx
))
1136 if (!field_mul(group
, tmp1
, &a
->X
, Zb23
, ctx
))
1142 if (!field_sqr(group
, Za23
, &a
->Z
, ctx
))
1144 if (!field_mul(group
, tmp2
, &b
->X
, Za23
, ctx
))
1150 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1151 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1152 ret
= 1; /* points differ */
1156 if (!field_mul(group
, Zb23
, Zb23
, &b
->Z
, ctx
))
1158 if (!field_mul(group
, tmp1
, &a
->Y
, Zb23
, ctx
))
1164 if (!field_mul(group
, Za23
, Za23
, &a
->Z
, ctx
))
1166 if (!field_mul(group
, tmp2
, &b
->Y
, Za23
, ctx
))
1172 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1173 if (BN_cmp(tmp1_
, tmp2_
) != 0) {
1174 ret
= 1; /* points differ */
1177 /* points are equal */
1182 BN_CTX_free(new_ctx
);
1188 ec_GFp_simple_make_affine(const EC_GROUP
* group
, EC_POINT
* point
, BN_CTX
* ctx
)
1190 BN_CTX
*new_ctx
= NULL
;
1194 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
) > 0)
1198 ctx
= new_ctx
= BN_CTX_new();
1203 if ((x
= BN_CTX_get(ctx
)) == NULL
)
1205 if ((y
= BN_CTX_get(ctx
)) == NULL
)
1208 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1210 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
1212 if (!point
->Z_is_one
) {
1213 ECerror(ERR_R_INTERNAL_ERROR
);
1220 BN_CTX_free(new_ctx
);
1226 ec_GFp_simple_points_make_affine(const EC_GROUP
* group
, size_t num
, EC_POINT
* points
[], BN_CTX
* ctx
)
1228 BN_CTX
*new_ctx
= NULL
;
1229 BIGNUM
*tmp0
, *tmp1
;
1231 BIGNUM
**heap
= NULL
;
1239 ctx
= new_ctx
= BN_CTX_new();
1244 if ((tmp0
= BN_CTX_get(ctx
)) == NULL
)
1246 if ((tmp1
= BN_CTX_get(ctx
)) == NULL
)
1250 * Before converting the individual points, compute inverses of all Z
1251 * values. Modular inversion is rather slow, but luckily we can do
1252 * with a single explicit inversion, plus about 3 multiplications per
1260 * Now pow2 is the smallest power of 2 satifsying pow2 >= num. We
1265 heap
= reallocarray(NULL
, pow2
, sizeof heap
[0]);
1270 * The array is used as a binary tree, exactly as in heapsort:
1272 * heap[1] heap[2] heap[3] heap[4] heap[5]
1273 * heap[6] heap[7] heap[8]heap[9] heap[10]heap[11]
1274 * heap[12]heap[13] heap[14] heap[15]
1276 * We put the Z's in the last line; then we set each other node to the
1277 * product of its two child-nodes (where empty or 0 entries are
1278 * treated as ones); then we invert heap[1]; then we invert each
1279 * other node by replacing it by the product of its parent (after
1280 * inversion) and its sibling (before inversion).
1283 for (i
= pow2
/ 2 - 1; i
> 0; i
--)
1285 for (i
= 0; i
< num
; i
++)
1286 heap
[pow2
/ 2 + i
] = &points
[i
]->Z
;
1287 for (i
= pow2
/ 2 + num
; i
< pow2
; i
++)
1290 /* set each node to the product of its children */
1291 for (i
= pow2
/ 2 - 1; i
> 0; i
--) {
1293 if (heap
[i
] == NULL
)
1296 if (heap
[2 * i
] != NULL
) {
1297 if ((heap
[2 * i
+ 1] == NULL
) || BN_is_zero(heap
[2 * i
+ 1])) {
1298 if (!BN_copy(heap
[i
], heap
[2 * i
]))
1301 if (BN_is_zero(heap
[2 * i
])) {
1302 if (!BN_copy(heap
[i
], heap
[2 * i
+ 1]))
1305 if (!group
->meth
->field_mul(group
, heap
[i
],
1306 heap
[2 * i
], heap
[2 * i
+ 1], ctx
))
1313 /* invert heap[1] */
1314 if (!BN_is_zero(heap
[1])) {
1315 if (!BN_mod_inverse_ct(heap
[1], heap
[1], &group
->field
, ctx
)) {
1316 ECerror(ERR_R_BN_LIB
);
1320 if (group
->meth
->field_encode
!= 0) {
1322 * in the Montgomery case, we just turned R*H (representing
1323 * H) into 1/(R*H), but we need R*(1/H) (representing
1324 * 1/H); i.e. we have need to multiply by the Montgomery
1327 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
))
1329 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
))
1332 /* set other heap[i]'s to their inverses */
1333 for (i
= 2; i
< pow2
/ 2 + num
; i
+= 2) {
1335 if ((heap
[i
+ 1] != NULL
) && !BN_is_zero(heap
[i
+ 1])) {
1336 if (!group
->meth
->field_mul(group
, tmp0
, heap
[i
/ 2], heap
[i
+ 1], ctx
))
1338 if (!group
->meth
->field_mul(group
, tmp1
, heap
[i
/ 2], heap
[i
], ctx
))
1340 if (!BN_copy(heap
[i
], tmp0
))
1342 if (!BN_copy(heap
[i
+ 1], tmp1
))
1345 if (!BN_copy(heap
[i
], heap
[i
/ 2]))
1351 * we have replaced all non-zero Z's by their inverses, now fix up
1354 for (i
= 0; i
< num
; i
++) {
1355 EC_POINT
*p
= points
[i
];
1357 if (!BN_is_zero(&p
->Z
)) {
1358 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1360 if (!group
->meth
->field_sqr(group
, tmp1
, &p
->Z
, ctx
))
1362 if (!group
->meth
->field_mul(group
, &p
->X
, &p
->X
, tmp1
, ctx
))
1365 if (!group
->meth
->field_mul(group
, tmp1
, tmp1
, &p
->Z
, ctx
))
1367 if (!group
->meth
->field_mul(group
, &p
->Y
, &p
->Y
, tmp1
, ctx
))
1370 if (group
->meth
->field_set_to_one
!= 0) {
1371 if (!group
->meth
->field_set_to_one(group
, &p
->Z
, ctx
))
1385 BN_CTX_free(new_ctx
);
1388 * heap[pow2/2] .. heap[pow2-1] have not been allocated
1391 for (i
= pow2
/ 2 - 1; i
> 0; i
--) {
1392 BN_clear_free(heap
[i
]);
1401 ec_GFp_simple_field_mul(const EC_GROUP
* group
, BIGNUM
* r
, const BIGNUM
* a
, const BIGNUM
* b
, BN_CTX
* ctx
)
1403 return BN_mod_mul(r
, a
, b
, &group
->field
, ctx
);
1408 ec_GFp_simple_field_sqr(const EC_GROUP
* group
, BIGNUM
* r
, const BIGNUM
* a
, BN_CTX
* ctx
)
1410 return BN_mod_sqr(r
, a
, &group
->field
, ctx
);