ec2_oct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* crypto/ec/ec2_oct.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. /* Calculates and sets the affine coordinates of an EC_POINT from the given
  73. * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
  74. * Note that the simple implementation only uses affine coordinates.
  75. *
  76. * The method is from the following publication:
  77. *
  78. * Harper, Menezes, Vanstone:
  79. * "Public-Key Cryptosystems with Very Small Key Lengths",
  80. * EUROCRYPT '92, Springer-Verlag LNCS 658,
  81. * published February 1993
  82. *
  83. * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
  84. * the same method, but claim no priority date earlier than July 29, 1994
  85. * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
  86. */
  87. int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
  88. const BIGNUM *x_, int y_bit, BN_CTX *ctx)
  89. {
  90. BN_CTX *new_ctx = NULL;
  91. BIGNUM *tmp, *x, *y, *z;
  92. int ret = 0, z0;
  93. /* clear error queue */
  94. ERR_clear_error();
  95. if (ctx == NULL)
  96. {
  97. ctx = new_ctx = BN_CTX_new();
  98. if (ctx == NULL)
  99. return 0;
  100. }
  101. y_bit = (y_bit != 0) ? 1 : 0;
  102. BN_CTX_start(ctx);
  103. tmp = BN_CTX_get(ctx);
  104. x = BN_CTX_get(ctx);
  105. y = BN_CTX_get(ctx);
  106. z = BN_CTX_get(ctx);
  107. if (z == NULL) goto err;
  108. if (!BN_GF2m_mod_arr(x, x_, group->poly)) goto err;
  109. if (BN_is_zero(x))
  110. {
  111. if (!BN_GF2m_mod_sqrt_arr(y, &group->b, group->poly, ctx)) goto err;
  112. }
  113. else
  114. {
  115. if (!group->meth->field_sqr(group, tmp, x, ctx)) goto err;
  116. if (!group->meth->field_div(group, tmp, &group->b, tmp, ctx)) goto err;
  117. if (!BN_GF2m_add(tmp, &group->a, tmp)) goto err;
  118. if (!BN_GF2m_add(tmp, x, tmp)) goto err;
  119. if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx))
  120. {
  121. unsigned long err = ERR_peek_last_error();
  122. if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NO_SOLUTION)
  123. {
  124. ERR_clear_error();
  125. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
  126. }
  127. else
  128. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
  129. goto err;
  130. }
  131. z0 = (BN_is_odd(z)) ? 1 : 0;
  132. if (!group->meth->field_mul(group, y, x, z, ctx)) goto err;
  133. if (z0 != y_bit)
  134. {
  135. if (!BN_GF2m_add(y, y, x)) goto err;
  136. }
  137. }
  138. if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  139. ret = 1;
  140. err:
  141. BN_CTX_end(ctx);
  142. if (new_ctx != NULL)
  143. BN_CTX_free(new_ctx);
  144. return ret;
  145. }
  146. /* Converts an EC_POINT to an octet string.
  147. * If buf is NULL, the encoded length will be returned.
  148. * If the length len of buf is smaller than required an error will be returned.
  149. */
  150. size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
  151. unsigned char *buf, size_t len, BN_CTX *ctx)
  152. {
  153. size_t ret;
  154. BN_CTX *new_ctx = NULL;
  155. int used_ctx = 0;
  156. BIGNUM *x, *y, *yxi;
  157. size_t field_len, i, skip;
  158. if ((form != POINT_CONVERSION_COMPRESSED)
  159. && (form != POINT_CONVERSION_UNCOMPRESSED)
  160. && (form != POINT_CONVERSION_HYBRID))
  161. {
  162. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  163. goto err;
  164. }
  165. if (EC_POINT_is_at_infinity(group, point))
  166. {
  167. /* encodes to a single 0 octet */
  168. if (buf != NULL)
  169. {
  170. if (len < 1)
  171. {
  172. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  173. return 0;
  174. }
  175. buf[0] = 0;
  176. }
  177. return 1;
  178. }
  179. /* ret := required output buffer length */
  180. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  181. ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  182. /* if 'buf' is NULL, just return required length */
  183. if (buf != NULL)
  184. {
  185. if (len < ret)
  186. {
  187. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  188. goto err;
  189. }
  190. if (ctx == NULL)
  191. {
  192. ctx = new_ctx = BN_CTX_new();
  193. if (ctx == NULL)
  194. return 0;
  195. }
  196. BN_CTX_start(ctx);
  197. used_ctx = 1;
  198. x = BN_CTX_get(ctx);
  199. y = BN_CTX_get(ctx);
  200. yxi = BN_CTX_get(ctx);
  201. if (yxi == NULL) goto err;
  202. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  203. buf[0] = form;
  204. if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x))
  205. {
  206. if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
  207. if (BN_is_odd(yxi)) buf[0]++;
  208. }
  209. i = 1;
  210. skip = field_len - BN_num_bytes(x);
  211. if (skip > field_len)
  212. {
  213. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  214. goto err;
  215. }
  216. while (skip > 0)
  217. {
  218. buf[i++] = 0;
  219. skip--;
  220. }
  221. skip = BN_bn2bin(x, buf + i);
  222. i += skip;
  223. if (i != 1 + field_len)
  224. {
  225. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  226. goto err;
  227. }
  228. if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
  229. {
  230. skip = field_len - BN_num_bytes(y);
  231. if (skip > field_len)
  232. {
  233. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  234. goto err;
  235. }
  236. while (skip > 0)
  237. {
  238. buf[i++] = 0;
  239. skip--;
  240. }
  241. skip = BN_bn2bin(y, buf + i);
  242. i += skip;
  243. }
  244. if (i != ret)
  245. {
  246. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  247. goto err;
  248. }
  249. }
  250. if (used_ctx)
  251. BN_CTX_end(ctx);
  252. if (new_ctx != NULL)
  253. BN_CTX_free(new_ctx);
  254. return ret;
  255. err:
  256. if (used_ctx)
  257. BN_CTX_end(ctx);
  258. if (new_ctx != NULL)
  259. BN_CTX_free(new_ctx);
  260. return 0;
  261. }
  262. /* Converts an octet string representation to an EC_POINT.
  263. * Note that the simple implementation only uses affine coordinates.
  264. */
  265. int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  266. const unsigned char *buf, size_t len, BN_CTX *ctx)
  267. {
  268. point_conversion_form_t form;
  269. int y_bit;
  270. BN_CTX *new_ctx = NULL;
  271. BIGNUM *x, *y, *yxi;
  272. size_t field_len, enc_len;
  273. int ret = 0;
  274. if (len == 0)
  275. {
  276. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  277. return 0;
  278. }
  279. form = buf[0];
  280. y_bit = form & 1;
  281. form = form & ~1U;
  282. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  283. && (form != POINT_CONVERSION_UNCOMPRESSED)
  284. && (form != POINT_CONVERSION_HYBRID))
  285. {
  286. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  287. return 0;
  288. }
  289. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
  290. {
  291. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  292. return 0;
  293. }
  294. if (form == 0)
  295. {
  296. if (len != 1)
  297. {
  298. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  299. return 0;
  300. }
  301. return EC_POINT_set_to_infinity(group, point);
  302. }
  303. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  304. enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  305. if (len != enc_len)
  306. {
  307. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  308. return 0;
  309. }
  310. if (ctx == NULL)
  311. {
  312. ctx = new_ctx = BN_CTX_new();
  313. if (ctx == NULL)
  314. return 0;
  315. }
  316. BN_CTX_start(ctx);
  317. x = BN_CTX_get(ctx);
  318. y = BN_CTX_get(ctx);
  319. yxi = BN_CTX_get(ctx);
  320. if (yxi == NULL) goto err;
  321. if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
  322. if (BN_ucmp(x, &group->field) >= 0)
  323. {
  324. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  325. goto err;
  326. }
  327. if (form == POINT_CONVERSION_COMPRESSED)
  328. {
  329. if (!EC_POINT_set_compressed_coordinates_GF2m(group, point, x, y_bit, ctx)) goto err;
  330. }
  331. else
  332. {
  333. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
  334. if (BN_ucmp(y, &group->field) >= 0)
  335. {
  336. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  337. goto err;
  338. }
  339. if (form == POINT_CONVERSION_HYBRID)
  340. {
  341. if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
  342. if (y_bit != BN_is_odd(yxi))
  343. {
  344. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  345. goto err;
  346. }
  347. }
  348. if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
  349. }
  350. if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
  351. {
  352. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  353. goto err;
  354. }
  355. ret = 1;
  356. err:
  357. BN_CTX_end(ctx);
  358. if (new_ctx != NULL)
  359. BN_CTX_free(new_ctx);
  360. return ret;
  361. }
  362. #endif