ecp_oct.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
  3. * for the OpenSSL project. Includes code written by Bodo Moeller for the
  4. * OpenSSL project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@openssl.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* ====================================================================
  60. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  61. * Portions of this software developed by SUN MICROSYSTEMS, INC.,
  62. * and contributed to the OpenSSL project.
  63. */
  64. #include <openssl/err.h>
  65. #include <openssl/symhacks.h>
  66. #include "ec_lcl.h"
  67. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
  68. EC_POINT *point,
  69. const BIGNUM *x_, int y_bit,
  70. BN_CTX *ctx)
  71. {
  72. BN_CTX *new_ctx = NULL;
  73. BIGNUM *tmp1, *tmp2, *x, *y;
  74. int ret = 0;
  75. /* clear error queue */
  76. ERR_clear_error();
  77. if (ctx == NULL) {
  78. ctx = new_ctx = BN_CTX_new();
  79. if (ctx == NULL)
  80. return 0;
  81. }
  82. y_bit = (y_bit != 0);
  83. BN_CTX_start(ctx);
  84. tmp1 = BN_CTX_get(ctx);
  85. tmp2 = BN_CTX_get(ctx);
  86. x = BN_CTX_get(ctx);
  87. y = BN_CTX_get(ctx);
  88. if (y == NULL)
  89. goto err;
  90. /*-
  91. * Recover y. We have a Weierstrass equation
  92. * y^2 = x^3 + a*x + b,
  93. * so y is one of the square roots of x^3 + a*x + b.
  94. */
  95. /* tmp1 := x^3 */
  96. if (!BN_nnmod(x, x_, group->field, ctx))
  97. goto err;
  98. if (group->meth->field_decode == 0) {
  99. /* field_{sqr,mul} work on standard representation */
  100. if (!group->meth->field_sqr(group, tmp2, x_, ctx))
  101. goto err;
  102. if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
  103. goto err;
  104. } else {
  105. if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
  106. goto err;
  107. if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
  108. goto err;
  109. }
  110. /* tmp1 := tmp1 + a*x */
  111. if (group->a_is_minus3) {
  112. if (!BN_mod_lshift1_quick(tmp2, x, group->field))
  113. goto err;
  114. if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
  115. goto err;
  116. if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
  117. goto err;
  118. } else {
  119. if (group->meth->field_decode) {
  120. if (!group->meth->field_decode(group, tmp2, group->a, ctx))
  121. goto err;
  122. if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
  123. goto err;
  124. } else {
  125. /* field_mul works on standard representation */
  126. if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
  127. goto err;
  128. }
  129. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
  130. goto err;
  131. }
  132. /* tmp1 := tmp1 + b */
  133. if (group->meth->field_decode) {
  134. if (!group->meth->field_decode(group, tmp2, group->b, ctx))
  135. goto err;
  136. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
  137. goto err;
  138. } else {
  139. if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
  140. goto err;
  141. }
  142. if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
  143. unsigned long err = ERR_peek_last_error();
  144. if (ERR_GET_LIB(err) == ERR_LIB_BN
  145. && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  146. ERR_clear_error();
  147. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  148. EC_R_INVALID_COMPRESSED_POINT);
  149. } else
  150. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  151. ERR_R_BN_LIB);
  152. goto err;
  153. }
  154. if (y_bit != BN_is_odd(y)) {
  155. if (BN_is_zero(y)) {
  156. int kron;
  157. kron = BN_kronecker(x, group->field, ctx);
  158. if (kron == -2)
  159. goto err;
  160. if (kron == 1)
  161. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  162. EC_R_INVALID_COMPRESSION_BIT);
  163. else
  164. /*
  165. * BN_mod_sqrt() should have cought this error (not a square)
  166. */
  167. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  168. EC_R_INVALID_COMPRESSED_POINT);
  169. goto err;
  170. }
  171. if (!BN_usub(y, group->field, y))
  172. goto err;
  173. }
  174. if (y_bit != BN_is_odd(y)) {
  175. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  176. ERR_R_INTERNAL_ERROR);
  177. goto err;
  178. }
  179. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
  180. goto err;
  181. ret = 1;
  182. err:
  183. BN_CTX_end(ctx);
  184. BN_CTX_free(new_ctx);
  185. return ret;
  186. }
  187. size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  188. point_conversion_form_t form,
  189. unsigned char *buf, size_t len, BN_CTX *ctx)
  190. {
  191. size_t ret;
  192. BN_CTX *new_ctx = NULL;
  193. int used_ctx = 0;
  194. BIGNUM *x, *y;
  195. size_t field_len, i, skip;
  196. if ((form != POINT_CONVERSION_COMPRESSED)
  197. && (form != POINT_CONVERSION_UNCOMPRESSED)
  198. && (form != POINT_CONVERSION_HYBRID)) {
  199. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  200. goto err;
  201. }
  202. if (EC_POINT_is_at_infinity(group, point)) {
  203. /* encodes to a single 0 octet */
  204. if (buf != NULL) {
  205. if (len < 1) {
  206. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  207. return 0;
  208. }
  209. buf[0] = 0;
  210. }
  211. return 1;
  212. }
  213. /* ret := required output buffer length */
  214. field_len = BN_num_bytes(group->field);
  215. ret =
  216. (form ==
  217. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  218. /* if 'buf' is NULL, just return required length */
  219. if (buf != NULL) {
  220. if (len < ret) {
  221. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  222. goto err;
  223. }
  224. if (ctx == NULL) {
  225. ctx = new_ctx = BN_CTX_new();
  226. if (ctx == NULL)
  227. return 0;
  228. }
  229. BN_CTX_start(ctx);
  230. used_ctx = 1;
  231. x = BN_CTX_get(ctx);
  232. y = BN_CTX_get(ctx);
  233. if (y == NULL)
  234. goto err;
  235. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
  236. goto err;
  237. if ((form == POINT_CONVERSION_COMPRESSED
  238. || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
  239. buf[0] = form + 1;
  240. else
  241. buf[0] = form;
  242. i = 1;
  243. skip = field_len - BN_num_bytes(x);
  244. if (skip > field_len) {
  245. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  246. goto err;
  247. }
  248. while (skip > 0) {
  249. buf[i++] = 0;
  250. skip--;
  251. }
  252. skip = BN_bn2bin(x, buf + i);
  253. i += skip;
  254. if (i != 1 + field_len) {
  255. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  256. goto err;
  257. }
  258. if (form == POINT_CONVERSION_UNCOMPRESSED
  259. || form == POINT_CONVERSION_HYBRID) {
  260. skip = field_len - BN_num_bytes(y);
  261. if (skip > field_len) {
  262. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  263. goto err;
  264. }
  265. while (skip > 0) {
  266. buf[i++] = 0;
  267. skip--;
  268. }
  269. skip = BN_bn2bin(y, buf + i);
  270. i += skip;
  271. }
  272. if (i != ret) {
  273. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  274. goto err;
  275. }
  276. }
  277. if (used_ctx)
  278. BN_CTX_end(ctx);
  279. BN_CTX_free(new_ctx);
  280. return ret;
  281. err:
  282. if (used_ctx)
  283. BN_CTX_end(ctx);
  284. BN_CTX_free(new_ctx);
  285. return 0;
  286. }
  287. int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  288. const unsigned char *buf, size_t len, BN_CTX *ctx)
  289. {
  290. point_conversion_form_t form;
  291. int y_bit;
  292. BN_CTX *new_ctx = NULL;
  293. BIGNUM *x, *y;
  294. size_t field_len, enc_len;
  295. int ret = 0;
  296. if (len == 0) {
  297. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  298. return 0;
  299. }
  300. form = buf[0];
  301. y_bit = form & 1;
  302. form = form & ~1U;
  303. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  304. && (form != POINT_CONVERSION_UNCOMPRESSED)
  305. && (form != POINT_CONVERSION_HYBRID)) {
  306. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  307. return 0;
  308. }
  309. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  310. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  311. return 0;
  312. }
  313. if (form == 0) {
  314. if (len != 1) {
  315. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  316. return 0;
  317. }
  318. return EC_POINT_set_to_infinity(group, point);
  319. }
  320. field_len = BN_num_bytes(group->field);
  321. enc_len =
  322. (form ==
  323. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  324. if (len != enc_len) {
  325. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  326. return 0;
  327. }
  328. if (ctx == NULL) {
  329. ctx = new_ctx = BN_CTX_new();
  330. if (ctx == NULL)
  331. return 0;
  332. }
  333. BN_CTX_start(ctx);
  334. x = BN_CTX_get(ctx);
  335. y = BN_CTX_get(ctx);
  336. if (y == NULL)
  337. goto err;
  338. if (!BN_bin2bn(buf + 1, field_len, x))
  339. goto err;
  340. if (BN_ucmp(x, group->field) >= 0) {
  341. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  342. goto err;
  343. }
  344. if (form == POINT_CONVERSION_COMPRESSED) {
  345. if (!EC_POINT_set_compressed_coordinates_GFp
  346. (group, point, x, y_bit, ctx))
  347. goto err;
  348. } else {
  349. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  350. goto err;
  351. if (BN_ucmp(y, group->field) >= 0) {
  352. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  353. goto err;
  354. }
  355. if (form == POINT_CONVERSION_HYBRID) {
  356. if (y_bit != BN_is_odd(y)) {
  357. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  358. goto err;
  359. }
  360. }
  361. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
  362. goto err;
  363. }
  364. /* test required by X9.62 */
  365. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  366. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  367. goto err;
  368. }
  369. ret = 1;
  370. err:
  371. BN_CTX_end(ctx);
  372. BN_CTX_free(new_ctx);
  373. return ret;
  374. }