ec_key.c 27 KB

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