2
0

ec2_oct.c 12 KB

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