ecp_oct.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. if (new_ctx != NULL)
  186. BN_CTX_free(new_ctx);
  187. return ret;
  188. }
  189. size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  190. point_conversion_form_t form,
  191. unsigned char *buf, size_t len, BN_CTX *ctx)
  192. {
  193. size_t ret;
  194. BN_CTX *new_ctx = NULL;
  195. int used_ctx = 0;
  196. BIGNUM *x, *y;
  197. size_t field_len, i, skip;
  198. if ((form != POINT_CONVERSION_COMPRESSED)
  199. && (form != POINT_CONVERSION_UNCOMPRESSED)
  200. && (form != POINT_CONVERSION_HYBRID)) {
  201. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  202. goto err;
  203. }
  204. if (EC_POINT_is_at_infinity(group, point)) {
  205. /* encodes to a single 0 octet */
  206. if (buf != NULL) {
  207. if (len < 1) {
  208. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  209. return 0;
  210. }
  211. buf[0] = 0;
  212. }
  213. return 1;
  214. }
  215. /* ret := required output buffer length */
  216. field_len = BN_num_bytes(&group->field);
  217. ret =
  218. (form ==
  219. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  220. /* if 'buf' is NULL, just return required length */
  221. if (buf != NULL) {
  222. if (len < ret) {
  223. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  224. goto err;
  225. }
  226. if (ctx == NULL) {
  227. ctx = new_ctx = BN_CTX_new();
  228. if (ctx == NULL)
  229. return 0;
  230. }
  231. BN_CTX_start(ctx);
  232. used_ctx = 1;
  233. x = BN_CTX_get(ctx);
  234. y = BN_CTX_get(ctx);
  235. if (y == NULL)
  236. goto err;
  237. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
  238. goto err;
  239. if ((form == POINT_CONVERSION_COMPRESSED
  240. || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
  241. buf[0] = form + 1;
  242. else
  243. buf[0] = form;
  244. i = 1;
  245. skip = field_len - BN_num_bytes(x);
  246. if (skip > field_len) {
  247. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  248. goto err;
  249. }
  250. while (skip > 0) {
  251. buf[i++] = 0;
  252. skip--;
  253. }
  254. skip = BN_bn2bin(x, buf + i);
  255. i += skip;
  256. if (i != 1 + field_len) {
  257. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  258. goto err;
  259. }
  260. if (form == POINT_CONVERSION_UNCOMPRESSED
  261. || form == POINT_CONVERSION_HYBRID) {
  262. skip = field_len - BN_num_bytes(y);
  263. if (skip > field_len) {
  264. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  265. goto err;
  266. }
  267. while (skip > 0) {
  268. buf[i++] = 0;
  269. skip--;
  270. }
  271. skip = BN_bn2bin(y, buf + i);
  272. i += skip;
  273. }
  274. if (i != ret) {
  275. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  276. goto err;
  277. }
  278. }
  279. if (used_ctx)
  280. BN_CTX_end(ctx);
  281. if (new_ctx != NULL)
  282. BN_CTX_free(new_ctx);
  283. return ret;
  284. err:
  285. if (used_ctx)
  286. BN_CTX_end(ctx);
  287. if (new_ctx != NULL)
  288. BN_CTX_free(new_ctx);
  289. return 0;
  290. }
  291. int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  292. const unsigned char *buf, size_t len, BN_CTX *ctx)
  293. {
  294. point_conversion_form_t form;
  295. int y_bit;
  296. BN_CTX *new_ctx = NULL;
  297. BIGNUM *x, *y;
  298. size_t field_len, enc_len;
  299. int ret = 0;
  300. if (len == 0) {
  301. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  302. return 0;
  303. }
  304. form = buf[0];
  305. y_bit = form & 1;
  306. form = form & ~1U;
  307. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  308. && (form != POINT_CONVERSION_UNCOMPRESSED)
  309. && (form != POINT_CONVERSION_HYBRID)) {
  310. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  311. return 0;
  312. }
  313. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  314. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  315. return 0;
  316. }
  317. if (form == 0) {
  318. if (len != 1) {
  319. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  320. return 0;
  321. }
  322. return EC_POINT_set_to_infinity(group, point);
  323. }
  324. field_len = BN_num_bytes(&group->field);
  325. enc_len =
  326. (form ==
  327. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  328. if (len != enc_len) {
  329. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  330. return 0;
  331. }
  332. if (ctx == NULL) {
  333. ctx = new_ctx = BN_CTX_new();
  334. if (ctx == NULL)
  335. return 0;
  336. }
  337. BN_CTX_start(ctx);
  338. x = BN_CTX_get(ctx);
  339. y = BN_CTX_get(ctx);
  340. if (y == NULL)
  341. goto err;
  342. if (!BN_bin2bn(buf + 1, field_len, x))
  343. goto err;
  344. if (BN_ucmp(x, &group->field) >= 0) {
  345. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  346. goto err;
  347. }
  348. if (form == POINT_CONVERSION_COMPRESSED) {
  349. if (!EC_POINT_set_compressed_coordinates_GFp
  350. (group, point, x, y_bit, ctx))
  351. goto err;
  352. } else {
  353. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  354. goto err;
  355. if (BN_ucmp(y, &group->field) >= 0) {
  356. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  357. goto err;
  358. }
  359. if (form == POINT_CONVERSION_HYBRID) {
  360. if (y_bit != BN_is_odd(y)) {
  361. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  362. goto err;
  363. }
  364. }
  365. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
  366. goto err;
  367. }
  368. /* test required by X9.62 */
  369. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  370. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  371. goto err;
  372. }
  373. ret = 1;
  374. err:
  375. BN_CTX_end(ctx);
  376. if (new_ctx != NULL)
  377. BN_CTX_free(new_ctx);
  378. return ret;
  379. }