simple.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <string.h>
  69. #include <openssl/bn.h>
  70. #include <openssl/err.h>
  71. #include <openssl/mem.h>
  72. #include "internal.h"
  73. #include "../../internal.h"
  74. // Most method functions in this file are designed to work with non-trivial
  75. // representations of field elements if necessary (see ecp_mont.c): while
  76. // standard modular addition and subtraction are used, the field_mul and
  77. // field_sqr methods will be used for multiplication, and field_encode and
  78. // field_decode (if defined) will be used for converting between
  79. // representations.
  80. //
  81. // Functions here specifically assume that if a non-trivial representation is
  82. // used, it is a Montgomery representation (i.e. 'encoding' means multiplying
  83. // by some factor R).
  84. int ec_GFp_simple_group_init(EC_GROUP *group) {
  85. BN_init(&group->field);
  86. BN_init(&group->a);
  87. BN_init(&group->b);
  88. BN_init(&group->one);
  89. group->a_is_minus3 = 0;
  90. return 1;
  91. }
  92. void ec_GFp_simple_group_finish(EC_GROUP *group) {
  93. BN_free(&group->field);
  94. BN_free(&group->a);
  95. BN_free(&group->b);
  96. BN_free(&group->one);
  97. }
  98. int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  99. const BIGNUM *a, const BIGNUM *b,
  100. BN_CTX *ctx) {
  101. int ret = 0;
  102. BN_CTX *new_ctx = NULL;
  103. BIGNUM *tmp_a;
  104. // p must be a prime > 3
  105. if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
  106. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
  107. return 0;
  108. }
  109. if (ctx == NULL) {
  110. ctx = new_ctx = BN_CTX_new();
  111. if (ctx == NULL) {
  112. return 0;
  113. }
  114. }
  115. BN_CTX_start(ctx);
  116. tmp_a = BN_CTX_get(ctx);
  117. if (tmp_a == NULL) {
  118. goto err;
  119. }
  120. // group->field
  121. if (!BN_copy(&group->field, p)) {
  122. goto err;
  123. }
  124. BN_set_negative(&group->field, 0);
  125. // group->a
  126. if (!BN_nnmod(tmp_a, a, p, ctx)) {
  127. goto err;
  128. }
  129. if (group->meth->field_encode) {
  130. if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) {
  131. goto err;
  132. }
  133. } else if (!BN_copy(&group->a, tmp_a)) {
  134. goto err;
  135. }
  136. // group->b
  137. if (!BN_nnmod(&group->b, b, p, ctx)) {
  138. goto err;
  139. }
  140. if (group->meth->field_encode &&
  141. !group->meth->field_encode(group, &group->b, &group->b, ctx)) {
  142. goto err;
  143. }
  144. // group->a_is_minus3
  145. if (!BN_add_word(tmp_a, 3)) {
  146. goto err;
  147. }
  148. group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
  149. if (group->meth->field_encode != NULL) {
  150. if (!group->meth->field_encode(group, &group->one, BN_value_one(), ctx)) {
  151. goto err;
  152. }
  153. } else if (!BN_copy(&group->one, BN_value_one())) {
  154. goto err;
  155. }
  156. ret = 1;
  157. err:
  158. BN_CTX_end(ctx);
  159. BN_CTX_free(new_ctx);
  160. return ret;
  161. }
  162. int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  163. BIGNUM *b, BN_CTX *ctx) {
  164. int ret = 0;
  165. BN_CTX *new_ctx = NULL;
  166. if (p != NULL && !BN_copy(p, &group->field)) {
  167. return 0;
  168. }
  169. if (a != NULL || b != NULL) {
  170. if (group->meth->field_decode) {
  171. if (ctx == NULL) {
  172. ctx = new_ctx = BN_CTX_new();
  173. if (ctx == NULL) {
  174. return 0;
  175. }
  176. }
  177. if (a != NULL && !group->meth->field_decode(group, a, &group->a, ctx)) {
  178. goto err;
  179. }
  180. if (b != NULL && !group->meth->field_decode(group, b, &group->b, ctx)) {
  181. goto err;
  182. }
  183. } else {
  184. if (a != NULL && !BN_copy(a, &group->a)) {
  185. goto err;
  186. }
  187. if (b != NULL && !BN_copy(b, &group->b)) {
  188. goto err;
  189. }
  190. }
  191. }
  192. ret = 1;
  193. err:
  194. BN_CTX_free(new_ctx);
  195. return ret;
  196. }
  197. unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
  198. return BN_num_bits(&group->field);
  199. }
  200. int ec_GFp_simple_point_init(EC_POINT *point) {
  201. BN_init(&point->X);
  202. BN_init(&point->Y);
  203. BN_init(&point->Z);
  204. return 1;
  205. }
  206. void ec_GFp_simple_point_finish(EC_POINT *point) {
  207. BN_free(&point->X);
  208. BN_free(&point->Y);
  209. BN_free(&point->Z);
  210. }
  211. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
  212. if (!BN_copy(&dest->X, &src->X) ||
  213. !BN_copy(&dest->Y, &src->Y) ||
  214. !BN_copy(&dest->Z, &src->Z)) {
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
  220. EC_POINT *point) {
  221. BN_zero(&point->Z);
  222. return 1;
  223. }
  224. static int set_Jprojective_coordinate_GFp(const EC_GROUP *group, BIGNUM *out,
  225. const BIGNUM *in, BN_CTX *ctx) {
  226. if (in == NULL) {
  227. return 1;
  228. }
  229. if (BN_is_negative(in) ||
  230. BN_cmp(in, &group->field) >= 0) {
  231. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  232. return 0;
  233. }
  234. if (group->meth->field_encode) {
  235. return group->meth->field_encode(group, out, in, ctx);
  236. }
  237. return BN_copy(out, in) != NULL;
  238. }
  239. int ec_GFp_simple_set_Jprojective_coordinates_GFp(
  240. const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y,
  241. const BIGNUM *z, BN_CTX *ctx) {
  242. BN_CTX *new_ctx = NULL;
  243. int ret = 0;
  244. if (ctx == NULL) {
  245. ctx = new_ctx = BN_CTX_new();
  246. if (ctx == NULL) {
  247. return 0;
  248. }
  249. }
  250. if (!set_Jprojective_coordinate_GFp(group, &point->X, x, ctx) ||
  251. !set_Jprojective_coordinate_GFp(group, &point->Y, y, ctx) ||
  252. !set_Jprojective_coordinate_GFp(group, &point->Z, z, ctx)) {
  253. goto err;
  254. }
  255. ret = 1;
  256. err:
  257. BN_CTX_free(new_ctx);
  258. return ret;
  259. }
  260. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
  261. EC_POINT *point, const BIGNUM *x,
  262. const BIGNUM *y, BN_CTX *ctx) {
  263. if (x == NULL || y == NULL) {
  264. // unlike for projective coordinates, we do not tolerate this
  265. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  266. return 0;
  267. }
  268. return ec_point_set_Jprojective_coordinates_GFp(group, point, x, y,
  269. BN_value_one(), ctx);
  270. }
  271. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  272. const EC_POINT *b, BN_CTX *ctx) {
  273. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  274. BN_CTX *);
  275. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  276. const BIGNUM *p;
  277. BN_CTX *new_ctx = NULL;
  278. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  279. int ret = 0;
  280. if (a == b) {
  281. return EC_POINT_dbl(group, r, a, ctx);
  282. }
  283. if (EC_POINT_is_at_infinity(group, a)) {
  284. return EC_POINT_copy(r, b);
  285. }
  286. if (EC_POINT_is_at_infinity(group, b)) {
  287. return EC_POINT_copy(r, a);
  288. }
  289. field_mul = group->meth->field_mul;
  290. field_sqr = group->meth->field_sqr;
  291. p = &group->field;
  292. if (ctx == NULL) {
  293. ctx = new_ctx = BN_CTX_new();
  294. if (ctx == NULL) {
  295. return 0;
  296. }
  297. }
  298. BN_CTX_start(ctx);
  299. n0 = BN_CTX_get(ctx);
  300. n1 = BN_CTX_get(ctx);
  301. n2 = BN_CTX_get(ctx);
  302. n3 = BN_CTX_get(ctx);
  303. n4 = BN_CTX_get(ctx);
  304. n5 = BN_CTX_get(ctx);
  305. n6 = BN_CTX_get(ctx);
  306. if (n6 == NULL) {
  307. goto end;
  308. }
  309. // Note that in this function we must not read components of 'a' or 'b'
  310. // once we have written the corresponding components of 'r'.
  311. // ('r' might be one of 'a' or 'b'.)
  312. // n1, n2
  313. int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
  314. if (b_Z_is_one) {
  315. if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
  316. goto end;
  317. }
  318. // n1 = X_a
  319. // n2 = Y_a
  320. } else {
  321. if (!field_sqr(group, n0, &b->Z, ctx) ||
  322. !field_mul(group, n1, &a->X, n0, ctx)) {
  323. goto end;
  324. }
  325. // n1 = X_a * Z_b^2
  326. if (!field_mul(group, n0, n0, &b->Z, ctx) ||
  327. !field_mul(group, n2, &a->Y, n0, ctx)) {
  328. goto end;
  329. }
  330. // n2 = Y_a * Z_b^3
  331. }
  332. // n3, n4
  333. int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
  334. if (a_Z_is_one) {
  335. if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
  336. goto end;
  337. }
  338. // n3 = X_b
  339. // n4 = Y_b
  340. } else {
  341. if (!field_sqr(group, n0, &a->Z, ctx) ||
  342. !field_mul(group, n3, &b->X, n0, ctx)) {
  343. goto end;
  344. }
  345. // n3 = X_b * Z_a^2
  346. if (!field_mul(group, n0, n0, &a->Z, ctx) ||
  347. !field_mul(group, n4, &b->Y, n0, ctx)) {
  348. goto end;
  349. }
  350. // n4 = Y_b * Z_a^3
  351. }
  352. // n5, n6
  353. if (!BN_mod_sub_quick(n5, n1, n3, p) ||
  354. !BN_mod_sub_quick(n6, n2, n4, p)) {
  355. goto end;
  356. }
  357. // n5 = n1 - n3
  358. // n6 = n2 - n4
  359. if (BN_is_zero(n5)) {
  360. if (BN_is_zero(n6)) {
  361. // a is the same point as b
  362. BN_CTX_end(ctx);
  363. ret = EC_POINT_dbl(group, r, a, ctx);
  364. ctx = NULL;
  365. goto end;
  366. } else {
  367. // a is the inverse of b
  368. BN_zero(&r->Z);
  369. ret = 1;
  370. goto end;
  371. }
  372. }
  373. // 'n7', 'n8'
  374. if (!BN_mod_add_quick(n1, n1, n3, p) ||
  375. !BN_mod_add_quick(n2, n2, n4, p)) {
  376. goto end;
  377. }
  378. // 'n7' = n1 + n3
  379. // 'n8' = n2 + n4
  380. // Z_r
  381. if (a_Z_is_one && b_Z_is_one) {
  382. if (!BN_copy(&r->Z, n5)) {
  383. goto end;
  384. }
  385. } else {
  386. if (a_Z_is_one) {
  387. if (!BN_copy(n0, &b->Z)) {
  388. goto end;
  389. }
  390. } else if (b_Z_is_one) {
  391. if (!BN_copy(n0, &a->Z)) {
  392. goto end;
  393. }
  394. } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
  395. goto end;
  396. }
  397. if (!field_mul(group, &r->Z, n0, n5, ctx)) {
  398. goto end;
  399. }
  400. }
  401. // Z_r = Z_a * Z_b * n5
  402. // X_r
  403. if (!field_sqr(group, n0, n6, ctx) ||
  404. !field_sqr(group, n4, n5, ctx) ||
  405. !field_mul(group, n3, n1, n4, ctx) ||
  406. !BN_mod_sub_quick(&r->X, n0, n3, p)) {
  407. goto end;
  408. }
  409. // X_r = n6^2 - n5^2 * 'n7'
  410. // 'n9'
  411. if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
  412. !BN_mod_sub_quick(n0, n3, n0, p)) {
  413. goto end;
  414. }
  415. // n9 = n5^2 * 'n7' - 2 * X_r
  416. // Y_r
  417. if (!field_mul(group, n0, n0, n6, ctx) ||
  418. !field_mul(group, n5, n4, n5, ctx)) {
  419. goto end; // now n5 is n5^3
  420. }
  421. if (!field_mul(group, n1, n2, n5, ctx) ||
  422. !BN_mod_sub_quick(n0, n0, n1, p)) {
  423. goto end;
  424. }
  425. if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
  426. goto end;
  427. }
  428. // now 0 <= n0 < 2*p, and n0 is even
  429. if (!BN_rshift1(&r->Y, n0)) {
  430. goto end;
  431. }
  432. // Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2
  433. ret = 1;
  434. end:
  435. if (ctx) {
  436. // otherwise we already called BN_CTX_end
  437. BN_CTX_end(ctx);
  438. }
  439. BN_CTX_free(new_ctx);
  440. return ret;
  441. }
  442. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  443. BN_CTX *ctx) {
  444. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  445. BN_CTX *);
  446. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  447. const BIGNUM *p;
  448. BN_CTX *new_ctx = NULL;
  449. BIGNUM *n0, *n1, *n2, *n3;
  450. int ret = 0;
  451. if (EC_POINT_is_at_infinity(group, a)) {
  452. BN_zero(&r->Z);
  453. return 1;
  454. }
  455. field_mul = group->meth->field_mul;
  456. field_sqr = group->meth->field_sqr;
  457. p = &group->field;
  458. if (ctx == NULL) {
  459. ctx = new_ctx = BN_CTX_new();
  460. if (ctx == NULL) {
  461. return 0;
  462. }
  463. }
  464. BN_CTX_start(ctx);
  465. n0 = BN_CTX_get(ctx);
  466. n1 = BN_CTX_get(ctx);
  467. n2 = BN_CTX_get(ctx);
  468. n3 = BN_CTX_get(ctx);
  469. if (n3 == NULL) {
  470. goto err;
  471. }
  472. // Note that in this function we must not read components of 'a'
  473. // once we have written the corresponding components of 'r'.
  474. // ('r' might the same as 'a'.)
  475. // n1
  476. if (BN_cmp(&a->Z, &group->one) == 0) {
  477. if (!field_sqr(group, n0, &a->X, ctx) ||
  478. !BN_mod_lshift1_quick(n1, n0, p) ||
  479. !BN_mod_add_quick(n0, n0, n1, p) ||
  480. !BN_mod_add_quick(n1, n0, &group->a, p)) {
  481. goto err;
  482. }
  483. // n1 = 3 * X_a^2 + a_curve
  484. } else if (group->a_is_minus3) {
  485. if (!field_sqr(group, n1, &a->Z, ctx) ||
  486. !BN_mod_add_quick(n0, &a->X, n1, p) ||
  487. !BN_mod_sub_quick(n2, &a->X, n1, p) ||
  488. !field_mul(group, n1, n0, n2, ctx) ||
  489. !BN_mod_lshift1_quick(n0, n1, p) ||
  490. !BN_mod_add_quick(n1, n0, n1, p)) {
  491. goto err;
  492. }
  493. // n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  494. // = 3 * X_a^2 - 3 * Z_a^4
  495. } else {
  496. if (!field_sqr(group, n0, &a->X, ctx) ||
  497. !BN_mod_lshift1_quick(n1, n0, p) ||
  498. !BN_mod_add_quick(n0, n0, n1, p) ||
  499. !field_sqr(group, n1, &a->Z, ctx) ||
  500. !field_sqr(group, n1, n1, ctx) ||
  501. !field_mul(group, n1, n1, &group->a, ctx) ||
  502. !BN_mod_add_quick(n1, n1, n0, p)) {
  503. goto err;
  504. }
  505. // n1 = 3 * X_a^2 + a_curve * Z_a^4
  506. }
  507. // Z_r
  508. if (BN_cmp(&a->Z, &group->one) == 0) {
  509. if (!BN_copy(n0, &a->Y)) {
  510. goto err;
  511. }
  512. } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
  513. goto err;
  514. }
  515. if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
  516. goto err;
  517. }
  518. // Z_r = 2 * Y_a * Z_a
  519. // n2
  520. if (!field_sqr(group, n3, &a->Y, ctx) ||
  521. !field_mul(group, n2, &a->X, n3, ctx) ||
  522. !BN_mod_lshift_quick(n2, n2, 2, p)) {
  523. goto err;
  524. }
  525. // n2 = 4 * X_a * Y_a^2
  526. // X_r
  527. if (!BN_mod_lshift1_quick(n0, n2, p) ||
  528. !field_sqr(group, &r->X, n1, ctx) ||
  529. !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
  530. goto err;
  531. }
  532. // X_r = n1^2 - 2 * n2
  533. // n3
  534. if (!field_sqr(group, n0, n3, ctx) ||
  535. !BN_mod_lshift_quick(n3, n0, 3, p)) {
  536. goto err;
  537. }
  538. // n3 = 8 * Y_a^4
  539. // Y_r
  540. if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
  541. !field_mul(group, n0, n1, n0, ctx) ||
  542. !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
  543. goto err;
  544. }
  545. // Y_r = n1 * (n2 - X_r) - n3
  546. ret = 1;
  547. err:
  548. BN_CTX_end(ctx);
  549. BN_CTX_free(new_ctx);
  550. return ret;
  551. }
  552. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
  553. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
  554. // point is its own inverse
  555. return 1;
  556. }
  557. return BN_usub(&point->Y, &group->field, &point->Y);
  558. }
  559. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
  560. return BN_is_zero(&point->Z);
  561. }
  562. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  563. BN_CTX *ctx) {
  564. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  565. BN_CTX *);
  566. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  567. const BIGNUM *p;
  568. BN_CTX *new_ctx = NULL;
  569. BIGNUM *rh, *tmp, *Z4, *Z6;
  570. int ret = 0;
  571. if (EC_POINT_is_at_infinity(group, point)) {
  572. return 1;
  573. }
  574. field_mul = group->meth->field_mul;
  575. field_sqr = group->meth->field_sqr;
  576. p = &group->field;
  577. if (ctx == NULL) {
  578. ctx = new_ctx = BN_CTX_new();
  579. if (ctx == NULL) {
  580. return 0;
  581. }
  582. }
  583. BN_CTX_start(ctx);
  584. rh = BN_CTX_get(ctx);
  585. tmp = BN_CTX_get(ctx);
  586. Z4 = BN_CTX_get(ctx);
  587. Z6 = BN_CTX_get(ctx);
  588. if (Z6 == NULL) {
  589. goto err;
  590. }
  591. // We have a curve defined by a Weierstrass equation
  592. // y^2 = x^3 + a*x + b.
  593. // The point to consider is given in Jacobian projective coordinates
  594. // where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  595. // Substituting this and multiplying by Z^6 transforms the above equation
  596. // into
  597. // Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  598. // To test this, we add up the right-hand side in 'rh'.
  599. // rh := X^2
  600. if (!field_sqr(group, rh, &point->X, ctx)) {
  601. goto err;
  602. }
  603. if (BN_cmp(&point->Z, &group->one) != 0) {
  604. if (!field_sqr(group, tmp, &point->Z, ctx) ||
  605. !field_sqr(group, Z4, tmp, ctx) ||
  606. !field_mul(group, Z6, Z4, tmp, ctx)) {
  607. goto err;
  608. }
  609. // rh := (rh + a*Z^4)*X
  610. if (group->a_is_minus3) {
  611. if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
  612. !BN_mod_add_quick(tmp, tmp, Z4, p) ||
  613. !BN_mod_sub_quick(rh, rh, tmp, p) ||
  614. !field_mul(group, rh, rh, &point->X, ctx)) {
  615. goto err;
  616. }
  617. } else {
  618. if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
  619. !BN_mod_add_quick(rh, rh, tmp, p) ||
  620. !field_mul(group, rh, rh, &point->X, ctx)) {
  621. goto err;
  622. }
  623. }
  624. // rh := rh + b*Z^6
  625. if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
  626. !BN_mod_add_quick(rh, rh, tmp, p)) {
  627. goto err;
  628. }
  629. } else {
  630. // rh := (rh + a)*X
  631. if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
  632. !field_mul(group, rh, rh, &point->X, ctx)) {
  633. goto err;
  634. }
  635. // rh := rh + b
  636. if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
  637. goto err;
  638. }
  639. }
  640. // 'lh' := Y^2
  641. if (!field_sqr(group, tmp, &point->Y, ctx)) {
  642. goto err;
  643. }
  644. ret = (0 == BN_ucmp(tmp, rh));
  645. err:
  646. BN_CTX_end(ctx);
  647. BN_CTX_free(new_ctx);
  648. return ret;
  649. }
  650. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  651. const EC_POINT *b, BN_CTX *ctx) {
  652. // return values:
  653. // -1 error
  654. // 0 equal (in affine coordinates)
  655. // 1 not equal
  656. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  657. BN_CTX *);
  658. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  659. BN_CTX *new_ctx = NULL;
  660. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  661. const BIGNUM *tmp1_, *tmp2_;
  662. int ret = -1;
  663. if (ec_GFp_simple_is_at_infinity(group, a)) {
  664. return ec_GFp_simple_is_at_infinity(group, b) ? 0 : 1;
  665. }
  666. if (ec_GFp_simple_is_at_infinity(group, b)) {
  667. return 1;
  668. }
  669. int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
  670. int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
  671. if (a_Z_is_one && b_Z_is_one) {
  672. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  673. }
  674. field_mul = group->meth->field_mul;
  675. field_sqr = group->meth->field_sqr;
  676. if (ctx == NULL) {
  677. ctx = new_ctx = BN_CTX_new();
  678. if (ctx == NULL) {
  679. return -1;
  680. }
  681. }
  682. BN_CTX_start(ctx);
  683. tmp1 = BN_CTX_get(ctx);
  684. tmp2 = BN_CTX_get(ctx);
  685. Za23 = BN_CTX_get(ctx);
  686. Zb23 = BN_CTX_get(ctx);
  687. if (Zb23 == NULL) {
  688. goto end;
  689. }
  690. // We have to decide whether
  691. // (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  692. // or equivalently, whether
  693. // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  694. if (!b_Z_is_one) {
  695. if (!field_sqr(group, Zb23, &b->Z, ctx) ||
  696. !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
  697. goto end;
  698. }
  699. tmp1_ = tmp1;
  700. } else {
  701. tmp1_ = &a->X;
  702. }
  703. if (!a_Z_is_one) {
  704. if (!field_sqr(group, Za23, &a->Z, ctx) ||
  705. !field_mul(group, tmp2, &b->X, Za23, ctx)) {
  706. goto end;
  707. }
  708. tmp2_ = tmp2;
  709. } else {
  710. tmp2_ = &b->X;
  711. }
  712. // compare X_a*Z_b^2 with X_b*Z_a^2
  713. if (BN_cmp(tmp1_, tmp2_) != 0) {
  714. ret = 1; // points differ
  715. goto end;
  716. }
  717. if (!b_Z_is_one) {
  718. if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
  719. !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
  720. goto end;
  721. }
  722. // tmp1_ = tmp1
  723. } else {
  724. tmp1_ = &a->Y;
  725. }
  726. if (!a_Z_is_one) {
  727. if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
  728. !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
  729. goto end;
  730. }
  731. // tmp2_ = tmp2
  732. } else {
  733. tmp2_ = &b->Y;
  734. }
  735. // compare Y_a*Z_b^3 with Y_b*Z_a^3
  736. if (BN_cmp(tmp1_, tmp2_) != 0) {
  737. ret = 1; // points differ
  738. goto end;
  739. }
  740. // points are equal
  741. ret = 0;
  742. end:
  743. BN_CTX_end(ctx);
  744. BN_CTX_free(new_ctx);
  745. return ret;
  746. }
  747. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  748. BN_CTX *ctx) {
  749. BN_CTX *new_ctx = NULL;
  750. BIGNUM *x, *y;
  751. int ret = 0;
  752. if (BN_cmp(&point->Z, &group->one) == 0 ||
  753. EC_POINT_is_at_infinity(group, point)) {
  754. return 1;
  755. }
  756. if (ctx == NULL) {
  757. ctx = new_ctx = BN_CTX_new();
  758. if (ctx == NULL) {
  759. return 0;
  760. }
  761. }
  762. BN_CTX_start(ctx);
  763. x = BN_CTX_get(ctx);
  764. y = BN_CTX_get(ctx);
  765. if (y == NULL) {
  766. goto err;
  767. }
  768. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) ||
  769. !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  770. goto err;
  771. }
  772. if (BN_cmp(&point->Z, &group->one) != 0) {
  773. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  774. goto err;
  775. }
  776. ret = 1;
  777. err:
  778. BN_CTX_end(ctx);
  779. BN_CTX_free(new_ctx);
  780. return ret;
  781. }
  782. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
  783. EC_POINT *points[], BN_CTX *ctx) {
  784. BN_CTX *new_ctx = NULL;
  785. BIGNUM *tmp, *tmp_Z;
  786. BIGNUM **prod_Z = NULL;
  787. int ret = 0;
  788. if (num == 0) {
  789. return 1;
  790. }
  791. if (ctx == NULL) {
  792. ctx = new_ctx = BN_CTX_new();
  793. if (ctx == NULL) {
  794. return 0;
  795. }
  796. }
  797. BN_CTX_start(ctx);
  798. tmp = BN_CTX_get(ctx);
  799. tmp_Z = BN_CTX_get(ctx);
  800. if (tmp == NULL || tmp_Z == NULL) {
  801. goto err;
  802. }
  803. prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
  804. if (prod_Z == NULL) {
  805. goto err;
  806. }
  807. OPENSSL_memset(prod_Z, 0, num * sizeof(prod_Z[0]));
  808. for (size_t i = 0; i < num; i++) {
  809. prod_Z[i] = BN_new();
  810. if (prod_Z[i] == NULL) {
  811. goto err;
  812. }
  813. }
  814. // Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
  815. // skipping any zero-valued inputs (pretend that they're 1).
  816. if (!BN_is_zero(&points[0]->Z)) {
  817. if (!BN_copy(prod_Z[0], &points[0]->Z)) {
  818. goto err;
  819. }
  820. } else {
  821. if (BN_copy(prod_Z[0], &group->one) == NULL) {
  822. goto err;
  823. }
  824. }
  825. for (size_t i = 1; i < num; i++) {
  826. if (!BN_is_zero(&points[i]->Z)) {
  827. if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
  828. &points[i]->Z, ctx)) {
  829. goto err;
  830. }
  831. } else {
  832. if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
  833. goto err;
  834. }
  835. }
  836. }
  837. // Now use a single explicit inversion to replace every non-zero points[i]->Z
  838. // by its inverse. We use |BN_mod_inverse_odd| instead of doing a constant-
  839. // time inversion using Fermat's Little Theorem because this function is
  840. // usually only used for converting multiples of a public key point to
  841. // affine, and a public key point isn't secret. If we were to use Fermat's
  842. // Little Theorem then the cost of the inversion would usually be so high
  843. // that converting the multiples to affine would be counterproductive.
  844. int no_inverse;
  845. if (!BN_mod_inverse_odd(tmp, &no_inverse, prod_Z[num - 1], &group->field,
  846. ctx)) {
  847. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  848. goto err;
  849. }
  850. if (group->meth->field_encode != NULL) {
  851. // In the Montgomery case, we just turned R*H (representing H)
  852. // into 1/(R*H), but we need R*(1/H) (representing 1/H);
  853. // i.e. we need to multiply by the Montgomery factor twice.
  854. if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
  855. !group->meth->field_encode(group, tmp, tmp, ctx)) {
  856. goto err;
  857. }
  858. }
  859. for (size_t i = num - 1; i > 0; --i) {
  860. // Loop invariant: tmp is the product of the inverses of
  861. // points[0]->Z .. points[i]->Z (zero-valued inputs skipped).
  862. if (BN_is_zero(&points[i]->Z)) {
  863. continue;
  864. }
  865. // Set tmp_Z to the inverse of points[i]->Z (as product
  866. // of Z inverses 0 .. i, Z values 0 .. i - 1).
  867. if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
  868. // Update tmp to satisfy the loop invariant for i - 1.
  869. !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
  870. // Replace points[i]->Z by its inverse.
  871. !BN_copy(&points[i]->Z, tmp_Z)) {
  872. goto err;
  873. }
  874. }
  875. // Replace points[0]->Z by its inverse.
  876. if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
  877. goto err;
  878. }
  879. // Finally, fix up the X and Y coordinates for all points.
  880. for (size_t i = 0; i < num; i++) {
  881. EC_POINT *p = points[i];
  882. if (!BN_is_zero(&p->Z)) {
  883. // turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1).
  884. if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
  885. !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
  886. !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
  887. !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
  888. goto err;
  889. }
  890. if (BN_copy(&p->Z, &group->one) == NULL) {
  891. goto err;
  892. }
  893. }
  894. }
  895. ret = 1;
  896. err:
  897. BN_CTX_end(ctx);
  898. BN_CTX_free(new_ctx);
  899. if (prod_Z != NULL) {
  900. for (size_t i = 0; i < num; i++) {
  901. if (prod_Z[i] == NULL) {
  902. break;
  903. }
  904. BN_clear_free(prod_Z[i]);
  905. }
  906. OPENSSL_free(prod_Z);
  907. }
  908. return ret;
  909. }
  910. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  911. const BIGNUM *b, BN_CTX *ctx) {
  912. return BN_mod_mul(r, a, b, &group->field, ctx);
  913. }
  914. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  915. BN_CTX *ctx) {
  916. return BN_mod_sqr(r, a, &group->field, ctx);
  917. }