2
0

ec_key.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #include <string.h>
  67. EC_KEY *EC_KEY_new(void)
  68. {
  69. EC_KEY *ret;
  70. ret=(EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
  71. if (ret == NULL)
  72. {
  73. ECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE);
  74. return(NULL);
  75. }
  76. ret->version = 1;
  77. ret->group = NULL;
  78. ret->pub_key = NULL;
  79. ret->priv_key= NULL;
  80. ret->enc_flag= 0;
  81. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  82. ret->references= 1;
  83. ret->method_data = NULL;
  84. return(ret);
  85. }
  86. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  87. {
  88. EC_KEY *ret = EC_KEY_new();
  89. if (ret == NULL)
  90. return NULL;
  91. ret->group = EC_GROUP_new_by_curve_name(nid);
  92. if (ret->group == NULL)
  93. {
  94. EC_KEY_free(ret);
  95. return NULL;
  96. }
  97. return ret;
  98. }
  99. void EC_KEY_free(EC_KEY *r)
  100. {
  101. int i;
  102. if (r == NULL) return;
  103. i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC);
  104. #ifdef REF_PRINT
  105. REF_PRINT("EC_KEY",r);
  106. #endif
  107. if (i > 0) return;
  108. #ifdef REF_CHECK
  109. if (i < 0)
  110. {
  111. fprintf(stderr,"EC_KEY_free, bad reference count\n");
  112. abort();
  113. }
  114. #endif
  115. if (r->group != NULL)
  116. EC_GROUP_free(r->group);
  117. if (r->pub_key != NULL)
  118. EC_POINT_free(r->pub_key);
  119. if (r->priv_key != NULL)
  120. BN_clear_free(r->priv_key);
  121. EC_EX_DATA_free_all_data(&r->method_data);
  122. OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
  123. OPENSSL_free(r);
  124. }
  125. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  126. {
  127. EC_EXTRA_DATA *d;
  128. if (dest == NULL || src == NULL)
  129. {
  130. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  131. return NULL;
  132. }
  133. /* copy the parameters */
  134. if (src->group)
  135. {
  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. {
  149. if (dest->pub_key)
  150. EC_POINT_free(dest->pub_key);
  151. dest->pub_key = EC_POINT_new(src->group);
  152. if (dest->pub_key == NULL)
  153. return NULL;
  154. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  155. return NULL;
  156. }
  157. /* copy the private key */
  158. if (src->priv_key)
  159. {
  160. if (dest->priv_key == NULL)
  161. {
  162. dest->priv_key = BN_new();
  163. if (dest->priv_key == NULL)
  164. return NULL;
  165. }
  166. if (!BN_copy(dest->priv_key, src->priv_key))
  167. return NULL;
  168. }
  169. /* copy method/extra data */
  170. EC_EX_DATA_free_all_data(&dest->method_data);
  171. for (d = src->method_data; d != NULL; d = d->next)
  172. {
  173. void *t = d->dup_func(d->data);
  174. if (t == NULL)
  175. return 0;
  176. if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func, d->free_func, d->clear_free_func))
  177. return 0;
  178. }
  179. /* copy the rest */
  180. dest->enc_flag = src->enc_flag;
  181. dest->conv_form = src->conv_form;
  182. dest->version = src->version;
  183. return dest;
  184. }
  185. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  186. {
  187. EC_KEY *ret = EC_KEY_new();
  188. if (ret == NULL)
  189. return NULL;
  190. if (EC_KEY_copy(ret, ec_key) == NULL)
  191. {
  192. EC_KEY_free(ret);
  193. return NULL;
  194. }
  195. return ret;
  196. }
  197. int EC_KEY_up_ref(EC_KEY *r)
  198. {
  199. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
  200. #ifdef REF_PRINT
  201. REF_PRINT("EC_KEY",r);
  202. #endif
  203. #ifdef REF_CHECK
  204. if (i < 2)
  205. {
  206. fprintf(stderr, "EC_KEY_up, bad reference count\n");
  207. abort();
  208. }
  209. #endif
  210. return ((i > 1) ? 1 : 0);
  211. }
  212. int EC_KEY_generate_key(EC_KEY *eckey)
  213. {
  214. int ok = 0;
  215. BN_CTX *ctx = NULL;
  216. BIGNUM *priv_key = NULL, *order = NULL;
  217. EC_POINT *pub_key = NULL;
  218. if (!eckey || !eckey->group)
  219. {
  220. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  221. return 0;
  222. }
  223. if ((order = BN_new()) == NULL) goto err;
  224. if ((ctx = BN_CTX_new()) == NULL) goto err;
  225. if (eckey->priv_key == NULL)
  226. {
  227. priv_key = BN_new();
  228. if (priv_key == NULL)
  229. goto err;
  230. }
  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. {
  241. pub_key = EC_POINT_new(eckey->group);
  242. if (pub_key == NULL)
  243. goto err;
  244. }
  245. else
  246. pub_key = eckey->pub_key;
  247. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  248. goto err;
  249. eckey->priv_key = priv_key;
  250. eckey->pub_key = pub_key;
  251. ok=1;
  252. err:
  253. if (order)
  254. BN_free(order);
  255. if (pub_key != NULL && eckey->pub_key == NULL)
  256. EC_POINT_free(pub_key);
  257. if (priv_key != NULL && eckey->priv_key == NULL)
  258. BN_free(priv_key);
  259. if (ctx != NULL)
  260. BN_CTX_free(ctx);
  261. return(ok);
  262. }
  263. int EC_KEY_check_key(const EC_KEY *eckey)
  264. {
  265. int ok = 0;
  266. BN_CTX *ctx = NULL;
  267. BIGNUM *order = NULL;
  268. EC_POINT *point = NULL;
  269. if (!eckey || !eckey->group || !eckey->pub_key)
  270. {
  271. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  272. return 0;
  273. }
  274. if ((ctx = BN_CTX_new()) == NULL)
  275. goto err;
  276. if ((order = BN_new()) == NULL)
  277. goto err;
  278. if ((point = EC_POINT_new(eckey->group)) == NULL)
  279. goto err;
  280. /* testing whether the pub_key is on the elliptic curve */
  281. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx))
  282. {
  283. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  284. goto err;
  285. }
  286. /* testing whether pub_key * order is the point at infinity */
  287. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  288. {
  289. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  290. goto err;
  291. }
  292. if (!EC_POINT_copy(point, eckey->pub_key))
  293. {
  294. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  295. goto err;
  296. }
  297. if (!EC_POINT_mul(eckey->group, point, order, NULL, NULL, ctx))
  298. {
  299. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  300. goto err;
  301. }
  302. if (!EC_POINT_is_at_infinity(eckey->group, point))
  303. {
  304. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  305. goto err;
  306. }
  307. /* in case the priv_key is present :
  308. * check if generator * priv_key == pub_key
  309. */
  310. if (eckey->priv_key)
  311. {
  312. if (BN_cmp(eckey->priv_key, order) >= 0)
  313. {
  314. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  315. goto err;
  316. }
  317. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  318. NULL, NULL, ctx))
  319. {
  320. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  321. goto err;
  322. }
  323. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key,
  324. ctx) != 0)
  325. {
  326. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  327. goto err;
  328. }
  329. }
  330. ok = 1;
  331. err:
  332. if (ctx != NULL)
  333. BN_CTX_free(ctx);
  334. if (order != NULL)
  335. BN_free(order);
  336. if (point != NULL)
  337. EC_POINT_free(point);
  338. return(ok);
  339. }
  340. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  341. {
  342. return key->group;
  343. }
  344. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  345. {
  346. if (key->group != NULL)
  347. EC_GROUP_free(key->group);
  348. key->group = EC_GROUP_dup(group);
  349. return (key->group == NULL) ? 0 : 1;
  350. }
  351. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  352. {
  353. return key->priv_key;
  354. }
  355. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  356. {
  357. if (key->priv_key)
  358. BN_clear_free(key->priv_key);
  359. key->priv_key = BN_dup(priv_key);
  360. return (key->priv_key == NULL) ? 0 : 1;
  361. }
  362. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  363. {
  364. return key->pub_key;
  365. }
  366. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  367. {
  368. if (key->pub_key != NULL)
  369. EC_POINT_free(key->pub_key);
  370. key->pub_key = EC_POINT_dup(pub_key, key->group);
  371. return (key->pub_key == NULL) ? 0 : 1;
  372. }
  373. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  374. {
  375. return key->enc_flag;
  376. }
  377. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  378. {
  379. key->enc_flag = flags;
  380. }
  381. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  382. {
  383. return key->conv_form;
  384. }
  385. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  386. {
  387. key->conv_form = cform;
  388. if (key->group != NULL)
  389. EC_GROUP_set_point_conversion_form(key->group, cform);
  390. }
  391. void *EC_KEY_get_key_method_data(EC_KEY *key,
  392. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  393. {
  394. return EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
  395. }
  396. void EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
  397. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  398. {
  399. EC_EXTRA_DATA *ex_data;
  400. CRYPTO_w_lock(CRYPTO_LOCK_EC);
  401. ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
  402. if (ex_data == NULL)
  403. EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func);
  404. CRYPTO_w_unlock(CRYPTO_LOCK_EC);
  405. }
  406. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  407. {
  408. if (key->group != NULL)
  409. EC_GROUP_set_asn1_flag(key->group, flag);
  410. }
  411. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  412. {
  413. if (key->group == NULL)
  414. return 0;
  415. return EC_GROUP_precompute_mult(key->group, ctx);
  416. }