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.
 
 
 
 
 
 

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