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 /* ====================================================================
5 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
58 #include <openssl/err.h>
59 #include <openssl/symhacks.h>
63 const EC_METHOD *EC_GFp_simple_method(void)
65 static const EC_METHOD ret = {
66 NID_X9_62_prime_field,
67 ec_GFp_simple_group_init,
68 ec_GFp_simple_group_finish,
69 ec_GFp_simple_group_clear_finish,
70 ec_GFp_simple_group_copy,
71 ec_GFp_simple_group_set_curve_GFp,
72 ec_GFp_simple_group_get_curve_GFp,
73 ec_GFp_simple_group_check_discriminant,
74 ec_GFp_simple_point_init,
75 ec_GFp_simple_point_finish,
76 ec_GFp_simple_point_clear_finish,
77 ec_GFp_simple_point_copy,
78 ec_GFp_simple_point_set_to_infinity,
79 ec_GFp_simple_set_Jprojective_coordinates_GFp,
80 ec_GFp_simple_get_Jprojective_coordinates_GFp,
81 ec_GFp_simple_point_set_affine_coordinates_GFp,
82 ec_GFp_simple_point_get_affine_coordinates_GFp,
83 ec_GFp_simple_set_compressed_coordinates_GFp,
84 ec_GFp_simple_point2oct,
85 ec_GFp_simple_oct2point,
89 ec_GFp_simple_is_at_infinity,
90 ec_GFp_simple_is_on_curve,
92 ec_GFp_simple_make_affine,
93 ec_GFp_simple_points_make_affine,
94 ec_GFp_simple_field_mul,
95 ec_GFp_simple_field_sqr,
98 0 /* field_set_to_one */ };
104 int ec_GFp_simple_group_init(EC_GROUP *group)
106 BN_init(&group->field);
109 group->a_is_minus3 = 0;
114 void ec_GFp_simple_group_finish(EC_GROUP *group)
116 BN_free(&group->field);
122 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
124 BN_clear_free(&group->field);
125 BN_clear_free(&group->a);
126 BN_clear_free(&group->b);
130 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
132 if (!BN_copy(&dest->field, &src->field)) return 0;
133 if (!BN_copy(&dest->a, &src->a)) return 0;
134 if (!BN_copy(&dest->b, &src->b)) return 0;
136 dest->a_is_minus3 = src->a_is_minus3;
142 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
143 const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
146 BN_CTX *new_ctx = NULL;
149 /* p must be a prime > 3 */
150 if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
152 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP, EC_R_INVALID_FIELD);
158 ctx = new_ctx = BN_CTX_new();
164 tmp_a = BN_CTX_get(ctx);
165 if (tmp_a == NULL) goto err;
168 if (!BN_copy(&group->field, p)) goto err;
169 group->field.neg = 0;
172 if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
173 if (group->meth->field_encode)
174 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }
176 if (!BN_copy(&group->a, tmp_a)) goto err;
179 if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
180 if (group->meth->field_encode)
181 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
183 /* group->a_is_minus3 */
184 if (!BN_add_word(tmp_a, 3)) goto err;
185 group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
192 BN_CTX_free(new_ctx);
197 int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
200 BN_CTX *new_ctx = NULL;
204 if (!BN_copy(p, &group->field)) return 0;
207 if (a != NULL || b != NULL)
209 if (group->meth->field_decode)
213 ctx = new_ctx = BN_CTX_new();
219 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
223 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
230 if (!BN_copy(a, &group->a)) goto err;
234 if (!BN_copy(b, &group->b)) goto err;
243 BN_CTX_free(new_ctx);
248 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
251 BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
252 const BIGNUM *p = &group->field;
253 BN_CTX *new_ctx = NULL;
257 ctx = new_ctx = BN_CTX_new();
260 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
267 tmp_1 = BN_CTX_get(ctx);
268 tmp_2 = BN_CTX_get(ctx);
269 order = BN_CTX_get(ctx);
270 if (order == NULL) goto err;
272 if (group->meth->field_decode)
274 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
275 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
279 if (!BN_copy(a, &group->a)) goto err;
280 if (!BN_copy(b, &group->b)) goto err;
283 /* check the discriminant:
284 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
288 if (BN_is_zero(b)) goto err;
290 else if (!BN_is_zero(b))
292 if (!BN_mod_sqr(tmp_1, a, p, ctx)) goto err;
293 if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) goto err;
294 if (!BN_lshift(tmp_1, tmp_2, 2)) goto err;
297 if (!BN_mod_sqr(tmp_2, b, p, ctx)) goto err;
298 if (!BN_mul_word(tmp_2, 27)) goto err;
301 if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) goto err;
302 if (BN_is_zero(a)) goto err;
309 BN_CTX_free(new_ctx);
314 int ec_GFp_simple_point_init(EC_POINT *point)
325 void ec_GFp_simple_point_finish(EC_POINT *point)
333 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
335 BN_clear_free(&point->X);
336 BN_clear_free(&point->Y);
337 BN_clear_free(&point->Z);
342 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
344 if (!BN_copy(&dest->X, &src->X)) return 0;
345 if (!BN_copy(&dest->Y, &src->Y)) return 0;
346 if (!BN_copy(&dest->Z, &src->Z)) return 0;
347 dest->Z_is_one = src->Z_is_one;
353 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
356 return (BN_zero(&point->Z));
360 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
361 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
363 BN_CTX *new_ctx = NULL;
368 ctx = new_ctx = BN_CTX_new();
375 if (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;
376 if (group->meth->field_encode)
378 if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
384 if (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;
385 if (group->meth->field_encode)
387 if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
395 if (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;
396 Z_is_one = BN_is_one(&point->Z);
397 if (group->meth->field_encode)
399 if (Z_is_one && (group->meth->field_set_to_one != 0))
401 if (!group->meth->field_set_to_one(group, &point->Z, ctx)) goto err;
405 if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
408 point->Z_is_one = Z_is_one;
415 BN_CTX_free(new_ctx);
420 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
421 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
423 BN_CTX *new_ctx = NULL;
426 if (group->meth->field_decode != 0)
430 ctx = new_ctx = BN_CTX_new();
437 if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
441 if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
445 if (!group->meth->field_decode(group, z, &point->Z, ctx)) goto err;
452 if (!BN_copy(x, &point->X)) goto err;
456 if (!BN_copy(y, &point->Y)) goto err;
460 if (!BN_copy(z, &point->Z)) goto err;
468 BN_CTX_free(new_ctx);
473 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
474 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
476 if (x == NULL || y == NULL)
478 /* unlike for projective coordinates, we do not tolerate this */
479 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_PASSED_NULL_PARAMETER);
483 return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);
487 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
488 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
490 BN_CTX *new_ctx = NULL;
491 BIGNUM *X, *Y, *Z, *Z_1, *Z_2, *Z_3;
492 const BIGNUM *X_, *Y_, *Z_;
495 if (EC_POINT_is_at_infinity(group, point))
497 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_POINT_AT_INFINITY);
503 ctx = new_ctx = BN_CTX_new();
512 Z_1 = BN_CTX_get(ctx);
513 Z_2 = BN_CTX_get(ctx);
514 Z_3 = BN_CTX_get(ctx);
515 if (Z_3 == NULL) goto err;
517 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
519 if (group->meth->field_decode)
521 if (!group->meth->field_decode(group, X, &point->X, ctx)) goto err;
522 if (!group->meth->field_decode(group, Y, &point->Y, ctx)) goto err;
523 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
524 X_ = X; Y_ = Y; Z_ = Z;
537 if (!BN_copy(x, X_)) goto err;
541 if (!BN_copy(y, Y_)) goto err;
546 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
548 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_BN_LIB);
552 if (group->meth->field_encode == 0)
554 /* field_sqr works on standard representation */
555 if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) goto err;
559 if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
564 if (group->meth->field_encode == 0)
566 /* field_mul works on standard representation */
567 if (!group->meth->field_mul(group, x, X_, Z_2, ctx)) goto err;
571 if (!BN_mod_mul(x, X_, Z_2, &group->field, ctx)) goto err;
577 if (group->meth->field_encode == 0)
579 /* field_mul works on standard representation */
580 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) goto err;
581 if (!group->meth->field_mul(group, y, Y_, Z_3, ctx)) goto err;
586 if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
587 if (!BN_mod_mul(y, Y_, Z_3, &group->field, ctx)) goto err;
597 BN_CTX_free(new_ctx);
602 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
603 const BIGNUM *x_, int y_bit, BN_CTX *ctx)
605 BN_CTX *new_ctx = NULL;
606 BIGNUM *tmp1, *tmp2, *x, *y;
611 ctx = new_ctx = BN_CTX_new();
616 y_bit = (y_bit != 0);
619 tmp1 = BN_CTX_get(ctx);
620 tmp2 = BN_CTX_get(ctx);
623 if (y == NULL) goto err;
625 /* Recover y. We have a Weierstrass equation
626 * y^2 = x^3 + a*x + b,
627 * so y is one of the square roots of x^3 + a*x + b.
631 if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
632 if (group->meth->field_decode == 0)
634 /* field_{sqr,mul} work on standard representation */
635 if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
636 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
640 if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
641 if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
644 /* tmp1 := tmp1 + a*x */
645 if (group->a_is_minus3)
647 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
648 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
649 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
653 if (group->meth->field_decode)
655 if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
656 if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
660 /* field_mul works on standard representation */
661 if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
664 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
667 /* tmp1 := tmp1 + b */
668 if (group->meth->field_decode)
670 if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
671 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
675 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
678 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
680 unsigned long err = ERR_peek_error();
682 if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
684 (void)ERR_get_error();
685 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSED_POINT);
688 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_BN_LIB);
691 /* If tmp1 is not a square (i.e. there is no point on the curve with
692 * our x), then y now is a nonsense value too */
694 if (y_bit != BN_is_odd(y))
700 kron = BN_kronecker(x, &group->field, ctx);
701 if (kron == -2) goto err;
704 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSION_BIT);
706 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSED_POINT);
709 if (!BN_usub(y, &group->field, y)) goto err;
711 if (y_bit != BN_is_odd(y))
713 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_INTERNAL_ERROR);
717 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
724 BN_CTX_free(new_ctx);
729 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
730 unsigned char *buf, size_t len, BN_CTX *ctx)
733 BN_CTX *new_ctx = NULL;
736 size_t field_len, i, skip;
738 if ((form != POINT_CONVERSION_COMPRESSED)
739 && (form != POINT_CONVERSION_UNCOMPRESSED)
740 && (form != POINT_CONVERSION_HYBRID))
742 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
746 if (EC_POINT_is_at_infinity(group, point))
748 /* encodes to a single 0 octet */
753 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
762 /* ret := required output buffer length */
763 field_len = BN_num_bytes(&group->field);
764 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
766 /* if 'buf' is NULL, just return required length */
771 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
777 ctx = new_ctx = BN_CTX_new();
786 if (y == NULL) goto err;
788 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
790 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
797 skip = field_len - BN_num_bytes(x);
798 if (skip > field_len)
800 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
808 skip = BN_bn2bin(x, buf + i);
810 if (i != 1 + field_len)
812 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
816 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
818 skip = field_len - BN_num_bytes(y);
819 if (skip > field_len)
821 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
829 skip = BN_bn2bin(y, buf + i);
835 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
843 BN_CTX_free(new_ctx);
850 BN_CTX_free(new_ctx);
855 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
856 const unsigned char *buf, size_t len, BN_CTX *ctx)
858 point_conversion_form_t form;
860 BN_CTX *new_ctx = NULL;
862 size_t field_len, enc_len;
867 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
873 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
874 && (form != POINT_CONVERSION_UNCOMPRESSED)
875 && (form != POINT_CONVERSION_HYBRID))
877 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
880 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
882 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
890 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
894 return EC_POINT_set_to_infinity(group, point);
897 field_len = BN_num_bytes(&group->field);
898 enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
902 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
908 ctx = new_ctx = BN_CTX_new();
916 if (y == NULL) goto err;
918 if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
919 if (BN_ucmp(x, &group->field) >= 0)
921 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
925 if (form == POINT_CONVERSION_COMPRESSED)
927 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
931 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
932 if (BN_ucmp(y, &group->field) >= 0)
934 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
937 if (form == POINT_CONVERSION_HYBRID)
939 if (y_bit != BN_is_odd(y))
941 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
946 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
949 if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
951 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
960 BN_CTX_free(new_ctx);
965 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
967 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
968 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
970 BN_CTX *new_ctx = NULL;
971 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
975 return EC_POINT_dbl(group, r, a, ctx);
976 if (EC_POINT_is_at_infinity(group, a))
977 return EC_POINT_copy(r, b);
978 if (EC_POINT_is_at_infinity(group, b))
979 return EC_POINT_copy(r, a);
981 field_mul = group->meth->field_mul;
982 field_sqr = group->meth->field_sqr;
987 ctx = new_ctx = BN_CTX_new();
993 n0 = BN_CTX_get(ctx);
994 n1 = BN_CTX_get(ctx);
995 n2 = BN_CTX_get(ctx);
996 n3 = BN_CTX_get(ctx);
997 n4 = BN_CTX_get(ctx);
998 n5 = BN_CTX_get(ctx);
999 n6 = BN_CTX_get(ctx);
1000 if (n6 == NULL) goto end;
1002 /* Note that in this function we must not read components of 'a' or 'b'
1003 * once we have written the corresponding components of 'r'.
1004 * ('r' might be one of 'a' or 'b'.)
1010 if (!BN_copy(n1, &a->X)) goto end;
1011 if (!BN_copy(n2, &a->Y)) goto end;
1017 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
1018 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
1019 /* n1 = X_a * Z_b^2 */
1021 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
1022 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
1023 /* n2 = Y_a * Z_b^3 */
1029 if (!BN_copy(n3, &b->X)) goto end;
1030 if (!BN_copy(n4, &b->Y)) goto end;
1036 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
1037 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
1038 /* n3 = X_b * Z_a^2 */
1040 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
1041 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
1042 /* n4 = Y_b * Z_a^3 */
1046 if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
1047 if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
1055 /* a is the same point as b */
1057 ret = EC_POINT_dbl(group, r, a, ctx);
1063 /* a is the inverse of b */
1064 if (!BN_zero(&r->Z)) goto end;
1072 if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
1073 if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
1074 /* 'n7' = n1 + n3 */
1075 /* 'n8' = n2 + n4 */
1078 if (a->Z_is_one && b->Z_is_one)
1080 if (!BN_copy(&r->Z, n5)) goto end;
1085 { if (!BN_copy(n0, &b->Z)) goto end; }
1086 else if (b->Z_is_one)
1087 { if (!BN_copy(n0, &a->Z)) goto end; }
1089 { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
1090 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
1093 /* Z_r = Z_a * Z_b * n5 */
1096 if (!field_sqr(group, n0, n6, ctx)) goto end;
1097 if (!field_sqr(group, n4, n5, ctx)) goto end;
1098 if (!field_mul(group, n3, n1, n4, ctx)) goto end;
1099 if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
1100 /* X_r = n6^2 - n5^2 * 'n7' */
1103 if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
1104 if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
1105 /* n9 = n5^2 * 'n7' - 2 * X_r */
1108 if (!field_mul(group, n0, n0, n6, ctx)) goto end;
1109 if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
1110 if (!field_mul(group, n1, n2, n5, ctx)) goto end;
1111 if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
1113 if (!BN_add(n0, n0, p)) goto end;
1114 /* now 0 <= n0 < 2*p, and n0 is even */
1115 if (!BN_rshift1(&r->Y, n0)) goto end;
1116 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
1121 if (ctx) /* otherwise we already called BN_CTX_end */
1123 if (new_ctx != NULL)
1124 BN_CTX_free(new_ctx);
1129 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
1131 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1132 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1134 BN_CTX *new_ctx = NULL;
1135 BIGNUM *n0, *n1, *n2, *n3;
1138 if (EC_POINT_is_at_infinity(group, a))
1140 if (!BN_zero(&r->Z)) return 0;
1145 field_mul = group->meth->field_mul;
1146 field_sqr = group->meth->field_sqr;
1151 ctx = new_ctx = BN_CTX_new();
1157 n0 = BN_CTX_get(ctx);
1158 n1 = BN_CTX_get(ctx);
1159 n2 = BN_CTX_get(ctx);
1160 n3 = BN_CTX_get(ctx);
1161 if (n3 == NULL) goto err;
1163 /* Note that in this function we must not read components of 'a'
1164 * once we have written the corresponding components of 'r'.
1165 * ('r' might the same as 'a'.)
1171 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1172 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1173 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1174 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
1175 /* n1 = 3 * X_a^2 + a_curve */
1177 else if (group->a_is_minus3)
1179 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1180 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
1181 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
1182 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
1183 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
1184 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
1185 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
1186 * = 3 * X_a^2 - 3 * Z_a^4 */
1190 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
1191 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
1192 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
1193 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
1194 if (!field_sqr(group, n1, n1, ctx)) goto err;
1195 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
1196 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
1197 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
1203 if (!BN_copy(n0, &a->Y)) goto err;
1207 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
1209 if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
1211 /* Z_r = 2 * Y_a * Z_a */
1214 if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
1215 if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
1216 if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
1217 /* n2 = 4 * X_a * Y_a^2 */
1220 if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
1221 if (!field_sqr(group, &r->X, n1, ctx)) goto err;
1222 if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
1223 /* X_r = n1^2 - 2 * n2 */
1226 if (!field_sqr(group, n0, n3, ctx)) goto err;
1227 if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
1228 /* n3 = 8 * Y_a^4 */
1231 if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
1232 if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1233 if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1234 /* Y_r = n1 * (n2 - X_r) - n3 */
1240 if (new_ctx != NULL)
1241 BN_CTX_free(new_ctx);
1246 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1248 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
1249 /* point is its own inverse */
1252 return BN_usub(&point->Y, &group->field, &point->Y);
1256 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1258 return BN_is_zero(&point->Z);
1262 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1264 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1265 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1267 BN_CTX *new_ctx = NULL;
1268 BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
1271 if (EC_POINT_is_at_infinity(group, point))
1274 field_mul = group->meth->field_mul;
1275 field_sqr = group->meth->field_sqr;
1280 ctx = new_ctx = BN_CTX_new();
1286 rh = BN_CTX_get(ctx);
1287 tmp1 = BN_CTX_get(ctx);
1288 tmp2 = BN_CTX_get(ctx);
1289 Z4 = BN_CTX_get(ctx);
1290 Z6 = BN_CTX_get(ctx);
1291 if (Z6 == NULL) goto err;
1293 /* We have a curve defined by a Weierstrass equation
1294 * y^2 = x^3 + a*x + b.
1295 * The point to consider is given in Jacobian projective coordinates
1296 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1297 * Substituting this and multiplying by Z^6 transforms the above equation into
1298 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1299 * To test this, we add up the right-hand side in 'rh'.
1303 if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1304 if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1306 if (!point->Z_is_one)
1308 if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
1309 if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
1310 if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
1312 /* rh := rh + a*X*Z^4 */
1313 if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
1314 if (group->a_is_minus3)
1316 if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
1317 if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
1318 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1322 if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
1323 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1326 /* rh := rh + b*Z^6 */
1327 if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
1328 if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
1332 /* point->Z_is_one */
1334 /* rh := rh + a*X */
1335 if (group->a_is_minus3)
1337 if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
1338 if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
1339 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1343 if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
1344 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1348 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1352 if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
1354 ret = (0 == BN_cmp(tmp1, rh));
1358 if (new_ctx != NULL)
1359 BN_CTX_free(new_ctx);
1364 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1368 * 0 equal (in affine coordinates)
1372 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1373 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1374 BN_CTX *new_ctx = NULL;
1375 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1376 const BIGNUM *tmp1_, *tmp2_;
1379 if (EC_POINT_is_at_infinity(group, a))
1381 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1384 if (a->Z_is_one && b->Z_is_one)
1386 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
1389 field_mul = group->meth->field_mul;
1390 field_sqr = group->meth->field_sqr;
1394 ctx = new_ctx = BN_CTX_new();
1400 tmp1 = BN_CTX_get(ctx);
1401 tmp2 = BN_CTX_get(ctx);
1402 Za23 = BN_CTX_get(ctx);
1403 Zb23 = BN_CTX_get(ctx);
1404 if (Zb23 == NULL) goto end;
1406 /* We have to decide whether
1407 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1408 * or equivalently, whether
1409 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1414 if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
1415 if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
1422 if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
1423 if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
1429 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1430 if (BN_cmp(tmp1_, tmp2_) != 0)
1432 ret = 1; /* points differ */
1439 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
1440 if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
1447 if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
1448 if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
1454 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1455 if (BN_cmp(tmp1_, tmp2_) != 0)
1457 ret = 1; /* points differ */
1461 /* points are equal */
1466 if (new_ctx != NULL)
1467 BN_CTX_free(new_ctx);
1472 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1474 BN_CTX *new_ctx = NULL;
1478 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1483 ctx = new_ctx = BN_CTX_new();
1489 x = BN_CTX_get(ctx);
1490 y = BN_CTX_get(ctx);
1491 if (y == NULL) goto err;
1493 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1494 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1495 if (!point->Z_is_one)
1497 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1505 if (new_ctx != NULL)
1506 BN_CTX_free(new_ctx);
1511 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1513 BN_CTX *new_ctx = NULL;
1514 BIGNUM *tmp0, *tmp1;
1516 BIGNUM **heap = NULL;
1525 ctx = new_ctx = BN_CTX_new();
1531 tmp0 = BN_CTX_get(ctx);
1532 tmp1 = BN_CTX_get(ctx);
1533 if (tmp0 == NULL || tmp1 == NULL) goto err;
1535 /* Before converting the individual points, compute inverses of all Z values.
1536 * Modular inversion is rather slow, but luckily we can do with a single
1537 * explicit inversion, plus about 3 multiplications per input value.
1543 /* Now pow2 is the smallest power of 2 satifsying pow2 >= num.
1544 * We need twice that. */
1547 heap = OPENSSL_malloc(pow2 * sizeof heap[0]);
1548 if (heap == NULL) goto err;
1550 /* The array is used as a binary tree, exactly as in heapsort:
1554 * heap[4] heap[5] heap[6] heap[7]
1555 * heap[8]heap[9] heap[10]heap[11] heap[12]heap[13] heap[14] heap[15]
1557 * We put the Z's in the last line;
1558 * then we set each other node to the product of its two child-nodes (where
1559 * empty or 0 entries are treated as ones);
1560 * then we invert heap[1];
1561 * then we invert each other node by replacing it by the product of its
1562 * parent (after inversion) and its sibling (before inversion).
1565 for (i = pow2/2 - 1; i > 0; i--)
1567 for (i = 0; i < num; i++)
1568 heap[pow2/2 + i] = &points[i]->Z;
1569 for (i = pow2/2 + num; i < pow2; i++)
1572 /* set each node to the product of its children */
1573 for (i = pow2/2 - 1; i > 0; i--)
1576 if (heap[i] == NULL) goto err;
1578 if (heap[2*i] != NULL)
1580 if ((heap[2*i + 1] == NULL) || BN_is_zero(heap[2*i + 1]))
1582 if (!BN_copy(heap[i], heap[2*i])) goto err;
1586 if (BN_is_zero(heap[2*i]))
1588 if (!BN_copy(heap[i], heap[2*i + 1])) goto err;
1592 if (!group->meth->field_mul(group, heap[i],
1593 heap[2*i], heap[2*i + 1], ctx)) goto err;
1599 /* invert heap[1] */
1600 if (!BN_is_zero(heap[1]))
1602 if (!BN_mod_inverse(heap[1], heap[1], &group->field, ctx))
1604 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1608 if (group->meth->field_encode != 0)
1610 /* in the Montgomery case, we just turned R*H (representing H)
1611 * into 1/(R*H), but we need R*(1/H) (representing 1/H);
1612 * i.e. we have need to multiply by the Montgomery factor twice */
1613 if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
1614 if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
1617 /* set other heap[i]'s to their inverses */
1618 for (i = 2; i < pow2/2 + num; i += 2)
1621 if ((heap[i + 1] != NULL) && !BN_is_zero(heap[i + 1]))
1623 if (!group->meth->field_mul(group, tmp0, heap[i/2], heap[i + 1], ctx)) goto err;
1624 if (!group->meth->field_mul(group, tmp1, heap[i/2], heap[i], ctx)) goto err;
1625 if (!BN_copy(heap[i], tmp0)) goto err;
1626 if (!BN_copy(heap[i + 1], tmp1)) goto err;
1630 if (!BN_copy(heap[i], heap[i/2])) goto err;
1634 /* we have replaced all non-zero Z's by their inverses, now fix up all the points */
1635 for (i = 0; i < num; i++)
1637 EC_POINT *p = points[i];
1639 if (!BN_is_zero(&p->Z))
1641 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1643 if (!group->meth->field_sqr(group, tmp1, &p->Z, ctx)) goto err;
1644 if (!group->meth->field_mul(group, &p->X, &p->X, tmp1, ctx)) goto err;
1646 if (!group->meth->field_mul(group, tmp1, tmp1, &p->Z, ctx)) goto err;
1647 if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp1, ctx)) goto err;
1649 if (group->meth->field_set_to_one != 0)
1651 if (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;
1655 if (!BN_one(&p->Z)) goto err;
1665 if (new_ctx != NULL)
1666 BN_CTX_free(new_ctx);
1669 /* heap[pow2/2] .. heap[pow2-1] have not been allocated locally! */
1670 for (i = pow2/2 - 1; i > 0; i--)
1672 if (heap[i] != NULL)
1673 BN_clear_free(heap[i]);
1681 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1683 return BN_mod_mul(r, a, b, &group->field, ctx);
1687 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1689 return BN_mod_sqr(r, a, &group->field, ctx);