ec2_smpl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 "ec_lcl.h"
  71. #ifndef OPENSSL_NO_EC2M
  72. # ifdef OPENSSL_FIPS
  73. # include <openssl/fips.h>
  74. # endif
  75. const EC_METHOD *EC_GF2m_simple_method(void)
  76. {
  77. static const EC_METHOD ret = {
  78. EC_FLAGS_DEFAULT_OCT,
  79. NID_X9_62_characteristic_two_field,
  80. ec_GF2m_simple_group_init,
  81. ec_GF2m_simple_group_finish,
  82. ec_GF2m_simple_group_clear_finish,
  83. ec_GF2m_simple_group_copy,
  84. ec_GF2m_simple_group_set_curve,
  85. ec_GF2m_simple_group_get_curve,
  86. ec_GF2m_simple_group_get_degree,
  87. ec_GF2m_simple_group_check_discriminant,
  88. ec_GF2m_simple_point_init,
  89. ec_GF2m_simple_point_finish,
  90. ec_GF2m_simple_point_clear_finish,
  91. ec_GF2m_simple_point_copy,
  92. ec_GF2m_simple_point_set_to_infinity,
  93. 0 /* set_Jprojective_coordinates_GFp */ ,
  94. 0 /* get_Jprojective_coordinates_GFp */ ,
  95. ec_GF2m_simple_point_set_affine_coordinates,
  96. ec_GF2m_simple_point_get_affine_coordinates,
  97. 0, 0, 0,
  98. ec_GF2m_simple_add,
  99. ec_GF2m_simple_dbl,
  100. ec_GF2m_simple_invert,
  101. ec_GF2m_simple_is_at_infinity,
  102. ec_GF2m_simple_is_on_curve,
  103. ec_GF2m_simple_cmp,
  104. ec_GF2m_simple_make_affine,
  105. ec_GF2m_simple_points_make_affine,
  106. /*
  107. * the following three method functions are defined in ec2_mult.c
  108. */
  109. ec_GF2m_simple_mul,
  110. ec_GF2m_precompute_mult,
  111. ec_GF2m_have_precompute_mult,
  112. ec_GF2m_simple_field_mul,
  113. ec_GF2m_simple_field_sqr,
  114. ec_GF2m_simple_field_div,
  115. 0 /* field_encode */ ,
  116. 0 /* field_decode */ ,
  117. 0 /* field_set_to_one */
  118. };
  119. # ifdef OPENSSL_FIPS
  120. if (FIPS_mode())
  121. return fips_ec_gf2m_simple_method();
  122. # endif
  123. return &ret;
  124. }
  125. /*
  126. * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
  127. * are handled by EC_GROUP_new.
  128. */
  129. int ec_GF2m_simple_group_init(EC_GROUP *group)
  130. {
  131. BN_init(&group->field);
  132. BN_init(&group->a);
  133. BN_init(&group->b);
  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. int i;
  169. if (!BN_copy(&dest->field, &src->field))
  170. return 0;
  171. if (!BN_copy(&dest->a, &src->a))
  172. return 0;
  173. if (!BN_copy(&dest->b, &src->b))
  174. return 0;
  175. dest->poly[0] = src->poly[0];
  176. dest->poly[1] = src->poly[1];
  177. dest->poly[2] = src->poly[2];
  178. dest->poly[3] = src->poly[3];
  179. dest->poly[4] = src->poly[4];
  180. dest->poly[5] = src->poly[5];
  181. if (bn_wexpand(&dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  182. == NULL)
  183. return 0;
  184. if (bn_wexpand(&dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  185. == NULL)
  186. return 0;
  187. for (i = dest->a.top; i < dest->a.dmax; i++)
  188. dest->a.d[i] = 0;
  189. for (i = dest->b.top; i < dest->b.dmax; i++)
  190. dest->b.d[i] = 0;
  191. return 1;
  192. }
  193. /* Set the curve parameters of an EC_GROUP structure. */
  194. int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
  195. const BIGNUM *p, const BIGNUM *a,
  196. const BIGNUM *b, BN_CTX *ctx)
  197. {
  198. int ret = 0, i;
  199. /* group->field */
  200. if (!BN_copy(&group->field, p))
  201. goto err;
  202. i = BN_GF2m_poly2arr(&group->field, group->poly, 6) - 1;
  203. if ((i != 5) && (i != 3)) {
  204. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
  205. goto err;
  206. }
  207. /* group->a */
  208. if (!BN_GF2m_mod_arr(&group->a, a, group->poly))
  209. goto err;
  210. if (bn_wexpand(&group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  211. == NULL)
  212. goto err;
  213. for (i = group->a.top; i < group->a.dmax; i++)
  214. group->a.d[i] = 0;
  215. /* group->b */
  216. if (!BN_GF2m_mod_arr(&group->b, b, group->poly))
  217. goto err;
  218. if (bn_wexpand(&group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  219. == NULL)
  220. goto err;
  221. for (i = group->b.top; i < group->b.dmax; i++)
  222. group->b.d[i] = 0;
  223. ret = 1;
  224. err:
  225. return ret;
  226. }
  227. /*
  228. * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
  229. * then there values will not be set but the method will return with success.
  230. */
  231. int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
  232. BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  233. {
  234. int ret = 0;
  235. if (p != NULL) {
  236. if (!BN_copy(p, &group->field))
  237. return 0;
  238. }
  239. if (a != NULL) {
  240. if (!BN_copy(a, &group->a))
  241. goto err;
  242. }
  243. if (b != NULL) {
  244. if (!BN_copy(b, &group->b))
  245. goto err;
  246. }
  247. ret = 1;
  248. err:
  249. return ret;
  250. }
  251. /*
  252. * Gets the degree of the field. For a curve over GF(2^m) this is the value
  253. * m.
  254. */
  255. int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
  256. {
  257. return BN_num_bits(&group->field) - 1;
  258. }
  259. /*
  260. * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
  261. * elliptic curve <=> b != 0 (mod p)
  262. */
  263. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
  264. BN_CTX *ctx)
  265. {
  266. int ret = 0;
  267. BIGNUM *b;
  268. BN_CTX *new_ctx = NULL;
  269. if (ctx == NULL) {
  270. ctx = new_ctx = BN_CTX_new();
  271. if (ctx == NULL) {
  272. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
  273. ERR_R_MALLOC_FAILURE);
  274. goto err;
  275. }
  276. }
  277. BN_CTX_start(ctx);
  278. b = BN_CTX_get(ctx);
  279. if (b == NULL)
  280. goto err;
  281. if (!BN_GF2m_mod_arr(b, &group->b, group->poly))
  282. goto err;
  283. /*
  284. * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
  285. * curve <=> b != 0 (mod p)
  286. */
  287. if (BN_is_zero(b))
  288. goto err;
  289. ret = 1;
  290. err:
  291. if (ctx != NULL)
  292. BN_CTX_end(ctx);
  293. if (new_ctx != NULL)
  294. BN_CTX_free(new_ctx);
  295. return ret;
  296. }
  297. /* Initializes an EC_POINT. */
  298. int ec_GF2m_simple_point_init(EC_POINT *point)
  299. {
  300. BN_init(&point->X);
  301. BN_init(&point->Y);
  302. BN_init(&point->Z);
  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. if (new_ctx != NULL)
  510. BN_CTX_free(new_ctx);
  511. return ret;
  512. }
  513. /*
  514. * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
  515. * A.10.2 of IEEE P1363.
  516. */
  517. int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  518. BN_CTX *ctx)
  519. {
  520. return ec_GF2m_simple_add(group, r, a, a, ctx);
  521. }
  522. int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  523. {
  524. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
  525. /* point is its own inverse */
  526. return 1;
  527. if (!EC_POINT_make_affine(group, point, ctx))
  528. return 0;
  529. return BN_GF2m_add(&point->Y, &point->X, &point->Y);
  530. }
  531. /* Indicates whether the given point is the point at infinity. */
  532. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
  533. const EC_POINT *point)
  534. {
  535. return BN_is_zero(&point->Z);
  536. }
  537. /*-
  538. * Determines whether the given EC_POINT is an actual point on the curve defined
  539. * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
  540. * y^2 + x*y = x^3 + a*x^2 + b.
  541. */
  542. int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  543. BN_CTX *ctx)
  544. {
  545. int ret = -1;
  546. BN_CTX *new_ctx = NULL;
  547. BIGNUM *lh, *y2;
  548. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  549. const BIGNUM *, BN_CTX *);
  550. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  551. if (EC_POINT_is_at_infinity(group, point))
  552. return 1;
  553. field_mul = group->meth->field_mul;
  554. field_sqr = group->meth->field_sqr;
  555. /* only support affine coordinates */
  556. if (!point->Z_is_one)
  557. return -1;
  558. if (ctx == NULL) {
  559. ctx = new_ctx = BN_CTX_new();
  560. if (ctx == NULL)
  561. return -1;
  562. }
  563. BN_CTX_start(ctx);
  564. y2 = BN_CTX_get(ctx);
  565. lh = BN_CTX_get(ctx);
  566. if (lh == NULL)
  567. goto err;
  568. /*-
  569. * We have a curve defined by a Weierstrass equation
  570. * y^2 + x*y = x^3 + a*x^2 + b.
  571. * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
  572. * <=> ((x + a) * x + y ) * x + b + y^2 = 0
  573. */
  574. if (!BN_GF2m_add(lh, &point->X, &group->a))
  575. goto err;
  576. if (!field_mul(group, lh, lh, &point->X, ctx))
  577. goto err;
  578. if (!BN_GF2m_add(lh, lh, &point->Y))
  579. goto err;
  580. if (!field_mul(group, lh, lh, &point->X, ctx))
  581. goto err;
  582. if (!BN_GF2m_add(lh, lh, &group->b))
  583. goto err;
  584. if (!field_sqr(group, y2, &point->Y, ctx))
  585. goto err;
  586. if (!BN_GF2m_add(lh, lh, y2))
  587. goto err;
  588. ret = BN_is_zero(lh);
  589. err:
  590. if (ctx)
  591. BN_CTX_end(ctx);
  592. if (new_ctx)
  593. BN_CTX_free(new_ctx);
  594. return ret;
  595. }
  596. /*-
  597. * Indicates whether two points are equal.
  598. * Return values:
  599. * -1 error
  600. * 0 equal (in affine coordinates)
  601. * 1 not equal
  602. */
  603. int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  604. const EC_POINT *b, BN_CTX *ctx)
  605. {
  606. BIGNUM *aX, *aY, *bX, *bY;
  607. BN_CTX *new_ctx = NULL;
  608. int ret = -1;
  609. if (EC_POINT_is_at_infinity(group, a)) {
  610. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  611. }
  612. if (EC_POINT_is_at_infinity(group, b))
  613. return 1;
  614. if (a->Z_is_one && b->Z_is_one) {
  615. return ((BN_cmp(&a->X, &b->X) == 0)
  616. && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  617. }
  618. if (ctx == NULL) {
  619. ctx = new_ctx = BN_CTX_new();
  620. if (ctx == NULL)
  621. return -1;
  622. }
  623. BN_CTX_start(ctx);
  624. aX = BN_CTX_get(ctx);
  625. aY = BN_CTX_get(ctx);
  626. bX = BN_CTX_get(ctx);
  627. bY = BN_CTX_get(ctx);
  628. if (bY == NULL)
  629. goto err;
  630. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
  631. goto err;
  632. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
  633. goto err;
  634. ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
  635. err:
  636. if (ctx)
  637. BN_CTX_end(ctx);
  638. if (new_ctx)
  639. BN_CTX_free(new_ctx);
  640. return ret;
  641. }
  642. /* Forces the given EC_POINT to internally use affine coordinates. */
  643. int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  644. BN_CTX *ctx)
  645. {
  646. BN_CTX *new_ctx = NULL;
  647. BIGNUM *x, *y;
  648. int ret = 0;
  649. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  650. return 1;
  651. if (ctx == NULL) {
  652. ctx = new_ctx = BN_CTX_new();
  653. if (ctx == NULL)
  654. return 0;
  655. }
  656. BN_CTX_start(ctx);
  657. x = BN_CTX_get(ctx);
  658. y = BN_CTX_get(ctx);
  659. if (y == NULL)
  660. goto err;
  661. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
  662. goto err;
  663. if (!BN_copy(&point->X, x))
  664. goto err;
  665. if (!BN_copy(&point->Y, y))
  666. goto err;
  667. if (!BN_one(&point->Z))
  668. goto err;
  669. point->Z_is_one = 1;
  670. ret = 1;
  671. err:
  672. if (ctx)
  673. BN_CTX_end(ctx);
  674. if (new_ctx)
  675. BN_CTX_free(new_ctx);
  676. return ret;
  677. }
  678. /*
  679. * Forces each of the EC_POINTs in the given array to use affine coordinates.
  680. */
  681. int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
  682. EC_POINT *points[], BN_CTX *ctx)
  683. {
  684. size_t i;
  685. for (i = 0; i < num; i++) {
  686. if (!group->meth->make_affine(group, points[i], ctx))
  687. return 0;
  688. }
  689. return 1;
  690. }
  691. /* Wrapper to simple binary polynomial field multiplication implementation. */
  692. int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
  693. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  694. {
  695. return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
  696. }
  697. /* Wrapper to simple binary polynomial field squaring implementation. */
  698. int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
  699. const BIGNUM *a, BN_CTX *ctx)
  700. {
  701. return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
  702. }
  703. /* Wrapper to simple binary polynomial field division implementation. */
  704. int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
  705. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  706. {
  707. return BN_GF2m_mod_div(r, a, b, &group->field, ctx);
  708. }
  709. #endif