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-2001 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>
63 const EC_METHOD *EC_GFp_simple_method(void)
65 static const EC_METHOD ret = {
66 ec_GFp_simple_group_init,
67 ec_GFp_simple_group_set_curve_GFp,
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_generator,
72 /* TODO: 'set' and 'get' functions for EC_GROUPs */
73 ec_GFp_simple_point_init,
74 ec_GFp_simple_point_finish,
75 ec_GFp_simple_point_clear_finish,
76 ec_GFp_simple_point_copy,
77 ec_GFp_simple_point_set_to_infinity,
78 ec_GFp_simple_set_Jprojective_coordinates_GFp,
79 ec_GFp_simple_get_Jprojective_coordinates_GFp,
80 ec_GFp_simple_point_set_affine_coordinates_GFp,
81 ec_GFp_simple_point_get_affine_coordinates_GFp,
82 ec_GFp_simple_set_compressed_coordinates_GFp,
83 ec_GFp_simple_point2oct,
84 ec_GFp_simple_oct2point,
88 ec_GFp_simple_is_at_infinity,
89 ec_GFp_simple_is_on_curve,
91 ec_GFp_simple_make_affine,
92 ec_GFp_simple_field_mul,
93 ec_GFp_simple_field_sqr,
95 0 /* field_decode */ };
101 int ec_GFp_simple_group_init(EC_GROUP *group)
103 BN_init(&group->field);
106 group->a_is_minus3 = 0;
107 group->generator = NULL;
108 BN_init(&group->order);
109 BN_init(&group->cofactor);
114 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
115 const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
118 BN_CTX *new_ctx = NULL;
121 /* p must be a prime > 3 */
122 if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
124 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP, EC_R_INVALID_FIELD);
130 ctx = new_ctx = BN_CTX_new();
136 tmp_a = BN_CTX_get(ctx);
137 if (tmp_a == NULL) goto err;
140 if (!BN_copy(&group->field, p)) goto err;
141 group->field.neg = 0;
144 if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
145 if (group->meth->field_encode)
146 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }
148 if (!BN_copy(&group->a, tmp_a)) goto err;
151 if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
152 if (group->meth->field_encode)
153 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
155 /* group->a_is_minus3 */
156 if (!BN_add_word(tmp_a, 3)) goto err;
157 group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
164 BN_CTX_free(new_ctx);
169 void ec_GFp_simple_group_finish(EC_GROUP *group)
171 BN_free(&group->field);
174 if (group->generator != NULL)
175 EC_POINT_free(group->generator);
176 BN_free(&group->order);
177 BN_free(&group->cofactor);
181 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
183 BN_clear_free(&group->field);
184 BN_clear_free(&group->a);
185 BN_clear_free(&group->b);
186 if (group->generator != NULL)
188 EC_POINT_clear_free(group->generator);
189 group->generator = NULL;
191 BN_clear_free(&group->order);
192 BN_clear_free(&group->cofactor);
196 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
198 if (!BN_copy(&dest->field, &src->field)) return 0;
199 if (!BN_copy(&dest->a, &src->a)) return 0;
200 if (!BN_copy(&dest->b, &src->b)) return 0;
202 dest->a_is_minus3 = src->a_is_minus3;
204 if (src->generator != NULL)
206 if (dest->generator == NULL)
208 dest->generator = EC_POINT_new(dest);
209 if (dest->generator == NULL) return 0;
211 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
215 /* src->generator == NULL */
216 if (dest->generator != NULL)
218 EC_POINT_clear_free(dest->generator);
219 dest->generator = NULL;
223 if (!BN_copy(&dest->order, &src->order)) return 0;
224 if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
230 int ec_GFp_simple_group_set_generator(EC_GROUP *group, const EC_POINT *generator,
231 const BIGNUM *order, const BIGNUM *cofactor)
235 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
239 if (group->generator == NULL)
241 group->generator = EC_POINT_new(group);
242 if (group->generator == NULL) return 0;
244 if (!EC_POINT_copy(group->generator, generator)) return 0;
247 { if (!BN_copy(&group->order, order)) return 0; }
249 { if (!BN_zero(&group->order)) return 0; }
251 if (cofactor != NULL)
252 { if (!BN_copy(&group->cofactor, cofactor)) return 0; }
254 { if (!BN_zero(&group->cofactor)) return 0; }
260 /* TODO: 'set' and 'get' functions for EC_GROUPs */
263 int ec_GFp_simple_point_init(EC_POINT *point)
274 void ec_GFp_simple_point_finish(EC_POINT *point)
282 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
284 BN_clear_free(&point->X);
285 BN_clear_free(&point->Y);
286 BN_clear_free(&point->Z);
291 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
293 if (!BN_copy(&dest->X, &src->X)) return 0;
294 if (!BN_copy(&dest->Y, &src->Y)) return 0;
295 if (!BN_copy(&dest->Z, &src->Z)) return 0;
296 dest->Z_is_one = src->Z_is_one;
302 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
305 return (BN_zero(&point->Z));
309 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
310 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx);
314 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
315 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx);
319 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
320 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
322 BN_CTX *new_ctx = NULL;
325 if (!BN_copy(&point->X, x)) goto err;
326 if (!BN_copy(&point->Y, y)) goto err;
327 if (!BN_one(&point->Z)) goto err;
329 if (group->meth->field_encode)
333 ctx = new_ctx = BN_CTX_new();
338 if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
339 if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
340 if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
347 BN_CTX_free(new_ctx);
352 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
353 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
355 BN_CTX *new_ctx = NULL;
356 BIGNUM *X, *Y, *Z, *Z_1, *Z_2, *Z_3;
357 const BIGNUM *X_, *Y_, *Z_;
360 if (EC_POINT_is_at_infinity(group, point))
362 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_POINT_AT_INFINITY);
368 ctx = new_ctx = BN_CTX_new();
377 Z_1 = BN_CTX_get(ctx);
378 Z_2 = BN_CTX_get(ctx);
379 Z_3 = BN_CTX_get(ctx);
380 if (Z_3 == NULL) goto err;
382 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
384 if (group->meth->field_decode)
386 if (!group->meth->field_decode(group, X, &point->X, ctx)) goto err;
387 if (!group->meth->field_decode(group, Y, &point->Y, ctx)) goto err;
388 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
389 X_ = X; Y_ = Y; Z_ = Z;
402 if (!BN_copy(x, X_)) goto err;
406 if (!BN_copy(y, Y_)) goto err;
411 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
413 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_BN_LIB);
416 if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
420 if (!BN_mod_mul(x, X_, Z_2, &group->field, ctx)) goto err;
425 if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
426 if (!BN_mod_mul(y, Y_, Z_3, &group->field, ctx)) goto err;
435 BN_CTX_free(new_ctx);
440 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
441 const BIGNUM *x, int y_bit, BN_CTX *);
445 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
446 unsigned char *buf, size_t len, BN_CTX *ctx)
449 BN_CTX *new_ctx = NULL;
452 size_t field_len, i, skip;
454 if ((form != POINT_CONVERSION_COMPRESSED)
455 && (form != POINT_CONVERSION_UNCOMPRESSED)
456 && (form != POINT_CONVERSION_HYBRID))
458 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
462 if (EC_POINT_is_at_infinity(group, point))
464 /* encodes to a single 0 octet */
469 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
478 /* ret := required output buffer length */
479 field_len = BN_num_bytes(&group->field);
480 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
482 /* if 'buf' is NULL, just return required length */
487 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
493 ctx = new_ctx = BN_CTX_new();
502 if (y == NULL) goto err;
504 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
506 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
513 skip = field_len - BN_num_bytes(x);
514 if (skip > field_len)
516 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
524 skip = BN_bn2bin(x, buf + i);
526 if (i != 1 + field_len)
528 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
532 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
534 skip = field_len - BN_num_bytes(y);
535 if (skip > field_len)
537 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
545 skip = BN_bn2bin(y, buf + i);
551 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
559 BN_CTX_free(new_ctx);
566 BN_CTX_free(new_ctx);
571 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
572 const unsigned char *buf, size_t len, BN_CTX *ctx)
574 point_conversion_form_t form;
576 BN_CTX *new_ctx = NULL;
578 size_t field_len, enc_len;
583 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
589 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
590 && (form != POINT_CONVERSION_UNCOMPRESSED)
591 && (form != POINT_CONVERSION_HYBRID))
593 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
596 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
598 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
606 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
610 return EC_POINT_set_to_infinity(group, point);
613 field_len = BN_num_bytes(&group->field);
614 enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
618 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
624 ctx = new_ctx = BN_CTX_new();
632 if (y == NULL) goto err;
634 if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
635 if (BN_ucmp(x, &group->field) >= 0)
637 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
641 if (form != POINT_CONVERSION_COMPRESSED)
643 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
644 if (BN_ucmp(y, &group->field) >= 0)
646 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
649 if (form == POINT_CONVERSION_HYBRID)
651 if (y_bit != BN_is_odd(y))
653 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
659 if (form == POINT_CONVERSION_COMPRESSED)
661 /* Recover y. We have a Weierstrass equation
662 * y^2 = x^3 + a*x + b,
663 * so y is one of the square roots of x^3 + a*x + b.
668 tmp1 = BN_CTX_get(ctx);
669 tmp2 = BN_CTX_get(ctx);
670 if (tmp2 == NULL) goto err;
673 if (!BN_mod_sqr(tmp2, x, &group->field, ctx)) goto err;
674 if (!BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) goto err;
676 /* tmp1 := tmp1 + a*x */
677 if (group->a_is_minus3)
679 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
680 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
681 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
685 if (!BN_mod_mul(tmp2, &group->a, x, &group->field, ctx)) goto err;
686 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
689 /* tmp1 := tmp1 + b */
690 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
692 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
694 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, ERR_R_BN_LIB);
698 if (y_bit != BN_is_odd(y))
702 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
705 if (!BN_usub(y, &group->field, y)) goto err;
707 if (y_bit != BN_is_odd(y))
709 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, ERR_R_INTERNAL_ERROR);
714 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
716 if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
718 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
727 BN_CTX_free(new_ctx);
732 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
734 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
735 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
737 BN_CTX *new_ctx = NULL;
738 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
742 return EC_POINT_dbl(group, r, a, ctx);
743 if (EC_POINT_is_at_infinity(group, a))
744 return EC_POINT_copy(r, b);
745 if (EC_POINT_is_at_infinity(group, b))
746 return EC_POINT_copy(r, a);
748 field_mul = group->meth->field_mul;
749 field_sqr = group->meth->field_sqr;
754 ctx = new_ctx = BN_CTX_new();
760 n0 = BN_CTX_get(ctx);
761 n1 = BN_CTX_get(ctx);
762 n2 = BN_CTX_get(ctx);
763 n3 = BN_CTX_get(ctx);
764 n4 = BN_CTX_get(ctx);
765 n5 = BN_CTX_get(ctx);
766 n6 = BN_CTX_get(ctx);
767 if (n6 == NULL) goto end;
769 /* Note that in this function we must not read components of 'a' or 'b'
770 * once we have written the corresponding components of 'r'.
771 * ('r' might be one of 'a' or 'b'.)
777 if (!BN_copy(n1, &a->X)) goto end;
778 if (!BN_copy(n2, &a->Y)) goto end;
784 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
785 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
786 /* n1 = X_a * Z_b^2 */
788 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
789 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
790 /* n2 = Y_a * Z_b^3 */
796 if (!BN_copy(n3, &b->X)) goto end;
797 if (!BN_copy(n4, &b->Y)) goto end;
803 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
804 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
805 /* n3 = X_b * Z_a^2 */
807 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
808 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
809 /* n4 = Y_b * Z_a^3 */
813 if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
814 if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
822 /* a is the same point as b */
824 ret = EC_POINT_dbl(group, r, a, ctx);
830 /* a is the inverse of b */
831 if (!BN_zero(&r->Z)) goto end;
839 if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
840 if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
845 if (a->Z_is_one && b->Z_is_one)
847 if (!BN_copy(&r->Z, n5)) goto end;
852 { if (!BN_copy(n0, &b->Z)) goto end; }
853 else if (b->Z_is_one)
854 { if (!BN_copy(n0, &a->Z)) goto end; }
856 { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
857 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
860 /* Z_r = Z_a * Z_b * n5 */
863 if (!field_sqr(group, n0, n6, ctx)) goto end;
864 if (!field_sqr(group, n4, n5, ctx)) goto end;
865 if (!field_mul(group, n3, n1, n4, ctx)) goto end;
866 if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
867 /* X_r = n6^2 - n5^2 * 'n7' */
870 if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
871 if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
872 /* n9 = n5^2 * 'n7' - 2 * X_r */
875 if (!field_mul(group, n0, n0, n6, ctx)) goto end;
876 if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
877 if (!field_mul(group, n1, n2, n5, ctx)) goto end;
878 if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
880 if (!BN_add(n0, n0, p)) goto end;
881 /* now 0 <= n0 < 2*p, and n0 is even */
882 if (!BN_rshift1(&r->Y, n0)) goto end;
883 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
888 if (ctx) /* otherwise we already called BN_CTX_end */
891 BN_CTX_free(new_ctx);
896 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
898 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
899 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
901 BN_CTX *new_ctx = NULL;
902 BIGNUM *n0, *n1, *n2, *n3;
905 if (EC_POINT_is_at_infinity(group, a))
907 if (!BN_zero(&r->Z)) return 0;
912 field_mul = group->meth->field_mul;
913 field_sqr = group->meth->field_sqr;
918 ctx = new_ctx = BN_CTX_new();
924 n0 = BN_CTX_get(ctx);
925 n1 = BN_CTX_get(ctx);
926 n2 = BN_CTX_get(ctx);
927 n3 = BN_CTX_get(ctx);
928 if (n3 == NULL) goto err;
930 /* Note that in this function we must not read components of 'a'
931 * once we have written the corresponding components of 'r'.
932 * ('r' might the same as 'a'.)
938 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
939 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
940 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
941 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
942 /* n1 = 3 * X_a^2 + a_curve */
944 else if (group->a_is_minus3)
946 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
947 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
948 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
949 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
950 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
951 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
952 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
953 * = 3 * X_a^2 - 3 * Z_a^4 */
957 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
958 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
959 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
960 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
961 if (!field_sqr(group, n1, n1, ctx)) goto err;
962 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
963 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
964 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
970 if (!BN_copy(n0, &a->Y)) goto err;
974 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
976 if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
978 /* Z_r = 2 * Y_a * Z_a */
981 if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
982 if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
983 if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
984 /* n2 = 4 * X_a * Y_a^2 */
987 if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
988 if (!field_sqr(group, &r->X, n1, ctx)) goto err;
989 if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
990 /* X_r = n1^2 - 2 * n2 */
993 if (!field_sqr(group, n0, n3, ctx)) goto err;
994 if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
998 if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
999 if (!field_mul(group, n0, n1, n0, ctx)) goto err;
1000 if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
1001 /* Y_r = n1 * (n2 - X_r) - n3 */
1007 if (new_ctx != NULL)
1008 BN_CTX_free(new_ctx);
1013 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
1017 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1019 return BN_is_zero(&point->Z);
1023 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1025 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1026 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1028 BN_CTX *new_ctx = NULL;
1029 BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
1032 if (EC_POINT_is_at_infinity(group, point))
1035 field_mul = group->meth->field_mul;
1036 field_sqr = group->meth->field_sqr;
1041 ctx = new_ctx = BN_CTX_new();
1047 rh = BN_CTX_get(ctx);
1048 tmp1 = BN_CTX_get(ctx);
1049 tmp2 = BN_CTX_get(ctx);
1050 Z4 = BN_CTX_get(ctx);
1051 Z6 = BN_CTX_get(ctx);
1052 if (Z6 == NULL) goto err;
1054 /* We have a curve defined by a Weierstrass equation
1055 * y^2 = x^3 + a*x + b.
1056 * The point to consider is given in Jacobian projective coordinates
1057 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1058 * Substituting this and multiplying by Z^6 transforms the above equation into
1059 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1060 * To test this, we add up the right-hand side in 'rh'.
1064 if (!field_sqr(group, rh, &point->X, ctx)) goto err;
1065 if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1067 if (!point->Z_is_one)
1069 if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
1070 if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
1071 if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
1073 /* rh := rh + a*X*Z^4 */
1074 if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
1075 if (&group->a_is_minus3)
1077 if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
1078 if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
1079 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1083 if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
1084 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1087 /* rh := rh + b*Z^6 */
1088 if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
1089 if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
1093 /* point->Z_is_one */
1095 /* rh := rh + a*X */
1096 if (&group->a_is_minus3)
1098 if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
1099 if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
1100 if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
1104 if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
1105 if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
1109 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1113 if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
1115 ret = (0 == BN_cmp(tmp1, rh));
1119 if (new_ctx != NULL)
1120 BN_CTX_free(new_ctx);
1125 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
1129 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1131 BN_CTX *new_ctx = NULL;
1135 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1140 ctx = new_ctx = BN_CTX_new();
1146 x = BN_CTX_get(ctx);
1147 y = BN_CTX_get(ctx);
1148 if (y == NULL) goto err;
1150 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1151 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1152 if (!point->Z_is_one)
1154 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1162 if (new_ctx != NULL)
1163 BN_CTX_free(new_ctx);
1168 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1170 return BN_mod_mul(r, a, b, &group->field, ctx);
1174 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1176 return BN_mod_sqr(r, a, &group->field, ctx);