No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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