1
0

dsa_ossl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright 1995-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. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include "crypto/bn.h"
  17. #include <openssl/bn.h>
  18. #include <openssl/sha.h>
  19. #include "dsa_local.h"
  20. #include <openssl/asn1.h>
  21. #define MIN_DSA_SIGN_QBITS 128
  22. #define MAX_DSA_SIGN_RETRIES 8
  23. static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  24. static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
  25. BIGNUM **rp);
  26. static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
  27. BIGNUM **rp, const unsigned char *dgst, int dlen);
  28. static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
  29. DSA_SIG *sig, DSA *dsa);
  30. static int dsa_init(DSA *dsa);
  31. static int dsa_finish(DSA *dsa);
  32. static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
  33. BN_CTX *ctx);
  34. static DSA_METHOD openssl_dsa_meth = {
  35. "OpenSSL DSA method",
  36. dsa_do_sign,
  37. dsa_sign_setup_no_digest,
  38. dsa_do_verify,
  39. NULL, /* dsa_mod_exp, */
  40. NULL, /* dsa_bn_mod_exp, */
  41. dsa_init,
  42. dsa_finish,
  43. DSA_FLAG_FIPS_METHOD,
  44. NULL,
  45. NULL,
  46. NULL
  47. };
  48. static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
  49. #ifndef FIPS_MODULE
  50. void DSA_set_default_method(const DSA_METHOD *meth)
  51. {
  52. default_DSA_method = meth;
  53. }
  54. #endif /* FIPS_MODULE */
  55. const DSA_METHOD *DSA_get_default_method(void)
  56. {
  57. return default_DSA_method;
  58. }
  59. const DSA_METHOD *DSA_OpenSSL(void)
  60. {
  61. return &openssl_dsa_meth;
  62. }
  63. DSA_SIG *ossl_dsa_do_sign_int(const unsigned char *dgst, int dlen, DSA *dsa)
  64. {
  65. BIGNUM *kinv = NULL;
  66. BIGNUM *m, *blind, *blindm, *tmp;
  67. BN_CTX *ctx = NULL;
  68. int reason = ERR_R_BN_LIB;
  69. DSA_SIG *ret = NULL;
  70. int rv = 0;
  71. int retries = 0;
  72. if (dsa->params.p == NULL
  73. || dsa->params.q == NULL
  74. || dsa->params.g == NULL) {
  75. reason = DSA_R_MISSING_PARAMETERS;
  76. goto err;
  77. }
  78. if (dsa->priv_key == NULL) {
  79. reason = DSA_R_MISSING_PRIVATE_KEY;
  80. goto err;
  81. }
  82. ret = DSA_SIG_new();
  83. if (ret == NULL)
  84. goto err;
  85. ret->r = BN_new();
  86. ret->s = BN_new();
  87. if (ret->r == NULL || ret->s == NULL)
  88. goto err;
  89. ctx = BN_CTX_new_ex(dsa->libctx);
  90. if (ctx == NULL)
  91. goto err;
  92. m = BN_CTX_get(ctx);
  93. blind = BN_CTX_get(ctx);
  94. blindm = BN_CTX_get(ctx);
  95. tmp = BN_CTX_get(ctx);
  96. if (tmp == NULL)
  97. goto err;
  98. redo:
  99. if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
  100. goto err;
  101. if (dlen > BN_num_bytes(dsa->params.q))
  102. /*
  103. * if the digest length is greater than the size of q use the
  104. * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
  105. * 4.2
  106. */
  107. dlen = BN_num_bytes(dsa->params.q);
  108. if (BN_bin2bn(dgst, dlen, m) == NULL)
  109. goto err;
  110. /*
  111. * The normal signature calculation is:
  112. *
  113. * s := k^-1 * (m + r * priv_key) mod q
  114. *
  115. * We will blind this to protect against side channel attacks
  116. *
  117. * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
  118. */
  119. /*
  120. * Generate a blinding value
  121. * The size of q is tested in dsa_sign_setup() so there should not be an infinite loop here.
  122. */
  123. do {
  124. if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->params.q) - 1,
  125. BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
  126. goto err;
  127. } while (BN_is_zero(blind));
  128. BN_set_flags(blind, BN_FLG_CONSTTIME);
  129. BN_set_flags(blindm, BN_FLG_CONSTTIME);
  130. BN_set_flags(tmp, BN_FLG_CONSTTIME);
  131. /* tmp := blind * priv_key * r mod q */
  132. if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->params.q, ctx))
  133. goto err;
  134. if (!BN_mod_mul(tmp, tmp, ret->r, dsa->params.q, ctx))
  135. goto err;
  136. /* blindm := blind * m mod q */
  137. if (!BN_mod_mul(blindm, blind, m, dsa->params.q, ctx))
  138. goto err;
  139. /* s : = (blind * priv_key * r) + (blind * m) mod q */
  140. if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->params.q))
  141. goto err;
  142. /* s := s * k^-1 mod q */
  143. if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->params.q, ctx))
  144. goto err;
  145. /* s:= s * blind^-1 mod q */
  146. if (BN_mod_inverse(blind, blind, dsa->params.q, ctx) == NULL)
  147. goto err;
  148. if (!BN_mod_mul(ret->s, ret->s, blind, dsa->params.q, ctx))
  149. goto err;
  150. /*
  151. * Redo if r or s is zero as required by FIPS 186-4: Section 4.6
  152. * This is very unlikely.
  153. * Limit the retries so there is no possibility of an infinite
  154. * loop for bad domain parameter values.
  155. */
  156. if (BN_is_zero(ret->r) || BN_is_zero(ret->s)) {
  157. if (retries++ > MAX_DSA_SIGN_RETRIES) {
  158. reason = DSA_R_TOO_MANY_RETRIES;
  159. goto err;
  160. }
  161. goto redo;
  162. }
  163. rv = 1;
  164. err:
  165. if (rv == 0) {
  166. ERR_raise(ERR_LIB_DSA, reason);
  167. DSA_SIG_free(ret);
  168. ret = NULL;
  169. }
  170. BN_CTX_free(ctx);
  171. BN_clear_free(kinv);
  172. return ret;
  173. }
  174. static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  175. {
  176. return ossl_dsa_do_sign_int(dgst, dlen, dsa);
  177. }
  178. static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
  179. BIGNUM **kinvp, BIGNUM **rp)
  180. {
  181. return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
  182. }
  183. static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
  184. BIGNUM **kinvp, BIGNUM **rp,
  185. const unsigned char *dgst, int dlen)
  186. {
  187. BN_CTX *ctx = NULL;
  188. BIGNUM *k, *kinv = NULL, *r = *rp;
  189. BIGNUM *l;
  190. int ret = 0;
  191. int q_bits, q_words;
  192. if (!dsa->params.p || !dsa->params.q || !dsa->params.g) {
  193. ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
  194. return 0;
  195. }
  196. /* Reject obviously invalid parameters */
  197. if (BN_is_zero(dsa->params.p)
  198. || BN_is_zero(dsa->params.q)
  199. || BN_is_zero(dsa->params.g)
  200. || BN_is_negative(dsa->params.p)
  201. || BN_is_negative(dsa->params.q)
  202. || BN_is_negative(dsa->params.g)) {
  203. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_PARAMETERS);
  204. return 0;
  205. }
  206. if (dsa->priv_key == NULL) {
  207. ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PRIVATE_KEY);
  208. return 0;
  209. }
  210. k = BN_new();
  211. l = BN_new();
  212. if (k == NULL || l == NULL)
  213. goto err;
  214. if (ctx_in == NULL) {
  215. /* if you don't pass in ctx_in you get a default libctx */
  216. if ((ctx = BN_CTX_new_ex(NULL)) == NULL)
  217. goto err;
  218. } else
  219. ctx = ctx_in;
  220. /* Preallocate space */
  221. q_bits = BN_num_bits(dsa->params.q);
  222. q_words = bn_get_top(dsa->params.q);
  223. if (q_bits < MIN_DSA_SIGN_QBITS
  224. || !bn_wexpand(k, q_words + 2)
  225. || !bn_wexpand(l, q_words + 2))
  226. goto err;
  227. /* Get random k */
  228. do {
  229. if (dgst != NULL) {
  230. /*
  231. * We calculate k from SHA512(private_key + H(message) + random).
  232. * This protects the private key from a weak PRNG.
  233. */
  234. if (!BN_generate_dsa_nonce(k, dsa->params.q, dsa->priv_key, dgst,
  235. dlen, ctx))
  236. goto err;
  237. } else if (!BN_priv_rand_range_ex(k, dsa->params.q, 0, ctx))
  238. goto err;
  239. } while (BN_is_zero(k));
  240. BN_set_flags(k, BN_FLG_CONSTTIME);
  241. BN_set_flags(l, BN_FLG_CONSTTIME);
  242. if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
  243. if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
  244. dsa->lock, dsa->params.p, ctx))
  245. goto err;
  246. }
  247. /* Compute r = (g^k mod p) mod q */
  248. /*
  249. * We do not want timing information to leak the length of k, so we
  250. * compute G^k using an equivalent scalar of fixed bit-length.
  251. *
  252. * We unconditionally perform both of these additions to prevent a
  253. * small timing information leakage. We then choose the sum that is
  254. * one bit longer than the modulus.
  255. *
  256. * There are some concerns about the efficacy of doing this. More
  257. * specifically refer to the discussion starting with:
  258. * https://github.com/openssl/openssl/pull/7486#discussion_r228323705
  259. * The fix is to rework BN so these gymnastics aren't required.
  260. */
  261. if (!BN_add(l, k, dsa->params.q)
  262. || !BN_add(k, l, dsa->params.q))
  263. goto err;
  264. BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
  265. if ((dsa)->meth->bn_mod_exp != NULL) {
  266. if (!dsa->meth->bn_mod_exp(dsa, r, dsa->params.g, k, dsa->params.p,
  267. ctx, dsa->method_mont_p))
  268. goto err;
  269. } else {
  270. if (!BN_mod_exp_mont(r, dsa->params.g, k, dsa->params.p, ctx,
  271. dsa->method_mont_p))
  272. goto err;
  273. }
  274. if (!BN_mod(r, r, dsa->params.q, ctx))
  275. goto err;
  276. /* Compute part of 's = inv(k) (m + xr) mod q' */
  277. if ((kinv = dsa_mod_inverse_fermat(k, dsa->params.q, ctx)) == NULL)
  278. goto err;
  279. BN_clear_free(*kinvp);
  280. *kinvp = kinv;
  281. kinv = NULL;
  282. ret = 1;
  283. err:
  284. if (!ret)
  285. ERR_raise(ERR_LIB_DSA, ERR_R_BN_LIB);
  286. if (ctx != ctx_in)
  287. BN_CTX_free(ctx);
  288. BN_clear_free(k);
  289. BN_clear_free(l);
  290. return ret;
  291. }
  292. static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
  293. DSA_SIG *sig, DSA *dsa)
  294. {
  295. BN_CTX *ctx;
  296. BIGNUM *u1, *u2, *t1;
  297. BN_MONT_CTX *mont = NULL;
  298. const BIGNUM *r, *s;
  299. int ret = -1, i;
  300. if (dsa->params.p == NULL
  301. || dsa->params.q == NULL
  302. || dsa->params.g == NULL) {
  303. ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
  304. return -1;
  305. }
  306. i = BN_num_bits(dsa->params.q);
  307. /* fips 186-3 allows only different sizes for q */
  308. if (i != 160 && i != 224 && i != 256) {
  309. ERR_raise(ERR_LIB_DSA, DSA_R_BAD_Q_VALUE);
  310. return -1;
  311. }
  312. if (BN_num_bits(dsa->params.p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  313. ERR_raise(ERR_LIB_DSA, DSA_R_MODULUS_TOO_LARGE);
  314. return -1;
  315. }
  316. u1 = BN_new();
  317. u2 = BN_new();
  318. t1 = BN_new();
  319. ctx = BN_CTX_new_ex(NULL); /* verify does not need a libctx */
  320. if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
  321. goto err;
  322. DSA_SIG_get0(sig, &r, &s);
  323. if (BN_is_zero(r) || BN_is_negative(r) ||
  324. BN_ucmp(r, dsa->params.q) >= 0) {
  325. ret = 0;
  326. goto err;
  327. }
  328. if (BN_is_zero(s) || BN_is_negative(s) ||
  329. BN_ucmp(s, dsa->params.q) >= 0) {
  330. ret = 0;
  331. goto err;
  332. }
  333. /*
  334. * Calculate W = inv(S) mod Q save W in u2
  335. */
  336. if ((BN_mod_inverse(u2, s, dsa->params.q, ctx)) == NULL)
  337. goto err;
  338. /* save M in u1 */
  339. if (dgst_len > (i >> 3))
  340. /*
  341. * if the digest length is greater than the size of q use the
  342. * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
  343. * 4.2
  344. */
  345. dgst_len = (i >> 3);
  346. if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
  347. goto err;
  348. /* u1 = M * w mod q */
  349. if (!BN_mod_mul(u1, u1, u2, dsa->params.q, ctx))
  350. goto err;
  351. /* u2 = r * w mod q */
  352. if (!BN_mod_mul(u2, r, u2, dsa->params.q, ctx))
  353. goto err;
  354. if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
  355. mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
  356. dsa->lock, dsa->params.p, ctx);
  357. if (!mont)
  358. goto err;
  359. }
  360. if (dsa->meth->dsa_mod_exp != NULL) {
  361. if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->params.g, u1, dsa->pub_key, u2,
  362. dsa->params.p, ctx, mont))
  363. goto err;
  364. } else {
  365. if (!BN_mod_exp2_mont(t1, dsa->params.g, u1, dsa->pub_key, u2,
  366. dsa->params.p, ctx, mont))
  367. goto err;
  368. }
  369. /* let u1 = u1 mod q */
  370. if (!BN_mod(u1, t1, dsa->params.q, ctx))
  371. goto err;
  372. /*
  373. * V is now in u1. If the signature is correct, it will be equal to R.
  374. */
  375. ret = (BN_ucmp(u1, r) == 0);
  376. err:
  377. if (ret < 0)
  378. ERR_raise(ERR_LIB_DSA, ERR_R_BN_LIB);
  379. BN_CTX_free(ctx);
  380. BN_free(u1);
  381. BN_free(u2);
  382. BN_free(t1);
  383. return ret;
  384. }
  385. static int dsa_init(DSA *dsa)
  386. {
  387. dsa->flags |= DSA_FLAG_CACHE_MONT_P;
  388. dsa->dirty_cnt++;
  389. return 1;
  390. }
  391. static int dsa_finish(DSA *dsa)
  392. {
  393. BN_MONT_CTX_free(dsa->method_mont_p);
  394. return 1;
  395. }
  396. /*
  397. * Compute the inverse of k modulo q.
  398. * Since q is prime, Fermat's Little Theorem applies, which reduces this to
  399. * mod-exp operation. Both the exponent and modulus are public information
  400. * so a mod-exp that doesn't leak the base is sufficient. A newly allocated
  401. * BIGNUM is returned which the caller must free.
  402. */
  403. static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
  404. BN_CTX *ctx)
  405. {
  406. BIGNUM *res = NULL;
  407. BIGNUM *r, *e;
  408. if ((r = BN_new()) == NULL)
  409. return NULL;
  410. BN_CTX_start(ctx);
  411. if ((e = BN_CTX_get(ctx)) != NULL
  412. && BN_set_word(r, 2)
  413. && BN_sub(e, q, r)
  414. && BN_mod_exp_mont(r, k, e, q, ctx, NULL))
  415. res = r;
  416. else
  417. BN_free(r);
  418. BN_CTX_end(ctx);
  419. return res;
  420. }