simple.c 27 KB

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