ecp_smpl.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. * Copyright 2001-2020 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_local.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. * NB: "a" must be in _decoded_ form. (i.e. field_decode must precede.)
  1212. */
  1213. int ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  1214. BN_CTX *ctx)
  1215. {
  1216. BIGNUM *e = NULL;
  1217. BN_CTX *new_ctx = NULL;
  1218. int ret = 0;
  1219. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  1220. return 0;
  1221. BN_CTX_start(ctx);
  1222. if ((e = BN_CTX_get(ctx)) == NULL)
  1223. goto err;
  1224. do {
  1225. if (!BN_priv_rand_range(e, group->field))
  1226. goto err;
  1227. } while (BN_is_zero(e));
  1228. /* r := a * e */
  1229. if (!group->meth->field_mul(group, r, a, e, ctx))
  1230. goto err;
  1231. /* r := 1/(a * e) */
  1232. if (!BN_mod_inverse(r, r, group->field, ctx)) {
  1233. ECerr(EC_F_EC_GFP_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
  1234. goto err;
  1235. }
  1236. /* r := e/(a * e) = 1/a */
  1237. if (!group->meth->field_mul(group, r, r, e, ctx))
  1238. goto err;
  1239. ret = 1;
  1240. err:
  1241. BN_CTX_end(ctx);
  1242. BN_CTX_free(new_ctx);
  1243. return ret;
  1244. }
  1245. /*-
  1246. * Apply randomization of EC point projective coordinates:
  1247. *
  1248. * (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z)
  1249. * lambda = [1,group->field)
  1250. *
  1251. */
  1252. int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  1253. BN_CTX *ctx)
  1254. {
  1255. int ret = 0;
  1256. BIGNUM *lambda = NULL;
  1257. BIGNUM *temp = NULL;
  1258. BN_CTX_start(ctx);
  1259. lambda = BN_CTX_get(ctx);
  1260. temp = BN_CTX_get(ctx);
  1261. if (temp == NULL) {
  1262. ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
  1263. goto end;
  1264. }
  1265. /*-
  1266. * Make sure lambda is not zero.
  1267. * If the RNG fails, we cannot blind but nevertheless want
  1268. * code to continue smoothly and not clobber the error stack.
  1269. */
  1270. do {
  1271. ERR_set_mark();
  1272. ret = BN_priv_rand_range(lambda, group->field);
  1273. ERR_pop_to_mark();
  1274. if (ret == 0) {
  1275. ret = 1;
  1276. goto end;
  1277. }
  1278. } while (BN_is_zero(lambda));
  1279. /* if field_encode defined convert between representations */
  1280. if ((group->meth->field_encode != NULL
  1281. && !group->meth->field_encode(group, lambda, lambda, ctx))
  1282. || !group->meth->field_mul(group, p->Z, p->Z, lambda, ctx)
  1283. || !group->meth->field_sqr(group, temp, lambda, ctx)
  1284. || !group->meth->field_mul(group, p->X, p->X, temp, ctx)
  1285. || !group->meth->field_mul(group, temp, temp, lambda, ctx)
  1286. || !group->meth->field_mul(group, p->Y, p->Y, temp, ctx))
  1287. goto end;
  1288. p->Z_is_one = 0;
  1289. ret = 1;
  1290. end:
  1291. BN_CTX_end(ctx);
  1292. return ret;
  1293. }
  1294. /*-
  1295. * Input:
  1296. * - p: affine coordinates
  1297. *
  1298. * Output:
  1299. * - s := p, r := 2p: blinded projective (homogeneous) coordinates
  1300. *
  1301. * For doubling we use Formula 3 from Izu-Takagi "A fast parallel elliptic curve
  1302. * multiplication resistant against side channel attacks" appendix, described at
  1303. * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#doubling-dbl-2002-it-2
  1304. * simplified for Z1=1.
  1305. *
  1306. * Blinding uses the equivalence relation (\lambda X, \lambda Y, \lambda Z)
  1307. * for any non-zero \lambda that holds for projective (homogeneous) coords.
  1308. */
  1309. int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
  1310. EC_POINT *r, EC_POINT *s,
  1311. EC_POINT *p, BN_CTX *ctx)
  1312. {
  1313. BIGNUM *t1, *t2, *t3, *t4, *t5 = NULL;
  1314. t1 = s->Z;
  1315. t2 = r->Z;
  1316. t3 = s->X;
  1317. t4 = r->X;
  1318. t5 = s->Y;
  1319. if (!p->Z_is_one /* r := 2p */
  1320. || !group->meth->field_sqr(group, t3, p->X, ctx)
  1321. || !BN_mod_sub_quick(t4, t3, group->a, group->field)
  1322. || !group->meth->field_sqr(group, t4, t4, ctx)
  1323. || !group->meth->field_mul(group, t5, p->X, group->b, ctx)
  1324. || !BN_mod_lshift_quick(t5, t5, 3, group->field)
  1325. /* r->X coord output */
  1326. || !BN_mod_sub_quick(r->X, t4, t5, group->field)
  1327. || !BN_mod_add_quick(t1, t3, group->a, group->field)
  1328. || !group->meth->field_mul(group, t2, p->X, t1, ctx)
  1329. || !BN_mod_add_quick(t2, group->b, t2, group->field)
  1330. /* r->Z coord output */
  1331. || !BN_mod_lshift_quick(r->Z, t2, 2, group->field))
  1332. return 0;
  1333. /* make sure lambda (r->Y here for storage) is not zero */
  1334. do {
  1335. if (!BN_priv_rand_range(r->Y, group->field))
  1336. return 0;
  1337. } while (BN_is_zero(r->Y));
  1338. /* make sure lambda (s->Z here for storage) is not zero */
  1339. do {
  1340. if (!BN_priv_rand_range(s->Z, group->field))
  1341. return 0;
  1342. } while (BN_is_zero(s->Z));
  1343. /* if field_encode defined convert between representations */
  1344. if (group->meth->field_encode != NULL
  1345. && (!group->meth->field_encode(group, r->Y, r->Y, ctx)
  1346. || !group->meth->field_encode(group, s->Z, s->Z, ctx)))
  1347. return 0;
  1348. /* blind r and s independently */
  1349. if (!group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx)
  1350. || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx)
  1351. || !group->meth->field_mul(group, s->X, p->X, s->Z, ctx)) /* s := p */
  1352. return 0;
  1353. r->Z_is_one = 0;
  1354. s->Z_is_one = 0;
  1355. return 1;
  1356. }
  1357. /*-
  1358. * Input:
  1359. * - s, r: projective (homogeneous) coordinates
  1360. * - p: affine coordinates
  1361. *
  1362. * Output:
  1363. * - s := r + s, r := 2r: projective (homogeneous) coordinates
  1364. *
  1365. * Differential addition-and-doubling using Eq. (9) and (10) from Izu-Takagi
  1366. * "A fast parallel elliptic curve multiplication resistant against side channel
  1367. * attacks", as described at
  1368. * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-mladd-2002-it-4
  1369. */
  1370. int ec_GFp_simple_ladder_step(const EC_GROUP *group,
  1371. EC_POINT *r, EC_POINT *s,
  1372. EC_POINT *p, BN_CTX *ctx)
  1373. {
  1374. int ret = 0;
  1375. BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
  1376. BN_CTX_start(ctx);
  1377. t0 = BN_CTX_get(ctx);
  1378. t1 = BN_CTX_get(ctx);
  1379. t2 = BN_CTX_get(ctx);
  1380. t3 = BN_CTX_get(ctx);
  1381. t4 = BN_CTX_get(ctx);
  1382. t5 = BN_CTX_get(ctx);
  1383. t6 = BN_CTX_get(ctx);
  1384. if (t6 == NULL
  1385. || !group->meth->field_mul(group, t6, r->X, s->X, ctx)
  1386. || !group->meth->field_mul(group, t0, r->Z, s->Z, ctx)
  1387. || !group->meth->field_mul(group, t4, r->X, s->Z, ctx)
  1388. || !group->meth->field_mul(group, t3, r->Z, s->X, ctx)
  1389. || !group->meth->field_mul(group, t5, group->a, t0, ctx)
  1390. || !BN_mod_add_quick(t5, t6, t5, group->field)
  1391. || !BN_mod_add_quick(t6, t3, t4, group->field)
  1392. || !group->meth->field_mul(group, t5, t6, t5, ctx)
  1393. || !group->meth->field_sqr(group, t0, t0, ctx)
  1394. || !BN_mod_lshift_quick(t2, group->b, 2, group->field)
  1395. || !group->meth->field_mul(group, t0, t2, t0, ctx)
  1396. || !BN_mod_lshift1_quick(t5, t5, group->field)
  1397. || !BN_mod_sub_quick(t3, t4, t3, group->field)
  1398. /* s->Z coord output */
  1399. || !group->meth->field_sqr(group, s->Z, t3, ctx)
  1400. || !group->meth->field_mul(group, t4, s->Z, p->X, ctx)
  1401. || !BN_mod_add_quick(t0, t0, t5, group->field)
  1402. /* s->X coord output */
  1403. || !BN_mod_sub_quick(s->X, t0, t4, group->field)
  1404. || !group->meth->field_sqr(group, t4, r->X, ctx)
  1405. || !group->meth->field_sqr(group, t5, r->Z, ctx)
  1406. || !group->meth->field_mul(group, t6, t5, group->a, ctx)
  1407. || !BN_mod_add_quick(t1, r->X, r->Z, group->field)
  1408. || !group->meth->field_sqr(group, t1, t1, ctx)
  1409. || !BN_mod_sub_quick(t1, t1, t4, group->field)
  1410. || !BN_mod_sub_quick(t1, t1, t5, group->field)
  1411. || !BN_mod_sub_quick(t3, t4, t6, group->field)
  1412. || !group->meth->field_sqr(group, t3, t3, ctx)
  1413. || !group->meth->field_mul(group, t0, t5, t1, ctx)
  1414. || !group->meth->field_mul(group, t0, t2, t0, ctx)
  1415. /* r->X coord output */
  1416. || !BN_mod_sub_quick(r->X, t3, t0, group->field)
  1417. || !BN_mod_add_quick(t3, t4, t6, group->field)
  1418. || !group->meth->field_sqr(group, t4, t5, ctx)
  1419. || !group->meth->field_mul(group, t4, t4, t2, ctx)
  1420. || !group->meth->field_mul(group, t1, t1, t3, ctx)
  1421. || !BN_mod_lshift1_quick(t1, t1, group->field)
  1422. /* r->Z coord output */
  1423. || !BN_mod_add_quick(r->Z, t4, t1, group->field))
  1424. goto err;
  1425. ret = 1;
  1426. err:
  1427. BN_CTX_end(ctx);
  1428. return ret;
  1429. }
  1430. /*-
  1431. * Input:
  1432. * - s, r: projective (homogeneous) coordinates
  1433. * - p: affine coordinates
  1434. *
  1435. * Output:
  1436. * - r := (x,y): affine coordinates
  1437. *
  1438. * Recovers the y-coordinate of r using Eq. (8) from Brier-Joye, "Weierstrass
  1439. * Elliptic Curves and Side-Channel Attacks", modified to work in mixed
  1440. * projective coords, i.e. p is affine and (r,s) in projective (homogeneous)
  1441. * coords, and return r in affine coordinates.
  1442. *
  1443. * X4 = two*Y1*X2*Z3*Z2;
  1444. * Y4 = two*b*Z3*SQR(Z2) + Z3*(a*Z2+X1*X2)*(X1*Z2+X2) - X3*SQR(X1*Z2-X2);
  1445. * Z4 = two*Y1*Z3*SQR(Z2);
  1446. *
  1447. * Z4 != 0 because:
  1448. * - Z2==0 implies r is at infinity (handled by the BN_is_zero(r->Z) branch);
  1449. * - Z3==0 implies s is at infinity (handled by the BN_is_zero(s->Z) branch);
  1450. * - Y1==0 implies p has order 2, so either r or s are infinity and handled by
  1451. * one of the BN_is_zero(...) branches.
  1452. */
  1453. int ec_GFp_simple_ladder_post(const EC_GROUP *group,
  1454. EC_POINT *r, EC_POINT *s,
  1455. EC_POINT *p, BN_CTX *ctx)
  1456. {
  1457. int ret = 0;
  1458. BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL;
  1459. if (BN_is_zero(r->Z))
  1460. return EC_POINT_set_to_infinity(group, r);
  1461. if (BN_is_zero(s->Z)) {
  1462. if (!EC_POINT_copy(r, p)
  1463. || !EC_POINT_invert(group, r, ctx))
  1464. return 0;
  1465. return 1;
  1466. }
  1467. BN_CTX_start(ctx);
  1468. t0 = BN_CTX_get(ctx);
  1469. t1 = BN_CTX_get(ctx);
  1470. t2 = BN_CTX_get(ctx);
  1471. t3 = BN_CTX_get(ctx);
  1472. t4 = BN_CTX_get(ctx);
  1473. t5 = BN_CTX_get(ctx);
  1474. t6 = BN_CTX_get(ctx);
  1475. if (t6 == NULL
  1476. || !BN_mod_lshift1_quick(t4, p->Y, group->field)
  1477. || !group->meth->field_mul(group, t6, r->X, t4, ctx)
  1478. || !group->meth->field_mul(group, t6, s->Z, t6, ctx)
  1479. || !group->meth->field_mul(group, t5, r->Z, t6, ctx)
  1480. || !BN_mod_lshift1_quick(t1, group->b, group->field)
  1481. || !group->meth->field_mul(group, t1, s->Z, t1, ctx)
  1482. || !group->meth->field_sqr(group, t3, r->Z, ctx)
  1483. || !group->meth->field_mul(group, t2, t3, t1, ctx)
  1484. || !group->meth->field_mul(group, t6, r->Z, group->a, ctx)
  1485. || !group->meth->field_mul(group, t1, p->X, r->X, ctx)
  1486. || !BN_mod_add_quick(t1, t1, t6, group->field)
  1487. || !group->meth->field_mul(group, t1, s->Z, t1, ctx)
  1488. || !group->meth->field_mul(group, t0, p->X, r->Z, ctx)
  1489. || !BN_mod_add_quick(t6, r->X, t0, group->field)
  1490. || !group->meth->field_mul(group, t6, t6, t1, ctx)
  1491. || !BN_mod_add_quick(t6, t6, t2, group->field)
  1492. || !BN_mod_sub_quick(t0, t0, r->X, group->field)
  1493. || !group->meth->field_sqr(group, t0, t0, ctx)
  1494. || !group->meth->field_mul(group, t0, t0, s->X, ctx)
  1495. || !BN_mod_sub_quick(t0, t6, t0, group->field)
  1496. || !group->meth->field_mul(group, t1, s->Z, t4, ctx)
  1497. || !group->meth->field_mul(group, t1, t3, t1, ctx)
  1498. || (group->meth->field_decode != NULL
  1499. && !group->meth->field_decode(group, t1, t1, ctx))
  1500. || !group->meth->field_inv(group, t1, t1, ctx)
  1501. || (group->meth->field_encode != NULL
  1502. && !group->meth->field_encode(group, t1, t1, ctx))
  1503. || !group->meth->field_mul(group, r->X, t5, t1, ctx)
  1504. || !group->meth->field_mul(group, r->Y, t0, t1, ctx))
  1505. goto err;
  1506. if (group->meth->field_set_to_one != NULL) {
  1507. if (!group->meth->field_set_to_one(group, r->Z, ctx))
  1508. goto err;
  1509. } else {
  1510. if (!BN_one(r->Z))
  1511. goto err;
  1512. }
  1513. r->Z_is_one = 1;
  1514. ret = 1;
  1515. err:
  1516. BN_CTX_end(ctx);
  1517. return ret;
  1518. }