1 /* crypto/ec/ecp_smpl.c */
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 #define OPENSSL_FIPSAPI
67 #include <openssl/err.h>
68 #include <openssl/symhacks.h>
72 const EC_METHOD
*EC_GFp_simple_method(void)
74 static const EC_METHOD ret
= {
76 NID_X9_62_prime_field
,
77 ec_GFp_simple_group_init
,
78 ec_GFp_simple_group_finish
,
79 ec_GFp_simple_group_clear_finish
,
80 ec_GFp_simple_group_copy
,
81 ec_GFp_simple_group_set_curve
,
82 ec_GFp_simple_group_get_curve
,
83 ec_GFp_simple_group_get_degree
,
84 ec_GFp_simple_group_check_discriminant
,
85 ec_GFp_simple_point_init
,
86 ec_GFp_simple_point_finish
,
87 ec_GFp_simple_point_clear_finish
,
88 ec_GFp_simple_point_copy
,
89 ec_GFp_simple_point_set_to_infinity
,
90 ec_GFp_simple_set_Jprojective_coordinates_GFp
,
91 ec_GFp_simple_get_Jprojective_coordinates_GFp
,
92 ec_GFp_simple_point_set_affine_coordinates
,
93 ec_GFp_simple_point_get_affine_coordinates
,
98 ec_GFp_simple_is_at_infinity
,
99 ec_GFp_simple_is_on_curve
,
101 ec_GFp_simple_make_affine
,
102 ec_GFp_simple_points_make_affine
,
104 0 /* precompute_mult */,
105 0 /* have_precompute_mult */,
106 ec_GFp_simple_field_mul
,
107 ec_GFp_simple_field_sqr
,
109 0 /* field_encode */,
110 0 /* field_decode */,
111 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).
131 int ec_GFp_simple_group_init(EC_GROUP
*group
)
133 BN_init(&group
->field
);
136 group
->a_is_minus3
= 0;
141 void ec_GFp_simple_group_finish(EC_GROUP
*group
)
143 BN_free(&group
->field
);
149 void 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
);
157 int ec_GFp_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
159 if (!BN_copy(&dest
->field
, &src
->field
)) return 0;
160 if (!BN_copy(&dest
->a
, &src
->a
)) return 0;
161 if (!BN_copy(&dest
->b
, &src
->b
)) return 0;
163 dest
->a_is_minus3
= src
->a_is_minus3
;
169 int ec_GFp_simple_group_set_curve(EC_GROUP
*group
,
170 const BIGNUM
*p
, const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
173 BN_CTX
*new_ctx
= NULL
;
176 /* p must be a prime > 3 */
177 if (BN_num_bits(p
) <= 2 || !BN_is_odd(p
))
179 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE
, EC_R_INVALID_FIELD
);
185 ctx
= new_ctx
= BN_CTX_new();
191 tmp_a
= BN_CTX_get(ctx
);
192 if (tmp_a
== NULL
) goto err
;
195 if (!BN_copy(&group
->field
, p
)) goto err
;
196 BN_set_negative(&group
->field
, 0);
199 if (!BN_nnmod(tmp_a
, a
, p
, ctx
)) goto err
;
200 if (group
->meth
->field_encode
)
201 { if (!group
->meth
->field_encode(group
, &group
->a
, tmp_a
, ctx
)) goto err
; }
203 if (!BN_copy(&group
->a
, tmp_a
)) goto err
;
206 if (!BN_nnmod(&group
->b
, b
, p
, ctx
)) goto err
;
207 if (group
->meth
->field_encode
)
208 if (!group
->meth
->field_encode(group
, &group
->b
, &group
->b
, ctx
)) goto err
;
210 /* group->a_is_minus3 */
211 if (!BN_add_word(tmp_a
, 3)) goto err
;
212 group
->a_is_minus3
= (0 == BN_cmp(tmp_a
, &group
->field
));
219 BN_CTX_free(new_ctx
);
224 int ec_GFp_simple_group_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
, BN_CTX
*ctx
)
227 BN_CTX
*new_ctx
= NULL
;
231 if (!BN_copy(p
, &group
->field
)) return 0;
234 if (a
!= NULL
|| b
!= NULL
)
236 if (group
->meth
->field_decode
)
240 ctx
= new_ctx
= BN_CTX_new();
246 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
)) goto err
;
250 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
)) goto err
;
257 if (!BN_copy(a
, &group
->a
)) goto err
;
261 if (!BN_copy(b
, &group
->b
)) goto err
;
270 BN_CTX_free(new_ctx
);
275 int ec_GFp_simple_group_get_degree(const EC_GROUP
*group
)
277 return BN_num_bits(&group
->field
);
281 int ec_GFp_simple_group_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
284 BIGNUM
*a
,*b
,*order
,*tmp_1
,*tmp_2
;
285 const BIGNUM
*p
= &group
->field
;
286 BN_CTX
*new_ctx
= NULL
;
290 ctx
= new_ctx
= BN_CTX_new();
293 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT
, ERR_R_MALLOC_FAILURE
);
300 tmp_1
= BN_CTX_get(ctx
);
301 tmp_2
= BN_CTX_get(ctx
);
302 order
= BN_CTX_get(ctx
);
303 if (order
== NULL
) goto err
;
305 if (group
->meth
->field_decode
)
307 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
)) goto err
;
308 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
)) goto err
;
312 if (!BN_copy(a
, &group
->a
)) goto err
;
313 if (!BN_copy(b
, &group
->b
)) goto err
;
316 /* check the discriminant:
317 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
321 if (BN_is_zero(b
)) goto err
;
323 else if (!BN_is_zero(b
))
325 if (!BN_mod_sqr(tmp_1
, a
, p
, ctx
)) goto err
;
326 if (!BN_mod_mul(tmp_2
, tmp_1
, a
, p
, ctx
)) goto err
;
327 if (!BN_lshift(tmp_1
, tmp_2
, 2)) goto err
;
330 if (!BN_mod_sqr(tmp_2
, b
, p
, ctx
)) goto err
;
331 if (!BN_mul_word(tmp_2
, 27)) goto err
;
334 if (!BN_mod_add(a
, tmp_1
, tmp_2
, p
, ctx
)) goto err
;
335 if (BN_is_zero(a
)) goto err
;
343 BN_CTX_free(new_ctx
);
348 int ec_GFp_simple_point_init(EC_POINT
*point
)
359 void ec_GFp_simple_point_finish(EC_POINT
*point
)
367 void ec_GFp_simple_point_clear_finish(EC_POINT
*point
)
369 BN_clear_free(&point
->X
);
370 BN_clear_free(&point
->Y
);
371 BN_clear_free(&point
->Z
);
376 int ec_GFp_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
378 if (!BN_copy(&dest
->X
, &src
->X
)) return 0;
379 if (!BN_copy(&dest
->Y
, &src
->Y
)) return 0;
380 if (!BN_copy(&dest
->Z
, &src
->Z
)) return 0;
381 dest
->Z_is_one
= src
->Z_is_one
;
387 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
395 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
396 const BIGNUM
*x
, const BIGNUM
*y
, const BIGNUM
*z
, BN_CTX
*ctx
)
398 BN_CTX
*new_ctx
= NULL
;
403 ctx
= new_ctx
= BN_CTX_new();
410 if (!BN_nnmod(&point
->X
, x
, &group
->field
, ctx
)) goto err
;
411 if (group
->meth
->field_encode
)
413 if (!group
->meth
->field_encode(group
, &point
->X
, &point
->X
, ctx
)) goto err
;
419 if (!BN_nnmod(&point
->Y
, y
, &group
->field
, ctx
)) goto err
;
420 if (group
->meth
->field_encode
)
422 if (!group
->meth
->field_encode(group
, &point
->Y
, &point
->Y
, ctx
)) goto err
;
430 if (!BN_nnmod(&point
->Z
, z
, &group
->field
, ctx
)) goto err
;
431 Z_is_one
= BN_is_one(&point
->Z
);
432 if (group
->meth
->field_encode
)
434 if (Z_is_one
&& (group
->meth
->field_set_to_one
!= 0))
436 if (!group
->meth
->field_set_to_one(group
, &point
->Z
, ctx
)) goto err
;
440 if (!group
->meth
->field_encode(group
, &point
->Z
, &point
->Z
, ctx
)) goto err
;
443 point
->Z_is_one
= Z_is_one
;
450 BN_CTX_free(new_ctx
);
455 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
, const EC_POINT
*point
,
456 BIGNUM
*x
, BIGNUM
*y
, BIGNUM
*z
, BN_CTX
*ctx
)
458 BN_CTX
*new_ctx
= NULL
;
461 if (group
->meth
->field_decode
!= 0)
465 ctx
= new_ctx
= BN_CTX_new();
472 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
)) goto err
;
476 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
)) goto err
;
480 if (!group
->meth
->field_decode(group
, z
, &point
->Z
, ctx
)) goto err
;
487 if (!BN_copy(x
, &point
->X
)) goto err
;
491 if (!BN_copy(y
, &point
->Y
)) goto err
;
495 if (!BN_copy(z
, &point
->Z
)) goto err
;
503 BN_CTX_free(new_ctx
);
508 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
509 const BIGNUM
*x
, const BIGNUM
*y
, BN_CTX
*ctx
)
511 if (x
== NULL
|| y
== NULL
)
513 /* unlike for projective coordinates, we do not tolerate this */
514 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES
, ERR_R_PASSED_NULL_PARAMETER
);
518 return EC_POINT_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, BN_value_one(), ctx
);
522 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP
*group
, const EC_POINT
*point
,
523 BIGNUM
*x
, BIGNUM
*y
, BN_CTX
*ctx
)
525 BN_CTX
*new_ctx
= NULL
;
526 BIGNUM
*Z
, *Z_1
, *Z_2
, *Z_3
;
530 if (EC_POINT_is_at_infinity(group
, point
))
532 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
, EC_R_POINT_AT_INFINITY
);
538 ctx
= new_ctx
= BN_CTX_new();
545 Z_1
= BN_CTX_get(ctx
);
546 Z_2
= BN_CTX_get(ctx
);
547 Z_3
= BN_CTX_get(ctx
);
548 if (Z_3
== NULL
) goto err
;
550 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
552 if (group
->meth
->field_decode
)
554 if (!group
->meth
->field_decode(group
, Z
, &point
->Z
, ctx
)) goto err
;
564 if (group
->meth
->field_decode
)
568 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
)) goto err
;
572 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
)) goto err
;
579 if (!BN_copy(x
, &point
->X
)) goto err
;
583 if (!BN_copy(y
, &point
->Y
)) goto err
;
589 if (!BN_mod_inverse(Z_1
, Z_
, &group
->field
, ctx
))
591 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES
, ERR_R_BN_LIB
);
595 if (group
->meth
->field_encode
== 0)
597 /* field_sqr works on standard representation */
598 if (!group
->meth
->field_sqr(group
, Z_2
, Z_1
, ctx
)) goto err
;
602 if (!BN_mod_sqr(Z_2
, Z_1
, &group
->field
, ctx
)) goto err
;
607 /* in the Montgomery case, field_mul will cancel out Montgomery factor in X: */
608 if (!group
->meth
->field_mul(group
, x
, &point
->X
, Z_2
, ctx
)) goto err
;
613 if (group
->meth
->field_encode
== 0)
615 /* field_mul works on standard representation */
616 if (!group
->meth
->field_mul(group
, Z_3
, Z_2
, Z_1
, ctx
)) goto err
;
620 if (!BN_mod_mul(Z_3
, Z_2
, Z_1
, &group
->field
, ctx
)) goto err
;
623 /* in the Montgomery case, field_mul will cancel out Montgomery factor in Y: */
624 if (!group
->meth
->field_mul(group
, y
, &point
->Y
, Z_3
, ctx
)) goto err
;
633 BN_CTX_free(new_ctx
);
637 int ec_GFp_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
, const EC_POINT
*b
, BN_CTX
*ctx
)
639 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
640 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
642 BN_CTX
*new_ctx
= NULL
;
643 BIGNUM
*n0
, *n1
, *n2
, *n3
, *n4
, *n5
, *n6
;
647 return EC_POINT_dbl(group
, r
, a
, ctx
);
648 if (EC_POINT_is_at_infinity(group
, a
))
649 return EC_POINT_copy(r
, b
);
650 if (EC_POINT_is_at_infinity(group
, b
))
651 return EC_POINT_copy(r
, a
);
653 field_mul
= group
->meth
->field_mul
;
654 field_sqr
= group
->meth
->field_sqr
;
659 ctx
= new_ctx
= BN_CTX_new();
665 n0
= BN_CTX_get(ctx
);
666 n1
= BN_CTX_get(ctx
);
667 n2
= BN_CTX_get(ctx
);
668 n3
= BN_CTX_get(ctx
);
669 n4
= BN_CTX_get(ctx
);
670 n5
= BN_CTX_get(ctx
);
671 n6
= BN_CTX_get(ctx
);
672 if (n6
== NULL
) goto end
;
674 /* Note that in this function we must not read components of 'a' or 'b'
675 * once we have written the corresponding components of 'r'.
676 * ('r' might be one of 'a' or 'b'.)
682 if (!BN_copy(n1
, &a
->X
)) goto end
;
683 if (!BN_copy(n2
, &a
->Y
)) goto end
;
689 if (!field_sqr(group
, n0
, &b
->Z
, ctx
)) goto end
;
690 if (!field_mul(group
, n1
, &a
->X
, n0
, ctx
)) goto end
;
691 /* n1 = X_a * Z_b^2 */
693 if (!field_mul(group
, n0
, n0
, &b
->Z
, ctx
)) goto end
;
694 if (!field_mul(group
, n2
, &a
->Y
, n0
, ctx
)) goto end
;
695 /* n2 = Y_a * Z_b^3 */
701 if (!BN_copy(n3
, &b
->X
)) goto end
;
702 if (!BN_copy(n4
, &b
->Y
)) goto end
;
708 if (!field_sqr(group
, n0
, &a
->Z
, ctx
)) goto end
;
709 if (!field_mul(group
, n3
, &b
->X
, n0
, ctx
)) goto end
;
710 /* n3 = X_b * Z_a^2 */
712 if (!field_mul(group
, n0
, n0
, &a
->Z
, ctx
)) goto end
;
713 if (!field_mul(group
, n4
, &b
->Y
, n0
, ctx
)) goto end
;
714 /* n4 = Y_b * Z_a^3 */
718 if (!BN_mod_sub_quick(n5
, n1
, n3
, p
)) goto end
;
719 if (!BN_mod_sub_quick(n6
, n2
, n4
, p
)) goto end
;
727 /* a is the same point as b */
729 ret
= EC_POINT_dbl(group
, r
, a
, ctx
);
735 /* a is the inverse of b */
744 if (!BN_mod_add_quick(n1
, n1
, n3
, p
)) goto end
;
745 if (!BN_mod_add_quick(n2
, n2
, n4
, p
)) goto end
;
750 if (a
->Z_is_one
&& b
->Z_is_one
)
752 if (!BN_copy(&r
->Z
, n5
)) goto end
;
757 { if (!BN_copy(n0
, &b
->Z
)) goto end
; }
758 else if (b
->Z_is_one
)
759 { if (!BN_copy(n0
, &a
->Z
)) goto end
; }
761 { if (!field_mul(group
, n0
, &a
->Z
, &b
->Z
, ctx
)) goto end
; }
762 if (!field_mul(group
, &r
->Z
, n0
, n5
, ctx
)) goto end
;
765 /* Z_r = Z_a * Z_b * n5 */
768 if (!field_sqr(group
, n0
, n6
, ctx
)) goto end
;
769 if (!field_sqr(group
, n4
, n5
, ctx
)) goto end
;
770 if (!field_mul(group
, n3
, n1
, n4
, ctx
)) goto end
;
771 if (!BN_mod_sub_quick(&r
->X
, n0
, n3
, p
)) goto end
;
772 /* X_r = n6^2 - n5^2 * 'n7' */
775 if (!BN_mod_lshift1_quick(n0
, &r
->X
, p
)) goto end
;
776 if (!BN_mod_sub_quick(n0
, n3
, n0
, p
)) goto end
;
777 /* n9 = n5^2 * 'n7' - 2 * X_r */
780 if (!field_mul(group
, n0
, n0
, n6
, ctx
)) goto end
;
781 if (!field_mul(group
, n5
, n4
, n5
, ctx
)) goto end
; /* now n5 is n5^3 */
782 if (!field_mul(group
, n1
, n2
, n5
, ctx
)) goto end
;
783 if (!BN_mod_sub_quick(n0
, n0
, n1
, p
)) goto end
;
785 if (!BN_add(n0
, n0
, p
)) goto end
;
786 /* now 0 <= n0 < 2*p, and n0 is even */
787 if (!BN_rshift1(&r
->Y
, n0
)) goto end
;
788 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
793 if (ctx
) /* otherwise we already called BN_CTX_end */
796 BN_CTX_free(new_ctx
);
801 int ec_GFp_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
, BN_CTX
*ctx
)
803 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
804 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
806 BN_CTX
*new_ctx
= NULL
;
807 BIGNUM
*n0
, *n1
, *n2
, *n3
;
810 if (EC_POINT_is_at_infinity(group
, a
))
817 field_mul
= group
->meth
->field_mul
;
818 field_sqr
= group
->meth
->field_sqr
;
823 ctx
= new_ctx
= BN_CTX_new();
829 n0
= BN_CTX_get(ctx
);
830 n1
= BN_CTX_get(ctx
);
831 n2
= BN_CTX_get(ctx
);
832 n3
= BN_CTX_get(ctx
);
833 if (n3
== NULL
) goto err
;
835 /* Note that in this function we must not read components of 'a'
836 * once we have written the corresponding components of 'r'.
837 * ('r' might the same as 'a'.)
843 if (!field_sqr(group
, n0
, &a
->X
, ctx
)) goto err
;
844 if (!BN_mod_lshift1_quick(n1
, n0
, p
)) goto err
;
845 if (!BN_mod_add_quick(n0
, n0
, n1
, p
)) goto err
;
846 if (!BN_mod_add_quick(n1
, n0
, &group
->a
, p
)) goto err
;
847 /* n1 = 3 * X_a^2 + a_curve */
849 else if (group
->a_is_minus3
)
851 if (!field_sqr(group
, n1
, &a
->Z
, ctx
)) goto err
;
852 if (!BN_mod_add_quick(n0
, &a
->X
, n1
, p
)) goto err
;
853 if (!BN_mod_sub_quick(n2
, &a
->X
, n1
, p
)) goto err
;
854 if (!field_mul(group
, n1
, n0
, n2
, ctx
)) goto err
;
855 if (!BN_mod_lshift1_quick(n0
, n1
, p
)) goto err
;
856 if (!BN_mod_add_quick(n1
, n0
, n1
, p
)) goto err
;
857 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
858 * = 3 * X_a^2 - 3 * Z_a^4 */
862 if (!field_sqr(group
, n0
, &a
->X
, ctx
)) goto err
;
863 if (!BN_mod_lshift1_quick(n1
, n0
, p
)) goto err
;
864 if (!BN_mod_add_quick(n0
, n0
, n1
, p
)) goto err
;
865 if (!field_sqr(group
, n1
, &a
->Z
, ctx
)) goto err
;
866 if (!field_sqr(group
, n1
, n1
, ctx
)) goto err
;
867 if (!field_mul(group
, n1
, n1
, &group
->a
, ctx
)) goto err
;
868 if (!BN_mod_add_quick(n1
, n1
, n0
, p
)) goto err
;
869 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
875 if (!BN_copy(n0
, &a
->Y
)) goto err
;
879 if (!field_mul(group
, n0
, &a
->Y
, &a
->Z
, ctx
)) goto err
;
881 if (!BN_mod_lshift1_quick(&r
->Z
, n0
, p
)) goto err
;
883 /* Z_r = 2 * Y_a * Z_a */
886 if (!field_sqr(group
, n3
, &a
->Y
, ctx
)) goto err
;
887 if (!field_mul(group
, n2
, &a
->X
, n3
, ctx
)) goto err
;
888 if (!BN_mod_lshift_quick(n2
, n2
, 2, p
)) goto err
;
889 /* n2 = 4 * X_a * Y_a^2 */
892 if (!BN_mod_lshift1_quick(n0
, n2
, p
)) goto err
;
893 if (!field_sqr(group
, &r
->X
, n1
, ctx
)) goto err
;
894 if (!BN_mod_sub_quick(&r
->X
, &r
->X
, n0
, p
)) goto err
;
895 /* X_r = n1^2 - 2 * n2 */
898 if (!field_sqr(group
, n0
, n3
, ctx
)) goto err
;
899 if (!BN_mod_lshift_quick(n3
, n0
, 3, p
)) goto err
;
903 if (!BN_mod_sub_quick(n0
, n2
, &r
->X
, p
)) goto err
;
904 if (!field_mul(group
, n0
, n1
, n0
, ctx
)) goto err
;
905 if (!BN_mod_sub_quick(&r
->Y
, n0
, n3
, p
)) goto err
;
906 /* Y_r = n1 * (n2 - X_r) - n3 */
913 BN_CTX_free(new_ctx
);
918 int ec_GFp_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
920 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(&point
->Y
))
921 /* point is its own inverse */
924 return BN_usub(&point
->Y
, &group
->field
, &point
->Y
);
928 int ec_GFp_simple_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
930 return BN_is_zero(&point
->Z
);
934 int ec_GFp_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
, BN_CTX
*ctx
)
936 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
937 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
939 BN_CTX
*new_ctx
= NULL
;
940 BIGNUM
*rh
, *tmp
, *Z4
, *Z6
;
943 if (EC_POINT_is_at_infinity(group
, point
))
946 field_mul
= group
->meth
->field_mul
;
947 field_sqr
= group
->meth
->field_sqr
;
952 ctx
= new_ctx
= BN_CTX_new();
958 rh
= BN_CTX_get(ctx
);
959 tmp
= BN_CTX_get(ctx
);
960 Z4
= BN_CTX_get(ctx
);
961 Z6
= BN_CTX_get(ctx
);
962 if (Z6
== NULL
) goto err
;
964 /* We have a curve defined by a Weierstrass equation
965 * y^2 = x^3 + a*x + b.
966 * The point to consider is given in Jacobian projective coordinates
967 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
968 * Substituting this and multiplying by Z^6 transforms the above equation into
969 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
970 * To test this, we add up the right-hand side in 'rh'.
974 if (!field_sqr(group
, rh
, &point
->X
, ctx
)) goto err
;
976 if (!point
->Z_is_one
)
978 if (!field_sqr(group
, tmp
, &point
->Z
, ctx
)) goto err
;
979 if (!field_sqr(group
, Z4
, tmp
, ctx
)) goto err
;
980 if (!field_mul(group
, Z6
, Z4
, tmp
, ctx
)) goto err
;
982 /* rh := (rh + a*Z^4)*X */
983 if (group
->a_is_minus3
)
985 if (!BN_mod_lshift1_quick(tmp
, Z4
, p
)) goto err
;
986 if (!BN_mod_add_quick(tmp
, tmp
, Z4
, p
)) goto err
;
987 if (!BN_mod_sub_quick(rh
, rh
, tmp
, p
)) goto err
;
988 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
)) goto err
;
992 if (!field_mul(group
, tmp
, Z4
, &group
->a
, ctx
)) goto err
;
993 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
)) goto err
;
994 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
)) goto err
;
997 /* rh := rh + b*Z^6 */
998 if (!field_mul(group
, tmp
, &group
->b
, Z6
, ctx
)) goto err
;
999 if (!BN_mod_add_quick(rh
, rh
, tmp
, p
)) goto err
;
1003 /* point->Z_is_one */
1005 /* rh := (rh + a)*X */
1006 if (!BN_mod_add_quick(rh
, rh
, &group
->a
, p
)) goto err
;
1007 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
)) goto err
;
1009 if (!BN_mod_add_quick(rh
, rh
, &group
->b
, p
)) goto err
;
1013 if (!field_sqr(group
, tmp
, &point
->Y
, ctx
)) goto err
;
1015 ret
= (0 == BN_ucmp(tmp
, rh
));
1019 if (new_ctx
!= NULL
)
1020 BN_CTX_free(new_ctx
);
1025 int ec_GFp_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
, BN_CTX
*ctx
)
1029 * 0 equal (in affine coordinates)
1033 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1034 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1035 BN_CTX
*new_ctx
= NULL
;
1036 BIGNUM
*tmp1
, *tmp2
, *Za23
, *Zb23
;
1037 const BIGNUM
*tmp1_
, *tmp2_
;
1040 if (EC_POINT_is_at_infinity(group
, a
))
1042 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
1045 if (EC_POINT_is_at_infinity(group
, b
))
1048 if (a
->Z_is_one
&& b
->Z_is_one
)
1050 return ((BN_cmp(&a
->X
, &b
->X
) == 0) && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
1053 field_mul
= group
->meth
->field_mul
;
1054 field_sqr
= group
->meth
->field_sqr
;
1058 ctx
= new_ctx
= BN_CTX_new();
1064 tmp1
= BN_CTX_get(ctx
);
1065 tmp2
= BN_CTX_get(ctx
);
1066 Za23
= BN_CTX_get(ctx
);
1067 Zb23
= BN_CTX_get(ctx
);
1068 if (Zb23
== NULL
) goto end
;
1070 /* We have to decide whether
1071 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1072 * or equivalently, whether
1073 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1078 if (!field_sqr(group
, Zb23
, &b
->Z
, ctx
)) goto end
;
1079 if (!field_mul(group
, tmp1
, &a
->X
, Zb23
, ctx
)) goto end
;
1086 if (!field_sqr(group
, Za23
, &a
->Z
, ctx
)) goto end
;
1087 if (!field_mul(group
, tmp2
, &b
->X
, Za23
, ctx
)) goto end
;
1093 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1094 if (BN_cmp(tmp1_
, tmp2_
) != 0)
1096 ret
= 1; /* points differ */
1103 if (!field_mul(group
, Zb23
, Zb23
, &b
->Z
, ctx
)) goto end
;
1104 if (!field_mul(group
, tmp1
, &a
->Y
, Zb23
, ctx
)) goto end
;
1111 if (!field_mul(group
, Za23
, Za23
, &a
->Z
, ctx
)) goto end
;
1112 if (!field_mul(group
, tmp2
, &b
->Y
, Za23
, ctx
)) goto end
;
1118 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1119 if (BN_cmp(tmp1_
, tmp2_
) != 0)
1121 ret
= 1; /* points differ */
1125 /* points are equal */
1130 if (new_ctx
!= NULL
)
1131 BN_CTX_free(new_ctx
);
1136 int ec_GFp_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1138 BN_CTX
*new_ctx
= NULL
;
1142 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
1147 ctx
= new_ctx
= BN_CTX_new();
1153 x
= BN_CTX_get(ctx
);
1154 y
= BN_CTX_get(ctx
);
1155 if (y
== NULL
) goto err
;
1157 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
1158 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
1159 if (!point
->Z_is_one
)
1161 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE
, ERR_R_INTERNAL_ERROR
);
1169 if (new_ctx
!= NULL
)
1170 BN_CTX_free(new_ctx
);
1175 int ec_GFp_simple_points_make_affine(const EC_GROUP
*group
, size_t num
, EC_POINT
*points
[], BN_CTX
*ctx
)
1177 BN_CTX
*new_ctx
= NULL
;
1178 BIGNUM
*tmp0
, *tmp1
;
1180 BIGNUM
**heap
= NULL
;
1189 ctx
= new_ctx
= BN_CTX_new();
1195 tmp0
= BN_CTX_get(ctx
);
1196 tmp1
= BN_CTX_get(ctx
);
1197 if (tmp0
== NULL
|| tmp1
== NULL
) goto err
;
1199 /* Before converting the individual points, compute inverses of all Z values.
1200 * Modular inversion is rather slow, but luckily we can do with a single
1201 * explicit inversion, plus about 3 multiplications per input value.
1207 /* Now pow2 is the smallest power of 2 satifsying pow2 >= num.
1208 * We need twice that. */
1211 heap
= OPENSSL_malloc(pow2
* sizeof heap
[0]);
1212 if (heap
== NULL
) goto err
;
1214 /* The array is used as a binary tree, exactly as in heapsort:
1218 * heap[4] heap[5] heap[6] heap[7]
1219 * heap[8]heap[9] heap[10]heap[11] heap[12]heap[13] heap[14] heap[15]
1221 * We put the Z's in the last line;
1222 * then we set each other node to the product of its two child-nodes (where
1223 * empty or 0 entries are treated as ones);
1224 * then we invert heap[1];
1225 * then we invert each other node by replacing it by the product of its
1226 * parent (after inversion) and its sibling (before inversion).
1229 for (i
= pow2
/2 - 1; i
> 0; i
--)
1231 for (i
= 0; i
< num
; i
++)
1232 heap
[pow2
/2 + i
] = &points
[i
]->Z
;
1233 for (i
= pow2
/2 + num
; i
< pow2
; i
++)
1236 /* set each node to the product of its children */
1237 for (i
= pow2
/2 - 1; i
> 0; i
--)
1240 if (heap
[i
] == NULL
) goto err
;
1242 if (heap
[2*i
] != NULL
)
1244 if ((heap
[2*i
+ 1] == NULL
) || BN_is_zero(heap
[2*i
+ 1]))
1246 if (!BN_copy(heap
[i
], heap
[2*i
])) goto err
;
1250 if (BN_is_zero(heap
[2*i
]))
1252 if (!BN_copy(heap
[i
], heap
[2*i
+ 1])) goto err
;
1256 if (!group
->meth
->field_mul(group
, heap
[i
],
1257 heap
[2*i
], heap
[2*i
+ 1], ctx
)) goto err
;
1263 /* invert heap[1] */
1264 if (!BN_is_zero(heap
[1]))
1266 if (!BN_mod_inverse(heap
[1], heap
[1], &group
->field
, ctx
))
1268 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE
, ERR_R_BN_LIB
);
1272 if (group
->meth
->field_encode
!= 0)
1274 /* in the Montgomery case, we just turned R*H (representing H)
1275 * into 1/(R*H), but we need R*(1/H) (representing 1/H);
1276 * i.e. we have need to multiply by the Montgomery factor twice */
1277 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
)) goto err
;
1278 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
)) goto err
;
1281 /* set other heap[i]'s to their inverses */
1282 for (i
= 2; i
< pow2
/2 + num
; i
+= 2)
1285 if ((heap
[i
+ 1] != NULL
) && !BN_is_zero(heap
[i
+ 1]))
1287 if (!group
->meth
->field_mul(group
, tmp0
, heap
[i
/2], heap
[i
+ 1], ctx
)) goto err
;
1288 if (!group
->meth
->field_mul(group
, tmp1
, heap
[i
/2], heap
[i
], ctx
)) goto err
;
1289 if (!BN_copy(heap
[i
], tmp0
)) goto err
;
1290 if (!BN_copy(heap
[i
+ 1], tmp1
)) goto err
;
1294 if (!BN_copy(heap
[i
], heap
[i
/2])) goto err
;
1298 /* we have replaced all non-zero Z's by their inverses, now fix up all the points */
1299 for (i
= 0; i
< num
; i
++)
1301 EC_POINT
*p
= points
[i
];
1303 if (!BN_is_zero(&p
->Z
))
1305 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1307 if (!group
->meth
->field_sqr(group
, tmp1
, &p
->Z
, ctx
)) goto err
;
1308 if (!group
->meth
->field_mul(group
, &p
->X
, &p
->X
, tmp1
, ctx
)) goto err
;
1310 if (!group
->meth
->field_mul(group
, tmp1
, tmp1
, &p
->Z
, ctx
)) goto err
;
1311 if (!group
->meth
->field_mul(group
, &p
->Y
, &p
->Y
, tmp1
, ctx
)) goto err
;
1313 if (group
->meth
->field_set_to_one
!= 0)
1315 if (!group
->meth
->field_set_to_one(group
, &p
->Z
, ctx
)) goto err
;
1319 if (!BN_one(&p
->Z
)) goto err
;
1329 if (new_ctx
!= NULL
)
1330 BN_CTX_free(new_ctx
);
1333 /* heap[pow2/2] .. heap[pow2-1] have not been allocated locally! */
1334 for (i
= pow2
/2 - 1; i
> 0; i
--)
1336 if (heap
[i
] != NULL
)
1337 BN_clear_free(heap
[i
]);
1345 int ec_GFp_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
1347 return BN_mod_mul(r
, a
, b
, &group
->field
, ctx
);
1351 int ec_GFp_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
, BN_CTX
*ctx
)
1353 return BN_mod_sqr(r
, a
, &group
->field
, ctx
);