Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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