ec_key.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* crypto/ec/ec_key.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Portions originally developed by SUN MICROSYSTEMS, INC., and
  61. * contributed to the OpenSSL project.
  62. */
  63. #define OPENSSL_FIPSAPI
  64. #include <string.h>
  65. #include "ec_lcl.h"
  66. #include <openssl/err.h>
  67. #include <string.h>
  68. EC_KEY *EC_KEY_new(void)
  69. {
  70. EC_KEY *ret;
  71. ret=(EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
  72. if (ret == NULL)
  73. {
  74. ECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE);
  75. return(NULL);
  76. }
  77. ret->version = 1;
  78. ret->flags = 0;
  79. ret->group = NULL;
  80. ret->pub_key = NULL;
  81. ret->priv_key= NULL;
  82. ret->enc_flag= 0;
  83. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  84. ret->references= 1;
  85. ret->method_data = NULL;
  86. return(ret);
  87. }
  88. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  89. {
  90. EC_KEY *ret = EC_KEY_new();
  91. if (ret == NULL)
  92. return NULL;
  93. ret->group = EC_GROUP_new_by_curve_name(nid);
  94. if (ret->group == NULL)
  95. {
  96. EC_KEY_free(ret);
  97. return NULL;
  98. }
  99. return ret;
  100. }
  101. void EC_KEY_free(EC_KEY *r)
  102. {
  103. int i;
  104. if (r == NULL) return;
  105. i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC);
  106. #ifdef REF_PRINT
  107. REF_PRINT("EC_KEY",r);
  108. #endif
  109. if (i > 0) return;
  110. #ifdef REF_CHECK
  111. if (i < 0)
  112. {
  113. fprintf(stderr,"EC_KEY_free, bad reference count\n");
  114. abort();
  115. }
  116. #endif
  117. if (r->group != NULL)
  118. EC_GROUP_free(r->group);
  119. if (r->pub_key != NULL)
  120. EC_POINT_free(r->pub_key);
  121. if (r->priv_key != NULL)
  122. BN_clear_free(r->priv_key);
  123. EC_EX_DATA_free_all_data(&r->method_data);
  124. OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
  125. OPENSSL_free(r);
  126. }
  127. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  128. {
  129. EC_EXTRA_DATA *d;
  130. if (dest == NULL || src == NULL)
  131. {
  132. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  133. return NULL;
  134. }
  135. /* copy the parameters */
  136. if (src->group)
  137. {
  138. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  139. /* clear the old group */
  140. if (dest->group)
  141. EC_GROUP_free(dest->group);
  142. dest->group = EC_GROUP_new(meth);
  143. if (dest->group == NULL)
  144. return NULL;
  145. if (!EC_GROUP_copy(dest->group, src->group))
  146. return NULL;
  147. }
  148. /* copy the public key */
  149. if (src->pub_key && src->group)
  150. {
  151. if (dest->pub_key)
  152. EC_POINT_free(dest->pub_key);
  153. dest->pub_key = EC_POINT_new(src->group);
  154. if (dest->pub_key == NULL)
  155. return NULL;
  156. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  157. return NULL;
  158. }
  159. /* copy the private key */
  160. if (src->priv_key)
  161. {
  162. if (dest->priv_key == NULL)
  163. {
  164. dest->priv_key = BN_new();
  165. if (dest->priv_key == NULL)
  166. return NULL;
  167. }
  168. if (!BN_copy(dest->priv_key, src->priv_key))
  169. return NULL;
  170. }
  171. /* copy method/extra data */
  172. EC_EX_DATA_free_all_data(&dest->method_data);
  173. for (d = src->method_data; d != NULL; d = d->next)
  174. {
  175. void *t = d->dup_func(d->data);
  176. if (t == NULL)
  177. return 0;
  178. if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func, d->free_func, d->clear_free_func))
  179. return 0;
  180. }
  181. /* copy the rest */
  182. dest->enc_flag = src->enc_flag;
  183. dest->conv_form = src->conv_form;
  184. dest->version = src->version;
  185. dest->flags = src->flags;
  186. return dest;
  187. }
  188. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  189. {
  190. EC_KEY *ret = EC_KEY_new();
  191. if (ret == NULL)
  192. return NULL;
  193. if (EC_KEY_copy(ret, ec_key) == NULL)
  194. {
  195. EC_KEY_free(ret);
  196. return NULL;
  197. }
  198. return ret;
  199. }
  200. int EC_KEY_up_ref(EC_KEY *r)
  201. {
  202. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
  203. #ifdef REF_PRINT
  204. REF_PRINT("EC_KEY",r);
  205. #endif
  206. #ifdef REF_CHECK
  207. if (i < 2)
  208. {
  209. fprintf(stderr, "EC_KEY_up, bad reference count\n");
  210. abort();
  211. }
  212. #endif
  213. return ((i > 1) ? 1 : 0);
  214. }
  215. #ifdef OPENSSL_FIPS
  216. #include <openssl/evp.h>
  217. #include <openssl/fips.h>
  218. #include <openssl/fips_rand.h>
  219. static int fips_check_ec(EC_KEY *key)
  220. {
  221. EVP_PKEY pk;
  222. unsigned char tbs[] = "ECDSA Pairwise Check Data";
  223. pk.type = EVP_PKEY_EC;
  224. pk.pkey.ec = key;
  225. if (!fips_pkey_signature_test(FIPS_TEST_PAIRWISE,
  226. &pk, tbs, 0, NULL, 0, NULL, 0, NULL))
  227. {
  228. FIPSerr(FIPS_F_FIPS_CHECK_EC,FIPS_R_PAIRWISE_TEST_FAILED);
  229. fips_set_selftest_fail();
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. int fips_check_ec_prng(EC_KEY *ec)
  235. {
  236. int bits, strength;
  237. if (!FIPS_module_mode())
  238. return 1;
  239. if (ec->flags & (EC_FLAG_NON_FIPS_ALLOW|EC_FLAG_FIPS_CHECKED))
  240. return 1;
  241. if (!ec->group)
  242. return 1;
  243. bits = BN_num_bits(&ec->group->order);
  244. if (bits < 160)
  245. {
  246. FIPSerr(FIPS_F_FIPS_CHECK_EC_PRNG,FIPS_R_KEY_TOO_SHORT);
  247. return 0;
  248. }
  249. /* Comparable algorithm strengths: from SP800-57 table 2 */
  250. if (bits >= 512)
  251. strength = 256;
  252. else if (bits >= 384)
  253. strength = 192;
  254. else if (bits >= 256)
  255. strength = 128;
  256. else if (bits >= 224)
  257. strength = 112;
  258. else
  259. strength = 80;
  260. if (FIPS_rand_strength() >= strength)
  261. return 1;
  262. FIPSerr(FIPS_F_FIPS_CHECK_EC_PRNG,FIPS_R_PRNG_STRENGTH_TOO_LOW);
  263. return 0;
  264. }
  265. #endif
  266. int EC_KEY_generate_key(EC_KEY *eckey)
  267. {
  268. int ok = 0;
  269. BN_CTX *ctx = NULL;
  270. BIGNUM *priv_key = NULL, *order = NULL;
  271. EC_POINT *pub_key = NULL;
  272. if (!eckey || !eckey->group)
  273. {
  274. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  275. return 0;
  276. }
  277. if ((order = BN_new()) == NULL) goto err;
  278. if ((ctx = BN_CTX_new()) == NULL) goto err;
  279. if (eckey->priv_key == NULL)
  280. {
  281. priv_key = BN_new();
  282. if (priv_key == NULL)
  283. goto err;
  284. }
  285. else
  286. priv_key = eckey->priv_key;
  287. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  288. goto err;
  289. #ifdef OPENSSL_FIPS
  290. if (!fips_check_ec_prng(eckey))
  291. goto err;
  292. #endif
  293. do
  294. if (!BN_rand_range(priv_key, order))
  295. goto err;
  296. while (BN_is_zero(priv_key));
  297. if (eckey->pub_key == NULL)
  298. {
  299. pub_key = EC_POINT_new(eckey->group);
  300. if (pub_key == NULL)
  301. goto err;
  302. }
  303. else
  304. pub_key = eckey->pub_key;
  305. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  306. goto err;
  307. eckey->priv_key = priv_key;
  308. eckey->pub_key = pub_key;
  309. #ifdef OPENSSL_FIPS
  310. if(!fips_check_ec(eckey))
  311. {
  312. eckey->priv_key = NULL;
  313. eckey->pub_key = NULL;
  314. goto err;
  315. }
  316. #endif
  317. ok=1;
  318. err:
  319. if (order)
  320. BN_free(order);
  321. if (pub_key != NULL && eckey->pub_key == NULL)
  322. EC_POINT_free(pub_key);
  323. if (priv_key != NULL && eckey->priv_key == NULL)
  324. BN_free(priv_key);
  325. if (ctx != NULL)
  326. BN_CTX_free(ctx);
  327. return(ok);
  328. }
  329. int EC_KEY_check_key(const EC_KEY *eckey)
  330. {
  331. int ok = 0;
  332. BN_CTX *ctx = NULL;
  333. const BIGNUM *order = NULL;
  334. EC_POINT *point = NULL;
  335. if (!eckey || !eckey->group || !eckey->pub_key)
  336. {
  337. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  338. return 0;
  339. }
  340. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key))
  341. {
  342. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY);
  343. goto err;
  344. }
  345. if ((ctx = BN_CTX_new()) == NULL)
  346. goto err;
  347. if ((point = EC_POINT_new(eckey->group)) == NULL)
  348. goto err;
  349. /* testing whether the pub_key is on the elliptic curve */
  350. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx))
  351. {
  352. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  353. goto err;
  354. }
  355. /* testing whether pub_key * order is the point at infinity */
  356. order = &eckey->group->order;
  357. if (BN_is_zero(order))
  358. {
  359. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  360. goto err;
  361. }
  362. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx))
  363. {
  364. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  365. goto err;
  366. }
  367. if (!EC_POINT_is_at_infinity(eckey->group, point))
  368. {
  369. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  370. goto err;
  371. }
  372. /* in case the priv_key is present :
  373. * check if generator * priv_key == pub_key
  374. */
  375. if (eckey->priv_key)
  376. {
  377. if (BN_cmp(eckey->priv_key, order) >= 0)
  378. {
  379. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  380. goto err;
  381. }
  382. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  383. NULL, NULL, ctx))
  384. {
  385. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  386. goto err;
  387. }
  388. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key,
  389. ctx) != 0)
  390. {
  391. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  392. goto err;
  393. }
  394. }
  395. ok = 1;
  396. err:
  397. if (ctx != NULL)
  398. BN_CTX_free(ctx);
  399. if (point != NULL)
  400. EC_POINT_free(point);
  401. return(ok);
  402. }
  403. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y)
  404. {
  405. BN_CTX *ctx = NULL;
  406. BIGNUM *tx, *ty;
  407. EC_POINT *point = NULL;
  408. int ok = 0, tmp_nid, is_char_two = 0;
  409. if (!key || !key->group || !x || !y)
  410. {
  411. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  412. ERR_R_PASSED_NULL_PARAMETER);
  413. return 0;
  414. }
  415. ctx = BN_CTX_new();
  416. if (!ctx)
  417. goto err;
  418. point = EC_POINT_new(key->group);
  419. if (!point)
  420. goto err;
  421. tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
  422. if (tmp_nid == NID_X9_62_characteristic_two_field)
  423. is_char_two = 1;
  424. tx = BN_CTX_get(ctx);
  425. ty = BN_CTX_get(ctx);
  426. #ifndef OPENSSL_NO_EC2M
  427. if (is_char_two)
  428. {
  429. if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
  430. x, y, ctx))
  431. goto err;
  432. if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
  433. tx, ty, ctx))
  434. goto err;
  435. }
  436. else
  437. #endif
  438. {
  439. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
  440. x, y, ctx))
  441. goto err;
  442. if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
  443. tx, ty, ctx))
  444. goto err;
  445. }
  446. /* Check if retrieved coordinates match originals: if not values
  447. * are out of range.
  448. */
  449. if (BN_cmp(x, tx) || BN_cmp(y, ty))
  450. {
  451. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  452. EC_R_COORDINATES_OUT_OF_RANGE);
  453. goto err;
  454. }
  455. if (!EC_KEY_set_public_key(key, point))
  456. goto err;
  457. if (EC_KEY_check_key(key) == 0)
  458. goto err;
  459. ok = 1;
  460. err:
  461. if (ctx)
  462. BN_CTX_free(ctx);
  463. if (point)
  464. EC_POINT_free(point);
  465. return ok;
  466. }
  467. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  468. {
  469. return key->group;
  470. }
  471. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  472. {
  473. if (key->group != NULL)
  474. EC_GROUP_free(key->group);
  475. key->group = EC_GROUP_dup(group);
  476. return (key->group == NULL) ? 0 : 1;
  477. }
  478. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  479. {
  480. return key->priv_key;
  481. }
  482. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  483. {
  484. if (key->priv_key)
  485. BN_clear_free(key->priv_key);
  486. key->priv_key = BN_dup(priv_key);
  487. return (key->priv_key == NULL) ? 0 : 1;
  488. }
  489. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  490. {
  491. return key->pub_key;
  492. }
  493. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  494. {
  495. if (key->pub_key != NULL)
  496. EC_POINT_free(key->pub_key);
  497. key->pub_key = EC_POINT_dup(pub_key, key->group);
  498. return (key->pub_key == NULL) ? 0 : 1;
  499. }
  500. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  501. {
  502. return key->enc_flag;
  503. }
  504. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  505. {
  506. key->enc_flag = flags;
  507. }
  508. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  509. {
  510. return key->conv_form;
  511. }
  512. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  513. {
  514. key->conv_form = cform;
  515. if (key->group != NULL)
  516. EC_GROUP_set_point_conversion_form(key->group, cform);
  517. }
  518. void *EC_KEY_get_key_method_data(EC_KEY *key,
  519. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  520. {
  521. return EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
  522. }
  523. void EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
  524. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  525. {
  526. EC_EXTRA_DATA *ex_data;
  527. CRYPTO_w_lock(CRYPTO_LOCK_EC);
  528. ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
  529. if (ex_data == NULL)
  530. EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func);
  531. CRYPTO_w_unlock(CRYPTO_LOCK_EC);
  532. }
  533. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  534. {
  535. if (key->group != NULL)
  536. EC_GROUP_set_asn1_flag(key->group, flag);
  537. }
  538. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  539. {
  540. if (key->group == NULL)
  541. return 0;
  542. return EC_GROUP_precompute_mult(key->group, ctx);
  543. }
  544. int EC_KEY_get_flags(const EC_KEY *key)
  545. {
  546. return key->flags;
  547. }
  548. void EC_KEY_set_flags(EC_KEY *key, int flags)
  549. {
  550. key->flags |= flags;
  551. }
  552. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  553. {
  554. key->flags &= ~flags;
  555. }