ecdsa_ossl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. *siglen = 0;
  95. if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
  96. nonce_type, digestname, libctx, propq))
  97. return 0;
  98. s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
  99. if (s == NULL)
  100. goto end;
  101. *siglen = i2d_ECDSA_SIG(s, &sig);
  102. ECDSA_SIG_free(s);
  103. ret = 1;
  104. end:
  105. BN_clear_free(kinv);
  106. BN_clear_free(r);
  107. return ret;
  108. }
  109. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
  110. BIGNUM **kinvp, BIGNUM **rp,
  111. const unsigned char *dgst, int dlen,
  112. unsigned int nonce_type, const char *digestname,
  113. OSSL_LIB_CTX *libctx, const char *propq)
  114. {
  115. BN_CTX *ctx = NULL;
  116. BIGNUM *k = NULL, *r = NULL, *X = NULL;
  117. const BIGNUM *order;
  118. EC_POINT *tmp_point = NULL;
  119. const EC_GROUP *group;
  120. int ret = 0;
  121. int order_bits;
  122. const BIGNUM *priv_key;
  123. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  124. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  125. return 0;
  126. }
  127. if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
  128. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  129. return 0;
  130. }
  131. if (!EC_KEY_can_sign(eckey)) {
  132. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  133. return 0;
  134. }
  135. if ((ctx = ctx_in) == NULL) {
  136. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
  137. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  138. return 0;
  139. }
  140. }
  141. k = BN_secure_new(); /* this value is later returned in *kinvp */
  142. r = BN_new(); /* this value is later returned in *rp */
  143. X = BN_new();
  144. if (k == NULL || r == NULL || X == NULL) {
  145. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  146. goto err;
  147. }
  148. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  149. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  150. goto err;
  151. }
  152. if ((order = EC_GROUP_get0_order(group)) == NULL) {
  153. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  154. goto err;
  155. }
  156. /* Preallocate space */
  157. order_bits = BN_num_bits(order);
  158. /* Check the number of bits here so that an infinite loop is not possible */
  159. if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
  160. || !BN_set_bit(k, order_bits)
  161. || !BN_set_bit(r, order_bits)
  162. || !BN_set_bit(X, order_bits))
  163. goto err;
  164. do {
  165. /* get random or deterministic value of k */
  166. do {
  167. int res = 0;
  168. if (dgst != NULL) {
  169. if (nonce_type == 1) {
  170. #ifndef FIPS_MODULE
  171. res = ossl_gen_deterministic_nonce_rfc6979(k, order,
  172. priv_key,
  173. dgst, dlen,
  174. digestname,
  175. libctx, propq);
  176. #endif
  177. } else {
  178. res = ossl_bn_gen_dsa_nonce_fixed_top(k, order, priv_key,
  179. dgst, dlen, ctx);
  180. }
  181. } else {
  182. res = ossl_bn_priv_rand_range_fixed_top(k, order, 0, ctx);
  183. }
  184. if (!res) {
  185. ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  186. goto err;
  187. }
  188. } while (ossl_bn_is_word_fixed_top(k, 0));
  189. /* compute r the x-coordinate of generator * k */
  190. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  191. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  192. goto err;
  193. }
  194. if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
  195. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  196. goto err;
  197. }
  198. if (!BN_nnmod(r, X, order, ctx)) {
  199. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  200. goto err;
  201. }
  202. } while (BN_is_zero(r));
  203. /* compute the inverse of k */
  204. if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
  205. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  206. goto err;
  207. }
  208. /* clear old values if necessary */
  209. BN_clear_free(*rp);
  210. BN_clear_free(*kinvp);
  211. /* save the pre-computed values */
  212. *rp = r;
  213. *kinvp = k;
  214. ret = 1;
  215. err:
  216. if (!ret) {
  217. BN_clear_free(k);
  218. BN_clear_free(r);
  219. }
  220. if (ctx != ctx_in)
  221. BN_CTX_free(ctx);
  222. EC_POINT_free(tmp_point);
  223. BN_clear_free(X);
  224. return ret;
  225. }
  226. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  227. BIGNUM **rp)
  228. {
  229. return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
  230. 0, NULL, NULL, NULL);
  231. }
  232. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  233. const BIGNUM *in_kinv, const BIGNUM *in_r,
  234. EC_KEY *eckey)
  235. {
  236. int ok = 0, i;
  237. int retries = 0;
  238. BIGNUM *kinv = NULL, *s, *m = NULL;
  239. const BIGNUM *order, *ckinv;
  240. BN_CTX *ctx = NULL;
  241. const EC_GROUP *group;
  242. ECDSA_SIG *ret;
  243. const BIGNUM *priv_key;
  244. group = EC_KEY_get0_group(eckey);
  245. priv_key = EC_KEY_get0_private_key(eckey);
  246. if (group == NULL) {
  247. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  248. return NULL;
  249. }
  250. if (priv_key == NULL) {
  251. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  252. return NULL;
  253. }
  254. if (!EC_KEY_can_sign(eckey)) {
  255. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  256. return NULL;
  257. }
  258. ret = ECDSA_SIG_new();
  259. if (ret == NULL) {
  260. ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
  261. return NULL;
  262. }
  263. ret->r = BN_new();
  264. ret->s = BN_new();
  265. if (ret->r == NULL || ret->s == NULL) {
  266. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  267. goto err;
  268. }
  269. s = ret->s;
  270. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
  271. || (m = BN_new()) == NULL) {
  272. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  273. goto err;
  274. }
  275. if ((order = EC_GROUP_get0_order(group)) == NULL) {
  276. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  277. goto err;
  278. }
  279. i = BN_num_bits(order);
  280. /*
  281. * Need to truncate digest if it is too long: first truncate whole bytes.
  282. */
  283. if (8 * dgst_len > i)
  284. dgst_len = (i + 7) / 8;
  285. if (!BN_bin2bn(dgst, dgst_len, m)) {
  286. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  287. goto err;
  288. }
  289. /* If still too long, truncate remaining bits with a shift */
  290. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  291. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  292. goto err;
  293. }
  294. do {
  295. if (in_kinv == NULL || in_r == NULL) {
  296. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
  297. 0, NULL, NULL, NULL)) {
  298. ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
  299. goto err;
  300. }
  301. ckinv = kinv;
  302. } else {
  303. ckinv = in_kinv;
  304. if (BN_copy(ret->r, in_r) == NULL) {
  305. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  306. goto err;
  307. }
  308. }
  309. /*
  310. * With only one multiplicant being in Montgomery domain
  311. * multiplication yields real result without post-conversion.
  312. * Also note that all operations but last are performed with
  313. * zero-padded vectors. Last operation, BN_mod_mul_montgomery
  314. * below, returns user-visible value with removed zero padding.
  315. */
  316. if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
  317. || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
  318. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  319. goto err;
  320. }
  321. if (!bn_mod_add_fixed_top(s, s, m, order)) {
  322. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  323. goto err;
  324. }
  325. /*
  326. * |s| can still be larger than modulus, because |m| can be. In
  327. * such case we count on Montgomery reduction to tie it up.
  328. */
  329. if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
  330. || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
  331. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  332. goto err;
  333. }
  334. if (BN_is_zero(s)) {
  335. /*
  336. * if kinv and r have been supplied by the caller, don't
  337. * generate new kinv and r values
  338. */
  339. if (in_kinv != NULL && in_r != NULL) {
  340. ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
  341. goto err;
  342. }
  343. /* Avoid infinite loops cause by invalid group parameters */
  344. if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
  345. ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
  346. goto err;
  347. }
  348. } else {
  349. /* s != 0 => we have a valid signature */
  350. break;
  351. }
  352. } while (1);
  353. ok = 1;
  354. err:
  355. if (!ok) {
  356. ECDSA_SIG_free(ret);
  357. ret = NULL;
  358. }
  359. BN_CTX_free(ctx);
  360. BN_clear_free(m);
  361. BN_clear_free(kinv);
  362. return ret;
  363. }
  364. /*-
  365. * returns
  366. * 1: correct signature
  367. * 0: incorrect signature
  368. * -1: error
  369. */
  370. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  371. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
  372. {
  373. ECDSA_SIG *s;
  374. const unsigned char *p = sigbuf;
  375. unsigned char *der = NULL;
  376. int derlen = -1;
  377. int ret = -1;
  378. s = ECDSA_SIG_new();
  379. if (s == NULL)
  380. return ret;
  381. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
  382. goto err;
  383. /* Ensure signature uses DER and doesn't have trailing garbage */
  384. derlen = i2d_ECDSA_SIG(s, &der);
  385. if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
  386. goto err;
  387. ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
  388. err:
  389. OPENSSL_free(der);
  390. ECDSA_SIG_free(s);
  391. return ret;
  392. }
  393. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  394. const ECDSA_SIG *sig, EC_KEY *eckey)
  395. {
  396. int ret = -1, i;
  397. BN_CTX *ctx;
  398. const BIGNUM *order;
  399. BIGNUM *u1, *u2, *m, *X;
  400. EC_POINT *point = NULL;
  401. const EC_GROUP *group;
  402. const EC_POINT *pub_key;
  403. /* check input values */
  404. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  405. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  406. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
  407. return -1;
  408. }
  409. if (!EC_KEY_can_sign(eckey)) {
  410. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  411. return -1;
  412. }
  413. ctx = BN_CTX_new_ex(eckey->libctx);
  414. if (ctx == NULL) {
  415. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  416. return -1;
  417. }
  418. BN_CTX_start(ctx);
  419. u1 = BN_CTX_get(ctx);
  420. u2 = BN_CTX_get(ctx);
  421. m = BN_CTX_get(ctx);
  422. X = BN_CTX_get(ctx);
  423. if (X == NULL) {
  424. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  425. goto err;
  426. }
  427. order = EC_GROUP_get0_order(group);
  428. if (order == NULL) {
  429. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  430. goto err;
  431. }
  432. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  433. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  434. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  435. ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
  436. ret = 0; /* signature is invalid */
  437. goto err;
  438. }
  439. /* calculate tmp1 = inv(S) mod order */
  440. if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
  441. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  442. goto err;
  443. }
  444. /* digest -> m */
  445. i = BN_num_bits(order);
  446. /*
  447. * Need to truncate digest if it is too long: first truncate whole bytes.
  448. */
  449. if (8 * dgst_len > i)
  450. dgst_len = (i + 7) / 8;
  451. if (!BN_bin2bn(dgst, dgst_len, m)) {
  452. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  453. goto err;
  454. }
  455. /* If still too long truncate remaining bits with a shift */
  456. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  457. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  458. goto err;
  459. }
  460. /* u1 = m * tmp mod order */
  461. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  462. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  463. goto err;
  464. }
  465. /* u2 = r * w mod q */
  466. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  467. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  468. goto err;
  469. }
  470. if ((point = EC_POINT_new(group)) == NULL) {
  471. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  472. goto err;
  473. }
  474. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  475. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  476. goto err;
  477. }
  478. if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
  479. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  480. goto err;
  481. }
  482. if (!BN_nnmod(u1, X, order, ctx)) {
  483. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  484. goto err;
  485. }
  486. /* if the signature is correct u1 is equal to sig->r */
  487. ret = (BN_ucmp(u1, sig->r) == 0);
  488. err:
  489. BN_CTX_end(ctx);
  490. BN_CTX_free(ctx);
  491. EC_POINT_free(point);
  492. return ret;
  493. }