Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_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_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_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, 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, ERR_R_INTERNAL_ERROR);
  137. goto err;
  138. }
  139. i += field_len;
  140. }
  141. if (i != ret) {
  142. OPENSSL_PUT_ERROR(EC, 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_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_R_INVALID_ENCODING);
  177. return 0;
  178. }
  179. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  180. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  181. return 0;
  182. }
  183. if (form == 0) {
  184. if (len != 1) {
  185. OPENSSL_PUT_ERROR(EC, 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_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_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_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_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, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  248. return 0;
  249. }
  250. if (group->meth != point->meth) {
  251. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  252. return 0;
  253. }
  254. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  255. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  256. }
  257. return group->meth->oct2point(group, point, buf, len, ctx);
  258. }
  259. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  260. point_conversion_form_t form, uint8_t *buf,
  261. size_t len, BN_CTX *ctx) {
  262. if (group->meth->point2oct == 0 &&
  263. !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  264. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  265. return 0;
  266. }
  267. if (group->meth != point->meth) {
  268. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  269. return 0;
  270. }
  271. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  272. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  273. }
  274. return group->meth->point2oct(group, point, form, buf, len, ctx);
  275. }
  276. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
  277. EC_POINT *point, const BIGNUM *x_,
  278. int y_bit, BN_CTX *ctx) {
  279. BN_CTX *new_ctx = NULL;
  280. BIGNUM *tmp1, *tmp2, *x, *y;
  281. int ret = 0;
  282. ERR_clear_error();
  283. if (ctx == NULL) {
  284. ctx = new_ctx = BN_CTX_new();
  285. if (ctx == NULL) {
  286. return 0;
  287. }
  288. }
  289. y_bit = (y_bit != 0);
  290. BN_CTX_start(ctx);
  291. tmp1 = BN_CTX_get(ctx);
  292. tmp2 = BN_CTX_get(ctx);
  293. x = BN_CTX_get(ctx);
  294. y = BN_CTX_get(ctx);
  295. if (y == NULL) {
  296. goto err;
  297. }
  298. /* Recover y. We have a Weierstrass equation
  299. * y^2 = x^3 + a*x + b,
  300. * so y is one of the square roots of x^3 + a*x + b. */
  301. /* tmp1 := x^3 */
  302. if (!BN_nnmod(x, x_, &group->field, ctx)) {
  303. goto err;
  304. }
  305. if (group->meth->field_decode == 0) {
  306. /* field_{sqr,mul} work on standard representation */
  307. if (!group->meth->field_sqr(group, tmp2, x_, ctx) ||
  308. !group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) {
  309. goto err;
  310. }
  311. } else {
  312. if (!BN_mod_sqr(tmp2, x_, &group->field, ctx) ||
  313. !BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) {
  314. goto err;
  315. }
  316. }
  317. /* tmp1 := tmp1 + a*x */
  318. if (group->a_is_minus3) {
  319. if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
  320. !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
  321. !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
  322. goto err;
  323. }
  324. } else {
  325. if (group->meth->field_decode) {
  326. if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
  327. !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
  328. goto err;
  329. }
  330. } else {
  331. /* field_mul works on standard representation */
  332. if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
  333. goto err;
  334. }
  335. }
  336. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  337. goto err;
  338. }
  339. }
  340. /* tmp1 := tmp1 + b */
  341. if (group->meth->field_decode) {
  342. if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
  343. !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  344. goto err;
  345. }
  346. } else {
  347. if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
  348. goto err;
  349. }
  350. }
  351. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
  352. unsigned long err = ERR_peek_last_error();
  353. if (ERR_GET_LIB(err) == ERR_LIB_BN &&
  354. ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  355. ERR_clear_error();
  356. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  357. } else {
  358. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  359. }
  360. goto err;
  361. }
  362. if (y_bit != BN_is_odd(y)) {
  363. if (BN_is_zero(y)) {
  364. int kron;
  365. kron = BN_kronecker(x, &group->field, ctx);
  366. if (kron == -2) {
  367. goto err;
  368. }
  369. if (kron == 1) {
  370. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
  371. } else {
  372. /* BN_mod_sqrt() should have cought this error (not a square) */
  373. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  374. }
  375. goto err;
  376. }
  377. if (!BN_usub(y, &group->field, y)) {
  378. goto err;
  379. }
  380. }
  381. if (y_bit != BN_is_odd(y)) {
  382. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  383. goto err;
  384. }
  385. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  386. goto err;
  387. }
  388. ret = 1;
  389. err:
  390. BN_CTX_end(ctx);
  391. BN_CTX_free(new_ctx);
  392. return ret;
  393. }
  394. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  395. EC_POINT *point, const BIGNUM *x,
  396. int y_bit, BN_CTX *ctx) {
  397. if (group->meth->point_set_compressed_coordinates == 0 &&
  398. !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  399. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  400. return 0;
  401. }
  402. if (group->meth != point->meth) {
  403. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  404. return 0;
  405. }
  406. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  407. return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit,
  408. ctx);
  409. }
  410. return group->meth->point_set_compressed_coordinates(group, point, x, y_bit,
  411. ctx);
  412. }