ec2_smpl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. const EC_METHOD *EC_GF2m_simple_method(void)
  72. {
  73. static const EC_METHOD ret = {
  74. NID_X9_62_characteristic_two_field,
  75. ec_GF2m_simple_group_init,
  76. ec_GF2m_simple_group_finish,
  77. ec_GF2m_simple_group_clear_finish,
  78. ec_GF2m_simple_group_copy,
  79. ec_GF2m_simple_group_set_curve,
  80. ec_GF2m_simple_group_get_curve,
  81. ec_GF2m_simple_group_get_degree,
  82. ec_GF2m_simple_group_check_discriminant,
  83. ec_GF2m_simple_point_init,
  84. ec_GF2m_simple_point_finish,
  85. ec_GF2m_simple_point_clear_finish,
  86. ec_GF2m_simple_point_copy,
  87. ec_GF2m_simple_point_set_to_infinity,
  88. 0 /* set_Jprojective_coordinates_GFp */ ,
  89. 0 /* get_Jprojective_coordinates_GFp */ ,
  90. ec_GF2m_simple_point_set_affine_coordinates,
  91. ec_GF2m_simple_point_get_affine_coordinates,
  92. ec_GF2m_simple_set_compressed_coordinates,
  93. ec_GF2m_simple_point2oct,
  94. ec_GF2m_simple_oct2point,
  95. ec_GF2m_simple_add,
  96. ec_GF2m_simple_dbl,
  97. ec_GF2m_simple_invert,
  98. ec_GF2m_simple_is_at_infinity,
  99. ec_GF2m_simple_is_on_curve,
  100. ec_GF2m_simple_cmp,
  101. ec_GF2m_simple_make_affine,
  102. ec_GF2m_simple_points_make_affine,
  103. /*
  104. * the following three method functions are defined in ec2_mult.c
  105. */
  106. ec_GF2m_simple_mul,
  107. ec_GF2m_precompute_mult,
  108. ec_GF2m_have_precompute_mult,
  109. ec_GF2m_simple_field_mul,
  110. ec_GF2m_simple_field_sqr,
  111. ec_GF2m_simple_field_div,
  112. 0 /* field_encode */ ,
  113. 0 /* field_decode */ ,
  114. 0 /* field_set_to_one */
  115. };
  116. return &ret;
  117. }
  118. /*
  119. * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members
  120. * are handled by EC_GROUP_new.
  121. */
  122. int ec_GF2m_simple_group_init(EC_GROUP *group)
  123. {
  124. BN_init(&group->field);
  125. BN_init(&group->a);
  126. BN_init(&group->b);
  127. return 1;
  128. }
  129. /*
  130. * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are
  131. * handled by EC_GROUP_free.
  132. */
  133. void ec_GF2m_simple_group_finish(EC_GROUP *group)
  134. {
  135. BN_free(&group->field);
  136. BN_free(&group->a);
  137. BN_free(&group->b);
  138. }
  139. /*
  140. * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other
  141. * members are handled by EC_GROUP_clear_free.
  142. */
  143. void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
  144. {
  145. BN_clear_free(&group->field);
  146. BN_clear_free(&group->a);
  147. BN_clear_free(&group->b);
  148. group->poly[0] = 0;
  149. group->poly[1] = 0;
  150. group->poly[2] = 0;
  151. group->poly[3] = 0;
  152. group->poly[4] = 0;
  153. group->poly[5] = -1;
  154. }
  155. /*
  156. * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are
  157. * handled by EC_GROUP_copy.
  158. */
  159. int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  160. {
  161. int i;
  162. if (!BN_copy(&dest->field, &src->field))
  163. return 0;
  164. if (!BN_copy(&dest->a, &src->a))
  165. return 0;
  166. if (!BN_copy(&dest->b, &src->b))
  167. return 0;
  168. dest->poly[0] = src->poly[0];
  169. dest->poly[1] = src->poly[1];
  170. dest->poly[2] = src->poly[2];
  171. dest->poly[3] = src->poly[3];
  172. dest->poly[4] = src->poly[4];
  173. dest->poly[5] = src->poly[5];
  174. if (bn_wexpand(&dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  175. == NULL)
  176. return 0;
  177. if (bn_wexpand(&dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  178. == NULL)
  179. return 0;
  180. for (i = dest->a.top; i < dest->a.dmax; i++)
  181. dest->a.d[i] = 0;
  182. for (i = dest->b.top; i < dest->b.dmax; i++)
  183. dest->b.d[i] = 0;
  184. return 1;
  185. }
  186. /* Set the curve parameters of an EC_GROUP structure. */
  187. int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
  188. const BIGNUM *p, const BIGNUM *a,
  189. const BIGNUM *b, BN_CTX *ctx)
  190. {
  191. int ret = 0, i;
  192. /* group->field */
  193. if (!BN_copy(&group->field, p))
  194. goto err;
  195. i = BN_GF2m_poly2arr(&group->field, group->poly, 6) - 1;
  196. if ((i != 5) && (i != 3)) {
  197. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
  198. goto err;
  199. }
  200. /* group->a */
  201. if (!BN_GF2m_mod_arr(&group->a, a, group->poly))
  202. goto err;
  203. if (bn_wexpand(&group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  204. == NULL)
  205. goto err;
  206. for (i = group->a.top; i < group->a.dmax; i++)
  207. group->a.d[i] = 0;
  208. /* group->b */
  209. if (!BN_GF2m_mod_arr(&group->b, b, group->poly))
  210. goto err;
  211. if (bn_wexpand(&group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
  212. == NULL)
  213. goto err;
  214. for (i = group->b.top; i < group->b.dmax; i++)
  215. group->b.d[i] = 0;
  216. ret = 1;
  217. err:
  218. return ret;
  219. }
  220. /*
  221. * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL
  222. * then there values will not be set but the method will return with success.
  223. */
  224. int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
  225. BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  226. {
  227. int ret = 0;
  228. if (p != NULL) {
  229. if (!BN_copy(p, &group->field))
  230. return 0;
  231. }
  232. if (a != NULL) {
  233. if (!BN_copy(a, &group->a))
  234. goto err;
  235. }
  236. if (b != NULL) {
  237. if (!BN_copy(b, &group->b))
  238. goto err;
  239. }
  240. ret = 1;
  241. err:
  242. return ret;
  243. }
  244. /*
  245. * Gets the degree of the field. For a curve over GF(2^m) this is the value
  246. * m.
  247. */
  248. int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
  249. {
  250. return BN_num_bits(&group->field) - 1;
  251. }
  252. /*
  253. * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an
  254. * elliptic curve <=> b != 0 (mod p)
  255. */
  256. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
  257. BN_CTX *ctx)
  258. {
  259. int ret = 0;
  260. BIGNUM *b;
  261. BN_CTX *new_ctx = NULL;
  262. if (ctx == NULL) {
  263. ctx = new_ctx = BN_CTX_new();
  264. if (ctx == NULL) {
  265. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
  266. ERR_R_MALLOC_FAILURE);
  267. goto err;
  268. }
  269. }
  270. BN_CTX_start(ctx);
  271. b = BN_CTX_get(ctx);
  272. if (b == NULL)
  273. goto err;
  274. if (!BN_GF2m_mod_arr(b, &group->b, group->poly))
  275. goto err;
  276. /*
  277. * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic
  278. * curve <=> b != 0 (mod p)
  279. */
  280. if (BN_is_zero(b))
  281. goto err;
  282. ret = 1;
  283. err:
  284. if (ctx != NULL)
  285. BN_CTX_end(ctx);
  286. if (new_ctx != NULL)
  287. BN_CTX_free(new_ctx);
  288. return ret;
  289. }
  290. /* Initializes an EC_POINT. */
  291. int ec_GF2m_simple_point_init(EC_POINT *point)
  292. {
  293. BN_init(&point->X);
  294. BN_init(&point->Y);
  295. BN_init(&point->Z);
  296. return 1;
  297. }
  298. /* Frees an EC_POINT. */
  299. void ec_GF2m_simple_point_finish(EC_POINT *point)
  300. {
  301. BN_free(&point->X);
  302. BN_free(&point->Y);
  303. BN_free(&point->Z);
  304. }
  305. /* Clears and frees an EC_POINT. */
  306. void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
  307. {
  308. BN_clear_free(&point->X);
  309. BN_clear_free(&point->Y);
  310. BN_clear_free(&point->Z);
  311. point->Z_is_one = 0;
  312. }
  313. /*
  314. * Copy the contents of one EC_POINT into another. Assumes dest is
  315. * initialized.
  316. */
  317. int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  318. {
  319. if (!BN_copy(&dest->X, &src->X))
  320. return 0;
  321. if (!BN_copy(&dest->Y, &src->Y))
  322. return 0;
  323. if (!BN_copy(&dest->Z, &src->Z))
  324. return 0;
  325. dest->Z_is_one = src->Z_is_one;
  326. return 1;
  327. }
  328. /*
  329. * Set an EC_POINT to the point at infinity. A point at infinity is
  330. * represented by having Z=0.
  331. */
  332. int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
  333. EC_POINT *point)
  334. {
  335. point->Z_is_one = 0;
  336. BN_zero(&point->Z);
  337. return 1;
  338. }
  339. /*
  340. * Set the coordinates of an EC_POINT using affine coordinates. Note that
  341. * the simple implementation only uses affine coordinates.
  342. */
  343. int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
  344. EC_POINT *point,
  345. const BIGNUM *x,
  346. const BIGNUM *y, BN_CTX *ctx)
  347. {
  348. int ret = 0;
  349. if (x == NULL || y == NULL) {
  350. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
  351. ERR_R_PASSED_NULL_PARAMETER);
  352. return 0;
  353. }
  354. if (!BN_copy(&point->X, x))
  355. goto err;
  356. BN_set_negative(&point->X, 0);
  357. if (!BN_copy(&point->Y, y))
  358. goto err;
  359. BN_set_negative(&point->Y, 0);
  360. if (!BN_copy(&point->Z, BN_value_one()))
  361. goto err;
  362. BN_set_negative(&point->Z, 0);
  363. point->Z_is_one = 1;
  364. ret = 1;
  365. err:
  366. return ret;
  367. }
  368. /*
  369. * Gets the affine coordinates of an EC_POINT. Note that the simple
  370. * implementation only uses affine coordinates.
  371. */
  372. int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
  373. const EC_POINT *point,
  374. BIGNUM *x, BIGNUM *y,
  375. BN_CTX *ctx)
  376. {
  377. int ret = 0;
  378. if (EC_POINT_is_at_infinity(group, point)) {
  379. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  380. EC_R_POINT_AT_INFINITY);
  381. return 0;
  382. }
  383. if (BN_cmp(&point->Z, BN_value_one())) {
  384. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
  385. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  386. return 0;
  387. }
  388. if (x != NULL) {
  389. if (!BN_copy(x, &point->X))
  390. goto err;
  391. BN_set_negative(x, 0);
  392. }
  393. if (y != NULL) {
  394. if (!BN_copy(y, &point->Y))
  395. goto err;
  396. BN_set_negative(y, 0);
  397. }
  398. ret = 1;
  399. err:
  400. return ret;
  401. }
  402. /*-
  403. * Calculates and sets the affine coordinates of an EC_POINT from the given
  404. * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
  405. * Note that the simple implementation only uses affine coordinates.
  406. *
  407. * The method is from the following publication:
  408. *
  409. * Harper, Menezes, Vanstone:
  410. * "Public-Key Cryptosystems with Very Small Key Lengths",
  411. * EUROCRYPT '92, Springer-Verlag LNCS 658,
  412. * published February 1993
  413. *
  414. * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
  415. * the same method, but claim no priority date earlier than July 29, 1994
  416. * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
  417. */
  418. int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
  419. EC_POINT *point,
  420. const BIGNUM *x_, int y_bit,
  421. BN_CTX *ctx)
  422. {
  423. BN_CTX *new_ctx = NULL;
  424. BIGNUM *tmp, *x, *y, *z;
  425. int ret = 0, z0;
  426. /* clear error queue */
  427. ERR_clear_error();
  428. if (ctx == NULL) {
  429. ctx = new_ctx = BN_CTX_new();
  430. if (ctx == NULL)
  431. return 0;
  432. }
  433. y_bit = (y_bit != 0) ? 1 : 0;
  434. BN_CTX_start(ctx);
  435. tmp = BN_CTX_get(ctx);
  436. x = BN_CTX_get(ctx);
  437. y = BN_CTX_get(ctx);
  438. z = BN_CTX_get(ctx);
  439. if (z == NULL)
  440. goto err;
  441. if (!BN_GF2m_mod_arr(x, x_, group->poly))
  442. goto err;
  443. if (BN_is_zero(x)) {
  444. if (!BN_GF2m_mod_sqrt_arr(y, &group->b, group->poly, ctx))
  445. goto err;
  446. } else {
  447. if (!group->meth->field_sqr(group, tmp, x, ctx))
  448. goto err;
  449. if (!group->meth->field_div(group, tmp, &group->b, tmp, ctx))
  450. goto err;
  451. if (!BN_GF2m_add(tmp, &group->a, tmp))
  452. goto err;
  453. if (!BN_GF2m_add(tmp, x, tmp))
  454. goto err;
  455. if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
  456. unsigned long err = ERR_peek_last_error();
  457. if (ERR_GET_LIB(err) == ERR_LIB_BN
  458. && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
  459. ERR_clear_error();
  460. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  461. EC_R_INVALID_COMPRESSED_POINT);
  462. } else
  463. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  464. ERR_R_BN_LIB);
  465. goto err;
  466. }
  467. z0 = (BN_is_odd(z)) ? 1 : 0;
  468. if (!group->meth->field_mul(group, y, x, z, ctx))
  469. goto err;
  470. if (z0 != y_bit) {
  471. if (!BN_GF2m_add(y, y, x))
  472. goto err;
  473. }
  474. }
  475. if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))
  476. goto err;
  477. ret = 1;
  478. err:
  479. BN_CTX_end(ctx);
  480. if (new_ctx != NULL)
  481. BN_CTX_free(new_ctx);
  482. return ret;
  483. }
  484. /*
  485. * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
  486. * length will be returned. If the length len of buf is smaller than required
  487. * an error will be returned.
  488. */
  489. size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  490. point_conversion_form_t form,
  491. unsigned char *buf, size_t len, BN_CTX *ctx)
  492. {
  493. size_t ret;
  494. BN_CTX *new_ctx = NULL;
  495. int used_ctx = 0;
  496. BIGNUM *x, *y, *yxi;
  497. size_t field_len, i, skip;
  498. if ((form != POINT_CONVERSION_COMPRESSED)
  499. && (form != POINT_CONVERSION_UNCOMPRESSED)
  500. && (form != POINT_CONVERSION_HYBRID)) {
  501. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  502. goto err;
  503. }
  504. if (EC_POINT_is_at_infinity(group, point)) {
  505. /* encodes to a single 0 octet */
  506. if (buf != NULL) {
  507. if (len < 1) {
  508. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  509. return 0;
  510. }
  511. buf[0] = 0;
  512. }
  513. return 1;
  514. }
  515. /* ret := required output buffer length */
  516. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  517. ret =
  518. (form ==
  519. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  520. /* if 'buf' is NULL, just return required length */
  521. if (buf != NULL) {
  522. if (len < ret) {
  523. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  524. goto err;
  525. }
  526. if (ctx == NULL) {
  527. ctx = new_ctx = BN_CTX_new();
  528. if (ctx == NULL)
  529. return 0;
  530. }
  531. BN_CTX_start(ctx);
  532. used_ctx = 1;
  533. x = BN_CTX_get(ctx);
  534. y = BN_CTX_get(ctx);
  535. yxi = BN_CTX_get(ctx);
  536. if (yxi == NULL)
  537. goto err;
  538. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
  539. goto err;
  540. buf[0] = form;
  541. if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
  542. if (!group->meth->field_div(group, yxi, y, x, ctx))
  543. goto err;
  544. if (BN_is_odd(yxi))
  545. buf[0]++;
  546. }
  547. i = 1;
  548. skip = field_len - BN_num_bytes(x);
  549. if (skip > field_len) {
  550. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  551. goto err;
  552. }
  553. while (skip > 0) {
  554. buf[i++] = 0;
  555. skip--;
  556. }
  557. skip = BN_bn2bin(x, buf + i);
  558. i += skip;
  559. if (i != 1 + field_len) {
  560. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  561. goto err;
  562. }
  563. if (form == POINT_CONVERSION_UNCOMPRESSED
  564. || form == POINT_CONVERSION_HYBRID) {
  565. skip = field_len - BN_num_bytes(y);
  566. if (skip > field_len) {
  567. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  568. goto err;
  569. }
  570. while (skip > 0) {
  571. buf[i++] = 0;
  572. skip--;
  573. }
  574. skip = BN_bn2bin(y, buf + i);
  575. i += skip;
  576. }
  577. if (i != ret) {
  578. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  579. goto err;
  580. }
  581. }
  582. if (used_ctx)
  583. BN_CTX_end(ctx);
  584. if (new_ctx != NULL)
  585. BN_CTX_free(new_ctx);
  586. return ret;
  587. err:
  588. if (used_ctx)
  589. BN_CTX_end(ctx);
  590. if (new_ctx != NULL)
  591. BN_CTX_free(new_ctx);
  592. return 0;
  593. }
  594. /*
  595. * Converts an octet string representation to an EC_POINT. Note that the
  596. * simple implementation only uses affine coordinates.
  597. */
  598. int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  599. const unsigned char *buf, size_t len,
  600. BN_CTX *ctx)
  601. {
  602. point_conversion_form_t form;
  603. int y_bit;
  604. BN_CTX *new_ctx = NULL;
  605. BIGNUM *x, *y, *yxi;
  606. size_t field_len, enc_len;
  607. int ret = 0;
  608. if (len == 0) {
  609. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  610. return 0;
  611. }
  612. form = buf[0];
  613. y_bit = form & 1;
  614. form = form & ~1U;
  615. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  616. && (form != POINT_CONVERSION_UNCOMPRESSED)
  617. && (form != POINT_CONVERSION_HYBRID)) {
  618. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  619. return 0;
  620. }
  621. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  622. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  623. return 0;
  624. }
  625. if (form == 0) {
  626. if (len != 1) {
  627. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  628. return 0;
  629. }
  630. return EC_POINT_set_to_infinity(group, point);
  631. }
  632. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  633. enc_len =
  634. (form ==
  635. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  636. if (len != enc_len) {
  637. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  638. return 0;
  639. }
  640. if (ctx == NULL) {
  641. ctx = new_ctx = BN_CTX_new();
  642. if (ctx == NULL)
  643. return 0;
  644. }
  645. BN_CTX_start(ctx);
  646. x = BN_CTX_get(ctx);
  647. y = BN_CTX_get(ctx);
  648. yxi = BN_CTX_get(ctx);
  649. if (yxi == NULL)
  650. goto err;
  651. if (!BN_bin2bn(buf + 1, field_len, x))
  652. goto err;
  653. if (BN_ucmp(x, &group->field) >= 0) {
  654. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  655. goto err;
  656. }
  657. if (form == POINT_CONVERSION_COMPRESSED) {
  658. if (!EC_POINT_set_compressed_coordinates_GF2m
  659. (group, point, x, y_bit, ctx))
  660. goto err;
  661. } else {
  662. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  663. goto err;
  664. if (BN_ucmp(y, &group->field) >= 0) {
  665. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  666. goto err;
  667. }
  668. if (form == POINT_CONVERSION_HYBRID) {
  669. if (!group->meth->field_div(group, yxi, y, x, ctx))
  670. goto err;
  671. if (y_bit != BN_is_odd(yxi)) {
  672. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  673. goto err;
  674. }
  675. }
  676. if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))
  677. goto err;
  678. }
  679. /* test required by X9.62 */
  680. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  681. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  682. goto err;
  683. }
  684. ret = 1;
  685. err:
  686. BN_CTX_end(ctx);
  687. if (new_ctx != NULL)
  688. BN_CTX_free(new_ctx);
  689. return ret;
  690. }
  691. /*
  692. * Computes a + b and stores the result in r. r could be a or b, a could be
  693. * b. Uses algorithm A.10.2 of IEEE P1363.
  694. */
  695. int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  696. const EC_POINT *b, BN_CTX *ctx)
  697. {
  698. BN_CTX *new_ctx = NULL;
  699. BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
  700. int ret = 0;
  701. if (EC_POINT_is_at_infinity(group, a)) {
  702. if (!EC_POINT_copy(r, b))
  703. return 0;
  704. return 1;
  705. }
  706. if (EC_POINT_is_at_infinity(group, b)) {
  707. if (!EC_POINT_copy(r, a))
  708. return 0;
  709. return 1;
  710. }
  711. if (ctx == NULL) {
  712. ctx = new_ctx = BN_CTX_new();
  713. if (ctx == NULL)
  714. return 0;
  715. }
  716. BN_CTX_start(ctx);
  717. x0 = BN_CTX_get(ctx);
  718. y0 = BN_CTX_get(ctx);
  719. x1 = BN_CTX_get(ctx);
  720. y1 = BN_CTX_get(ctx);
  721. x2 = BN_CTX_get(ctx);
  722. y2 = BN_CTX_get(ctx);
  723. s = BN_CTX_get(ctx);
  724. t = BN_CTX_get(ctx);
  725. if (t == NULL)
  726. goto err;
  727. if (a->Z_is_one) {
  728. if (!BN_copy(x0, &a->X))
  729. goto err;
  730. if (!BN_copy(y0, &a->Y))
  731. goto err;
  732. } else {
  733. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))
  734. goto err;
  735. }
  736. if (b->Z_is_one) {
  737. if (!BN_copy(x1, &b->X))
  738. goto err;
  739. if (!BN_copy(y1, &b->Y))
  740. goto err;
  741. } else {
  742. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))
  743. goto err;
  744. }
  745. if (BN_GF2m_cmp(x0, x1)) {
  746. if (!BN_GF2m_add(t, x0, x1))
  747. goto err;
  748. if (!BN_GF2m_add(s, y0, y1))
  749. goto err;
  750. if (!group->meth->field_div(group, s, s, t, ctx))
  751. goto err;
  752. if (!group->meth->field_sqr(group, x2, s, ctx))
  753. goto err;
  754. if (!BN_GF2m_add(x2, x2, &group->a))
  755. goto err;
  756. if (!BN_GF2m_add(x2, x2, s))
  757. goto err;
  758. if (!BN_GF2m_add(x2, x2, t))
  759. goto err;
  760. } else {
  761. if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {
  762. if (!EC_POINT_set_to_infinity(group, r))
  763. goto err;
  764. ret = 1;
  765. goto err;
  766. }
  767. if (!group->meth->field_div(group, s, y1, x1, ctx))
  768. goto err;
  769. if (!BN_GF2m_add(s, s, x1))
  770. goto err;
  771. if (!group->meth->field_sqr(group, x2, s, ctx))
  772. goto err;
  773. if (!BN_GF2m_add(x2, x2, s))
  774. goto err;
  775. if (!BN_GF2m_add(x2, x2, &group->a))
  776. goto err;
  777. }
  778. if (!BN_GF2m_add(y2, x1, x2))
  779. goto err;
  780. if (!group->meth->field_mul(group, y2, y2, s, ctx))
  781. goto err;
  782. if (!BN_GF2m_add(y2, y2, x2))
  783. goto err;
  784. if (!BN_GF2m_add(y2, y2, y1))
  785. goto err;
  786. if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))
  787. goto err;
  788. ret = 1;
  789. err:
  790. BN_CTX_end(ctx);
  791. if (new_ctx != NULL)
  792. BN_CTX_free(new_ctx);
  793. return ret;
  794. }
  795. /*
  796. * Computes 2 * a and stores the result in r. r could be a. Uses algorithm
  797. * A.10.2 of IEEE P1363.
  798. */
  799. int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  800. BN_CTX *ctx)
  801. {
  802. return ec_GF2m_simple_add(group, r, a, a, ctx);
  803. }
  804. int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  805. {
  806. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
  807. /* point is its own inverse */
  808. return 1;
  809. if (!EC_POINT_make_affine(group, point, ctx))
  810. return 0;
  811. return BN_GF2m_add(&point->Y, &point->X, &point->Y);
  812. }
  813. /* Indicates whether the given point is the point at infinity. */
  814. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
  815. const EC_POINT *point)
  816. {
  817. return BN_is_zero(&point->Z);
  818. }
  819. /*-
  820. * Determines whether the given EC_POINT is an actual point on the curve defined
  821. * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
  822. * y^2 + x*y = x^3 + a*x^2 + b.
  823. */
  824. int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  825. BN_CTX *ctx)
  826. {
  827. int ret = -1;
  828. BN_CTX *new_ctx = NULL;
  829. BIGNUM *lh, *y2;
  830. int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
  831. const BIGNUM *, BN_CTX *);
  832. int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
  833. if (EC_POINT_is_at_infinity(group, point))
  834. return 1;
  835. field_mul = group->meth->field_mul;
  836. field_sqr = group->meth->field_sqr;
  837. /* only support affine coordinates */
  838. if (!point->Z_is_one)
  839. return -1;
  840. if (ctx == NULL) {
  841. ctx = new_ctx = BN_CTX_new();
  842. if (ctx == NULL)
  843. return -1;
  844. }
  845. BN_CTX_start(ctx);
  846. y2 = BN_CTX_get(ctx);
  847. lh = BN_CTX_get(ctx);
  848. if (lh == NULL)
  849. goto err;
  850. /*-
  851. * We have a curve defined by a Weierstrass equation
  852. * y^2 + x*y = x^3 + a*x^2 + b.
  853. * <=> x^3 + a*x^2 + x*y + b + y^2 = 0
  854. * <=> ((x + a) * x + y ) * x + b + y^2 = 0
  855. */
  856. if (!BN_GF2m_add(lh, &point->X, &group->a))
  857. goto err;
  858. if (!field_mul(group, lh, lh, &point->X, ctx))
  859. goto err;
  860. if (!BN_GF2m_add(lh, lh, &point->Y))
  861. goto err;
  862. if (!field_mul(group, lh, lh, &point->X, ctx))
  863. goto err;
  864. if (!BN_GF2m_add(lh, lh, &group->b))
  865. goto err;
  866. if (!field_sqr(group, y2, &point->Y, ctx))
  867. goto err;
  868. if (!BN_GF2m_add(lh, lh, y2))
  869. goto err;
  870. ret = BN_is_zero(lh);
  871. err:
  872. if (ctx)
  873. BN_CTX_end(ctx);
  874. if (new_ctx)
  875. BN_CTX_free(new_ctx);
  876. return ret;
  877. }
  878. /*-
  879. * Indicates whether two points are equal.
  880. * Return values:
  881. * -1 error
  882. * 0 equal (in affine coordinates)
  883. * 1 not equal
  884. */
  885. int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
  886. const EC_POINT *b, BN_CTX *ctx)
  887. {
  888. BIGNUM *aX, *aY, *bX, *bY;
  889. BN_CTX *new_ctx = NULL;
  890. int ret = -1;
  891. if (EC_POINT_is_at_infinity(group, a)) {
  892. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  893. }
  894. if (EC_POINT_is_at_infinity(group, b))
  895. return 1;
  896. if (a->Z_is_one && b->Z_is_one) {
  897. return ((BN_cmp(&a->X, &b->X) == 0)
  898. && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  899. }
  900. if (ctx == NULL) {
  901. ctx = new_ctx = BN_CTX_new();
  902. if (ctx == NULL)
  903. return -1;
  904. }
  905. BN_CTX_start(ctx);
  906. aX = BN_CTX_get(ctx);
  907. aY = BN_CTX_get(ctx);
  908. bX = BN_CTX_get(ctx);
  909. bY = BN_CTX_get(ctx);
  910. if (bY == NULL)
  911. goto err;
  912. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))
  913. goto err;
  914. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))
  915. goto err;
  916. ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
  917. err:
  918. if (ctx)
  919. BN_CTX_end(ctx);
  920. if (new_ctx)
  921. BN_CTX_free(new_ctx);
  922. return ret;
  923. }
  924. /* Forces the given EC_POINT to internally use affine coordinates. */
  925. int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
  926. BN_CTX *ctx)
  927. {
  928. BN_CTX *new_ctx = NULL;
  929. BIGNUM *x, *y;
  930. int ret = 0;
  931. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  932. return 1;
  933. if (ctx == NULL) {
  934. ctx = new_ctx = BN_CTX_new();
  935. if (ctx == NULL)
  936. return 0;
  937. }
  938. BN_CTX_start(ctx);
  939. x = BN_CTX_get(ctx);
  940. y = BN_CTX_get(ctx);
  941. if (y == NULL)
  942. goto err;
  943. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
  944. goto err;
  945. if (!BN_copy(&point->X, x))
  946. goto err;
  947. if (!BN_copy(&point->Y, y))
  948. goto err;
  949. if (!BN_one(&point->Z))
  950. goto err;
  951. ret = 1;
  952. err:
  953. if (ctx)
  954. BN_CTX_end(ctx);
  955. if (new_ctx)
  956. BN_CTX_free(new_ctx);
  957. return ret;
  958. }
  959. /*
  960. * Forces each of the EC_POINTs in the given array to use affine coordinates.
  961. */
  962. int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
  963. EC_POINT *points[], BN_CTX *ctx)
  964. {
  965. size_t i;
  966. for (i = 0; i < num; i++) {
  967. if (!group->meth->make_affine(group, points[i], ctx))
  968. return 0;
  969. }
  970. return 1;
  971. }
  972. /* Wrapper to simple binary polynomial field multiplication implementation. */
  973. int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
  974. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  975. {
  976. return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
  977. }
  978. /* Wrapper to simple binary polynomial field squaring implementation. */
  979. int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
  980. const BIGNUM *a, BN_CTX *ctx)
  981. {
  982. return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
  983. }
  984. /* Wrapper to simple binary polynomial field division implementation. */
  985. int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
  986. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  987. {
  988. return BN_GF2m_mod_div(r, a, b, &group->field, ctx);
  989. }