ecp_smpl.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. /*
  2. * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/err.h>
  11. #include <openssl/symhacks.h>
  12. #include "ec_lcl.h"
  13. const EC_METHOD *EC_GFp_simple_method(void)
  14. {
  15. static const EC_METHOD ret = {
  16. EC_FLAGS_DEFAULT_OCT,
  17. NID_X9_62_prime_field,
  18. ec_GFp_simple_group_init,
  19. ec_GFp_simple_group_finish,
  20. ec_GFp_simple_group_clear_finish,
  21. ec_GFp_simple_group_copy,
  22. ec_GFp_simple_group_set_curve,
  23. ec_GFp_simple_group_get_curve,
  24. ec_GFp_simple_group_get_degree,
  25. ec_group_simple_order_bits,
  26. ec_GFp_simple_group_check_discriminant,
  27. ec_GFp_simple_point_init,
  28. ec_GFp_simple_point_finish,
  29. ec_GFp_simple_point_clear_finish,
  30. ec_GFp_simple_point_copy,
  31. ec_GFp_simple_point_set_to_infinity,
  32. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  33. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  34. ec_GFp_simple_point_set_affine_coordinates,
  35. ec_GFp_simple_point_get_affine_coordinates,
  36. 0, 0, 0,
  37. ec_GFp_simple_add,
  38. ec_GFp_simple_dbl,
  39. ec_GFp_simple_invert,
  40. ec_GFp_simple_is_at_infinity,
  41. ec_GFp_simple_is_on_curve,
  42. ec_GFp_simple_cmp,
  43. ec_GFp_simple_make_affine,
  44. ec_GFp_simple_points_make_affine,
  45. 0 /* mul */ ,
  46. 0 /* precompute_mult */ ,
  47. 0 /* have_precompute_mult */ ,
  48. ec_GFp_simple_field_mul,
  49. ec_GFp_simple_field_sqr,
  50. 0 /* field_div */ ,
  51. ec_GFp_simple_field_inv,
  52. 0 /* field_encode */ ,
  53. 0 /* field_decode */ ,
  54. 0, /* field_set_to_one */
  55. ec_key_simple_priv2oct,
  56. ec_key_simple_oct2priv,
  57. 0, /* set private */
  58. ec_key_simple_generate_key,
  59. ec_key_simple_check_key,
  60. ec_key_simple_generate_public_key,
  61. 0, /* keycopy */
  62. 0, /* keyfinish */
  63. ecdh_simple_compute_key,
  64. 0, /* field_inverse_mod_ord */
  65. ec_GFp_simple_blind_coordinates,
  66. ec_GFp_simple_ladder_pre,
  67. ec_GFp_simple_ladder_step,
  68. ec_GFp_simple_ladder_post
  69. };
  70. return &ret;
  71. }
  72. /*
  73. * Most method functions in this file are designed to work with
  74. * non-trivial representations of field elements if necessary
  75. * (see ecp_mont.c): while standard modular addition and subtraction
  76. * are used, the field_mul and field_sqr methods will be used for
  77. * multiplication, and field_encode and field_decode (if defined)
  78. * will be used for converting between representations.
  79. *
  80. * Functions ec_GFp_simple_points_make_affine() and
  81. * ec_GFp_simple_point_get_affine_coordinates() specifically assume
  82. * that if a non-trivial representation is used, it is a Montgomery
  83. * representation (i.e. 'encoding' means multiplying by some factor R).
  84. */
  85. int ec_GFp_simple_group_init(EC_GROUP *group)
  86. {
  87. group->field = BN_new();
  88. group->a = BN_new();
  89. group->b = BN_new();
  90. if (group->field == NULL || group->a == NULL || group->b == NULL) {
  91. BN_free(group->field);
  92. BN_free(group->a);
  93. BN_free(group->b);
  94. return 0;
  95. }
  96. group->a_is_minus3 = 0;
  97. return 1;
  98. }
  99. void ec_GFp_simple_group_finish(EC_GROUP *group)
  100. {
  101. BN_free(group->field);
  102. BN_free(group->a);
  103. BN_free(group->b);
  104. }
  105. void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
  106. {
  107. BN_clear_free(group->field);
  108. BN_clear_free(group->a);
  109. BN_clear_free(group->b);
  110. }
  111. int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  112. {
  113. if (!BN_copy(dest->field, src->field))
  114. return 0;
  115. if (!BN_copy(dest->a, src->a))
  116. return 0;
  117. if (!BN_copy(dest->b, src->b))
  118. return 0;
  119. dest->a_is_minus3 = src->a_is_minus3;
  120. return 1;
  121. }
  122. int ec_GFp_simple_group_set_curve(EC_GROUP *group,
  123. const BIGNUM *p, const BIGNUM *a,
  124. const BIGNUM *b, BN_CTX *ctx)
  125. {
  126. int ret = 0;
  127. BN_CTX *new_ctx = NULL;
  128. BIGNUM *tmp_a;
  129. /* p must be a prime > 3 */
  130. if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
  131. ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
  132. return 0;
  133. }
  134. if (ctx == NULL) {
  135. ctx = new_ctx = BN_CTX_new();
  136. if (ctx == NULL)
  137. return 0;
  138. }
  139. BN_CTX_start(ctx);
  140. tmp_a = BN_CTX_get(ctx);
  141. if (tmp_a == NULL)
  142. goto err;
  143. /* group->field */
  144. if (!BN_copy(group->field, p))
  145. goto err;
  146. BN_set_negative(group->field, 0);
  147. /* group->a */
  148. if (!BN_nnmod(tmp_a, a, p, ctx))
  149. goto err;
  150. if (group->meth->field_encode) {
  151. if (!group->meth->field_encode(group, group->a, tmp_a, ctx))
  152. goto err;
  153. } else if (!BN_copy(group->a, tmp_a))
  154. goto err;
  155. /* group->b */
  156. if (!BN_nnmod(group->b, b, p, ctx))
  157. goto err;
  158. if (group->meth->field_encode)
  159. if (!group->meth->field_encode(group, group->b, group->b, ctx))
  160. goto err;
  161. /* group->a_is_minus3 */
  162. if (!BN_add_word(tmp_a, 3))
  163. goto err;
  164. group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));
  165. ret = 1;
  166. err:
  167. BN_CTX_end(ctx);
  168. BN_CTX_free(new_ctx);
  169. return ret;
  170. }
  171. int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  172. BIGNUM *b, BN_CTX *ctx)
  173. {
  174. int ret = 0;
  175. BN_CTX *new_ctx = NULL;
  176. if (p != NULL) {
  177. if (!BN_copy(p, group->field))
  178. return 0;
  179. }
  180. if (a != NULL || b != NULL) {
  181. if (group->meth->field_decode) {
  182. if (ctx == NULL) {
  183. ctx = new_ctx = BN_CTX_new();
  184. if (ctx == NULL)
  185. return 0;
  186. }
  187. if (a != NULL) {
  188. if (!group->meth->field_decode(group, a, group->a, ctx))
  189. goto err;
  190. }
  191. if (b != NULL) {
  192. if (!group->meth->field_decode(group, b, group->b, ctx))
  193. goto err;
  194. }
  195. } else {
  196. if (a != NULL) {
  197. if (!BN_copy(a, group->a))
  198. goto err;
  199. }
  200. if (b != NULL) {
  201. if (!BN_copy(b, group->b))
  202. goto err;
  203. }
  204. }
  205. }
  206. ret = 1;
  207. err:
  208. BN_CTX_free(new_ctx);
  209. return ret;
  210. }
  211. int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
  212. {
  213. return BN_num_bits(group->field);
  214. }
  215. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  216. {
  217. int ret = 0;
  218. BIGNUM *a, *b, *order, *tmp_1, *tmp_2;
  219. const BIGNUM *p = group->field;
  220. BN_CTX *new_ctx = NULL;
  221. if (ctx == NULL) {
  222. ctx = new_ctx = BN_CTX_new();
  223. if (ctx == NULL) {
  224. ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
  225. ERR_R_MALLOC_FAILURE);
  226. goto err;
  227. }
  228. }
  229. BN_CTX_start(ctx);
  230. a = BN_CTX_get(ctx);
  231. b = BN_CTX_get(ctx);
  232. tmp_1 = BN_CTX_get(ctx);
  233. tmp_2 = BN_CTX_get(ctx);
  234. order = BN_CTX_get(ctx);
  235. if (order == NULL)
  236. goto err;
  237. if (group->meth->field_decode) {
  238. if (!group->meth->field_decode(group, a, group->a, ctx))
  239. goto err;
  240. if (!group->meth->field_decode(group, b, group->b, ctx))
  241. goto err;
  242. } else {
  243. if (!BN_copy(a, group->a))
  244. goto err;
  245. if (!BN_copy(b, group->b))
  246. goto err;
  247. }
  248. /*-
  249. * check the discriminant:
  250. * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
  251. * 0 =< a, b < p
  252. */
  253. if (BN_is_zero(a)) {
  254. if (BN_is_zero(b))
  255. goto err;
  256. } else if (!BN_is_zero(b)) {
  257. if (!BN_mod_sqr(tmp_1, a, p, ctx))
  258. goto err;
  259. if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
  260. goto err;
  261. if (!BN_lshift(tmp_1, tmp_2, 2))
  262. goto err;
  263. /* tmp_1 = 4*a^3 */
  264. if (!BN_mod_sqr(tmp_2, b, p, ctx))
  265. goto err;
  266. if (!BN_mul_word(tmp_2, 27))
  267. goto err;
  268. /* tmp_2 = 27*b^2 */
  269. if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
  270. goto err;
  271. if (BN_is_zero(a))
  272. goto err;
  273. }
  274. ret = 1;
  275. err:
  276. BN_CTX_end(ctx);
  277. BN_CTX_free(new_ctx);
  278. return ret;
  279. }
  280. int ec_GFp_simple_point_init(EC_POINT *point)
  281. {
  282. point->X = BN_new();
  283. point->Y = BN_new();
  284. point->Z = BN_new();
  285. point->Z_is_one = 0;
  286. if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
  287. BN_free(point->X);
  288. BN_free(point->Y);
  289. BN_free(point->Z);
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. void ec_GFp_simple_point_finish(EC_POINT *point)
  295. {
  296. BN_free(point->X);
  297. BN_free(point->Y);
  298. BN_free(point->Z);
  299. }
  300. void ec_GFp_simple_point_clear_finish(EC_POINT *point)
  301. {
  302. BN_clear_free(point->X);
  303. BN_clear_free(point->Y);
  304. BN_clear_free(point->Z);
  305. point->Z_is_one = 0;
  306. }
  307. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  308. {
  309. if (!BN_copy(dest->X, src->X))
  310. return 0;
  311. if (!BN_copy(dest->Y, src->Y))
  312. return 0;
  313. if (!BN_copy(dest->Z, src->Z))
  314. return 0;
  315. dest->Z_is_one = src->Z_is_one;
  316. dest->curve_name = src->curve_name;
  317. return 1;
  318. }
  319. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
  320. EC_POINT *point)
  321. {
  322. point->Z_is_one = 0;
  323. BN_zero(point->Z);
  324. return 1;
  325. }
  326. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  327. EC_POINT *point,
  328. const BIGNUM *x,
  329. const BIGNUM *y,
  330. const BIGNUM *z,
  331. BN_CTX *ctx)
  332. {
  333. BN_CTX *new_ctx = NULL;
  334. int ret = 0;
  335. if (ctx == NULL) {
  336. ctx = new_ctx = BN_CTX_new();
  337. if (ctx == NULL)
  338. return 0;
  339. }
  340. if (x != NULL) {
  341. if (!BN_nnmod(point->X, x, group->field, ctx))
  342. goto err;
  343. if (group->meth->field_encode) {
  344. if (!group->meth->field_encode(group, point->X, point->X, ctx))
  345. goto err;
  346. }
  347. }
  348. if (y != NULL) {
  349. if (!BN_nnmod(point->Y, y, group->field, ctx))
  350. goto err;
  351. if (group->meth->field_encode) {
  352. if (!group->meth->field_encode(group, point->Y, point->Y, ctx))
  353. goto err;
  354. }
  355. }
  356. if (z != NULL) {
  357. int Z_is_one;
  358. if (!BN_nnmod(point->Z, z, group->field, ctx))
  359. goto err;
  360. Z_is_one = BN_is_one(point->Z);
  361. if (group->meth->field_encode) {
  362. if (Z_is_one && (group->meth->field_set_to_one != 0)) {
  363. if (!group->meth->field_set_to_one(group, point->Z, ctx))
  364. goto err;
  365. } else {
  366. if (!group->
  367. meth->field_encode(group, point->Z, point->Z, ctx))
  368. goto err;
  369. }
  370. }
  371. point->Z_is_one = Z_is_one;
  372. }
  373. ret = 1;
  374. err:
  375. BN_CTX_free(new_ctx);
  376. return ret;
  377. }
  378. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  379. const EC_POINT *point,
  380. BIGNUM *x, BIGNUM *y,
  381. BIGNUM *z, BN_CTX *ctx)
  382. {
  383. BN_CTX *new_ctx = NULL;
  384. int ret = 0;
  385. if (group->meth->field_decode != 0) {
  386. if (ctx == NULL) {
  387. ctx = new_ctx = BN_CTX_new();
  388. if (ctx == NULL)
  389. return 0;
  390. }
  391. if (x != NULL) {
  392. if (!group->meth->field_decode(group, x, point->X, ctx))
  393. goto err;
  394. }
  395. if (y != NULL) {
  396. if (!group->meth->field_decode(group, y, point->Y, ctx))
  397. goto err;
  398. }
  399. if (z != NULL) {
  400. if (!group->meth->field_decode(group, z, point->Z, ctx))
  401. goto err;
  402. }
  403. } else {
  404. if (x != NULL) {
  405. if (!BN_copy(x, point->X))
  406. goto err;
  407. }
  408. if (y != NULL) {
  409. if (!BN_copy(y, point->Y))
  410. goto err;
  411. }
  412. if (z != NULL) {
  413. if (!BN_copy(z, point->Z))
  414. goto err;
  415. }
  416. }
  417. ret = 1;
  418. err:
  419. BN_CTX_free(new_ctx);
  420. return ret;
  421. }
  422. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
  423. EC_POINT *point,
  424. const BIGNUM *x,
  425. const BIGNUM *y, BN_CTX *ctx)
  426. {
  427. if (x == NULL || y == NULL) {
  428. /*
  429. * unlike for projective coordinates, we do not tolerate this
  430. */
  431. ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
  432. ERR_R_PASSED_NULL_PARAMETER);
  433. return 0;
  434. }
  435. return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y,
  436. BN_value_one(), ctx);
  437. }
  438. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
  439. const EC_POINT *point,
  440. BIGNUM *x, BIGNUM *y,
  441. BN_CTX *ctx)
  442. {
  443. BN_CTX *new_ctx = NULL;
  444. BIGNUM *Z, *Z_1, *Z_2, *Z_3;
  445. const BIGNUM *Z_;
  446. int ret = 0;
  447. if (EC_POINT_is_at_infinity(group, point)) {
  448. ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  449. EC_R_POINT_AT_INFINITY);
  450. return 0;
  451. }
  452. if (ctx == NULL) {
  453. ctx = new_ctx = BN_CTX_new();
  454. if (ctx == NULL)
  455. return 0;
  456. }
  457. BN_CTX_start(ctx);
  458. Z = BN_CTX_get(ctx);
  459. Z_1 = BN_CTX_get(ctx);
  460. Z_2 = BN_CTX_get(ctx);
  461. Z_3 = BN_CTX_get(ctx);
  462. if (Z_3 == NULL)
  463. goto err;
  464. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  465. if (group->meth->field_decode) {
  466. if (!group->meth->field_decode(group, Z, point->Z, ctx))
  467. goto err;
  468. Z_ = Z;
  469. } else {
  470. Z_ = point->Z;
  471. }
  472. if (BN_is_one(Z_)) {
  473. if (group->meth->field_decode) {
  474. if (x != NULL) {
  475. if (!group->meth->field_decode(group, x, point->X, ctx))
  476. goto err;
  477. }
  478. if (y != NULL) {
  479. if (!group->meth->field_decode(group, y, point->Y, ctx))
  480. goto err;
  481. }
  482. } else {
  483. if (x != NULL) {
  484. if (!BN_copy(x, point->X))
  485. goto err;
  486. }
  487. if (y != NULL) {
  488. if (!BN_copy(y, point->Y))
  489. goto err;
  490. }
  491. }
  492. } else {
  493. if (!group->meth->field_inv(group, Z_1, Z_, ctx)) {
  494. ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  495. ERR_R_BN_LIB);
  496. goto err;
  497. }
  498. if (group->meth->field_encode == 0) {
  499. /* field_sqr works on standard representation */
  500. if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
  501. goto err;
  502. } else {
  503. if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx))
  504. goto err;
  505. }
  506. if (x != NULL) {
  507. /*
  508. * in the Montgomery case, field_mul will cancel out Montgomery
  509. * factor in X:
  510. */
  511. if (!group->meth->field_mul(group, x, point->X, Z_2, ctx))
  512. goto err;
  513. }
  514. if (y != NULL) {
  515. if (group->meth->field_encode == 0) {
  516. /*
  517. * field_mul works on standard representation
  518. */
  519. if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
  520. goto err;
  521. } else {
  522. if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx))
  523. goto err;
  524. }
  525. /*
  526. * in the Montgomery case, field_mul will cancel out Montgomery
  527. * factor in Y:
  528. */
  529. if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx))
  530. goto err;
  531. }
  532. }
  533. ret = 1;
  534. err:
  535. BN_CTX_end(ctx);
  536. BN_CTX_free(new_ctx);
  537. return ret;
  538. }
  539. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  540. const EC_POINT *b, BN_CTX *ctx)
  541. {
  542. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  543. const BIGNUM *, BN_CTX *);
  544. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  545. const BIGNUM *p;
  546. BN_CTX *new_ctx = NULL;
  547. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  548. int ret = 0;
  549. if (a == b)
  550. return EC_POINT_dbl(group, r, a, ctx);
  551. if (EC_POINT_is_at_infinity(group, a))
  552. return EC_POINT_copy(r, b);
  553. if (EC_POINT_is_at_infinity(group, b))
  554. return EC_POINT_copy(r, a);
  555. field_mul = group->meth->field_mul;
  556. field_sqr = group->meth->field_sqr;
  557. p = group->field;
  558. if (ctx == NULL) {
  559. ctx = new_ctx = BN_CTX_new();
  560. if (ctx == NULL)
  561. return 0;
  562. }
  563. BN_CTX_start(ctx);
  564. n0 = BN_CTX_get(ctx);
  565. n1 = BN_CTX_get(ctx);
  566. n2 = BN_CTX_get(ctx);
  567. n3 = BN_CTX_get(ctx);
  568. n4 = BN_CTX_get(ctx);
  569. n5 = BN_CTX_get(ctx);
  570. n6 = BN_CTX_get(ctx);
  571. if (n6 == NULL)
  572. goto end;
  573. /*
  574. * Note that in this function we must not read components of 'a' or 'b'
  575. * once we have written the corresponding components of 'r'. ('r' might
  576. * be one of 'a' or 'b'.)
  577. */
  578. /* n1, n2 */
  579. if (b->Z_is_one) {
  580. if (!BN_copy(n1, a->X))
  581. goto end;
  582. if (!BN_copy(n2, a->Y))
  583. goto end;
  584. /* n1 = X_a */
  585. /* n2 = Y_a */
  586. } else {
  587. if (!field_sqr(group, n0, b->Z, ctx))
  588. goto end;
  589. if (!field_mul(group, n1, a->X, n0, ctx))
  590. goto end;
  591. /* n1 = X_a * Z_b^2 */
  592. if (!field_mul(group, n0, n0, b->Z, ctx))
  593. goto end;
  594. if (!field_mul(group, n2, a->Y, n0, ctx))
  595. goto end;
  596. /* n2 = Y_a * Z_b^3 */
  597. }
  598. /* n3, n4 */
  599. if (a->Z_is_one) {
  600. if (!BN_copy(n3, b->X))
  601. goto end;
  602. if (!BN_copy(n4, b->Y))
  603. goto end;
  604. /* n3 = X_b */
  605. /* n4 = Y_b */
  606. } else {
  607. if (!field_sqr(group, n0, a->Z, ctx))
  608. goto end;
  609. if (!field_mul(group, n3, b->X, n0, ctx))
  610. goto end;
  611. /* n3 = X_b * Z_a^2 */
  612. if (!field_mul(group, n0, n0, a->Z, ctx))
  613. goto end;
  614. if (!field_mul(group, n4, b->Y, n0, ctx))
  615. goto end;
  616. /* n4 = Y_b * Z_a^3 */
  617. }
  618. /* n5, n6 */
  619. if (!BN_mod_sub_quick(n5, n1, n3, p))
  620. goto end;
  621. if (!BN_mod_sub_quick(n6, n2, n4, p))
  622. goto end;
  623. /* n5 = n1 - n3 */
  624. /* n6 = n2 - n4 */
  625. if (BN_is_zero(n5)) {
  626. if (BN_is_zero(n6)) {
  627. /* a is the same point as b */
  628. BN_CTX_end(ctx);
  629. ret = EC_POINT_dbl(group, r, a, ctx);
  630. ctx = NULL;
  631. goto end;
  632. } else {
  633. /* a is the inverse of b */
  634. BN_zero(r->Z);
  635. r->Z_is_one = 0;
  636. ret = 1;
  637. goto end;
  638. }
  639. }
  640. /* 'n7', 'n8' */
  641. if (!BN_mod_add_quick(n1, n1, n3, p))
  642. goto end;
  643. if (!BN_mod_add_quick(n2, n2, n4, p))
  644. goto end;
  645. /* 'n7' = n1 + n3 */
  646. /* 'n8' = n2 + n4 */
  647. /* Z_r */
  648. if (a->Z_is_one && b->Z_is_one) {
  649. if (!BN_copy(r->Z, n5))
  650. goto end;
  651. } else {
  652. if (a->Z_is_one) {
  653. if (!BN_copy(n0, b->Z))
  654. goto end;
  655. } else if (b->Z_is_one) {
  656. if (!BN_copy(n0, a->Z))
  657. goto end;
  658. } else {
  659. if (!field_mul(group, n0, a->Z, b->Z, ctx))
  660. goto end;
  661. }
  662. if (!field_mul(group, r->Z, n0, n5, ctx))
  663. goto end;
  664. }
  665. r->Z_is_one = 0;
  666. /* Z_r = Z_a * Z_b * n5 */
  667. /* X_r */
  668. if (!field_sqr(group, n0, n6, ctx))
  669. goto end;
  670. if (!field_sqr(group, n4, n5, ctx))
  671. goto end;
  672. if (!field_mul(group, n3, n1, n4, ctx))
  673. goto end;
  674. if (!BN_mod_sub_quick(r->X, n0, n3, p))
  675. goto end;
  676. /* X_r = n6^2 - n5^2 * 'n7' */
  677. /* 'n9' */
  678. if (!BN_mod_lshift1_quick(n0, r->X, p))
  679. goto end;
  680. if (!BN_mod_sub_quick(n0, n3, n0, p))
  681. goto end;
  682. /* n9 = n5^2 * 'n7' - 2 * X_r */
  683. /* Y_r */
  684. if (!field_mul(group, n0, n0, n6, ctx))
  685. goto end;
  686. if (!field_mul(group, n5, n4, n5, ctx))
  687. goto end; /* now n5 is n5^3 */
  688. if (!field_mul(group, n1, n2, n5, ctx))
  689. goto end;
  690. if (!BN_mod_sub_quick(n0, n0, n1, p))
  691. goto end;
  692. if (BN_is_odd(n0))
  693. if (!BN_add(n0, n0, p))
  694. goto end;
  695. /* now 0 <= n0 < 2*p, and n0 is even */
  696. if (!BN_rshift1(r->Y, n0))
  697. goto end;
  698. /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  699. ret = 1;
  700. end:
  701. BN_CTX_end(ctx);
  702. BN_CTX_free(new_ctx);
  703. return ret;
  704. }
  705. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  706. BN_CTX *ctx)
  707. {
  708. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  709. const BIGNUM *, BN_CTX *);
  710. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  711. const BIGNUM *p;
  712. BN_CTX *new_ctx = NULL;
  713. BIGNUM *n0, *n1, *n2, *n3;
  714. int ret = 0;
  715. if (EC_POINT_is_at_infinity(group, a)) {
  716. BN_zero(r->Z);
  717. r->Z_is_one = 0;
  718. return 1;
  719. }
  720. field_mul = group->meth->field_mul;
  721. field_sqr = group->meth->field_sqr;
  722. p = group->field;
  723. if (ctx == NULL) {
  724. ctx = new_ctx = BN_CTX_new();
  725. if (ctx == NULL)
  726. return 0;
  727. }
  728. BN_CTX_start(ctx);
  729. n0 = BN_CTX_get(ctx);
  730. n1 = BN_CTX_get(ctx);
  731. n2 = BN_CTX_get(ctx);
  732. n3 = BN_CTX_get(ctx);
  733. if (n3 == NULL)
  734. goto err;
  735. /*
  736. * Note that in this function we must not read components of 'a' once we
  737. * have written the corresponding components of 'r'. ('r' might the same
  738. * as 'a'.)
  739. */
  740. /* n1 */
  741. if (a->Z_is_one) {
  742. if (!field_sqr(group, n0, a->X, ctx))
  743. goto err;
  744. if (!BN_mod_lshift1_quick(n1, n0, p))
  745. goto err;
  746. if (!BN_mod_add_quick(n0, n0, n1, p))
  747. goto err;
  748. if (!BN_mod_add_quick(n1, n0, group->a, p))
  749. goto err;
  750. /* n1 = 3 * X_a^2 + a_curve */
  751. } else if (group->a_is_minus3) {
  752. if (!field_sqr(group, n1, a->Z, ctx))
  753. goto err;
  754. if (!BN_mod_add_quick(n0, a->X, n1, p))
  755. goto err;
  756. if (!BN_mod_sub_quick(n2, a->X, n1, p))
  757. goto err;
  758. if (!field_mul(group, n1, n0, n2, ctx))
  759. goto err;
  760. if (!BN_mod_lshift1_quick(n0, n1, p))
  761. goto err;
  762. if (!BN_mod_add_quick(n1, n0, n1, p))
  763. goto err;
  764. /*-
  765. * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  766. * = 3 * X_a^2 - 3 * Z_a^4
  767. */
  768. } else {
  769. if (!field_sqr(group, n0, a->X, ctx))
  770. goto err;
  771. if (!BN_mod_lshift1_quick(n1, n0, p))
  772. goto err;
  773. if (!BN_mod_add_quick(n0, n0, n1, p))
  774. goto err;
  775. if (!field_sqr(group, n1, a->Z, ctx))
  776. goto err;
  777. if (!field_sqr(group, n1, n1, ctx))
  778. goto err;
  779. if (!field_mul(group, n1, n1, group->a, ctx))
  780. goto err;
  781. if (!BN_mod_add_quick(n1, n1, n0, p))
  782. goto err;
  783. /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
  784. }
  785. /* Z_r */
  786. if (a->Z_is_one) {
  787. if (!BN_copy(n0, a->Y))
  788. goto err;
  789. } else {
  790. if (!field_mul(group, n0, a->Y, a->Z, ctx))
  791. goto err;
  792. }
  793. if (!BN_mod_lshift1_quick(r->Z, n0, p))
  794. goto err;
  795. r->Z_is_one = 0;
  796. /* Z_r = 2 * Y_a * Z_a */
  797. /* n2 */
  798. if (!field_sqr(group, n3, a->Y, ctx))
  799. goto err;
  800. if (!field_mul(group, n2, a->X, n3, ctx))
  801. goto err;
  802. if (!BN_mod_lshift_quick(n2, n2, 2, p))
  803. goto err;
  804. /* n2 = 4 * X_a * Y_a^2 */
  805. /* X_r */
  806. if (!BN_mod_lshift1_quick(n0, n2, p))
  807. goto err;
  808. if (!field_sqr(group, r->X, n1, ctx))
  809. goto err;
  810. if (!BN_mod_sub_quick(r->X, r->X, n0, p))
  811. goto err;
  812. /* X_r = n1^2 - 2 * n2 */
  813. /* n3 */
  814. if (!field_sqr(group, n0, n3, ctx))
  815. goto err;
  816. if (!BN_mod_lshift_quick(n3, n0, 3, p))
  817. goto err;
  818. /* n3 = 8 * Y_a^4 */
  819. /* Y_r */
  820. if (!BN_mod_sub_quick(n0, n2, r->X, p))
  821. goto err;
  822. if (!field_mul(group, n0, n1, n0, ctx))
  823. goto err;
  824. if (!BN_mod_sub_quick(r->Y, n0, n3, p))
  825. goto err;
  826. /* Y_r = n1 * (n2 - X_r) - n3 */
  827. ret = 1;
  828. err:
  829. BN_CTX_end(ctx);
  830. BN_CTX_free(new_ctx);
  831. return ret;
  832. }
  833. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  834. {
  835. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
  836. /* point is its own inverse */
  837. return 1;
  838. return BN_usub(point->Y, group->field, point->Y);
  839. }
  840. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  841. {
  842. return BN_is_zero(point->Z);
  843. }
  844. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  845. BN_CTX *ctx)
  846. {
  847. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  848. const BIGNUM *, BN_CTX *);
  849. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  850. const BIGNUM *p;
  851. BN_CTX *new_ctx = NULL;
  852. BIGNUM *rh, *tmp, *Z4, *Z6;
  853. int ret = -1;
  854. if (EC_POINT_is_at_infinity(group, point))
  855. return 1;
  856. field_mul = group->meth->field_mul;
  857. field_sqr = group->meth->field_sqr;
  858. p = group->field;
  859. if (ctx == NULL) {
  860. ctx = new_ctx = BN_CTX_new();
  861. if (ctx == NULL)
  862. return -1;
  863. }
  864. BN_CTX_start(ctx);
  865. rh = BN_CTX_get(ctx);
  866. tmp = BN_CTX_get(ctx);
  867. Z4 = BN_CTX_get(ctx);
  868. Z6 = BN_CTX_get(ctx);
  869. if (Z6 == NULL)
  870. goto err;
  871. /*-
  872. * We have a curve defined by a Weierstrass equation
  873. * y^2 = x^3 + a*x + b.
  874. * The point to consider is given in Jacobian projective coordinates
  875. * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  876. * Substituting this and multiplying by Z^6 transforms the above equation into
  877. * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  878. * To test this, we add up the right-hand side in 'rh'.
  879. */
  880. /* rh := X^2 */
  881. if (!field_sqr(group, rh, point->X, ctx))
  882. goto err;
  883. if (!point->Z_is_one) {
  884. if (!field_sqr(group, tmp, point->Z, ctx))
  885. goto err;
  886. if (!field_sqr(group, Z4, tmp, ctx))
  887. goto err;
  888. if (!field_mul(group, Z6, Z4, tmp, ctx))
  889. goto err;
  890. /* rh := (rh + a*Z^4)*X */
  891. if (group->a_is_minus3) {
  892. if (!BN_mod_lshift1_quick(tmp, Z4, p))
  893. goto err;
  894. if (!BN_mod_add_quick(tmp, tmp, Z4, p))
  895. goto err;
  896. if (!BN_mod_sub_quick(rh, rh, tmp, p))
  897. goto err;
  898. if (!field_mul(group, rh, rh, point->X, ctx))
  899. goto err;
  900. } else {
  901. if (!field_mul(group, tmp, Z4, group->a, ctx))
  902. goto err;
  903. if (!BN_mod_add_quick(rh, rh, tmp, p))
  904. goto err;
  905. if (!field_mul(group, rh, rh, point->X, ctx))
  906. goto err;
  907. }
  908. /* rh := rh + b*Z^6 */
  909. if (!field_mul(group, tmp, group->b, Z6, ctx))
  910. goto err;
  911. if (!BN_mod_add_quick(rh, rh, tmp, p))
  912. goto err;
  913. } else {
  914. /* point->Z_is_one */
  915. /* rh := (rh + a)*X */
  916. if (!BN_mod_add_quick(rh, rh, group->a, p))
  917. goto err;
  918. if (!field_mul(group, rh, rh, point->X, ctx))
  919. goto err;
  920. /* rh := rh + b */
  921. if (!BN_mod_add_quick(rh, rh, group->b, p))
  922. goto err;
  923. }
  924. /* 'lh' := Y^2 */
  925. if (!field_sqr(group, tmp, point->Y, ctx))
  926. goto err;
  927. ret = (0 == BN_ucmp(tmp, rh));
  928. err:
  929. BN_CTX_end(ctx);
  930. BN_CTX_free(new_ctx);
  931. return ret;
  932. }
  933. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  934. const EC_POINT *b, BN_CTX *ctx)
  935. {
  936. /*-
  937. * return values:
  938. * -1 error
  939. * 0 equal (in affine coordinates)
  940. * 1 not equal
  941. */
  942. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  943. const BIGNUM *, BN_CTX *);
  944. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  945. BN_CTX *new_ctx = NULL;
  946. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  947. const BIGNUM *tmp1_, *tmp2_;
  948. int ret = -1;
  949. if (EC_POINT_is_at_infinity(group, a)) {
  950. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  951. }
  952. if (EC_POINT_is_at_infinity(group, b))
  953. return 1;
  954. if (a->Z_is_one && b->Z_is_one) {
  955. return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
  956. }
  957. field_mul = group->meth->field_mul;
  958. field_sqr = group->meth->field_sqr;
  959. if (ctx == NULL) {
  960. ctx = new_ctx = BN_CTX_new();
  961. if (ctx == NULL)
  962. return -1;
  963. }
  964. BN_CTX_start(ctx);
  965. tmp1 = BN_CTX_get(ctx);
  966. tmp2 = BN_CTX_get(ctx);
  967. Za23 = BN_CTX_get(ctx);
  968. Zb23 = BN_CTX_get(ctx);
  969. if (Zb23 == NULL)
  970. goto end;
  971. /*-
  972. * We have to decide whether
  973. * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  974. * or equivalently, whether
  975. * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  976. */
  977. if (!b->Z_is_one) {
  978. if (!field_sqr(group, Zb23, b->Z, ctx))
  979. goto end;
  980. if (!field_mul(group, tmp1, a->X, Zb23, ctx))
  981. goto end;
  982. tmp1_ = tmp1;
  983. } else
  984. tmp1_ = a->X;
  985. if (!a->Z_is_one) {
  986. if (!field_sqr(group, Za23, a->Z, ctx))
  987. goto end;
  988. if (!field_mul(group, tmp2, b->X, Za23, ctx))
  989. goto end;
  990. tmp2_ = tmp2;
  991. } else
  992. tmp2_ = b->X;
  993. /* compare X_a*Z_b^2 with X_b*Z_a^2 */
  994. if (BN_cmp(tmp1_, tmp2_) != 0) {
  995. ret = 1; /* points differ */
  996. goto end;
  997. }
  998. if (!b->Z_is_one) {
  999. if (!field_mul(group, Zb23, Zb23, b->Z, ctx))
  1000. goto end;
  1001. if (!field_mul(group, tmp1, a->Y, Zb23, ctx))
  1002. goto end;
  1003. /* tmp1_ = tmp1 */
  1004. } else
  1005. tmp1_ = a->Y;
  1006. if (!a->Z_is_one) {
  1007. if (!field_mul(group, Za23, Za23, a->Z, ctx))
  1008. goto end;
  1009. if (!field_mul(group, tmp2, b->Y, Za23, ctx))
  1010. goto end;
  1011. /* tmp2_ = tmp2 */
  1012. } else
  1013. tmp2_ = b->Y;
  1014. /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
  1015. if (BN_cmp(tmp1_, tmp2_) != 0) {
  1016. ret = 1; /* points differ */
  1017. goto end;
  1018. }
  1019. /* points are equal */
  1020. ret = 0;
  1021. end:
  1022. BN_CTX_end(ctx);
  1023. BN_CTX_free(new_ctx);
  1024. return ret;
  1025. }
  1026. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  1027. BN_CTX *ctx)
  1028. {
  1029. BN_CTX *new_ctx = NULL;
  1030. BIGNUM *x, *y;
  1031. int ret = 0;
  1032. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  1033. return 1;
  1034. if (ctx == NULL) {
  1035. ctx = new_ctx = BN_CTX_new();
  1036. if (ctx == NULL)
  1037. return 0;
  1038. }
  1039. BN_CTX_start(ctx);
  1040. x = BN_CTX_get(ctx);
  1041. y = BN_CTX_get(ctx);
  1042. if (y == NULL)
  1043. goto err;
  1044. if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
  1045. goto err;
  1046. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  1047. goto err;
  1048. if (!point->Z_is_one) {
  1049. ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
  1050. goto err;
  1051. }
  1052. ret = 1;
  1053. err:
  1054. BN_CTX_end(ctx);
  1055. BN_CTX_free(new_ctx);
  1056. return ret;
  1057. }
  1058. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
  1059. EC_POINT *points[], BN_CTX *ctx)
  1060. {
  1061. BN_CTX *new_ctx = NULL;
  1062. BIGNUM *tmp, *tmp_Z;
  1063. BIGNUM **prod_Z = NULL;
  1064. size_t i;
  1065. int ret = 0;
  1066. if (num == 0)
  1067. return 1;
  1068. if (ctx == NULL) {
  1069. ctx = new_ctx = BN_CTX_new();
  1070. if (ctx == NULL)
  1071. return 0;
  1072. }
  1073. BN_CTX_start(ctx);
  1074. tmp = BN_CTX_get(ctx);
  1075. tmp_Z = BN_CTX_get(ctx);
  1076. if (tmp_Z == NULL)
  1077. goto err;
  1078. prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
  1079. if (prod_Z == NULL)
  1080. goto err;
  1081. for (i = 0; i < num; i++) {
  1082. prod_Z[i] = BN_new();
  1083. if (prod_Z[i] == NULL)
  1084. goto err;
  1085. }
  1086. /*
  1087. * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
  1088. * skipping any zero-valued inputs (pretend that they're 1).
  1089. */
  1090. if (!BN_is_zero(points[0]->Z)) {
  1091. if (!BN_copy(prod_Z[0], points[0]->Z))
  1092. goto err;
  1093. } else {
  1094. if (group->meth->field_set_to_one != 0) {
  1095. if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))
  1096. goto err;
  1097. } else {
  1098. if (!BN_one(prod_Z[0]))
  1099. goto err;
  1100. }
  1101. }
  1102. for (i = 1; i < num; i++) {
  1103. if (!BN_is_zero(points[i]->Z)) {
  1104. if (!group->
  1105. meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,
  1106. ctx))
  1107. goto err;
  1108. } else {
  1109. if (!BN_copy(prod_Z[i], prod_Z[i - 1]))
  1110. goto err;
  1111. }
  1112. }
  1113. /*
  1114. * Now use a single explicit inversion to replace every non-zero
  1115. * points[i]->Z by its inverse.
  1116. */
  1117. if (!group->meth->field_inv(group, tmp, prod_Z[num - 1], ctx)) {
  1118. ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
  1119. goto err;
  1120. }
  1121. if (group->meth->field_encode != 0) {
  1122. /*
  1123. * In the Montgomery case, we just turned R*H (representing H) into
  1124. * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to
  1125. * multiply by the Montgomery factor twice.
  1126. */
  1127. if (!group->meth->field_encode(group, tmp, tmp, ctx))
  1128. goto err;
  1129. if (!group->meth->field_encode(group, tmp, tmp, ctx))
  1130. goto err;
  1131. }
  1132. for (i = num - 1; i > 0; --i) {
  1133. /*
  1134. * Loop invariant: tmp is the product of the inverses of points[0]->Z
  1135. * .. points[i]->Z (zero-valued inputs skipped).
  1136. */
  1137. if (!BN_is_zero(points[i]->Z)) {
  1138. /*
  1139. * Set tmp_Z to the inverse of points[i]->Z (as product of Z
  1140. * inverses 0 .. i, Z values 0 .. i - 1).
  1141. */
  1142. if (!group->
  1143. meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))
  1144. goto err;
  1145. /*
  1146. * Update tmp to satisfy the loop invariant for i - 1.
  1147. */
  1148. if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))
  1149. goto err;
  1150. /* Replace points[i]->Z by its inverse. */
  1151. if (!BN_copy(points[i]->Z, tmp_Z))
  1152. goto err;
  1153. }
  1154. }
  1155. if (!BN_is_zero(points[0]->Z)) {
  1156. /* Replace points[0]->Z by its inverse. */
  1157. if (!BN_copy(points[0]->Z, tmp))
  1158. goto err;
  1159. }
  1160. /* Finally, fix up the X and Y coordinates for all points. */
  1161. for (i = 0; i < num; i++) {
  1162. EC_POINT *p = points[i];
  1163. if (!BN_is_zero(p->Z)) {
  1164. /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
  1165. if (!group->meth->field_sqr(group, tmp, p->Z, ctx))
  1166. goto err;
  1167. if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))
  1168. goto err;
  1169. if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))
  1170. goto err;
  1171. if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))
  1172. goto err;
  1173. if (group->meth->field_set_to_one != 0) {
  1174. if (!group->meth->field_set_to_one(group, p->Z, ctx))
  1175. goto err;
  1176. } else {
  1177. if (!BN_one(p->Z))
  1178. goto err;
  1179. }
  1180. p->Z_is_one = 1;
  1181. }
  1182. }
  1183. ret = 1;
  1184. err:
  1185. BN_CTX_end(ctx);
  1186. BN_CTX_free(new_ctx);
  1187. if (prod_Z != NULL) {
  1188. for (i = 0; i < num; i++) {
  1189. if (prod_Z[i] == NULL)
  1190. break;
  1191. BN_clear_free(prod_Z[i]);
  1192. }
  1193. OPENSSL_free(prod_Z);
  1194. }
  1195. return ret;
  1196. }
  1197. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1198. const BIGNUM *b, BN_CTX *ctx)
  1199. {
  1200. return BN_mod_mul(r, a, b, group->field, ctx);
  1201. }
  1202. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1203. BN_CTX *ctx)
  1204. {
  1205. return BN_mod_sqr(r, a, group->field, ctx);
  1206. }
  1207. /*-
  1208. * Computes the multiplicative inverse of a in GF(p), storing the result in r.
  1209. * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
  1210. * Since we don't have a Mont structure here, SCA hardening is with blinding.
  1211. */
  1212. int ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1213. BN_CTX *ctx)
  1214. {
  1215. BIGNUM *e = NULL;
  1216. BN_CTX *new_ctx = NULL;
  1217. int ret = 0;
  1218. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  1219. return 0;
  1220. BN_CTX_start(ctx);
  1221. if ((e = BN_CTX_get(ctx)) == NULL)
  1222. goto err;
  1223. do {
  1224. if (!BN_priv_rand_range(e, group->field))
  1225. goto err;
  1226. } while (BN_is_zero(e));
  1227. /* r := a * e */
  1228. if (!group->meth->field_mul(group, r, a, e, ctx))
  1229. goto err;
  1230. /* r := 1/(a * e) */
  1231. if (!BN_mod_inverse(r, r, group->field, ctx)) {
  1232. ECerr(EC_F_EC_GFP_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
  1233. goto err;
  1234. }
  1235. /* r := e/(a * e) = 1/a */
  1236. if (!group->meth->field_mul(group, r, r, e, ctx))
  1237. goto err;
  1238. ret = 1;
  1239. err:
  1240. BN_CTX_end(ctx);
  1241. BN_CTX_free(new_ctx);
  1242. return ret;
  1243. }
  1244. /*-
  1245. * Apply randomization of EC point projective coordinates:
  1246. *
  1247. * (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z)
  1248. * lambda = [1,group->field)
  1249. *
  1250. */
  1251. int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  1252. BN_CTX *ctx)
  1253. {
  1254. int ret = 0;
  1255. BIGNUM *lambda = NULL;
  1256. BIGNUM *temp = NULL;
  1257. BN_CTX_start(ctx);
  1258. lambda = BN_CTX_get(ctx);
  1259. temp = BN_CTX_get(ctx);
  1260. if (temp == NULL) {
  1261. ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
  1262. goto err;
  1263. }
  1264. /* make sure lambda is not zero */
  1265. do {
  1266. if (!BN_priv_rand_range(lambda, group->field)) {
  1267. ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_BN_LIB);
  1268. goto err;
  1269. }
  1270. } while (BN_is_zero(lambda));
  1271. /* if field_encode defined convert between representations */
  1272. if (group->meth->field_encode != NULL
  1273. && !group->meth->field_encode(group, lambda, lambda, ctx))
  1274. goto err;
  1275. if (!group->meth->field_mul(group, p->Z, p->Z, lambda, ctx))
  1276. goto err;
  1277. if (!group->meth->field_sqr(group, temp, lambda, ctx))
  1278. goto err;
  1279. if (!group->meth->field_mul(group, p->X, p->X, temp, ctx))
  1280. goto err;
  1281. if (!group->meth->field_mul(group, temp, temp, lambda, ctx))
  1282. goto err;
  1283. if (!group->meth->field_mul(group, p->Y, p->Y, temp, ctx))
  1284. goto err;
  1285. p->Z_is_one = 0;
  1286. ret = 1;
  1287. err:
  1288. BN_CTX_end(ctx);
  1289. return ret;
  1290. }
  1291. /*-
  1292. * Set s := p, r := 2p.
  1293. *
  1294. * For doubling we use Formula 3 from Izu-Takagi "A fast parallel elliptic curve
  1295. * multiplication resistant against side channel attacks" appendix, as described
  1296. * at
  1297. * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#doubling-dbl-2002-it-2
  1298. *
  1299. * The input point p will be in randomized Jacobian projective coords:
  1300. * x = X/Z**2, y=Y/Z**3
  1301. *
  1302. * The output points p, s, and r are converted to standard (homogeneous)
  1303. * projective coords:
  1304. * x = X/Z, y=Y/Z
  1305. */
  1306. int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
  1307. EC_POINT *r, EC_POINT *s,
  1308. EC_POINT *p, BN_CTX *ctx)
  1309. {
  1310. BIGNUM *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
  1311. t1 = r->Z;
  1312. t2 = r->Y;
  1313. t3 = s->X;
  1314. t4 = r->X;
  1315. t5 = s->Y;
  1316. t6 = s->Z;
  1317. /* convert p: (X,Y,Z) -> (XZ,Y,Z**3) */
  1318. if (!group->meth->field_mul(group, p->X, p->X, p->Z, ctx)
  1319. || !group->meth->field_sqr(group, t1, p->Z, ctx)
  1320. || !group->meth->field_mul(group, p->Z, p->Z, t1, ctx)
  1321. /* r := 2p */
  1322. || !group->meth->field_sqr(group, t2, p->X, ctx)
  1323. || !group->meth->field_sqr(group, t3, p->Z, ctx)
  1324. || !group->meth->field_mul(group, t4, t3, group->a, ctx)
  1325. || !BN_mod_sub_quick(t5, t2, t4, group->field)
  1326. || !BN_mod_add_quick(t2, t2, t4, group->field)
  1327. || !group->meth->field_sqr(group, t5, t5, ctx)
  1328. || !group->meth->field_mul(group, t6, t3, group->b, ctx)
  1329. || !group->meth->field_mul(group, t1, p->X, p->Z, ctx)
  1330. || !group->meth->field_mul(group, t4, t1, t6, ctx)
  1331. || !BN_mod_lshift_quick(t4, t4, 3, group->field)
  1332. /* r->X coord output */
  1333. || !BN_mod_sub_quick(r->X, t5, t4, group->field)
  1334. || !group->meth->field_mul(group, t1, t1, t2, ctx)
  1335. || !group->meth->field_mul(group, t2, t3, t6, ctx)
  1336. || !BN_mod_add_quick(t1, t1, t2, group->field)
  1337. /* r->Z coord output */
  1338. || !BN_mod_lshift_quick(r->Z, t1, 2, group->field)
  1339. || !EC_POINT_copy(s, p))
  1340. return 0;
  1341. r->Z_is_one = 0;
  1342. s->Z_is_one = 0;
  1343. p->Z_is_one = 0;
  1344. return 1;
  1345. }
  1346. /*-
  1347. * Differential addition-and-doubling using Eq. (9) and (10) from Izu-Takagi
  1348. * "A fast parallel elliptic curve multiplication resistant against side channel
  1349. * attacks", as described at
  1350. * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-ladd-2002-it-4
  1351. */
  1352. int ec_GFp_simple_ladder_step(const EC_GROUP *group,
  1353. EC_POINT *r, EC_POINT *s,
  1354. EC_POINT *p, BN_CTX *ctx)
  1355. {
  1356. int ret = 0;
  1357. BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6, *t7 = NULL;
  1358. BN_CTX_start(ctx);
  1359. t0 = BN_CTX_get(ctx);
  1360. t1 = BN_CTX_get(ctx);
  1361. t2 = BN_CTX_get(ctx);
  1362. t3 = BN_CTX_get(ctx);
  1363. t4 = BN_CTX_get(ctx);
  1364. t5 = BN_CTX_get(ctx);
  1365. t6 = BN_CTX_get(ctx);
  1366. t7 = BN_CTX_get(ctx);
  1367. if (t7 == NULL
  1368. || !group->meth->field_mul(group, t0, r->X, s->X, ctx)
  1369. || !group->meth->field_mul(group, t1, r->Z, s->Z, ctx)
  1370. || !group->meth->field_mul(group, t2, r->X, s->Z, ctx)
  1371. || !group->meth->field_mul(group, t3, r->Z, s->X, ctx)
  1372. || !group->meth->field_mul(group, t4, group->a, t1, ctx)
  1373. || !BN_mod_add_quick(t0, t0, t4, group->field)
  1374. || !BN_mod_add_quick(t4, t3, t2, group->field)
  1375. || !group->meth->field_mul(group, t0, t4, t0, ctx)
  1376. || !group->meth->field_sqr(group, t1, t1, ctx)
  1377. || !BN_mod_lshift_quick(t7, group->b, 2, group->field)
  1378. || !group->meth->field_mul(group, t1, t7, t1, ctx)
  1379. || !BN_mod_lshift1_quick(t0, t0, group->field)
  1380. || !BN_mod_add_quick(t0, t1, t0, group->field)
  1381. || !BN_mod_sub_quick(t1, t2, t3, group->field)
  1382. || !group->meth->field_sqr(group, t1, t1, ctx)
  1383. || !group->meth->field_mul(group, t3, t1, p->X, ctx)
  1384. || !group->meth->field_mul(group, t0, p->Z, t0, ctx)
  1385. /* s->X coord output */
  1386. || !BN_mod_sub_quick(s->X, t0, t3, group->field)
  1387. /* s->Z coord output */
  1388. || !group->meth->field_mul(group, s->Z, p->Z, t1, ctx)
  1389. || !group->meth->field_sqr(group, t3, r->X, ctx)
  1390. || !group->meth->field_sqr(group, t2, r->Z, ctx)
  1391. || !group->meth->field_mul(group, t4, t2, group->a, ctx)
  1392. || !BN_mod_add_quick(t5, r->X, r->Z, group->field)
  1393. || !group->meth->field_sqr(group, t5, t5, ctx)
  1394. || !BN_mod_sub_quick(t5, t5, t3, group->field)
  1395. || !BN_mod_sub_quick(t5, t5, t2, group->field)
  1396. || !BN_mod_sub_quick(t6, t3, t4, group->field)
  1397. || !group->meth->field_sqr(group, t6, t6, ctx)
  1398. || !group->meth->field_mul(group, t0, t2, t5, ctx)
  1399. || !group->meth->field_mul(group, t0, t7, t0, ctx)
  1400. /* r->X coord output */
  1401. || !BN_mod_sub_quick(r->X, t6, t0, group->field)
  1402. || !BN_mod_add_quick(t6, t3, t4, group->field)
  1403. || !group->meth->field_sqr(group, t3, t2, ctx)
  1404. || !group->meth->field_mul(group, t7, t3, t7, ctx)
  1405. || !group->meth->field_mul(group, t5, t5, t6, ctx)
  1406. || !BN_mod_lshift1_quick(t5, t5, group->field)
  1407. /* r->Z coord output */
  1408. || !BN_mod_add_quick(r->Z, t7, t5, group->field))
  1409. goto err;
  1410. ret = 1;
  1411. err:
  1412. BN_CTX_end(ctx);
  1413. return ret;
  1414. }
  1415. /*-
  1416. * Recovers the y-coordinate of r using Eq. (8) from Brier-Joye, "Weierstrass
  1417. * Elliptic Curves and Side-Channel Attacks", modified to work in projective
  1418. * coordinates and return r in Jacobian projective coordinates.
  1419. *
  1420. * X4 = two*Y1*X2*Z3*Z2*Z1;
  1421. * Y4 = two*b*Z3*SQR(Z2*Z1) + Z3*(a*Z2*Z1+X1*X2)*(X1*Z2+X2*Z1) - X3*SQR(X1*Z2-X2*Z1);
  1422. * Z4 = two*Y1*Z3*SQR(Z2)*Z1;
  1423. *
  1424. * Z4 != 0 because:
  1425. * - Z1==0 implies p is at infinity, which would have caused an early exit in
  1426. * the caller;
  1427. * - Z2==0 implies r is at infinity (handled by the BN_is_zero(r->Z) branch);
  1428. * - Z3==0 implies s is at infinity (handled by the BN_is_zero(s->Z) branch);
  1429. * - Y1==0 implies p has order 2, so either r or s are infinity and handled by
  1430. * one of the BN_is_zero(...) branches.
  1431. */
  1432. int ec_GFp_simple_ladder_post(const EC_GROUP *group,
  1433. EC_POINT *r, EC_POINT *s,
  1434. EC_POINT *p, BN_CTX *ctx)
  1435. {
  1436. int ret = 0;
  1437. BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
  1438. if (BN_is_zero(r->Z))
  1439. return EC_POINT_set_to_infinity(group, r);
  1440. if (BN_is_zero(s->Z)) {
  1441. /* (X,Y,Z) -> (XZ,YZ**2,Z) */
  1442. if (!group->meth->field_mul(group, r->X, p->X, p->Z, ctx)
  1443. || !group->meth->field_sqr(group, r->Z, p->Z, ctx)
  1444. || !group->meth->field_mul(group, r->Y, p->Y, r->Z, ctx)
  1445. || !BN_copy(r->Z, p->Z)
  1446. || !EC_POINT_invert(group, r, ctx))
  1447. return 0;
  1448. return 1;
  1449. }
  1450. BN_CTX_start(ctx);
  1451. t0 = BN_CTX_get(ctx);
  1452. t1 = BN_CTX_get(ctx);
  1453. t2 = BN_CTX_get(ctx);
  1454. t3 = BN_CTX_get(ctx);
  1455. t4 = BN_CTX_get(ctx);
  1456. t5 = BN_CTX_get(ctx);
  1457. t6 = BN_CTX_get(ctx);
  1458. if (t6 == NULL
  1459. || !BN_mod_lshift1_quick(t0, p->Y, group->field)
  1460. || !group->meth->field_mul(group, t1, r->X, p->Z, ctx)
  1461. || !group->meth->field_mul(group, t2, r->Z, s->Z, ctx)
  1462. || !group->meth->field_mul(group, t2, t1, t2, ctx)
  1463. || !group->meth->field_mul(group, t3, t2, t0, ctx)
  1464. || !group->meth->field_mul(group, t2, r->Z, p->Z, ctx)
  1465. || !group->meth->field_sqr(group, t4, t2, ctx)
  1466. || !BN_mod_lshift1_quick(t5, group->b, group->field)
  1467. || !group->meth->field_mul(group, t4, t4, t5, ctx)
  1468. || !group->meth->field_mul(group, t6, t2, group->a, ctx)
  1469. || !group->meth->field_mul(group, t5, r->X, p->X, ctx)
  1470. || !BN_mod_add_quick(t5, t6, t5, group->field)
  1471. || !group->meth->field_mul(group, t6, r->Z, p->X, ctx)
  1472. || !BN_mod_add_quick(t2, t6, t1, group->field)
  1473. || !group->meth->field_mul(group, t5, t5, t2, ctx)
  1474. || !BN_mod_sub_quick(t6, t6, t1, group->field)
  1475. || !group->meth->field_sqr(group, t6, t6, ctx)
  1476. || !group->meth->field_mul(group, t6, t6, s->X, ctx)
  1477. || !BN_mod_add_quick(t4, t5, t4, group->field)
  1478. || !group->meth->field_mul(group, t4, t4, s->Z, ctx)
  1479. || !BN_mod_sub_quick(t4, t4, t6, group->field)
  1480. || !group->meth->field_sqr(group, t5, r->Z, ctx)
  1481. || !group->meth->field_mul(group, r->Z, p->Z, s->Z, ctx)
  1482. || !group->meth->field_mul(group, r->Z, t5, r->Z, ctx)
  1483. || !group->meth->field_mul(group, r->Z, r->Z, t0, ctx)
  1484. /* t3 := X, t4 := Y */
  1485. /* (X,Y,Z) -> (XZ,YZ**2,Z) */
  1486. || !group->meth->field_mul(group, r->X, t3, r->Z, ctx)
  1487. || !group->meth->field_sqr(group, t3, r->Z, ctx)
  1488. || !group->meth->field_mul(group, r->Y, t4, t3, ctx))
  1489. goto err;
  1490. ret = 1;
  1491. err:
  1492. BN_CTX_end(ctx);
  1493. return ret;
  1494. }