ec_key.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. #include <string.h>
  64. #include "ec_lcl.h"
  65. #include <openssl/err.h>
  66. #ifdef OPENSSL_FIPS
  67. # include <openssl/fips.h>
  68. #endif
  69. EC_KEY *EC_KEY_new(void)
  70. {
  71. EC_KEY *ret;
  72. ret = (EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
  73. if (ret == NULL) {
  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. EC_KEY_free(ret);
  96. return NULL;
  97. }
  98. return ret;
  99. }
  100. void EC_KEY_free(EC_KEY *r)
  101. {
  102. int i;
  103. if (r == NULL)
  104. 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)
  110. return;
  111. #ifdef REF_CHECK
  112. if (i < 0) {
  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. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  132. return NULL;
  133. }
  134. /* copy the parameters */
  135. if (src->group) {
  136. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  137. /* clear the old group */
  138. if (dest->group)
  139. EC_GROUP_free(dest->group);
  140. dest->group = EC_GROUP_new(meth);
  141. if (dest->group == NULL)
  142. return NULL;
  143. if (!EC_GROUP_copy(dest->group, src->group))
  144. return NULL;
  145. }
  146. /* copy the public key */
  147. if (src->pub_key && src->group) {
  148. if (dest->pub_key)
  149. EC_POINT_free(dest->pub_key);
  150. dest->pub_key = EC_POINT_new(src->group);
  151. if (dest->pub_key == NULL)
  152. return NULL;
  153. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  154. return NULL;
  155. }
  156. /* copy the private key */
  157. if (src->priv_key) {
  158. if (dest->priv_key == NULL) {
  159. dest->priv_key = BN_new();
  160. if (dest->priv_key == NULL)
  161. return NULL;
  162. }
  163. if (!BN_copy(dest->priv_key, src->priv_key))
  164. return NULL;
  165. }
  166. /* copy method/extra data */
  167. EC_EX_DATA_free_all_data(&dest->method_data);
  168. for (d = src->method_data; d != NULL; d = d->next) {
  169. void *t = d->dup_func(d->data);
  170. if (t == NULL)
  171. return 0;
  172. if (!EC_EX_DATA_set_data
  173. (&dest->method_data, t, d->dup_func, d->free_func,
  174. d->clear_free_func))
  175. return 0;
  176. }
  177. /* copy the rest */
  178. dest->enc_flag = src->enc_flag;
  179. dest->conv_form = src->conv_form;
  180. dest->version = src->version;
  181. dest->flags = src->flags;
  182. return dest;
  183. }
  184. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  185. {
  186. EC_KEY *ret = EC_KEY_new();
  187. if (ret == NULL)
  188. return NULL;
  189. if (EC_KEY_copy(ret, ec_key) == NULL) {
  190. EC_KEY_free(ret);
  191. return NULL;
  192. }
  193. return ret;
  194. }
  195. int EC_KEY_up_ref(EC_KEY *r)
  196. {
  197. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
  198. #ifdef REF_PRINT
  199. REF_PRINT("EC_KEY", r);
  200. #endif
  201. #ifdef REF_CHECK
  202. if (i < 2) {
  203. fprintf(stderr, "EC_KEY_up, bad reference count\n");
  204. abort();
  205. }
  206. #endif
  207. return ((i > 1) ? 1 : 0);
  208. }
  209. int EC_KEY_generate_key(EC_KEY *eckey)
  210. {
  211. int ok = 0;
  212. BN_CTX *ctx = NULL;
  213. BIGNUM *priv_key = NULL, *order = NULL;
  214. EC_POINT *pub_key = NULL;
  215. #ifdef OPENSSL_FIPS
  216. if (FIPS_mode())
  217. return FIPS_ec_key_generate_key(eckey);
  218. #endif
  219. if (!eckey || !eckey->group) {
  220. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  221. return 0;
  222. }
  223. if ((order = BN_new()) == NULL)
  224. goto err;
  225. if ((ctx = BN_CTX_new()) == NULL)
  226. goto err;
  227. if (eckey->priv_key == NULL) {
  228. priv_key = BN_new();
  229. if (priv_key == NULL)
  230. goto err;
  231. } else
  232. priv_key = eckey->priv_key;
  233. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  234. goto err;
  235. do
  236. if (!BN_rand_range(priv_key, order))
  237. goto err;
  238. while (BN_is_zero(priv_key)) ;
  239. if (eckey->pub_key == NULL) {
  240. pub_key = EC_POINT_new(eckey->group);
  241. if (pub_key == NULL)
  242. goto err;
  243. } else
  244. pub_key = eckey->pub_key;
  245. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  246. goto err;
  247. eckey->priv_key = priv_key;
  248. eckey->pub_key = pub_key;
  249. ok = 1;
  250. err:
  251. if (order)
  252. BN_free(order);
  253. if (pub_key != NULL && eckey->pub_key == NULL)
  254. EC_POINT_free(pub_key);
  255. if (priv_key != NULL && eckey->priv_key == NULL)
  256. BN_free(priv_key);
  257. if (ctx != NULL)
  258. BN_CTX_free(ctx);
  259. return (ok);
  260. }
  261. int EC_KEY_check_key(const EC_KEY *eckey)
  262. {
  263. int ok = 0;
  264. BN_CTX *ctx = NULL;
  265. const BIGNUM *order = NULL;
  266. EC_POINT *point = NULL;
  267. if (!eckey || !eckey->group || !eckey->pub_key) {
  268. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  269. return 0;
  270. }
  271. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  272. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY);
  273. goto err;
  274. }
  275. if ((ctx = BN_CTX_new()) == NULL)
  276. goto err;
  277. if ((point = EC_POINT_new(eckey->group)) == NULL)
  278. goto err;
  279. /* testing whether the pub_key is on the elliptic curve */
  280. if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
  281. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  282. goto err;
  283. }
  284. /* testing whether pub_key * order is the point at infinity */
  285. order = &eckey->group->order;
  286. if (BN_is_zero(order)) {
  287. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  288. goto err;
  289. }
  290. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  291. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  292. goto err;
  293. }
  294. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  295. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  296. goto err;
  297. }
  298. /*
  299. * in case the priv_key is present : check if generator * priv_key ==
  300. * pub_key
  301. */
  302. if (eckey->priv_key) {
  303. if (BN_cmp(eckey->priv_key, order) >= 0) {
  304. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  305. goto err;
  306. }
  307. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  308. NULL, NULL, ctx)) {
  309. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  310. goto err;
  311. }
  312. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  313. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  314. goto err;
  315. }
  316. }
  317. ok = 1;
  318. err:
  319. if (ctx != NULL)
  320. BN_CTX_free(ctx);
  321. if (point != NULL)
  322. EC_POINT_free(point);
  323. return (ok);
  324. }
  325. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  326. BIGNUM *y)
  327. {
  328. BN_CTX *ctx = NULL;
  329. BIGNUM *tx, *ty;
  330. EC_POINT *point = NULL;
  331. int ok = 0;
  332. #ifndef OPENSSL_NO_EC2M
  333. int tmp_nid, is_char_two = 0;
  334. #endif
  335. if (!key || !key->group || !x || !y) {
  336. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  337. ERR_R_PASSED_NULL_PARAMETER);
  338. return 0;
  339. }
  340. ctx = BN_CTX_new();
  341. if (ctx == NULL)
  342. return 0;
  343. BN_CTX_start(ctx);
  344. point = EC_POINT_new(key->group);
  345. if (!point)
  346. goto err;
  347. tx = BN_CTX_get(ctx);
  348. ty = BN_CTX_get(ctx);
  349. if (ty == NULL)
  350. goto err;
  351. #ifndef OPENSSL_NO_EC2M
  352. tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
  353. if (tmp_nid == NID_X9_62_characteristic_two_field)
  354. is_char_two = 1;
  355. if (is_char_two) {
  356. if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
  357. x, y, ctx))
  358. goto err;
  359. if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
  360. tx, ty, ctx))
  361. goto err;
  362. } else
  363. #endif
  364. {
  365. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
  366. x, y, ctx))
  367. goto err;
  368. if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
  369. tx, ty, ctx))
  370. goto err;
  371. }
  372. /*
  373. * Check if retrieved coordinates match originals: if not values are out
  374. * of range.
  375. */
  376. if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
  377. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  378. EC_R_COORDINATES_OUT_OF_RANGE);
  379. goto err;
  380. }
  381. if (!EC_KEY_set_public_key(key, point))
  382. goto err;
  383. if (EC_KEY_check_key(key) == 0)
  384. goto err;
  385. ok = 1;
  386. err:
  387. BN_CTX_end(ctx);
  388. BN_CTX_free(ctx);
  389. EC_POINT_free(point);
  390. return ok;
  391. }
  392. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  393. {
  394. return key->group;
  395. }
  396. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  397. {
  398. if (key->group != NULL)
  399. EC_GROUP_free(key->group);
  400. key->group = EC_GROUP_dup(group);
  401. return (key->group == NULL) ? 0 : 1;
  402. }
  403. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  404. {
  405. return key->priv_key;
  406. }
  407. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  408. {
  409. if (key->priv_key)
  410. BN_clear_free(key->priv_key);
  411. key->priv_key = BN_dup(priv_key);
  412. return (key->priv_key == NULL) ? 0 : 1;
  413. }
  414. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  415. {
  416. return key->pub_key;
  417. }
  418. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  419. {
  420. if (key->pub_key != NULL)
  421. EC_POINT_free(key->pub_key);
  422. key->pub_key = EC_POINT_dup(pub_key, key->group);
  423. return (key->pub_key == NULL) ? 0 : 1;
  424. }
  425. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  426. {
  427. return key->enc_flag;
  428. }
  429. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  430. {
  431. key->enc_flag = flags;
  432. }
  433. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  434. {
  435. return key->conv_form;
  436. }
  437. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  438. {
  439. key->conv_form = cform;
  440. if (key->group != NULL)
  441. EC_GROUP_set_point_conversion_form(key->group, cform);
  442. }
  443. void *EC_KEY_get_key_method_data(EC_KEY *key,
  444. void *(*dup_func) (void *),
  445. void (*free_func) (void *),
  446. void (*clear_free_func) (void *))
  447. {
  448. void *ret;
  449. CRYPTO_r_lock(CRYPTO_LOCK_EC);
  450. ret =
  451. EC_EX_DATA_get_data(key->method_data, dup_func, free_func,
  452. clear_free_func);
  453. CRYPTO_r_unlock(CRYPTO_LOCK_EC);
  454. return ret;
  455. }
  456. void *EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
  457. void *(*dup_func) (void *),
  458. void (*free_func) (void *),
  459. void (*clear_free_func) (void *))
  460. {
  461. EC_EXTRA_DATA *ex_data;
  462. CRYPTO_w_lock(CRYPTO_LOCK_EC);
  463. ex_data =
  464. EC_EX_DATA_get_data(key->method_data, dup_func, free_func,
  465. clear_free_func);
  466. if (ex_data == NULL)
  467. EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func,
  468. clear_free_func);
  469. CRYPTO_w_unlock(CRYPTO_LOCK_EC);
  470. return ex_data;
  471. }
  472. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  473. {
  474. if (key->group != NULL)
  475. EC_GROUP_set_asn1_flag(key->group, flag);
  476. }
  477. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  478. {
  479. if (key->group == NULL)
  480. return 0;
  481. return EC_GROUP_precompute_mult(key->group, ctx);
  482. }
  483. int EC_KEY_get_flags(const EC_KEY *key)
  484. {
  485. return key->flags;
  486. }
  487. void EC_KEY_set_flags(EC_KEY *key, int flags)
  488. {
  489. key->flags |= flags;
  490. }
  491. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  492. {
  493. key->flags &= ~flags;
  494. }