ecp_smpl.c 32 KB

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