2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
11 #include <openssl/err.h>
12 #include <openssl/symhacks.h>
16 const EC_METHOD *EC_GFp_simple_method(void)
18 static const EC_METHOD ret = {
20 NID_X9_62_prime_field,
21 ec_GFp_simple_group_init,
22 ec_GFp_simple_group_finish,
23 ec_GFp_simple_group_clear_finish,
24 ec_GFp_simple_group_copy,
25 ec_GFp_simple_group_set_curve,
26 ec_GFp_simple_group_get_curve,
27 ec_GFp_simple_group_get_degree,
28 ec_group_simple_order_bits,
29 ec_GFp_simple_group_check_discriminant,
30 ec_GFp_simple_point_init,
31 ec_GFp_simple_point_finish,
32 ec_GFp_simple_point_clear_finish,
33 ec_GFp_simple_point_copy,
34 ec_GFp_simple_point_set_to_infinity,
35 ec_GFp_simple_set_Jprojective_coordinates_GFp,
36 ec_GFp_simple_get_Jprojective_coordinates_GFp,
37 ec_GFp_simple_point_set_affine_coordinates,
38 ec_GFp_simple_point_get_affine_coordinates,
43 ec_GFp_simple_is_at_infinity,
44 ec_GFp_simple_is_on_curve,
46 ec_GFp_simple_make_affine,
47 ec_GFp_simple_points_make_affine,
49 0 /* precompute_mult */ ,
50 0 /* have_precompute_mult */ ,
51 ec_GFp_simple_field_mul,
52 ec_GFp_simple_field_sqr,
54 0 /* field_encode */ ,
55 0 /* field_decode */ ,
56 0, /* field_set_to_one */
57 ec_key_simple_priv2oct,
58 ec_key_simple_oct2priv,
60 ec_key_simple_generate_key,
61 ec_key_simple_check_key,
62 ec_key_simple_generate_public_key,
65 ecdh_simple_compute_key,
66 0, /* field_inverse_mod_ord */
67 ec_GFp_simple_blind_coordinates
74 * Most method functions in this file are designed to work with
75 * non-trivial representations of field elements if necessary
76 * (see ecp_mont.c): while standard modular addition and subtraction
77 * are used, the field_mul and field_sqr methods will be used for
78 * multiplication, and field_encode and field_decode (if defined)
79 * will be used for converting between representations.
81 * Functions ec_GFp_simple_points_make_affine() and
82 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
83 * that if a non-trivial representation is used, it is a Montgomery
84 * representation (i.e. 'encoding' means multiplying by some factor R).
87 int ec_GFp_simple_group_init(EC_GROUP *group)
89 group->field = BN_new();
92 if (group->field == NULL || group->a == NULL || group->b == NULL) {
93 BN_free(group->field);
98 group->a_is_minus3 = 0;
102 void ec_GFp_simple_group_finish(EC_GROUP *group)
104 BN_free(group->field);
109 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
111 BN_clear_free(group->field);
112 BN_clear_free(group->a);
113 BN_clear_free(group->b);
116 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
118 if (!BN_copy(dest->field, src->field))
120 if (!BN_copy(dest->a, src->a))
122 if (!BN_copy(dest->b, src->b))
125 dest->a_is_minus3 = src->a_is_minus3;
130 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
131 const BIGNUM *p, const BIGNUM *a,
132 const BIGNUM *b, BN_CTX *ctx)
135 BN_CTX *new_ctx = NULL;
138 /* p must be a prime > 3 */
139 if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
140 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
145 ctx = new_ctx = BN_CTX_new();
151 tmp_a = BN_CTX_get(ctx);
156 if (!BN_copy(group->field, p))
158 BN_set_negative(group->field, 0);
161 if (!BN_nnmod(tmp_a, a, p, ctx))
163 if (group->meth->field_encode) {
164 if (!group->meth->field_encode(group, group->a, tmp_a, ctx))
166 } else if (!BN_copy(group->a, tmp_a))
170 if (!BN_nnmod(group->b, b, p, ctx))
172 if (group->meth->field_encode)
173 if (!group->meth->field_encode(group, group->b, group->b, ctx))
176 /* group->a_is_minus3 */
177 if (!BN_add_word(tmp_a, 3))
179 group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));
185 BN_CTX_free(new_ctx);
189 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
190 BIGNUM *b, BN_CTX *ctx)
193 BN_CTX *new_ctx = NULL;
196 if (!BN_copy(p, group->field))
200 if (a != NULL || b != NULL) {
201 if (group->meth->field_decode) {
203 ctx = new_ctx = BN_CTX_new();
208 if (!group->meth->field_decode(group, a, group->a, ctx))
212 if (!group->meth->field_decode(group, b, group->b, ctx))
217 if (!BN_copy(a, group->a))
221 if (!BN_copy(b, group->b))
230 BN_CTX_free(new_ctx);
234 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
236 return BN_num_bits(group->field);
239 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
242 BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
243 const BIGNUM *p = group->field;
244 BN_CTX *new_ctx = NULL;
247 ctx = new_ctx = BN_CTX_new();
249 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
250 ERR_R_MALLOC_FAILURE);
257 tmp_1 = BN_CTX_get(ctx);
258 tmp_2 = BN_CTX_get(ctx);
259 order = BN_CTX_get(ctx);
263 if (group->meth->field_decode) {
264 if (!group->meth->field_decode(group, a, group->a, ctx))
266 if (!group->meth->field_decode(group, b, group->b, ctx))
269 if (!BN_copy(a, group->a))
271 if (!BN_copy(b, group->b))
276 * check the discriminant:
277 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
283 } else if (!BN_is_zero(b)) {
284 if (!BN_mod_sqr(tmp_1, a, p, ctx))
286 if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
288 if (!BN_lshift(tmp_1, tmp_2, 2))
292 if (!BN_mod_sqr(tmp_2, b, p, ctx))
294 if (!BN_mul_word(tmp_2, 27))
298 if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
308 BN_CTX_free(new_ctx);
312 int ec_GFp_simple_point_init(EC_POINT *point)
319 if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
328 void ec_GFp_simple_point_finish(EC_POINT *point)
335 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
337 BN_clear_free(point->X);
338 BN_clear_free(point->Y);
339 BN_clear_free(point->Z);
343 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
345 if (!BN_copy(dest->X, src->X))
347 if (!BN_copy(dest->Y, src->Y))
349 if (!BN_copy(dest->Z, src->Z))
351 dest->Z_is_one = src->Z_is_one;
352 dest->curve_name = src->curve_name;
357 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
365 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
372 BN_CTX *new_ctx = NULL;
376 ctx = new_ctx = BN_CTX_new();
382 if (!BN_nnmod(point->X, x, group->field, ctx))
384 if (group->meth->field_encode) {
385 if (!group->meth->field_encode(group, point->X, point->X, ctx))
391 if (!BN_nnmod(point->Y, y, group->field, ctx))
393 if (group->meth->field_encode) {
394 if (!group->meth->field_encode(group, point->Y, point->Y, ctx))
402 if (!BN_nnmod(point->Z, z, group->field, ctx))
404 Z_is_one = BN_is_one(point->Z);
405 if (group->meth->field_encode) {
406 if (Z_is_one && (group->meth->field_set_to_one != 0)) {
407 if (!group->meth->field_set_to_one(group, point->Z, ctx))
411 meth->field_encode(group, point->Z, point->Z, ctx))
415 point->Z_is_one = Z_is_one;
421 BN_CTX_free(new_ctx);
425 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
426 const EC_POINT *point,
427 BIGNUM *x, BIGNUM *y,
428 BIGNUM *z, BN_CTX *ctx)
430 BN_CTX *new_ctx = NULL;
433 if (group->meth->field_decode != 0) {
435 ctx = new_ctx = BN_CTX_new();
441 if (!group->meth->field_decode(group, x, point->X, ctx))
445 if (!group->meth->field_decode(group, y, point->Y, ctx))
449 if (!group->meth->field_decode(group, z, point->Z, ctx))
454 if (!BN_copy(x, point->X))
458 if (!BN_copy(y, point->Y))
462 if (!BN_copy(z, point->Z))
470 BN_CTX_free(new_ctx);
474 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
477 const BIGNUM *y, BN_CTX *ctx)
479 if (x == NULL || y == NULL) {
481 * unlike for projective coordinates, we do not tolerate this
483 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
484 ERR_R_PASSED_NULL_PARAMETER);
488 return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y,
489 BN_value_one(), ctx);
492 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
493 const EC_POINT *point,
494 BIGNUM *x, BIGNUM *y,
497 BN_CTX *new_ctx = NULL;
498 BIGNUM *Z, *Z_1, *Z_2, *Z_3;
502 if (EC_POINT_is_at_infinity(group, point)) {
503 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
504 EC_R_POINT_AT_INFINITY);
509 ctx = new_ctx = BN_CTX_new();
516 Z_1 = BN_CTX_get(ctx);
517 Z_2 = BN_CTX_get(ctx);
518 Z_3 = BN_CTX_get(ctx);
522 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
524 if (group->meth->field_decode) {
525 if (!group->meth->field_decode(group, Z, point->Z, ctx))
533 if (group->meth->field_decode) {
535 if (!group->meth->field_decode(group, x, point->X, ctx))
539 if (!group->meth->field_decode(group, y, point->Y, ctx))
544 if (!BN_copy(x, point->X))
548 if (!BN_copy(y, point->Y))
553 if (!BN_mod_inverse(Z_1, Z_, group->field, ctx)) {
554 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
559 if (group->meth->field_encode == 0) {
560 /* field_sqr works on standard representation */
561 if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
564 if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx))
570 * in the Montgomery case, field_mul will cancel out Montgomery
573 if (!group->meth->field_mul(group, x, point->X, Z_2, ctx))
578 if (group->meth->field_encode == 0) {
580 * field_mul works on standard representation
582 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
585 if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx))
590 * in the Montgomery case, field_mul will cancel out Montgomery
593 if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx))
602 BN_CTX_free(new_ctx);
606 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
607 const EC_POINT *b, BN_CTX *ctx)
609 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
610 const BIGNUM *, BN_CTX *);
611 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
613 BN_CTX *new_ctx = NULL;
614 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
618 return EC_POINT_dbl(group, r, a, ctx);
619 if (EC_POINT_is_at_infinity(group, a))
620 return EC_POINT_copy(r, b);
621 if (EC_POINT_is_at_infinity(group, b))
622 return EC_POINT_copy(r, a);
624 field_mul = group->meth->field_mul;
625 field_sqr = group->meth->field_sqr;
629 ctx = new_ctx = BN_CTX_new();
635 n0 = BN_CTX_get(ctx);
636 n1 = BN_CTX_get(ctx);
637 n2 = BN_CTX_get(ctx);
638 n3 = BN_CTX_get(ctx);
639 n4 = BN_CTX_get(ctx);
640 n5 = BN_CTX_get(ctx);
641 n6 = BN_CTX_get(ctx);
646 * Note that in this function we must not read components of 'a' or 'b'
647 * once we have written the corresponding components of 'r'. ('r' might
648 * be one of 'a' or 'b'.)
653 if (!BN_copy(n1, a->X))
655 if (!BN_copy(n2, a->Y))
660 if (!field_sqr(group, n0, b->Z, ctx))
662 if (!field_mul(group, n1, a->X, n0, ctx))
664 /* n1 = X_a * Z_b^2 */
666 if (!field_mul(group, n0, n0, b->Z, ctx))
668 if (!field_mul(group, n2, a->Y, n0, ctx))
670 /* n2 = Y_a * Z_b^3 */
675 if (!BN_copy(n3, b->X))
677 if (!BN_copy(n4, b->Y))
682 if (!field_sqr(group, n0, a->Z, ctx))
684 if (!field_mul(group, n3, b->X, n0, ctx))
686 /* n3 = X_b * Z_a^2 */
688 if (!field_mul(group, n0, n0, a->Z, ctx))
690 if (!field_mul(group, n4, b->Y, n0, ctx))
692 /* n4 = Y_b * Z_a^3 */
696 if (!BN_mod_sub_quick(n5, n1, n3, p))
698 if (!BN_mod_sub_quick(n6, n2, n4, p))
703 if (BN_is_zero(n5)) {
704 if (BN_is_zero(n6)) {
705 /* a is the same point as b */
707 ret = EC_POINT_dbl(group, r, a, ctx);
711 /* a is the inverse of b */
720 if (!BN_mod_add_quick(n1, n1, n3, p))
722 if (!BN_mod_add_quick(n2, n2, n4, p))
728 if (a->Z_is_one && b->Z_is_one) {
729 if (!BN_copy(r->Z, n5))
733 if (!BN_copy(n0, b->Z))
735 } else if (b->Z_is_one) {
736 if (!BN_copy(n0, a->Z))
739 if (!field_mul(group, n0, a->Z, b->Z, ctx))
742 if (!field_mul(group, r->Z, n0, n5, ctx))
746 /* Z_r = Z_a * Z_b * n5 */
749 if (!field_sqr(group, n0, n6, ctx))
751 if (!field_sqr(group, n4, n5, ctx))
753 if (!field_mul(group, n3, n1, n4, ctx))
755 if (!BN_mod_sub_quick(r->X, n0, n3, p))
757 /* X_r = n6^2 - n5^2 * 'n7' */
760 if (!BN_mod_lshift1_quick(n0, r->X, p))
762 if (!BN_mod_sub_quick(n0, n3, n0, p))
764 /* n9 = n5^2 * 'n7' - 2 * X_r */
767 if (!field_mul(group, n0, n0, n6, ctx))
769 if (!field_mul(group, n5, n4, n5, ctx))
770 goto end; /* now n5 is n5^3 */
771 if (!field_mul(group, n1, n2, n5, ctx))
773 if (!BN_mod_sub_quick(n0, n0, n1, p))
776 if (!BN_add(n0, n0, p))
778 /* now 0 <= n0 < 2*p, and n0 is even */
779 if (!BN_rshift1(r->Y, n0))
781 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
786 if (ctx) /* otherwise we already called BN_CTX_end */
788 BN_CTX_free(new_ctx);
792 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
795 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
796 const BIGNUM *, BN_CTX *);
797 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
799 BN_CTX *new_ctx = NULL;
800 BIGNUM *n0, *n1, *n2, *n3;
803 if (EC_POINT_is_at_infinity(group, a)) {
809 field_mul = group->meth->field_mul;
810 field_sqr = group->meth->field_sqr;
814 ctx = new_ctx = BN_CTX_new();
820 n0 = BN_CTX_get(ctx);
821 n1 = BN_CTX_get(ctx);
822 n2 = BN_CTX_get(ctx);
823 n3 = BN_CTX_get(ctx);
828 * Note that in this function we must not read components of 'a' once we
829 * have written the corresponding components of 'r'. ('r' might the same
835 if (!field_sqr(group, n0, a->X, ctx))
837 if (!BN_mod_lshift1_quick(n1, n0, p))
839 if (!BN_mod_add_quick(n0, n0, n1, p))
841 if (!BN_mod_add_quick(n1, n0, group->a, p))
843 /* n1 = 3 * X_a^2 + a_curve */
844 } else if (group->a_is_minus3) {
845 if (!field_sqr(group, n1, a->Z, ctx))
847 if (!BN_mod_add_quick(n0, a->X, n1, p))
849 if (!BN_mod_sub_quick(n2, a->X, n1, p))
851 if (!field_mul(group, n1, n0, n2, ctx))
853 if (!BN_mod_lshift1_quick(n0, n1, p))
855 if (!BN_mod_add_quick(n1, n0, n1, p))
858 * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
859 * = 3 * X_a^2 - 3 * Z_a^4
862 if (!field_sqr(group, n0, a->X, ctx))
864 if (!BN_mod_lshift1_quick(n1, n0, p))
866 if (!BN_mod_add_quick(n0, n0, n1, p))
868 if (!field_sqr(group, n1, a->Z, ctx))
870 if (!field_sqr(group, n1, n1, ctx))
872 if (!field_mul(group, n1, n1, group->a, ctx))
874 if (!BN_mod_add_quick(n1, n1, n0, p))
876 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
881 if (!BN_copy(n0, a->Y))
884 if (!field_mul(group, n0, a->Y, a->Z, ctx))
887 if (!BN_mod_lshift1_quick(r->Z, n0, p))
890 /* Z_r = 2 * Y_a * Z_a */
893 if (!field_sqr(group, n3, a->Y, ctx))
895 if (!field_mul(group, n2, a->X, n3, ctx))
897 if (!BN_mod_lshift_quick(n2, n2, 2, p))
899 /* n2 = 4 * X_a * Y_a^2 */
902 if (!BN_mod_lshift1_quick(n0, n2, p))
904 if (!field_sqr(group, r->X, n1, ctx))
906 if (!BN_mod_sub_quick(r->X, r->X, n0, p))
908 /* X_r = n1^2 - 2 * n2 */
911 if (!field_sqr(group, n0, n3, ctx))
913 if (!BN_mod_lshift_quick(n3, n0, 3, p))
918 if (!BN_mod_sub_quick(n0, n2, r->X, p))
920 if (!field_mul(group, n0, n1, n0, ctx))
922 if (!BN_mod_sub_quick(r->Y, n0, n3, p))
924 /* Y_r = n1 * (n2 - X_r) - n3 */
930 BN_CTX_free(new_ctx);
934 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
936 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
937 /* point is its own inverse */
940 return BN_usub(point->Y, group->field, point->Y);
943 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
945 return BN_is_zero(point->Z);
948 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
951 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
952 const BIGNUM *, BN_CTX *);
953 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
955 BN_CTX *new_ctx = NULL;
956 BIGNUM *rh, *tmp, *Z4, *Z6;
959 if (EC_POINT_is_at_infinity(group, point))
962 field_mul = group->meth->field_mul;
963 field_sqr = group->meth->field_sqr;
967 ctx = new_ctx = BN_CTX_new();
973 rh = BN_CTX_get(ctx);
974 tmp = BN_CTX_get(ctx);
975 Z4 = BN_CTX_get(ctx);
976 Z6 = BN_CTX_get(ctx);
981 * We have a curve defined by a Weierstrass equation
982 * y^2 = x^3 + a*x + b.
983 * The point to consider is given in Jacobian projective coordinates
984 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
985 * Substituting this and multiplying by Z^6 transforms the above equation into
986 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
987 * To test this, we add up the right-hand side in 'rh'.
991 if (!field_sqr(group, rh, point->X, ctx))
994 if (!point->Z_is_one) {
995 if (!field_sqr(group, tmp, point->Z, ctx))
997 if (!field_sqr(group, Z4, tmp, ctx))
999 if (!field_mul(group, Z6, Z4, tmp, ctx))
1002 /* rh := (rh + a*Z^4)*X */
1003 if (group->a_is_minus3) {
1004 if (!BN_mod_lshift1_quick(tmp, Z4, p))
1006 if (!BN_mod_add_quick(tmp, tmp, Z4, p))
1008 if (!BN_mod_sub_quick(rh, rh, tmp, p))
1010 if (!field_mul(group, rh, rh, point->X, ctx))
1013 if (!field_mul(group, tmp, Z4, group->a, ctx))
1015 if (!BN_mod_add_quick(rh, rh, tmp, p))
1017 if (!field_mul(group, rh, rh, point->X, ctx))
1021 /* rh := rh + b*Z^6 */
1022 if (!field_mul(group, tmp, group->b, Z6, ctx))
1024 if (!BN_mod_add_quick(rh, rh, tmp, p))
1027 /* point->Z_is_one */
1029 /* rh := (rh + a)*X */
1030 if (!BN_mod_add_quick(rh, rh, group->a, p))
1032 if (!field_mul(group, rh, rh, point->X, ctx))
1035 if (!BN_mod_add_quick(rh, rh, group->b, p))
1040 if (!field_sqr(group, tmp, point->Y, ctx))
1043 ret = (0 == BN_ucmp(tmp, rh));
1047 BN_CTX_free(new_ctx);
1051 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
1052 const EC_POINT *b, BN_CTX *ctx)
1057 * 0 equal (in affine coordinates)
1061 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
1062 const BIGNUM *, BN_CTX *);
1063 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1064 BN_CTX *new_ctx = NULL;
1065 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1066 const BIGNUM *tmp1_, *tmp2_;
1069 if (EC_POINT_is_at_infinity(group, a)) {
1070 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1073 if (EC_POINT_is_at_infinity(group, b))
1076 if (a->Z_is_one && b->Z_is_one) {
1077 return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
1080 field_mul = group->meth->field_mul;
1081 field_sqr = group->meth->field_sqr;
1084 ctx = new_ctx = BN_CTX_new();
1090 tmp1 = BN_CTX_get(ctx);
1091 tmp2 = BN_CTX_get(ctx);
1092 Za23 = BN_CTX_get(ctx);
1093 Zb23 = BN_CTX_get(ctx);
1098 * We have to decide whether
1099 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1100 * or equivalently, whether
1101 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1105 if (!field_sqr(group, Zb23, b->Z, ctx))
1107 if (!field_mul(group, tmp1, a->X, Zb23, ctx))
1113 if (!field_sqr(group, Za23, a->Z, ctx))
1115 if (!field_mul(group, tmp2, b->X, Za23, ctx))
1121 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1122 if (BN_cmp(tmp1_, tmp2_) != 0) {
1123 ret = 1; /* points differ */
1128 if (!field_mul(group, Zb23, Zb23, b->Z, ctx))
1130 if (!field_mul(group, tmp1, a->Y, Zb23, ctx))
1136 if (!field_mul(group, Za23, Za23, a->Z, ctx))
1138 if (!field_mul(group, tmp2, b->Y, Za23, ctx))
1144 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1145 if (BN_cmp(tmp1_, tmp2_) != 0) {
1146 ret = 1; /* points differ */
1150 /* points are equal */
1155 BN_CTX_free(new_ctx);
1159 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
1162 BN_CTX *new_ctx = NULL;
1166 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1170 ctx = new_ctx = BN_CTX_new();
1176 x = BN_CTX_get(ctx);
1177 y = BN_CTX_get(ctx);
1181 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
1183 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
1185 if (!point->Z_is_one) {
1186 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1194 BN_CTX_free(new_ctx);
1198 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
1199 EC_POINT *points[], BN_CTX *ctx)
1201 BN_CTX *new_ctx = NULL;
1202 BIGNUM *tmp, *tmp_Z;
1203 BIGNUM **prod_Z = NULL;
1211 ctx = new_ctx = BN_CTX_new();
1217 tmp = BN_CTX_get(ctx);
1218 tmp_Z = BN_CTX_get(ctx);
1222 prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
1225 for (i = 0; i < num; i++) {
1226 prod_Z[i] = BN_new();
1227 if (prod_Z[i] == NULL)
1232 * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1233 * skipping any zero-valued inputs (pretend that they're 1).
1236 if (!BN_is_zero(points[0]->Z)) {
1237 if (!BN_copy(prod_Z[0], points[0]->Z))
1240 if (group->meth->field_set_to_one != 0) {
1241 if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))
1244 if (!BN_one(prod_Z[0]))
1249 for (i = 1; i < num; i++) {
1250 if (!BN_is_zero(points[i]->Z)) {
1252 meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,
1256 if (!BN_copy(prod_Z[i], prod_Z[i - 1]))
1262 * Now use a single explicit inversion to replace every non-zero
1263 * points[i]->Z by its inverse.
1266 if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {
1267 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1270 if (group->meth->field_encode != 0) {
1272 * In the Montgomery case, we just turned R*H (representing H) into
1273 * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
1274 * multiply by the Montgomery factor twice.
1276 if (!group->meth->field_encode(group, tmp, tmp, ctx))
1278 if (!group->meth->field_encode(group, tmp, tmp, ctx))
1282 for (i = num - 1; i > 0; --i) {
1284 * Loop invariant: tmp is the product of the inverses of points[0]->Z
1285 * .. points[i]->Z (zero-valued inputs skipped).
1287 if (!BN_is_zero(points[i]->Z)) {
1289 * Set tmp_Z to the inverse of points[i]->Z (as product of Z
1290 * inverses 0 .. i, Z values 0 .. i - 1).
1293 meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))
1296 * Update tmp to satisfy the loop invariant for i - 1.
1298 if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))
1300 /* Replace points[i]->Z by its inverse. */
1301 if (!BN_copy(points[i]->Z, tmp_Z))
1306 if (!BN_is_zero(points[0]->Z)) {
1307 /* Replace points[0]->Z by its inverse. */
1308 if (!BN_copy(points[0]->Z, tmp))
1312 /* Finally, fix up the X and Y coordinates for all points. */
1314 for (i = 0; i < num; i++) {
1315 EC_POINT *p = points[i];
1317 if (!BN_is_zero(p->Z)) {
1318 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1320 if (!group->meth->field_sqr(group, tmp, p->Z, ctx))
1322 if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))
1325 if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))
1327 if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))
1330 if (group->meth->field_set_to_one != 0) {
1331 if (!group->meth->field_set_to_one(group, p->Z, ctx))
1345 BN_CTX_free(new_ctx);
1346 if (prod_Z != NULL) {
1347 for (i = 0; i < num; i++) {
1348 if (prod_Z[i] == NULL)
1350 BN_clear_free(prod_Z[i]);
1352 OPENSSL_free(prod_Z);
1357 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1358 const BIGNUM *b, BN_CTX *ctx)
1360 return BN_mod_mul(r, a, b, group->field, ctx);
1363 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1366 return BN_mod_sqr(r, a, group->field, ctx);
1370 * Apply randomization of EC point projective coordinates:
1372 * (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z)
1373 * lambda = [1,group->field)
1376 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1380 BIGNUM *lambda = NULL;
1381 BIGNUM *temp = NULL;
1384 lambda = BN_CTX_get(ctx);
1385 temp = BN_CTX_get(ctx);
1387 ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
1391 /* make sure lambda is not zero */
1393 if (!BN_priv_rand_range(lambda, group->field)) {
1394 ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_BN_LIB);
1397 } while (BN_is_zero(lambda));
1399 /* if field_encode defined convert between representations */
1400 if (group->meth->field_encode != NULL
1401 && !group->meth->field_encode(group, lambda, lambda, ctx))
1403 if (!group->meth->field_mul(group, p->Z, p->Z, lambda, ctx))
1405 if (!group->meth->field_sqr(group, temp, lambda, ctx))
1407 if (!group->meth->field_mul(group, p->X, p->X, temp, ctx))
1409 if (!group->meth->field_mul(group, temp, temp, lambda, ctx))
1411 if (!group->meth->field_mul(group, p->Y, p->Y, temp, ctx))