ec_key.c 15 KB

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