Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

518 rindas
14 KiB

  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec_key.h>
  68. #include <openssl/ec.h>
  69. #include <openssl/engine.h>
  70. #include <openssl/err.h>
  71. #include <openssl/ex_data.h>
  72. #include <openssl/mem.h>
  73. #include "internal.h"
  74. EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
  75. EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
  76. EC_KEY *ret = (EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
  77. if (ret == NULL) {
  78. OPENSSL_PUT_ERROR(EC, EC_KEY_new_method, ERR_R_MALLOC_FAILURE);
  79. return NULL;
  80. }
  81. memset(ret, 0, sizeof(EC_KEY));
  82. if (engine) {
  83. ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
  84. }
  85. if (ret->ecdsa_meth) {
  86. METHOD_ref(ret->ecdsa_meth);
  87. }
  88. ret->version = 1;
  89. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  90. ret->references = 1;
  91. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
  92. goto err1;
  93. }
  94. if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
  95. goto err2;
  96. }
  97. return ret;
  98. err2:
  99. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
  100. err1:
  101. if (ret->ecdsa_meth) {
  102. METHOD_unref(ret->ecdsa_meth);
  103. }
  104. OPENSSL_free(ret);
  105. return NULL;
  106. }
  107. EC_KEY *EC_KEY_new_by_curve_name(int nid) {
  108. EC_KEY *ret = EC_KEY_new();
  109. if (ret == NULL) {
  110. return NULL;
  111. }
  112. ret->group = EC_GROUP_new_by_curve_name(nid);
  113. if (ret->group == NULL) {
  114. EC_KEY_free(ret);
  115. return NULL;
  116. }
  117. return ret;
  118. }
  119. void EC_KEY_free(EC_KEY *r) {
  120. if (r == NULL) {
  121. return;
  122. }
  123. if (CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC)) {
  124. return;
  125. }
  126. if (r->ecdsa_meth) {
  127. if (r->ecdsa_meth->finish) {
  128. r->ecdsa_meth->finish(r);
  129. }
  130. METHOD_unref(r->ecdsa_meth);
  131. }
  132. if (r->group != NULL) {
  133. EC_GROUP_free(r->group);
  134. }
  135. if (r->pub_key != NULL) {
  136. EC_POINT_free(r->pub_key);
  137. }
  138. if (r->priv_key != NULL) {
  139. BN_clear_free(r->priv_key);
  140. }
  141. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
  142. OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
  143. OPENSSL_free(r);
  144. }
  145. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) {
  146. if (dest == NULL || src == NULL) {
  147. OPENSSL_PUT_ERROR(EC, EC_KEY_copy, ERR_R_PASSED_NULL_PARAMETER);
  148. return NULL;
  149. }
  150. /* copy the parameters */
  151. if (src->group) {
  152. /* TODO(fork): duplicating the group seems wasteful. */
  153. const EC_METHOD *meth = src->group->meth;
  154. /* clear the old group */
  155. if (dest->group) {
  156. EC_GROUP_free(dest->group);
  157. }
  158. dest->group = ec_group_new(meth);
  159. if (dest->group == NULL) {
  160. return NULL;
  161. }
  162. if (!EC_GROUP_copy(dest->group, src->group)) {
  163. return NULL;
  164. }
  165. }
  166. /* copy the public key */
  167. if (src->pub_key && src->group) {
  168. if (dest->pub_key) {
  169. EC_POINT_free(dest->pub_key);
  170. }
  171. dest->pub_key = EC_POINT_new(src->group);
  172. if (dest->pub_key == NULL) {
  173. return NULL;
  174. }
  175. if (!EC_POINT_copy(dest->pub_key, src->pub_key)) {
  176. return NULL;
  177. }
  178. }
  179. /* copy the private key */
  180. if (src->priv_key) {
  181. if (dest->priv_key == NULL) {
  182. dest->priv_key = BN_new();
  183. if (dest->priv_key == NULL) {
  184. return NULL;
  185. }
  186. }
  187. if (!BN_copy(dest->priv_key, src->priv_key)) {
  188. return NULL;
  189. }
  190. }
  191. /* copy method/extra data */
  192. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, dest, &dest->ex_data);
  193. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, &dest->ex_data,
  194. &src->ex_data)) {
  195. return NULL;
  196. }
  197. /* copy the rest */
  198. dest->enc_flag = src->enc_flag;
  199. dest->conv_form = src->conv_form;
  200. dest->version = src->version;
  201. dest->flags = src->flags;
  202. return dest;
  203. }
  204. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key) {
  205. EC_KEY *ret = EC_KEY_new();
  206. if (ret == NULL) {
  207. return NULL;
  208. }
  209. if (EC_KEY_copy(ret, ec_key) == NULL) {
  210. EC_KEY_free(ret);
  211. return NULL;
  212. }
  213. return ret;
  214. }
  215. int EC_KEY_up_ref(EC_KEY *r) {
  216. return CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC) > 1;
  217. }
  218. int EC_KEY_is_opaque(const EC_KEY *key) {
  219. return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
  220. }
  221. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
  222. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
  223. if (key->group != NULL) {
  224. EC_GROUP_free(key->group);
  225. }
  226. /* TODO(fork): duplicating the group seems wasteful but see
  227. * |EC_KEY_set_conv_form|. */
  228. key->group = EC_GROUP_dup(group);
  229. return (key->group == NULL) ? 0 : 1;
  230. }
  231. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
  232. return key->priv_key;
  233. }
  234. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
  235. if (key->priv_key) {
  236. BN_clear_free(key->priv_key);
  237. }
  238. key->priv_key = BN_dup(priv_key);
  239. return (key->priv_key == NULL) ? 0 : 1;
  240. }
  241. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) {
  242. return key->pub_key;
  243. }
  244. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
  245. if (key->pub_key != NULL) {
  246. EC_POINT_free(key->pub_key);
  247. }
  248. key->pub_key = EC_POINT_dup(pub_key, key->group);
  249. return (key->pub_key == NULL) ? 0 : 1;
  250. }
  251. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; }
  252. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) {
  253. key->enc_flag = flags;
  254. }
  255. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) {
  256. return key->conv_form;
  257. }
  258. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) {
  259. key->conv_form = cform;
  260. if (key->group != NULL) {
  261. EC_GROUP_set_point_conversion_form(key->group, cform);
  262. }
  263. }
  264. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx) {
  265. if (key->group == NULL) {
  266. return 0;
  267. }
  268. return EC_GROUP_precompute_mult(key->group, ctx);
  269. }
  270. int EC_KEY_check_key(const EC_KEY *eckey) {
  271. int ok = 0;
  272. BN_CTX *ctx = NULL;
  273. const BIGNUM *order = NULL;
  274. EC_POINT *point = NULL;
  275. if (!eckey || !eckey->group || !eckey->pub_key) {
  276. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, ERR_R_PASSED_NULL_PARAMETER);
  277. return 0;
  278. }
  279. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  280. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_POINT_AT_INFINITY);
  281. goto err;
  282. }
  283. ctx = BN_CTX_new();
  284. point = EC_POINT_new(eckey->group);
  285. if (ctx == NULL ||
  286. point == NULL) {
  287. goto err;
  288. }
  289. /* testing whether the pub_key is on the elliptic curve */
  290. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) {
  291. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_POINT_IS_NOT_ON_CURVE);
  292. goto err;
  293. }
  294. /* testing whether pub_key * order is the point at infinity */
  295. /* TODO(fork): can this be skipped if the cofactor is one or if we're about
  296. * to check the private key, below? */
  297. order = &eckey->group->order;
  298. if (BN_is_zero(order)) {
  299. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_INVALID_GROUP_ORDER);
  300. goto err;
  301. }
  302. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  303. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, ERR_R_EC_LIB);
  304. goto err;
  305. }
  306. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  307. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_WRONG_ORDER);
  308. goto err;
  309. }
  310. /* in case the priv_key is present :
  311. * check if generator * priv_key == pub_key
  312. */
  313. if (eckey->priv_key) {
  314. if (BN_cmp(eckey->priv_key, order) >= 0) {
  315. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_WRONG_ORDER);
  316. goto err;
  317. }
  318. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
  319. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, ERR_R_EC_LIB);
  320. goto err;
  321. }
  322. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  323. OPENSSL_PUT_ERROR(EC, EC_KEY_check_key, EC_R_INVALID_PRIVATE_KEY);
  324. goto err;
  325. }
  326. }
  327. ok = 1;
  328. err:
  329. if (ctx != NULL)
  330. BN_CTX_free(ctx);
  331. if (point != NULL)
  332. EC_POINT_free(point);
  333. return ok;
  334. }
  335. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  336. BIGNUM *y) {
  337. BN_CTX *ctx = NULL;
  338. BIGNUM *tx, *ty;
  339. EC_POINT *point = NULL;
  340. int ok = 0;
  341. if (!key || !key->group || !x || !y) {
  342. OPENSSL_PUT_ERROR(EC, EC_KEY_set_public_key_affine_coordinates,
  343. ERR_R_PASSED_NULL_PARAMETER);
  344. return 0;
  345. }
  346. ctx = BN_CTX_new();
  347. point = EC_POINT_new(key->group);
  348. if (ctx == NULL ||
  349. point == NULL) {
  350. goto err;
  351. }
  352. tx = BN_CTX_get(ctx);
  353. ty = BN_CTX_get(ctx);
  354. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, ctx) ||
  355. !EC_POINT_get_affine_coordinates_GFp(key->group, point, tx, ty, ctx)) {
  356. goto err;
  357. }
  358. /* Check if retrieved coordinates match originals: if not values
  359. * are out of range. */
  360. if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
  361. OPENSSL_PUT_ERROR(EC, EC_KEY_set_public_key_affine_coordinates,
  362. EC_R_COORDINATES_OUT_OF_RANGE);
  363. goto err;
  364. }
  365. if (!EC_KEY_set_public_key(key, point)) {
  366. goto err;
  367. }
  368. if (EC_KEY_check_key(key) == 0) {
  369. goto err;
  370. }
  371. ok = 1;
  372. err:
  373. if (ctx)
  374. BN_CTX_free(ctx);
  375. if (point)
  376. EC_POINT_free(point);
  377. return ok;
  378. }
  379. int EC_KEY_generate_key(EC_KEY *eckey) {
  380. int ok = 0;
  381. BN_CTX *ctx = NULL;
  382. BIGNUM *priv_key = NULL, *order = NULL;
  383. EC_POINT *pub_key = NULL;
  384. if (!eckey || !eckey->group) {
  385. OPENSSL_PUT_ERROR(EC, EC_KEY_generate_key, ERR_R_PASSED_NULL_PARAMETER);
  386. return 0;
  387. }
  388. order = BN_new();
  389. ctx = BN_CTX_new();
  390. if (order == NULL ||
  391. ctx == NULL) {
  392. goto err;
  393. }
  394. if (eckey->priv_key == NULL) {
  395. priv_key = BN_new();
  396. if (priv_key == NULL) {
  397. goto err;
  398. }
  399. } else {
  400. priv_key = eckey->priv_key;
  401. }
  402. if (!EC_GROUP_get_order(eckey->group, order, ctx)) {
  403. goto err;
  404. }
  405. do {
  406. if (!BN_rand_range(priv_key, order)) {
  407. goto err;
  408. }
  409. } while (BN_is_zero(priv_key));
  410. if (eckey->pub_key == NULL) {
  411. pub_key = EC_POINT_new(eckey->group);
  412. if (pub_key == NULL) {
  413. goto err;
  414. }
  415. } else {
  416. pub_key = eckey->pub_key;
  417. }
  418. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) {
  419. goto err;
  420. }
  421. eckey->priv_key = priv_key;
  422. eckey->pub_key = pub_key;
  423. ok = 1;
  424. err:
  425. if (order)
  426. BN_free(order);
  427. if (pub_key != NULL && eckey->pub_key == NULL)
  428. EC_POINT_free(pub_key);
  429. if (priv_key != NULL && eckey->priv_key == NULL)
  430. BN_free(priv_key);
  431. if (ctx != NULL)
  432. BN_CTX_free(ctx);
  433. return ok;
  434. }
  435. int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  436. CRYPTO_EX_dup *dup_func,
  437. CRYPTO_EX_free *free_func) {
  438. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, argl, argp, new_func,
  439. dup_func, free_func);
  440. }
  441. int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
  442. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  443. }
  444. void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
  445. return CRYPTO_get_ex_data(&d->ex_data, idx);
  446. }