ec2_smpl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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-2003 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. /* the following three method functions are defined in ec2_mult.c */
  104. ec_GF2m_simple_mul,
  105. ec_GF2m_precompute_mult,
  106. ec_GF2m_have_precompute_mult,
  107. ec_GF2m_simple_field_mul,
  108. ec_GF2m_simple_field_sqr,
  109. ec_GF2m_simple_field_div,
  110. 0 /* field_encode */,
  111. 0 /* field_decode */,
  112. 0 /* field_set_to_one */ };
  113. return &ret;
  114. }
  115. /* Initialize a GF(2^m)-based EC_GROUP structure.
  116. * Note that all other members are handled by EC_GROUP_new.
  117. */
  118. int ec_GF2m_simple_group_init(EC_GROUP *group)
  119. {
  120. BN_init(&group->field);
  121. BN_init(&group->a);
  122. BN_init(&group->b);
  123. return 1;
  124. }
  125. /* Free a GF(2^m)-based EC_GROUP structure.
  126. * Note that all other members are handled by EC_GROUP_free.
  127. */
  128. void ec_GF2m_simple_group_finish(EC_GROUP *group)
  129. {
  130. BN_free(&group->field);
  131. BN_free(&group->a);
  132. BN_free(&group->b);
  133. }
  134. /* Clear and free a GF(2^m)-based EC_GROUP structure.
  135. * Note that all other members are handled by EC_GROUP_clear_free.
  136. */
  137. void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
  138. {
  139. BN_clear_free(&group->field);
  140. BN_clear_free(&group->a);
  141. BN_clear_free(&group->b);
  142. group->poly[0] = 0;
  143. group->poly[1] = 0;
  144. group->poly[2] = 0;
  145. group->poly[3] = 0;
  146. group->poly[4] = 0;
  147. }
  148. /* Copy a GF(2^m)-based EC_GROUP structure.
  149. * Note that all other members are handled by EC_GROUP_copy.
  150. */
  151. int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  152. {
  153. int i;
  154. if (!BN_copy(&dest->field, &src->field)) return 0;
  155. if (!BN_copy(&dest->a, &src->a)) return 0;
  156. if (!BN_copy(&dest->b, &src->b)) return 0;
  157. dest->poly[0] = src->poly[0];
  158. dest->poly[1] = src->poly[1];
  159. dest->poly[2] = src->poly[2];
  160. dest->poly[3] = src->poly[3];
  161. dest->poly[4] = src->poly[4];
  162. bn_wexpand(&dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2);
  163. bn_wexpand(&dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2);
  164. for (i = dest->a.top; i < dest->a.dmax; i++) dest->a.d[i] = 0;
  165. for (i = dest->b.top; i < dest->b.dmax; i++) dest->b.d[i] = 0;
  166. return 1;
  167. }
  168. /* Set the curve parameters of an EC_GROUP structure. */
  169. int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
  170. const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  171. {
  172. int ret = 0, i;
  173. /* group->field */
  174. if (!BN_copy(&group->field, p)) goto err;
  175. i = BN_GF2m_poly2arr(&group->field, group->poly, 5);
  176. if ((i != 5) && (i != 3))
  177. {
  178. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
  179. goto err;
  180. }
  181. /* group->a */
  182. if (!BN_GF2m_mod_arr(&group->a, a, group->poly)) goto err;
  183. bn_wexpand(&group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2);
  184. for (i = group->a.top; i < group->a.dmax; i++) group->a.d[i] = 0;
  185. /* group->b */
  186. if (!BN_GF2m_mod_arr(&group->b, b, group->poly)) goto err;
  187. bn_wexpand(&group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2);
  188. for (i = group->b.top; i < group->b.dmax; i++) group->b.d[i] = 0;
  189. ret = 1;
  190. err:
  191. return ret;
  192. }
  193. /* Get the curve parameters of an EC_GROUP structure.
  194. * If p, a, or b are NULL then there values will not be set but the method will return with success.
  195. */
  196. int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  197. {
  198. int ret = 0;
  199. if (p != NULL)
  200. {
  201. if (!BN_copy(p, &group->field)) return 0;
  202. }
  203. if (a != NULL)
  204. {
  205. if (!BN_copy(a, &group->a)) goto err;
  206. }
  207. if (b != NULL)
  208. {
  209. if (!BN_copy(b, &group->b)) goto err;
  210. }
  211. ret = 1;
  212. err:
  213. return ret;
  214. }
  215. /* Gets the degree of the field. For a curve over GF(2^m) this is the value m. */
  216. int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
  217. {
  218. return BN_num_bits(&group->field)-1;
  219. }
  220. /* Checks the discriminant of the curve.
  221. * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
  222. */
  223. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  224. {
  225. int ret = 0;
  226. BIGNUM *b;
  227. BN_CTX *new_ctx = NULL;
  228. if (ctx == NULL)
  229. {
  230. ctx = new_ctx = BN_CTX_new();
  231. if (ctx == NULL)
  232. {
  233. ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
  234. goto err;
  235. }
  236. }
  237. BN_CTX_start(ctx);
  238. b = BN_CTX_get(ctx);
  239. if (b == NULL) goto err;
  240. if (!BN_GF2m_mod_arr(b, &group->b, group->poly)) goto err;
  241. /* check the discriminant:
  242. * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p)
  243. */
  244. if (BN_is_zero(b)) goto err;
  245. ret = 1;
  246. err:
  247. BN_CTX_end(ctx);
  248. if (new_ctx != NULL)
  249. BN_CTX_free(new_ctx);
  250. return ret;
  251. }
  252. /* Initializes an EC_POINT. */
  253. int ec_GF2m_simple_point_init(EC_POINT *point)
  254. {
  255. BN_init(&point->X);
  256. BN_init(&point->Y);
  257. BN_init(&point->Z);
  258. return 1;
  259. }
  260. /* Frees an EC_POINT. */
  261. void ec_GF2m_simple_point_finish(EC_POINT *point)
  262. {
  263. BN_free(&point->X);
  264. BN_free(&point->Y);
  265. BN_free(&point->Z);
  266. }
  267. /* Clears and frees an EC_POINT. */
  268. void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
  269. {
  270. BN_clear_free(&point->X);
  271. BN_clear_free(&point->Y);
  272. BN_clear_free(&point->Z);
  273. point->Z_is_one = 0;
  274. }
  275. /* Copy the contents of one EC_POINT into another. Assumes dest is initialized. */
  276. int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
  277. {
  278. if (!BN_copy(&dest->X, &src->X)) return 0;
  279. if (!BN_copy(&dest->Y, &src->Y)) return 0;
  280. if (!BN_copy(&dest->Z, &src->Z)) return 0;
  281. dest->Z_is_one = src->Z_is_one;
  282. return 1;
  283. }
  284. /* Set an EC_POINT to the point at infinity.
  285. * A point at infinity is represented by having Z=0.
  286. */
  287. int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  288. {
  289. point->Z_is_one = 0;
  290. BN_zero(&point->Z);
  291. return 1;
  292. }
  293. /* Set the coordinates of an EC_POINT using affine coordinates.
  294. * Note that the simple implementation only uses affine coordinates.
  295. */
  296. int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  297. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
  298. {
  299. int ret = 0;
  300. if (x == NULL || y == NULL)
  301. {
  302. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
  303. return 0;
  304. }
  305. if (!BN_copy(&point->X, x)) goto err;
  306. BN_set_sign(&point->X, 0);
  307. if (!BN_copy(&point->Y, y)) goto err;
  308. BN_set_sign(&point->Y, 0);
  309. if (!BN_copy(&point->Z, BN_value_one())) goto err;
  310. BN_set_sign(&point->Z, 0);
  311. point->Z_is_one = 1;
  312. ret = 1;
  313. err:
  314. return ret;
  315. }
  316. /* Gets the affine coordinates of an EC_POINT.
  317. * Note that the simple implementation only uses affine coordinates.
  318. */
  319. int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
  320. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  321. {
  322. int ret = 0;
  323. if (EC_POINT_is_at_infinity(group, point))
  324. {
  325. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
  326. return 0;
  327. }
  328. if (BN_cmp(&point->Z, BN_value_one()))
  329. {
  330. ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  331. return 0;
  332. }
  333. if (x != NULL)
  334. {
  335. if (!BN_copy(x, &point->X)) goto err;
  336. BN_set_sign(x, 0);
  337. }
  338. if (y != NULL)
  339. {
  340. if (!BN_copy(y, &point->Y)) goto err;
  341. BN_set_sign(y, 0);
  342. }
  343. ret = 1;
  344. err:
  345. return ret;
  346. }
  347. /* Include patented algorithms. */
  348. #include "ec2_smpt.c"
  349. /* Converts an EC_POINT to an octet string.
  350. * If buf is NULL, the encoded length will be returned.
  351. * If the length len of buf is smaller than required an error will be returned.
  352. *
  353. * The point compression section of this function is patented by Certicom Corp.
  354. * under US Patent 6,141,420. Point compression is disabled by default and can
  355. * be enabled by defining the preprocessor macro OPENSSL_EC_BIN_PT_COMP at
  356. * Configure-time.
  357. */
  358. size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
  359. unsigned char *buf, size_t len, BN_CTX *ctx)
  360. {
  361. size_t ret;
  362. BN_CTX *new_ctx = NULL;
  363. int used_ctx = 0;
  364. BIGNUM *x, *y, *yxi;
  365. size_t field_len, i, skip;
  366. #ifndef OPENSSL_EC_BIN_PT_COMP
  367. if ((form == POINT_CONVERSION_COMPRESSED) || (form == POINT_CONVERSION_HYBRID))
  368. {
  369. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_DISABLED);
  370. goto err;
  371. }
  372. #endif
  373. if ((form != POINT_CONVERSION_COMPRESSED)
  374. && (form != POINT_CONVERSION_UNCOMPRESSED)
  375. && (form != POINT_CONVERSION_HYBRID))
  376. {
  377. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  378. goto err;
  379. }
  380. if (EC_POINT_is_at_infinity(group, point))
  381. {
  382. /* encodes to a single 0 octet */
  383. if (buf != NULL)
  384. {
  385. if (len < 1)
  386. {
  387. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  388. return 0;
  389. }
  390. buf[0] = 0;
  391. }
  392. return 1;
  393. }
  394. /* ret := required output buffer length */
  395. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  396. ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  397. /* if 'buf' is NULL, just return required length */
  398. if (buf != NULL)
  399. {
  400. if (len < ret)
  401. {
  402. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  403. goto err;
  404. }
  405. if (ctx == NULL)
  406. {
  407. ctx = new_ctx = BN_CTX_new();
  408. if (ctx == NULL)
  409. return 0;
  410. }
  411. BN_CTX_start(ctx);
  412. used_ctx = 1;
  413. x = BN_CTX_get(ctx);
  414. y = BN_CTX_get(ctx);
  415. yxi = BN_CTX_get(ctx);
  416. if (yxi == NULL) goto err;
  417. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  418. buf[0] = form;
  419. #ifdef OPENSSL_EC_BIN_PT_COMP
  420. if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x))
  421. {
  422. if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
  423. if (BN_is_odd(yxi)) buf[0]++;
  424. }
  425. #endif
  426. i = 1;
  427. skip = field_len - BN_num_bytes(x);
  428. if (skip > field_len)
  429. {
  430. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  431. goto err;
  432. }
  433. while (skip > 0)
  434. {
  435. buf[i++] = 0;
  436. skip--;
  437. }
  438. skip = BN_bn2bin(x, buf + i);
  439. i += skip;
  440. if (i != 1 + field_len)
  441. {
  442. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  443. goto err;
  444. }
  445. if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
  446. {
  447. skip = field_len - BN_num_bytes(y);
  448. if (skip > field_len)
  449. {
  450. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  451. goto err;
  452. }
  453. while (skip > 0)
  454. {
  455. buf[i++] = 0;
  456. skip--;
  457. }
  458. skip = BN_bn2bin(y, buf + i);
  459. i += skip;
  460. }
  461. if (i != ret)
  462. {
  463. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  464. goto err;
  465. }
  466. }
  467. if (used_ctx)
  468. BN_CTX_end(ctx);
  469. if (new_ctx != NULL)
  470. BN_CTX_free(new_ctx);
  471. return ret;
  472. err:
  473. if (used_ctx)
  474. BN_CTX_end(ctx);
  475. if (new_ctx != NULL)
  476. BN_CTX_free(new_ctx);
  477. return 0;
  478. }
  479. /* Converts an octet string representation to an EC_POINT.
  480. * Note that the simple implementation only uses affine coordinates.
  481. */
  482. int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  483. const unsigned char *buf, size_t len, BN_CTX *ctx)
  484. {
  485. point_conversion_form_t form;
  486. int y_bit;
  487. BN_CTX *new_ctx = NULL;
  488. BIGNUM *x, *y, *yxi;
  489. size_t field_len, enc_len;
  490. int ret = 0;
  491. if (len == 0)
  492. {
  493. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  494. return 0;
  495. }
  496. form = buf[0];
  497. y_bit = form & 1;
  498. form = form & ~1U;
  499. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  500. && (form != POINT_CONVERSION_UNCOMPRESSED)
  501. && (form != POINT_CONVERSION_HYBRID))
  502. {
  503. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  504. return 0;
  505. }
  506. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
  507. {
  508. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  509. return 0;
  510. }
  511. if (form == 0)
  512. {
  513. if (len != 1)
  514. {
  515. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  516. return 0;
  517. }
  518. return EC_POINT_set_to_infinity(group, point);
  519. }
  520. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  521. enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  522. if (len != enc_len)
  523. {
  524. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  525. return 0;
  526. }
  527. if (ctx == NULL)
  528. {
  529. ctx = new_ctx = BN_CTX_new();
  530. if (ctx == NULL)
  531. return 0;
  532. }
  533. BN_CTX_start(ctx);
  534. x = BN_CTX_get(ctx);
  535. y = BN_CTX_get(ctx);
  536. yxi = BN_CTX_get(ctx);
  537. if (yxi == NULL) goto err;
  538. if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
  539. if (BN_ucmp(x, &group->field) >= 0)
  540. {
  541. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  542. goto err;
  543. }
  544. if (form == POINT_CONVERSION_COMPRESSED)
  545. {
  546. if (!EC_POINT_set_compressed_coordinates_GF2m(group, point, x, y_bit, ctx)) goto err;
  547. }
  548. else
  549. {
  550. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
  551. if (BN_ucmp(y, &group->field) >= 0)
  552. {
  553. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  554. goto err;
  555. }
  556. if (form == POINT_CONVERSION_HYBRID)
  557. {
  558. if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
  559. if (y_bit != BN_is_odd(yxi))
  560. {
  561. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  562. goto err;
  563. }
  564. }
  565. if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  566. }
  567. if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
  568. {
  569. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  570. goto err;
  571. }
  572. ret = 1;
  573. err:
  574. BN_CTX_end(ctx);
  575. if (new_ctx != NULL)
  576. BN_CTX_free(new_ctx);
  577. return ret;
  578. }
  579. /* Computes a + b and stores the result in r. r could be a or b, a could be b.
  580. * Uses algorithm A.10.2 of IEEE P1363.
  581. */
  582. int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  583. {
  584. BN_CTX *new_ctx = NULL;
  585. BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
  586. int ret = 0;
  587. if (EC_POINT_is_at_infinity(group, a))
  588. {
  589. if (!EC_POINT_copy(r, b)) return 0;
  590. return 1;
  591. }
  592. if (EC_POINT_is_at_infinity(group, b))
  593. {
  594. if (!EC_POINT_copy(r, a)) return 0;
  595. return 1;
  596. }
  597. if (ctx == NULL)
  598. {
  599. ctx = new_ctx = BN_CTX_new();
  600. if (ctx == NULL)
  601. return 0;
  602. }
  603. BN_CTX_start(ctx);
  604. x0 = BN_CTX_get(ctx);
  605. y0 = BN_CTX_get(ctx);
  606. x1 = BN_CTX_get(ctx);
  607. y1 = BN_CTX_get(ctx);
  608. x2 = BN_CTX_get(ctx);
  609. y2 = BN_CTX_get(ctx);
  610. s = BN_CTX_get(ctx);
  611. t = BN_CTX_get(ctx);
  612. if (t == NULL) goto err;
  613. if (a->Z_is_one)
  614. {
  615. if (!BN_copy(x0, &a->X)) goto err;
  616. if (!BN_copy(y0, &a->Y)) goto err;
  617. }
  618. else
  619. {
  620. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx)) goto err;
  621. }
  622. if (b->Z_is_one)
  623. {
  624. if (!BN_copy(x1, &b->X)) goto err;
  625. if (!BN_copy(y1, &b->Y)) goto err;
  626. }
  627. else
  628. {
  629. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx)) goto err;
  630. }
  631. if (BN_GF2m_cmp(x0, x1))
  632. {
  633. if (!BN_GF2m_add(t, x0, x1)) goto err;
  634. if (!BN_GF2m_add(s, y0, y1)) goto err;
  635. if (!group->meth->field_div(group, s, s, t, ctx)) goto err;
  636. if (!group->meth->field_sqr(group, x2, s, ctx)) goto err;
  637. if (!BN_GF2m_add(x2, x2, &group->a)) goto err;
  638. if (!BN_GF2m_add(x2, x2, s)) goto err;
  639. if (!BN_GF2m_add(x2, x2, t)) goto err;
  640. }
  641. else
  642. {
  643. if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1))
  644. {
  645. if (!EC_POINT_set_to_infinity(group, r)) goto err;
  646. ret = 1;
  647. goto err;
  648. }
  649. if (!group->meth->field_div(group, s, y1, x1, ctx)) goto err;
  650. if (!BN_GF2m_add(s, s, x1)) goto err;
  651. if (!group->meth->field_sqr(group, x2, s, ctx)) goto err;
  652. if (!BN_GF2m_add(x2, x2, s)) goto err;
  653. if (!BN_GF2m_add(x2, x2, &group->a)) goto err;
  654. }
  655. if (!BN_GF2m_add(y2, x1, x2)) goto err;
  656. if (!group->meth->field_mul(group, y2, y2, s, ctx)) goto err;
  657. if (!BN_GF2m_add(y2, y2, x2)) goto err;
  658. if (!BN_GF2m_add(y2, y2, y1)) goto err;
  659. if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx)) goto err;
  660. ret = 1;
  661. err:
  662. BN_CTX_end(ctx);
  663. if (new_ctx != NULL)
  664. BN_CTX_free(new_ctx);
  665. return ret;
  666. }
  667. /* Computes 2 * a and stores the result in r. r could be a.
  668. * Uses algorithm A.10.2 of IEEE P1363.
  669. */
  670. int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
  671. {
  672. return ec_GF2m_simple_add(group, r, a, a, ctx);
  673. }
  674. int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  675. {
  676. if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
  677. /* point is its own inverse */
  678. return 1;
  679. if (!EC_POINT_make_affine(group, point, ctx)) return 0;
  680. return BN_GF2m_add(&point->Y, &point->X, &point->Y);
  681. }
  682. /* Indicates whether the given point is the point at infinity. */
  683. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  684. {
  685. return BN_is_zero(&point->Z);
  686. }
  687. /* Determines whether the given EC_POINT is an actual point on the curve defined
  688. * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation:
  689. * y^2 + x*y = x^3 + a*x^2 + b.
  690. */
  691. int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
  692. {
  693. BN_CTX *new_ctx = NULL;
  694. BIGNUM *rh, *lh, *tmp1;
  695. int ret = -1;
  696. if (EC_POINT_is_at_infinity(group, point))
  697. return 1;
  698. /* only support affine coordinates */
  699. if (!point->Z_is_one) goto err;
  700. if (ctx == NULL)
  701. {
  702. ctx = new_ctx = BN_CTX_new();
  703. if (ctx == NULL)
  704. return -1;
  705. }
  706. BN_CTX_start(ctx);
  707. rh = BN_CTX_get(ctx);
  708. lh = BN_CTX_get(ctx);
  709. tmp1 = BN_CTX_get(ctx);
  710. if (tmp1 == NULL) goto err;
  711. /* We have a curve defined by a Weierstrass equation
  712. * y^2 + x*y = x^3 + a*x^2 + b.
  713. * To test this, we add up the right-hand side in 'rh'
  714. * and the left-hand side in 'lh'.
  715. */
  716. /* rh := X^3 */
  717. if (!group->meth->field_sqr(group, tmp1, &point->X, ctx)) goto err;
  718. if (!group->meth->field_mul(group, rh, tmp1, &point->X, ctx)) goto err;
  719. /* rh := rh + a*X^2 */
  720. if (!group->meth->field_mul(group, tmp1, tmp1, &group->a, ctx)) goto err;
  721. if (!BN_GF2m_add(rh, rh, tmp1)) goto err;
  722. /* rh := rh + b */
  723. if (!BN_GF2m_add(rh, rh, &group->b)) goto err;
  724. /* lh := Y^2 */
  725. if (!group->meth->field_sqr(group, lh, &point->Y, ctx)) goto err;
  726. /* lh := lh + x*y */
  727. if (!group->meth->field_mul(group, tmp1, &point->X, &point->Y, ctx)) goto err;
  728. if (!BN_GF2m_add(lh, lh, tmp1)) goto err;
  729. ret = (0 == BN_GF2m_cmp(lh, rh));
  730. err:
  731. if (ctx) BN_CTX_end(ctx);
  732. if (new_ctx) BN_CTX_free(new_ctx);
  733. return ret;
  734. }
  735. /* Indicates whether two points are equal.
  736. * Return values:
  737. * -1 error
  738. * 0 equal (in affine coordinates)
  739. * 1 not equal
  740. */
  741. int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  742. {
  743. BIGNUM *aX, *aY, *bX, *bY;
  744. BN_CTX *new_ctx = NULL;
  745. int ret = -1;
  746. if (EC_POINT_is_at_infinity(group, a))
  747. {
  748. return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
  749. }
  750. if (a->Z_is_one && b->Z_is_one)
  751. {
  752. return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
  753. }
  754. if (ctx == NULL)
  755. {
  756. ctx = new_ctx = BN_CTX_new();
  757. if (ctx == NULL)
  758. return -1;
  759. }
  760. BN_CTX_start(ctx);
  761. aX = BN_CTX_get(ctx);
  762. aY = BN_CTX_get(ctx);
  763. bX = BN_CTX_get(ctx);
  764. bY = BN_CTX_get(ctx);
  765. if (bY == NULL) goto err;
  766. if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx)) goto err;
  767. if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx)) goto err;
  768. ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
  769. err:
  770. if (ctx) BN_CTX_end(ctx);
  771. if (new_ctx) BN_CTX_free(new_ctx);
  772. return ret;
  773. }
  774. /* Forces the given EC_POINT to internally use affine coordinates. */
  775. int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  776. {
  777. BN_CTX *new_ctx = NULL;
  778. BIGNUM *x, *y;
  779. int ret = 0;
  780. if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
  781. return 1;
  782. if (ctx == NULL)
  783. {
  784. ctx = new_ctx = BN_CTX_new();
  785. if (ctx == NULL)
  786. return 0;
  787. }
  788. BN_CTX_start(ctx);
  789. x = BN_CTX_get(ctx);
  790. y = BN_CTX_get(ctx);
  791. if (y == NULL) goto err;
  792. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  793. if (!BN_copy(&point->X, x)) goto err;
  794. if (!BN_copy(&point->Y, y)) goto err;
  795. if (!BN_one(&point->Z)) goto err;
  796. ret = 1;
  797. err:
  798. if (ctx) BN_CTX_end(ctx);
  799. if (new_ctx) BN_CTX_free(new_ctx);
  800. return ret;
  801. }
  802. /* Forces each of the EC_POINTs in the given array to use affine coordinates. */
  803. int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
  804. {
  805. size_t i;
  806. for (i = 0; i < num; i++)
  807. {
  808. if (!group->meth->make_affine(group, points[i], ctx)) return 0;
  809. }
  810. return 1;
  811. }
  812. /* Wrapper to simple binary polynomial field multiplication implementation. */
  813. int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  814. {
  815. return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
  816. }
  817. /* Wrapper to simple binary polynomial field squaring implementation. */
  818. int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
  819. {
  820. return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
  821. }
  822. /* Wrapper to simple binary polynomial field division implementation. */
  823. int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  824. {
  825. return BN_GF2m_mod_div(r, a, b, &group->field, ctx);
  826. }