ecp_smpl.c 48 KB

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