ec_key.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* crypto/ec/ec_key.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2002 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_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->meth_data = NULL;
  84. return(ret);
  85. }
  86. void EC_KEY_free(EC_KEY *r)
  87. {
  88. int i;
  89. if (r == NULL) return;
  90. i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC);
  91. #ifdef REF_PRINT
  92. REF_PRINT("EC_KEY",r);
  93. #endif
  94. if (i > 0) return;
  95. #ifdef REF_CHECK
  96. if (i < 0)
  97. {
  98. fprintf(stderr,"EC_KEY_free, bad reference count\n");
  99. abort();
  100. }
  101. #endif
  102. if (r->group != NULL)
  103. EC_GROUP_free(r->group);
  104. if (r->pub_key != NULL)
  105. EC_POINT_free(r->pub_key);
  106. if (r->priv_key != NULL)
  107. BN_clear_free(r->priv_key);
  108. if (r->meth_data && r->meth_data->finish)
  109. r->meth_data->finish(r);
  110. OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
  111. OPENSSL_free(r);
  112. }
  113. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  114. {
  115. if (dest == NULL || src == NULL)
  116. {
  117. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  118. return NULL;
  119. }
  120. /* copy the parameters */
  121. if (src->group)
  122. {
  123. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  124. /* clear the old group */
  125. if (dest->group)
  126. EC_GROUP_free(dest->group);
  127. dest->group = EC_GROUP_new(meth);
  128. if (dest->group == NULL)
  129. return NULL;
  130. if (!EC_GROUP_copy(dest->group, src->group))
  131. return NULL;
  132. }
  133. /* copy the public key */
  134. if (src->pub_key && src->group)
  135. {
  136. if (dest->pub_key)
  137. EC_POINT_free(dest->pub_key);
  138. dest->pub_key = EC_POINT_new(src->group);
  139. if (dest->pub_key == NULL)
  140. return NULL;
  141. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  142. return NULL;
  143. }
  144. /* copy the private key */
  145. if (src->priv_key)
  146. {
  147. if (dest->priv_key == NULL)
  148. {
  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 the rest */
  157. dest->enc_flag = src->enc_flag;
  158. dest->conv_form = src->conv_form;
  159. dest->version = src->version;
  160. return dest;
  161. }
  162. EC_KEY *EC_KEY_dup(const EC_KEY *eckey)
  163. {
  164. EC_KEY *ret = NULL;
  165. int ok = 1;
  166. ret = EC_KEY_new();
  167. if (ret == NULL)
  168. return NULL;
  169. /* copy the parameters */
  170. if (eckey->group)
  171. {
  172. ret->group = EC_GROUP_dup(eckey->group);
  173. if (ret->group == NULL)
  174. ok = 0;
  175. }
  176. /* copy the public key */
  177. if (eckey->pub_key && eckey->group)
  178. {
  179. ret->pub_key = EC_POINT_dup(eckey->pub_key, eckey->group);
  180. if (ret->pub_key == NULL)
  181. ok = 0;
  182. }
  183. /* copy the private key */
  184. if (eckey->priv_key)
  185. {
  186. ret->priv_key = BN_dup(eckey->priv_key);
  187. if (ret->priv_key == NULL)
  188. ok = 0;
  189. }
  190. /* copy the rest */
  191. ret->enc_flag = eckey->enc_flag;
  192. ret->conv_form = eckey->conv_form;
  193. ret->version = eckey->version;
  194. if (!ok)
  195. {
  196. EC_KEY_free(ret);
  197. ret = NULL;
  198. }
  199. return ret;
  200. }
  201. int EC_KEY_up_ref(EC_KEY *r)
  202. {
  203. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
  204. #ifdef REF_PRINT
  205. REF_PRINT("EC_KEY",r);
  206. #endif
  207. #ifdef REF_CHECK
  208. if (i < 2)
  209. {
  210. fprintf(stderr, "EC_KEY_up, bad reference count\n");
  211. abort();
  212. }
  213. #endif
  214. return ((i > 1) ? 1 : 0);
  215. }
  216. int EC_KEY_generate_key(EC_KEY *eckey)
  217. {
  218. int ok = 0;
  219. BN_CTX *ctx = NULL;
  220. BIGNUM *priv_key = NULL, *order = NULL;
  221. EC_POINT *pub_key = NULL;
  222. if (!eckey || !eckey->group)
  223. {
  224. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  225. return 0;
  226. }
  227. if ((order = BN_new()) == NULL) goto err;
  228. if ((ctx = BN_CTX_new()) == NULL) goto err;
  229. if (eckey->priv_key == NULL)
  230. {
  231. priv_key = BN_new();
  232. if (priv_key == NULL)
  233. goto err;
  234. }
  235. else
  236. priv_key = eckey->priv_key;
  237. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  238. goto err;
  239. do
  240. if (!BN_rand_range(priv_key, order))
  241. goto err;
  242. while (BN_is_zero(priv_key));
  243. if (eckey->pub_key == NULL)
  244. {
  245. pub_key = EC_POINT_new(eckey->group);
  246. if (pub_key == NULL)
  247. goto err;
  248. }
  249. else
  250. pub_key = eckey->pub_key;
  251. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  252. goto err;
  253. eckey->priv_key = priv_key;
  254. eckey->pub_key = pub_key;
  255. ok=1;
  256. err:
  257. if (order)
  258. BN_free(order);
  259. if (pub_key != NULL && eckey->pub_key == NULL)
  260. EC_POINT_free(pub_key);
  261. if (priv_key != NULL && eckey->priv_key == NULL)
  262. BN_free(priv_key);
  263. if (ctx != NULL)
  264. BN_CTX_free(ctx);
  265. return(ok);
  266. }
  267. int EC_KEY_check_key(const EC_KEY *eckey)
  268. {
  269. int ok = 0;
  270. BN_CTX *ctx = NULL;
  271. BIGNUM *order = NULL;
  272. EC_POINT *point = NULL;
  273. if (!eckey || !eckey->group || !eckey->pub_key)
  274. {
  275. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  276. return 0;
  277. }
  278. if ((ctx = BN_CTX_new()) == NULL)
  279. goto err;
  280. if ((order = BN_new()) == NULL)
  281. goto err;
  282. if ((point = EC_POINT_new(eckey->group)) == NULL)
  283. goto err;
  284. /* testing whether the pub_key is on the elliptic curve */
  285. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx))
  286. {
  287. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  288. goto err;
  289. }
  290. /* testing whether pub_key * order is the point at infinity */
  291. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  292. {
  293. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  294. goto err;
  295. }
  296. if (!EC_POINT_copy(point, eckey->pub_key))
  297. {
  298. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  299. goto err;
  300. }
  301. if (!EC_POINT_mul(eckey->group, point, order, NULL, NULL, ctx))
  302. {
  303. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  304. goto err;
  305. }
  306. if (!EC_POINT_is_at_infinity(eckey->group, point))
  307. {
  308. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  309. goto err;
  310. }
  311. /* in case the priv_key is present :
  312. * check if generator * priv_key == pub_key
  313. */
  314. if (eckey->priv_key)
  315. {
  316. if (BN_cmp(eckey->priv_key, order) >= 0)
  317. {
  318. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  319. goto err;
  320. }
  321. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  322. NULL, NULL, ctx))
  323. {
  324. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  325. goto err;
  326. }
  327. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key,
  328. ctx) != 0)
  329. {
  330. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  331. goto err;
  332. }
  333. }
  334. ok = 1;
  335. err:
  336. if (ctx != NULL)
  337. BN_CTX_free(ctx);
  338. if (order != NULL)
  339. BN_free(order);
  340. if (point != NULL)
  341. EC_POINT_free(point);
  342. return(ok);
  343. }