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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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_key.h>
  68. #include <string.h>
  69. #include <openssl/ec.h>
  70. #include <openssl/engine.h>
  71. #include <openssl/err.h>
  72. #include <openssl/ex_data.h>
  73. #include <openssl/mem.h>
  74. #include <openssl/thread.h>
  75. #include "internal.h"
  76. #include "../internal.h"
  77. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  78. EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
  79. EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
  80. EC_KEY *ret = OPENSSL_malloc(sizeof(EC_KEY));
  81. if (ret == NULL) {
  82. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  83. return NULL;
  84. }
  85. memset(ret, 0, sizeof(EC_KEY));
  86. if (engine) {
  87. ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
  88. }
  89. if (ret->ecdsa_meth) {
  90. METHOD_ref(ret->ecdsa_meth);
  91. }
  92. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  93. ret->references = 1;
  94. CRYPTO_new_ex_data(&ret->ex_data);
  95. if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
  96. CRYPTO_free_ex_data(&g_ex_data_class, ret, &ret->ex_data);
  97. if (ret->ecdsa_meth) {
  98. METHOD_unref(ret->ecdsa_meth);
  99. }
  100. OPENSSL_free(ret);
  101. return NULL;
  102. }
  103. return ret;
  104. }
  105. EC_KEY *EC_KEY_new_by_curve_name(int nid) {
  106. EC_KEY *ret = EC_KEY_new();
  107. if (ret == NULL) {
  108. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  109. return NULL;
  110. }
  111. ret->group = EC_GROUP_new_by_curve_name(nid);
  112. if (ret->group == NULL) {
  113. EC_KEY_free(ret);
  114. return NULL;
  115. }
  116. return ret;
  117. }
  118. void EC_KEY_free(EC_KEY *r) {
  119. if (r == NULL) {
  120. return;
  121. }
  122. if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) {
  123. return;
  124. }
  125. if (r->ecdsa_meth) {
  126. if (r->ecdsa_meth->finish) {
  127. r->ecdsa_meth->finish(r);
  128. }
  129. METHOD_unref(r->ecdsa_meth);
  130. }
  131. EC_GROUP_free(r->group);
  132. EC_POINT_free(r->pub_key);
  133. BN_clear_free(r->priv_key);
  134. CRYPTO_free_ex_data(&g_ex_data_class, r, &r->ex_data);
  135. OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
  136. OPENSSL_free(r);
  137. }
  138. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) {
  139. if (dest == NULL || src == NULL) {
  140. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  141. return NULL;
  142. }
  143. /* Copy the parameters. */
  144. if (src->group) {
  145. /* TODO(fork): duplicating the group seems wasteful. */
  146. EC_GROUP_free(dest->group);
  147. dest->group = EC_GROUP_dup(src->group);
  148. if (dest->group == NULL) {
  149. return NULL;
  150. }
  151. }
  152. /* Copy the public key. */
  153. if (src->pub_key && src->group) {
  154. EC_POINT_free(dest->pub_key);
  155. dest->pub_key = EC_POINT_dup(src->pub_key, src->group);
  156. if (dest->pub_key == NULL) {
  157. return NULL;
  158. }
  159. }
  160. /* copy the private key */
  161. if (src->priv_key) {
  162. if (dest->priv_key == NULL) {
  163. dest->priv_key = BN_new();
  164. if (dest->priv_key == NULL) {
  165. return NULL;
  166. }
  167. }
  168. if (!BN_copy(dest->priv_key, src->priv_key)) {
  169. return NULL;
  170. }
  171. }
  172. /* copy method/extra data */
  173. if (src->ecdsa_meth) {
  174. METHOD_unref(dest->ecdsa_meth);
  175. dest->ecdsa_meth = src->ecdsa_meth;
  176. METHOD_ref(dest->ecdsa_meth);
  177. }
  178. CRYPTO_free_ex_data(&g_ex_data_class, dest, &dest->ex_data);
  179. if (!CRYPTO_dup_ex_data(&g_ex_data_class, &dest->ex_data,
  180. &src->ex_data)) {
  181. return NULL;
  182. }
  183. /* copy the rest */
  184. dest->enc_flag = src->enc_flag;
  185. dest->conv_form = src->conv_form;
  186. return dest;
  187. }
  188. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key) {
  189. EC_KEY *ret = EC_KEY_new();
  190. if (ret == NULL) {
  191. return NULL;
  192. }
  193. if (EC_KEY_copy(ret, ec_key) == NULL) {
  194. EC_KEY_free(ret);
  195. return NULL;
  196. }
  197. return ret;
  198. }
  199. int EC_KEY_up_ref(EC_KEY *r) {
  200. CRYPTO_refcount_inc(&r->references);
  201. return 1;
  202. }
  203. int EC_KEY_is_opaque(const EC_KEY *key) {
  204. return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
  205. }
  206. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
  207. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
  208. EC_GROUP_free(key->group);
  209. /* TODO(fork): duplicating the group seems wasteful but see
  210. * |EC_KEY_set_conv_form|. */
  211. key->group = EC_GROUP_dup(group);
  212. if (key->group == NULL) {
  213. return 0;
  214. }
  215. /* XXX: |BN_cmp| is not constant time. */
  216. if (key->priv_key != NULL &&
  217. BN_cmp(key->priv_key, EC_GROUP_get0_order(group)) >= 0) {
  218. return 0;
  219. }
  220. return 1;
  221. }
  222. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
  223. return key->priv_key;
  224. }
  225. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
  226. /* XXX: |BN_cmp| is not constant time. */
  227. if (key->group != NULL &&
  228. BN_cmp(priv_key, EC_GROUP_get0_order(key->group)) >= 0) {
  229. OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
  230. return 0;
  231. }
  232. BN_clear_free(key->priv_key);
  233. key->priv_key = BN_dup(priv_key);
  234. return (key->priv_key == NULL) ? 0 : 1;
  235. }
  236. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) {
  237. return key->pub_key;
  238. }
  239. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
  240. EC_POINT_free(key->pub_key);
  241. key->pub_key = EC_POINT_dup(pub_key, key->group);
  242. return (key->pub_key == NULL) ? 0 : 1;
  243. }
  244. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; }
  245. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) {
  246. key->enc_flag = flags;
  247. }
  248. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) {
  249. return key->conv_form;
  250. }
  251. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) {
  252. key->conv_form = cform;
  253. }
  254. int EC_KEY_check_key(const EC_KEY *eckey) {
  255. int ok = 0;
  256. BN_CTX *ctx = NULL;
  257. EC_POINT *point = NULL;
  258. if (!eckey || !eckey->group || !eckey->pub_key) {
  259. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  260. return 0;
  261. }
  262. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  263. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  264. goto err;
  265. }
  266. ctx = BN_CTX_new();
  267. if (ctx == NULL) {
  268. goto err;
  269. }
  270. /* testing whether the pub_key is on the elliptic curve */
  271. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) {
  272. OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
  273. goto err;
  274. }
  275. /* in case the priv_key is present :
  276. * check if generator * priv_key == pub_key
  277. */
  278. if (eckey->priv_key) {
  279. /* XXX: |BN_cmp| is not constant time. */
  280. if (BN_cmp(eckey->priv_key, EC_GROUP_get0_order(eckey->group)) >= 0) {
  281. OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
  282. goto err;
  283. }
  284. point = EC_POINT_new(eckey->group);
  285. if (point == NULL ||
  286. !EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
  287. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  288. goto err;
  289. }
  290. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  291. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
  292. goto err;
  293. }
  294. }
  295. ok = 1;
  296. err:
  297. BN_CTX_free(ctx);
  298. EC_POINT_free(point);
  299. return ok;
  300. }
  301. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  302. BIGNUM *y) {
  303. BN_CTX *ctx = NULL;
  304. BIGNUM *tx, *ty;
  305. EC_POINT *point = NULL;
  306. int ok = 0;
  307. if (!key || !key->group || !x || !y) {
  308. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  309. return 0;
  310. }
  311. ctx = BN_CTX_new();
  312. if (ctx == NULL) {
  313. return 0;
  314. }
  315. BN_CTX_start(ctx);
  316. point = EC_POINT_new(key->group);
  317. if (point == NULL) {
  318. goto err;
  319. }
  320. tx = BN_CTX_get(ctx);
  321. ty = BN_CTX_get(ctx);
  322. if (tx == NULL ||
  323. ty == NULL) {
  324. goto err;
  325. }
  326. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, ctx) ||
  327. !EC_POINT_get_affine_coordinates_GFp(key->group, point, tx, ty, ctx)) {
  328. goto err;
  329. }
  330. /* Check if retrieved coordinates match originals: if not values
  331. * are out of range. */
  332. if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
  333. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  334. goto err;
  335. }
  336. if (!EC_KEY_set_public_key(key, point)) {
  337. goto err;
  338. }
  339. if (EC_KEY_check_key(key) == 0) {
  340. goto err;
  341. }
  342. ok = 1;
  343. err:
  344. BN_CTX_end(ctx);
  345. BN_CTX_free(ctx);
  346. EC_POINT_free(point);
  347. return ok;
  348. }
  349. int EC_KEY_generate_key(EC_KEY *eckey) {
  350. int ok = 0;
  351. BIGNUM *priv_key = NULL;
  352. EC_POINT *pub_key = NULL;
  353. if (!eckey || !eckey->group) {
  354. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  355. return 0;
  356. }
  357. if (eckey->priv_key == NULL) {
  358. priv_key = BN_new();
  359. if (priv_key == NULL) {
  360. goto err;
  361. }
  362. } else {
  363. priv_key = eckey->priv_key;
  364. }
  365. const BIGNUM *order = EC_GROUP_get0_order(eckey->group);
  366. if (!BN_rand_range_ex(priv_key, 1, order)) {
  367. goto err;
  368. }
  369. if (eckey->pub_key == NULL) {
  370. pub_key = EC_POINT_new(eckey->group);
  371. if (pub_key == NULL) {
  372. goto err;
  373. }
  374. } else {
  375. pub_key = eckey->pub_key;
  376. }
  377. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, NULL)) {
  378. goto err;
  379. }
  380. eckey->priv_key = priv_key;
  381. eckey->pub_key = pub_key;
  382. ok = 1;
  383. err:
  384. if (eckey->pub_key == NULL) {
  385. EC_POINT_free(pub_key);
  386. }
  387. if (eckey->priv_key == NULL) {
  388. BN_free(priv_key);
  389. }
  390. return ok;
  391. }
  392. int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  393. CRYPTO_EX_dup *dup_func,
  394. CRYPTO_EX_free *free_func) {
  395. int index;
  396. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  397. free_func)) {
  398. return -1;
  399. }
  400. return index;
  401. }
  402. int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
  403. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  404. }
  405. void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
  406. return CRYPTO_get_ex_data(&d->ex_data, idx);
  407. }
  408. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {}