dsa_ossl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright 1995-2016 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 <openssl/bn.h>
  12. #include <openssl/sha.h>
  13. #include "dsa_locl.h"
  14. #include <openssl/asn1.h>
  15. static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  16. static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
  17. BIGNUM **rp);
  18. static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
  19. BIGNUM **rp, const unsigned char *dgst, int dlen);
  20. static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
  21. DSA_SIG *sig, DSA *dsa);
  22. static int dsa_init(DSA *dsa);
  23. static int dsa_finish(DSA *dsa);
  24. static DSA_METHOD openssl_dsa_meth = {
  25. "OpenSSL DSA method",
  26. dsa_do_sign,
  27. dsa_sign_setup_no_digest,
  28. dsa_do_verify,
  29. NULL, /* dsa_mod_exp, */
  30. NULL, /* dsa_bn_mod_exp, */
  31. dsa_init,
  32. dsa_finish,
  33. DSA_FLAG_FIPS_METHOD,
  34. NULL,
  35. NULL,
  36. NULL
  37. };
  38. static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
  39. void DSA_set_default_method(const DSA_METHOD *meth)
  40. {
  41. default_DSA_method = meth;
  42. }
  43. const DSA_METHOD *DSA_get_default_method(void)
  44. {
  45. return default_DSA_method;
  46. }
  47. const DSA_METHOD *DSA_OpenSSL(void)
  48. {
  49. return &openssl_dsa_meth;
  50. }
  51. static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  52. {
  53. BIGNUM *kinv = NULL;
  54. BIGNUM *m;
  55. BIGNUM *xr;
  56. BN_CTX *ctx = NULL;
  57. int reason = ERR_R_BN_LIB;
  58. DSA_SIG *ret = NULL;
  59. int rv = 0;
  60. m = BN_new();
  61. xr = BN_new();
  62. if (m == NULL || xr == NULL)
  63. goto err;
  64. if (!dsa->p || !dsa->q || !dsa->g) {
  65. reason = DSA_R_MISSING_PARAMETERS;
  66. goto err;
  67. }
  68. ret = DSA_SIG_new();
  69. if (ret == NULL)
  70. goto err;
  71. ret->r = BN_new();
  72. ret->s = BN_new();
  73. if (ret->r == NULL || ret->s == NULL)
  74. goto err;
  75. ctx = BN_CTX_new();
  76. if (ctx == NULL)
  77. goto err;
  78. redo:
  79. if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
  80. goto err;
  81. if (dlen > BN_num_bytes(dsa->q))
  82. /*
  83. * if the digest length is greater than the size of q use the
  84. * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
  85. * 4.2
  86. */
  87. dlen = BN_num_bytes(dsa->q);
  88. if (BN_bin2bn(dgst, dlen, m) == NULL)
  89. goto err;
  90. /* Compute s = inv(k) (m + xr) mod q */
  91. if (!BN_mod_mul(xr, dsa->priv_key, ret->r, dsa->q, ctx))
  92. goto err; /* s = xr */
  93. if (!BN_add(ret->s, xr, m))
  94. goto err; /* s = m + xr */
  95. if (BN_cmp(ret->s, dsa->q) > 0)
  96. if (!BN_sub(ret->s, ret->s, dsa->q))
  97. goto err;
  98. if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
  99. goto err;
  100. /*
  101. * Redo if r or s is zero as required by FIPS 186-3: this is very
  102. * unlikely.
  103. */
  104. if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
  105. goto redo;
  106. rv = 1;
  107. err:
  108. if (rv == 0) {
  109. DSAerr(DSA_F_DSA_DO_SIGN, reason);
  110. DSA_SIG_free(ret);
  111. ret = NULL;
  112. }
  113. BN_CTX_free(ctx);
  114. BN_clear_free(m);
  115. BN_clear_free(xr);
  116. BN_clear_free(kinv);
  117. return ret;
  118. }
  119. static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
  120. BIGNUM **kinvp, BIGNUM **rp)
  121. {
  122. return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
  123. }
  124. static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
  125. BIGNUM **kinvp, BIGNUM **rp,
  126. const unsigned char *dgst, int dlen)
  127. {
  128. BN_CTX *ctx = NULL;
  129. BIGNUM *k, *kinv = NULL, *r = *rp;
  130. BIGNUM *l, *m;
  131. int ret = 0;
  132. int q_bits;
  133. if (!dsa->p || !dsa->q || !dsa->g) {
  134. DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
  135. return 0;
  136. }
  137. k = BN_new();
  138. l = BN_new();
  139. m = BN_new();
  140. if (k == NULL || l == NULL || m == NULL)
  141. goto err;
  142. if (ctx_in == NULL) {
  143. if ((ctx = BN_CTX_new()) == NULL)
  144. goto err;
  145. } else
  146. ctx = ctx_in;
  147. /* Preallocate space */
  148. q_bits = BN_num_bits(dsa->q);
  149. if (!BN_set_bit(k, q_bits)
  150. || !BN_set_bit(l, q_bits)
  151. || !BN_set_bit(m, q_bits))
  152. goto err;
  153. /* Get random k */
  154. do {
  155. if (dgst != NULL) {
  156. /*
  157. * We calculate k from SHA512(private_key + H(message) + random).
  158. * This protects the private key from a weak PRNG.
  159. */
  160. if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
  161. dlen, ctx))
  162. goto err;
  163. } else if (!BN_priv_rand_range(k, dsa->q))
  164. goto err;
  165. } while (BN_is_zero(k));
  166. BN_set_flags(k, BN_FLG_CONSTTIME);
  167. if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
  168. if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
  169. dsa->lock, dsa->p, ctx))
  170. goto err;
  171. }
  172. /* Compute r = (g^k mod p) mod q */
  173. /*
  174. * We do not want timing information to leak the length of k, so we
  175. * compute G^k using an equivalent scalar of fixed bit-length.
  176. *
  177. * We unconditionally perform both of these additions to prevent a
  178. * small timing information leakage. We then choose the sum that is
  179. * one bit longer than the modulus.
  180. *
  181. * TODO: revisit the BN_copy aiming for a memory access agnostic
  182. * conditional copy.
  183. */
  184. if (!BN_add(l, k, dsa->q)
  185. || !BN_add(m, l, dsa->q)
  186. || !BN_copy(k, BN_num_bits(l) > q_bits ? l : m))
  187. goto err;
  188. if ((dsa)->meth->bn_mod_exp != NULL) {
  189. if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
  190. dsa->method_mont_p))
  191. goto err;
  192. } else {
  193. if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
  194. goto err;
  195. }
  196. if (!BN_mod(r, r, dsa->q, ctx))
  197. goto err;
  198. /* Compute part of 's = inv(k) (m + xr) mod q' */
  199. if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
  200. goto err;
  201. BN_clear_free(*kinvp);
  202. *kinvp = kinv;
  203. kinv = NULL;
  204. ret = 1;
  205. err:
  206. if (!ret)
  207. DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
  208. if (ctx != ctx_in)
  209. BN_CTX_free(ctx);
  210. BN_clear_free(k);
  211. BN_clear_free(l);
  212. BN_clear_free(m);
  213. return ret;
  214. }
  215. static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
  216. DSA_SIG *sig, DSA *dsa)
  217. {
  218. BN_CTX *ctx;
  219. BIGNUM *u1, *u2, *t1;
  220. BN_MONT_CTX *mont = NULL;
  221. const BIGNUM *r, *s;
  222. int ret = -1, i;
  223. if (!dsa->p || !dsa->q || !dsa->g) {
  224. DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
  225. return -1;
  226. }
  227. i = BN_num_bits(dsa->q);
  228. /* fips 186-3 allows only different sizes for q */
  229. if (i != 160 && i != 224 && i != 256) {
  230. DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
  231. return -1;
  232. }
  233. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  234. DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
  235. return -1;
  236. }
  237. u1 = BN_new();
  238. u2 = BN_new();
  239. t1 = BN_new();
  240. ctx = BN_CTX_new();
  241. if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
  242. goto err;
  243. DSA_SIG_get0(sig, &r, &s);
  244. if (BN_is_zero(r) || BN_is_negative(r) ||
  245. BN_ucmp(r, dsa->q) >= 0) {
  246. ret = 0;
  247. goto err;
  248. }
  249. if (BN_is_zero(s) || BN_is_negative(s) ||
  250. BN_ucmp(s, dsa->q) >= 0) {
  251. ret = 0;
  252. goto err;
  253. }
  254. /*
  255. * Calculate W = inv(S) mod Q save W in u2
  256. */
  257. if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
  258. goto err;
  259. /* save M in u1 */
  260. if (dgst_len > (i >> 3))
  261. /*
  262. * if the digest length is greater than the size of q use the
  263. * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
  264. * 4.2
  265. */
  266. dgst_len = (i >> 3);
  267. if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
  268. goto err;
  269. /* u1 = M * w mod q */
  270. if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
  271. goto err;
  272. /* u2 = r * w mod q */
  273. if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
  274. goto err;
  275. if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
  276. mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
  277. dsa->lock, dsa->p, ctx);
  278. if (!mont)
  279. goto err;
  280. }
  281. if (dsa->meth->dsa_mod_exp != NULL) {
  282. if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
  283. dsa->p, ctx, mont))
  284. goto err;
  285. } else {
  286. if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
  287. mont))
  288. goto err;
  289. }
  290. /* let u1 = u1 mod q */
  291. if (!BN_mod(u1, t1, dsa->q, ctx))
  292. goto err;
  293. /*
  294. * V is now in u1. If the signature is correct, it will be equal to R.
  295. */
  296. ret = (BN_ucmp(u1, r) == 0);
  297. err:
  298. if (ret < 0)
  299. DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
  300. BN_CTX_free(ctx);
  301. BN_free(u1);
  302. BN_free(u2);
  303. BN_free(t1);
  304. return ret;
  305. }
  306. static int dsa_init(DSA *dsa)
  307. {
  308. dsa->flags |= DSA_FLAG_CACHE_MONT_P;
  309. return 1;
  310. }
  311. static int dsa_finish(DSA *dsa)
  312. {
  313. BN_MONT_CTX_free(dsa->method_mont_p);
  314. return 1;
  315. }