ecp_smpl.c 48 KB

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