ecp_smpl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. #include <openssl/err.h>
  65. #include <openssl/symhacks.h>
  66. #include "ec_lcl.h"
  67. const EC_METHOD *EC_GFp_simple_method(void)
  68. {
  69. static const EC_METHOD ret = {
  70. NID_X9_62_prime_field,
  71. ec_GFp_simple_group_init,
  72. ec_GFp_simple_group_finish,
  73. ec_GFp_simple_group_clear_finish,
  74. ec_GFp_simple_group_copy,
  75. ec_GFp_simple_group_set_curve,
  76. ec_GFp_simple_group_get_curve,
  77. ec_GFp_simple_group_get_degree,
  78. ec_GFp_simple_group_check_discriminant,
  79. ec_GFp_simple_point_init,
  80. ec_GFp_simple_point_finish,
  81. ec_GFp_simple_point_clear_finish,
  82. ec_GFp_simple_point_copy,
  83. ec_GFp_simple_point_set_to_infinity,
  84. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  85. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  86. ec_GFp_simple_point_set_affine_coordinates,
  87. ec_GFp_simple_point_get_affine_coordinates,
  88. ec_GFp_simple_set_compressed_coordinates,
  89. ec_GFp_simple_point2oct,
  90. ec_GFp_simple_oct2point,
  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_sign(&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. BN_CTX_end(ctx);
  296. if (new_ctx != NULL)
  297. BN_CTX_free(new_ctx);
  298. return ret;
  299. }
  300. int ec_GFp_simple_point_init(EC_POINT *point)
  301. {
  302. BN_init(&point->X);
  303. BN_init(&point->Y);
  304. BN_init(&point->Z);
  305. point->Z_is_one = 0;
  306. return 1;
  307. }
  308. void ec_GFp_simple_point_finish(EC_POINT *point)
  309. {
  310. BN_free(&point->X);
  311. BN_free(&point->Y);
  312. BN_free(&point->Z);
  313. }
  314. void ec_GFp_simple_point_clear_finish(EC_POINT *point)
  315. {
  316. BN_clear_free(&point->X);
  317. BN_clear_free(&point->Y);
  318. BN_clear_free(&point->Z);
  319. point->Z_is_one = 0;
  320. }
  321. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  322. {
  323. if (!BN_copy(&dest->X, &src->X)) return 0;
  324. if (!BN_copy(&dest->Y, &src->Y)) return 0;
  325. if (!BN_copy(&dest->Z, &src->Z)) return 0;
  326. dest->Z_is_one = src->Z_is_one;
  327. return 1;
  328. }
  329. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  330. {
  331. point->Z_is_one = 0;
  332. BN_zero(&point->Z);
  333. return 1;
  334. }
  335. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
  336. const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
  337. {
  338. BN_CTX *new_ctx = NULL;
  339. int ret = 0;
  340. if (ctx == NULL)
  341. {
  342. ctx = new_ctx = BN_CTX_new();
  343. if (ctx == NULL)
  344. return 0;
  345. }
  346. if (x != NULL)
  347. {
  348. if (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;
  349. if (group->meth->field_encode)
  350. {
  351. if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
  352. }
  353. }
  354. if (y != NULL)
  355. {
  356. if (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;
  357. if (group->meth->field_encode)
  358. {
  359. if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
  360. }
  361. }
  362. if (z != NULL)
  363. {
  364. int Z_is_one;
  365. if (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;
  366. Z_is_one = BN_is_one(&point->Z);
  367. if (group->meth->field_encode)
  368. {
  369. if (Z_is_one && (group->meth->field_set_to_one != 0))
  370. {
  371. if (!group->meth->field_set_to_one(group, &point->Z, ctx)) goto err;
  372. }
  373. else
  374. {
  375. if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
  376. }
  377. }
  378. point->Z_is_one = Z_is_one;
  379. }
  380. ret = 1;
  381. err:
  382. if (new_ctx != NULL)
  383. BN_CTX_free(new_ctx);
  384. return ret;
  385. }
  386. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
  387. BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
  388. {
  389. BN_CTX *new_ctx = NULL;
  390. int ret = 0;
  391. if (group->meth->field_decode != 0)
  392. {
  393. if (ctx == NULL)
  394. {
  395. ctx = new_ctx = BN_CTX_new();
  396. if (ctx == NULL)
  397. return 0;
  398. }
  399. if (x != NULL)
  400. {
  401. if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
  402. }
  403. if (y != NULL)
  404. {
  405. if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
  406. }
  407. if (z != NULL)
  408. {
  409. if (!group->meth->field_decode(group, z, &point->Z, ctx)) goto err;
  410. }
  411. }
  412. else
  413. {
  414. if (x != NULL)
  415. {
  416. if (!BN_copy(x, &point->X)) goto err;
  417. }
  418. if (y != NULL)
  419. {
  420. if (!BN_copy(y, &point->Y)) goto err;
  421. }
  422. if (z != NULL)
  423. {
  424. if (!BN_copy(z, &point->Z)) goto err;
  425. }
  426. }
  427. ret = 1;
  428. err:
  429. if (new_ctx != NULL)
  430. BN_CTX_free(new_ctx);
  431. return ret;
  432. }
  433. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  434. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
  435. {
  436. if (x == NULL || y == NULL)
  437. {
  438. /* unlike for projective coordinates, we do not tolerate this */
  439. ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
  440. return 0;
  441. }
  442. return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);
  443. }
  444. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
  445. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  446. {
  447. BN_CTX *new_ctx = NULL;
  448. BIGNUM *Z, *Z_1, *Z_2, *Z_3;
  449. const BIGNUM *Z_;
  450. int ret = 0;
  451. if (EC_POINT_is_at_infinity(group, point))
  452. {
  453. ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
  454. return 0;
  455. }
  456. if (ctx == NULL)
  457. {
  458. ctx = new_ctx = BN_CTX_new();
  459. if (ctx == NULL)
  460. return 0;
  461. }
  462. BN_CTX_start(ctx);
  463. Z = BN_CTX_get(ctx);
  464. Z_1 = BN_CTX_get(ctx);
  465. Z_2 = BN_CTX_get(ctx);
  466. Z_3 = BN_CTX_get(ctx);
  467. if (Z_3 == NULL) goto err;
  468. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  469. if (group->meth->field_decode)
  470. {
  471. if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
  472. Z_ = Z;
  473. }
  474. else
  475. {
  476. Z_ = &point->Z;
  477. }
  478. if (BN_is_one(Z_))
  479. {
  480. if (group->meth->field_decode)
  481. {
  482. if (x != NULL)
  483. {
  484. if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
  485. }
  486. if (y != NULL)
  487. {
  488. if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
  489. }
  490. }
  491. else
  492. {
  493. if (x != NULL)
  494. {
  495. if (!BN_copy(x, &point->X)) goto err;
  496. }
  497. if (y != NULL)
  498. {
  499. if (!BN_copy(y, &point->Y)) goto err;
  500. }
  501. }
  502. }
  503. else
  504. {
  505. if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
  506. {
  507. ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB);
  508. goto err;
  509. }
  510. if (group->meth->field_encode == 0)
  511. {
  512. /* field_sqr works on standard representation */
  513. if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) goto err;
  514. }
  515. else
  516. {
  517. if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
  518. }
  519. if (x != NULL)
  520. {
  521. /* in the Montgomery case, field_mul will cancel out Montgomery factor in X: */
  522. if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx)) goto err;
  523. }
  524. if (y != NULL)
  525. {
  526. if (group->meth->field_encode == 0)
  527. {
  528. /* field_mul works on standard representation */
  529. if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) goto err;
  530. }
  531. else
  532. {
  533. if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
  534. }
  535. /* in the Montgomery case, field_mul will cancel out Montgomery factor in Y: */
  536. if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) goto err;
  537. }
  538. }
  539. ret = 1;
  540. err:
  541. BN_CTX_end(ctx);
  542. if (new_ctx != NULL)
  543. BN_CTX_free(new_ctx);
  544. return ret;
  545. }
  546. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
  547. const BIGNUM *x_, int y_bit, BN_CTX *ctx)
  548. {
  549. BN_CTX *new_ctx = NULL;
  550. BIGNUM *tmp1, *tmp2, *x, *y;
  551. int ret = 0;
  552. if (ctx == NULL)
  553. {
  554. ctx = new_ctx = BN_CTX_new();
  555. if (ctx == NULL)
  556. return 0;
  557. }
  558. y_bit = (y_bit != 0);
  559. BN_CTX_start(ctx);
  560. tmp1 = BN_CTX_get(ctx);
  561. tmp2 = BN_CTX_get(ctx);
  562. x = BN_CTX_get(ctx);
  563. y = BN_CTX_get(ctx);
  564. if (y == NULL) goto err;
  565. /* Recover y. We have a Weierstrass equation
  566. * y^2 = x^3 + a*x + b,
  567. * so y is one of the square roots of x^3 + a*x + b.
  568. */
  569. /* tmp1 := x^3 */
  570. if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
  571. if (group->meth->field_decode == 0)
  572. {
  573. /* field_{sqr,mul} work on standard representation */
  574. if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
  575. if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
  576. }
  577. else
  578. {
  579. if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
  580. if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
  581. }
  582. /* tmp1 := tmp1 + a*x */
  583. if (group->a_is_minus3)
  584. {
  585. if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
  586. if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
  587. if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  588. }
  589. else
  590. {
  591. if (group->meth->field_decode)
  592. {
  593. if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
  594. if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
  595. }
  596. else
  597. {
  598. /* field_mul works on standard representation */
  599. if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
  600. }
  601. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  602. }
  603. /* tmp1 := tmp1 + b */
  604. if (group->meth->field_decode)
  605. {
  606. if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
  607. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  608. }
  609. else
  610. {
  611. if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
  612. }
  613. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
  614. {
  615. unsigned long err = ERR_peek_error();
  616. if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
  617. {
  618. (void)ERR_get_error();
  619. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
  620. }
  621. else
  622. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
  623. goto err;
  624. }
  625. if (y_bit != BN_is_odd(y))
  626. {
  627. if (BN_is_zero(y))
  628. {
  629. int kron;
  630. kron = BN_kronecker(x, &group->field, ctx);
  631. if (kron == -2) goto err;
  632. if (kron == 1)
  633. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
  634. else
  635. /* BN_mod_sqrt() should have cought this error (not a square) */
  636. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
  637. goto err;
  638. }
  639. if (!BN_usub(y, &group->field, y)) goto err;
  640. }
  641. if (y_bit != BN_is_odd(y))
  642. {
  643. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
  644. goto err;
  645. }
  646. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  647. ret = 1;
  648. err:
  649. BN_CTX_end(ctx);
  650. if (new_ctx != NULL)
  651. BN_CTX_free(new_ctx);
  652. return ret;
  653. }
  654. size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
  655. unsigned char *buf, size_t len, BN_CTX *ctx)
  656. {
  657. size_t ret;
  658. BN_CTX *new_ctx = NULL;
  659. int used_ctx = 0;
  660. BIGNUM *x, *y;
  661. size_t field_len, i, skip;
  662. if ((form != POINT_CONVERSION_COMPRESSED)
  663. && (form != POINT_CONVERSION_UNCOMPRESSED)
  664. && (form != POINT_CONVERSION_HYBRID))
  665. {
  666. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  667. goto err;
  668. }
  669. if (EC_POINT_is_at_infinity(group, point))
  670. {
  671. /* encodes to a single 0 octet */
  672. if (buf != NULL)
  673. {
  674. if (len < 1)
  675. {
  676. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  677. return 0;
  678. }
  679. buf[0] = 0;
  680. }
  681. return 1;
  682. }
  683. /* ret := required output buffer length */
  684. field_len = BN_num_bytes(&group->field);
  685. ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  686. /* if 'buf' is NULL, just return required length */
  687. if (buf != NULL)
  688. {
  689. if (len < ret)
  690. {
  691. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  692. goto err;
  693. }
  694. if (ctx == NULL)
  695. {
  696. ctx = new_ctx = BN_CTX_new();
  697. if (ctx == NULL)
  698. return 0;
  699. }
  700. BN_CTX_start(ctx);
  701. used_ctx = 1;
  702. x = BN_CTX_get(ctx);
  703. y = BN_CTX_get(ctx);
  704. if (y == NULL) goto err;
  705. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  706. if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
  707. buf[0] = form + 1;
  708. else
  709. buf[0] = form;
  710. i = 1;
  711. skip = field_len - BN_num_bytes(x);
  712. if (skip > field_len)
  713. {
  714. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  715. goto err;
  716. }
  717. while (skip > 0)
  718. {
  719. buf[i++] = 0;
  720. skip--;
  721. }
  722. skip = BN_bn2bin(x, buf + i);
  723. i += skip;
  724. if (i != 1 + field_len)
  725. {
  726. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  727. goto err;
  728. }
  729. if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
  730. {
  731. skip = field_len - BN_num_bytes(y);
  732. if (skip > field_len)
  733. {
  734. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  735. goto err;
  736. }
  737. while (skip > 0)
  738. {
  739. buf[i++] = 0;
  740. skip--;
  741. }
  742. skip = BN_bn2bin(y, buf + i);
  743. i += skip;
  744. }
  745. if (i != ret)
  746. {
  747. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  748. goto err;
  749. }
  750. }
  751. if (used_ctx)
  752. BN_CTX_end(ctx);
  753. if (new_ctx != NULL)
  754. BN_CTX_free(new_ctx);
  755. return ret;
  756. err:
  757. if (used_ctx)
  758. BN_CTX_end(ctx);
  759. if (new_ctx != NULL)
  760. BN_CTX_free(new_ctx);
  761. return 0;
  762. }
  763. int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  764. const unsigned char *buf, size_t len, BN_CTX *ctx)
  765. {
  766. point_conversion_form_t form;
  767. int y_bit;
  768. BN_CTX *new_ctx = NULL;
  769. BIGNUM *x, *y;
  770. size_t field_len, enc_len;
  771. int ret = 0;
  772. if (len == 0)
  773. {
  774. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  775. return 0;
  776. }
  777. form = buf[0];
  778. y_bit = form & 1;
  779. form = form & ~1U;
  780. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  781. && (form != POINT_CONVERSION_UNCOMPRESSED)
  782. && (form != POINT_CONVERSION_HYBRID))
  783. {
  784. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  785. return 0;
  786. }
  787. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
  788. {
  789. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  790. return 0;
  791. }
  792. if (form == 0)
  793. {
  794. if (len != 1)
  795. {
  796. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  797. return 0;
  798. }
  799. return EC_POINT_set_to_infinity(group, point);
  800. }
  801. field_len = BN_num_bytes(&group->field);
  802. enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  803. if (len != enc_len)
  804. {
  805. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  806. return 0;
  807. }
  808. if (ctx == NULL)
  809. {
  810. ctx = new_ctx = BN_CTX_new();
  811. if (ctx == NULL)
  812. return 0;
  813. }
  814. BN_CTX_start(ctx);
  815. x = BN_CTX_get(ctx);
  816. y = BN_CTX_get(ctx);
  817. if (y == NULL) goto err;
  818. if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
  819. if (BN_ucmp(x, &group->field) >= 0)
  820. {
  821. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  822. goto err;
  823. }
  824. if (form == POINT_CONVERSION_COMPRESSED)
  825. {
  826. if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
  827. }
  828. else
  829. {
  830. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
  831. if (BN_ucmp(y, &group->field) >= 0)
  832. {
  833. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  834. goto err;
  835. }
  836. if (form == POINT_CONVERSION_HYBRID)
  837. {
  838. if (y_bit != BN_is_odd(y))
  839. {
  840. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  841. goto err;
  842. }
  843. }
  844. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  845. }
  846. if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
  847. {
  848. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  849. goto err;
  850. }
  851. ret = 1;
  852. err:
  853. BN_CTX_end(ctx);
  854. if (new_ctx != NULL)
  855. BN_CTX_free(new_ctx);
  856. return ret;
  857. }
  858. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  859. {
  860. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
  861. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  862. const BIGNUM *p;
  863. BN_CTX *new_ctx = NULL;
  864. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  865. int ret = 0;
  866. if (a == b)
  867. return EC_POINT_dbl(group, r, a, ctx);
  868. if (EC_POINT_is_at_infinity(group, a))
  869. return EC_POINT_copy(r, b);
  870. if (EC_POINT_is_at_infinity(group, b))
  871. return EC_POINT_copy(r, a);
  872. field_mul = group->meth->field_mul;
  873. field_sqr = group->meth->field_sqr;
  874. p = &group->field;
  875. if (ctx == NULL)
  876. {
  877. ctx = new_ctx = BN_CTX_new();
  878. if (ctx == NULL)
  879. return 0;
  880. }
  881. BN_CTX_start(ctx);
  882. n0 = BN_CTX_get(ctx);
  883. n1 = BN_CTX_get(ctx);
  884. n2 = BN_CTX_get(ctx);
  885. n3 = BN_CTX_get(ctx);
  886. n4 = BN_CTX_get(ctx);
  887. n5 = BN_CTX_get(ctx);
  888. n6 = BN_CTX_get(ctx);
  889. if (n6 == NULL) goto end;
  890. /* Note that in this function we must not read components of 'a' or 'b'
  891. * once we have written the corresponding components of 'r'.
  892. * ('r' might be one of 'a' or 'b'.)
  893. */
  894. /* n1, n2 */
  895. if (b->Z_is_one)
  896. {
  897. if (!BN_copy(n1, &a->X)) goto end;
  898. if (!BN_copy(n2, &a->Y)) goto end;
  899. /* n1 = X_a */
  900. /* n2 = Y_a */
  901. }
  902. else
  903. {
  904. if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
  905. if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
  906. /* n1 = X_a * Z_b^2 */
  907. if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
  908. if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
  909. /* n2 = Y_a * Z_b^3 */
  910. }
  911. /* n3, n4 */
  912. if (a->Z_is_one)
  913. {
  914. if (!BN_copy(n3, &b->X)) goto end;
  915. if (!BN_copy(n4, &b->Y)) goto end;
  916. /* n3 = X_b */
  917. /* n4 = Y_b */
  918. }
  919. else
  920. {
  921. if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
  922. if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
  923. /* n3 = X_b * Z_a^2 */
  924. if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
  925. if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
  926. /* n4 = Y_b * Z_a^3 */
  927. }
  928. /* n5, n6 */
  929. if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
  930. if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
  931. /* n5 = n1 - n3 */
  932. /* n6 = n2 - n4 */
  933. if (BN_is_zero(n5))
  934. {
  935. if (BN_is_zero(n6))
  936. {
  937. /* a is the same point as b */
  938. BN_CTX_end(ctx);
  939. ret = EC_POINT_dbl(group, r, a, ctx);
  940. ctx = NULL;
  941. goto end;
  942. }
  943. else
  944. {
  945. /* a is the inverse of b */
  946. BN_zero(&r->Z);
  947. r->Z_is_one = 0;
  948. ret = 1;
  949. goto end;
  950. }
  951. }
  952. /* 'n7', 'n8' */
  953. if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
  954. if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
  955. /* 'n7' = n1 + n3 */
  956. /* 'n8' = n2 + n4 */
  957. /* Z_r */
  958. if (a->Z_is_one && b->Z_is_one)
  959. {
  960. if (!BN_copy(&r->Z, n5)) goto end;
  961. }
  962. else
  963. {
  964. if (a->Z_is_one)
  965. { if (!BN_copy(n0, &b->Z)) goto end; }
  966. else if (b->Z_is_one)
  967. { if (!BN_copy(n0, &a->Z)) goto end; }
  968. else
  969. { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
  970. if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
  971. }
  972. r->Z_is_one = 0;
  973. /* Z_r = Z_a * Z_b * n5 */
  974. /* X_r */
  975. if (!field_sqr(group, n0, n6, ctx)) goto end;
  976. if (!field_sqr(group, n4, n5, ctx)) goto end;
  977. if (!field_mul(group, n3, n1, n4, ctx)) goto end;
  978. if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
  979. /* X_r = n6^2 - n5^2 * 'n7' */
  980. /* 'n9' */
  981. if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
  982. if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
  983. /* n9 = n5^2 * 'n7' - 2 * X_r */
  984. /* Y_r */
  985. if (!field_mul(group, n0, n0, n6, ctx)) goto end;
  986. if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
  987. if (!field_mul(group, n1, n2, n5, ctx)) goto end;
  988. if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
  989. if (BN_is_odd(n0))
  990. if (!BN_add(n0, n0, p)) goto end;
  991. /* now 0 <= n0 < 2*p, and n0 is even */
  992. if (!BN_rshift1(&r->Y, n0)) goto end;
  993. /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  994. ret = 1;
  995. end:
  996. if (ctx) /* otherwise we already called BN_CTX_end */
  997. BN_CTX_end(ctx);
  998. if (new_ctx != NULL)
  999. BN_CTX_free(new_ctx);
  1000. return ret;
  1001. }
  1002. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
  1003. {
  1004. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
  1005. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  1006. const BIGNUM *p;
  1007. BN_CTX *new_ctx = NULL;
  1008. BIGNUM *n0, *n1, *n2, *n3;
  1009. int ret = 0;
  1010. if (EC_POINT_is_at_infinity(group, a))
  1011. {
  1012. BN_zero(&r->Z);
  1013. r->Z_is_one = 0;
  1014. return 1;
  1015. }
  1016. field_mul = group->meth->field_mul;
  1017. field_sqr = group->meth->field_sqr;
  1018. p = &group->field;
  1019. if (ctx == NULL)
  1020. {
  1021. ctx = new_ctx = BN_CTX_new();
  1022. if (ctx == NULL)
  1023. return 0;
  1024. }
  1025. BN_CTX_start(ctx);
  1026. n0 = BN_CTX_get(ctx);
  1027. n1 = BN_CTX_get(ctx);
  1028. n2 = BN_CTX_get(ctx);
  1029. n3 = BN_CTX_get(ctx);
  1030. if (n3 == NULL) goto err;
  1031. /* Note that in this function we must not read components of 'a'
  1032. * once we have written the corresponding components of 'r'.
  1033. * ('r' might the same as 'a'.)
  1034. */
  1035. /* n1 */
  1036. if (a->Z_is_one)
  1037. {
  1038. if (!field_sqr(group, n0, &a->X, ctx)) goto err;
  1039. if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
  1040. if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
  1041. if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
  1042. /* n1 = 3 * X_a^2 + a_curve */
  1043. }
  1044. else if (group->a_is_minus3)
  1045. {
  1046. if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
  1047. if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
  1048. if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
  1049. if (!field_mul(group, n1, n0, n2, ctx)) goto err;
  1050. if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
  1051. if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
  1052. /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  1053. * = 3 * X_a^2 - 3 * Z_a^4 */
  1054. }
  1055. else
  1056. {
  1057. if (!field_sqr(group, n0, &a->X, ctx)) goto err;
  1058. if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
  1059. if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
  1060. if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
  1061. if (!field_sqr(group, n1, n1, ctx)) goto err;
  1062. if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
  1063. if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
  1064. /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
  1065. }
  1066. /* Z_r */
  1067. if (a->Z_is_one)
  1068. {
  1069. if (!BN_copy(n0, &a->Y)) goto err;
  1070. }
  1071. else
  1072. {
  1073. if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
  1074. }
  1075. if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
  1076. r->Z_is_one = 0;
  1077. /* Z_r = 2 * Y_a * Z_a */
  1078. /* n2 */
  1079. if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
  1080. if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
  1081. if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
  1082. /* n2 = 4 * X_a * Y_a^2 */
  1083. /* X_r */
  1084. if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
  1085. if (!field_sqr(group, &r->X, n1, ctx)) goto err;
  1086. if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
  1087. /* X_r = n1^2 - 2 * n2 */
  1088. /* n3 */
  1089. if (!field_sqr(group, n0, n3, ctx)) goto err;
  1090. if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
  1091. /* n3 = 8 * Y_a^4 */
  1092. /* Y_r */
  1093. if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
  1094. if (!field_mul(group, n0, n1, n0, ctx)) goto err;
  1095. if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
  1096. /* Y_r = n1 * (n2 - X_r) - n3 */
  1097. ret = 1;
  1098. err:
  1099. BN_CTX_end(ctx);
  1100. if (new_ctx != NULL)
  1101. BN_CTX_free(new_ctx);
  1102. return ret;
  1103. }
  1104. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  1105. {
  1106. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
  1107. /* point is its own inverse */
  1108. return 1;
  1109. return BN_usub(&point->Y, &group->field, &point->Y);
  1110. }
  1111. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  1112. {
  1113. return BN_is_zero(&point->Z);
  1114. }
  1115. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
  1116. {
  1117. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
  1118. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  1119. const BIGNUM *p;
  1120. BN_CTX *new_ctx = NULL;
  1121. BIGNUM *rh, *tmp1, *tmp2, *Z4, *Z6;
  1122. int ret = -1;
  1123. if (EC_POINT_is_at_infinity(group, point))
  1124. return 1;
  1125. field_mul = group->meth->field_mul;
  1126. field_sqr = group->meth->field_sqr;
  1127. p = &group->field;
  1128. if (ctx == NULL)
  1129. {
  1130. ctx = new_ctx = BN_CTX_new();
  1131. if (ctx == NULL)
  1132. return -1;
  1133. }
  1134. BN_CTX_start(ctx);
  1135. rh = BN_CTX_get(ctx);
  1136. tmp1 = BN_CTX_get(ctx);
  1137. tmp2 = BN_CTX_get(ctx);
  1138. Z4 = BN_CTX_get(ctx);
  1139. Z6 = BN_CTX_get(ctx);
  1140. if (Z6 == NULL) goto err;
  1141. /* We have a curve defined by a Weierstrass equation
  1142. * y^2 = x^3 + a*x + b.
  1143. * The point to consider is given in Jacobian projective coordinates
  1144. * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  1145. * Substituting this and multiplying by Z^6 transforms the above equation into
  1146. * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  1147. * To test this, we add up the right-hand side in 'rh'.
  1148. */
  1149. /* rh := X^3 */
  1150. if (!field_sqr(group, rh, &point->X, ctx)) goto err;
  1151. if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
  1152. if (!point->Z_is_one)
  1153. {
  1154. if (!field_sqr(group, tmp1, &point->Z, ctx)) goto err;
  1155. if (!field_sqr(group, Z4, tmp1, ctx)) goto err;
  1156. if (!field_mul(group, Z6, Z4, tmp1, ctx)) goto err;
  1157. /* rh := rh + a*X*Z^4 */
  1158. if (!field_mul(group, tmp1, &point->X, Z4, ctx)) goto err;
  1159. if (group->a_is_minus3)
  1160. {
  1161. if (!BN_mod_lshift1_quick(tmp2, tmp1, p)) goto err;
  1162. if (!BN_mod_add_quick(tmp2, tmp2, tmp1, p)) goto err;
  1163. if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
  1164. }
  1165. else
  1166. {
  1167. if (!field_mul(group, tmp2, tmp1, &group->a, ctx)) goto err;
  1168. if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
  1169. }
  1170. /* rh := rh + b*Z^6 */
  1171. if (!field_mul(group, tmp1, &group->b, Z6, ctx)) goto err;
  1172. if (!BN_mod_add_quick(rh, rh, tmp1, p)) goto err;
  1173. }
  1174. else
  1175. {
  1176. /* point->Z_is_one */
  1177. /* rh := rh + a*X */
  1178. if (group->a_is_minus3)
  1179. {
  1180. if (!BN_mod_lshift1_quick(tmp2, &point->X, p)) goto err;
  1181. if (!BN_mod_add_quick(tmp2, tmp2, &point->X, p)) goto err;
  1182. if (!BN_mod_sub_quick(rh, rh, tmp2, p)) goto err;
  1183. }
  1184. else
  1185. {
  1186. if (!field_mul(group, tmp2, &point->X, &group->a, ctx)) goto err;
  1187. if (!BN_mod_add_quick(rh, rh, tmp2, p)) goto err;
  1188. }
  1189. /* rh := rh + b */
  1190. if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
  1191. }
  1192. /* 'lh' := Y^2 */
  1193. if (!field_sqr(group, tmp1, &point->Y, ctx)) goto err;
  1194. ret = (0 == BN_cmp(tmp1, rh));
  1195. err:
  1196. BN_CTX_end(ctx);
  1197. if (new_ctx != NULL)
  1198. BN_CTX_free(new_ctx);
  1199. return ret;
  1200. }
  1201. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  1202. {
  1203. /* return values:
  1204. * -1 error
  1205. * 0 equal (in affine coordinates)
  1206. * 1 not equal
  1207. */
  1208. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
  1209. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  1210. BN_CTX *new_ctx = NULL;
  1211. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  1212. const BIGNUM *tmp1_, *tmp2_;
  1213. int ret = -1;
  1214. if (EC_POINT_is_at_infinity(group, a))
  1215. {
  1216. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  1217. }
  1218. if (a->Z_is_one && b->Z_is_one)
  1219. {
  1220. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  1221. }
  1222. field_mul = group->meth->field_mul;
  1223. field_sqr = group->meth->field_sqr;
  1224. if (ctx == NULL)
  1225. {
  1226. ctx = new_ctx = BN_CTX_new();
  1227. if (ctx == NULL)
  1228. return -1;
  1229. }
  1230. BN_CTX_start(ctx);
  1231. tmp1 = BN_CTX_get(ctx);
  1232. tmp2 = BN_CTX_get(ctx);
  1233. Za23 = BN_CTX_get(ctx);
  1234. Zb23 = BN_CTX_get(ctx);
  1235. if (Zb23 == NULL) goto end;
  1236. /* We have to decide whether
  1237. * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  1238. * or equivalently, whether
  1239. * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  1240. */
  1241. if (!b->Z_is_one)
  1242. {
  1243. if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
  1244. if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
  1245. tmp1_ = tmp1;
  1246. }
  1247. else
  1248. tmp1_ = &a->X;
  1249. if (!a->Z_is_one)
  1250. {
  1251. if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
  1252. if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
  1253. tmp2_ = tmp2;
  1254. }
  1255. else
  1256. tmp2_ = &b->X;
  1257. /* compare X_a*Z_b^2 with X_b*Z_a^2 */
  1258. if (BN_cmp(tmp1_, tmp2_) != 0)
  1259. {
  1260. ret = 1; /* points differ */
  1261. goto end;
  1262. }
  1263. if (!b->Z_is_one)
  1264. {
  1265. if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
  1266. if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
  1267. /* tmp1_ = tmp1 */
  1268. }
  1269. else
  1270. tmp1_ = &a->Y;
  1271. if (!a->Z_is_one)
  1272. {
  1273. if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
  1274. if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
  1275. /* tmp2_ = tmp2 */
  1276. }
  1277. else
  1278. tmp2_ = &b->Y;
  1279. /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
  1280. if (BN_cmp(tmp1_, tmp2_) != 0)
  1281. {
  1282. ret = 1; /* points differ */
  1283. goto end;
  1284. }
  1285. /* points are equal */
  1286. ret = 0;
  1287. end:
  1288. BN_CTX_end(ctx);
  1289. if (new_ctx != NULL)
  1290. BN_CTX_free(new_ctx);
  1291. return ret;
  1292. }
  1293. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  1294. {
  1295. BN_CTX *new_ctx = NULL;
  1296. BIGNUM *x, *y;
  1297. int ret = 0;
  1298. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  1299. return 1;
  1300. if (ctx == NULL)
  1301. {
  1302. ctx = new_ctx = BN_CTX_new();
  1303. if (ctx == NULL)
  1304. return 0;
  1305. }
  1306. BN_CTX_start(ctx);
  1307. x = BN_CTX_get(ctx);
  1308. y = BN_CTX_get(ctx);
  1309. if (y == NULL) goto err;
  1310. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  1311. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  1312. if (!point->Z_is_one)
  1313. {
  1314. ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
  1315. goto err;
  1316. }
  1317. ret = 1;
  1318. err:
  1319. BN_CTX_end(ctx);
  1320. if (new_ctx != NULL)
  1321. BN_CTX_free(new_ctx);
  1322. return ret;
  1323. }
  1324. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
  1325. {
  1326. BN_CTX *new_ctx = NULL;
  1327. BIGNUM *tmp0, *tmp1;
  1328. size_t pow2 = 0;
  1329. BIGNUM **heap = NULL;
  1330. size_t i;
  1331. int ret = 0;
  1332. if (num == 0)
  1333. return 1;
  1334. if (ctx == NULL)
  1335. {
  1336. ctx = new_ctx = BN_CTX_new();
  1337. if (ctx == NULL)
  1338. return 0;
  1339. }
  1340. BN_CTX_start(ctx);
  1341. tmp0 = BN_CTX_get(ctx);
  1342. tmp1 = BN_CTX_get(ctx);
  1343. if (tmp0 == NULL || tmp1 == NULL) goto err;
  1344. /* Before converting the individual points, compute inverses of all Z values.
  1345. * Modular inversion is rather slow, but luckily we can do with a single
  1346. * explicit inversion, plus about 3 multiplications per input value.
  1347. */
  1348. pow2 = 1;
  1349. while (num > pow2)
  1350. pow2 <<= 1;
  1351. /* Now pow2 is the smallest power of 2 satifsying pow2 >= num.
  1352. * We need twice that. */
  1353. pow2 <<= 1;
  1354. heap = OPENSSL_malloc(pow2 * sizeof heap[0]);
  1355. if (heap == NULL) goto err;
  1356. /* The array is used as a binary tree, exactly as in heapsort:
  1357. *
  1358. * heap[1]
  1359. * heap[2] heap[3]
  1360. * heap[4] heap[5] heap[6] heap[7]
  1361. * heap[8]heap[9] heap[10]heap[11] heap[12]heap[13] heap[14] heap[15]
  1362. *
  1363. * We put the Z's in the last line;
  1364. * then we set each other node to the product of its two child-nodes (where
  1365. * empty or 0 entries are treated as ones);
  1366. * then we invert heap[1];
  1367. * then we invert each other node by replacing it by the product of its
  1368. * parent (after inversion) and its sibling (before inversion).
  1369. */
  1370. heap[0] = NULL;
  1371. for (i = pow2/2 - 1; i > 0; i--)
  1372. heap[i] = NULL;
  1373. for (i = 0; i < num; i++)
  1374. heap[pow2/2 + i] = &points[i]->Z;
  1375. for (i = pow2/2 + num; i < pow2; i++)
  1376. heap[i] = NULL;
  1377. /* set each node to the product of its children */
  1378. for (i = pow2/2 - 1; i > 0; i--)
  1379. {
  1380. heap[i] = BN_new();
  1381. if (heap[i] == NULL) goto err;
  1382. if (heap[2*i] != NULL)
  1383. {
  1384. if ((heap[2*i + 1] == NULL) || BN_is_zero(heap[2*i + 1]))
  1385. {
  1386. if (!BN_copy(heap[i], heap[2*i])) goto err;
  1387. }
  1388. else
  1389. {
  1390. if (BN_is_zero(heap[2*i]))
  1391. {
  1392. if (!BN_copy(heap[i], heap[2*i + 1])) goto err;
  1393. }
  1394. else
  1395. {
  1396. if (!group->meth->field_mul(group, heap[i],
  1397. heap[2*i], heap[2*i + 1], ctx)) goto err;
  1398. }
  1399. }
  1400. }
  1401. }
  1402. /* invert heap[1] */
  1403. if (!BN_is_zero(heap[1]))
  1404. {
  1405. if (!BN_mod_inverse(heap[1], heap[1], &group->field, ctx))
  1406. {
  1407. ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
  1408. goto err;
  1409. }
  1410. }
  1411. if (group->meth->field_encode != 0)
  1412. {
  1413. /* in the Montgomery case, we just turned R*H (representing H)
  1414. * into 1/(R*H), but we need R*(1/H) (representing 1/H);
  1415. * i.e. we have need to multiply by the Montgomery factor twice */
  1416. if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
  1417. if (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;
  1418. }
  1419. /* set other heap[i]'s to their inverses */
  1420. for (i = 2; i < pow2/2 + num; i += 2)
  1421. {
  1422. /* i is even */
  1423. if ((heap[i + 1] != NULL) && !BN_is_zero(heap[i + 1]))
  1424. {
  1425. if (!group->meth->field_mul(group, tmp0, heap[i/2], heap[i + 1], ctx)) goto err;
  1426. if (!group->meth->field_mul(group, tmp1, heap[i/2], heap[i], ctx)) goto err;
  1427. if (!BN_copy(heap[i], tmp0)) goto err;
  1428. if (!BN_copy(heap[i + 1], tmp1)) goto err;
  1429. }
  1430. else
  1431. {
  1432. if (!BN_copy(heap[i], heap[i/2])) goto err;
  1433. }
  1434. }
  1435. /* we have replaced all non-zero Z's by their inverses, now fix up all the points */
  1436. for (i = 0; i < num; i++)
  1437. {
  1438. EC_POINT *p = points[i];
  1439. if (!BN_is_zero(&p->Z))
  1440. {
  1441. /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
  1442. if (!group->meth->field_sqr(group, tmp1, &p->Z, ctx)) goto err;
  1443. if (!group->meth->field_mul(group, &p->X, &p->X, tmp1, ctx)) goto err;
  1444. if (!group->meth->field_mul(group, tmp1, tmp1, &p->Z, ctx)) goto err;
  1445. if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp1, ctx)) goto err;
  1446. if (group->meth->field_set_to_one != 0)
  1447. {
  1448. if (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;
  1449. }
  1450. else
  1451. {
  1452. if (!BN_one(&p->Z)) goto err;
  1453. }
  1454. p->Z_is_one = 1;
  1455. }
  1456. }
  1457. ret = 1;
  1458. err:
  1459. BN_CTX_end(ctx);
  1460. if (new_ctx != NULL)
  1461. BN_CTX_free(new_ctx);
  1462. if (heap != NULL)
  1463. {
  1464. /* heap[pow2/2] .. heap[pow2-1] have not been allocated locally! */
  1465. for (i = pow2/2 - 1; i > 0; i--)
  1466. {
  1467. if (heap[i] != NULL)
  1468. BN_clear_free(heap[i]);
  1469. }
  1470. OPENSSL_free(heap);
  1471. }
  1472. return ret;
  1473. }
  1474. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  1475. {
  1476. return BN_mod_mul(r, a, b, &group->field, ctx);
  1477. }
  1478. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
  1479. {
  1480. return BN_mod_sqr(r, a, &group->field, ctx);
  1481. }