ecp_oct.c 13 KB

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