ec2_smpl.c 27 KB

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