ec_key.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/cryptlib.h"
  11. #include <string.h>
  12. #include "ec_local.h"
  13. #include "internal/refcount.h"
  14. #include <openssl/err.h>
  15. #include <openssl/engine.h>
  16. #ifndef FIPS_MODE
  17. EC_KEY *EC_KEY_new(void)
  18. {
  19. return ec_key_new_method_int(NULL, NULL);
  20. }
  21. #endif
  22. EC_KEY *EC_KEY_new_ex(OPENSSL_CTX *ctx)
  23. {
  24. return ec_key_new_method_int(ctx, NULL);
  25. }
  26. EC_KEY *EC_KEY_new_by_curve_name_ex(OPENSSL_CTX *ctx, int nid)
  27. {
  28. EC_KEY *ret = EC_KEY_new_ex(ctx);
  29. if (ret == NULL)
  30. return NULL;
  31. ret->group = EC_GROUP_new_by_curve_name_ex(ctx, nid);
  32. if (ret->group == NULL) {
  33. EC_KEY_free(ret);
  34. return NULL;
  35. }
  36. if (ret->meth->set_group != NULL
  37. && ret->meth->set_group(ret, ret->group) == 0) {
  38. EC_KEY_free(ret);
  39. return NULL;
  40. }
  41. return ret;
  42. }
  43. #ifndef FIPS_MODE
  44. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  45. {
  46. return EC_KEY_new_by_curve_name_ex(NULL, nid);
  47. }
  48. #endif
  49. void EC_KEY_free(EC_KEY *r)
  50. {
  51. int i;
  52. if (r == NULL)
  53. return;
  54. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  55. REF_PRINT_COUNT("EC_KEY", r);
  56. if (i > 0)
  57. return;
  58. REF_ASSERT_ISNT(i < 0);
  59. if (r->meth != NULL && r->meth->finish != NULL)
  60. r->meth->finish(r);
  61. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  62. ENGINE_finish(r->engine);
  63. #endif
  64. if (r->group && r->group->meth->keyfinish)
  65. r->group->meth->keyfinish(r);
  66. #ifndef FIPS_MODE
  67. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
  68. #endif
  69. CRYPTO_THREAD_lock_free(r->lock);
  70. EC_GROUP_free(r->group);
  71. EC_POINT_free(r->pub_key);
  72. BN_clear_free(r->priv_key);
  73. OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
  74. }
  75. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  76. {
  77. if (dest == NULL || src == NULL) {
  78. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  79. return NULL;
  80. }
  81. if (src->meth != dest->meth) {
  82. if (dest->meth->finish != NULL)
  83. dest->meth->finish(dest);
  84. if (dest->group && dest->group->meth->keyfinish)
  85. dest->group->meth->keyfinish(dest);
  86. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  87. if (ENGINE_finish(dest->engine) == 0)
  88. return 0;
  89. dest->engine = NULL;
  90. #endif
  91. }
  92. dest->libctx = src->libctx;
  93. /* copy the parameters */
  94. if (src->group != NULL) {
  95. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  96. /* clear the old group */
  97. EC_GROUP_free(dest->group);
  98. dest->group = EC_GROUP_new_ex(src->libctx, meth);
  99. if (dest->group == NULL)
  100. return NULL;
  101. if (!EC_GROUP_copy(dest->group, src->group))
  102. return NULL;
  103. /* copy the public key */
  104. if (src->pub_key != NULL) {
  105. EC_POINT_free(dest->pub_key);
  106. dest->pub_key = EC_POINT_new(src->group);
  107. if (dest->pub_key == NULL)
  108. return NULL;
  109. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  110. return NULL;
  111. }
  112. /* copy the private key */
  113. if (src->priv_key != NULL) {
  114. if (dest->priv_key == NULL) {
  115. dest->priv_key = BN_new();
  116. if (dest->priv_key == NULL)
  117. return NULL;
  118. }
  119. if (!BN_copy(dest->priv_key, src->priv_key))
  120. return NULL;
  121. if (src->group->meth->keycopy
  122. && src->group->meth->keycopy(dest, src) == 0)
  123. return NULL;
  124. }
  125. }
  126. /* copy the rest */
  127. dest->enc_flag = src->enc_flag;
  128. dest->conv_form = src->conv_form;
  129. dest->version = src->version;
  130. dest->flags = src->flags;
  131. #ifndef FIPS_MODE
  132. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  133. &dest->ex_data, &src->ex_data))
  134. return NULL;
  135. #endif
  136. if (src->meth != dest->meth) {
  137. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  138. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  139. return NULL;
  140. dest->engine = src->engine;
  141. #endif
  142. dest->meth = src->meth;
  143. }
  144. if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
  145. return NULL;
  146. return dest;
  147. }
  148. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  149. {
  150. EC_KEY *ret = ec_key_new_method_int(ec_key->libctx, ec_key->engine);
  151. if (ret == NULL)
  152. return NULL;
  153. if (EC_KEY_copy(ret, ec_key) == NULL) {
  154. EC_KEY_free(ret);
  155. return NULL;
  156. }
  157. return ret;
  158. }
  159. int EC_KEY_up_ref(EC_KEY *r)
  160. {
  161. int i;
  162. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  163. return 0;
  164. REF_PRINT_COUNT("EC_KEY", r);
  165. REF_ASSERT_ISNT(i < 2);
  166. return ((i > 1) ? 1 : 0);
  167. }
  168. ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
  169. {
  170. return eckey->engine;
  171. }
  172. int EC_KEY_generate_key(EC_KEY *eckey)
  173. {
  174. if (eckey == NULL || eckey->group == NULL) {
  175. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  176. return 0;
  177. }
  178. if (eckey->meth->keygen != NULL)
  179. return eckey->meth->keygen(eckey);
  180. ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
  181. return 0;
  182. }
  183. int ossl_ec_key_gen(EC_KEY *eckey)
  184. {
  185. return eckey->group->meth->keygen(eckey);
  186. }
  187. /*
  188. * ECC Key generation.
  189. * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
  190. *
  191. * Params:
  192. * eckey An EC key object that contains domain params. The generated keypair
  193. * is stored in this object.
  194. * Returns 1 if the keypair was generated or 0 otherwise.
  195. */
  196. int ec_key_simple_generate_key(EC_KEY *eckey)
  197. {
  198. int ok = 0;
  199. BIGNUM *priv_key = NULL;
  200. const BIGNUM *order = NULL;
  201. EC_POINT *pub_key = NULL;
  202. const EC_GROUP *group = eckey->group;
  203. BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
  204. if (ctx == NULL)
  205. goto err;
  206. if (eckey->priv_key == NULL) {
  207. priv_key = BN_secure_new();
  208. if (priv_key == NULL)
  209. goto err;
  210. } else
  211. priv_key = eckey->priv_key;
  212. /*
  213. * Steps (1-2): Check domain parameters and security strength.
  214. * These steps must be done by the user. This would need to be
  215. * stated in the security policy.
  216. */
  217. order = EC_GROUP_get0_order(group);
  218. if (order == NULL)
  219. goto err;
  220. /*
  221. * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
  222. * Although this is slightly different from the standard, it is effectively
  223. * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
  224. * faster as the standard needs to retry more often. Also doing
  225. * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
  226. * rand so the simpler backward compatible method has been used here.
  227. */
  228. do
  229. if (!BN_priv_rand_range_ex(priv_key, order, ctx))
  230. goto err;
  231. while (BN_is_zero(priv_key)) ;
  232. if (eckey->pub_key == NULL) {
  233. pub_key = EC_POINT_new(group);
  234. if (pub_key == NULL)
  235. goto err;
  236. } else
  237. pub_key = eckey->pub_key;
  238. /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
  239. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
  240. goto err;
  241. eckey->priv_key = priv_key;
  242. eckey->pub_key = pub_key;
  243. priv_key = NULL;
  244. pub_key = NULL;
  245. ok = 1;
  246. err:
  247. /* Step (9): If there is an error return an invalid keypair. */
  248. if (!ok) {
  249. BN_clear(eckey->priv_key);
  250. if (eckey->pub_key != NULL)
  251. EC_POINT_set_to_infinity(group, eckey->pub_key);
  252. }
  253. EC_POINT_free(pub_key);
  254. BN_clear_free(priv_key);
  255. BN_CTX_free(ctx);
  256. return ok;
  257. }
  258. int ec_key_simple_generate_public_key(EC_KEY *eckey)
  259. {
  260. /*
  261. * See SP800-56AR3 5.6.1.2.2: Step (8)
  262. * pub_key = priv_key * G (where G is a point on the curve)
  263. */
  264. return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
  265. NULL, NULL);
  266. }
  267. int EC_KEY_check_key(const EC_KEY *eckey)
  268. {
  269. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  270. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  271. return 0;
  272. }
  273. if (eckey->group->meth->keycheck == NULL) {
  274. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  275. return 0;
  276. }
  277. return eckey->group->meth->keycheck(eckey);
  278. }
  279. /*
  280. * Check the range of the EC public key.
  281. * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
  282. * i.e.
  283. * - If q = odd prime p: Verify that xQ and yQ are integers in the
  284. * interval[0, p - 1], OR
  285. * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
  286. * Returns 1 if the public key has a valid range, otherwise it returns 0.
  287. */
  288. static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
  289. {
  290. int ret = 0;
  291. BIGNUM *x, *y;
  292. BN_CTX_start(ctx);
  293. x = BN_CTX_get(ctx);
  294. y = BN_CTX_get(ctx);
  295. if (y == NULL)
  296. goto err;
  297. if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
  298. goto err;
  299. if (EC_METHOD_get_field_type(key->group->meth) == NID_X9_62_prime_field) {
  300. if (BN_is_negative(x)
  301. || BN_cmp(x, key->group->field) >= 0
  302. || BN_is_negative(y)
  303. || BN_cmp(y, key->group->field) >= 0) {
  304. goto err;
  305. }
  306. } else {
  307. int m = EC_GROUP_get_degree(key->group);
  308. if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
  309. goto err;
  310. }
  311. }
  312. ret = 1;
  313. err:
  314. BN_CTX_end(ctx);
  315. return ret;
  316. }
  317. /*
  318. * ECC Key validation as specified in SP800-56A R3.
  319. * Section 5.6.2.3.3 ECC Full Public-Key Validation
  320. * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
  321. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  322. * NOTES:
  323. * Before calling this method in fips mode, there should be an assurance that
  324. * an approved elliptic-curve group is used.
  325. * Returns 1 if the key is valid, otherwise it returns 0.
  326. */
  327. int ec_key_simple_check_key(const EC_KEY *eckey)
  328. {
  329. int ok = 0;
  330. BN_CTX *ctx = NULL;
  331. const BIGNUM *order = NULL;
  332. EC_POINT *point = NULL;
  333. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  334. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  335. return 0;
  336. }
  337. /* 5.6.2.3.3 (Step 1): Q != infinity */
  338. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  339. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
  340. goto err;
  341. }
  342. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
  343. goto err;
  344. if ((point = EC_POINT_new(eckey->group)) == NULL)
  345. goto err;
  346. /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
  347. if (!ec_key_public_range_check(ctx, eckey)) {
  348. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_COORDINATES_OUT_OF_RANGE);
  349. goto err;
  350. }
  351. /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
  352. if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
  353. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  354. goto err;
  355. }
  356. order = eckey->group->order;
  357. if (BN_is_zero(order)) {
  358. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  359. goto err;
  360. }
  361. /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
  362. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  363. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
  364. goto err;
  365. }
  366. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  367. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
  368. goto err;
  369. }
  370. if (eckey->priv_key != NULL) {
  371. /*
  372. * 5.6.2.1.2 Owner Assurance of Private-Key Validity
  373. * The private key is in the range [1, order-1]
  374. */
  375. if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
  376. || BN_cmp(eckey->priv_key, order) >= 0) {
  377. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
  378. goto err;
  379. }
  380. /*
  381. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
  382. * Check if generator * priv_key = pub_key
  383. */
  384. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  385. NULL, NULL, ctx)) {
  386. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
  387. goto err;
  388. }
  389. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  390. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  391. goto err;
  392. }
  393. }
  394. ok = 1;
  395. err:
  396. BN_CTX_free(ctx);
  397. EC_POINT_free(point);
  398. return ok;
  399. }
  400. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  401. BIGNUM *y)
  402. {
  403. BN_CTX *ctx = NULL;
  404. BIGNUM *tx, *ty;
  405. EC_POINT *point = NULL;
  406. int ok = 0;
  407. if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
  408. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  409. ERR_R_PASSED_NULL_PARAMETER);
  410. return 0;
  411. }
  412. ctx = BN_CTX_new_ex(key->libctx);
  413. if (ctx == NULL)
  414. return 0;
  415. BN_CTX_start(ctx);
  416. point = EC_POINT_new(key->group);
  417. if (point == NULL)
  418. goto err;
  419. tx = BN_CTX_get(ctx);
  420. ty = BN_CTX_get(ctx);
  421. if (ty == NULL)
  422. goto err;
  423. if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
  424. goto err;
  425. if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
  426. goto err;
  427. /*
  428. * Check if retrieved coordinates match originals. The range check is done
  429. * inside EC_KEY_check_key().
  430. */
  431. if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
  432. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  433. EC_R_COORDINATES_OUT_OF_RANGE);
  434. goto err;
  435. }
  436. if (!EC_KEY_set_public_key(key, point))
  437. goto err;
  438. if (EC_KEY_check_key(key) == 0)
  439. goto err;
  440. ok = 1;
  441. err:
  442. BN_CTX_end(ctx);
  443. BN_CTX_free(ctx);
  444. EC_POINT_free(point);
  445. return ok;
  446. }
  447. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  448. {
  449. return key->group;
  450. }
  451. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  452. {
  453. if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
  454. return 0;
  455. EC_GROUP_free(key->group);
  456. key->group = EC_GROUP_dup(group);
  457. return (key->group == NULL) ? 0 : 1;
  458. }
  459. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  460. {
  461. return key->priv_key;
  462. }
  463. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  464. {
  465. if (key->group == NULL || key->group->meth == NULL)
  466. return 0;
  467. if (key->group->meth->set_private != NULL
  468. && key->group->meth->set_private(key, priv_key) == 0)
  469. return 0;
  470. if (key->meth->set_private != NULL
  471. && key->meth->set_private(key, priv_key) == 0)
  472. return 0;
  473. BN_clear_free(key->priv_key);
  474. key->priv_key = BN_dup(priv_key);
  475. return (key->priv_key == NULL) ? 0 : 1;
  476. }
  477. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  478. {
  479. return key->pub_key;
  480. }
  481. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  482. {
  483. if (key->meth->set_public != NULL
  484. && key->meth->set_public(key, pub_key) == 0)
  485. return 0;
  486. EC_POINT_free(key->pub_key);
  487. key->pub_key = EC_POINT_dup(pub_key, key->group);
  488. return (key->pub_key == NULL) ? 0 : 1;
  489. }
  490. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  491. {
  492. return key->enc_flag;
  493. }
  494. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  495. {
  496. key->enc_flag = flags;
  497. }
  498. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  499. {
  500. return key->conv_form;
  501. }
  502. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  503. {
  504. key->conv_form = cform;
  505. if (key->group != NULL)
  506. EC_GROUP_set_point_conversion_form(key->group, cform);
  507. }
  508. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  509. {
  510. if (key->group != NULL)
  511. EC_GROUP_set_asn1_flag(key->group, flag);
  512. }
  513. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  514. {
  515. if (key->group == NULL)
  516. return 0;
  517. return EC_GROUP_precompute_mult(key->group, ctx);
  518. }
  519. int EC_KEY_get_flags(const EC_KEY *key)
  520. {
  521. return key->flags;
  522. }
  523. void EC_KEY_set_flags(EC_KEY *key, int flags)
  524. {
  525. key->flags |= flags;
  526. }
  527. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  528. {
  529. key->flags &= ~flags;
  530. }
  531. size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
  532. unsigned char **pbuf, BN_CTX *ctx)
  533. {
  534. if (key == NULL || key->pub_key == NULL || key->group == NULL)
  535. return 0;
  536. return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
  537. }
  538. int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
  539. BN_CTX *ctx)
  540. {
  541. if (key == NULL || key->group == NULL)
  542. return 0;
  543. if (key->pub_key == NULL)
  544. key->pub_key = EC_POINT_new(key->group);
  545. if (key->pub_key == NULL)
  546. return 0;
  547. if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
  548. return 0;
  549. /*
  550. * Save the point conversion form.
  551. * For non-custom curves the first octet of the buffer (excluding
  552. * the last significant bit) contains the point conversion form.
  553. * EC_POINT_oct2point() has already performed sanity checking of
  554. * the buffer so we know it is valid.
  555. */
  556. if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
  557. key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
  558. return 1;
  559. }
  560. size_t EC_KEY_priv2oct(const EC_KEY *eckey,
  561. unsigned char *buf, size_t len)
  562. {
  563. if (eckey->group == NULL || eckey->group->meth == NULL)
  564. return 0;
  565. if (eckey->group->meth->priv2oct == NULL) {
  566. ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  567. return 0;
  568. }
  569. return eckey->group->meth->priv2oct(eckey, buf, len);
  570. }
  571. size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
  572. unsigned char *buf, size_t len)
  573. {
  574. size_t buf_len;
  575. buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
  576. if (eckey->priv_key == NULL)
  577. return 0;
  578. if (buf == NULL)
  579. return buf_len;
  580. else if (len < buf_len)
  581. return 0;
  582. /* Octetstring may need leading zeros if BN is to short */
  583. if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
  584. ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
  585. return 0;
  586. }
  587. return buf_len;
  588. }
  589. int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  590. {
  591. if (eckey->group == NULL || eckey->group->meth == NULL)
  592. return 0;
  593. if (eckey->group->meth->oct2priv == NULL) {
  594. ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  595. return 0;
  596. }
  597. return eckey->group->meth->oct2priv(eckey, buf, len);
  598. }
  599. int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  600. {
  601. if (eckey->priv_key == NULL)
  602. eckey->priv_key = BN_secure_new();
  603. if (eckey->priv_key == NULL) {
  604. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
  605. return 0;
  606. }
  607. eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
  608. if (eckey->priv_key == NULL) {
  609. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
  610. return 0;
  611. }
  612. return 1;
  613. }
  614. size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
  615. {
  616. size_t len;
  617. unsigned char *buf;
  618. len = EC_KEY_priv2oct(eckey, NULL, 0);
  619. if (len == 0)
  620. return 0;
  621. if ((buf = OPENSSL_malloc(len)) == NULL) {
  622. ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
  623. return 0;
  624. }
  625. len = EC_KEY_priv2oct(eckey, buf, len);
  626. if (len == 0) {
  627. OPENSSL_free(buf);
  628. return 0;
  629. }
  630. *pbuf = buf;
  631. return len;
  632. }
  633. int EC_KEY_can_sign(const EC_KEY *eckey)
  634. {
  635. if (eckey->group == NULL || eckey->group->meth == NULL
  636. || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
  637. return 0;
  638. return 1;
  639. }