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 #include <openssl/err.h>
66 #include <openssl/symhacks.h>
70 const EC_METHOD *EC_GFp_simple_method(void)
72 static const EC_METHOD ret = {
73 NID_X9_62_prime_field,
74 ec_GFp_simple_group_init,
75 ec_GFp_simple_group_finish,
76 ec_GFp_simple_group_clear_finish,
77 ec_GFp_simple_group_copy,
78 ec_GFp_simple_group_set_curve,
79 ec_GFp_simple_group_get_curve,
80 ec_GFp_simple_group_get_degree,
81 ec_GFp_simple_group_check_discriminant,
82 ec_GFp_simple_point_init,
83 ec_GFp_simple_point_finish,
84 ec_GFp_simple_point_clear_finish,
85 ec_GFp_simple_point_copy,
86 ec_GFp_simple_point_set_to_infinity,
87 ec_GFp_simple_set_Jprojective_coordinates_GFp,
88 ec_GFp_simple_get_Jprojective_coordinates_GFp,
89 ec_GFp_simple_point_set_affine_coordinates,
90 ec_GFp_simple_point_get_affine_coordinates,
91 ec_GFp_simple_set_compressed_coordinates,
92 ec_GFp_simple_point2oct,
93 ec_GFp_simple_oct2point,
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 */ };
116 /* Most method functions in this file are designed to work with
117 * non-trivial representations of field elements if necessary
118 * (see ecp_mont.c): while standard modular addition and subtraction
119 * are used, the field_mul and field_sqr methods will be used for
120 * multiplication, and field_encode and field_decode (if defined)
121 * will be used for converting between representations.
123 * Functions ec_GFp_simple_points_make_affine() and
124 * ec_GFp_simple_point_get_affine_coordinates() specifically assume
125 * that if a non-trivial representation is used, it is a Montgomery
126 * representation (i.e. 'encoding' means multiplying by some factor R).
130 int ec_GFp_simple_group_init(EC_GROUP *group)
132 BN_init(&group->field);
135 group->a_is_minus3 = 0;
140 void ec_GFp_simple_group_finish(EC_GROUP *group)
142 BN_free(&group->field);
148 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
150 BN_clear_free(&group->field);
151 BN_clear_free(&group->a);
152 BN_clear_free(&group->b);
156 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
158 if (!BN_copy(&dest->field, &src->field)) return 0;
159 if (!BN_copy(&dest->a, &src->a)) return 0;
160 if (!BN_copy(&dest->b, &src->b)) return 0;
162 dest->a_is_minus3 = src->a_is_minus3;
168 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
169 const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
172 BN_CTX *new_ctx = NULL;
175 /* p must be a prime > 3 */
176 if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
178 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
184 ctx = new_ctx = BN_CTX_new();
190 tmp_a = BN_CTX_get(ctx);
191 if (tmp_a == NULL) goto err;
194 if (!BN_copy(&group->field, p)) goto err;
195 BN_set_sign(&group->field, 0);
198 if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
199 if (group->meth->field_encode)
200 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }
202 if (!BN_copy(&group->a, tmp_a)) goto err;
205 if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
206 if (group->meth->field_encode)
207 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
209 /* group->a_is_minus3 */
210 if (!BN_add_word(tmp_a, 3)) goto err;
211 group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
218 BN_CTX_free(new_ctx);
223 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
226 BN_CTX *new_ctx = NULL;
230 if (!BN_copy(p, &group->field)) return 0;
233 if (a != NULL || b != NULL)
235 if (group->meth->field_decode)
239 ctx = new_ctx = BN_CTX_new();
245 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
249 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
256 if (!BN_copy(a, &group->a)) goto err;
260 if (!BN_copy(b, &group->b)) goto err;
269 BN_CTX_free(new_ctx);
274 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
276 return BN_num_bits(&group->field);
280 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
283 BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
284 const BIGNUM *p = &group->field;
285 BN_CTX *new_ctx = NULL;
289 ctx = new_ctx = BN_CTX_new();
292 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
299 tmp_1 = BN_CTX_get(ctx);
300 tmp_2 = BN_CTX_get(ctx);
301 order = BN_CTX_get(ctx);
302 if (order == NULL) goto err;
304 if (group->meth->field_decode)
306 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
307 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
311 if (!BN_copy(a, &group->a)) goto err;
312 if (!BN_copy(b, &group->b)) goto err;
315 /* check the discriminant:
316 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
320 if (BN_is_zero(b)) goto err;
322 else if (!BN_is_zero(b))
324 if (!BN_mod_sqr(tmp_1, a, p, ctx)) goto err;
325 if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) goto err;
326 if (!BN_lshift(tmp_1, tmp_2, 2)) goto err;
329 if (!BN_mod_sqr(tmp_2, b, p, ctx)) goto err;
330 if (!BN_mul_word(tmp_2, 27)) goto err;
333 if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) goto err;
334 if (BN_is_zero(a)) goto err;
341 BN_CTX_free(new_ctx);
346 int ec_GFp_simple_point_init(EC_POINT *point)
357 void ec_GFp_simple_point_finish(EC_POINT *point)
365 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
367 BN_clear_free(&point->X);
368 BN_clear_free(&point->Y);
369 BN_clear_free(&point->Z);
374 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
376 if (!BN_copy(&dest->X, &src->X)) return 0;
377 if (!BN_copy(&dest->Y, &src->Y)) return 0;
378 if (!BN_copy(&dest->Z, &src->Z)) return 0;
379 dest->Z_is_one = src->Z_is_one;
385 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
393 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
394 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
396 BN_CTX *new_ctx = NULL;
401 ctx = new_ctx = BN_CTX_new();
408 if (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;
409 if (group->meth->field_encode)
411 if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
417 if (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;
418 if (group->meth->field_encode)
420 if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
428 if (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;
429 Z_is_one = BN_is_one(&point->Z);
430 if (group->meth->field_encode)
432 if (Z_is_one && (group->meth->field_set_to_one != 0))
434 if (!group->meth->field_set_to_one(group, &point->Z, ctx)) goto err;
438 if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
441 point->Z_is_one = Z_is_one;
448 BN_CTX_free(new_ctx);
453 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
454 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
456 BN_CTX *new_ctx = NULL;
459 if (group->meth->field_decode != 0)
463 ctx = new_ctx = BN_CTX_new();
470 if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
474 if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
478 if (!group->meth->field_decode(group, z, &point->Z, ctx)) goto err;
485 if (!BN_copy(x, &point->X)) goto err;
489 if (!BN_copy(y, &point->Y)) goto err;
493 if (!BN_copy(z, &point->Z)) goto err;
501 BN_CTX_free(new_ctx);
506 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
507 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
509 if (x == NULL || y == NULL)
511 /* unlike for projective coordinates, we do not tolerate this */
512 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
516 return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);
520 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
521 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
523 BN_CTX *new_ctx = NULL;
524 BIGNUM *Z, *Z_1, *Z_2, *Z_3;
528 if (EC_POINT_is_at_infinity(group, point))
530 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
536 ctx = new_ctx = BN_CTX_new();
543 Z_1 = BN_CTX_get(ctx);
544 Z_2 = BN_CTX_get(ctx);
545 Z_3 = BN_CTX_get(ctx);
546 if (Z_3 == NULL) goto err;
548 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
550 if (group->meth->field_decode)
552 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
562 if (group->meth->field_decode)
566 if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
570 if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
577 if (!BN_copy(x, &point->X)) goto err;
581 if (!BN_copy(y, &point->Y)) goto err;
587 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
589 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB);
593 if (group->meth->field_encode == 0)
595 /* field_sqr works on standard representation */
596 if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) goto err;
600 if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
605 /* in the Montgomery case, field_mul will cancel out Montgomery factor in X: */
606 if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx)) goto err;
611 if (group->meth->field_encode == 0)
613 /* field_mul works on standard representation */
614 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) goto err;
618 if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
621 /* in the Montgomery case, field_mul will cancel out Montgomery factor in Y: */
622 if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) goto err;
631 BN_CTX_free(new_ctx);
636 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
637 const BIGNUM *x_, int y_bit, BN_CTX *ctx)
639 BN_CTX *new_ctx = NULL;
640 BIGNUM *tmp1, *tmp2, *x, *y;
645 ctx = new_ctx = BN_CTX_new();
650 y_bit = (y_bit != 0);
653 tmp1 = BN_CTX_get(ctx);
654 tmp2 = BN_CTX_get(ctx);
657 if (y == NULL) goto err;
659 /* Recover y. We have a Weierstrass equation
660 * y^2 = x^3 + a*x + b,
661 * so y is one of the square roots of x^3 + a*x + b.
665 if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
666 if (group->meth->field_decode == 0)
668 /* field_{sqr,mul} work on standard representation */
669 if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
670 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
674 if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
675 if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
678 /* tmp1 := tmp1 + a*x */
679 if (group->a_is_minus3)
681 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
682 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
683 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
687 if (group->meth->field_decode)
689 if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
690 if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
694 /* field_mul works on standard representation */
695 if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
698 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
701 /* tmp1 := tmp1 + b */
702 if (group->meth->field_decode)
704 if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
705 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
709 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
712 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
714 unsigned long err = ERR_peek_error();
716 if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
718 (void)ERR_get_error();
719 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
722 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
726 if (y_bit != BN_is_odd(y))
732 kron = BN_kronecker(x, &group->field, ctx);
733 if (kron == -2) goto err;
736 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
738 /* BN_mod_sqrt() should have cought this error (not a square) */
739 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
742 if (!BN_usub(y, &group->field, y)) goto err;
744 if (y_bit != BN_is_odd(y))
746 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
750 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
757 BN_CTX_free(new_ctx);
762 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
763 unsigned char *buf, size_t len, BN_CTX *ctx)
766 BN_CTX *new_ctx = NULL;
769 size_t field_len, i, skip;
771 if ((form != POINT_CONVERSION_COMPRESSED)
772 && (form != POINT_CONVERSION_UNCOMPRESSED)
773 && (form != POINT_CONVERSION_HYBRID))
775 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
779 if (EC_POINT_is_at_infinity(group, point))
781 /* encodes to a single 0 octet */
786 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
795 /* ret := required output buffer length */
796 field_len = BN_num_bytes(&group->field);
797 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
799 /* if 'buf' is NULL, just return required length */
804 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
810 ctx = new_ctx = BN_CTX_new();
819 if (y == NULL) goto err;
821 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
823 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
830 skip = field_len - BN_num_bytes(x);
831 if (skip > field_len)
833 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
841 skip = BN_bn2bin(x, buf + i);
843 if (i != 1 + field_len)
845 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
849 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
851 skip = field_len - BN_num_bytes(y);
852 if (skip > field_len)
854 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
862 skip = BN_bn2bin(y, buf + i);
868 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
876 BN_CTX_free(new_ctx);
883 BN_CTX_free(new_ctx);
888 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
889 const unsigned char *buf, size_t len, BN_CTX *ctx)
891 point_conversion_form_t form;
893 BN_CTX *new_ctx = NULL;
895 size_t field_len, enc_len;
900 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
906 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
907 && (form != POINT_CONVERSION_UNCOMPRESSED)
908 && (form != POINT_CONVERSION_HYBRID))
910 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
913 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
915 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
923 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
927 return EC_POINT_set_to_infinity(group, point);
930 field_len = BN_num_bytes(&group->field);
931 enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
935 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
941 ctx = new_ctx = BN_CTX_new();
949 if (y == NULL) goto err;
951 if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
952 if (BN_ucmp(x, &group->field) >= 0)
954 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
958 if (form == POINT_CONVERSION_COMPRESSED)
960 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
964 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
965 if (BN_ucmp(y, &group->field) >= 0)
967 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
970 if (form == POINT_CONVERSION_HYBRID)
972 if (y_bit != BN_is_odd(y))
974 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
979 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
982 if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
984 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
993 BN_CTX_free(new_ctx);
998 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1000 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1001 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1003 BN_CTX *new_ctx = NULL;
1004 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
1008 return EC_POINT_dbl(group, r, a, ctx);
1009 if (EC_POINT_is_at_infinity(group, a))
1010 return EC_POINT_copy(r, b);
1011 if (EC_POINT_is_at_infinity(group, b))
1012 return EC_POINT_copy(r, a);
1014 field_mul = group->meth->field_mul;
1015 field_sqr = group->meth->field_sqr;
1020 ctx = new_ctx = BN_CTX_new();
1026 n0 = BN_CTX_get(ctx);
1027 n1 = BN_CTX_get(ctx);
1028 n2 = BN_CTX_get(ctx);
1029 n3 = BN_CTX_get(ctx);
1030 n4 = BN_CTX_get(ctx);
1031 n5 = BN_CTX_get(ctx);
1032 n6 = BN_CTX_get(ctx);
1033 if (n6 == NULL) goto end;
1035 /* Note that in this function we must not read components of 'a' or 'b'
1036 * once we have written the corresponding components of 'r'.
1037 * ('r' might be one of 'a' or 'b'.)
1043 if (!BN_copy(n1, &a->X)) goto end;
1044 if (!BN_copy(n2, &a->Y)) goto end;
1050 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
1051 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
1052 /* n1 = X_a * Z_b^2 */
1054 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
1055 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
1056 /* n2 = Y_a * Z_b^3 */
1062 if (!BN_copy(n3, &b->X)) goto end;
1063 if (!BN_copy(n4, &b->Y)) goto end;
1069 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
1070 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
1071 /* n3 = X_b * Z_a^2 */
1073 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
1074 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
1075 /* n4 = Y_b * Z_a^3 */
1079 if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
1080 if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
1088 /* a is the same point as b */
1090 ret = EC_POINT_dbl(group, r, a, ctx);
1096 /* a is the inverse of b */
1105 if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
1106 if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
1107 /* 'n7' = n1 + n3 */
1108 /* 'n8' = n2 + n4 */
1111 if (a->Z_is_one && b->Z_is_one)
1113 if (!BN_copy(&r->Z, n5)) goto end;
1118 { if (!BN_copy(n0, &b->Z)) goto end; }
1119 else if (b->Z_is_one)
1120 { if (!BN_copy(n0, &a->Z)) goto end; }
1122 { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
1123 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
1126 /* Z_r = Z_a * Z_b * n5 */
1129 if (!field_sqr(group, n0, n6, ctx)) goto end;
1130 if (!field_sqr(group, n4, n5, ctx)) goto end;
1131 if (!field_mul(group, n3, n1, n4, ctx)) goto end;
1132 if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
1133 /* X_r = n6^2 - n5^2 * 'n7' */
1136 if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
1137 if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
1138 /* n9 = n5^2 * 'n7' - 2 * X_r */
1141 if (!field_mul(group, n0, n0, n6, ctx)) goto end;
1142 if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
1143 if (!field_mul(group, n1, n2, n5, ctx)) goto end;
1144 if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
1146 if (!BN_add(n0, n0, p)) goto end;
1147 /* now 0 <= n0 < 2*p, and n0 is even */
1148 if (!BN_rshift1(&r->Y, n0)) goto end;
1149 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
1154 if (ctx) /* otherwise we already called BN_CTX_end */
1156 if (new_ctx != NULL)
1157 BN_CTX_free(new_ctx);
1162 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
1164 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1165 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1167 BN_CTX *new_ctx = NULL;
1168 BIGNUM *n0, *n1, *n2, *n3;
1171 if (EC_POINT_is_at_infinity(group, a))
1178 field_mul = group->meth->field_mul;
1179 field_sqr = group->meth->field_sqr;
1184 ctx = new_ctx = BN_CTX_new();
1190 n0 = BN_CTX_get(ctx);
1191 n1 = BN_CTX_get(ctx);
1192 n2 = BN_CTX_get(ctx);
1193 n3 = BN_CTX_get(ctx);
1194 if (n3 == NULL) goto err;
1196 /* Note that in this function we must not read components of 'a'
1197 * once we have written the corresponding components of 'r'.
1198 * ('r' might the same as 'a'.)
1204 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1205 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1206 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1207 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
1208 /* n1 = 3 * X_a^2 + a_curve */
1210 else if (group->a_is_minus3)
1212 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1213 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
1214 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
1215 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
1216 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
1217 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
1218 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
1219 * = 3 * X_a^2 - 3 * Z_a^4 */
1223 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1224 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1225 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1226 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1227 if (!field_sqr(group, n1, n1, ctx)) goto err;
1228 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
1229 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
1230 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
1236 if (!BN_copy(n0, &a->Y)) goto err;
1240 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
1242 if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
1244 /* Z_r = 2 * Y_a * Z_a */
1247 if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
1248 if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
1249 if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
1250 /* n2 = 4 * X_a * Y_a^2 */
1253 if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
1254 if (!field_sqr(group, &r->X, n1, ctx)) goto err;
1255 if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
1256 /* X_r = n1^2 - 2 * n2 */
1259 if (!field_sqr(group, n0, n3, ctx)) goto err;
1260 if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
1261 /* n3 = 8 * Y_a^4 */
1264 if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
1265 if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1266 if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1267 /* Y_r = n1 * (n2 - X_r) - n3 */
1273 if (new_ctx != NULL)
1274 BN_CTX_free(new_ctx);
1279 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1281 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
1282 /* point is its own inverse */
1285 return BN_usub(&point->Y, &group->field, &point->Y);
1289 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1291 return BN_is_zero(&point->Z);
1295 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1297 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1298 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1300 BN_CTX *new_ctx = NULL;
1301 BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
1304 if (EC_POINT_is_at_infinity(group, point))
1307 field_mul = group->meth->field_mul;
1308 field_sqr = group->meth->field_sqr;
1313 ctx = new_ctx = BN_CTX_new();
1319 rh = BN_CTX_get(ctx);
1320 tmp1 = BN_CTX_get(ctx);
1321 tmp2 = BN_CTX_get(ctx);
1322 Z4 = BN_CTX_get(ctx);
1323 Z6 = BN_CTX_get(ctx);
1324 if (Z6 == NULL) goto err;
1326 /* We have a curve defined by a Weierstrass equation
1327 * y^2 = x^3 + a*x + b.
1328 * The point to consider is given in Jacobian projective coordinates
1329 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1330 * Substituting this and multiplying by Z^6 transforms the above equation into
1331 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1332 * To test this, we add up the right-hand side in 'rh'.
1336 if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1337 if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1339 if (!point->Z_is_one)
1341 if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
1342 if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
1343 if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
1345 /* rh := rh + a*X*Z^4 */
1346 if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
1347 if (group->a_is_minus3)
1349 if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
1350 if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
1351 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1355 if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
1356 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1359 /* rh := rh + b*Z^6 */
1360 if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
1361 if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
1365 /* point->Z_is_one */
1367 /* rh := rh + a*X */
1368 if (group->a_is_minus3)
1370 if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
1371 if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
1372 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1376 if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
1377 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1381 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1385 if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
1387 ret = (0 == BN_cmp(tmp1, rh));
1391 if (new_ctx != NULL)
1392 BN_CTX_free(new_ctx);
1397 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1401 * 0 equal (in affine coordinates)
1405 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1406 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1407 BN_CTX *new_ctx = NULL;
1408 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1409 const BIGNUM *tmp1_, *tmp2_;
1412 if (EC_POINT_is_at_infinity(group, a))
1414 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1417 if (a->Z_is_one && b->Z_is_one)
1419 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
1422 field_mul = group->meth->field_mul;
1423 field_sqr = group->meth->field_sqr;
1427 ctx = new_ctx = BN_CTX_new();
1433 tmp1 = BN_CTX_get(ctx);
1434 tmp2 = BN_CTX_get(ctx);
1435 Za23 = BN_CTX_get(ctx);
1436 Zb23 = BN_CTX_get(ctx);
1437 if (Zb23 == NULL) goto end;
1439 /* We have to decide whether
1440 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1441 * or equivalently, whether
1442 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1447 if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
1448 if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
1455 if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
1456 if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
1462 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1463 if (BN_cmp(tmp1_, tmp2_) != 0)
1465 ret = 1; /* points differ */
1472 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
1473 if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
1480 if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
1481 if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
1487 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1488 if (BN_cmp(tmp1_, tmp2_) != 0)
1490 ret = 1; /* points differ */
1494 /* points are equal */
1499 if (new_ctx != NULL)
1500 BN_CTX_free(new_ctx);
1505 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1507 BN_CTX *new_ctx = NULL;
1511 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1516 ctx = new_ctx = BN_CTX_new();
1522 x = BN_CTX_get(ctx);
1523 y = BN_CTX_get(ctx);
1524 if (y == NULL) goto err;
1526 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1527 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1528 if (!point->Z_is_one)
1530 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1538 if (new_ctx != NULL)
1539 BN_CTX_free(new_ctx);
1544 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1546 BN_CTX *new_ctx = NULL;
1547 BIGNUM *tmp0, *tmp1;
1549 BIGNUM **heap = NULL;
1558 ctx = new_ctx = BN_CTX_new();
1564 tmp0 = BN_CTX_get(ctx);
1565 tmp1 = BN_CTX_get(ctx);
1566 if (tmp0 == NULL || tmp1 == NULL) goto err;
1568 /* Before converting the individual points, compute inverses of all Z values.
1569 * Modular inversion is rather slow, but luckily we can do with a single
1570 * explicit inversion, plus about 3 multiplications per input value.
1576 /* Now pow2 is the smallest power of 2 satifsying pow2 >= num.
1577 * We need twice that. */
1580 heap = OPENSSL_malloc(pow2 * sizeof heap[0]);
1581 if (heap == NULL) goto err;
1583 /* The array is used as a binary tree, exactly as in heapsort:
1587 * heap[4] heap[5] heap[6] heap[7]
1588 * heap[8]heap[9] heap[10]heap[11] heap[12]heap[13] heap[14] heap[15]
1590 * We put the Z's in the last line;
1591 * then we set each other node to the product of its two child-nodes (where
1592 * empty or 0 entries are treated as ones);
1593 * then we invert heap[1];
1594 * then we invert each other node by replacing it by the product of its
1595 * parent (after inversion) and its sibling (before inversion).
1598 for (i = pow2/2 - 1; i > 0; i--)
1600 for (i = 0; i < num; i++)
1601 heap[pow2/2 + i] = &points[i]->Z;
1602 for (i = pow2/2 + num; i < pow2; i++)
1605 /* set each node to the product of its children */
1606 for (i = pow2/2 - 1; i > 0; i--)
1609 if (heap[i] == NULL) goto err;
1611 if (heap[2*i] != NULL)
1613 if ((heap[2*i + 1] == NULL) || BN_is_zero(heap[2*i + 1]))
1615 if (!BN_copy(heap[i], heap[2*i])) goto err;
1619 if (BN_is_zero(heap[2*i]))
1621 if (!BN_copy(heap[i], heap[2*i + 1])) goto err;
1625 if (!group->meth->field_mul(group, heap[i],
1626 heap[2*i], heap[2*i + 1], ctx)) goto err;
1632 /* invert heap[1] */
1633 if (!BN_is_zero(heap[1]))
1635 if (!BN_mod_inverse(heap[1], heap[1], &group->field, ctx))
1637 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1641 if (group->meth->field_encode != 0)
1643 /* in the Montgomery case, we just turned R*H (representing H)
1644 * into 1/(R*H), but we need R*(1/H) (representing 1/H);
1645 * i.e. we have need to multiply by the Montgomery factor twice */
1646 if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
1647 if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
1650 /* set other heap[i]'s to their inverses */
1651 for (i = 2; i < pow2/2 + num; i += 2)
1654 if ((heap[i + 1] != NULL) && !BN_is_zero(heap[i + 1]))
1656 if (!group->meth->field_mul(group, tmp0, heap[i/2], heap[i + 1], ctx)) goto err;
1657 if (!group->meth->field_mul(group, tmp1, heap[i/2], heap[i], ctx)) goto err;
1658 if (!BN_copy(heap[i], tmp0)) goto err;
1659 if (!BN_copy(heap[i + 1], tmp1)) goto err;
1663 if (!BN_copy(heap[i], heap[i/2])) goto err;
1667 /* we have replaced all non-zero Z's by their inverses, now fix up all the points */
1668 for (i = 0; i < num; i++)
1670 EC_POINT *p = points[i];
1672 if (!BN_is_zero(&p->Z))
1674 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1676 if (!group->meth->field_sqr(group, tmp1, &p->Z, ctx)) goto err;
1677 if (!group->meth->field_mul(group, &p->X, &p->X, tmp1, ctx)) goto err;
1679 if (!group->meth->field_mul(group, tmp1, tmp1, &p->Z, ctx)) goto err;
1680 if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp1, ctx)) goto err;
1682 if (group->meth->field_set_to_one != 0)
1684 if (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;
1688 if (!BN_one(&p->Z)) goto err;
1698 if (new_ctx != NULL)
1699 BN_CTX_free(new_ctx);
1702 /* heap[pow2/2] .. heap[pow2-1] have not been allocated locally! */
1703 for (i = pow2/2 - 1; i > 0; i--)
1705 if (heap[i] != NULL)
1706 BN_clear_free(heap[i]);
1714 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1716 return BN_mod_mul(r, a, b, &group->field, ctx);
1720 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1722 return BN_mod_sqr(r, a, &group->field, ctx);