ecdsa_ossl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/err.h>
  16. #include <openssl/obj_mac.h>
  17. #include <openssl/rand.h>
  18. #include "crypto/bn.h"
  19. #include "ec_local.h"
  20. #include "internal/deterministic_nonce.h"
  21. #define MIN_ECDSA_SIGN_ORDERBITS 64
  22. /*
  23. * It is highly unlikely that a retry will happen,
  24. * Multiple retries would indicate that something is wrong
  25. * with the group parameters (which would normally only happen
  26. * with a bad custom group).
  27. */
  28. #define MAX_ECDSA_SIGN_RETRIES 8
  29. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
  30. BIGNUM **kinvp, BIGNUM **rp,
  31. const unsigned char *dgst, int dlen,
  32. unsigned int nonce_type, const char *digestname,
  33. OSSL_LIB_CTX *libctx, const char *propq);
  34. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  35. BIGNUM **rp)
  36. {
  37. if (eckey->group->meth->ecdsa_sign_setup == NULL) {
  38. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  39. return 0;
  40. }
  41. return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
  42. }
  43. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  44. const BIGNUM *in_kinv, const BIGNUM *in_r,
  45. EC_KEY *eckey)
  46. {
  47. if (eckey->group->meth->ecdsa_sign_sig == NULL) {
  48. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  49. return NULL;
  50. }
  51. return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
  52. in_kinv, in_r, eckey);
  53. }
  54. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  55. const ECDSA_SIG *sig, EC_KEY *eckey)
  56. {
  57. if (eckey->group->meth->ecdsa_verify_sig == NULL) {
  58. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  59. return 0;
  60. }
  61. return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
  62. }
  63. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  64. unsigned char *sig, unsigned int *siglen,
  65. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
  66. {
  67. ECDSA_SIG *s;
  68. if (sig == NULL && (kinv == NULL || r == NULL)) {
  69. *siglen = ECDSA_size(eckey);
  70. return 1;
  71. }
  72. s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
  73. if (s == NULL) {
  74. *siglen = 0;
  75. return 0;
  76. }
  77. *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
  78. ECDSA_SIG_free(s);
  79. return 1;
  80. }
  81. int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
  82. unsigned char *sig, unsigned int *siglen,
  83. EC_KEY *eckey, unsigned int nonce_type,
  84. const char *digestname,
  85. OSSL_LIB_CTX *libctx, const char *propq)
  86. {
  87. ECDSA_SIG *s;
  88. BIGNUM *kinv = NULL, *r = NULL;
  89. int ret = 0;
  90. if (sig == NULL) {
  91. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  92. return 0;
  93. }
  94. if (digestname == NULL) {
  95. ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
  96. return 0;
  97. }
  98. *siglen = 0;
  99. if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
  100. nonce_type, digestname, libctx, propq))
  101. return 0;
  102. s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
  103. if (s == NULL)
  104. goto end;
  105. *siglen = i2d_ECDSA_SIG(s, &sig);
  106. ECDSA_SIG_free(s);
  107. ret = 1;
  108. end:
  109. BN_clear_free(kinv);
  110. BN_clear_free(r);
  111. return ret;
  112. }
  113. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
  114. BIGNUM **kinvp, BIGNUM **rp,
  115. const unsigned char *dgst, int dlen,
  116. unsigned int nonce_type, const char *digestname,
  117. OSSL_LIB_CTX *libctx, const char *propq)
  118. {
  119. BN_CTX *ctx = NULL;
  120. BIGNUM *k = NULL, *r = NULL, *X = NULL;
  121. const BIGNUM *order;
  122. EC_POINT *tmp_point = NULL;
  123. const EC_GROUP *group;
  124. int ret = 0;
  125. int order_bits;
  126. const BIGNUM *priv_key;
  127. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  128. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  129. return 0;
  130. }
  131. if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
  132. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  133. return 0;
  134. }
  135. if (!EC_KEY_can_sign(eckey)) {
  136. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  137. return 0;
  138. }
  139. if ((ctx = ctx_in) == NULL) {
  140. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
  141. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  142. return 0;
  143. }
  144. }
  145. k = BN_secure_new(); /* this value is later returned in *kinvp */
  146. r = BN_new(); /* this value is later returned in *rp */
  147. X = BN_new();
  148. if (k == NULL || r == NULL || X == NULL) {
  149. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  150. goto err;
  151. }
  152. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  153. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  154. goto err;
  155. }
  156. if ((order = EC_GROUP_get0_order(group)) == NULL) {
  157. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  158. goto err;
  159. }
  160. /* Preallocate space */
  161. order_bits = BN_num_bits(order);
  162. /* Check the number of bits here so that an infinite loop is not possible */
  163. if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
  164. || !BN_set_bit(k, order_bits)
  165. || !BN_set_bit(r, order_bits)
  166. || !BN_set_bit(X, order_bits))
  167. goto err;
  168. do {
  169. /* get random or deterministic value of k */
  170. do {
  171. int res = 0;
  172. if (dgst != NULL) {
  173. if (nonce_type == 1) {
  174. #ifndef FIPS_MODULE
  175. res = ossl_gen_deterministic_nonce_rfc6979(k, order,
  176. priv_key,
  177. dgst, dlen,
  178. digestname,
  179. libctx, propq);
  180. #endif
  181. } else {
  182. res = ossl_bn_gen_dsa_nonce_fixed_top(k, order, priv_key,
  183. dgst, dlen, ctx);
  184. }
  185. } else {
  186. res = ossl_bn_priv_rand_range_fixed_top(k, order, 0, ctx);
  187. }
  188. if (!res) {
  189. ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  190. goto err;
  191. }
  192. } while (ossl_bn_is_word_fixed_top(k, 0));
  193. /* compute r the x-coordinate of generator * k */
  194. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  195. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  196. goto err;
  197. }
  198. if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
  199. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  200. goto err;
  201. }
  202. if (!BN_nnmod(r, X, order, ctx)) {
  203. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  204. goto err;
  205. }
  206. } while (BN_is_zero(r));
  207. /* compute the inverse of k */
  208. if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
  209. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  210. goto err;
  211. }
  212. /* clear old values if necessary */
  213. BN_clear_free(*rp);
  214. BN_clear_free(*kinvp);
  215. /* save the pre-computed values */
  216. *rp = r;
  217. *kinvp = k;
  218. ret = 1;
  219. err:
  220. if (!ret) {
  221. BN_clear_free(k);
  222. BN_clear_free(r);
  223. }
  224. if (ctx != ctx_in)
  225. BN_CTX_free(ctx);
  226. EC_POINT_free(tmp_point);
  227. BN_clear_free(X);
  228. return ret;
  229. }
  230. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  231. BIGNUM **rp)
  232. {
  233. return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
  234. 0, NULL, NULL, NULL);
  235. }
  236. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  237. const BIGNUM *in_kinv, const BIGNUM *in_r,
  238. EC_KEY *eckey)
  239. {
  240. int ok = 0, i;
  241. int retries = 0;
  242. BIGNUM *kinv = NULL, *s, *m = NULL;
  243. const BIGNUM *order, *ckinv;
  244. BN_CTX *ctx = NULL;
  245. const EC_GROUP *group;
  246. ECDSA_SIG *ret;
  247. const BIGNUM *priv_key;
  248. group = EC_KEY_get0_group(eckey);
  249. priv_key = EC_KEY_get0_private_key(eckey);
  250. if (group == NULL) {
  251. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  252. return NULL;
  253. }
  254. if (priv_key == NULL) {
  255. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  256. return NULL;
  257. }
  258. if (!EC_KEY_can_sign(eckey)) {
  259. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  260. return NULL;
  261. }
  262. ret = ECDSA_SIG_new();
  263. if (ret == NULL) {
  264. ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
  265. return NULL;
  266. }
  267. ret->r = BN_new();
  268. ret->s = BN_new();
  269. if (ret->r == NULL || ret->s == NULL) {
  270. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  271. goto err;
  272. }
  273. s = ret->s;
  274. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
  275. || (m = BN_new()) == NULL) {
  276. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  277. goto err;
  278. }
  279. if ((order = EC_GROUP_get0_order(group)) == NULL) {
  280. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  281. goto err;
  282. }
  283. i = BN_num_bits(order);
  284. /*
  285. * Need to truncate digest if it is too long: first truncate whole bytes.
  286. */
  287. if (8 * dgst_len > i)
  288. dgst_len = (i + 7) / 8;
  289. if (!BN_bin2bn(dgst, dgst_len, m)) {
  290. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  291. goto err;
  292. }
  293. /* If still too long, truncate remaining bits with a shift */
  294. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  295. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  296. goto err;
  297. }
  298. do {
  299. if (in_kinv == NULL || in_r == NULL) {
  300. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
  301. 0, NULL, NULL, NULL)) {
  302. ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
  303. goto err;
  304. }
  305. ckinv = kinv;
  306. } else {
  307. ckinv = in_kinv;
  308. if (BN_copy(ret->r, in_r) == NULL) {
  309. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  310. goto err;
  311. }
  312. }
  313. /*
  314. * With only one multiplicant being in Montgomery domain
  315. * multiplication yields real result without post-conversion.
  316. * Also note that all operations but last are performed with
  317. * zero-padded vectors. Last operation, BN_mod_mul_montgomery
  318. * below, returns user-visible value with removed zero padding.
  319. */
  320. if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
  321. || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
  322. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  323. goto err;
  324. }
  325. if (!bn_mod_add_fixed_top(s, s, m, order)) {
  326. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  327. goto err;
  328. }
  329. /*
  330. * |s| can still be larger than modulus, because |m| can be. In
  331. * such case we count on Montgomery reduction to tie it up.
  332. */
  333. if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
  334. || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
  335. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  336. goto err;
  337. }
  338. if (BN_is_zero(s)) {
  339. /*
  340. * if kinv and r have been supplied by the caller, don't
  341. * generate new kinv and r values
  342. */
  343. if (in_kinv != NULL && in_r != NULL) {
  344. ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
  345. goto err;
  346. }
  347. /* Avoid infinite loops cause by invalid group parameters */
  348. if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
  349. ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
  350. goto err;
  351. }
  352. } else {
  353. /* s != 0 => we have a valid signature */
  354. break;
  355. }
  356. } while (1);
  357. ok = 1;
  358. err:
  359. if (!ok) {
  360. ECDSA_SIG_free(ret);
  361. ret = NULL;
  362. }
  363. BN_CTX_free(ctx);
  364. BN_clear_free(m);
  365. BN_clear_free(kinv);
  366. return ret;
  367. }
  368. /*-
  369. * returns
  370. * 1: correct signature
  371. * 0: incorrect signature
  372. * -1: error
  373. */
  374. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  375. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
  376. {
  377. ECDSA_SIG *s;
  378. const unsigned char *p = sigbuf;
  379. unsigned char *der = NULL;
  380. int derlen = -1;
  381. int ret = -1;
  382. s = ECDSA_SIG_new();
  383. if (s == NULL)
  384. return ret;
  385. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
  386. goto err;
  387. /* Ensure signature uses DER and doesn't have trailing garbage */
  388. derlen = i2d_ECDSA_SIG(s, &der);
  389. if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
  390. goto err;
  391. ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
  392. err:
  393. OPENSSL_free(der);
  394. ECDSA_SIG_free(s);
  395. return ret;
  396. }
  397. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  398. const ECDSA_SIG *sig, EC_KEY *eckey)
  399. {
  400. int ret = -1, i;
  401. BN_CTX *ctx;
  402. const BIGNUM *order;
  403. BIGNUM *u1, *u2, *m, *X;
  404. EC_POINT *point = NULL;
  405. const EC_GROUP *group;
  406. const EC_POINT *pub_key;
  407. /* check input values */
  408. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  409. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  410. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
  411. return -1;
  412. }
  413. if (!EC_KEY_can_sign(eckey)) {
  414. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  415. return -1;
  416. }
  417. ctx = BN_CTX_new_ex(eckey->libctx);
  418. if (ctx == NULL) {
  419. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  420. return -1;
  421. }
  422. BN_CTX_start(ctx);
  423. u1 = BN_CTX_get(ctx);
  424. u2 = BN_CTX_get(ctx);
  425. m = BN_CTX_get(ctx);
  426. X = BN_CTX_get(ctx);
  427. if (X == NULL) {
  428. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  429. goto err;
  430. }
  431. order = EC_GROUP_get0_order(group);
  432. if (order == NULL) {
  433. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  434. goto err;
  435. }
  436. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  437. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  438. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  439. ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
  440. ret = 0; /* signature is invalid */
  441. goto err;
  442. }
  443. /* calculate tmp1 = inv(S) mod order */
  444. if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
  445. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  446. goto err;
  447. }
  448. /* digest -> m */
  449. i = BN_num_bits(order);
  450. /*
  451. * Need to truncate digest if it is too long: first truncate whole bytes.
  452. */
  453. if (8 * dgst_len > i)
  454. dgst_len = (i + 7) / 8;
  455. if (!BN_bin2bn(dgst, dgst_len, m)) {
  456. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  457. goto err;
  458. }
  459. /* If still too long truncate remaining bits with a shift */
  460. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  461. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  462. goto err;
  463. }
  464. /* u1 = m * tmp mod order */
  465. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  466. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  467. goto err;
  468. }
  469. /* u2 = r * w mod q */
  470. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  471. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  472. goto err;
  473. }
  474. if ((point = EC_POINT_new(group)) == NULL) {
  475. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  476. goto err;
  477. }
  478. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  479. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  480. goto err;
  481. }
  482. if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
  483. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  484. goto err;
  485. }
  486. if (!BN_nnmod(u1, X, order, ctx)) {
  487. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  488. goto err;
  489. }
  490. /* if the signature is correct u1 is equal to sig->r */
  491. ret = (BN_ucmp(u1, sig->r) == 0);
  492. err:
  493. BN_CTX_end(ctx);
  494. BN_CTX_free(ctx);
  495. EC_POINT_free(point);
  496. return ret;
  497. }