Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

486 linhas
14 KiB

  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. if (new_ctx != NULL) {
  150. BN_CTX_free(new_ctx);
  151. }
  152. return ret;
  153. err:
  154. if (used_ctx) {
  155. BN_CTX_end(ctx);
  156. }
  157. if (new_ctx != NULL) {
  158. BN_CTX_free(new_ctx);
  159. }
  160. return 0;
  161. }
  162. static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  163. const uint8_t *buf, size_t len,
  164. BN_CTX *ctx) {
  165. point_conversion_form_t form;
  166. int y_bit;
  167. BN_CTX *new_ctx = NULL;
  168. BIGNUM *x, *y;
  169. size_t field_len, enc_len;
  170. int ret = 0;
  171. if (len == 0) {
  172. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_BUFFER_TOO_SMALL);
  173. return 0;
  174. }
  175. form = buf[0];
  176. y_bit = form & 1;
  177. form = form & ~1U;
  178. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) &&
  179. (form != POINT_CONVERSION_UNCOMPRESSED)) {
  180. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  181. return 0;
  182. }
  183. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  184. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  185. return 0;
  186. }
  187. if (form == 0) {
  188. if (len != 1) {
  189. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  190. return 0;
  191. }
  192. return EC_POINT_set_to_infinity(group, point);
  193. }
  194. field_len = BN_num_bytes(&group->field);
  195. enc_len =
  196. (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  197. if (len != enc_len) {
  198. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  199. return 0;
  200. }
  201. if (ctx == NULL) {
  202. ctx = new_ctx = BN_CTX_new();
  203. if (ctx == NULL) {
  204. return 0;
  205. }
  206. }
  207. BN_CTX_start(ctx);
  208. x = BN_CTX_get(ctx);
  209. y = BN_CTX_get(ctx);
  210. if (y == NULL) {
  211. goto err;
  212. }
  213. if (!BN_bin2bn(buf + 1, field_len, x)) {
  214. goto err;
  215. }
  216. if (BN_ucmp(x, &group->field) >= 0) {
  217. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  218. goto err;
  219. }
  220. if (form == POINT_CONVERSION_COMPRESSED) {
  221. if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
  222. goto err;
  223. }
  224. } else {
  225. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
  226. goto err;
  227. }
  228. if (BN_ucmp(y, &group->field) >= 0) {
  229. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
  230. goto err;
  231. }
  232. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  233. goto err;
  234. }
  235. }
  236. /* test required by X9.62 */
  237. if (!EC_POINT_is_on_curve(group, point, ctx)) {
  238. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_POINT_IS_NOT_ON_CURVE);
  239. goto err;
  240. }
  241. ret = 1;
  242. err:
  243. BN_CTX_end(ctx);
  244. if (new_ctx != NULL) {
  245. BN_CTX_free(new_ctx);
  246. }
  247. return ret;
  248. }
  249. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  250. const uint8_t *buf, size_t len, BN_CTX *ctx) {
  251. if (group->meth->oct2point == 0 &&
  252. !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  253. OPENSSL_PUT_ERROR(EC, EC_POINT_oct2point,
  254. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  255. return 0;
  256. }
  257. if (group->meth != point->meth) {
  258. OPENSSL_PUT_ERROR(EC, EC_POINT_oct2point, EC_R_INCOMPATIBLE_OBJECTS);
  259. return 0;
  260. }
  261. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  262. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  263. }
  264. return group->meth->oct2point(group, point, buf, len, ctx);
  265. }
  266. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  267. point_conversion_form_t form, uint8_t *buf,
  268. size_t len, BN_CTX *ctx) {
  269. if (group->meth->point2oct == 0 &&
  270. !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  271. OPENSSL_PUT_ERROR(EC, EC_POINT_point2oct,
  272. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  273. return 0;
  274. }
  275. if (group->meth != point->meth) {
  276. OPENSSL_PUT_ERROR(EC, EC_POINT_point2oct, EC_R_INCOMPATIBLE_OBJECTS);
  277. return 0;
  278. }
  279. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  280. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  281. }
  282. return group->meth->point2oct(group, point, form, buf, len, ctx);
  283. }
  284. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
  285. EC_POINT *point, const BIGNUM *x_,
  286. int y_bit, BN_CTX *ctx) {
  287. BN_CTX *new_ctx = NULL;
  288. BIGNUM *tmp1, *tmp2, *x, *y;
  289. int ret = 0;
  290. ERR_clear_error();
  291. if (ctx == NULL) {
  292. ctx = new_ctx = BN_CTX_new();
  293. if (ctx == NULL) {
  294. return 0;
  295. }
  296. }
  297. y_bit = (y_bit != 0);
  298. BN_CTX_start(ctx);
  299. tmp1 = BN_CTX_get(ctx);
  300. tmp2 = BN_CTX_get(ctx);
  301. x = BN_CTX_get(ctx);
  302. y = BN_CTX_get(ctx);
  303. if (y == NULL) {
  304. goto err;
  305. }
  306. /* Recover y. We have a Weierstrass equation
  307. * y^2 = x^3 + a*x + b,
  308. * so y is one of the square roots of x^3 + a*x + b. */
  309. /* tmp1 := x^3 */
  310. if (!BN_nnmod(x, x_, &group->field, ctx)) {
  311. goto err;
  312. }
  313. if (group->meth->field_decode == 0) {
  314. /* field_{sqr,mul} work on standard representation */
  315. if (!group->meth->field_sqr(group, tmp2, x_, ctx) ||
  316. !group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) {
  317. goto err;
  318. }
  319. } else {
  320. if (!BN_mod_sqr(tmp2, x_, &group->field, ctx) ||
  321. !BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) {
  322. goto err;
  323. }
  324. }
  325. /* tmp1 := tmp1 + a*x */
  326. if (group->a_is_minus3) {
  327. if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
  328. !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
  329. !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
  330. goto err;
  331. }
  332. } else {
  333. if (group->meth->field_decode) {
  334. if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
  335. !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
  336. goto err;
  337. }
  338. } else {
  339. /* field_mul works on standard representation */
  340. if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
  341. goto err;
  342. }
  343. }
  344. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  345. goto err;
  346. }
  347. }
  348. /* tmp1 := tmp1 + b */
  349. if (group->meth->field_decode) {
  350. if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
  351. !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  352. goto err;
  353. }
  354. } else {
  355. if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
  356. goto err;
  357. }
  358. }
  359. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
  360. unsigned long err = ERR_peek_last_error();
  361. if (ERR_GET_LIB(err) == ERR_LIB_BN &&
  362. ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  363. ERR_clear_error();
  364. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates, EC_R_INVALID_COMPRESSED_POINT);
  365. } else {
  366. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates, ERR_R_BN_LIB);
  367. }
  368. goto err;
  369. }
  370. if (y_bit != BN_is_odd(y)) {
  371. if (BN_is_zero(y)) {
  372. int kron;
  373. kron = BN_kronecker(x, &group->field, ctx);
  374. if (kron == -2) {
  375. goto err;
  376. }
  377. if (kron == 1) {
  378. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
  379. EC_R_INVALID_COMPRESSION_BIT);
  380. } else {
  381. /* BN_mod_sqrt() should have cought this error (not a square) */
  382. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
  383. EC_R_INVALID_COMPRESSED_POINT);
  384. }
  385. goto err;
  386. }
  387. if (!BN_usub(y, &group->field, y)) {
  388. goto err;
  389. }
  390. }
  391. if (y_bit != BN_is_odd(y)) {
  392. OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
  393. ERR_R_INTERNAL_ERROR);
  394. goto err;
  395. }
  396. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  397. goto err;
  398. }
  399. ret = 1;
  400. err:
  401. BN_CTX_end(ctx);
  402. if (new_ctx != NULL) {
  403. BN_CTX_free(new_ctx);
  404. }
  405. return ret;
  406. }
  407. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  408. EC_POINT *point, const BIGNUM *x,
  409. int y_bit, BN_CTX *ctx) {
  410. if (group->meth->point_set_compressed_coordinates == 0 &&
  411. !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  412. OPENSSL_PUT_ERROR(EC, EC_POINT_set_compressed_coordinates_GFp,
  413. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  414. return 0;
  415. }
  416. if (group->meth != point->meth) {
  417. OPENSSL_PUT_ERROR(EC, EC_POINT_set_compressed_coordinates_GFp,
  418. EC_R_INCOMPATIBLE_OBJECTS);
  419. return 0;
  420. }
  421. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  422. return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit,
  423. ctx);
  424. }
  425. return group->meth->point_set_compressed_coordinates(group, point, x, y_bit,
  426. ctx);
  427. }