ec2_smpl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/err.h>
  11. #include "internal/bn_int.h"
  12. #include "ec_lcl.h"
  13. #ifndef OPENSSL_NO_EC2M
  14. /*
  15. * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
  16. * are handled by EC_GROUP_new.
  17. */
  18. int ec_GF2m_simple_group_init(EC_GROUP *group)
  19. {
  20. group->field = BN_new();
  21. group->a = BN_new();
  22. group->b = BN_new();
  23. if (group->field == NULL || group->a == NULL || group->b == NULL) {
  24. BN_free(group->field);
  25. BN_free(group->a);
  26. BN_free(group->b);
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. /*
  32. * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
  33. * handled by EC_GROUP_free.
  34. */
  35. void ec_GF2m_simple_group_finish(EC_GROUP *group)
  36. {
  37. BN_free(group->field);
  38. BN_free(group->a);
  39. BN_free(group->b);
  40. }
  41. /*
  42. * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
  43. * members are handled by EC_GROUP_clear_free.
  44. */
  45. void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
  46. {
  47. BN_clear_free(group->field);
  48. BN_clear_free(group->a);
  49. BN_clear_free(group->b);
  50. group->poly[0] = 0;
  51. group->poly[1] = 0;
  52. group->poly[2] = 0;
  53. group->poly[3] = 0;
  54. group->poly[4] = 0;
  55. group->poly[5] = -1;
  56. }
  57. /*
  58. * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
  59. * handled by EC_GROUP_copy.
  60. */
  61. int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  62. {
  63. if (!BN_copy(dest->field, src->field))
  64. return 0;
  65. if (!BN_copy(dest->a, src->a))
  66. return 0;
  67. if (!BN_copy(dest->b, src->b))
  68. return 0;
  69. dest->poly[0] = src->poly[0];
  70. dest->poly[1] = src->poly[1];
  71. dest->poly[2] = src->poly[2];
  72. dest->poly[3] = src->poly[3];
  73. dest->poly[4] = src->poly[4];
  74. dest->poly[5] = src->poly[5];
  75. if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
  76. NULL)
  77. return 0;
  78. if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
  79. NULL)
  80. return 0;
  81. bn_set_all_zero(dest->a);
  82. bn_set_all_zero(dest->b);
  83. return 1;
  84. }
  85. /* Set the curve parameters of an EC_GROUP structure. */
  86. int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
  87. const BIGNUM *p, const BIGNUM *a,
  88. const BIGNUM *b, BN_CTX *ctx)
  89. {
  90. int ret = 0, i;
  91. /* group->field */
  92. if (!BN_copy(group->field, p))
  93. goto err;
  94. i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
  95. if ((i != 5) && (i != 3)) {
  96. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
  97. goto err;
  98. }
  99. /* group->a */
  100. if (!BN_GF2m_mod_arr(group->a, a, group->poly))
  101. goto err;
  102. if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  103. == NULL)
  104. goto err;
  105. bn_set_all_zero(group->a);
  106. /* group->b */
  107. if (!BN_GF2m_mod_arr(group->b, b, group->poly))
  108. goto err;
  109. if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  110. == NULL)
  111. goto err;
  112. bn_set_all_zero(group->b);
  113. ret = 1;
  114. err:
  115. return ret;
  116. }
  117. /*
  118. * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
  119. * then there values will not be set but the method will return with success.
  120. */
  121. int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
  122. BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  123. {
  124. int ret = 0;
  125. if (p != NULL) {
  126. if (!BN_copy(p, group->field))
  127. return 0;
  128. }
  129. if (a != NULL) {
  130. if (!BN_copy(a, group->a))
  131. goto err;
  132. }
  133. if (b != NULL) {
  134. if (!BN_copy(b, group->b))
  135. goto err;
  136. }
  137. ret = 1;
  138. err:
  139. return ret;
  140. }
  141. /*
  142. * Gets the degree of the field. For a curve over GF(2^m) this is the value
  143. * m.
  144. */
  145. int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
  146. {
  147. return BN_num_bits(group->field) - 1;
  148. }
  149. /*
  150. * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
  151. * elliptic curve <=> b != 0 (mod p)
  152. */
  153. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
  154. BN_CTX *ctx)
  155. {
  156. int ret = 0;
  157. BIGNUM *b;
  158. BN_CTX *new_ctx = NULL;
  159. if (ctx == NULL) {
  160. ctx = new_ctx = BN_CTX_new();
  161. if (ctx == NULL) {
  162. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
  163. ERR_R_MALLOC_FAILURE);
  164. goto err;
  165. }
  166. }
  167. BN_CTX_start(ctx);
  168. b = BN_CTX_get(ctx);
  169. if (b == NULL)
  170. goto err;
  171. if (!BN_GF2m_mod_arr(b, group->b, group->poly))
  172. goto err;
  173. /*
  174. * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
  175. * curve <=> b != 0 (mod p)
  176. */
  177. if (BN_is_zero(b))
  178. goto err;
  179. ret = 1;
  180. err:
  181. BN_CTX_end(ctx);
  182. BN_CTX_free(new_ctx);
  183. return ret;
  184. }
  185. /* Initializes an EC_POINT. */
  186. int ec_GF2m_simple_point_init(EC_POINT *point)
  187. {
  188. point->X = BN_new();
  189. point->Y = BN_new();
  190. point->Z = BN_new();
  191. if (point->X == NULL || point->Y == NULL || point->Z == NULL) {
  192. BN_free(point->X);
  193. BN_free(point->Y);
  194. BN_free(point->Z);
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. /* Frees an EC_POINT. */
  200. void ec_GF2m_simple_point_finish(EC_POINT *point)
  201. {
  202. BN_free(point->X);
  203. BN_free(point->Y);
  204. BN_free(point->Z);
  205. }
  206. /* Clears and frees an EC_POINT. */
  207. void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
  208. {
  209. BN_clear_free(point->X);
  210. BN_clear_free(point->Y);
  211. BN_clear_free(point->Z);
  212. point->Z_is_one = 0;
  213. }
  214. /*
  215. * Copy the contents of one EC_POINT into another. Assumes dest is
  216. * initialized.
  217. */
  218. int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  219. {
  220. if (!BN_copy(dest->X, src->X))
  221. return 0;
  222. if (!BN_copy(dest->Y, src->Y))
  223. return 0;
  224. if (!BN_copy(dest->Z, src->Z))
  225. return 0;
  226. dest->Z_is_one = src->Z_is_one;
  227. dest->curve_name = src->curve_name;
  228. return 1;
  229. }
  230. /*
  231. * Set an EC_POINT to the point at infinity. A point at infinity is
  232. * represented by having Z=0.
  233. */
  234. int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
  235. EC_POINT *point)
  236. {
  237. point->Z_is_one = 0;
  238. BN_zero(point->Z);
  239. return 1;
  240. }
  241. /*
  242. * Set the coordinates of an EC_POINT using affine coordinates. Note that
  243. * the simple implementation only uses affine coordinates.
  244. */
  245. int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
  246. EC_POINT *point,
  247. const BIGNUM *x,
  248. const BIGNUM *y, BN_CTX *ctx)
  249. {
  250. int ret = 0;
  251. if (x == NULL || y == NULL) {
  252. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
  253. ERR_R_PASSED_NULL_PARAMETER);
  254. return 0;
  255. }
  256. if (!BN_copy(point->X, x))
  257. goto err;
  258. BN_set_negative(point->X, 0);
  259. if (!BN_copy(point->Y, y))
  260. goto err;
  261. BN_set_negative(point->Y, 0);
  262. if (!BN_copy(point->Z, BN_value_one()))
  263. goto err;
  264. BN_set_negative(point->Z, 0);
  265. point->Z_is_one = 1;
  266. ret = 1;
  267. err:
  268. return ret;
  269. }
  270. /*
  271. * Gets the affine coordinates of an EC_POINT. Note that the simple
  272. * implementation only uses affine coordinates.
  273. */
  274. int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
  275. const EC_POINT *point,
  276. BIGNUM *x, BIGNUM *y,
  277. BN_CTX *ctx)
  278. {
  279. int ret = 0;
  280. if (EC_POINT_is_at_infinity(group, point)) {
  281. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  282. EC_R_POINT_AT_INFINITY);
  283. return 0;
  284. }
  285. if (BN_cmp(point->Z, BN_value_one())) {
  286. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  287. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  288. return 0;
  289. }
  290. if (x != NULL) {
  291. if (!BN_copy(x, point->X))
  292. goto err;
  293. BN_set_negative(x, 0);
  294. }
  295. if (y != NULL) {
  296. if (!BN_copy(y, point->Y))
  297. goto err;
  298. BN_set_negative(y, 0);
  299. }
  300. ret = 1;
  301. err:
  302. return ret;
  303. }
  304. /*
  305. * Computes a + b and stores the result in r. r could be a or b, a could be
  306. * b. Uses algorithm A.10.2 of IEEE P1363.
  307. */
  308. int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  309. const EC_POINT *b, BN_CTX *ctx)
  310. {
  311. BN_CTX *new_ctx = NULL;
  312. BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
  313. int ret = 0;
  314. if (EC_POINT_is_at_infinity(group, a)) {
  315. if (!EC_POINT_copy(r, b))
  316. return 0;
  317. return 1;
  318. }
  319. if (EC_POINT_is_at_infinity(group, b)) {
  320. if (!EC_POINT_copy(r, a))
  321. return 0;
  322. return 1;
  323. }
  324. if (ctx == NULL) {
  325. ctx = new_ctx = BN_CTX_new();
  326. if (ctx == NULL)
  327. return 0;
  328. }
  329. BN_CTX_start(ctx);
  330. x0 = BN_CTX_get(ctx);
  331. y0 = BN_CTX_get(ctx);
  332. x1 = BN_CTX_get(ctx);
  333. y1 = BN_CTX_get(ctx);
  334. x2 = BN_CTX_get(ctx);
  335. y2 = BN_CTX_get(ctx);
  336. s = BN_CTX_get(ctx);
  337. t = BN_CTX_get(ctx);
  338. if (t == NULL)
  339. goto err;
  340. if (a->Z_is_one) {
  341. if (!BN_copy(x0, a->X))
  342. goto err;
  343. if (!BN_copy(y0, a->Y))
  344. goto err;
  345. } else {
  346. if (!EC_POINT_get_affine_coordinates(group, a, x0, y0, ctx))
  347. goto err;
  348. }
  349. if (b->Z_is_one) {
  350. if (!BN_copy(x1, b->X))
  351. goto err;
  352. if (!BN_copy(y1, b->Y))
  353. goto err;
  354. } else {
  355. if (!EC_POINT_get_affine_coordinates(group, b, x1, y1, ctx))
  356. goto err;
  357. }
  358. if (BN_GF2m_cmp(x0, x1)) {
  359. if (!BN_GF2m_add(t, x0, x1))
  360. goto err;
  361. if (!BN_GF2m_add(s, y0, y1))
  362. goto err;
  363. if (!group->meth->field_div(group, s, s, t, ctx))
  364. goto err;
  365. if (!group->meth->field_sqr(group, x2, s, ctx))
  366. goto err;
  367. if (!BN_GF2m_add(x2, x2, group->a))
  368. goto err;
  369. if (!BN_GF2m_add(x2, x2, s))
  370. goto err;
  371. if (!BN_GF2m_add(x2, x2, t))
  372. goto err;
  373. } else {
  374. if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
  375. if (!EC_POINT_set_to_infinity(group, r))
  376. goto err;
  377. ret = 1;
  378. goto err;
  379. }
  380. if (!group->meth->field_div(group, s, y1, x1, ctx))
  381. goto err;
  382. if (!BN_GF2m_add(s, s, x1))
  383. goto err;
  384. if (!group->meth->field_sqr(group, x2, s, ctx))
  385. goto err;
  386. if (!BN_GF2m_add(x2, x2, s))
  387. goto err;
  388. if (!BN_GF2m_add(x2, x2, group->a))
  389. goto err;
  390. }
  391. if (!BN_GF2m_add(y2, x1, x2))
  392. goto err;
  393. if (!group->meth->field_mul(group, y2, y2, s, ctx))
  394. goto err;
  395. if (!BN_GF2m_add(y2, y2, x2))
  396. goto err;
  397. if (!BN_GF2m_add(y2, y2, y1))
  398. goto err;
  399. if (!EC_POINT_set_affine_coordinates(group, r, x2, y2, ctx))
  400. goto err;
  401. ret = 1;
  402. err:
  403. BN_CTX_end(ctx);
  404. BN_CTX_free(new_ctx);
  405. return ret;
  406. }
  407. /*
  408. * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
  409. * A.10.2 of IEEE P1363.
  410. */
  411. int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  412. BN_CTX *ctx)
  413. {
  414. return ec_GF2m_simple_add(group, r, a, a, ctx);
  415. }
  416. int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  417. {
  418. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
  419. /* point is its own inverse */
  420. return 1;
  421. if (!EC_POINT_make_affine(group, point, ctx))
  422. return 0;
  423. return BN_GF2m_add(point->Y, point->X, point->Y);
  424. }
  425. /* Indicates whether the given point is the point at infinity. */
  426. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
  427. const EC_POINT *point)
  428. {
  429. return BN_is_zero(point->Z);
  430. }
  431. /*-
  432. * Determines whether the given EC_POINT is an actual point on the curve defined
  433. * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
  434. * y^2 + x*y = x^3 + a*x^2 + b.
  435. */
  436. int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  437. BN_CTX *ctx)
  438. {
  439. int ret = -1;
  440. BN_CTX *new_ctx = NULL;
  441. BIGNUM *lh, *y2;
  442. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  443. const BIGNUM *, BN_CTX *);
  444. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  445. if (EC_POINT_is_at_infinity(group, point))
  446. return 1;
  447. field_mul = group->meth->field_mul;
  448. field_sqr = group->meth->field_sqr;
  449. /* only support affine coordinates */
  450. if (!point->Z_is_one)
  451. return -1;
  452. if (ctx == NULL) {
  453. ctx = new_ctx = BN_CTX_new();
  454. if (ctx == NULL)
  455. return -1;
  456. }
  457. BN_CTX_start(ctx);
  458. y2 = BN_CTX_get(ctx);
  459. lh = BN_CTX_get(ctx);
  460. if (lh == NULL)
  461. goto err;
  462. /*-
  463. * We have a curve defined by a Weierstrass equation
  464. * y^2 + x*y = x^3 + a*x^2 + b.
  465. * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
  466. * <=> ((x + a) * x + y ) * x + b + y^2 = 0
  467. */
  468. if (!BN_GF2m_add(lh, point->X, group->a))
  469. goto err;
  470. if (!field_mul(group, lh, lh, point->X, ctx))
  471. goto err;
  472. if (!BN_GF2m_add(lh, lh, point->Y))
  473. goto err;
  474. if (!field_mul(group, lh, lh, point->X, ctx))
  475. goto err;
  476. if (!BN_GF2m_add(lh, lh, group->b))
  477. goto err;
  478. if (!field_sqr(group, y2, point->Y, ctx))
  479. goto err;
  480. if (!BN_GF2m_add(lh, lh, y2))
  481. goto err;
  482. ret = BN_is_zero(lh);
  483. err:
  484. BN_CTX_end(ctx);
  485. BN_CTX_free(new_ctx);
  486. return ret;
  487. }
  488. /*-
  489. * Indicates whether two points are equal.
  490. * Return values:
  491. * -1 error
  492. * 0 equal (in affine coordinates)
  493. * 1 not equal
  494. */
  495. int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  496. const EC_POINT *b, BN_CTX *ctx)
  497. {
  498. BIGNUM *aX, *aY, *bX, *bY;
  499. BN_CTX *new_ctx = NULL;
  500. int ret = -1;
  501. if (EC_POINT_is_at_infinity(group, a)) {
  502. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  503. }
  504. if (EC_POINT_is_at_infinity(group, b))
  505. return 1;
  506. if (a->Z_is_one && b->Z_is_one) {
  507. return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
  508. }
  509. if (ctx == NULL) {
  510. ctx = new_ctx = BN_CTX_new();
  511. if (ctx == NULL)
  512. return -1;
  513. }
  514. BN_CTX_start(ctx);
  515. aX = BN_CTX_get(ctx);
  516. aY = BN_CTX_get(ctx);
  517. bX = BN_CTX_get(ctx);
  518. bY = BN_CTX_get(ctx);
  519. if (bY == NULL)
  520. goto err;
  521. if (!EC_POINT_get_affine_coordinates(group, a, aX, aY, ctx))
  522. goto err;
  523. if (!EC_POINT_get_affine_coordinates(group, b, bX, bY, ctx))
  524. goto err;
  525. ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
  526. err:
  527. BN_CTX_end(ctx);
  528. BN_CTX_free(new_ctx);
  529. return ret;
  530. }
  531. /* Forces the given EC_POINT to internally use affine coordinates. */
  532. int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  533. BN_CTX *ctx)
  534. {
  535. BN_CTX *new_ctx = NULL;
  536. BIGNUM *x, *y;
  537. int ret = 0;
  538. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  539. return 1;
  540. if (ctx == NULL) {
  541. ctx = new_ctx = BN_CTX_new();
  542. if (ctx == NULL)
  543. return 0;
  544. }
  545. BN_CTX_start(ctx);
  546. x = BN_CTX_get(ctx);
  547. y = BN_CTX_get(ctx);
  548. if (y == NULL)
  549. goto err;
  550. if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
  551. goto err;
  552. if (!BN_copy(point->X, x))
  553. goto err;
  554. if (!BN_copy(point->Y, y))
  555. goto err;
  556. if (!BN_one(point->Z))
  557. goto err;
  558. point->Z_is_one = 1;
  559. ret = 1;
  560. err:
  561. BN_CTX_end(ctx);
  562. BN_CTX_free(new_ctx);
  563. return ret;
  564. }
  565. /*
  566. * Forces each of the EC_POINTs in the given array to use affine coordinates.
  567. */
  568. int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
  569. EC_POINT *points[], BN_CTX *ctx)
  570. {
  571. size_t i;
  572. for (i = 0; i < num; i++) {
  573. if (!group->meth->make_affine(group, points[i], ctx))
  574. return 0;
  575. }
  576. return 1;
  577. }
  578. /* Wrapper to simple binary polynomial field multiplication implementation. */
  579. int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
  580. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  581. {
  582. return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
  583. }
  584. /* Wrapper to simple binary polynomial field squaring implementation. */
  585. int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
  586. const BIGNUM *a, BN_CTX *ctx)
  587. {
  588. return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
  589. }
  590. /* Wrapper to simple binary polynomial field division implementation. */
  591. int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
  592. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  593. {
  594. return BN_GF2m_mod_div(r, a, b, group->field, ctx);
  595. }
  596. /*-
  597. * Lopez-Dahab ladder, pre step.
  598. * See e.g. "Guide to ECC" Alg 3.40.
  599. * Modified to blind s and r independently.
  600. * s:= p, r := 2p
  601. */
  602. static
  603. int ec_GF2m_simple_ladder_pre(const EC_GROUP *group,
  604. EC_POINT *r, EC_POINT *s,
  605. EC_POINT *p, BN_CTX *ctx)
  606. {
  607. /* if p is not affine, something is wrong */
  608. if (p->Z_is_one == 0)
  609. return 0;
  610. /* s blinding: make sure lambda (s->Z here) is not zero */
  611. do {
  612. if (!BN_priv_rand(s->Z, BN_num_bits(group->field) - 1,
  613. BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) {
  614. ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_PRE, ERR_R_BN_LIB);
  615. return 0;
  616. }
  617. } while (BN_is_zero(s->Z));
  618. /* if field_encode defined convert between representations */
  619. if ((group->meth->field_encode != NULL
  620. && !group->meth->field_encode(group, s->Z, s->Z, ctx))
  621. || !group->meth->field_mul(group, s->X, p->X, s->Z, ctx))
  622. return 0;
  623. /* r blinding: make sure lambda (r->Y here for storage) is not zero */
  624. do {
  625. if (!BN_priv_rand(r->Y, BN_num_bits(group->field) - 1,
  626. BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) {
  627. ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_PRE, ERR_R_BN_LIB);
  628. return 0;
  629. }
  630. } while (BN_is_zero(r->Y));
  631. if ((group->meth->field_encode != NULL
  632. && !group->meth->field_encode(group, r->Y, r->Y, ctx))
  633. || !group->meth->field_sqr(group, r->Z, p->X, ctx)
  634. || !group->meth->field_sqr(group, r->X, r->Z, ctx)
  635. || !BN_GF2m_add(r->X, r->X, group->b)
  636. || !group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx)
  637. || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx))
  638. return 0;
  639. s->Z_is_one = 0;
  640. r->Z_is_one = 0;
  641. return 1;
  642. }
  643. /*-
  644. * Ladder step: differential addition-and-doubling, mixed Lopez-Dahab coords.
  645. * http://www.hyperelliptic.org/EFD/g12o/auto-code/shortw/xz/ladder/mladd-2003-s.op3
  646. * s := r + s, r := 2r
  647. */
  648. static
  649. int ec_GF2m_simple_ladder_step(const EC_GROUP *group,
  650. EC_POINT *r, EC_POINT *s,
  651. EC_POINT *p, BN_CTX *ctx)
  652. {
  653. if (!group->meth->field_mul(group, r->Y, r->Z, s->X, ctx)
  654. || !group->meth->field_mul(group, s->X, r->X, s->Z, ctx)
  655. || !group->meth->field_sqr(group, s->Y, r->Z, ctx)
  656. || !group->meth->field_sqr(group, r->Z, r->X, ctx)
  657. || !BN_GF2m_add(s->Z, r->Y, s->X)
  658. || !group->meth->field_sqr(group, s->Z, s->Z, ctx)
  659. || !group->meth->field_mul(group, s->X, r->Y, s->X, ctx)
  660. || !group->meth->field_mul(group, r->Y, s->Z, p->X, ctx)
  661. || !BN_GF2m_add(s->X, s->X, r->Y)
  662. || !group->meth->field_sqr(group, r->Y, r->Z, ctx)
  663. || !group->meth->field_mul(group, r->Z, r->Z, s->Y, ctx)
  664. || !group->meth->field_sqr(group, s->Y, s->Y, ctx)
  665. || !group->meth->field_mul(group, s->Y, s->Y, group->b, ctx)
  666. || !BN_GF2m_add(r->X, r->Y, s->Y))
  667. return 0;
  668. return 1;
  669. }
  670. /*-
  671. * Recover affine (x,y) result from Lopez-Dahab r and s, affine p.
  672. * See e.g. "Fast Multiplication on Elliptic Curves over GF(2**m)
  673. * without Precomputation" (Lopez and Dahab, CHES 1999),
  674. * Appendix Alg Mxy.
  675. */
  676. static
  677. int ec_GF2m_simple_ladder_post(const EC_GROUP *group,
  678. EC_POINT *r, EC_POINT *s,
  679. EC_POINT *p, BN_CTX *ctx)
  680. {
  681. int ret = 0;
  682. BIGNUM *t0, *t1, *t2 = NULL;
  683. if (BN_is_zero(r->Z))
  684. return EC_POINT_set_to_infinity(group, r);
  685. if (BN_is_zero(s->Z)) {
  686. if (!EC_POINT_copy(r, p)
  687. || !EC_POINT_invert(group, r, ctx)) {
  688. ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_POST, ERR_R_EC_LIB);
  689. return 0;
  690. }
  691. return 1;
  692. }
  693. BN_CTX_start(ctx);
  694. t0 = BN_CTX_get(ctx);
  695. t1 = BN_CTX_get(ctx);
  696. t2 = BN_CTX_get(ctx);
  697. if (t2 == NULL) {
  698. ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_POST, ERR_R_MALLOC_FAILURE);
  699. goto err;
  700. }
  701. if (!group->meth->field_mul(group, t0, r->Z, s->Z, ctx)
  702. || !group->meth->field_mul(group, t1, p->X, r->Z, ctx)
  703. || !BN_GF2m_add(t1, r->X, t1)
  704. || !group->meth->field_mul(group, t2, p->X, s->Z, ctx)
  705. || !group->meth->field_mul(group, r->Z, r->X, t2, ctx)
  706. || !BN_GF2m_add(t2, t2, s->X)
  707. || !group->meth->field_mul(group, t1, t1, t2, ctx)
  708. || !group->meth->field_sqr(group, t2, p->X, ctx)
  709. || !BN_GF2m_add(t2, p->Y, t2)
  710. || !group->meth->field_mul(group, t2, t2, t0, ctx)
  711. || !BN_GF2m_add(t1, t2, t1)
  712. || !group->meth->field_mul(group, t2, p->X, t0, ctx)
  713. || !group->meth->field_inv(group, t2, t2, ctx)
  714. || !group->meth->field_mul(group, t1, t1, t2, ctx)
  715. || !group->meth->field_mul(group, r->X, r->Z, t2, ctx)
  716. || !BN_GF2m_add(t2, p->X, r->X)
  717. || !group->meth->field_mul(group, t2, t2, t1, ctx)
  718. || !BN_GF2m_add(r->Y, p->Y, t2)
  719. || !BN_one(r->Z))
  720. goto err;
  721. r->Z_is_one = 1;
  722. /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
  723. BN_set_negative(r->X, 0);
  724. BN_set_negative(r->Y, 0);
  725. ret = 1;
  726. err:
  727. BN_CTX_end(ctx);
  728. return ret;
  729. }
  730. static
  731. int ec_GF2m_simple_points_mul(const EC_GROUP *group, EC_POINT *r,
  732. const BIGNUM *scalar, size_t num,
  733. const EC_POINT *points[],
  734. const BIGNUM *scalars[],
  735. BN_CTX *ctx)
  736. {
  737. int ret = 0;
  738. EC_POINT *t = NULL;
  739. /*-
  740. * We limit use of the ladder only to the following cases:
  741. * - r := scalar * G
  742. * Fixed point mul: scalar != NULL && num == 0;
  743. * - r := scalars[0] * points[0]
  744. * Variable point mul: scalar == NULL && num == 1;
  745. * - r := scalar * G + scalars[0] * points[0]
  746. * used, e.g., in ECDSA verification: scalar != NULL && num == 1
  747. *
  748. * In any other case (num > 1) we use the default wNAF implementation.
  749. *
  750. * We also let the default implementation handle degenerate cases like group
  751. * order or cofactor set to 0.
  752. */
  753. if (num > 1 || BN_is_zero(group->order) || BN_is_zero(group->cofactor))
  754. return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  755. if (scalar != NULL && num == 0)
  756. /* Fixed point multiplication */
  757. return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
  758. if (scalar == NULL && num == 1)
  759. /* Variable point multiplication */
  760. return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);
  761. /*-
  762. * Double point multiplication:
  763. * r := scalar * G + scalars[0] * points[0]
  764. */
  765. if ((t = EC_POINT_new(group)) == NULL) {
  766. ECerr(EC_F_EC_GF2M_SIMPLE_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  767. return 0;
  768. }
  769. if (!ec_scalar_mul_ladder(group, t, scalar, NULL, ctx)
  770. || !ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx)
  771. || !EC_POINT_add(group, r, t, r, ctx))
  772. goto err;
  773. ret = 1;
  774. err:
  775. EC_POINT_free(t);
  776. return ret;
  777. }
  778. /*-
  779. * Computes the multiplicative inverse of a in GF(2^m), storing the result in r.
  780. * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
  781. * SCA hardening is with blinding: BN_GF2m_mod_inv does that.
  782. */
  783. static int ec_GF2m_simple_field_inv(const EC_GROUP *group, BIGNUM *r,
  784. const BIGNUM *a, BN_CTX *ctx)
  785. {
  786. int ret;
  787. if (!(ret = BN_GF2m_mod_inv(r, a, group->field, ctx)))
  788. ECerr(EC_F_EC_GF2M_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
  789. return ret;
  790. }
  791. const EC_METHOD *EC_GF2m_simple_method(void)
  792. {
  793. static const EC_METHOD ret = {
  794. EC_FLAGS_DEFAULT_OCT,
  795. NID_X9_62_characteristic_two_field,
  796. ec_GF2m_simple_group_init,
  797. ec_GF2m_simple_group_finish,
  798. ec_GF2m_simple_group_clear_finish,
  799. ec_GF2m_simple_group_copy,
  800. ec_GF2m_simple_group_set_curve,
  801. ec_GF2m_simple_group_get_curve,
  802. ec_GF2m_simple_group_get_degree,
  803. ec_group_simple_order_bits,
  804. ec_GF2m_simple_group_check_discriminant,
  805. ec_GF2m_simple_point_init,
  806. ec_GF2m_simple_point_finish,
  807. ec_GF2m_simple_point_clear_finish,
  808. ec_GF2m_simple_point_copy,
  809. ec_GF2m_simple_point_set_to_infinity,
  810. 0, /* set_Jprojective_coordinates_GFp */
  811. 0, /* get_Jprojective_coordinates_GFp */
  812. ec_GF2m_simple_point_set_affine_coordinates,
  813. ec_GF2m_simple_point_get_affine_coordinates,
  814. 0, /* point_set_compressed_coordinates */
  815. 0, /* point2oct */
  816. 0, /* oct2point */
  817. ec_GF2m_simple_add,
  818. ec_GF2m_simple_dbl,
  819. ec_GF2m_simple_invert,
  820. ec_GF2m_simple_is_at_infinity,
  821. ec_GF2m_simple_is_on_curve,
  822. ec_GF2m_simple_cmp,
  823. ec_GF2m_simple_make_affine,
  824. ec_GF2m_simple_points_make_affine,
  825. ec_GF2m_simple_points_mul,
  826. 0, /* precompute_mult */
  827. 0, /* have_precompute_mult */
  828. ec_GF2m_simple_field_mul,
  829. ec_GF2m_simple_field_sqr,
  830. ec_GF2m_simple_field_div,
  831. ec_GF2m_simple_field_inv,
  832. 0, /* field_encode */
  833. 0, /* field_decode */
  834. 0, /* field_set_to_one */
  835. ec_key_simple_priv2oct,
  836. ec_key_simple_oct2priv,
  837. 0, /* set private */
  838. ec_key_simple_generate_key,
  839. ec_key_simple_check_key,
  840. ec_key_simple_generate_public_key,
  841. 0, /* keycopy */
  842. 0, /* keyfinish */
  843. ecdh_simple_compute_key,
  844. 0, /* field_inverse_mod_ord */
  845. 0, /* blind_coordinates */
  846. ec_GF2m_simple_ladder_pre,
  847. ec_GF2m_simple_ladder_step,
  848. ec_GF2m_simple_ladder_post
  849. };
  850. return &ret;
  851. }
  852. #endif