ecdsa_ossl.c 16 KB

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