Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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