ec_key.c 27 KB

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