You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

417 lines
12 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_R_INVALID_FORM);
  83. goto err;
  84. }
  85. if (EC_POINT_is_at_infinity(group, point)) {
  86. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  87. goto err;
  88. }
  89. /* ret := required output buffer length */
  90. field_len = BN_num_bytes(&group->field);
  91. ret =
  92. (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  93. /* if 'buf' is NULL, just return required length */
  94. if (buf != NULL) {
  95. if (len < ret) {
  96. OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
  97. goto err;
  98. }
  99. if (ctx == NULL) {
  100. ctx = new_ctx = BN_CTX_new();
  101. if (ctx == NULL) {
  102. goto err;
  103. }
  104. }
  105. BN_CTX_start(ctx);
  106. used_ctx = 1;
  107. x = BN_CTX_get(ctx);
  108. y = BN_CTX_get(ctx);
  109. if (y == NULL) {
  110. goto err;
  111. }
  112. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) {
  113. goto err;
  114. }
  115. if ((form == POINT_CONVERSION_COMPRESSED) &&
  116. BN_is_odd(y)) {
  117. buf[0] = form + 1;
  118. } else {
  119. buf[0] = form;
  120. }
  121. i = 1;
  122. if (!BN_bn2bin_padded(buf + i, field_len, x)) {
  123. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  124. goto err;
  125. }
  126. i += field_len;
  127. if (form == POINT_CONVERSION_UNCOMPRESSED) {
  128. if (!BN_bn2bin_padded(buf + i, field_len, y)) {
  129. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  130. goto err;
  131. }
  132. i += field_len;
  133. }
  134. if (i != ret) {
  135. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  136. goto err;
  137. }
  138. }
  139. if (used_ctx) {
  140. BN_CTX_end(ctx);
  141. }
  142. BN_CTX_free(new_ctx);
  143. return ret;
  144. err:
  145. if (used_ctx) {
  146. BN_CTX_end(ctx);
  147. }
  148. BN_CTX_free(new_ctx);
  149. return 0;
  150. }
  151. static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  152. const uint8_t *buf, size_t len,
  153. BN_CTX *ctx) {
  154. point_conversion_form_t form;
  155. int y_bit;
  156. BN_CTX *new_ctx = NULL;
  157. BIGNUM *x, *y;
  158. size_t field_len, enc_len;
  159. int ret = 0;
  160. if (len == 0) {
  161. OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
  162. return 0;
  163. }
  164. form = buf[0];
  165. y_bit = form & 1;
  166. form = form & ~1U;
  167. if ((form != POINT_CONVERSION_COMPRESSED &&
  168. form != POINT_CONVERSION_UNCOMPRESSED) ||
  169. (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
  170. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  171. return 0;
  172. }
  173. field_len = BN_num_bytes(&group->field);
  174. enc_len =
  175. (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  176. if (len != enc_len) {
  177. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  178. return 0;
  179. }
  180. if (ctx == NULL) {
  181. ctx = new_ctx = BN_CTX_new();
  182. if (ctx == NULL) {
  183. return 0;
  184. }
  185. }
  186. BN_CTX_start(ctx);
  187. x = BN_CTX_get(ctx);
  188. y = BN_CTX_get(ctx);
  189. if (x == NULL || y == NULL) {
  190. goto err;
  191. }
  192. if (!BN_bin2bn(buf + 1, field_len, x)) {
  193. goto err;
  194. }
  195. if (BN_ucmp(x, &group->field) >= 0) {
  196. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  197. goto err;
  198. }
  199. if (form == POINT_CONVERSION_COMPRESSED) {
  200. if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
  201. goto err;
  202. }
  203. } else {
  204. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
  205. goto err;
  206. }
  207. if (BN_ucmp(y, &group->field) >= 0) {
  208. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  209. goto err;
  210. }
  211. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  212. goto err;
  213. }
  214. }
  215. ret = 1;
  216. err:
  217. BN_CTX_end(ctx);
  218. BN_CTX_free(new_ctx);
  219. return ret;
  220. }
  221. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  222. const uint8_t *buf, size_t len, BN_CTX *ctx) {
  223. if (group->meth != point->meth) {
  224. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  225. return 0;
  226. }
  227. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  228. }
  229. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  230. point_conversion_form_t form, uint8_t *buf,
  231. size_t len, BN_CTX *ctx) {
  232. if (group->meth != point->meth) {
  233. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  234. return 0;
  235. }
  236. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  237. }
  238. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
  239. EC_POINT *point, const BIGNUM *x_,
  240. int y_bit, BN_CTX *ctx) {
  241. BN_CTX *new_ctx = NULL;
  242. BIGNUM *tmp1, *tmp2, *x, *y;
  243. int ret = 0;
  244. ERR_clear_error();
  245. if (ctx == NULL) {
  246. ctx = new_ctx = BN_CTX_new();
  247. if (ctx == NULL) {
  248. return 0;
  249. }
  250. }
  251. y_bit = (y_bit != 0);
  252. BN_CTX_start(ctx);
  253. tmp1 = BN_CTX_get(ctx);
  254. tmp2 = BN_CTX_get(ctx);
  255. x = BN_CTX_get(ctx);
  256. y = BN_CTX_get(ctx);
  257. if (y == NULL) {
  258. goto err;
  259. }
  260. /* Recover y. We have a Weierstrass equation
  261. * y^2 = x^3 + a*x + b,
  262. * so y is one of the square roots of x^3 + a*x + b. */
  263. /* tmp1 := x^3 */
  264. if (!BN_nnmod(x, x_, &group->field, ctx)) {
  265. goto err;
  266. }
  267. if (group->meth->field_decode == 0) {
  268. /* field_{sqr,mul} work on standard representation */
  269. if (!group->meth->field_sqr(group, tmp2, x_, ctx) ||
  270. !group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) {
  271. goto err;
  272. }
  273. } else {
  274. if (!BN_mod_sqr(tmp2, x_, &group->field, ctx) ||
  275. !BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) {
  276. goto err;
  277. }
  278. }
  279. /* tmp1 := tmp1 + a*x */
  280. if (group->a_is_minus3) {
  281. if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
  282. !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
  283. !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
  284. goto err;
  285. }
  286. } else {
  287. if (group->meth->field_decode) {
  288. if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
  289. !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
  290. goto err;
  291. }
  292. } else {
  293. /* field_mul works on standard representation */
  294. if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
  295. goto err;
  296. }
  297. }
  298. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  299. goto err;
  300. }
  301. }
  302. /* tmp1 := tmp1 + b */
  303. if (group->meth->field_decode) {
  304. if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
  305. !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
  306. goto err;
  307. }
  308. } else {
  309. if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
  310. goto err;
  311. }
  312. }
  313. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
  314. unsigned long err = ERR_peek_last_error();
  315. if (ERR_GET_LIB(err) == ERR_LIB_BN &&
  316. ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  317. ERR_clear_error();
  318. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  319. } else {
  320. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  321. }
  322. goto err;
  323. }
  324. if (y_bit != BN_is_odd(y)) {
  325. if (BN_is_zero(y)) {
  326. int kron;
  327. kron = BN_kronecker(x, &group->field, ctx);
  328. if (kron == -2) {
  329. goto err;
  330. }
  331. if (kron == 1) {
  332. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
  333. } else {
  334. /* BN_mod_sqrt() should have cought this error (not a square) */
  335. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  336. }
  337. goto err;
  338. }
  339. if (!BN_usub(y, &group->field, y)) {
  340. goto err;
  341. }
  342. }
  343. if (y_bit != BN_is_odd(y)) {
  344. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  345. goto err;
  346. }
  347. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  348. goto err;
  349. }
  350. ret = 1;
  351. err:
  352. BN_CTX_end(ctx);
  353. BN_CTX_free(new_ctx);
  354. return ret;
  355. }
  356. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  357. EC_POINT *point, const BIGNUM *x,
  358. int y_bit, BN_CTX *ctx) {
  359. if (group->meth != point->meth) {
  360. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  361. return 0;
  362. }
  363. return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit, ctx);
  364. }