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>
71 const EC_METHOD *EC_GFp_simple_method(void)
73 static const EC_METHOD ret = {
75 NID_X9_62_prime_field,
76 ec_GFp_simple_group_init,
77 ec_GFp_simple_group_finish,
78 ec_GFp_simple_group_clear_finish,
79 ec_GFp_simple_group_copy,
80 ec_GFp_simple_group_set_curve,
81 ec_GFp_simple_group_get_curve,
82 ec_GFp_simple_group_get_degree,
83 ec_GFp_simple_group_check_discriminant,
84 ec_GFp_simple_point_init,
85 ec_GFp_simple_point_finish,
86 ec_GFp_simple_point_clear_finish,
87 ec_GFp_simple_point_copy,
88 ec_GFp_simple_point_set_to_infinity,
89 ec_GFp_simple_set_Jprojective_coordinates_GFp,
90 ec_GFp_simple_get_Jprojective_coordinates_GFp,
91 ec_GFp_simple_point_set_affine_coordinates,
92 ec_GFp_simple_point_get_affine_coordinates,
97 ec_GFp_simple_is_at_infinity,
98 ec_GFp_simple_is_on_curve,
100 ec_GFp_simple_make_affine,
101 ec_GFp_simple_points_make_affine,
103 0 /* precompute_mult */ ,
104 0 /* have_precompute_mult */ ,
105 ec_GFp_simple_field_mul,
106 ec_GFp_simple_field_sqr,
108 0 /* field_encode */ ,
109 0 /* field_decode */ ,
110 0 /* field_set_to_one */
117 * Most method functions in this file are designed to work with
118 * non-trivial representations of field elements if necessary
119 * (see ecp_mont.c): while standard modular addition and subtraction
120 * are used, the field_mul and field_sqr methods will be used for
121 * multiplication, and field_encode and field_decode (if defined)
122 * will be used for converting between representations.
124 * Functions ec_GFp_simple_points_make_affine() and
125 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
126 * that if a non-trivial representation is used, it is a Montgomery
127 * representation (i.e. 'encoding' means multiplying by some factor R).
130 int ec_GFp_simple_group_init(EC_GROUP *group)
132 group->field = BN_new();
135 if (!group->field || !group->a || !group->b) {
136 BN_free(group->field);
141 group->a_is_minus3 = 0;
145 void ec_GFp_simple_group_finish(EC_GROUP *group)
147 BN_free(group->field);
152 void ec_GFp_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);
159 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
161 if (!BN_copy(dest->field, src->field))
163 if (!BN_copy(dest->a, src->a))
165 if (!BN_copy(dest->b, src->b))
168 dest->a_is_minus3 = src->a_is_minus3;
173 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
174 const BIGNUM *p, const BIGNUM *a,
175 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 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
188 ctx = new_ctx = BN_CTX_new();
194 tmp_a = BN_CTX_get(ctx);
199 if (!BN_copy(group->field, p))
201 BN_set_negative(group->field, 0);
204 if (!BN_nnmod(tmp_a, a, p, ctx))
206 if (group->meth->field_encode) {
207 if (!group->meth->field_encode(group, group->a, tmp_a, ctx))
209 } else if (!BN_copy(group->a, tmp_a))
213 if (!BN_nnmod(group->b, b, p, ctx))
215 if (group->meth->field_encode)
216 if (!group->meth->field_encode(group, group->b, group->b, ctx))
219 /* group->a_is_minus3 */
220 if (!BN_add_word(tmp_a, 3))
222 group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));
228 BN_CTX_free(new_ctx);
232 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
233 BIGNUM *b, BN_CTX *ctx)
236 BN_CTX *new_ctx = NULL;
239 if (!BN_copy(p, group->field))
243 if (a != NULL || b != NULL) {
244 if (group->meth->field_decode) {
246 ctx = new_ctx = BN_CTX_new();
251 if (!group->meth->field_decode(group, a, group->a, ctx))
255 if (!group->meth->field_decode(group, b, group->b, ctx))
260 if (!BN_copy(a, group->a))
264 if (!BN_copy(b, group->b))
273 BN_CTX_free(new_ctx);
277 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
279 return BN_num_bits(group->field);
282 int 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 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
293 ERR_R_MALLOC_FAILURE);
300 tmp_1 = BN_CTX_get(ctx);
301 tmp_2 = BN_CTX_get(ctx);
302 order = BN_CTX_get(ctx);
306 if (group->meth->field_decode) {
307 if (!group->meth->field_decode(group, a, group->a, ctx))
309 if (!group->meth->field_decode(group, b, group->b, ctx))
312 if (!BN_copy(a, group->a))
314 if (!BN_copy(b, group->b))
319 * check the discriminant:
320 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
326 } else if (!BN_is_zero(b)) {
327 if (!BN_mod_sqr(tmp_1, a, p, ctx))
329 if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
331 if (!BN_lshift(tmp_1, tmp_2, 2))
335 if (!BN_mod_sqr(tmp_2, b, p, ctx))
337 if (!BN_mul_word(tmp_2, 27))
341 if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
351 BN_CTX_free(new_ctx);
355 int ec_GFp_simple_point_init(EC_POINT *point)
362 if (!point->X || !point->Y || !point->Z) {
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;
463 BN_CTX_free(new_ctx);
467 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
468 const EC_POINT *point,
469 BIGNUM *x, BIGNUM *y,
470 BIGNUM *z, BN_CTX *ctx)
472 BN_CTX *new_ctx = NULL;
475 if (group->meth->field_decode != 0) {
477 ctx = new_ctx = BN_CTX_new();
483 if (!group->meth->field_decode(group, x, point->X, ctx))
487 if (!group->meth->field_decode(group, y, point->Y, ctx))
491 if (!group->meth->field_decode(group, z, point->Z, ctx))
496 if (!BN_copy(x, point->X))
500 if (!BN_copy(y, point->Y))
504 if (!BN_copy(z, point->Z))
512 BN_CTX_free(new_ctx);
516 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
519 const BIGNUM *y, BN_CTX *ctx)
521 if (x == NULL || y == NULL) {
523 * unlike for projective coordinates, we do not tolerate this
525 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
526 ERR_R_PASSED_NULL_PARAMETER);
530 return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y,
531 BN_value_one(), ctx);
534 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
535 const EC_POINT *point,
536 BIGNUM *x, BIGNUM *y,
539 BN_CTX *new_ctx = NULL;
540 BIGNUM *Z, *Z_1, *Z_2, *Z_3;
544 if (EC_POINT_is_at_infinity(group, point)) {
545 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
546 EC_R_POINT_AT_INFINITY);
551 ctx = new_ctx = BN_CTX_new();
558 Z_1 = BN_CTX_get(ctx);
559 Z_2 = BN_CTX_get(ctx);
560 Z_3 = BN_CTX_get(ctx);
564 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
566 if (group->meth->field_decode) {
567 if (!group->meth->field_decode(group, Z, point->Z, ctx))
575 if (group->meth->field_decode) {
577 if (!group->meth->field_decode(group, x, point->X, ctx))
581 if (!group->meth->field_decode(group, y, point->Y, ctx))
586 if (!BN_copy(x, point->X))
590 if (!BN_copy(y, point->Y))
595 if (!BN_mod_inverse(Z_1, Z_, group->field, ctx)) {
596 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
601 if (group->meth->field_encode == 0) {
602 /* field_sqr works on standard representation */
603 if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
606 if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx))
612 * in the Montgomery case, field_mul will cancel out Montgomery
615 if (!group->meth->field_mul(group, x, point->X, Z_2, ctx))
620 if (group->meth->field_encode == 0) {
622 * field_mul works on standard representation
624 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
627 if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx))
632 * in the Montgomery case, field_mul will cancel out Montgomery
635 if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx))
644 BN_CTX_free(new_ctx);
648 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
649 const EC_POINT *b, BN_CTX *ctx)
651 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
652 const BIGNUM *, BN_CTX *);
653 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
655 BN_CTX *new_ctx = NULL;
656 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
660 return EC_POINT_dbl(group, r, a, ctx);
661 if (EC_POINT_is_at_infinity(group, a))
662 return EC_POINT_copy(r, b);
663 if (EC_POINT_is_at_infinity(group, b))
664 return EC_POINT_copy(r, a);
666 field_mul = group->meth->field_mul;
667 field_sqr = group->meth->field_sqr;
671 ctx = new_ctx = BN_CTX_new();
677 n0 = BN_CTX_get(ctx);
678 n1 = BN_CTX_get(ctx);
679 n2 = BN_CTX_get(ctx);
680 n3 = BN_CTX_get(ctx);
681 n4 = BN_CTX_get(ctx);
682 n5 = BN_CTX_get(ctx);
683 n6 = BN_CTX_get(ctx);
688 * Note that in this function we must not read components of 'a' or 'b'
689 * once we have written the corresponding components of 'r'. ('r' might
690 * be one of 'a' or 'b'.)
695 if (!BN_copy(n1, a->X))
697 if (!BN_copy(n2, a->Y))
702 if (!field_sqr(group, n0, b->Z, ctx))
704 if (!field_mul(group, n1, a->X, n0, ctx))
706 /* n1 = X_a * Z_b^2 */
708 if (!field_mul(group, n0, n0, b->Z, ctx))
710 if (!field_mul(group, n2, a->Y, n0, ctx))
712 /* n2 = Y_a * Z_b^3 */
717 if (!BN_copy(n3, b->X))
719 if (!BN_copy(n4, b->Y))
724 if (!field_sqr(group, n0, a->Z, ctx))
726 if (!field_mul(group, n3, b->X, n0, ctx))
728 /* n3 = X_b * Z_a^2 */
730 if (!field_mul(group, n0, n0, a->Z, ctx))
732 if (!field_mul(group, n4, b->Y, n0, ctx))
734 /* n4 = Y_b * Z_a^3 */
738 if (!BN_mod_sub_quick(n5, n1, n3, p))
740 if (!BN_mod_sub_quick(n6, n2, n4, p))
745 if (BN_is_zero(n5)) {
746 if (BN_is_zero(n6)) {
747 /* a is the same point as b */
749 ret = EC_POINT_dbl(group, r, a, ctx);
753 /* a is the inverse of b */
762 if (!BN_mod_add_quick(n1, n1, n3, p))
764 if (!BN_mod_add_quick(n2, n2, n4, p))
770 if (a->Z_is_one && b->Z_is_one) {
771 if (!BN_copy(r->Z, n5))
775 if (!BN_copy(n0, b->Z))
777 } else if (b->Z_is_one) {
778 if (!BN_copy(n0, a->Z))
781 if (!field_mul(group, n0, a->Z, b->Z, ctx))
784 if (!field_mul(group, r->Z, n0, n5, ctx))
788 /* Z_r = Z_a * Z_b * n5 */
791 if (!field_sqr(group, n0, n6, ctx))
793 if (!field_sqr(group, n4, n5, ctx))
795 if (!field_mul(group, n3, n1, n4, ctx))
797 if (!BN_mod_sub_quick(r->X, n0, n3, p))
799 /* X_r = n6^2 - n5^2 * 'n7' */
802 if (!BN_mod_lshift1_quick(n0, r->X, p))
804 if (!BN_mod_sub_quick(n0, n3, n0, p))
806 /* n9 = n5^2 * 'n7' - 2 * X_r */
809 if (!field_mul(group, n0, n0, n6, ctx))
811 if (!field_mul(group, n5, n4, n5, ctx))
812 goto end; /* now n5 is n5^3 */
813 if (!field_mul(group, n1, n2, n5, ctx))
815 if (!BN_mod_sub_quick(n0, n0, n1, p))
818 if (!BN_add(n0, n0, p))
820 /* now 0 <= n0 < 2*p, and n0 is even */
821 if (!BN_rshift1(r->Y, n0))
823 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
828 if (ctx) /* otherwise we already called BN_CTX_end */
830 BN_CTX_free(new_ctx);
834 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
837 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
838 const BIGNUM *, BN_CTX *);
839 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
841 BN_CTX *new_ctx = NULL;
842 BIGNUM *n0, *n1, *n2, *n3;
845 if (EC_POINT_is_at_infinity(group, a)) {
851 field_mul = group->meth->field_mul;
852 field_sqr = group->meth->field_sqr;
856 ctx = new_ctx = BN_CTX_new();
862 n0 = BN_CTX_get(ctx);
863 n1 = BN_CTX_get(ctx);
864 n2 = BN_CTX_get(ctx);
865 n3 = BN_CTX_get(ctx);
870 * Note that in this function we must not read components of 'a' once we
871 * have written the corresponding components of 'r'. ('r' might the same
877 if (!field_sqr(group, n0, a->X, ctx))
879 if (!BN_mod_lshift1_quick(n1, n0, p))
881 if (!BN_mod_add_quick(n0, n0, n1, p))
883 if (!BN_mod_add_quick(n1, n0, group->a, p))
885 /* n1 = 3 * X_a^2 + a_curve */
886 } else if (group->a_is_minus3) {
887 if (!field_sqr(group, n1, a->Z, ctx))
889 if (!BN_mod_add_quick(n0, a->X, n1, p))
891 if (!BN_mod_sub_quick(n2, a->X, n1, p))
893 if (!field_mul(group, n1, n0, n2, ctx))
895 if (!BN_mod_lshift1_quick(n0, n1, p))
897 if (!BN_mod_add_quick(n1, n0, n1, p))
900 * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
901 * = 3 * X_a^2 - 3 * Z_a^4
904 if (!field_sqr(group, n0, a->X, ctx))
906 if (!BN_mod_lshift1_quick(n1, n0, p))
908 if (!BN_mod_add_quick(n0, n0, n1, p))
910 if (!field_sqr(group, n1, a->Z, ctx))
912 if (!field_sqr(group, n1, n1, ctx))
914 if (!field_mul(group, n1, n1, group->a, ctx))
916 if (!BN_mod_add_quick(n1, n1, n0, p))
918 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
923 if (!BN_copy(n0, a->Y))
926 if (!field_mul(group, n0, a->Y, a->Z, ctx))
929 if (!BN_mod_lshift1_quick(r->Z, n0, p))
932 /* Z_r = 2 * Y_a * Z_a */
935 if (!field_sqr(group, n3, a->Y, ctx))
937 if (!field_mul(group, n2, a->X, n3, ctx))
939 if (!BN_mod_lshift_quick(n2, n2, 2, p))
941 /* n2 = 4 * X_a * Y_a^2 */
944 if (!BN_mod_lshift1_quick(n0, n2, p))
946 if (!field_sqr(group, r->X, n1, ctx))
948 if (!BN_mod_sub_quick(r->X, r->X, n0, p))
950 /* X_r = n1^2 - 2 * n2 */
953 if (!field_sqr(group, n0, n3, ctx))
955 if (!BN_mod_lshift_quick(n3, n0, 3, p))
960 if (!BN_mod_sub_quick(n0, n2, r->X, p))
962 if (!field_mul(group, n0, n1, n0, ctx))
964 if (!BN_mod_sub_quick(r->Y, n0, n3, p))
966 /* Y_r = n1 * (n2 - X_r) - n3 */
972 BN_CTX_free(new_ctx);
976 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
978 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
979 /* point is its own inverse */
982 return BN_usub(point->Y, group->field, point->Y);
985 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
987 return BN_is_zero(point->Z);
990 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
993 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
994 const BIGNUM *, BN_CTX *);
995 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
997 BN_CTX *new_ctx = NULL;
998 BIGNUM *rh, *tmp, *Z4, *Z6;
1001 if (EC_POINT_is_at_infinity(group, point))
1004 field_mul = group->meth->field_mul;
1005 field_sqr = group->meth->field_sqr;
1009 ctx = new_ctx = BN_CTX_new();
1015 rh = BN_CTX_get(ctx);
1016 tmp = BN_CTX_get(ctx);
1017 Z4 = BN_CTX_get(ctx);
1018 Z6 = BN_CTX_get(ctx);
1023 * We have a curve defined by a Weierstrass equation
1024 * y^2 = x^3 + a*x + b.
1025 * The point to consider is given in Jacobian projective coordinates
1026 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1027 * Substituting this and multiplying by Z^6 transforms the above equation into
1028 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1029 * To test this, we add up the right-hand side in 'rh'.
1033 if (!field_sqr(group, rh, point->X, ctx))
1036 if (!point->Z_is_one) {
1037 if (!field_sqr(group, tmp, point->Z, ctx))
1039 if (!field_sqr(group, Z4, tmp, ctx))
1041 if (!field_mul(group, Z6, Z4, tmp, ctx))
1044 /* rh := (rh + a*Z^4)*X */
1045 if (group->a_is_minus3) {
1046 if (!BN_mod_lshift1_quick(tmp, Z4, p))
1048 if (!BN_mod_add_quick(tmp, tmp, Z4, p))
1050 if (!BN_mod_sub_quick(rh, rh, tmp, p))
1052 if (!field_mul(group, rh, rh, point->X, ctx))
1055 if (!field_mul(group, tmp, Z4, group->a, ctx))
1057 if (!BN_mod_add_quick(rh, rh, tmp, p))
1059 if (!field_mul(group, rh, rh, point->X, ctx))
1063 /* rh := rh + b*Z^6 */
1064 if (!field_mul(group, tmp, group->b, Z6, ctx))
1066 if (!BN_mod_add_quick(rh, rh, tmp, p))
1069 /* point->Z_is_one */
1071 /* rh := (rh + a)*X */
1072 if (!BN_mod_add_quick(rh, rh, group->a, p))
1074 if (!field_mul(group, rh, rh, point->X, ctx))
1077 if (!BN_mod_add_quick(rh, rh, group->b, p))
1082 if (!field_sqr(group, tmp, point->Y, ctx))
1085 ret = (0 == BN_ucmp(tmp, rh));
1089 BN_CTX_free(new_ctx);
1093 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
1094 const EC_POINT *b, BN_CTX *ctx)
1099 * 0 equal (in affine coordinates)
1103 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
1104 const BIGNUM *, BN_CTX *);
1105 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1106 BN_CTX *new_ctx = NULL;
1107 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1108 const BIGNUM *tmp1_, *tmp2_;
1111 if (EC_POINT_is_at_infinity(group, a)) {
1112 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1115 if (EC_POINT_is_at_infinity(group, b))
1118 if (a->Z_is_one && b->Z_is_one) {
1119 return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
1122 field_mul = group->meth->field_mul;
1123 field_sqr = group->meth->field_sqr;
1126 ctx = new_ctx = BN_CTX_new();
1132 tmp1 = BN_CTX_get(ctx);
1133 tmp2 = BN_CTX_get(ctx);
1134 Za23 = BN_CTX_get(ctx);
1135 Zb23 = BN_CTX_get(ctx);
1140 * We have to decide whether
1141 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1142 * or equivalently, whether
1143 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1147 if (!field_sqr(group, Zb23, b->Z, ctx))
1149 if (!field_mul(group, tmp1, a->X, Zb23, ctx))
1155 if (!field_sqr(group, Za23, a->Z, ctx))
1157 if (!field_mul(group, tmp2, b->X, Za23, ctx))
1163 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1164 if (BN_cmp(tmp1_, tmp2_) != 0) {
1165 ret = 1; /* points differ */
1170 if (!field_mul(group, Zb23, Zb23, b->Z, ctx))
1172 if (!field_mul(group, tmp1, a->Y, Zb23, ctx))
1178 if (!field_mul(group, Za23, Za23, a->Z, ctx))
1180 if (!field_mul(group, tmp2, b->Y, Za23, ctx))
1186 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1187 if (BN_cmp(tmp1_, tmp2_) != 0) {
1188 ret = 1; /* points differ */
1192 /* points are equal */
1197 BN_CTX_free(new_ctx);
1201 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
1204 BN_CTX *new_ctx = NULL;
1208 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1212 ctx = new_ctx = BN_CTX_new();
1218 x = BN_CTX_get(ctx);
1219 y = BN_CTX_get(ctx);
1223 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
1225 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
1227 if (!point->Z_is_one) {
1228 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1236 BN_CTX_free(new_ctx);
1240 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
1241 EC_POINT *points[], BN_CTX *ctx)
1243 BN_CTX *new_ctx = NULL;
1244 BIGNUM *tmp, *tmp_Z;
1245 BIGNUM **prod_Z = NULL;
1253 ctx = new_ctx = BN_CTX_new();
1259 tmp = BN_CTX_get(ctx);
1260 tmp_Z = BN_CTX_get(ctx);
1261 if (tmp == NULL || tmp_Z == NULL)
1264 prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);
1267 for (i = 0; i < num; i++) {
1268 prod_Z[i] = BN_new();
1269 if (prod_Z[i] == NULL)
1274 * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1275 * skipping any zero-valued inputs (pretend that they're 1).
1278 if (!BN_is_zero(points[0]->Z)) {
1279 if (!BN_copy(prod_Z[0], points[0]->Z))
1282 if (group->meth->field_set_to_one != 0) {
1283 if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))
1286 if (!BN_one(prod_Z[0]))
1291 for (i = 1; i < num; i++) {
1292 if (!BN_is_zero(points[i]->Z)) {
1294 meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,
1298 if (!BN_copy(prod_Z[i], prod_Z[i - 1]))
1304 * Now use a single explicit inversion to replace every non-zero
1305 * points[i]->Z by its inverse.
1308 if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {
1309 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1312 if (group->meth->field_encode != 0) {
1314 * In the Montgomery case, we just turned R*H (representing H) into
1315 * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
1316 * multiply by the Montgomery factor twice.
1318 if (!group->meth->field_encode(group, tmp, tmp, ctx))
1320 if (!group->meth->field_encode(group, tmp, tmp, ctx))
1324 for (i = num - 1; i > 0; --i) {
1326 * Loop invariant: tmp is the product of the inverses of points[0]->Z
1327 * .. points[i]->Z (zero-valued inputs skipped).
1329 if (!BN_is_zero(points[i]->Z)) {
1331 * Set tmp_Z to the inverse of points[i]->Z (as product of Z
1332 * inverses 0 .. i, Z values 0 .. i - 1).
1335 meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))
1338 * Update tmp to satisfy the loop invariant for i - 1.
1340 if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))
1342 /* Replace points[i]->Z by its inverse. */
1343 if (!BN_copy(points[i]->Z, tmp_Z))
1348 if (!BN_is_zero(points[0]->Z)) {
1349 /* Replace points[0]->Z by its inverse. */
1350 if (!BN_copy(points[0]->Z, tmp))
1354 /* Finally, fix up the X and Y coordinates for all points. */
1356 for (i = 0; i < num; i++) {
1357 EC_POINT *p = points[i];
1359 if (!BN_is_zero(p->Z)) {
1360 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1362 if (!group->meth->field_sqr(group, tmp, p->Z, ctx))
1364 if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))
1367 if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))
1369 if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))
1372 if (group->meth->field_set_to_one != 0) {
1373 if (!group->meth->field_set_to_one(group, p->Z, ctx))
1387 BN_CTX_free(new_ctx);
1388 if (prod_Z != NULL) {
1389 for (i = 0; i < num; i++) {
1390 if (prod_Z[i] == NULL)
1392 BN_clear_free(prod_Z[i]);
1394 OPENSSL_free(prod_Z);
1399 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1400 const BIGNUM *b, BN_CTX *ctx)
1402 return BN_mod_mul(r, a, b, group->field, ctx);
1405 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1408 return BN_mod_sqr(r, a, group->field, ctx);