ec2_smpl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /* crypto/ec/ec2_smpl.c */
  2. /* ====================================================================
  3. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  4. *
  5. * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
  6. * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
  7. * to the OpenSSL project.
  8. *
  9. * The ECC Code is licensed pursuant to the OpenSSL open source
  10. * license provided below.
  11. *
  12. * The software is originally written by Sheueling Chang Shantz and
  13. * Douglas Stebila of Sun Microsystems Laboratories.
  14. *
  15. */
  16. /* ====================================================================
  17. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. *
  31. * 3. All advertising materials mentioning features or use of this
  32. * software must display the following acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  35. *
  36. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  37. * endorse or promote products derived from this software without
  38. * prior written permission. For written permission, please contact
  39. * openssl-core@openssl.org.
  40. *
  41. * 5. Products derived from this software may not be called "OpenSSL"
  42. * nor may "OpenSSL" appear in their names without prior written
  43. * permission of the OpenSSL Project.
  44. *
  45. * 6. Redistributions of any form whatsoever must retain the following
  46. * acknowledgment:
  47. * "This product includes software developed by the OpenSSL Project
  48. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  51. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  54. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  56. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  57. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  58. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  59. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  60. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  61. * OF THE POSSIBILITY OF SUCH DAMAGE.
  62. * ====================================================================
  63. *
  64. * This product includes cryptographic software written by Eric Young
  65. * (eay@cryptsoft.com). This product includes software written by Tim
  66. * Hudson (tjh@cryptsoft.com).
  67. *
  68. */
  69. #include <openssl/err.h>
  70. #include "internal/bn_int.h"
  71. #include "ec_lcl.h"
  72. #ifndef OPENSSL_NO_EC2M
  73. const EC_METHOD *EC_GF2m_simple_method(void)
  74. {
  75. static const EC_METHOD ret = {
  76. EC_FLAGS_DEFAULT_OCT,
  77. NID_X9_62_characteristic_two_field,
  78. ec_GF2m_simple_group_init,
  79. ec_GF2m_simple_group_finish,
  80. ec_GF2m_simple_group_clear_finish,
  81. ec_GF2m_simple_group_copy,
  82. ec_GF2m_simple_group_set_curve,
  83. ec_GF2m_simple_group_get_curve,
  84. ec_GF2m_simple_group_get_degree,
  85. ec_GF2m_simple_group_check_discriminant,
  86. ec_GF2m_simple_point_init,
  87. ec_GF2m_simple_point_finish,
  88. ec_GF2m_simple_point_clear_finish,
  89. ec_GF2m_simple_point_copy,
  90. ec_GF2m_simple_point_set_to_infinity,
  91. 0 /* set_Jprojective_coordinates_GFp */ ,
  92. 0 /* get_Jprojective_coordinates_GFp */ ,
  93. ec_GF2m_simple_point_set_affine_coordinates,
  94. ec_GF2m_simple_point_get_affine_coordinates,
  95. 0, 0, 0,
  96. ec_GF2m_simple_add,
  97. ec_GF2m_simple_dbl,
  98. ec_GF2m_simple_invert,
  99. ec_GF2m_simple_is_at_infinity,
  100. ec_GF2m_simple_is_on_curve,
  101. ec_GF2m_simple_cmp,
  102. ec_GF2m_simple_make_affine,
  103. ec_GF2m_simple_points_make_affine,
  104. /*
  105. * the following three method functions are defined in ec2_mult.c
  106. */
  107. ec_GF2m_simple_mul,
  108. ec_GF2m_precompute_mult,
  109. ec_GF2m_have_precompute_mult,
  110. ec_GF2m_simple_field_mul,
  111. ec_GF2m_simple_field_sqr,
  112. ec_GF2m_simple_field_div,
  113. 0 /* field_encode */ ,
  114. 0 /* field_decode */ ,
  115. 0 /* field_set_to_one */
  116. };
  117. return &ret;
  118. }
  119. /*
  120. * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
  121. * are handled by EC_GROUP_new.
  122. */
  123. int ec_GF2m_simple_group_init(EC_GROUP *group)
  124. {
  125. group->field = BN_new();
  126. group->a = BN_new();
  127. group->b = BN_new();
  128. if (!group->field || !group->a || !group->b) {
  129. BN_free(group->field);
  130. BN_free(group->a);
  131. BN_free(group->b);
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. /*
  137. * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
  138. * handled by EC_GROUP_free.
  139. */
  140. void ec_GF2m_simple_group_finish(EC_GROUP *group)
  141. {
  142. BN_free(group->field);
  143. BN_free(group->a);
  144. BN_free(group->b);
  145. }
  146. /*
  147. * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
  148. * members are handled by EC_GROUP_clear_free.
  149. */
  150. void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
  151. {
  152. BN_clear_free(group->field);
  153. BN_clear_free(group->a);
  154. BN_clear_free(group->b);
  155. group->poly[0] = 0;
  156. group->poly[1] = 0;
  157. group->poly[2] = 0;
  158. group->poly[3] = 0;
  159. group->poly[4] = 0;
  160. group->poly[5] = -1;
  161. }
  162. /*
  163. * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
  164. * handled by EC_GROUP_copy.
  165. */
  166. int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  167. {
  168. if (!BN_copy(dest->field, src->field))
  169. return 0;
  170. if (!BN_copy(dest->a, src->a))
  171. return 0;
  172. if (!BN_copy(dest->b, src->b))
  173. return 0;
  174. dest->poly[0] = src->poly[0];
  175. dest->poly[1] = src->poly[1];
  176. dest->poly[2] = src->poly[2];
  177. dest->poly[3] = src->poly[3];
  178. dest->poly[4] = src->poly[4];
  179. dest->poly[5] = src->poly[5];
  180. if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
  181. NULL)
  182. return 0;
  183. if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) ==
  184. NULL)
  185. return 0;
  186. bn_set_all_zero(dest->a);
  187. bn_set_all_zero(dest->b);
  188. return 1;
  189. }
  190. /* Set the curve parameters of an EC_GROUP structure. */
  191. int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
  192. const BIGNUM *p, const BIGNUM *a,
  193. const BIGNUM *b, BN_CTX *ctx)
  194. {
  195. int ret = 0, i;
  196. /* group->field */
  197. if (!BN_copy(group->field, p))
  198. goto err;
  199. i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
  200. if ((i != 5) && (i != 3)) {
  201. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
  202. goto err;
  203. }
  204. /* group->a */
  205. if (!BN_GF2m_mod_arr(group->a, a, group->poly))
  206. goto err;
  207. if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  208. == NULL)
  209. goto err;
  210. bn_set_all_zero(group->a);
  211. /* group->b */
  212. if (!BN_GF2m_mod_arr(group->b, b, group->poly))
  213. goto err;
  214. if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  215. == NULL)
  216. goto err;
  217. bn_set_all_zero(group->b);
  218. ret = 1;
  219. err:
  220. return ret;
  221. }
  222. /*
  223. * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
  224. * then there values will not be set but the method will return with success.
  225. */
  226. int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
  227. BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  228. {
  229. int ret = 0;
  230. if (p != NULL) {
  231. if (!BN_copy(p, group->field))
  232. return 0;
  233. }
  234. if (a != NULL) {
  235. if (!BN_copy(a, group->a))
  236. goto err;
  237. }
  238. if (b != NULL) {
  239. if (!BN_copy(b, group->b))
  240. goto err;
  241. }
  242. ret = 1;
  243. err:
  244. return ret;
  245. }
  246. /*
  247. * Gets the degree of the field. For a curve over GF(2^m) this is the value
  248. * m.
  249. */
  250. int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
  251. {
  252. return BN_num_bits(group->field) - 1;
  253. }
  254. /*
  255. * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
  256. * elliptic curve <=> b != 0 (mod p)
  257. */
  258. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
  259. BN_CTX *ctx)
  260. {
  261. int ret = 0;
  262. BIGNUM *b;
  263. BN_CTX *new_ctx = NULL;
  264. if (ctx == NULL) {
  265. ctx = new_ctx = BN_CTX_new();
  266. if (ctx == NULL) {
  267. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
  268. ERR_R_MALLOC_FAILURE);
  269. goto err;
  270. }
  271. }
  272. BN_CTX_start(ctx);
  273. b = BN_CTX_get(ctx);
  274. if (b == NULL)
  275. goto err;
  276. if (!BN_GF2m_mod_arr(b, group->b, group->poly))
  277. goto err;
  278. /*
  279. * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
  280. * curve <=> b != 0 (mod p)
  281. */
  282. if (BN_is_zero(b))
  283. goto err;
  284. ret = 1;
  285. err:
  286. if (ctx != NULL)
  287. BN_CTX_end(ctx);
  288. BN_CTX_free(new_ctx);
  289. return ret;
  290. }
  291. /* Initializes an EC_POINT. */
  292. int ec_GF2m_simple_point_init(EC_POINT *point)
  293. {
  294. point->X = BN_new();
  295. point->Y = BN_new();
  296. point->Z = BN_new();
  297. if (!point->X || !point->Y || !point->Z) {
  298. BN_free(point->X);
  299. BN_free(point->Y);
  300. BN_free(point->Z);
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. /* Frees an EC_POINT. */
  306. void ec_GF2m_simple_point_finish(EC_POINT *point)
  307. {
  308. BN_free(point->X);
  309. BN_free(point->Y);
  310. BN_free(point->Z);
  311. }
  312. /* Clears and frees an EC_POINT. */
  313. void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
  314. {
  315. BN_clear_free(point->X);
  316. BN_clear_free(point->Y);
  317. BN_clear_free(point->Z);
  318. point->Z_is_one = 0;
  319. }
  320. /*
  321. * Copy the contents of one EC_POINT into another. Assumes dest is
  322. * initialized.
  323. */
  324. int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  325. {
  326. if (!BN_copy(dest->X, src->X))
  327. return 0;
  328. if (!BN_copy(dest->Y, src->Y))
  329. return 0;
  330. if (!BN_copy(dest->Z, src->Z))
  331. return 0;
  332. dest->Z_is_one = src->Z_is_one;
  333. return 1;
  334. }
  335. /*
  336. * Set an EC_POINT to the point at infinity. A point at infinity is
  337. * represented by having Z=0.
  338. */
  339. int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
  340. EC_POINT *point)
  341. {
  342. point->Z_is_one = 0;
  343. BN_zero(point->Z);
  344. return 1;
  345. }
  346. /*
  347. * Set the coordinates of an EC_POINT using affine coordinates. Note that
  348. * the simple implementation only uses affine coordinates.
  349. */
  350. int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
  351. EC_POINT *point,
  352. const BIGNUM *x,
  353. const BIGNUM *y, BN_CTX *ctx)
  354. {
  355. int ret = 0;
  356. if (x == NULL || y == NULL) {
  357. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
  358. ERR_R_PASSED_NULL_PARAMETER);
  359. return 0;
  360. }
  361. if (!BN_copy(point->X, x))
  362. goto err;
  363. BN_set_negative(point->X, 0);
  364. if (!BN_copy(point->Y, y))
  365. goto err;
  366. BN_set_negative(point->Y, 0);
  367. if (!BN_copy(point->Z, BN_value_one()))
  368. goto err;
  369. BN_set_negative(point->Z, 0);
  370. point->Z_is_one = 1;
  371. ret = 1;
  372. err:
  373. return ret;
  374. }
  375. /*
  376. * Gets the affine coordinates of an EC_POINT. Note that the simple
  377. * implementation only uses affine coordinates.
  378. */
  379. int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
  380. const EC_POINT *point,
  381. BIGNUM *x, BIGNUM *y,
  382. BN_CTX *ctx)
  383. {
  384. int ret = 0;
  385. if (EC_POINT_is_at_infinity(group, point)) {
  386. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  387. EC_R_POINT_AT_INFINITY);
  388. return 0;
  389. }
  390. if (BN_cmp(point->Z, BN_value_one())) {
  391. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  392. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  393. return 0;
  394. }
  395. if (x != NULL) {
  396. if (!BN_copy(x, point->X))
  397. goto err;
  398. BN_set_negative(x, 0);
  399. }
  400. if (y != NULL) {
  401. if (!BN_copy(y, point->Y))
  402. goto err;
  403. BN_set_negative(y, 0);
  404. }
  405. ret = 1;
  406. err:
  407. return ret;
  408. }
  409. /*
  410. * Computes a + b and stores the result in r. r could be a or b, a could be
  411. * b. Uses algorithm A.10.2 of IEEE P1363.
  412. */
  413. int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  414. const EC_POINT *b, BN_CTX *ctx)
  415. {
  416. BN_CTX *new_ctx = NULL;
  417. BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
  418. int ret = 0;
  419. if (EC_POINT_is_at_infinity(group, a)) {
  420. if (!EC_POINT_copy(r, b))
  421. return 0;
  422. return 1;
  423. }
  424. if (EC_POINT_is_at_infinity(group, b)) {
  425. if (!EC_POINT_copy(r, a))
  426. return 0;
  427. return 1;
  428. }
  429. if (ctx == NULL) {
  430. ctx = new_ctx = BN_CTX_new();
  431. if (ctx == NULL)
  432. return 0;
  433. }
  434. BN_CTX_start(ctx);
  435. x0 = BN_CTX_get(ctx);
  436. y0 = BN_CTX_get(ctx);
  437. x1 = BN_CTX_get(ctx);
  438. y1 = BN_CTX_get(ctx);
  439. x2 = BN_CTX_get(ctx);
  440. y2 = BN_CTX_get(ctx);
  441. s = BN_CTX_get(ctx);
  442. t = BN_CTX_get(ctx);
  443. if (t == NULL)
  444. goto err;
  445. if (a->Z_is_one) {
  446. if (!BN_copy(x0, a->X))
  447. goto err;
  448. if (!BN_copy(y0, a->Y))
  449. goto err;
  450. } else {
  451. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
  452. goto err;
  453. }
  454. if (b->Z_is_one) {
  455. if (!BN_copy(x1, b->X))
  456. goto err;
  457. if (!BN_copy(y1, b->Y))
  458. goto err;
  459. } else {
  460. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
  461. goto err;
  462. }
  463. if (BN_GF2m_cmp(x0, x1)) {
  464. if (!BN_GF2m_add(t, x0, x1))
  465. goto err;
  466. if (!BN_GF2m_add(s, y0, y1))
  467. goto err;
  468. if (!group->meth->field_div(group, s, s, t, ctx))
  469. goto err;
  470. if (!group->meth->field_sqr(group, x2, s, ctx))
  471. goto err;
  472. if (!BN_GF2m_add(x2, x2, group->a))
  473. goto err;
  474. if (!BN_GF2m_add(x2, x2, s))
  475. goto err;
  476. if (!BN_GF2m_add(x2, x2, t))
  477. goto err;
  478. } else {
  479. if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
  480. if (!EC_POINT_set_to_infinity(group, r))
  481. goto err;
  482. ret = 1;
  483. goto err;
  484. }
  485. if (!group->meth->field_div(group, s, y1, x1, ctx))
  486. goto err;
  487. if (!BN_GF2m_add(s, s, x1))
  488. goto err;
  489. if (!group->meth->field_sqr(group, x2, s, ctx))
  490. goto err;
  491. if (!BN_GF2m_add(x2, x2, s))
  492. goto err;
  493. if (!BN_GF2m_add(x2, x2, group->a))
  494. goto err;
  495. }
  496. if (!BN_GF2m_add(y2, x1, x2))
  497. goto err;
  498. if (!group->meth->field_mul(group, y2, y2, s, ctx))
  499. goto err;
  500. if (!BN_GF2m_add(y2, y2, x2))
  501. goto err;
  502. if (!BN_GF2m_add(y2, y2, y1))
  503. goto err;
  504. if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
  505. goto err;
  506. ret = 1;
  507. err:
  508. BN_CTX_end(ctx);
  509. BN_CTX_free(new_ctx);
  510. return ret;
  511. }
  512. /*
  513. * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
  514. * A.10.2 of IEEE P1363.
  515. */
  516. int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  517. BN_CTX *ctx)
  518. {
  519. return ec_GF2m_simple_add(group, r, a, a, ctx);
  520. }
  521. int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  522. {
  523. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
  524. /* point is its own inverse */
  525. return 1;
  526. if (!EC_POINT_make_affine(group, point, ctx))
  527. return 0;
  528. return BN_GF2m_add(point->Y, point->X, point->Y);
  529. }
  530. /* Indicates whether the given point is the point at infinity. */
  531. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
  532. const EC_POINT *point)
  533. {
  534. return BN_is_zero(point->Z);
  535. }
  536. /*-
  537. * Determines whether the given EC_POINT is an actual point on the curve defined
  538. * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
  539. * y^2 + x*y = x^3 + a*x^2 + b.
  540. */
  541. int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  542. BN_CTX *ctx)
  543. {
  544. int ret = -1;
  545. BN_CTX *new_ctx = NULL;
  546. BIGNUM *lh, *y2;
  547. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  548. const BIGNUM *, BN_CTX *);
  549. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  550. if (EC_POINT_is_at_infinity(group, point))
  551. return 1;
  552. field_mul = group->meth->field_mul;
  553. field_sqr = group->meth->field_sqr;
  554. /* only support affine coordinates */
  555. if (!point->Z_is_one)
  556. return -1;
  557. if (ctx == NULL) {
  558. ctx = new_ctx = BN_CTX_new();
  559. if (ctx == NULL)
  560. return -1;
  561. }
  562. BN_CTX_start(ctx);
  563. y2 = BN_CTX_get(ctx);
  564. lh = BN_CTX_get(ctx);
  565. if (lh == NULL)
  566. goto err;
  567. /*-
  568. * We have a curve defined by a Weierstrass equation
  569. * y^2 + x*y = x^3 + a*x^2 + b.
  570. * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
  571. * <=> ((x + a) * x + y ) * x + b + y^2 = 0
  572. */
  573. if (!BN_GF2m_add(lh, point->X, group->a))
  574. goto err;
  575. if (!field_mul(group, lh, lh, point->X, ctx))
  576. goto err;
  577. if (!BN_GF2m_add(lh, lh, point->Y))
  578. goto err;
  579. if (!field_mul(group, lh, lh, point->X, ctx))
  580. goto err;
  581. if (!BN_GF2m_add(lh, lh, group->b))
  582. goto err;
  583. if (!field_sqr(group, y2, point->Y, ctx))
  584. goto err;
  585. if (!BN_GF2m_add(lh, lh, y2))
  586. goto err;
  587. ret = BN_is_zero(lh);
  588. err:
  589. if (ctx)
  590. BN_CTX_end(ctx);
  591. BN_CTX_free(new_ctx);
  592. return ret;
  593. }
  594. /*-
  595. * Indicates whether two points are equal.
  596. * Return values:
  597. * -1 error
  598. * 0 equal (in affine coordinates)
  599. * 1 not equal
  600. */
  601. int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  602. const EC_POINT *b, BN_CTX *ctx)
  603. {
  604. BIGNUM *aX, *aY, *bX, *bY;
  605. BN_CTX *new_ctx = NULL;
  606. int ret = -1;
  607. if (EC_POINT_is_at_infinity(group, a)) {
  608. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  609. }
  610. if (EC_POINT_is_at_infinity(group, b))
  611. return 1;
  612. if (a->Z_is_one && b->Z_is_one) {
  613. return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
  614. }
  615. if (ctx == NULL) {
  616. ctx = new_ctx = BN_CTX_new();
  617. if (ctx == NULL)
  618. return -1;
  619. }
  620. BN_CTX_start(ctx);
  621. aX = BN_CTX_get(ctx);
  622. aY = BN_CTX_get(ctx);
  623. bX = BN_CTX_get(ctx);
  624. bY = BN_CTX_get(ctx);
  625. if (bY == NULL)
  626. goto err;
  627. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
  628. goto err;
  629. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
  630. goto err;
  631. ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
  632. err:
  633. if (ctx)
  634. BN_CTX_end(ctx);
  635. BN_CTX_free(new_ctx);
  636. return ret;
  637. }
  638. /* Forces the given EC_POINT to internally use affine coordinates. */
  639. int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  640. BN_CTX *ctx)
  641. {
  642. BN_CTX *new_ctx = NULL;
  643. BIGNUM *x, *y;
  644. int ret = 0;
  645. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  646. return 1;
  647. if (ctx == NULL) {
  648. ctx = new_ctx = BN_CTX_new();
  649. if (ctx == NULL)
  650. return 0;
  651. }
  652. BN_CTX_start(ctx);
  653. x = BN_CTX_get(ctx);
  654. y = BN_CTX_get(ctx);
  655. if (y == NULL)
  656. goto err;
  657. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
  658. goto err;
  659. if (!BN_copy(point->X, x))
  660. goto err;
  661. if (!BN_copy(point->Y, y))
  662. goto err;
  663. if (!BN_one(point->Z))
  664. goto err;
  665. ret = 1;
  666. err:
  667. if (ctx)
  668. BN_CTX_end(ctx);
  669. BN_CTX_free(new_ctx);
  670. return ret;
  671. }
  672. /*
  673. * Forces each of the EC_POINTs in the given array to use affine coordinates.
  674. */
  675. int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
  676. EC_POINT *points[], BN_CTX *ctx)
  677. {
  678. size_t i;
  679. for (i = 0; i < num; i++) {
  680. if (!group->meth->make_affine(group, points[i], ctx))
  681. return 0;
  682. }
  683. return 1;
  684. }
  685. /* Wrapper to simple binary polynomial field multiplication implementation. */
  686. int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
  687. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  688. {
  689. return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
  690. }
  691. /* Wrapper to simple binary polynomial field squaring implementation. */
  692. int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
  693. const BIGNUM *a, BN_CTX *ctx)
  694. {
  695. return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
  696. }
  697. /* Wrapper to simple binary polynomial field division implementation. */
  698. int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
  699. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  700. {
  701. return BN_GF2m_mod_div(r, a, b, group->field, ctx);
  702. }
  703. #endif