No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

simple.c 35 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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_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. BN_CTX_free(new_ctx);
  205. return ret;
  206. }
  207. int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  208. BIGNUM *b, BN_CTX *ctx) {
  209. int ret = 0;
  210. BN_CTX *new_ctx = NULL;
  211. if (p != NULL && !BN_copy(p, &group->field)) {
  212. return 0;
  213. }
  214. if (a != NULL || b != NULL) {
  215. if (group->meth->field_decode) {
  216. if (ctx == NULL) {
  217. ctx = new_ctx = BN_CTX_new();
  218. if (ctx == NULL) {
  219. return 0;
  220. }
  221. }
  222. if (a != NULL && !group->meth->field_decode(group, a, &group->a, ctx)) {
  223. goto err;
  224. }
  225. if (b != NULL && !group->meth->field_decode(group, b, &group->b, ctx)) {
  226. goto err;
  227. }
  228. } else {
  229. if (a != NULL && !BN_copy(a, &group->a)) {
  230. goto err;
  231. }
  232. if (b != NULL && !BN_copy(b, &group->b)) {
  233. goto err;
  234. }
  235. }
  236. }
  237. ret = 1;
  238. err:
  239. BN_CTX_free(new_ctx);
  240. return ret;
  241. }
  242. int ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
  243. return BN_num_bits(&group->field);
  244. }
  245. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) {
  246. int ret = 0;
  247. BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
  248. const BIGNUM *p = &group->field;
  249. BN_CTX *new_ctx = NULL;
  250. if (ctx == NULL) {
  251. ctx = new_ctx = BN_CTX_new();
  252. if (ctx == NULL) {
  253. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  254. goto err;
  255. }
  256. }
  257. BN_CTX_start(ctx);
  258. a = BN_CTX_get(ctx);
  259. b = BN_CTX_get(ctx);
  260. tmp_1 = BN_CTX_get(ctx);
  261. tmp_2 = BN_CTX_get(ctx);
  262. order = BN_CTX_get(ctx);
  263. if (order == NULL) {
  264. goto err;
  265. }
  266. if (group->meth->field_decode) {
  267. if (!group->meth->field_decode(group, a, &group->a, ctx) ||
  268. !group->meth->field_decode(group, b, &group->b, ctx)) {
  269. goto err;
  270. }
  271. } else {
  272. if (!BN_copy(a, &group->a) || !BN_copy(b, &group->b)) {
  273. goto err;
  274. }
  275. }
  276. /* check the discriminant:
  277. * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
  278. * 0 =< a, b < p */
  279. if (BN_is_zero(a)) {
  280. if (BN_is_zero(b)) {
  281. goto err;
  282. }
  283. } else if (!BN_is_zero(b)) {
  284. if (!BN_mod_sqr(tmp_1, a, p, ctx) ||
  285. !BN_mod_mul(tmp_2, tmp_1, a, p, ctx) ||
  286. !BN_lshift(tmp_1, tmp_2, 2)) {
  287. goto err;
  288. }
  289. /* tmp_1 = 4*a^3 */
  290. if (!BN_mod_sqr(tmp_2, b, p, ctx) ||
  291. !BN_mul_word(tmp_2, 27)) {
  292. goto err;
  293. }
  294. /* tmp_2 = 27*b^2 */
  295. if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx) ||
  296. BN_is_zero(a)) {
  297. goto err;
  298. }
  299. }
  300. ret = 1;
  301. err:
  302. if (ctx != NULL) {
  303. BN_CTX_end(ctx);
  304. }
  305. BN_CTX_free(new_ctx);
  306. return ret;
  307. }
  308. int ec_GFp_simple_point_init(EC_POINT *point) {
  309. BN_init(&point->X);
  310. BN_init(&point->Y);
  311. BN_init(&point->Z);
  312. point->Z_is_one = 0;
  313. return 1;
  314. }
  315. void ec_GFp_simple_point_finish(EC_POINT *point) {
  316. BN_free(&point->X);
  317. BN_free(&point->Y);
  318. BN_free(&point->Z);
  319. }
  320. void ec_GFp_simple_point_clear_finish(EC_POINT *point) {
  321. BN_clear_free(&point->X);
  322. BN_clear_free(&point->Y);
  323. BN_clear_free(&point->Z);
  324. point->Z_is_one = 0;
  325. }
  326. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
  327. if (!BN_copy(&dest->X, &src->X) ||
  328. !BN_copy(&dest->Y, &src->Y) ||
  329. !BN_copy(&dest->Z, &src->Z)) {
  330. return 0;
  331. }
  332. dest->Z_is_one = src->Z_is_one;
  333. return 1;
  334. }
  335. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
  336. EC_POINT *point) {
  337. point->Z_is_one = 0;
  338. BN_zero(&point->Z);
  339. return 1;
  340. }
  341. int ec_GFp_simple_set_Jprojective_coordinates_GFp(
  342. const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y,
  343. const BIGNUM *z, BN_CTX *ctx) {
  344. BN_CTX *new_ctx = NULL;
  345. int ret = 0;
  346. if (ctx == NULL) {
  347. ctx = new_ctx = BN_CTX_new();
  348. if (ctx == NULL) {
  349. return 0;
  350. }
  351. }
  352. if (x != NULL) {
  353. if (!BN_nnmod(&point->X, x, &group->field, ctx)) {
  354. goto err;
  355. }
  356. if (group->meth->field_encode &&
  357. !group->meth->field_encode(group, &point->X, &point->X, ctx)) {
  358. goto err;
  359. }
  360. }
  361. if (y != NULL) {
  362. if (!BN_nnmod(&point->Y, y, &group->field, ctx)) {
  363. goto err;
  364. }
  365. if (group->meth->field_encode &&
  366. !group->meth->field_encode(group, &point->Y, &point->Y, ctx)) {
  367. goto err;
  368. }
  369. }
  370. if (z != NULL) {
  371. int Z_is_one;
  372. if (!BN_nnmod(&point->Z, z, &group->field, ctx)) {
  373. goto err;
  374. }
  375. Z_is_one = BN_is_one(&point->Z);
  376. if (group->meth->field_encode) {
  377. if (Z_is_one && (group->meth->field_set_to_one != 0)) {
  378. if (!group->meth->field_set_to_one(group, &point->Z, ctx)) {
  379. goto err;
  380. }
  381. } else if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) {
  382. goto err;
  383. }
  384. }
  385. point->Z_is_one = Z_is_one;
  386. }
  387. ret = 1;
  388. err:
  389. BN_CTX_free(new_ctx);
  390. return ret;
  391. }
  392. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  393. const EC_POINT *point,
  394. BIGNUM *x, BIGNUM *y,
  395. BIGNUM *z, BN_CTX *ctx) {
  396. BN_CTX *new_ctx = NULL;
  397. int ret = 0;
  398. if (group->meth->field_decode != 0) {
  399. if (ctx == NULL) {
  400. ctx = new_ctx = BN_CTX_new();
  401. if (ctx == NULL) {
  402. return 0;
  403. }
  404. }
  405. if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
  406. goto err;
  407. }
  408. if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
  409. goto err;
  410. }
  411. if (z != NULL && !group->meth->field_decode(group, z, &point->Z, ctx)) {
  412. goto err;
  413. }
  414. } else {
  415. if (x != NULL && !BN_copy(x, &point->X)) {
  416. goto err;
  417. }
  418. if (y != NULL && !BN_copy(y, &point->Y)) {
  419. goto err;
  420. }
  421. if (z != NULL && !BN_copy(z, &point->Z)) {
  422. goto err;
  423. }
  424. }
  425. ret = 1;
  426. err:
  427. BN_CTX_free(new_ctx);
  428. return ret;
  429. }
  430. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
  431. EC_POINT *point, const BIGNUM *x,
  432. const BIGNUM *y, BN_CTX *ctx) {
  433. if (x == NULL || y == NULL) {
  434. /* unlike for projective coordinates, we do not tolerate this */
  435. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  436. return 0;
  437. }
  438. return ec_point_set_Jprojective_coordinates_GFp(group, point, x, y,
  439. BN_value_one(), ctx);
  440. }
  441. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
  442. const EC_POINT *point, BIGNUM *x,
  443. BIGNUM *y, BN_CTX *ctx) {
  444. BN_CTX *new_ctx = NULL;
  445. BIGNUM *Z, *Z_1, *Z_2, *Z_3;
  446. const BIGNUM *Z_;
  447. int ret = 0;
  448. if (EC_POINT_is_at_infinity(group, point)) {
  449. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  450. return 0;
  451. }
  452. if (ctx == NULL) {
  453. ctx = new_ctx = BN_CTX_new();
  454. if (ctx == NULL) {
  455. return 0;
  456. }
  457. }
  458. BN_CTX_start(ctx);
  459. Z = BN_CTX_get(ctx);
  460. Z_1 = BN_CTX_get(ctx);
  461. Z_2 = BN_CTX_get(ctx);
  462. Z_3 = BN_CTX_get(ctx);
  463. if (Z_3 == NULL) {
  464. goto err;
  465. }
  466. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  467. if (group->meth->field_decode) {
  468. if (!group->meth->field_decode(group, Z, &point->Z, ctx)) {
  469. goto err;
  470. }
  471. Z_ = Z;
  472. } else {
  473. Z_ = &point->Z;
  474. }
  475. if (BN_is_one(Z_)) {
  476. if (group->meth->field_decode) {
  477. if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
  478. goto err;
  479. }
  480. if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
  481. goto err;
  482. }
  483. } else {
  484. if (x != NULL && !BN_copy(x, &point->X)) {
  485. goto err;
  486. }
  487. if (y != NULL && !BN_copy(y, &point->Y)) {
  488. goto err;
  489. }
  490. }
  491. } else {
  492. if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx)) {
  493. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  494. goto err;
  495. }
  496. if (group->meth->field_encode == 0) {
  497. /* field_sqr works on standard representation */
  498. if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) {
  499. goto err;
  500. }
  501. } else if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) {
  502. goto err;
  503. }
  504. /* in the Montgomery case, field_mul will cancel out Montgomery factor in
  505. * X: */
  506. if (x != NULL && !group->meth->field_mul(group, x, &point->X, Z_2, ctx)) {
  507. goto err;
  508. }
  509. if (y != NULL) {
  510. if (group->meth->field_encode == 0) {
  511. /* field_mul works on standard representation */
  512. if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) {
  513. goto err;
  514. }
  515. } else if (!BN_mod_mul(Z_3, 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. * Y: */
  520. if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) {
  521. goto err;
  522. }
  523. }
  524. }
  525. ret = 1;
  526. err:
  527. BN_CTX_end(ctx);
  528. BN_CTX_free(new_ctx);
  529. return ret;
  530. }
  531. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  532. const EC_POINT *b, BN_CTX *ctx) {
  533. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  534. BN_CTX *);
  535. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  536. const BIGNUM *p;
  537. BN_CTX *new_ctx = NULL;
  538. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  539. int ret = 0;
  540. if (a == b) {
  541. return EC_POINT_dbl(group, r, a, ctx);
  542. }
  543. if (EC_POINT_is_at_infinity(group, a)) {
  544. return EC_POINT_copy(r, b);
  545. }
  546. if (EC_POINT_is_at_infinity(group, b)) {
  547. return EC_POINT_copy(r, a);
  548. }
  549. field_mul = group->meth->field_mul;
  550. field_sqr = group->meth->field_sqr;
  551. p = &group->field;
  552. if (ctx == NULL) {
  553. ctx = new_ctx = BN_CTX_new();
  554. if (ctx == NULL) {
  555. return 0;
  556. }
  557. }
  558. BN_CTX_start(ctx);
  559. n0 = BN_CTX_get(ctx);
  560. n1 = BN_CTX_get(ctx);
  561. n2 = BN_CTX_get(ctx);
  562. n3 = BN_CTX_get(ctx);
  563. n4 = BN_CTX_get(ctx);
  564. n5 = BN_CTX_get(ctx);
  565. n6 = BN_CTX_get(ctx);
  566. if (n6 == NULL) {
  567. goto end;
  568. }
  569. /* Note that in this function we must not read components of 'a' or 'b'
  570. * once we have written the corresponding components of 'r'.
  571. * ('r' might be one of 'a' or 'b'.)
  572. */
  573. /* n1, n2 */
  574. if (b->Z_is_one) {
  575. if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
  576. goto end;
  577. }
  578. /* n1 = X_a */
  579. /* n2 = Y_a */
  580. } else {
  581. if (!field_sqr(group, n0, &b->Z, ctx) ||
  582. !field_mul(group, n1, &a->X, n0, ctx)) {
  583. goto end;
  584. }
  585. /* n1 = X_a * Z_b^2 */
  586. if (!field_mul(group, n0, n0, &b->Z, ctx) ||
  587. !field_mul(group, n2, &a->Y, n0, ctx)) {
  588. goto end;
  589. }
  590. /* n2 = Y_a * Z_b^3 */
  591. }
  592. /* n3, n4 */
  593. if (a->Z_is_one) {
  594. if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
  595. goto end;
  596. }
  597. /* n3 = X_b */
  598. /* n4 = Y_b */
  599. } else {
  600. if (!field_sqr(group, n0, &a->Z, ctx) ||
  601. !field_mul(group, n3, &b->X, n0, ctx)) {
  602. goto end;
  603. }
  604. /* n3 = X_b * Z_a^2 */
  605. if (!field_mul(group, n0, n0, &a->Z, ctx) ||
  606. !field_mul(group, n4, &b->Y, n0, ctx)) {
  607. goto end;
  608. }
  609. /* n4 = Y_b * Z_a^3 */
  610. }
  611. /* n5, n6 */
  612. if (!BN_mod_sub_quick(n5, n1, n3, p) ||
  613. !BN_mod_sub_quick(n6, n2, n4, p)) {
  614. goto end;
  615. }
  616. /* n5 = n1 - n3 */
  617. /* n6 = n2 - n4 */
  618. if (BN_is_zero(n5)) {
  619. if (BN_is_zero(n6)) {
  620. /* a is the same point as b */
  621. BN_CTX_end(ctx);
  622. ret = EC_POINT_dbl(group, r, a, ctx);
  623. ctx = NULL;
  624. goto end;
  625. } else {
  626. /* a is the inverse of b */
  627. BN_zero(&r->Z);
  628. r->Z_is_one = 0;
  629. ret = 1;
  630. goto end;
  631. }
  632. }
  633. /* 'n7', 'n8' */
  634. if (!BN_mod_add_quick(n1, n1, n3, p) ||
  635. !BN_mod_add_quick(n2, n2, n4, p)) {
  636. goto end;
  637. }
  638. /* 'n7' = n1 + n3 */
  639. /* 'n8' = n2 + n4 */
  640. /* Z_r */
  641. if (a->Z_is_one && b->Z_is_one) {
  642. if (!BN_copy(&r->Z, n5)) {
  643. goto end;
  644. }
  645. } else {
  646. if (a->Z_is_one) {
  647. if (!BN_copy(n0, &b->Z)) {
  648. goto end;
  649. }
  650. } else if (b->Z_is_one) {
  651. if (!BN_copy(n0, &a->Z)) {
  652. goto end;
  653. }
  654. } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
  655. goto end;
  656. }
  657. if (!field_mul(group, &r->Z, n0, n5, ctx)) {
  658. goto end;
  659. }
  660. }
  661. r->Z_is_one = 0;
  662. /* Z_r = Z_a * Z_b * n5 */
  663. /* X_r */
  664. if (!field_sqr(group, n0, n6, ctx) ||
  665. !field_sqr(group, n4, n5, ctx) ||
  666. !field_mul(group, n3, n1, n4, ctx) ||
  667. !BN_mod_sub_quick(&r->X, n0, n3, p)) {
  668. goto end;
  669. }
  670. /* X_r = n6^2 - n5^2 * 'n7' */
  671. /* 'n9' */
  672. if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
  673. !BN_mod_sub_quick(n0, n3, n0, p)) {
  674. goto end;
  675. }
  676. /* n9 = n5^2 * 'n7' - 2 * X_r */
  677. /* Y_r */
  678. if (!field_mul(group, n0, n0, n6, ctx) ||
  679. !field_mul(group, n5, n4, n5, ctx)) {
  680. goto end; /* now n5 is n5^3 */
  681. }
  682. if (!field_mul(group, n1, n2, n5, ctx) ||
  683. !BN_mod_sub_quick(n0, n0, n1, p)) {
  684. goto end;
  685. }
  686. if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
  687. goto end;
  688. }
  689. /* now 0 <= n0 < 2*p, and n0 is even */
  690. if (!BN_rshift1(&r->Y, n0)) {
  691. goto end;
  692. }
  693. /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  694. ret = 1;
  695. end:
  696. if (ctx) {
  697. /* otherwise we already called BN_CTX_end */
  698. BN_CTX_end(ctx);
  699. }
  700. BN_CTX_free(new_ctx);
  701. return ret;
  702. }
  703. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  704. BN_CTX *ctx) {
  705. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  706. BN_CTX *);
  707. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  708. const BIGNUM *p;
  709. BN_CTX *new_ctx = NULL;
  710. BIGNUM *n0, *n1, *n2, *n3;
  711. int ret = 0;
  712. if (EC_POINT_is_at_infinity(group, a)) {
  713. BN_zero(&r->Z);
  714. r->Z_is_one = 0;
  715. return 1;
  716. }
  717. field_mul = group->meth->field_mul;
  718. field_sqr = group->meth->field_sqr;
  719. p = &group->field;
  720. if (ctx == NULL) {
  721. ctx = new_ctx = BN_CTX_new();
  722. if (ctx == NULL) {
  723. return 0;
  724. }
  725. }
  726. BN_CTX_start(ctx);
  727. n0 = BN_CTX_get(ctx);
  728. n1 = BN_CTX_get(ctx);
  729. n2 = BN_CTX_get(ctx);
  730. n3 = BN_CTX_get(ctx);
  731. if (n3 == NULL) {
  732. goto err;
  733. }
  734. /* Note that in this function we must not read components of 'a'
  735. * once we have written the corresponding components of 'r'.
  736. * ('r' might the same as 'a'.)
  737. */
  738. /* n1 */
  739. if (a->Z_is_one) {
  740. if (!field_sqr(group, n0, &a->X, ctx) ||
  741. !BN_mod_lshift1_quick(n1, n0, p) ||
  742. !BN_mod_add_quick(n0, n0, n1, p) ||
  743. !BN_mod_add_quick(n1, n0, &group->a, p)) {
  744. goto err;
  745. }
  746. /* n1 = 3 * X_a^2 + a_curve */
  747. } else if (group->a_is_minus3) {
  748. if (!field_sqr(group, n1, &a->Z, ctx) ||
  749. !BN_mod_add_quick(n0, &a->X, n1, p) ||
  750. !BN_mod_sub_quick(n2, &a->X, n1, p) ||
  751. !field_mul(group, n1, n0, n2, ctx) ||
  752. !BN_mod_lshift1_quick(n0, n1, p) ||
  753. !BN_mod_add_quick(n1, n0, n1, p)) {
  754. goto err;
  755. }
  756. /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  757. * = 3 * X_a^2 - 3 * Z_a^4 */
  758. } else {
  759. if (!field_sqr(group, n0, &a->X, ctx) ||
  760. !BN_mod_lshift1_quick(n1, n0, p) ||
  761. !BN_mod_add_quick(n0, n0, n1, p) ||
  762. !field_sqr(group, n1, &a->Z, ctx) ||
  763. !field_sqr(group, n1, n1, ctx) ||
  764. !field_mul(group, n1, n1, &group->a, ctx) ||
  765. !BN_mod_add_quick(n1, n1, n0, p)) {
  766. goto err;
  767. }
  768. /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
  769. }
  770. /* Z_r */
  771. if (a->Z_is_one) {
  772. if (!BN_copy(n0, &a->Y)) {
  773. goto err;
  774. }
  775. } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
  776. goto err;
  777. }
  778. if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
  779. goto err;
  780. }
  781. r->Z_is_one = 0;
  782. /* Z_r = 2 * Y_a * Z_a */
  783. /* n2 */
  784. if (!field_sqr(group, n3, &a->Y, ctx) ||
  785. !field_mul(group, n2, &a->X, n3, ctx) ||
  786. !BN_mod_lshift_quick(n2, n2, 2, p)) {
  787. goto err;
  788. }
  789. /* n2 = 4 * X_a * Y_a^2 */
  790. /* X_r */
  791. if (!BN_mod_lshift1_quick(n0, n2, p) ||
  792. !field_sqr(group, &r->X, n1, ctx) ||
  793. !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
  794. goto err;
  795. }
  796. /* X_r = n1^2 - 2 * n2 */
  797. /* n3 */
  798. if (!field_sqr(group, n0, n3, ctx) ||
  799. !BN_mod_lshift_quick(n3, n0, 3, p)) {
  800. goto err;
  801. }
  802. /* n3 = 8 * Y_a^4 */
  803. /* Y_r */
  804. if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
  805. !field_mul(group, n0, n1, n0, ctx) ||
  806. !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
  807. goto err;
  808. }
  809. /* Y_r = n1 * (n2 - X_r) - n3 */
  810. ret = 1;
  811. err:
  812. BN_CTX_end(ctx);
  813. BN_CTX_free(new_ctx);
  814. return ret;
  815. }
  816. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
  817. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
  818. /* point is its own inverse */
  819. return 1;
  820. }
  821. return BN_usub(&point->Y, &group->field, &point->Y);
  822. }
  823. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
  824. return !point->Z_is_one && BN_is_zero(&point->Z);
  825. }
  826. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  827. BN_CTX *ctx) {
  828. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  829. BN_CTX *);
  830. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  831. const BIGNUM *p;
  832. BN_CTX *new_ctx = NULL;
  833. BIGNUM *rh, *tmp, *Z4, *Z6;
  834. int ret = -1;
  835. if (EC_POINT_is_at_infinity(group, point)) {
  836. return 1;
  837. }
  838. field_mul = group->meth->field_mul;
  839. field_sqr = group->meth->field_sqr;
  840. p = &group->field;
  841. if (ctx == NULL) {
  842. ctx = new_ctx = BN_CTX_new();
  843. if (ctx == NULL) {
  844. return -1;
  845. }
  846. }
  847. BN_CTX_start(ctx);
  848. rh = BN_CTX_get(ctx);
  849. tmp = BN_CTX_get(ctx);
  850. Z4 = BN_CTX_get(ctx);
  851. Z6 = BN_CTX_get(ctx);
  852. if (Z6 == NULL) {
  853. goto err;
  854. }
  855. /* We have a curve defined by a Weierstrass equation
  856. * y^2 = x^3 + a*x + b.
  857. * The point to consider is given in Jacobian projective coordinates
  858. * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  859. * Substituting this and multiplying by Z^6 transforms the above equation
  860. * into
  861. * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  862. * To test this, we add up the right-hand side in 'rh'.
  863. */
  864. /* rh := X^2 */
  865. if (!field_sqr(group, rh, &point->X, ctx)) {
  866. goto err;
  867. }
  868. if (!point->Z_is_one) {
  869. if (!field_sqr(group, tmp, &point->Z, ctx) ||
  870. !field_sqr(group, Z4, tmp, ctx) ||
  871. !field_mul(group, Z6, Z4, tmp, ctx)) {
  872. goto err;
  873. }
  874. /* rh := (rh + a*Z^4)*X */
  875. if (group->a_is_minus3) {
  876. if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
  877. !BN_mod_add_quick(tmp, tmp, Z4, p) ||
  878. !BN_mod_sub_quick(rh, rh, tmp, p) ||
  879. !field_mul(group, rh, rh, &point->X, ctx)) {
  880. goto err;
  881. }
  882. } else {
  883. if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
  884. !BN_mod_add_quick(rh, rh, tmp, p) ||
  885. !field_mul(group, rh, rh, &point->X, ctx)) {
  886. goto err;
  887. }
  888. }
  889. /* rh := rh + b*Z^6 */
  890. if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
  891. !BN_mod_add_quick(rh, rh, tmp, p)) {
  892. goto err;
  893. }
  894. } else {
  895. /* point->Z_is_one */
  896. /* rh := (rh + a)*X */
  897. if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
  898. !field_mul(group, rh, rh, &point->X, ctx)) {
  899. goto err;
  900. }
  901. /* rh := rh + b */
  902. if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
  903. goto err;
  904. }
  905. }
  906. /* 'lh' := Y^2 */
  907. if (!field_sqr(group, tmp, &point->Y, ctx)) {
  908. goto err;
  909. }
  910. ret = (0 == BN_ucmp(tmp, rh));
  911. err:
  912. BN_CTX_end(ctx);
  913. BN_CTX_free(new_ctx);
  914. return ret;
  915. }
  916. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  917. const EC_POINT *b, BN_CTX *ctx) {
  918. /* return values:
  919. * -1 error
  920. * 0 equal (in affine coordinates)
  921. * 1 not equal
  922. */
  923. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  924. BN_CTX *);
  925. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  926. BN_CTX *new_ctx = NULL;
  927. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  928. const BIGNUM *tmp1_, *tmp2_;
  929. int ret = -1;
  930. if (EC_POINT_is_at_infinity(group, a)) {
  931. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  932. }
  933. if (EC_POINT_is_at_infinity(group, b)) {
  934. return 1;
  935. }
  936. if (a->Z_is_one && b->Z_is_one) {
  937. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  938. }
  939. field_mul = group->meth->field_mul;
  940. field_sqr = group->meth->field_sqr;
  941. if (ctx == NULL) {
  942. ctx = new_ctx = BN_CTX_new();
  943. if (ctx == NULL) {
  944. return -1;
  945. }
  946. }
  947. BN_CTX_start(ctx);
  948. tmp1 = BN_CTX_get(ctx);
  949. tmp2 = BN_CTX_get(ctx);
  950. Za23 = BN_CTX_get(ctx);
  951. Zb23 = BN_CTX_get(ctx);
  952. if (Zb23 == NULL) {
  953. goto end;
  954. }
  955. /* We have to decide whether
  956. * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  957. * or equivalently, whether
  958. * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  959. */
  960. if (!b->Z_is_one) {
  961. if (!field_sqr(group, Zb23, &b->Z, ctx) ||
  962. !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
  963. goto end;
  964. }
  965. tmp1_ = tmp1;
  966. } else {
  967. tmp1_ = &a->X;
  968. }
  969. if (!a->Z_is_one) {
  970. if (!field_sqr(group, Za23, &a->Z, ctx) ||
  971. !field_mul(group, tmp2, &b->X, Za23, ctx)) {
  972. goto end;
  973. }
  974. tmp2_ = tmp2;
  975. } else {
  976. tmp2_ = &b->X;
  977. }
  978. /* compare X_a*Z_b^2 with X_b*Z_a^2 */
  979. if (BN_cmp(tmp1_, tmp2_) != 0) {
  980. ret = 1; /* points differ */
  981. goto end;
  982. }
  983. if (!b->Z_is_one) {
  984. if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
  985. !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
  986. goto end;
  987. }
  988. /* tmp1_ = tmp1 */
  989. } else {
  990. tmp1_ = &a->Y;
  991. }
  992. if (!a->Z_is_one) {
  993. if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
  994. !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
  995. goto end;
  996. }
  997. /* tmp2_ = tmp2 */
  998. } else {
  999. tmp2_ = &b->Y;
  1000. }
  1001. /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
  1002. if (BN_cmp(tmp1_, tmp2_) != 0) {
  1003. ret = 1; /* points differ */
  1004. goto end;
  1005. }
  1006. /* points are equal */
  1007. ret = 0;
  1008. end:
  1009. BN_CTX_end(ctx);
  1010. BN_CTX_free(new_ctx);
  1011. return ret;
  1012. }
  1013. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  1014. BN_CTX *ctx) {
  1015. BN_CTX *new_ctx = NULL;
  1016. BIGNUM *x, *y;
  1017. int ret = 0;
  1018. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) {
  1019. return 1;
  1020. }
  1021. if (ctx == NULL) {
  1022. ctx = new_ctx = BN_CTX_new();
  1023. if (ctx == NULL) {
  1024. return 0;
  1025. }
  1026. }
  1027. BN_CTX_start(ctx);
  1028. x = BN_CTX_get(ctx);
  1029. y = BN_CTX_get(ctx);
  1030. if (y == NULL) {
  1031. goto err;
  1032. }
  1033. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) ||
  1034. !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  1035. goto err;
  1036. }
  1037. if (!point->Z_is_one) {
  1038. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  1039. goto err;
  1040. }
  1041. ret = 1;
  1042. err:
  1043. BN_CTX_end(ctx);
  1044. BN_CTX_free(new_ctx);
  1045. return ret;
  1046. }
  1047. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
  1048. EC_POINT *points[], BN_CTX *ctx) {
  1049. BN_CTX *new_ctx = NULL;
  1050. BIGNUM *tmp, *tmp_Z;
  1051. BIGNUM **prod_Z = NULL;
  1052. size_t i;
  1053. int ret = 0;
  1054. if (num == 0) {
  1055. return 1;
  1056. }
  1057. if (ctx == NULL) {
  1058. ctx = new_ctx = BN_CTX_new();
  1059. if (ctx == NULL) {
  1060. return 0;
  1061. }
  1062. }
  1063. BN_CTX_start(ctx);
  1064. tmp = BN_CTX_get(ctx);
  1065. tmp_Z = BN_CTX_get(ctx);
  1066. if (tmp == NULL || tmp_Z == NULL) {
  1067. goto err;
  1068. }
  1069. prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
  1070. if (prod_Z == NULL) {
  1071. goto err;
  1072. }
  1073. memset(prod_Z, 0, num * sizeof(prod_Z[0]));
  1074. for (i = 0; i < num; i++) {
  1075. prod_Z[i] = BN_new();
  1076. if (prod_Z[i] == NULL) {
  1077. goto err;
  1078. }
  1079. }
  1080. /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
  1081. * skipping any zero-valued inputs (pretend that they're 1). */
  1082. if (!BN_is_zero(&points[0]->Z)) {
  1083. if (!BN_copy(prod_Z[0], &points[0]->Z)) {
  1084. goto err;
  1085. }
  1086. } else {
  1087. if (group->meth->field_set_to_one != 0) {
  1088. if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) {
  1089. goto err;
  1090. }
  1091. } else {
  1092. if (!BN_one(prod_Z[0])) {
  1093. goto err;
  1094. }
  1095. }
  1096. }
  1097. for (i = 1; i < num; i++) {
  1098. if (!BN_is_zero(&points[i]->Z)) {
  1099. if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
  1100. &points[i]->Z, ctx)) {
  1101. goto err;
  1102. }
  1103. } else {
  1104. if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
  1105. goto err;
  1106. }
  1107. }
  1108. }
  1109. /* Now use a single explicit inversion to replace every
  1110. * non-zero points[i]->Z by its inverse. */
  1111. if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx)) {
  1112. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1113. goto err;
  1114. }
  1115. if (group->meth->field_encode != NULL) {
  1116. /* In the Montgomery case, we just turned R*H (representing H)
  1117. * into 1/(R*H), but we need R*(1/H) (representing 1/H);
  1118. * i.e. we need to multiply by the Montgomery factor twice. */
  1119. if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
  1120. !group->meth->field_encode(group, tmp, tmp, ctx)) {
  1121. goto err;
  1122. }
  1123. }
  1124. for (i = num - 1; i > 0; --i) {
  1125. /* Loop invariant: tmp is the product of the inverses of
  1126. * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
  1127. if (BN_is_zero(&points[i]->Z)) {
  1128. continue;
  1129. }
  1130. /* Set tmp_Z to the inverse of points[i]->Z (as product
  1131. * of Z inverses 0 .. i, Z values 0 .. i - 1). */
  1132. if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
  1133. /* Update tmp to satisfy the loop invariant for i - 1. */
  1134. !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
  1135. /* Replace points[i]->Z by its inverse. */
  1136. !BN_copy(&points[i]->Z, tmp_Z)) {
  1137. goto err;
  1138. }
  1139. }
  1140. /* Replace points[0]->Z by its inverse. */
  1141. if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
  1142. goto err;
  1143. }
  1144. /* Finally, fix up the X and Y coordinates for all points. */
  1145. for (i = 0; i < num; i++) {
  1146. EC_POINT *p = points[i];
  1147. if (!BN_is_zero(&p->Z)) {
  1148. /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1). */
  1149. if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
  1150. !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
  1151. !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
  1152. !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
  1153. goto err;
  1154. }
  1155. if (group->meth->field_set_to_one != NULL) {
  1156. if (!group->meth->field_set_to_one(group, &p->Z, ctx)) {
  1157. goto err;
  1158. }
  1159. } else {
  1160. if (!BN_one(&p->Z)) {
  1161. goto err;
  1162. }
  1163. }
  1164. p->Z_is_one = 1;
  1165. }
  1166. }
  1167. ret = 1;
  1168. err:
  1169. BN_CTX_end(ctx);
  1170. BN_CTX_free(new_ctx);
  1171. if (prod_Z != NULL) {
  1172. for (i = 0; i < num; i++) {
  1173. if (prod_Z[i] == NULL) {
  1174. break;
  1175. }
  1176. BN_clear_free(prod_Z[i]);
  1177. }
  1178. OPENSSL_free(prod_Z);
  1179. }
  1180. return ret;
  1181. }
  1182. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1183. const BIGNUM *b, BN_CTX *ctx) {
  1184. return BN_mod_mul(r, a, b, &group->field, ctx);
  1185. }
  1186. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1187. BN_CTX *ctx) {
  1188. return BN_mod_sqr(r, a, &group->field, ctx);
  1189. }