1
0

dsa_ossl.c 12 KB

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