You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1386 lines
35 KiB

  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <string.h>
  69. #include <openssl/bn.h>
  70. #include <openssl/err.h>
  71. #include <openssl/mem.h>
  72. #include "internal.h"
  73. const EC_METHOD *EC_GFp_simple_method(void) {
  74. static const EC_METHOD ret = {EC_FLAGS_DEFAULT_OCT,
  75. ec_GFp_simple_group_init,
  76. ec_GFp_simple_group_finish,
  77. ec_GFp_simple_group_clear_finish,
  78. ec_GFp_simple_group_copy,
  79. ec_GFp_simple_group_set_curve,
  80. ec_GFp_simple_group_get_curve,
  81. ec_GFp_simple_group_get_degree,
  82. ec_GFp_simple_group_check_discriminant,
  83. ec_GFp_simple_point_init,
  84. ec_GFp_simple_point_finish,
  85. ec_GFp_simple_point_clear_finish,
  86. ec_GFp_simple_point_copy,
  87. ec_GFp_simple_point_set_to_infinity,
  88. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  89. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  90. ec_GFp_simple_point_set_affine_coordinates,
  91. ec_GFp_simple_point_get_affine_coordinates,
  92. 0,
  93. 0,
  94. 0,
  95. ec_GFp_simple_add,
  96. ec_GFp_simple_dbl,
  97. ec_GFp_simple_invert,
  98. ec_GFp_simple_is_at_infinity,
  99. ec_GFp_simple_is_on_curve,
  100. ec_GFp_simple_cmp,
  101. ec_GFp_simple_make_affine,
  102. ec_GFp_simple_points_make_affine,
  103. 0 /* mul */,
  104. 0 /* precompute_mult */,
  105. 0 /* have_precompute_mult */,
  106. ec_GFp_simple_field_mul,
  107. ec_GFp_simple_field_sqr,
  108. 0 /* field_div */,
  109. 0 /* field_encode */,
  110. 0 /* field_decode */,
  111. 0 /* field_set_to_one */};
  112. return &ret;
  113. }
  114. /* Most method functions in this file are designed to work with non-trivial
  115. * representations of field elements if necessary (see ecp_mont.c): while
  116. * standard modular addition and subtraction are used, the field_mul and
  117. * field_sqr methods will be used for multiplication, and field_encode and
  118. * field_decode (if defined) will be used for converting between
  119. * representations.
  120. * Functions ec_GFp_simple_points_make_affine() and
  121. * ec_GFp_simple_point_get_affine_coordinates() specifically assume that if a
  122. * non-trivial representation is used, it is a Montgomery representation (i.e.
  123. * 'encoding' means multiplying by some factor R). */
  124. int ec_GFp_simple_group_init(EC_GROUP *group) {
  125. BN_init(&group->field);
  126. BN_init(&group->a);
  127. BN_init(&group->b);
  128. group->a_is_minus3 = 0;
  129. return 1;
  130. }
  131. void ec_GFp_simple_group_finish(EC_GROUP *group) {
  132. BN_free(&group->field);
  133. BN_free(&group->a);
  134. BN_free(&group->b);
  135. }
  136. void ec_GFp_simple_group_clear_finish(EC_GROUP *group) {
  137. BN_clear_free(&group->field);
  138. BN_clear_free(&group->a);
  139. BN_clear_free(&group->b);
  140. }
  141. int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
  142. if (!BN_copy(&dest->field, &src->field) ||
  143. !BN_copy(&dest->a, &src->a) ||
  144. !BN_copy(&dest->b, &src->b)) {
  145. return 0;
  146. }
  147. dest->a_is_minus3 = src->a_is_minus3;
  148. return 1;
  149. }
  150. int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  151. const BIGNUM *a, const BIGNUM *b,
  152. BN_CTX *ctx) {
  153. int ret = 0;
  154. BN_CTX *new_ctx = NULL;
  155. BIGNUM *tmp_a;
  156. /* p must be a prime > 3 */
  157. if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
  158. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_group_set_curve, EC_R_INVALID_FIELD);
  159. return 0;
  160. }
  161. if (ctx == NULL) {
  162. ctx = new_ctx = BN_CTX_new();
  163. if (ctx == NULL) {
  164. return 0;
  165. }
  166. }
  167. BN_CTX_start(ctx);
  168. tmp_a = BN_CTX_get(ctx);
  169. if (tmp_a == NULL) {
  170. goto err;
  171. }
  172. /* group->field */
  173. if (!BN_copy(&group->field, p)) {
  174. goto err;
  175. }
  176. BN_set_negative(&group->field, 0);
  177. /* group->a */
  178. if (!BN_nnmod(tmp_a, a, p, ctx)) {
  179. goto err;
  180. }
  181. if (group->meth->field_encode) {
  182. if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) {
  183. goto err;
  184. }
  185. } else if (!BN_copy(&group->a, tmp_a)) {
  186. goto err;
  187. }
  188. /* group->b */
  189. if (!BN_nnmod(&group->b, b, p, ctx)) {
  190. goto err;
  191. }
  192. if (group->meth->field_encode &&
  193. !group->meth->field_encode(group, &group->b, &group->b, ctx)) {
  194. goto err;
  195. }
  196. /* group->a_is_minus3 */
  197. if (!BN_add_word(tmp_a, 3)) {
  198. goto err;
  199. }
  200. group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
  201. ret = 1;
  202. err:
  203. BN_CTX_end(ctx);
  204. if (new_ctx != NULL) {
  205. BN_CTX_free(new_ctx);
  206. }
  207. return ret;
  208. }
  209. int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  210. BIGNUM *b, BN_CTX *ctx) {
  211. int ret = 0;
  212. BN_CTX *new_ctx = NULL;
  213. if (p != NULL && !BN_copy(p, &group->field)) {
  214. return 0;
  215. }
  216. if (a != NULL || b != NULL) {
  217. if (group->meth->field_decode) {
  218. if (ctx == NULL) {
  219. ctx = new_ctx = BN_CTX_new();
  220. if (ctx == NULL) {
  221. return 0;
  222. }
  223. }
  224. if (a != NULL && !group->meth->field_decode(group, a, &group->a, ctx)) {
  225. goto err;
  226. }
  227. if (b != NULL && !group->meth->field_decode(group, b, &group->b, ctx)) {
  228. goto err;
  229. }
  230. } else {
  231. if (a != NULL && !BN_copy(a, &group->a)) {
  232. goto err;
  233. }
  234. if (b != NULL && !BN_copy(b, &group->b)) {
  235. goto err;
  236. }
  237. }
  238. }
  239. ret = 1;
  240. err:
  241. if (new_ctx) {
  242. BN_CTX_free(new_ctx);
  243. }
  244. return ret;
  245. }
  246. int ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
  247. return BN_num_bits(&group->field);
  248. }
  249. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) {
  250. int ret = 0;
  251. BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
  252. const BIGNUM *p = &group->field;
  253. BN_CTX *new_ctx = NULL;
  254. if (ctx == NULL) {
  255. ctx = new_ctx = BN_CTX_new();
  256. if (ctx == NULL) {
  257. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_group_check_discriminant,
  258. ERR_R_MALLOC_FAILURE);
  259. goto err;
  260. }
  261. }
  262. BN_CTX_start(ctx);
  263. a = BN_CTX_get(ctx);
  264. b = BN_CTX_get(ctx);
  265. tmp_1 = BN_CTX_get(ctx);
  266. tmp_2 = BN_CTX_get(ctx);
  267. order = BN_CTX_get(ctx);
  268. if (order == NULL) {
  269. goto err;
  270. }
  271. if (group->meth->field_decode) {
  272. if (!group->meth->field_decode(group, a, &group->a, ctx) ||
  273. !group->meth->field_decode(group, b, &group->b, ctx)) {
  274. goto err;
  275. }
  276. } else {
  277. if (!BN_copy(a, &group->a) || !BN_copy(b, &group->b)) {
  278. goto err;
  279. }
  280. }
  281. /* check the discriminant:
  282. * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
  283. * 0 =< a, b < p */
  284. if (BN_is_zero(a)) {
  285. if (BN_is_zero(b)) {
  286. goto err;
  287. }
  288. } else if (!BN_is_zero(b)) {
  289. if (!BN_mod_sqr(tmp_1, a, p, ctx) ||
  290. !BN_mod_mul(tmp_2, tmp_1, a, p, ctx) ||
  291. !BN_lshift(tmp_1, tmp_2, 2)) {
  292. goto err;
  293. }
  294. /* tmp_1 = 4*a^3 */
  295. if (!BN_mod_sqr(tmp_2, b, p, ctx) ||
  296. !BN_mul_word(tmp_2, 27)) {
  297. goto err;
  298. }
  299. /* tmp_2 = 27*b^2 */
  300. if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx) ||
  301. BN_is_zero(a)) {
  302. goto err;
  303. }
  304. }
  305. ret = 1;
  306. err:
  307. if (ctx != NULL) {
  308. BN_CTX_end(ctx);
  309. }
  310. if (new_ctx != NULL) {
  311. BN_CTX_free(new_ctx);
  312. }
  313. return ret;
  314. }
  315. int ec_GFp_simple_point_init(EC_POINT *point) {
  316. BN_init(&point->X);
  317. BN_init(&point->Y);
  318. BN_init(&point->Z);
  319. point->Z_is_one = 0;
  320. return 1;
  321. }
  322. void ec_GFp_simple_point_finish(EC_POINT *point) {
  323. BN_free(&point->X);
  324. BN_free(&point->Y);
  325. BN_free(&point->Z);
  326. }
  327. void ec_GFp_simple_point_clear_finish(EC_POINT *point) {
  328. BN_clear_free(&point->X);
  329. BN_clear_free(&point->Y);
  330. BN_clear_free(&point->Z);
  331. point->Z_is_one = 0;
  332. }
  333. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
  334. if (!BN_copy(&dest->X, &src->X) ||
  335. !BN_copy(&dest->Y, &src->Y) ||
  336. !BN_copy(&dest->Z, &src->Z)) {
  337. return 0;
  338. }
  339. dest->Z_is_one = src->Z_is_one;
  340. return 1;
  341. }
  342. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
  343. EC_POINT *point) {
  344. point->Z_is_one = 0;
  345. BN_zero(&point->Z);
  346. return 1;
  347. }
  348. int ec_GFp_simple_set_Jprojective_coordinates_GFp(
  349. const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y,
  350. const BIGNUM *z, BN_CTX *ctx) {
  351. BN_CTX *new_ctx = NULL;
  352. int ret = 0;
  353. if (ctx == NULL) {
  354. ctx = new_ctx = BN_CTX_new();
  355. if (ctx == NULL) {
  356. return 0;
  357. }
  358. }
  359. if (x != NULL) {
  360. if (!BN_nnmod(&point->X, x, &group->field, ctx)) {
  361. goto err;
  362. }
  363. if (group->meth->field_encode &&
  364. !group->meth->field_encode(group, &point->X, &point->X, ctx)) {
  365. goto err;
  366. }
  367. }
  368. if (y != NULL) {
  369. if (!BN_nnmod(&point->Y, y, &group->field, ctx)) {
  370. goto err;
  371. }
  372. if (group->meth->field_encode &&
  373. !group->meth->field_encode(group, &point->Y, &point->Y, ctx)) {
  374. goto err;
  375. }
  376. }
  377. if (z != NULL) {
  378. int Z_is_one;
  379. if (!BN_nnmod(&point->Z, z, &group->field, ctx)) {
  380. goto err;
  381. }
  382. Z_is_one = BN_is_one(&point->Z);
  383. if (group->meth->field_encode) {
  384. if (Z_is_one && (group->meth->field_set_to_one != 0)) {
  385. if (!group->meth->field_set_to_one(group, &point->Z, ctx)) {
  386. goto err;
  387. }
  388. } else if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) {
  389. goto err;
  390. }
  391. }
  392. point->Z_is_one = Z_is_one;
  393. }
  394. ret = 1;
  395. err:
  396. if (new_ctx != NULL) {
  397. BN_CTX_free(new_ctx);
  398. }
  399. return ret;
  400. }
  401. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  402. const EC_POINT *point,
  403. BIGNUM *x, BIGNUM *y,
  404. BIGNUM *z, BN_CTX *ctx) {
  405. BN_CTX *new_ctx = NULL;
  406. int ret = 0;
  407. if (group->meth->field_decode != 0) {
  408. if (ctx == NULL) {
  409. ctx = new_ctx = BN_CTX_new();
  410. if (ctx == NULL) {
  411. return 0;
  412. }
  413. }
  414. if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
  415. goto err;
  416. }
  417. if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
  418. goto err;
  419. }
  420. if (z != NULL && !group->meth->field_decode(group, z, &point->Z, ctx)) {
  421. goto err;
  422. }
  423. } else {
  424. if (x != NULL && !BN_copy(x, &point->X)) {
  425. goto err;
  426. }
  427. if (y != NULL && !BN_copy(y, &point->Y)) {
  428. goto err;
  429. }
  430. if (z != NULL && !BN_copy(z, &point->Z)) {
  431. goto err;
  432. }
  433. }
  434. ret = 1;
  435. err:
  436. if (new_ctx != NULL) {
  437. BN_CTX_free(new_ctx);
  438. }
  439. return ret;
  440. }
  441. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
  442. EC_POINT *point, const BIGNUM *x,
  443. const BIGNUM *y, BN_CTX *ctx) {
  444. if (x == NULL || y == NULL) {
  445. /* unlike for projective coordinates, we do not tolerate this */
  446. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point_set_affine_coordinates,
  447. ERR_R_PASSED_NULL_PARAMETER);
  448. return 0;
  449. }
  450. return ec_point_set_Jprojective_coordinates_GFp(group, point, x, y,
  451. BN_value_one(), ctx);
  452. }
  453. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
  454. const EC_POINT *point, BIGNUM *x,
  455. BIGNUM *y, BN_CTX *ctx) {
  456. BN_CTX *new_ctx = NULL;
  457. BIGNUM *Z, *Z_1, *Z_2, *Z_3;
  458. const BIGNUM *Z_;
  459. int ret = 0;
  460. if (EC_POINT_is_at_infinity(group, point)) {
  461. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point_get_affine_coordinates,
  462. EC_R_POINT_AT_INFINITY);
  463. return 0;
  464. }
  465. if (ctx == NULL) {
  466. ctx = new_ctx = BN_CTX_new();
  467. if (ctx == NULL) {
  468. return 0;
  469. }
  470. }
  471. BN_CTX_start(ctx);
  472. Z = BN_CTX_get(ctx);
  473. Z_1 = BN_CTX_get(ctx);
  474. Z_2 = BN_CTX_get(ctx);
  475. Z_3 = BN_CTX_get(ctx);
  476. if (Z_3 == NULL) {
  477. goto err;
  478. }
  479. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  480. if (group->meth->field_decode) {
  481. if (!group->meth->field_decode(group, Z, &point->Z, ctx)) {
  482. goto err;
  483. }
  484. Z_ = Z;
  485. } else {
  486. Z_ = &point->Z;
  487. }
  488. if (BN_is_one(Z_)) {
  489. if (group->meth->field_decode) {
  490. if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
  491. goto err;
  492. }
  493. if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
  494. goto err;
  495. }
  496. } else {
  497. if (x != NULL && !BN_copy(x, &point->X)) {
  498. goto err;
  499. }
  500. if (y != NULL && !BN_copy(y, &point->Y)) {
  501. goto err;
  502. }
  503. }
  504. } else {
  505. if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx)) {
  506. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point_get_affine_coordinates,
  507. ERR_R_BN_LIB);
  508. goto err;
  509. }
  510. if (group->meth->field_encode == 0) {
  511. /* field_sqr works on standard representation */
  512. if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) {
  513. goto err;
  514. }
  515. } else if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) {
  516. goto err;
  517. }
  518. /* in the Montgomery case, field_mul will cancel out Montgomery factor in
  519. * X: */
  520. if (x != NULL && !group->meth->field_mul(group, x, &point->X, Z_2, ctx)) {
  521. goto err;
  522. }
  523. if (y != NULL) {
  524. if (group->meth->field_encode == 0) {
  525. /* field_mul works on standard representation */
  526. if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) {
  527. goto err;
  528. }
  529. } else if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) {
  530. goto err;
  531. }
  532. /* in the Montgomery case, field_mul will cancel out Montgomery factor in
  533. * Y: */
  534. if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) {
  535. goto err;
  536. }
  537. }
  538. }
  539. ret = 1;
  540. err:
  541. BN_CTX_end(ctx);
  542. if (new_ctx != NULL) {
  543. BN_CTX_free(new_ctx);
  544. }
  545. return ret;
  546. }
  547. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  548. const EC_POINT *b, BN_CTX *ctx) {
  549. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  550. BN_CTX *);
  551. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  552. const BIGNUM *p;
  553. BN_CTX *new_ctx = NULL;
  554. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  555. int ret = 0;
  556. if (a == b) {
  557. return EC_POINT_dbl(group, r, a, ctx);
  558. }
  559. if (EC_POINT_is_at_infinity(group, a)) {
  560. return EC_POINT_copy(r, b);
  561. }
  562. if (EC_POINT_is_at_infinity(group, b)) {
  563. return EC_POINT_copy(r, a);
  564. }
  565. field_mul = group->meth->field_mul;
  566. field_sqr = group->meth->field_sqr;
  567. p = &group->field;
  568. if (ctx == NULL) {
  569. ctx = new_ctx = BN_CTX_new();
  570. if (ctx == NULL) {
  571. return 0;
  572. }
  573. }
  574. BN_CTX_start(ctx);
  575. n0 = BN_CTX_get(ctx);
  576. n1 = BN_CTX_get(ctx);
  577. n2 = BN_CTX_get(ctx);
  578. n3 = BN_CTX_get(ctx);
  579. n4 = BN_CTX_get(ctx);
  580. n5 = BN_CTX_get(ctx);
  581. n6 = BN_CTX_get(ctx);
  582. if (n6 == NULL) {
  583. goto end;
  584. }
  585. /* Note that in this function we must not read components of 'a' or 'b'
  586. * once we have written the corresponding components of 'r'.
  587. * ('r' might be one of 'a' or 'b'.)
  588. */
  589. /* n1, n2 */
  590. if (b->Z_is_one) {
  591. if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
  592. goto end;
  593. }
  594. /* n1 = X_a */
  595. /* n2 = Y_a */
  596. } else {
  597. if (!field_sqr(group, n0, &b->Z, ctx) ||
  598. !field_mul(group, n1, &a->X, n0, ctx)) {
  599. goto end;
  600. }
  601. /* n1 = X_a * Z_b^2 */
  602. if (!field_mul(group, n0, n0, &b->Z, ctx) ||
  603. !field_mul(group, n2, &a->Y, n0, ctx)) {
  604. goto end;
  605. }
  606. /* n2 = Y_a * Z_b^3 */
  607. }
  608. /* n3, n4 */
  609. if (a->Z_is_one) {
  610. if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
  611. goto end;
  612. }
  613. /* n3 = X_b */
  614. /* n4 = Y_b */
  615. } else {
  616. if (!field_sqr(group, n0, &a->Z, ctx) ||
  617. !field_mul(group, n3, &b->X, n0, ctx)) {
  618. goto end;
  619. }
  620. /* n3 = X_b * Z_a^2 */
  621. if (!field_mul(group, n0, n0, &a->Z, ctx) ||
  622. !field_mul(group, n4, &b->Y, n0, ctx)) {
  623. goto end;
  624. }
  625. /* n4 = Y_b * Z_a^3 */
  626. }
  627. /* n5, n6 */
  628. if (!BN_mod_sub_quick(n5, n1, n3, p) ||
  629. !BN_mod_sub_quick(n6, n2, n4, p)) {
  630. goto end;
  631. }
  632. /* n5 = n1 - n3 */
  633. /* n6 = n2 - n4 */
  634. if (BN_is_zero(n5)) {
  635. if (BN_is_zero(n6)) {
  636. /* a is the same point as b */
  637. BN_CTX_end(ctx);
  638. ret = EC_POINT_dbl(group, r, a, ctx);
  639. ctx = NULL;
  640. goto end;
  641. } else {
  642. /* a is the inverse of b */
  643. BN_zero(&r->Z);
  644. r->Z_is_one = 0;
  645. ret = 1;
  646. goto end;
  647. }
  648. }
  649. /* 'n7', 'n8' */
  650. if (!BN_mod_add_quick(n1, n1, n3, p) ||
  651. !BN_mod_add_quick(n2, n2, n4, p)) {
  652. goto end;
  653. }
  654. /* 'n7' = n1 + n3 */
  655. /* 'n8' = n2 + n4 */
  656. /* Z_r */
  657. if (a->Z_is_one && b->Z_is_one) {
  658. if (!BN_copy(&r->Z, n5)) {
  659. goto end;
  660. }
  661. } else {
  662. if (a->Z_is_one) {
  663. if (!BN_copy(n0, &b->Z)) {
  664. goto end;
  665. }
  666. } else if (b->Z_is_one) {
  667. if (!BN_copy(n0, &a->Z)) {
  668. goto end;
  669. }
  670. } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
  671. goto end;
  672. }
  673. if (!field_mul(group, &r->Z, n0, n5, ctx)) {
  674. goto end;
  675. }
  676. }
  677. r->Z_is_one = 0;
  678. /* Z_r = Z_a * Z_b * n5 */
  679. /* X_r */
  680. if (!field_sqr(group, n0, n6, ctx) ||
  681. !field_sqr(group, n4, n5, ctx) ||
  682. !field_mul(group, n3, n1, n4, ctx) ||
  683. !BN_mod_sub_quick(&r->X, n0, n3, p)) {
  684. goto end;
  685. }
  686. /* X_r = n6^2 - n5^2 * 'n7' */
  687. /* 'n9' */
  688. if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
  689. !BN_mod_sub_quick(n0, n3, n0, p)) {
  690. goto end;
  691. }
  692. /* n9 = n5^2 * 'n7' - 2 * X_r */
  693. /* Y_r */
  694. if (!field_mul(group, n0, n0, n6, ctx) ||
  695. !field_mul(group, n5, n4, n5, ctx)) {
  696. goto end; /* now n5 is n5^3 */
  697. }
  698. if (!field_mul(group, n1, n2, n5, ctx) ||
  699. !BN_mod_sub_quick(n0, n0, n1, p)) {
  700. goto end;
  701. }
  702. if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
  703. goto end;
  704. }
  705. /* now 0 <= n0 < 2*p, and n0 is even */
  706. if (!BN_rshift1(&r->Y, n0)) {
  707. goto end;
  708. }
  709. /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  710. ret = 1;
  711. end:
  712. if (ctx) {
  713. /* otherwise we already called BN_CTX_end */
  714. BN_CTX_end(ctx);
  715. }
  716. if (new_ctx != NULL) {
  717. BN_CTX_free(new_ctx);
  718. }
  719. return ret;
  720. }
  721. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  722. BN_CTX *ctx) {
  723. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  724. BN_CTX *);
  725. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  726. const BIGNUM *p;
  727. BN_CTX *new_ctx = NULL;
  728. BIGNUM *n0, *n1, *n2, *n3;
  729. int ret = 0;
  730. if (EC_POINT_is_at_infinity(group, a)) {
  731. BN_zero(&r->Z);
  732. r->Z_is_one = 0;
  733. return 1;
  734. }
  735. field_mul = group->meth->field_mul;
  736. field_sqr = group->meth->field_sqr;
  737. p = &group->field;
  738. if (ctx == NULL) {
  739. ctx = new_ctx = BN_CTX_new();
  740. if (ctx == NULL) {
  741. return 0;
  742. }
  743. }
  744. BN_CTX_start(ctx);
  745. n0 = BN_CTX_get(ctx);
  746. n1 = BN_CTX_get(ctx);
  747. n2 = BN_CTX_get(ctx);
  748. n3 = BN_CTX_get(ctx);
  749. if (n3 == NULL) {
  750. goto err;
  751. }
  752. /* Note that in this function we must not read components of 'a'
  753. * once we have written the corresponding components of 'r'.
  754. * ('r' might the same as 'a'.)
  755. */
  756. /* n1 */
  757. if (a->Z_is_one) {
  758. if (!field_sqr(group, n0, &a->X, ctx) ||
  759. !BN_mod_lshift1_quick(n1, n0, p) ||
  760. !BN_mod_add_quick(n0, n0, n1, p) ||
  761. !BN_mod_add_quick(n1, n0, &group->a, p)) {
  762. goto err;
  763. }
  764. /* n1 = 3 * X_a^2 + a_curve */
  765. } else if (group->a_is_minus3) {
  766. if (!field_sqr(group, n1, &a->Z, ctx) ||
  767. !BN_mod_add_quick(n0, &a->X, n1, p) ||
  768. !BN_mod_sub_quick(n2, &a->X, n1, p) ||
  769. !field_mul(group, n1, n0, n2, ctx) ||
  770. !BN_mod_lshift1_quick(n0, n1, p) ||
  771. !BN_mod_add_quick(n1, n0, n1, p)) {
  772. goto err;
  773. }
  774. /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  775. * = 3 * X_a^2 - 3 * Z_a^4 */
  776. } else {
  777. if (!field_sqr(group, n0, &a->X, ctx) ||
  778. !BN_mod_lshift1_quick(n1, n0, p) ||
  779. !BN_mod_add_quick(n0, n0, n1, p) ||
  780. !field_sqr(group, n1, &a->Z, ctx) ||
  781. !field_sqr(group, n1, n1, ctx) ||
  782. !field_mul(group, n1, n1, &group->a, ctx) ||
  783. !BN_mod_add_quick(n1, n1, n0, p)) {
  784. goto err;
  785. }
  786. /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
  787. }
  788. /* Z_r */
  789. if (a->Z_is_one) {
  790. if (!BN_copy(n0, &a->Y)) {
  791. goto err;
  792. }
  793. } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
  794. goto err;
  795. }
  796. if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
  797. goto err;
  798. }
  799. r->Z_is_one = 0;
  800. /* Z_r = 2 * Y_a * Z_a */
  801. /* n2 */
  802. if (!field_sqr(group, n3, &a->Y, ctx) ||
  803. !field_mul(group, n2, &a->X, n3, ctx) ||
  804. !BN_mod_lshift_quick(n2, n2, 2, p)) {
  805. goto err;
  806. }
  807. /* n2 = 4 * X_a * Y_a^2 */
  808. /* X_r */
  809. if (!BN_mod_lshift1_quick(n0, n2, p) ||
  810. !field_sqr(group, &r->X, n1, ctx) ||
  811. !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
  812. goto err;
  813. }
  814. /* X_r = n1^2 - 2 * n2 */
  815. /* n3 */
  816. if (!field_sqr(group, n0, n3, ctx) ||
  817. !BN_mod_lshift_quick(n3, n0, 3, p)) {
  818. goto err;
  819. }
  820. /* n3 = 8 * Y_a^4 */
  821. /* Y_r */
  822. if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
  823. !field_mul(group, n0, n1, n0, ctx) ||
  824. !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
  825. goto err;
  826. }
  827. /* Y_r = n1 * (n2 - X_r) - n3 */
  828. ret = 1;
  829. err:
  830. BN_CTX_end(ctx);
  831. if (new_ctx != NULL) {
  832. BN_CTX_free(new_ctx);
  833. }
  834. return ret;
  835. }
  836. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
  837. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
  838. /* point is its own inverse */
  839. return 1;
  840. }
  841. return BN_usub(&point->Y, &group->field, &point->Y);
  842. }
  843. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
  844. return !point->Z_is_one && BN_is_zero(&point->Z);
  845. }
  846. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  847. BN_CTX *ctx) {
  848. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  849. BN_CTX *);
  850. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  851. const BIGNUM *p;
  852. BN_CTX *new_ctx = NULL;
  853. BIGNUM *rh, *tmp, *Z4, *Z6;
  854. int ret = -1;
  855. if (EC_POINT_is_at_infinity(group, point)) {
  856. return 1;
  857. }
  858. field_mul = group->meth->field_mul;
  859. field_sqr = group->meth->field_sqr;
  860. p = &group->field;
  861. if (ctx == NULL) {
  862. ctx = new_ctx = BN_CTX_new();
  863. if (ctx == NULL) {
  864. return -1;
  865. }
  866. }
  867. BN_CTX_start(ctx);
  868. rh = BN_CTX_get(ctx);
  869. tmp = BN_CTX_get(ctx);
  870. Z4 = BN_CTX_get(ctx);
  871. Z6 = BN_CTX_get(ctx);
  872. if (Z6 == NULL) {
  873. goto err;
  874. }
  875. /* We have a curve defined by a Weierstrass equation
  876. * y^2 = x^3 + a*x + b.
  877. * The point to consider is given in Jacobian projective coordinates
  878. * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  879. * Substituting this and multiplying by Z^6 transforms the above equation
  880. * into
  881. * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  882. * To test this, we add up the right-hand side in 'rh'.
  883. */
  884. /* rh := X^2 */
  885. if (!field_sqr(group, rh, &point->X, ctx)) {
  886. goto err;
  887. }
  888. if (!point->Z_is_one) {
  889. if (!field_sqr(group, tmp, &point->Z, ctx) ||
  890. !field_sqr(group, Z4, tmp, ctx) ||
  891. !field_mul(group, Z6, Z4, tmp, ctx)) {
  892. goto err;
  893. }
  894. /* rh := (rh + a*Z^4)*X */
  895. if (group->a_is_minus3) {
  896. if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
  897. !BN_mod_add_quick(tmp, tmp, Z4, p) ||
  898. !BN_mod_sub_quick(rh, rh, tmp, p) ||
  899. !field_mul(group, rh, rh, &point->X, ctx)) {
  900. goto err;
  901. }
  902. } else {
  903. if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
  904. !BN_mod_add_quick(rh, rh, tmp, p) ||
  905. !field_mul(group, rh, rh, &point->X, ctx)) {
  906. goto err;
  907. }
  908. }
  909. /* rh := rh + b*Z^6 */
  910. if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
  911. !BN_mod_add_quick(rh, rh, tmp, p)) {
  912. goto err;
  913. }
  914. } else {
  915. /* point->Z_is_one */
  916. /* rh := (rh + a)*X */
  917. if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
  918. !field_mul(group, rh, rh, &point->X, ctx)) {
  919. goto err;
  920. }
  921. /* rh := rh + b */
  922. if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
  923. goto err;
  924. }
  925. }
  926. /* 'lh' := Y^2 */
  927. if (!field_sqr(group, tmp, &point->Y, ctx)) {
  928. goto err;
  929. }
  930. ret = (0 == BN_ucmp(tmp, rh));
  931. err:
  932. BN_CTX_end(ctx);
  933. if (new_ctx != NULL) {
  934. BN_CTX_free(new_ctx);
  935. }
  936. return ret;
  937. }
  938. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  939. const EC_POINT *b, BN_CTX *ctx) {
  940. /* return values:
  941. * -1 error
  942. * 0 equal (in affine coordinates)
  943. * 1 not equal
  944. */
  945. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  946. BN_CTX *);
  947. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  948. BN_CTX *new_ctx = NULL;
  949. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  950. const BIGNUM *tmp1_, *tmp2_;
  951. int ret = -1;
  952. if (EC_POINT_is_at_infinity(group, a)) {
  953. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  954. }
  955. if (EC_POINT_is_at_infinity(group, b)) {
  956. return 1;
  957. }
  958. if (a->Z_is_one && b->Z_is_one) {
  959. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  960. }
  961. field_mul = group->meth->field_mul;
  962. field_sqr = group->meth->field_sqr;
  963. if (ctx == NULL) {
  964. ctx = new_ctx = BN_CTX_new();
  965. if (ctx == NULL) {
  966. return -1;
  967. }
  968. }
  969. BN_CTX_start(ctx);
  970. tmp1 = BN_CTX_get(ctx);
  971. tmp2 = BN_CTX_get(ctx);
  972. Za23 = BN_CTX_get(ctx);
  973. Zb23 = BN_CTX_get(ctx);
  974. if (Zb23 == NULL) {
  975. goto end;
  976. }
  977. /* We have to decide whether
  978. * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  979. * or equivalently, whether
  980. * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  981. */
  982. if (!b->Z_is_one) {
  983. if (!field_sqr(group, Zb23, &b->Z, ctx) ||
  984. !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
  985. goto end;
  986. }
  987. tmp1_ = tmp1;
  988. } else {
  989. tmp1_ = &a->X;
  990. }
  991. if (!a->Z_is_one) {
  992. if (!field_sqr(group, Za23, &a->Z, ctx) ||
  993. !field_mul(group, tmp2, &b->X, Za23, ctx)) {
  994. goto end;
  995. }
  996. tmp2_ = tmp2;
  997. } else {
  998. tmp2_ = &b->X;
  999. }
  1000. /* compare X_a*Z_b^2 with X_b*Z_a^2 */
  1001. if (BN_cmp(tmp1_, tmp2_) != 0) {
  1002. ret = 1; /* points differ */
  1003. goto end;
  1004. }
  1005. if (!b->Z_is_one) {
  1006. if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
  1007. !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
  1008. goto end;
  1009. }
  1010. /* tmp1_ = tmp1 */
  1011. } else {
  1012. tmp1_ = &a->Y;
  1013. }
  1014. if (!a->Z_is_one) {
  1015. if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
  1016. !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
  1017. goto end;
  1018. }
  1019. /* tmp2_ = tmp2 */
  1020. } else {
  1021. tmp2_ = &b->Y;
  1022. }
  1023. /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
  1024. if (BN_cmp(tmp1_, tmp2_) != 0) {
  1025. ret = 1; /* points differ */
  1026. goto end;
  1027. }
  1028. /* points are equal */
  1029. ret = 0;
  1030. end:
  1031. BN_CTX_end(ctx);
  1032. if (new_ctx != NULL) {
  1033. BN_CTX_free(new_ctx);
  1034. }
  1035. return ret;
  1036. }
  1037. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  1038. BN_CTX *ctx) {
  1039. BN_CTX *new_ctx = NULL;
  1040. BIGNUM *x, *y;
  1041. int ret = 0;
  1042. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) {
  1043. return 1;
  1044. }
  1045. if (ctx == NULL) {
  1046. ctx = new_ctx = BN_CTX_new();
  1047. if (ctx == NULL) {
  1048. return 0;
  1049. }
  1050. }
  1051. BN_CTX_start(ctx);
  1052. x = BN_CTX_get(ctx);
  1053. y = BN_CTX_get(ctx);
  1054. if (y == NULL) {
  1055. goto err;
  1056. }
  1057. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) ||
  1058. !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  1059. goto err;
  1060. }
  1061. if (!point->Z_is_one) {
  1062. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_make_affine, ERR_R_INTERNAL_ERROR);
  1063. goto err;
  1064. }
  1065. ret = 1;
  1066. err:
  1067. BN_CTX_end(ctx);
  1068. if (new_ctx != NULL) {
  1069. BN_CTX_free(new_ctx);
  1070. }
  1071. return ret;
  1072. }
  1073. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
  1074. EC_POINT *points[], BN_CTX *ctx) {
  1075. BN_CTX *new_ctx = NULL;
  1076. BIGNUM *tmp, *tmp_Z;
  1077. BIGNUM **prod_Z = NULL;
  1078. size_t i;
  1079. int ret = 0;
  1080. if (num == 0) {
  1081. return 1;
  1082. }
  1083. if (ctx == NULL) {
  1084. ctx = new_ctx = BN_CTX_new();
  1085. if (ctx == NULL) {
  1086. return 0;
  1087. }
  1088. }
  1089. BN_CTX_start(ctx);
  1090. tmp = BN_CTX_get(ctx);
  1091. tmp_Z = BN_CTX_get(ctx);
  1092. if (tmp == NULL || tmp_Z == NULL) {
  1093. goto err;
  1094. }
  1095. prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
  1096. if (prod_Z == NULL) {
  1097. goto err;
  1098. }
  1099. memset(prod_Z, 0, num * sizeof(prod_Z[0]));
  1100. for (i = 0; i < num; i++) {
  1101. prod_Z[i] = BN_new();
  1102. if (prod_Z[i] == NULL) {
  1103. goto err;
  1104. }
  1105. }
  1106. /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
  1107. * skipping any zero-valued inputs (pretend that they're 1). */
  1108. if (!BN_is_zero(&points[0]->Z)) {
  1109. if (!BN_copy(prod_Z[0], &points[0]->Z)) {
  1110. goto err;
  1111. }
  1112. } else {
  1113. if (group->meth->field_set_to_one != 0) {
  1114. if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) {
  1115. goto err;
  1116. }
  1117. } else {
  1118. if (!BN_one(prod_Z[0])) {
  1119. goto err;
  1120. }
  1121. }
  1122. }
  1123. for (i = 1; i < num; i++) {
  1124. if (!BN_is_zero(&points[i]->Z)) {
  1125. if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
  1126. &points[i]->Z, ctx)) {
  1127. goto err;
  1128. }
  1129. } else {
  1130. if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
  1131. goto err;
  1132. }
  1133. }
  1134. }
  1135. /* Now use a single explicit inversion to replace every
  1136. * non-zero points[i]->Z by its inverse. */
  1137. if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx)) {
  1138. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_points_make_affine, ERR_R_BN_LIB);
  1139. goto err;
  1140. }
  1141. if (group->meth->field_encode != NULL) {
  1142. /* In the Montgomery case, we just turned R*H (representing H)
  1143. * into 1/(R*H), but we need R*(1/H) (representing 1/H);
  1144. * i.e. we need to multiply by the Montgomery factor twice. */
  1145. if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
  1146. !group->meth->field_encode(group, tmp, tmp, ctx)) {
  1147. goto err;
  1148. }
  1149. }
  1150. for (i = num - 1; i > 0; --i) {
  1151. /* Loop invariant: tmp is the product of the inverses of
  1152. * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
  1153. if (BN_is_zero(&points[i]->Z)) {
  1154. continue;
  1155. }
  1156. /* Set tmp_Z to the inverse of points[i]->Z (as product
  1157. * of Z inverses 0 .. i, Z values 0 .. i - 1). */
  1158. if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
  1159. /* Update tmp to satisfy the loop invariant for i - 1. */
  1160. !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
  1161. /* Replace points[i]->Z by its inverse. */
  1162. !BN_copy(&points[i]->Z, tmp_Z)) {
  1163. goto err;
  1164. }
  1165. }
  1166. /* Replace points[0]->Z by its inverse. */
  1167. if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
  1168. goto err;
  1169. }
  1170. /* Finally, fix up the X and Y coordinates for all points. */
  1171. for (i = 0; i < num; i++) {
  1172. EC_POINT *p = points[i];
  1173. if (!BN_is_zero(&p->Z)) {
  1174. /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1). */
  1175. if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
  1176. !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
  1177. !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
  1178. !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
  1179. goto err;
  1180. }
  1181. if (group->meth->field_set_to_one != NULL) {
  1182. if (!group->meth->field_set_to_one(group, &p->Z, ctx)) {
  1183. goto err;
  1184. }
  1185. } else {
  1186. if (!BN_one(&p->Z)) {
  1187. goto err;
  1188. }
  1189. }
  1190. p->Z_is_one = 1;
  1191. }
  1192. }
  1193. ret = 1;
  1194. err:
  1195. BN_CTX_end(ctx);
  1196. if (new_ctx != NULL) {
  1197. BN_CTX_free(new_ctx);
  1198. }
  1199. if (prod_Z != NULL) {
  1200. for (i = 0; i < num; i++) {
  1201. if (prod_Z[i] == NULL) {
  1202. break;
  1203. }
  1204. BN_clear_free(prod_Z[i]);
  1205. }
  1206. OPENSSL_free(prod_Z);
  1207. }
  1208. return ret;
  1209. }
  1210. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1211. const BIGNUM *b, BN_CTX *ctx) {
  1212. return BN_mod_mul(r, a, b, &group->field, ctx);
  1213. }
  1214. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1215. BN_CTX *ctx) {
  1216. return BN_mod_sqr(r, a, &group->field, ctx);
  1217. }