ec_key.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Copyright 2002-2020 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. /*
  11. * ECDSA low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include "internal/cryptlib.h"
  16. #include <string.h>
  17. #include "ec_local.h"
  18. #include "internal/refcount.h"
  19. #include <openssl/err.h>
  20. #include <openssl/engine.h>
  21. #include <openssl/self_test.h>
  22. #include "prov/providercommon.h"
  23. #include "crypto/bn.h"
  24. static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
  25. void *cbarg);
  26. #ifndef FIPS_MODULE
  27. EC_KEY *EC_KEY_new(void)
  28. {
  29. return ec_key_new_method_int(NULL, NULL, NULL);
  30. }
  31. #endif
  32. EC_KEY *EC_KEY_new_with_libctx(OPENSSL_CTX *ctx, const char *propq)
  33. {
  34. return ec_key_new_method_int(ctx, propq, NULL);
  35. }
  36. EC_KEY *EC_KEY_new_by_curve_name_with_libctx(OPENSSL_CTX *ctx,
  37. const char *propq, int nid)
  38. {
  39. EC_KEY *ret = EC_KEY_new_with_libctx(ctx, propq);
  40. if (ret == NULL)
  41. return NULL;
  42. ret->group = EC_GROUP_new_by_curve_name_with_libctx(ctx, propq, nid);
  43. if (ret->group == NULL) {
  44. EC_KEY_free(ret);
  45. return NULL;
  46. }
  47. if (ret->meth->set_group != NULL
  48. && ret->meth->set_group(ret, ret->group) == 0) {
  49. EC_KEY_free(ret);
  50. return NULL;
  51. }
  52. return ret;
  53. }
  54. #ifndef FIPS_MODULE
  55. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  56. {
  57. return EC_KEY_new_by_curve_name_with_libctx(NULL, NULL, nid);
  58. }
  59. #endif
  60. void EC_KEY_free(EC_KEY *r)
  61. {
  62. int i;
  63. if (r == NULL)
  64. return;
  65. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  66. REF_PRINT_COUNT("EC_KEY", r);
  67. if (i > 0)
  68. return;
  69. REF_ASSERT_ISNT(i < 0);
  70. if (r->meth != NULL && r->meth->finish != NULL)
  71. r->meth->finish(r);
  72. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  73. ENGINE_finish(r->engine);
  74. #endif
  75. if (r->group && r->group->meth->keyfinish)
  76. r->group->meth->keyfinish(r);
  77. #ifndef FIPS_MODULE
  78. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
  79. #endif
  80. CRYPTO_THREAD_lock_free(r->lock);
  81. EC_GROUP_free(r->group);
  82. EC_POINT_free(r->pub_key);
  83. BN_clear_free(r->priv_key);
  84. OPENSSL_free(r->propq);
  85. OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
  86. }
  87. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  88. {
  89. if (dest == NULL || src == NULL) {
  90. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  91. return NULL;
  92. }
  93. if (src->meth != dest->meth) {
  94. if (dest->meth->finish != NULL)
  95. dest->meth->finish(dest);
  96. if (dest->group && dest->group->meth->keyfinish)
  97. dest->group->meth->keyfinish(dest);
  98. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  99. if (ENGINE_finish(dest->engine) == 0)
  100. return 0;
  101. dest->engine = NULL;
  102. #endif
  103. }
  104. dest->libctx = src->libctx;
  105. /* copy the parameters */
  106. if (src->group != NULL) {
  107. /* clear the old group */
  108. EC_GROUP_free(dest->group);
  109. dest->group = ec_group_new_with_libctx(src->libctx, src->propq,
  110. src->group->meth);
  111. if (dest->group == NULL)
  112. return NULL;
  113. if (!EC_GROUP_copy(dest->group, src->group))
  114. return NULL;
  115. /* copy the public key */
  116. if (src->pub_key != NULL) {
  117. EC_POINT_free(dest->pub_key);
  118. dest->pub_key = EC_POINT_new(src->group);
  119. if (dest->pub_key == NULL)
  120. return NULL;
  121. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  122. return NULL;
  123. }
  124. /* copy the private key */
  125. if (src->priv_key != NULL) {
  126. if (dest->priv_key == NULL) {
  127. dest->priv_key = BN_new();
  128. if (dest->priv_key == NULL)
  129. return NULL;
  130. }
  131. if (!BN_copy(dest->priv_key, src->priv_key))
  132. return NULL;
  133. if (src->group->meth->keycopy
  134. && src->group->meth->keycopy(dest, src) == 0)
  135. return NULL;
  136. }
  137. }
  138. /* copy the rest */
  139. dest->enc_flag = src->enc_flag;
  140. dest->conv_form = src->conv_form;
  141. dest->version = src->version;
  142. dest->flags = src->flags;
  143. #ifndef FIPS_MODULE
  144. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  145. &dest->ex_data, &src->ex_data))
  146. return NULL;
  147. #endif
  148. if (src->meth != dest->meth) {
  149. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  150. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  151. return NULL;
  152. dest->engine = src->engine;
  153. #endif
  154. dest->meth = src->meth;
  155. }
  156. if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
  157. return NULL;
  158. dest->dirty_cnt++;
  159. return dest;
  160. }
  161. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  162. {
  163. EC_KEY *ret = ec_key_new_method_int(ec_key->libctx, ec_key->propq,
  164. ec_key->engine);
  165. if (ret == NULL)
  166. return NULL;
  167. if (EC_KEY_copy(ret, ec_key) == NULL) {
  168. EC_KEY_free(ret);
  169. return NULL;
  170. }
  171. return ret;
  172. }
  173. int EC_KEY_up_ref(EC_KEY *r)
  174. {
  175. int i;
  176. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  177. return 0;
  178. REF_PRINT_COUNT("EC_KEY", r);
  179. REF_ASSERT_ISNT(i < 2);
  180. return ((i > 1) ? 1 : 0);
  181. }
  182. ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
  183. {
  184. return eckey->engine;
  185. }
  186. int EC_KEY_generate_key(EC_KEY *eckey)
  187. {
  188. if (eckey == NULL || eckey->group == NULL) {
  189. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  190. return 0;
  191. }
  192. if (eckey->meth->keygen != NULL) {
  193. int ret;
  194. ret = eckey->meth->keygen(eckey);
  195. if (ret == 1)
  196. eckey->dirty_cnt++;
  197. return ret;
  198. }
  199. ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
  200. return 0;
  201. }
  202. int ossl_ec_key_gen(EC_KEY *eckey)
  203. {
  204. int ret;
  205. ret = eckey->group->meth->keygen(eckey);
  206. if (ret == 1)
  207. eckey->dirty_cnt++;
  208. return ret;
  209. }
  210. /*
  211. * ECC Key generation.
  212. * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
  213. *
  214. * Params:
  215. * libctx A context containing an optional self test callback.
  216. * eckey An EC key object that contains domain params. The generated keypair
  217. * is stored in this object.
  218. * pairwise_test Set to non zero to perform a pairwise test. If the test
  219. * fails then the keypair is not generated,
  220. * Returns 1 if the keypair was generated or 0 otherwise.
  221. */
  222. int ec_generate_key(OPENSSL_CTX *libctx, EC_KEY *eckey, int pairwise_test)
  223. {
  224. int ok = 0;
  225. BIGNUM *priv_key = NULL;
  226. const BIGNUM *order = NULL;
  227. EC_POINT *pub_key = NULL;
  228. const EC_GROUP *group = eckey->group;
  229. BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
  230. if (ctx == NULL)
  231. goto err;
  232. if (eckey->priv_key == NULL) {
  233. priv_key = BN_secure_new();
  234. if (priv_key == NULL)
  235. goto err;
  236. } else
  237. priv_key = eckey->priv_key;
  238. /*
  239. * Steps (1-2): Check domain parameters and security strength.
  240. * These steps must be done by the user. This would need to be
  241. * stated in the security policy.
  242. */
  243. order = EC_GROUP_get0_order(group);
  244. if (order == NULL)
  245. goto err;
  246. /*
  247. * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
  248. * Although this is slightly different from the standard, it is effectively
  249. * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
  250. * faster as the standard needs to retry more often. Also doing
  251. * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
  252. * rand so the simpler backward compatible method has been used here.
  253. */
  254. do
  255. if (!BN_priv_rand_range_ex(priv_key, order, ctx))
  256. goto err;
  257. while (BN_is_zero(priv_key)) ;
  258. if (eckey->pub_key == NULL) {
  259. pub_key = EC_POINT_new(group);
  260. if (pub_key == NULL)
  261. goto err;
  262. } else
  263. pub_key = eckey->pub_key;
  264. /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
  265. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
  266. goto err;
  267. eckey->priv_key = priv_key;
  268. eckey->pub_key = pub_key;
  269. priv_key = NULL;
  270. pub_key = NULL;
  271. eckey->dirty_cnt++;
  272. #ifdef FIPS_MODULE
  273. pairwise_test = 1;
  274. #endif /* FIPS_MODULE */
  275. ok = 1;
  276. if (pairwise_test) {
  277. OSSL_CALLBACK *cb = NULL;
  278. void *cbarg = NULL;
  279. OSSL_SELF_TEST_get_callback(libctx, &cb, &cbarg);
  280. ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
  281. }
  282. err:
  283. /* Step (9): If there is an error return an invalid keypair. */
  284. if (!ok) {
  285. ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
  286. BN_clear(eckey->priv_key);
  287. if (eckey->pub_key != NULL)
  288. EC_POINT_set_to_infinity(group, eckey->pub_key);
  289. }
  290. EC_POINT_free(pub_key);
  291. BN_clear_free(priv_key);
  292. BN_CTX_free(ctx);
  293. return ok;
  294. }
  295. int ec_key_simple_generate_key(EC_KEY *eckey)
  296. {
  297. return ec_generate_key(NULL, eckey, 0);
  298. }
  299. int ec_key_simple_generate_public_key(EC_KEY *eckey)
  300. {
  301. int ret;
  302. BN_CTX *ctx = BN_CTX_new_ex(eckey->libctx);
  303. if (ctx == NULL)
  304. return 0;
  305. /*
  306. * See SP800-56AR3 5.6.1.2.2: Step (8)
  307. * pub_key = priv_key * G (where G is a point on the curve)
  308. */
  309. ret = EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
  310. NULL, ctx);
  311. BN_CTX_free(ctx);
  312. if (ret == 1)
  313. eckey->dirty_cnt++;
  314. return ret;
  315. }
  316. int EC_KEY_check_key(const EC_KEY *eckey)
  317. {
  318. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  319. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  320. return 0;
  321. }
  322. if (eckey->group->meth->keycheck == NULL) {
  323. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  324. return 0;
  325. }
  326. return eckey->group->meth->keycheck(eckey);
  327. }
  328. /*
  329. * Check the range of the EC public key.
  330. * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
  331. * i.e.
  332. * - If q = odd prime p: Verify that xQ and yQ are integers in the
  333. * interval[0, p - 1], OR
  334. * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
  335. * Returns 1 if the public key has a valid range, otherwise it returns 0.
  336. */
  337. static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
  338. {
  339. int ret = 0;
  340. BIGNUM *x, *y;
  341. BN_CTX_start(ctx);
  342. x = BN_CTX_get(ctx);
  343. y = BN_CTX_get(ctx);
  344. if (y == NULL)
  345. goto err;
  346. if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
  347. goto err;
  348. if (EC_GROUP_get_field_type(key->group) == NID_X9_62_prime_field) {
  349. if (BN_is_negative(x)
  350. || BN_cmp(x, key->group->field) >= 0
  351. || BN_is_negative(y)
  352. || BN_cmp(y, key->group->field) >= 0) {
  353. goto err;
  354. }
  355. } else {
  356. int m = EC_GROUP_get_degree(key->group);
  357. if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
  358. goto err;
  359. }
  360. }
  361. ret = 1;
  362. err:
  363. BN_CTX_end(ctx);
  364. return ret;
  365. }
  366. /*
  367. * ECC Key validation as specified in SP800-56A R3.
  368. * Section 5.6.2.3.3 ECC Full Public-Key Validation.
  369. */
  370. int ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx)
  371. {
  372. int ret = 0;
  373. EC_POINT *point = NULL;
  374. const BIGNUM *order = NULL;
  375. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  376. ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
  377. return 0;
  378. }
  379. /* 5.6.2.3.3 (Step 1): Q != infinity */
  380. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  381. ECerr(0, EC_R_POINT_AT_INFINITY);
  382. return 0;
  383. }
  384. point = EC_POINT_new(eckey->group);
  385. if (point == NULL)
  386. return 0;
  387. /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
  388. if (!ec_key_public_range_check(ctx, eckey)) {
  389. ECerr(0, EC_R_COORDINATES_OUT_OF_RANGE);
  390. goto err;
  391. }
  392. /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
  393. if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
  394. ECerr(0, EC_R_POINT_IS_NOT_ON_CURVE);
  395. goto err;
  396. }
  397. order = eckey->group->order;
  398. if (BN_is_zero(order)) {
  399. ECerr(0, EC_R_INVALID_GROUP_ORDER);
  400. goto err;
  401. }
  402. /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
  403. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  404. ECerr(0, ERR_R_EC_LIB);
  405. goto err;
  406. }
  407. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  408. ECerr(0, EC_R_WRONG_ORDER);
  409. goto err;
  410. }
  411. ret = 1;
  412. err:
  413. EC_POINT_free(point);
  414. return ret;
  415. }
  416. /*
  417. * ECC Key validation as specified in SP800-56A R3.
  418. * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
  419. * The private key is in the range [1, order-1]
  420. */
  421. int ec_key_private_check(const EC_KEY *eckey)
  422. {
  423. if (eckey == NULL || eckey->group == NULL || eckey->priv_key == NULL) {
  424. ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
  425. return 0;
  426. }
  427. if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
  428. || BN_cmp(eckey->priv_key, eckey->group->order) >= 0) {
  429. ECerr(0, EC_R_INVALID_PRIVATE_KEY);
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. /*
  435. * ECC Key validation as specified in SP800-56A R3.
  436. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
  437. * Check if generator * priv_key = pub_key
  438. */
  439. int ec_key_pairwise_check(const EC_KEY *eckey, BN_CTX *ctx)
  440. {
  441. int ret = 0;
  442. EC_POINT *point = NULL;
  443. if (eckey == NULL
  444. || eckey->group == NULL
  445. || eckey->pub_key == NULL
  446. || eckey->priv_key == NULL) {
  447. ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
  448. return 0;
  449. }
  450. point = EC_POINT_new(eckey->group);
  451. if (point == NULL)
  452. goto err;
  453. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
  454. ECerr(0, ERR_R_EC_LIB);
  455. goto err;
  456. }
  457. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  458. ECerr(0, EC_R_INVALID_PRIVATE_KEY);
  459. goto err;
  460. }
  461. ret = 1;
  462. err:
  463. EC_POINT_free(point);
  464. return ret;
  465. }
  466. /*
  467. * ECC Key validation as specified in SP800-56A R3.
  468. * Section 5.6.2.3.3 ECC Full Public-Key Validation
  469. * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
  470. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  471. * NOTES:
  472. * Before calling this method in fips mode, there should be an assurance that
  473. * an approved elliptic-curve group is used.
  474. * Returns 1 if the key is valid, otherwise it returns 0.
  475. */
  476. int ec_key_simple_check_key(const EC_KEY *eckey)
  477. {
  478. int ok = 0;
  479. BN_CTX *ctx = NULL;
  480. if (eckey == NULL) {
  481. ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
  482. return 0;
  483. }
  484. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
  485. return 0;
  486. if (!ec_key_public_check(eckey, ctx))
  487. goto err;
  488. if (eckey->priv_key != NULL) {
  489. if (!ec_key_private_check(eckey)
  490. || !ec_key_pairwise_check(eckey, ctx))
  491. goto err;
  492. }
  493. ok = 1;
  494. err:
  495. BN_CTX_free(ctx);
  496. return ok;
  497. }
  498. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  499. BIGNUM *y)
  500. {
  501. BN_CTX *ctx = NULL;
  502. BIGNUM *tx, *ty;
  503. EC_POINT *point = NULL;
  504. int ok = 0;
  505. if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
  506. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  507. ERR_R_PASSED_NULL_PARAMETER);
  508. return 0;
  509. }
  510. ctx = BN_CTX_new_ex(key->libctx);
  511. if (ctx == NULL)
  512. return 0;
  513. BN_CTX_start(ctx);
  514. point = EC_POINT_new(key->group);
  515. if (point == NULL)
  516. goto err;
  517. tx = BN_CTX_get(ctx);
  518. ty = BN_CTX_get(ctx);
  519. if (ty == NULL)
  520. goto err;
  521. if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
  522. goto err;
  523. if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
  524. goto err;
  525. /*
  526. * Check if retrieved coordinates match originals. The range check is done
  527. * inside EC_KEY_check_key().
  528. */
  529. if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
  530. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  531. EC_R_COORDINATES_OUT_OF_RANGE);
  532. goto err;
  533. }
  534. /* EC_KEY_set_public_key updates dirty_cnt */
  535. if (!EC_KEY_set_public_key(key, point))
  536. goto err;
  537. if (EC_KEY_check_key(key) == 0)
  538. goto err;
  539. ok = 1;
  540. err:
  541. BN_CTX_end(ctx);
  542. BN_CTX_free(ctx);
  543. EC_POINT_free(point);
  544. return ok;
  545. }
  546. OPENSSL_CTX *ec_key_get_libctx(const EC_KEY *key)
  547. {
  548. return key->libctx;
  549. }
  550. const char *ec_key_get0_propq(const EC_KEY *key)
  551. {
  552. return key->propq;
  553. }
  554. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  555. {
  556. return key->group;
  557. }
  558. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  559. {
  560. if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
  561. return 0;
  562. EC_GROUP_free(key->group);
  563. key->group = EC_GROUP_dup(group);
  564. key->dirty_cnt++;
  565. return (key->group == NULL) ? 0 : 1;
  566. }
  567. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  568. {
  569. return key->priv_key;
  570. }
  571. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  572. {
  573. int fixed_top;
  574. const BIGNUM *order = NULL;
  575. BIGNUM *tmp_key = NULL;
  576. if (key->group == NULL || key->group->meth == NULL)
  577. return 0;
  578. /*
  579. * Not only should key->group be set, but it should also be in a valid
  580. * fully initialized state.
  581. *
  582. * Specifically, to operate in constant time, we need that the group order
  583. * is set, as we use its length as the fixed public size of any scalar used
  584. * as an EC private key.
  585. */
  586. order = EC_GROUP_get0_order(key->group);
  587. if (order == NULL || BN_is_zero(order))
  588. return 0; /* This should never happen */
  589. if (key->group->meth->set_private != NULL
  590. && key->group->meth->set_private(key, priv_key) == 0)
  591. return 0;
  592. if (key->meth->set_private != NULL
  593. && key->meth->set_private(key, priv_key) == 0)
  594. return 0;
  595. /*
  596. * We should never leak the bit length of the secret scalar in the key,
  597. * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
  598. * holding the secret scalar.
  599. *
  600. * This is important also because `BN_dup()` (and `BN_copy()`) do not
  601. * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
  602. * this brings an extra risk of inadvertently losing the flag, even when
  603. * the caller specifically set it.
  604. *
  605. * The propagation has been turned on and off a few times in the past
  606. * years because in some conditions has shown unintended consequences in
  607. * some code paths, so at the moment we can't fix this in the BN layer.
  608. *
  609. * In `EC_KEY_set_private_key()` we can work around the propagation by
  610. * manually setting the flag after `BN_dup()` as we know for sure that
  611. * inside the EC module the `BN_FLG_CONSTTIME` is always treated
  612. * correctly and should not generate unintended consequences.
  613. *
  614. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  615. * to preallocate the BIGNUM internal buffer to a fixed public size big
  616. * enough that operations performed during the processing never trigger
  617. * a realloc which would leak the size of the scalar through memory
  618. * accesses.
  619. *
  620. * Fixed Length
  621. * ------------
  622. *
  623. * The order of the large prime subgroup of the curve is our choice for
  624. * a fixed public size, as that is generally the upper bound for
  625. * generating a private key in EC cryptosystems and should fit all valid
  626. * secret scalars.
  627. *
  628. * For preallocating the BIGNUM storage we look at the number of "words"
  629. * required for the internal representation of the order, and we
  630. * preallocate 2 extra "words" in case any of the subsequent processing
  631. * might temporarily overflow the order length.
  632. */
  633. tmp_key = BN_dup(priv_key);
  634. if (tmp_key == NULL)
  635. return 0;
  636. BN_set_flags(tmp_key, BN_FLG_CONSTTIME);
  637. fixed_top = bn_get_top(order) + 2;
  638. if (bn_wexpand(tmp_key, fixed_top) == NULL) {
  639. BN_clear_free(tmp_key);
  640. return 0;
  641. }
  642. BN_clear_free(key->priv_key);
  643. key->priv_key = tmp_key;
  644. key->dirty_cnt++;
  645. return 1;
  646. }
  647. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  648. {
  649. return key->pub_key;
  650. }
  651. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  652. {
  653. if (key->meth->set_public != NULL
  654. && key->meth->set_public(key, pub_key) == 0)
  655. return 0;
  656. EC_POINT_free(key->pub_key);
  657. key->pub_key = EC_POINT_dup(pub_key, key->group);
  658. key->dirty_cnt++;
  659. return (key->pub_key == NULL) ? 0 : 1;
  660. }
  661. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  662. {
  663. return key->enc_flag;
  664. }
  665. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  666. {
  667. key->enc_flag = flags;
  668. }
  669. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  670. {
  671. return key->conv_form;
  672. }
  673. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  674. {
  675. key->conv_form = cform;
  676. if (key->group != NULL)
  677. EC_GROUP_set_point_conversion_form(key->group, cform);
  678. }
  679. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  680. {
  681. if (key->group != NULL)
  682. EC_GROUP_set_asn1_flag(key->group, flag);
  683. }
  684. #ifndef OPENSSL_NO_DEPRECATED_3_0
  685. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  686. {
  687. if (key->group == NULL)
  688. return 0;
  689. return EC_GROUP_precompute_mult(key->group, ctx);
  690. }
  691. #endif
  692. int EC_KEY_get_flags(const EC_KEY *key)
  693. {
  694. return key->flags;
  695. }
  696. void EC_KEY_set_flags(EC_KEY *key, int flags)
  697. {
  698. key->flags |= flags;
  699. key->dirty_cnt++;
  700. }
  701. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  702. {
  703. key->flags &= ~flags;
  704. key->dirty_cnt++;
  705. }
  706. size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
  707. unsigned char **pbuf, BN_CTX *ctx)
  708. {
  709. if (key == NULL || key->pub_key == NULL || key->group == NULL)
  710. return 0;
  711. return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
  712. }
  713. int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
  714. BN_CTX *ctx)
  715. {
  716. if (key == NULL || key->group == NULL)
  717. return 0;
  718. if (key->pub_key == NULL)
  719. key->pub_key = EC_POINT_new(key->group);
  720. if (key->pub_key == NULL)
  721. return 0;
  722. if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
  723. return 0;
  724. key->dirty_cnt++;
  725. /*
  726. * Save the point conversion form.
  727. * For non-custom curves the first octet of the buffer (excluding
  728. * the last significant bit) contains the point conversion form.
  729. * EC_POINT_oct2point() has already performed sanity checking of
  730. * the buffer so we know it is valid.
  731. */
  732. if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
  733. key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
  734. return 1;
  735. }
  736. size_t EC_KEY_priv2oct(const EC_KEY *eckey,
  737. unsigned char *buf, size_t len)
  738. {
  739. if (eckey->group == NULL || eckey->group->meth == NULL)
  740. return 0;
  741. if (eckey->group->meth->priv2oct == NULL) {
  742. ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  743. return 0;
  744. }
  745. return eckey->group->meth->priv2oct(eckey, buf, len);
  746. }
  747. size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
  748. unsigned char *buf, size_t len)
  749. {
  750. size_t buf_len;
  751. buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
  752. if (eckey->priv_key == NULL)
  753. return 0;
  754. if (buf == NULL)
  755. return buf_len;
  756. else if (len < buf_len)
  757. return 0;
  758. /* Octetstring may need leading zeros if BN is to short */
  759. if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
  760. ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
  761. return 0;
  762. }
  763. return buf_len;
  764. }
  765. int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  766. {
  767. int ret;
  768. if (eckey->group == NULL || eckey->group->meth == NULL)
  769. return 0;
  770. if (eckey->group->meth->oct2priv == NULL) {
  771. ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  772. return 0;
  773. }
  774. ret = eckey->group->meth->oct2priv(eckey, buf, len);
  775. if (ret == 1)
  776. eckey->dirty_cnt++;
  777. return ret;
  778. }
  779. int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  780. {
  781. if (eckey->priv_key == NULL)
  782. eckey->priv_key = BN_secure_new();
  783. if (eckey->priv_key == NULL) {
  784. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
  785. return 0;
  786. }
  787. eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
  788. if (eckey->priv_key == NULL) {
  789. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
  790. return 0;
  791. }
  792. eckey->dirty_cnt++;
  793. return 1;
  794. }
  795. size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
  796. {
  797. size_t len;
  798. unsigned char *buf;
  799. len = EC_KEY_priv2oct(eckey, NULL, 0);
  800. if (len == 0)
  801. return 0;
  802. if ((buf = OPENSSL_malloc(len)) == NULL) {
  803. ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
  804. return 0;
  805. }
  806. len = EC_KEY_priv2oct(eckey, buf, len);
  807. if (len == 0) {
  808. OPENSSL_free(buf);
  809. return 0;
  810. }
  811. *pbuf = buf;
  812. return len;
  813. }
  814. int EC_KEY_can_sign(const EC_KEY *eckey)
  815. {
  816. if (eckey->group == NULL || eckey->group->meth == NULL
  817. || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
  818. return 0;
  819. return 1;
  820. }
  821. /*
  822. * FIPS 140-2 IG 9.9 AS09.33
  823. * Perform a sign/verify operation.
  824. *
  825. * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
  826. * states that no additional pairwise tests are required (apart from the tests
  827. * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
  828. * omitted here.
  829. */
  830. static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
  831. void *cbarg)
  832. {
  833. int ret = 0;
  834. unsigned char dgst[16] = {0};
  835. int dgst_len = (int)sizeof(dgst);
  836. ECDSA_SIG *sig = NULL;
  837. OSSL_SELF_TEST *st = NULL;
  838. st = OSSL_SELF_TEST_new(cb, cbarg);
  839. if (st == NULL)
  840. return 0;
  841. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
  842. OSSL_SELF_TEST_DESC_PCT_ECDSA);
  843. sig = ECDSA_do_sign(dgst, dgst_len, eckey);
  844. if (sig == NULL)
  845. goto err;
  846. OSSL_SELF_TEST_oncorrupt_byte(st, dgst);
  847. if (ECDSA_do_verify(dgst, dgst_len, sig, eckey) != 1)
  848. goto err;
  849. ret = 1;
  850. err:
  851. OSSL_SELF_TEST_onend(st, ret);
  852. OSSL_SELF_TEST_free(st);
  853. ECDSA_SIG_free(sig);
  854. return ret;
  855. }