ec_key.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Written by Nils Larsch for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * openssl-core@openssl.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. * Portions originally developed by SUN MICROSYSTEMS, INC., and
  60. * contributed to the OpenSSL project.
  61. */
  62. #include <internal/cryptlib.h>
  63. #include <string.h>
  64. #include "ec_lcl.h"
  65. #include <openssl/err.h>
  66. #ifndef OPENSSL_NO_ENGINE
  67. # include <openssl/engine.h>
  68. #endif
  69. EC_KEY *EC_KEY_new(void)
  70. {
  71. return EC_KEY_new_method(NULL);
  72. }
  73. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  74. {
  75. EC_KEY *ret = EC_KEY_new();
  76. if (ret == NULL)
  77. return NULL;
  78. ret->group = EC_GROUP_new_by_curve_name(nid);
  79. if (ret->group == NULL) {
  80. EC_KEY_free(ret);
  81. return NULL;
  82. }
  83. if (ret->meth->set_group != NULL
  84. && ret->meth->set_group(ret, ret->group) == 0) {
  85. EC_KEY_free(ret);
  86. return NULL;
  87. }
  88. return ret;
  89. }
  90. void EC_KEY_free(EC_KEY *r)
  91. {
  92. int i;
  93. if (r == NULL)
  94. return;
  95. i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);
  96. #ifdef REF_PRINT
  97. REF_PRINT("EC_KEY", r);
  98. #endif
  99. if (i > 0)
  100. return;
  101. #ifdef REF_CHECK
  102. if (i < 0) {
  103. fprintf(stderr, "EC_KEY_free, bad reference count\n");
  104. abort();
  105. }
  106. #endif
  107. if (r->meth->finish != NULL)
  108. r->meth->finish(r);
  109. #ifndef OPENSSL_NO_ENGINE
  110. if (r->engine != NULL)
  111. ENGINE_finish(r->engine);
  112. #endif
  113. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
  114. EC_GROUP_free(r->group);
  115. EC_POINT_free(r->pub_key);
  116. BN_clear_free(r->priv_key);
  117. OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
  118. }
  119. EC_KEY *EC_KEY_copy(EC_KEY *dest, EC_KEY *src)
  120. {
  121. if (dest == NULL || src == NULL) {
  122. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  123. return NULL;
  124. }
  125. if (src->meth != dest->meth) {
  126. if (dest->meth->finish != NULL)
  127. dest->meth->finish(dest);
  128. #ifndef OPENSSL_NO_ENGINE
  129. if (dest->engine != NULL && ENGINE_finish(dest->engine) == 0)
  130. return 0;
  131. dest->engine = NULL;
  132. #endif
  133. }
  134. /* copy the parameters */
  135. if (src->group != NULL) {
  136. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  137. /* clear the old group */
  138. EC_GROUP_free(dest->group);
  139. dest->group = EC_GROUP_new(meth);
  140. if (dest->group == NULL)
  141. return NULL;
  142. if (!EC_GROUP_copy(dest->group, src->group))
  143. return NULL;
  144. }
  145. /* copy the public key */
  146. if (src->pub_key != NULL && src->group != NULL) {
  147. EC_POINT_free(dest->pub_key);
  148. dest->pub_key = EC_POINT_new(src->group);
  149. if (dest->pub_key == NULL)
  150. return NULL;
  151. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  152. return NULL;
  153. }
  154. /* copy the private key */
  155. if (src->priv_key != NULL) {
  156. if (dest->priv_key == NULL) {
  157. dest->priv_key = BN_new();
  158. if (dest->priv_key == NULL)
  159. return NULL;
  160. }
  161. if (!BN_copy(dest->priv_key, src->priv_key))
  162. return NULL;
  163. }
  164. /* copy the rest */
  165. dest->enc_flag = src->enc_flag;
  166. dest->conv_form = src->conv_form;
  167. dest->version = src->version;
  168. dest->flags = src->flags;
  169. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  170. &dest->ex_data, &src->ex_data))
  171. return NULL;
  172. if (src->meth != dest->meth) {
  173. #ifndef OPENSSL_NO_ENGINE
  174. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  175. return NULL;
  176. dest->engine = src->engine;
  177. #endif
  178. dest->meth = src->meth;
  179. }
  180. if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
  181. return NULL;
  182. return dest;
  183. }
  184. EC_KEY *EC_KEY_dup(EC_KEY *ec_key)
  185. {
  186. EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
  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. if (eckey == NULL || eckey->group == NULL) {
  212. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  213. return 0;
  214. }
  215. if (eckey->meth->keygen != NULL)
  216. return eckey->meth->keygen(eckey);
  217. ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
  218. return 0;
  219. }
  220. int ossl_ec_key_gen(EC_KEY *eckey)
  221. {
  222. int ok = 0;
  223. BN_CTX *ctx = NULL;
  224. BIGNUM *priv_key = NULL, *order = NULL;
  225. EC_POINT *pub_key = NULL;
  226. if ((order = BN_new()) == NULL)
  227. goto err;
  228. if ((ctx = BN_CTX_new()) == NULL)
  229. goto err;
  230. if (eckey->priv_key == NULL) {
  231. priv_key = BN_new();
  232. if (priv_key == NULL)
  233. goto err;
  234. } else
  235. priv_key = eckey->priv_key;
  236. if (!EC_GROUP_get_order(eckey->group, order, ctx))
  237. goto err;
  238. do
  239. if (!BN_rand_range(priv_key, order))
  240. goto err;
  241. while (BN_is_zero(priv_key)) ;
  242. if (eckey->pub_key == NULL) {
  243. pub_key = EC_POINT_new(eckey->group);
  244. if (pub_key == NULL)
  245. goto err;
  246. } else
  247. pub_key = eckey->pub_key;
  248. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  249. goto err;
  250. eckey->priv_key = priv_key;
  251. eckey->pub_key = pub_key;
  252. ok = 1;
  253. err:
  254. BN_free(order);
  255. if (eckey->pub_key == NULL)
  256. EC_POINT_free(pub_key);
  257. if (eckey->priv_key != priv_key)
  258. BN_free(priv_key);
  259. BN_CTX_free(ctx);
  260. return (ok);
  261. }
  262. int EC_KEY_check_key(const EC_KEY *eckey)
  263. {
  264. int ok = 0;
  265. BN_CTX *ctx = NULL;
  266. const BIGNUM *order = NULL;
  267. EC_POINT *point = NULL;
  268. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  269. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  270. return 0;
  271. }
  272. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  273. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY);
  274. goto err;
  275. }
  276. if ((ctx = BN_CTX_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) <= 0) {
  282. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  283. goto err;
  284. }
  285. /* testing whether pub_key * order is the point at infinity */
  286. order = eckey->group->order;
  287. if (BN_is_zero(order)) {
  288. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  289. goto err;
  290. }
  291. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  292. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  293. goto err;
  294. }
  295. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  296. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  297. goto err;
  298. }
  299. /*
  300. * in case the priv_key is present : check if generator * priv_key ==
  301. * pub_key
  302. */
  303. if (eckey->priv_key != NULL) {
  304. if (BN_cmp(eckey->priv_key, order) >= 0) {
  305. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
  306. goto err;
  307. }
  308. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  309. NULL, NULL, ctx)) {
  310. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
  311. goto err;
  312. }
  313. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  314. ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  315. goto err;
  316. }
  317. }
  318. ok = 1;
  319. err:
  320. BN_CTX_free(ctx);
  321. EC_POINT_free(point);
  322. return (ok);
  323. }
  324. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  325. BIGNUM *y)
  326. {
  327. BN_CTX *ctx = NULL;
  328. BIGNUM *tx, *ty;
  329. EC_POINT *point = NULL;
  330. int ok = 0;
  331. #ifndef OPENSSL_NO_EC2M
  332. int tmp_nid, is_char_two = 0;
  333. #endif
  334. if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
  335. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  336. ERR_R_PASSED_NULL_PARAMETER);
  337. return 0;
  338. }
  339. ctx = BN_CTX_new();
  340. if (ctx == NULL)
  341. goto err;
  342. point = EC_POINT_new(key->group);
  343. if (point == NULL)
  344. goto err;
  345. tx = BN_CTX_get(ctx);
  346. ty = BN_CTX_get(ctx);
  347. if (ty == NULL)
  348. goto err;
  349. #ifndef OPENSSL_NO_EC2M
  350. tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
  351. if (tmp_nid == NID_X9_62_characteristic_two_field)
  352. is_char_two = 1;
  353. if (is_char_two) {
  354. if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
  355. x, y, ctx))
  356. goto err;
  357. if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
  358. tx, ty, ctx))
  359. goto err;
  360. } else
  361. #endif
  362. {
  363. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
  364. x, y, ctx))
  365. goto err;
  366. if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
  367. tx, ty, ctx))
  368. goto err;
  369. }
  370. /*
  371. * Check if retrieved coordinates match originals and are less than field
  372. * order: if not values are out of range.
  373. */
  374. if (BN_cmp(x, tx) || BN_cmp(y, ty)
  375. || (BN_cmp(x, key->group->field) >= 0)
  376. || (BN_cmp(y, key->group->field) >= 0)) {
  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_free(ctx);
  388. EC_POINT_free(point);
  389. return ok;
  390. }
  391. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  392. {
  393. return key->group;
  394. }
  395. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  396. {
  397. if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
  398. return 0;
  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->meth->set_private != NULL
  410. && key->meth->set_private(key, priv_key) == 0)
  411. return 0;
  412. BN_clear_free(key->priv_key);
  413. key->priv_key = BN_dup(priv_key);
  414. return (key->priv_key == NULL) ? 0 : 1;
  415. }
  416. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  417. {
  418. return key->pub_key;
  419. }
  420. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  421. {
  422. if (key->meth->set_public != NULL
  423. && key->meth->set_public(key, pub_key) == 0)
  424. return 0;
  425. EC_POINT_free(key->pub_key);
  426. key->pub_key = EC_POINT_dup(pub_key, key->group);
  427. return (key->pub_key == NULL) ? 0 : 1;
  428. }
  429. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  430. {
  431. return key->enc_flag;
  432. }
  433. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  434. {
  435. key->enc_flag = flags;
  436. }
  437. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  438. {
  439. return key->conv_form;
  440. }
  441. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  442. {
  443. key->conv_form = cform;
  444. if (key->group != NULL)
  445. EC_GROUP_set_point_conversion_form(key->group, cform);
  446. }
  447. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  448. {
  449. if (key->group != NULL)
  450. EC_GROUP_set_asn1_flag(key->group, flag);
  451. }
  452. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  453. {
  454. if (key->group == NULL)
  455. return 0;
  456. return EC_GROUP_precompute_mult(key->group, ctx);
  457. }
  458. int EC_KEY_get_flags(const EC_KEY *key)
  459. {
  460. return key->flags;
  461. }
  462. void EC_KEY_set_flags(EC_KEY *key, int flags)
  463. {
  464. key->flags |= flags;
  465. }
  466. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  467. {
  468. key->flags &= ~flags;
  469. }
  470. size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
  471. unsigned char **pbuf, BN_CTX *ctx)
  472. {
  473. if (key == NULL || key->pub_key == NULL || key->group == NULL)
  474. return 0;
  475. return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
  476. }
  477. int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
  478. BN_CTX *ctx)
  479. {
  480. if (key == NULL || key->group == NULL)
  481. return 0;
  482. if (key->pub_key == NULL)
  483. key->pub_key = EC_POINT_new(key->group);
  484. if (key->pub_key == NULL)
  485. return 0;
  486. return EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx);
  487. }