simple.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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_copy(EC_GROUP *dest, const EC_GROUP *src) {
  99. if (!BN_copy(&dest->field, &src->field) ||
  100. !BN_copy(&dest->a, &src->a) ||
  101. !BN_copy(&dest->b, &src->b) ||
  102. !BN_copy(&dest->one, &src->one)) {
  103. return 0;
  104. }
  105. dest->a_is_minus3 = src->a_is_minus3;
  106. return 1;
  107. }
  108. int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  109. const BIGNUM *a, const BIGNUM *b,
  110. BN_CTX *ctx) {
  111. int ret = 0;
  112. BN_CTX *new_ctx = NULL;
  113. BIGNUM *tmp_a;
  114. /* p must be a prime > 3 */
  115. if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
  116. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
  117. return 0;
  118. }
  119. if (ctx == NULL) {
  120. ctx = new_ctx = BN_CTX_new();
  121. if (ctx == NULL) {
  122. return 0;
  123. }
  124. }
  125. BN_CTX_start(ctx);
  126. tmp_a = BN_CTX_get(ctx);
  127. if (tmp_a == NULL) {
  128. goto err;
  129. }
  130. /* group->field */
  131. if (!BN_copy(&group->field, p)) {
  132. goto err;
  133. }
  134. BN_set_negative(&group->field, 0);
  135. /* group->a */
  136. if (!BN_nnmod(tmp_a, a, p, ctx)) {
  137. goto err;
  138. }
  139. if (group->meth->field_encode) {
  140. if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) {
  141. goto err;
  142. }
  143. } else if (!BN_copy(&group->a, tmp_a)) {
  144. goto err;
  145. }
  146. /* group->b */
  147. if (!BN_nnmod(&group->b, b, p, ctx)) {
  148. goto err;
  149. }
  150. if (group->meth->field_encode &&
  151. !group->meth->field_encode(group, &group->b, &group->b, ctx)) {
  152. goto err;
  153. }
  154. /* group->a_is_minus3 */
  155. if (!BN_add_word(tmp_a, 3)) {
  156. goto err;
  157. }
  158. group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
  159. if (group->meth->field_encode != NULL) {
  160. if (!group->meth->field_encode(group, &group->one, BN_value_one(), ctx)) {
  161. goto err;
  162. }
  163. } else if (!BN_copy(&group->one, BN_value_one())) {
  164. goto err;
  165. }
  166. ret = 1;
  167. err:
  168. BN_CTX_end(ctx);
  169. BN_CTX_free(new_ctx);
  170. return ret;
  171. }
  172. int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  173. BIGNUM *b, BN_CTX *ctx) {
  174. int ret = 0;
  175. BN_CTX *new_ctx = NULL;
  176. if (p != NULL && !BN_copy(p, &group->field)) {
  177. return 0;
  178. }
  179. if (a != NULL || b != NULL) {
  180. if (group->meth->field_decode) {
  181. if (ctx == NULL) {
  182. ctx = new_ctx = BN_CTX_new();
  183. if (ctx == NULL) {
  184. return 0;
  185. }
  186. }
  187. if (a != NULL && !group->meth->field_decode(group, a, &group->a, ctx)) {
  188. goto err;
  189. }
  190. if (b != NULL && !group->meth->field_decode(group, b, &group->b, ctx)) {
  191. goto err;
  192. }
  193. } else {
  194. if (a != NULL && !BN_copy(a, &group->a)) {
  195. goto err;
  196. }
  197. if (b != NULL && !BN_copy(b, &group->b)) {
  198. goto err;
  199. }
  200. }
  201. }
  202. ret = 1;
  203. err:
  204. BN_CTX_free(new_ctx);
  205. return ret;
  206. }
  207. unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
  208. return BN_num_bits(&group->field);
  209. }
  210. int ec_GFp_simple_point_init(EC_POINT *point) {
  211. BN_init(&point->X);
  212. BN_init(&point->Y);
  213. BN_init(&point->Z);
  214. return 1;
  215. }
  216. void ec_GFp_simple_point_finish(EC_POINT *point) {
  217. BN_free(&point->X);
  218. BN_free(&point->Y);
  219. BN_free(&point->Z);
  220. }
  221. void ec_GFp_simple_point_clear_finish(EC_POINT *point) {
  222. BN_clear_free(&point->X);
  223. BN_clear_free(&point->Y);
  224. BN_clear_free(&point->Z);
  225. }
  226. int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
  227. if (!BN_copy(&dest->X, &src->X) ||
  228. !BN_copy(&dest->Y, &src->Y) ||
  229. !BN_copy(&dest->Z, &src->Z)) {
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
  235. EC_POINT *point) {
  236. BN_zero(&point->Z);
  237. return 1;
  238. }
  239. static int set_Jprojective_coordinate_GFp(const EC_GROUP *group, BIGNUM *out,
  240. const BIGNUM *in, BN_CTX *ctx) {
  241. if (in == NULL) {
  242. return 1;
  243. }
  244. if (BN_is_negative(in) ||
  245. BN_cmp(in, &group->field) >= 0) {
  246. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  247. return 0;
  248. }
  249. if (group->meth->field_encode) {
  250. return group->meth->field_encode(group, out, in, ctx);
  251. }
  252. return BN_copy(out, in) != NULL;
  253. }
  254. int ec_GFp_simple_set_Jprojective_coordinates_GFp(
  255. const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y,
  256. const BIGNUM *z, BN_CTX *ctx) {
  257. BN_CTX *new_ctx = NULL;
  258. int ret = 0;
  259. if (ctx == NULL) {
  260. ctx = new_ctx = BN_CTX_new();
  261. if (ctx == NULL) {
  262. return 0;
  263. }
  264. }
  265. if (!set_Jprojective_coordinate_GFp(group, &point->X, x, ctx) ||
  266. !set_Jprojective_coordinate_GFp(group, &point->Y, y, ctx) ||
  267. !set_Jprojective_coordinate_GFp(group, &point->Z, z, ctx)) {
  268. goto err;
  269. }
  270. ret = 1;
  271. err:
  272. BN_CTX_free(new_ctx);
  273. return ret;
  274. }
  275. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  276. const EC_POINT *point,
  277. BIGNUM *x, BIGNUM *y,
  278. BIGNUM *z, BN_CTX *ctx) {
  279. BN_CTX *new_ctx = NULL;
  280. int ret = 0;
  281. if (group->meth->field_decode != 0) {
  282. if (ctx == NULL) {
  283. ctx = new_ctx = BN_CTX_new();
  284. if (ctx == NULL) {
  285. return 0;
  286. }
  287. }
  288. if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
  289. goto err;
  290. }
  291. if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
  292. goto err;
  293. }
  294. if (z != NULL && !group->meth->field_decode(group, z, &point->Z, ctx)) {
  295. goto err;
  296. }
  297. } else {
  298. if (x != NULL && !BN_copy(x, &point->X)) {
  299. goto err;
  300. }
  301. if (y != NULL && !BN_copy(y, &point->Y)) {
  302. goto err;
  303. }
  304. if (z != NULL && !BN_copy(z, &point->Z)) {
  305. goto err;
  306. }
  307. }
  308. ret = 1;
  309. err:
  310. BN_CTX_free(new_ctx);
  311. return ret;
  312. }
  313. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
  314. EC_POINT *point, const BIGNUM *x,
  315. const BIGNUM *y, BN_CTX *ctx) {
  316. if (x == NULL || y == NULL) {
  317. /* unlike for projective coordinates, we do not tolerate this */
  318. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  319. return 0;
  320. }
  321. return ec_point_set_Jprojective_coordinates_GFp(group, point, x, y,
  322. BN_value_one(), ctx);
  323. }
  324. int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  325. const EC_POINT *b, BN_CTX *ctx) {
  326. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  327. BN_CTX *);
  328. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  329. const BIGNUM *p;
  330. BN_CTX *new_ctx = NULL;
  331. BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
  332. int ret = 0;
  333. if (a == b) {
  334. return EC_POINT_dbl(group, r, a, ctx);
  335. }
  336. if (EC_POINT_is_at_infinity(group, a)) {
  337. return EC_POINT_copy(r, b);
  338. }
  339. if (EC_POINT_is_at_infinity(group, b)) {
  340. return EC_POINT_copy(r, a);
  341. }
  342. field_mul = group->meth->field_mul;
  343. field_sqr = group->meth->field_sqr;
  344. p = &group->field;
  345. if (ctx == NULL) {
  346. ctx = new_ctx = BN_CTX_new();
  347. if (ctx == NULL) {
  348. return 0;
  349. }
  350. }
  351. BN_CTX_start(ctx);
  352. n0 = BN_CTX_get(ctx);
  353. n1 = BN_CTX_get(ctx);
  354. n2 = BN_CTX_get(ctx);
  355. n3 = BN_CTX_get(ctx);
  356. n4 = BN_CTX_get(ctx);
  357. n5 = BN_CTX_get(ctx);
  358. n6 = BN_CTX_get(ctx);
  359. if (n6 == NULL) {
  360. goto end;
  361. }
  362. /* Note that in this function we must not read components of 'a' or 'b'
  363. * once we have written the corresponding components of 'r'.
  364. * ('r' might be one of 'a' or 'b'.)
  365. */
  366. /* n1, n2 */
  367. int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
  368. if (b_Z_is_one) {
  369. if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
  370. goto end;
  371. }
  372. /* n1 = X_a */
  373. /* n2 = Y_a */
  374. } else {
  375. if (!field_sqr(group, n0, &b->Z, ctx) ||
  376. !field_mul(group, n1, &a->X, n0, ctx)) {
  377. goto end;
  378. }
  379. /* n1 = X_a * Z_b^2 */
  380. if (!field_mul(group, n0, n0, &b->Z, ctx) ||
  381. !field_mul(group, n2, &a->Y, n0, ctx)) {
  382. goto end;
  383. }
  384. /* n2 = Y_a * Z_b^3 */
  385. }
  386. /* n3, n4 */
  387. int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
  388. if (a_Z_is_one) {
  389. if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
  390. goto end;
  391. }
  392. /* n3 = X_b */
  393. /* n4 = Y_b */
  394. } else {
  395. if (!field_sqr(group, n0, &a->Z, ctx) ||
  396. !field_mul(group, n3, &b->X, n0, ctx)) {
  397. goto end;
  398. }
  399. /* n3 = X_b * Z_a^2 */
  400. if (!field_mul(group, n0, n0, &a->Z, ctx) ||
  401. !field_mul(group, n4, &b->Y, n0, ctx)) {
  402. goto end;
  403. }
  404. /* n4 = Y_b * Z_a^3 */
  405. }
  406. /* n5, n6 */
  407. if (!BN_mod_sub_quick(n5, n1, n3, p) ||
  408. !BN_mod_sub_quick(n6, n2, n4, p)) {
  409. goto end;
  410. }
  411. /* n5 = n1 - n3 */
  412. /* n6 = n2 - n4 */
  413. if (BN_is_zero(n5)) {
  414. if (BN_is_zero(n6)) {
  415. /* a is the same point as b */
  416. BN_CTX_end(ctx);
  417. ret = EC_POINT_dbl(group, r, a, ctx);
  418. ctx = NULL;
  419. goto end;
  420. } else {
  421. /* a is the inverse of b */
  422. BN_zero(&r->Z);
  423. ret = 1;
  424. goto end;
  425. }
  426. }
  427. /* 'n7', 'n8' */
  428. if (!BN_mod_add_quick(n1, n1, n3, p) ||
  429. !BN_mod_add_quick(n2, n2, n4, p)) {
  430. goto end;
  431. }
  432. /* 'n7' = n1 + n3 */
  433. /* 'n8' = n2 + n4 */
  434. /* Z_r */
  435. if (a_Z_is_one && b_Z_is_one) {
  436. if (!BN_copy(&r->Z, n5)) {
  437. goto end;
  438. }
  439. } else {
  440. if (a_Z_is_one) {
  441. if (!BN_copy(n0, &b->Z)) {
  442. goto end;
  443. }
  444. } else if (b_Z_is_one) {
  445. if (!BN_copy(n0, &a->Z)) {
  446. goto end;
  447. }
  448. } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
  449. goto end;
  450. }
  451. if (!field_mul(group, &r->Z, n0, n5, ctx)) {
  452. goto end;
  453. }
  454. }
  455. /* Z_r = Z_a * Z_b * n5 */
  456. /* X_r */
  457. if (!field_sqr(group, n0, n6, ctx) ||
  458. !field_sqr(group, n4, n5, ctx) ||
  459. !field_mul(group, n3, n1, n4, ctx) ||
  460. !BN_mod_sub_quick(&r->X, n0, n3, p)) {
  461. goto end;
  462. }
  463. /* X_r = n6^2 - n5^2 * 'n7' */
  464. /* 'n9' */
  465. if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
  466. !BN_mod_sub_quick(n0, n3, n0, p)) {
  467. goto end;
  468. }
  469. /* n9 = n5^2 * 'n7' - 2 * X_r */
  470. /* Y_r */
  471. if (!field_mul(group, n0, n0, n6, ctx) ||
  472. !field_mul(group, n5, n4, n5, ctx)) {
  473. goto end; /* now n5 is n5^3 */
  474. }
  475. if (!field_mul(group, n1, n2, n5, ctx) ||
  476. !BN_mod_sub_quick(n0, n0, n1, p)) {
  477. goto end;
  478. }
  479. if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
  480. goto end;
  481. }
  482. /* now 0 <= n0 < 2*p, and n0 is even */
  483. if (!BN_rshift1(&r->Y, n0)) {
  484. goto end;
  485. }
  486. /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  487. ret = 1;
  488. end:
  489. if (ctx) {
  490. /* otherwise we already called BN_CTX_end */
  491. BN_CTX_end(ctx);
  492. }
  493. BN_CTX_free(new_ctx);
  494. return ret;
  495. }
  496. int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  497. BN_CTX *ctx) {
  498. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  499. BN_CTX *);
  500. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  501. const BIGNUM *p;
  502. BN_CTX *new_ctx = NULL;
  503. BIGNUM *n0, *n1, *n2, *n3;
  504. int ret = 0;
  505. if (EC_POINT_is_at_infinity(group, a)) {
  506. BN_zero(&r->Z);
  507. return 1;
  508. }
  509. field_mul = group->meth->field_mul;
  510. field_sqr = group->meth->field_sqr;
  511. p = &group->field;
  512. if (ctx == NULL) {
  513. ctx = new_ctx = BN_CTX_new();
  514. if (ctx == NULL) {
  515. return 0;
  516. }
  517. }
  518. BN_CTX_start(ctx);
  519. n0 = BN_CTX_get(ctx);
  520. n1 = BN_CTX_get(ctx);
  521. n2 = BN_CTX_get(ctx);
  522. n3 = BN_CTX_get(ctx);
  523. if (n3 == NULL) {
  524. goto err;
  525. }
  526. /* Note that in this function we must not read components of 'a'
  527. * once we have written the corresponding components of 'r'.
  528. * ('r' might the same as 'a'.)
  529. */
  530. /* n1 */
  531. if (BN_cmp(&a->Z, &group->one) == 0) {
  532. if (!field_sqr(group, n0, &a->X, ctx) ||
  533. !BN_mod_lshift1_quick(n1, n0, p) ||
  534. !BN_mod_add_quick(n0, n0, n1, p) ||
  535. !BN_mod_add_quick(n1, n0, &group->a, p)) {
  536. goto err;
  537. }
  538. /* n1 = 3 * X_a^2 + a_curve */
  539. } else if (group->a_is_minus3) {
  540. if (!field_sqr(group, n1, &a->Z, ctx) ||
  541. !BN_mod_add_quick(n0, &a->X, n1, p) ||
  542. !BN_mod_sub_quick(n2, &a->X, n1, p) ||
  543. !field_mul(group, n1, n0, n2, ctx) ||
  544. !BN_mod_lshift1_quick(n0, n1, p) ||
  545. !BN_mod_add_quick(n1, n0, n1, p)) {
  546. goto err;
  547. }
  548. /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
  549. * = 3 * X_a^2 - 3 * Z_a^4 */
  550. } else {
  551. if (!field_sqr(group, n0, &a->X, ctx) ||
  552. !BN_mod_lshift1_quick(n1, n0, p) ||
  553. !BN_mod_add_quick(n0, n0, n1, p) ||
  554. !field_sqr(group, n1, &a->Z, ctx) ||
  555. !field_sqr(group, n1, n1, ctx) ||
  556. !field_mul(group, n1, n1, &group->a, ctx) ||
  557. !BN_mod_add_quick(n1, n1, n0, p)) {
  558. goto err;
  559. }
  560. /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
  561. }
  562. /* Z_r */
  563. if (BN_cmp(&a->Z, &group->one) == 0) {
  564. if (!BN_copy(n0, &a->Y)) {
  565. goto err;
  566. }
  567. } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
  568. goto err;
  569. }
  570. if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
  571. goto err;
  572. }
  573. /* Z_r = 2 * Y_a * Z_a */
  574. /* n2 */
  575. if (!field_sqr(group, n3, &a->Y, ctx) ||
  576. !field_mul(group, n2, &a->X, n3, ctx) ||
  577. !BN_mod_lshift_quick(n2, n2, 2, p)) {
  578. goto err;
  579. }
  580. /* n2 = 4 * X_a * Y_a^2 */
  581. /* X_r */
  582. if (!BN_mod_lshift1_quick(n0, n2, p) ||
  583. !field_sqr(group, &r->X, n1, ctx) ||
  584. !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
  585. goto err;
  586. }
  587. /* X_r = n1^2 - 2 * n2 */
  588. /* n3 */
  589. if (!field_sqr(group, n0, n3, ctx) ||
  590. !BN_mod_lshift_quick(n3, n0, 3, p)) {
  591. goto err;
  592. }
  593. /* n3 = 8 * Y_a^4 */
  594. /* Y_r */
  595. if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
  596. !field_mul(group, n0, n1, n0, ctx) ||
  597. !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
  598. goto err;
  599. }
  600. /* Y_r = n1 * (n2 - X_r) - n3 */
  601. ret = 1;
  602. err:
  603. BN_CTX_end(ctx);
  604. BN_CTX_free(new_ctx);
  605. return ret;
  606. }
  607. int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
  608. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
  609. /* point is its own inverse */
  610. return 1;
  611. }
  612. return BN_usub(&point->Y, &group->field, &point->Y);
  613. }
  614. int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
  615. return BN_is_zero(&point->Z);
  616. }
  617. int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  618. BN_CTX *ctx) {
  619. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  620. BN_CTX *);
  621. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  622. const BIGNUM *p;
  623. BN_CTX *new_ctx = NULL;
  624. BIGNUM *rh, *tmp, *Z4, *Z6;
  625. int ret = 0;
  626. if (EC_POINT_is_at_infinity(group, point)) {
  627. return 1;
  628. }
  629. field_mul = group->meth->field_mul;
  630. field_sqr = group->meth->field_sqr;
  631. p = &group->field;
  632. if (ctx == NULL) {
  633. ctx = new_ctx = BN_CTX_new();
  634. if (ctx == NULL) {
  635. return 0;
  636. }
  637. }
  638. BN_CTX_start(ctx);
  639. rh = BN_CTX_get(ctx);
  640. tmp = BN_CTX_get(ctx);
  641. Z4 = BN_CTX_get(ctx);
  642. Z6 = BN_CTX_get(ctx);
  643. if (Z6 == NULL) {
  644. goto err;
  645. }
  646. /* We have a curve defined by a Weierstrass equation
  647. * y^2 = x^3 + a*x + b.
  648. * The point to consider is given in Jacobian projective coordinates
  649. * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
  650. * Substituting this and multiplying by Z^6 transforms the above equation
  651. * into
  652. * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
  653. * To test this, we add up the right-hand side in 'rh'.
  654. */
  655. /* rh := X^2 */
  656. if (!field_sqr(group, rh, &point->X, ctx)) {
  657. goto err;
  658. }
  659. if (BN_cmp(&point->Z, &group->one) != 0) {
  660. if (!field_sqr(group, tmp, &point->Z, ctx) ||
  661. !field_sqr(group, Z4, tmp, ctx) ||
  662. !field_mul(group, Z6, Z4, tmp, ctx)) {
  663. goto err;
  664. }
  665. /* rh := (rh + a*Z^4)*X */
  666. if (group->a_is_minus3) {
  667. if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
  668. !BN_mod_add_quick(tmp, tmp, Z4, p) ||
  669. !BN_mod_sub_quick(rh, rh, tmp, p) ||
  670. !field_mul(group, rh, rh, &point->X, ctx)) {
  671. goto err;
  672. }
  673. } else {
  674. if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
  675. !BN_mod_add_quick(rh, rh, tmp, p) ||
  676. !field_mul(group, rh, rh, &point->X, ctx)) {
  677. goto err;
  678. }
  679. }
  680. /* rh := rh + b*Z^6 */
  681. if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
  682. !BN_mod_add_quick(rh, rh, tmp, p)) {
  683. goto err;
  684. }
  685. } else {
  686. /* rh := (rh + a)*X */
  687. if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
  688. !field_mul(group, rh, rh, &point->X, ctx)) {
  689. goto err;
  690. }
  691. /* rh := rh + b */
  692. if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
  693. goto err;
  694. }
  695. }
  696. /* 'lh' := Y^2 */
  697. if (!field_sqr(group, tmp, &point->Y, ctx)) {
  698. goto err;
  699. }
  700. ret = (0 == BN_ucmp(tmp, rh));
  701. err:
  702. BN_CTX_end(ctx);
  703. BN_CTX_free(new_ctx);
  704. return ret;
  705. }
  706. int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  707. const EC_POINT *b, BN_CTX *ctx) {
  708. /* return values:
  709. * -1 error
  710. * 0 equal (in affine coordinates)
  711. * 1 not equal
  712. */
  713. int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  714. BN_CTX *);
  715. int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  716. BN_CTX *new_ctx = NULL;
  717. BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
  718. const BIGNUM *tmp1_, *tmp2_;
  719. int ret = -1;
  720. if (EC_POINT_is_at_infinity(group, a)) {
  721. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  722. }
  723. if (EC_POINT_is_at_infinity(group, b)) {
  724. return 1;
  725. }
  726. int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
  727. int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
  728. if (a_Z_is_one && b_Z_is_one) {
  729. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  730. }
  731. field_mul = group->meth->field_mul;
  732. field_sqr = group->meth->field_sqr;
  733. if (ctx == NULL) {
  734. ctx = new_ctx = BN_CTX_new();
  735. if (ctx == NULL) {
  736. return -1;
  737. }
  738. }
  739. BN_CTX_start(ctx);
  740. tmp1 = BN_CTX_get(ctx);
  741. tmp2 = BN_CTX_get(ctx);
  742. Za23 = BN_CTX_get(ctx);
  743. Zb23 = BN_CTX_get(ctx);
  744. if (Zb23 == NULL) {
  745. goto end;
  746. }
  747. /* We have to decide whether
  748. * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
  749. * or equivalently, whether
  750. * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
  751. */
  752. if (!b_Z_is_one) {
  753. if (!field_sqr(group, Zb23, &b->Z, ctx) ||
  754. !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
  755. goto end;
  756. }
  757. tmp1_ = tmp1;
  758. } else {
  759. tmp1_ = &a->X;
  760. }
  761. if (!a_Z_is_one) {
  762. if (!field_sqr(group, Za23, &a->Z, ctx) ||
  763. !field_mul(group, tmp2, &b->X, Za23, ctx)) {
  764. goto end;
  765. }
  766. tmp2_ = tmp2;
  767. } else {
  768. tmp2_ = &b->X;
  769. }
  770. /* compare X_a*Z_b^2 with X_b*Z_a^2 */
  771. if (BN_cmp(tmp1_, tmp2_) != 0) {
  772. ret = 1; /* points differ */
  773. goto end;
  774. }
  775. if (!b_Z_is_one) {
  776. if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
  777. !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
  778. goto end;
  779. }
  780. /* tmp1_ = tmp1 */
  781. } else {
  782. tmp1_ = &a->Y;
  783. }
  784. if (!a_Z_is_one) {
  785. if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
  786. !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
  787. goto end;
  788. }
  789. /* tmp2_ = tmp2 */
  790. } else {
  791. tmp2_ = &b->Y;
  792. }
  793. /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
  794. if (BN_cmp(tmp1_, tmp2_) != 0) {
  795. ret = 1; /* points differ */
  796. goto end;
  797. }
  798. /* points are equal */
  799. ret = 0;
  800. end:
  801. BN_CTX_end(ctx);
  802. BN_CTX_free(new_ctx);
  803. return ret;
  804. }
  805. int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  806. BN_CTX *ctx) {
  807. BN_CTX *new_ctx = NULL;
  808. BIGNUM *x, *y;
  809. int ret = 0;
  810. if (BN_cmp(&point->Z, &group->one) == 0 ||
  811. EC_POINT_is_at_infinity(group, point)) {
  812. return 1;
  813. }
  814. if (ctx == NULL) {
  815. ctx = new_ctx = BN_CTX_new();
  816. if (ctx == NULL) {
  817. return 0;
  818. }
  819. }
  820. BN_CTX_start(ctx);
  821. x = BN_CTX_get(ctx);
  822. y = BN_CTX_get(ctx);
  823. if (y == NULL) {
  824. goto err;
  825. }
  826. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) ||
  827. !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  828. goto err;
  829. }
  830. if (BN_cmp(&point->Z, &group->one) != 0) {
  831. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  832. goto err;
  833. }
  834. ret = 1;
  835. err:
  836. BN_CTX_end(ctx);
  837. BN_CTX_free(new_ctx);
  838. return ret;
  839. }
  840. int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
  841. EC_POINT *points[], BN_CTX *ctx) {
  842. BN_CTX *new_ctx = NULL;
  843. BIGNUM *tmp, *tmp_Z;
  844. BIGNUM **prod_Z = NULL;
  845. int ret = 0;
  846. if (num == 0) {
  847. return 1;
  848. }
  849. if (ctx == NULL) {
  850. ctx = new_ctx = BN_CTX_new();
  851. if (ctx == NULL) {
  852. return 0;
  853. }
  854. }
  855. BN_CTX_start(ctx);
  856. tmp = BN_CTX_get(ctx);
  857. tmp_Z = BN_CTX_get(ctx);
  858. if (tmp == NULL || tmp_Z == NULL) {
  859. goto err;
  860. }
  861. prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
  862. if (prod_Z == NULL) {
  863. goto err;
  864. }
  865. OPENSSL_memset(prod_Z, 0, num * sizeof(prod_Z[0]));
  866. for (size_t i = 0; i < num; i++) {
  867. prod_Z[i] = BN_new();
  868. if (prod_Z[i] == NULL) {
  869. goto err;
  870. }
  871. }
  872. /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
  873. * skipping any zero-valued inputs (pretend that they're 1). */
  874. if (!BN_is_zero(&points[0]->Z)) {
  875. if (!BN_copy(prod_Z[0], &points[0]->Z)) {
  876. goto err;
  877. }
  878. } else {
  879. if (BN_copy(prod_Z[0], &group->one) == NULL) {
  880. goto err;
  881. }
  882. }
  883. for (size_t i = 1; i < num; i++) {
  884. if (!BN_is_zero(&points[i]->Z)) {
  885. if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
  886. &points[i]->Z, ctx)) {
  887. goto err;
  888. }
  889. } else {
  890. if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
  891. goto err;
  892. }
  893. }
  894. }
  895. /* Now use a single explicit inversion to replace every non-zero points[i]->Z
  896. * by its inverse. We use |BN_mod_inverse_odd| instead of doing a constant-
  897. * time inversion using Fermat's Little Theorem because this function is
  898. * usually only used for converting multiples of a public key point to
  899. * affine, and a public key point isn't secret. If we were to use Fermat's
  900. * Little Theorem then the cost of the inversion would usually be so high
  901. * that converting the multiples to affine would be counterproductive. */
  902. int no_inverse;
  903. if (!BN_mod_inverse_odd(tmp, &no_inverse, prod_Z[num - 1], &group->field,
  904. ctx)) {
  905. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  906. goto err;
  907. }
  908. if (group->meth->field_encode != NULL) {
  909. /* In the Montgomery case, we just turned R*H (representing H)
  910. * into 1/(R*H), but we need R*(1/H) (representing 1/H);
  911. * i.e. we need to multiply by the Montgomery factor twice. */
  912. if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
  913. !group->meth->field_encode(group, tmp, tmp, ctx)) {
  914. goto err;
  915. }
  916. }
  917. for (size_t i = num - 1; i > 0; --i) {
  918. /* Loop invariant: tmp is the product of the inverses of
  919. * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
  920. if (BN_is_zero(&points[i]->Z)) {
  921. continue;
  922. }
  923. /* Set tmp_Z to the inverse of points[i]->Z (as product
  924. * of Z inverses 0 .. i, Z values 0 .. i - 1). */
  925. if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
  926. /* Update tmp to satisfy the loop invariant for i - 1. */
  927. !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
  928. /* Replace points[i]->Z by its inverse. */
  929. !BN_copy(&points[i]->Z, tmp_Z)) {
  930. goto err;
  931. }
  932. }
  933. /* Replace points[0]->Z by its inverse. */
  934. if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
  935. goto err;
  936. }
  937. /* Finally, fix up the X and Y coordinates for all points. */
  938. for (size_t i = 0; i < num; i++) {
  939. EC_POINT *p = points[i];
  940. if (!BN_is_zero(&p->Z)) {
  941. /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1). */
  942. if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
  943. !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
  944. !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
  945. !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
  946. goto err;
  947. }
  948. if (BN_copy(&p->Z, &group->one) == NULL) {
  949. goto err;
  950. }
  951. }
  952. }
  953. ret = 1;
  954. err:
  955. BN_CTX_end(ctx);
  956. BN_CTX_free(new_ctx);
  957. if (prod_Z != NULL) {
  958. for (size_t i = 0; i < num; i++) {
  959. if (prod_Z[i] == NULL) {
  960. break;
  961. }
  962. BN_clear_free(prod_Z[i]);
  963. }
  964. OPENSSL_free(prod_Z);
  965. }
  966. return ret;
  967. }
  968. int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  969. const BIGNUM *b, BN_CTX *ctx) {
  970. return BN_mod_mul(r, a, b, &group->field, ctx);
  971. }
  972. int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  973. BN_CTX *ctx) {
  974. return BN_mod_sqr(r, a, &group->field, ctx);
  975. }