You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

982 lines
24 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. *
  57. * The DSS routines are based on patches supplied by
  58. * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
  59. #include <openssl/dsa.h>
  60. #include <string.h>
  61. #include <openssl/bn.h>
  62. #include <openssl/dh.h>
  63. #include <openssl/digest.h>
  64. #include <openssl/engine.h>
  65. #include <openssl/err.h>
  66. #include <openssl/ex_data.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/rand.h>
  69. #include <openssl/sha.h>
  70. #include <openssl/thread.h>
  71. #include "../fipsmodule/bn/internal.h"
  72. #include "../internal.h"
  73. #define OPENSSL_DSA_MAX_MODULUS_BITS 10000
  74. // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
  75. // Rabin-Miller
  76. #define DSS_prime_checks 50
  77. static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
  78. BIGNUM **out_r);
  79. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  80. DSA *DSA_new(void) {
  81. DSA *dsa = OPENSSL_malloc(sizeof(DSA));
  82. if (dsa == NULL) {
  83. OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
  84. return NULL;
  85. }
  86. OPENSSL_memset(dsa, 0, sizeof(DSA));
  87. dsa->references = 1;
  88. CRYPTO_MUTEX_init(&dsa->method_mont_lock);
  89. CRYPTO_new_ex_data(&dsa->ex_data);
  90. return dsa;
  91. }
  92. void DSA_free(DSA *dsa) {
  93. if (dsa == NULL) {
  94. return;
  95. }
  96. if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
  97. return;
  98. }
  99. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  100. BN_clear_free(dsa->p);
  101. BN_clear_free(dsa->q);
  102. BN_clear_free(dsa->g);
  103. BN_clear_free(dsa->pub_key);
  104. BN_clear_free(dsa->priv_key);
  105. BN_MONT_CTX_free(dsa->method_mont_p);
  106. BN_MONT_CTX_free(dsa->method_mont_q);
  107. CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
  108. OPENSSL_free(dsa);
  109. }
  110. int DSA_up_ref(DSA *dsa) {
  111. CRYPTO_refcount_inc(&dsa->references);
  112. return 1;
  113. }
  114. void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
  115. const BIGNUM **out_priv_key) {
  116. if (out_pub_key != NULL) {
  117. *out_pub_key = dsa->pub_key;
  118. }
  119. if (out_priv_key != NULL) {
  120. *out_priv_key = dsa->priv_key;
  121. }
  122. }
  123. void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
  124. const BIGNUM **out_g) {
  125. if (out_p != NULL) {
  126. *out_p = dsa->p;
  127. }
  128. if (out_q != NULL) {
  129. *out_q = dsa->q;
  130. }
  131. if (out_g != NULL) {
  132. *out_g = dsa->g;
  133. }
  134. }
  135. int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) {
  136. if (dsa->pub_key == NULL && pub_key == NULL) {
  137. return 0;
  138. }
  139. if (pub_key != NULL) {
  140. BN_free(dsa->pub_key);
  141. dsa->pub_key = pub_key;
  142. }
  143. if (priv_key != NULL) {
  144. BN_free(dsa->priv_key);
  145. dsa->priv_key = priv_key;
  146. }
  147. return 1;
  148. }
  149. int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
  150. if ((dsa->p == NULL && p == NULL) ||
  151. (dsa->q == NULL && q == NULL) ||
  152. (dsa->g == NULL && g == NULL)) {
  153. return 0;
  154. }
  155. if (p != NULL) {
  156. BN_free(dsa->p);
  157. dsa->p = p;
  158. }
  159. if (q != NULL) {
  160. BN_free(dsa->q);
  161. dsa->q = q;
  162. }
  163. if (g != NULL) {
  164. BN_free(dsa->g);
  165. dsa->g = g;
  166. }
  167. return 1;
  168. }
  169. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  170. size_t seed_len, int *out_counter,
  171. unsigned long *out_h, BN_GENCB *cb) {
  172. int ok = 0;
  173. unsigned char seed[SHA256_DIGEST_LENGTH];
  174. unsigned char md[SHA256_DIGEST_LENGTH];
  175. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  176. BIGNUM *r0, *W, *X, *c, *test;
  177. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  178. BN_MONT_CTX *mont = NULL;
  179. int k, n = 0, m = 0;
  180. unsigned i;
  181. int counter = 0;
  182. int r = 0;
  183. BN_CTX *ctx = NULL;
  184. unsigned int h = 2;
  185. unsigned qsize;
  186. const EVP_MD *evpmd;
  187. evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
  188. qsize = EVP_MD_size(evpmd);
  189. if (bits < 512) {
  190. bits = 512;
  191. }
  192. bits = (bits + 63) / 64 * 64;
  193. if (seed_in != NULL) {
  194. if (seed_len < (size_t)qsize) {
  195. return 0;
  196. }
  197. if (seed_len > (size_t)qsize) {
  198. // Only consume as much seed as is expected.
  199. seed_len = qsize;
  200. }
  201. OPENSSL_memcpy(seed, seed_in, seed_len);
  202. }
  203. ctx = BN_CTX_new();
  204. if (ctx == NULL) {
  205. goto err;
  206. }
  207. BN_CTX_start(ctx);
  208. r0 = BN_CTX_get(ctx);
  209. g = BN_CTX_get(ctx);
  210. W = BN_CTX_get(ctx);
  211. q = BN_CTX_get(ctx);
  212. X = BN_CTX_get(ctx);
  213. c = BN_CTX_get(ctx);
  214. p = BN_CTX_get(ctx);
  215. test = BN_CTX_get(ctx);
  216. if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
  217. goto err;
  218. }
  219. for (;;) {
  220. // Find q.
  221. for (;;) {
  222. // step 1
  223. if (!BN_GENCB_call(cb, 0, m++)) {
  224. goto err;
  225. }
  226. int use_random_seed = (seed_in == NULL);
  227. if (use_random_seed) {
  228. if (!RAND_bytes(seed, qsize)) {
  229. goto err;
  230. }
  231. } else {
  232. // If we come back through, use random seed next time.
  233. seed_in = NULL;
  234. }
  235. OPENSSL_memcpy(buf, seed, qsize);
  236. OPENSSL_memcpy(buf2, seed, qsize);
  237. // precompute "SEED + 1" for step 7:
  238. for (i = qsize - 1; i < qsize; i--) {
  239. buf[i]++;
  240. if (buf[i] != 0) {
  241. break;
  242. }
  243. }
  244. // step 2
  245. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
  246. !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
  247. goto err;
  248. }
  249. for (i = 0; i < qsize; i++) {
  250. md[i] ^= buf2[i];
  251. }
  252. // step 3
  253. md[0] |= 0x80;
  254. md[qsize - 1] |= 0x01;
  255. if (!BN_bin2bn(md, qsize, q)) {
  256. goto err;
  257. }
  258. // step 4
  259. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
  260. if (r > 0) {
  261. break;
  262. }
  263. if (r != 0) {
  264. goto err;
  265. }
  266. // do a callback call
  267. // step 5
  268. }
  269. if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
  270. goto err;
  271. }
  272. // step 6
  273. counter = 0;
  274. // "offset = 2"
  275. n = (bits - 1) / 160;
  276. for (;;) {
  277. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
  278. goto err;
  279. }
  280. // step 7
  281. BN_zero(W);
  282. // now 'buf' contains "SEED + offset - 1"
  283. for (k = 0; k <= n; k++) {
  284. // obtain "SEED + offset + k" by incrementing:
  285. for (i = qsize - 1; i < qsize; i--) {
  286. buf[i]++;
  287. if (buf[i] != 0) {
  288. break;
  289. }
  290. }
  291. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
  292. goto err;
  293. }
  294. // step 8
  295. if (!BN_bin2bn(md, qsize, r0) ||
  296. !BN_lshift(r0, r0, (qsize << 3) * k) ||
  297. !BN_add(W, W, r0)) {
  298. goto err;
  299. }
  300. }
  301. // more of step 8
  302. if (!BN_mask_bits(W, bits - 1) ||
  303. !BN_copy(X, W) ||
  304. !BN_add(X, X, test)) {
  305. goto err;
  306. }
  307. // step 9
  308. if (!BN_lshift1(r0, q) ||
  309. !BN_mod(c, X, r0, ctx) ||
  310. !BN_sub(r0, c, BN_value_one()) ||
  311. !BN_sub(p, X, r0)) {
  312. goto err;
  313. }
  314. // step 10
  315. if (BN_cmp(p, test) >= 0) {
  316. // step 11
  317. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  318. if (r > 0) {
  319. goto end; // found it
  320. }
  321. if (r != 0) {
  322. goto err;
  323. }
  324. }
  325. // step 13
  326. counter++;
  327. // "offset = offset + n + 1"
  328. // step 14
  329. if (counter >= 4096) {
  330. break;
  331. }
  332. }
  333. }
  334. end:
  335. if (!BN_GENCB_call(cb, 2, 1)) {
  336. goto err;
  337. }
  338. // We now need to generate g
  339. // Set r0=(p-1)/q
  340. if (!BN_sub(test, p, BN_value_one()) ||
  341. !BN_div(r0, NULL, test, q, ctx)) {
  342. goto err;
  343. }
  344. mont = BN_MONT_CTX_new_for_modulus(p, ctx);
  345. if (mont == NULL ||
  346. !BN_set_word(test, h)) {
  347. goto err;
  348. }
  349. for (;;) {
  350. // g=test^r0%p
  351. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
  352. goto err;
  353. }
  354. if (!BN_is_one(g)) {
  355. break;
  356. }
  357. if (!BN_add(test, test, BN_value_one())) {
  358. goto err;
  359. }
  360. h++;
  361. }
  362. if (!BN_GENCB_call(cb, 3, 1)) {
  363. goto err;
  364. }
  365. ok = 1;
  366. err:
  367. if (ok) {
  368. BN_free(dsa->p);
  369. BN_free(dsa->q);
  370. BN_free(dsa->g);
  371. dsa->p = BN_dup(p);
  372. dsa->q = BN_dup(q);
  373. dsa->g = BN_dup(g);
  374. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  375. ok = 0;
  376. goto err;
  377. }
  378. if (out_counter != NULL) {
  379. *out_counter = counter;
  380. }
  381. if (out_h != NULL) {
  382. *out_h = h;
  383. }
  384. }
  385. if (ctx) {
  386. BN_CTX_end(ctx);
  387. BN_CTX_free(ctx);
  388. }
  389. BN_MONT_CTX_free(mont);
  390. return ok;
  391. }
  392. DSA *DSAparams_dup(const DSA *dsa) {
  393. DSA *ret = DSA_new();
  394. if (ret == NULL) {
  395. return NULL;
  396. }
  397. ret->p = BN_dup(dsa->p);
  398. ret->q = BN_dup(dsa->q);
  399. ret->g = BN_dup(dsa->g);
  400. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  401. DSA_free(ret);
  402. return NULL;
  403. }
  404. return ret;
  405. }
  406. int DSA_generate_key(DSA *dsa) {
  407. int ok = 0;
  408. BN_CTX *ctx = NULL;
  409. BIGNUM *pub_key = NULL, *priv_key = NULL;
  410. ctx = BN_CTX_new();
  411. if (ctx == NULL) {
  412. goto err;
  413. }
  414. priv_key = dsa->priv_key;
  415. if (priv_key == NULL) {
  416. priv_key = BN_new();
  417. if (priv_key == NULL) {
  418. goto err;
  419. }
  420. }
  421. if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
  422. goto err;
  423. }
  424. pub_key = dsa->pub_key;
  425. if (pub_key == NULL) {
  426. pub_key = BN_new();
  427. if (pub_key == NULL) {
  428. goto err;
  429. }
  430. }
  431. if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
  432. dsa->p, ctx) ||
  433. !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
  434. dsa->method_mont_p)) {
  435. goto err;
  436. }
  437. dsa->priv_key = priv_key;
  438. dsa->pub_key = pub_key;
  439. ok = 1;
  440. err:
  441. if (dsa->pub_key == NULL) {
  442. BN_free(pub_key);
  443. }
  444. if (dsa->priv_key == NULL) {
  445. BN_free(priv_key);
  446. }
  447. BN_CTX_free(ctx);
  448. return ok;
  449. }
  450. DSA_SIG *DSA_SIG_new(void) {
  451. DSA_SIG *sig;
  452. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  453. if (!sig) {
  454. return NULL;
  455. }
  456. sig->r = NULL;
  457. sig->s = NULL;
  458. return sig;
  459. }
  460. void DSA_SIG_free(DSA_SIG *sig) {
  461. if (!sig) {
  462. return;
  463. }
  464. BN_free(sig->r);
  465. BN_free(sig->s);
  466. OPENSSL_free(sig);
  467. }
  468. // mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
  469. // |b| as secret. This function internally uses Montgomery reduction, but
  470. // neither inputs nor outputs are in Montgomery form.
  471. static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  472. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  473. BN_CTX_start(ctx);
  474. BIGNUM *tmp = BN_CTX_get(ctx);
  475. // |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a
  476. // single |BN_to_montgomery| which adds one factor of R.
  477. int ok = tmp != NULL &&
  478. BN_to_montgomery(tmp, a, mont, ctx) &&
  479. BN_mod_mul_montgomery(r, tmp, b, mont, ctx);
  480. BN_CTX_end(ctx);
  481. return ok;
  482. }
  483. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
  484. BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
  485. BIGNUM m;
  486. BIGNUM xr;
  487. BN_CTX *ctx = NULL;
  488. int reason = ERR_R_BN_LIB;
  489. DSA_SIG *ret = NULL;
  490. BN_init(&m);
  491. BN_init(&xr);
  492. if (!dsa->p || !dsa->q || !dsa->g) {
  493. reason = DSA_R_MISSING_PARAMETERS;
  494. goto err;
  495. }
  496. // We only support DSA keys that are a multiple of 8 bits. (This is a weaker
  497. // check than the one in |DSA_do_check_signature|, which only allows 160-,
  498. // 224-, and 256-bit keys.
  499. if (BN_num_bits(dsa->q) % 8 != 0) {
  500. reason = DSA_R_BAD_Q_VALUE;
  501. goto err;
  502. }
  503. s = BN_new();
  504. if (s == NULL) {
  505. goto err;
  506. }
  507. ctx = BN_CTX_new();
  508. if (ctx == NULL) {
  509. goto err;
  510. }
  511. redo:
  512. if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
  513. goto err;
  514. }
  515. if (digest_len > BN_num_bytes(dsa->q)) {
  516. // If the digest length is greater than the size of |dsa->q| use the
  517. // BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2.
  518. // Note the above check that |dsa->q| is a multiple of 8 bits.
  519. digest_len = BN_num_bytes(dsa->q);
  520. }
  521. if (BN_bin2bn(digest, digest_len, &m) == NULL) {
  522. goto err;
  523. }
  524. // |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This
  525. // violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions.
  526. // (The underlying algorithms could accept looser bounds, but we reduce for
  527. // simplicity.)
  528. size_t q_width = bn_minimal_width(dsa->q);
  529. if (!bn_resize_words(&m, q_width) ||
  530. !bn_resize_words(&xr, q_width)) {
  531. goto err;
  532. }
  533. bn_reduce_once_in_place(m.d, 0 /* no carry word */, dsa->q->d,
  534. xr.d /* scratch space */, q_width);
  535. // Compute s = inv(k) (m + xr) mod q. Note |dsa->method_mont_q| is
  536. // initialized by |dsa_sign_setup|.
  537. if (!mod_mul_consttime(&xr, dsa->priv_key, r, dsa->method_mont_q, ctx) ||
  538. !bn_mod_add_consttime(s, &xr, &m, dsa->q, ctx) ||
  539. !mod_mul_consttime(s, s, kinv, dsa->method_mont_q, ctx)) {
  540. goto err;
  541. }
  542. // Redo if r or s is zero as required by FIPS 186-3: this is
  543. // very unlikely.
  544. if (BN_is_zero(r) || BN_is_zero(s)) {
  545. goto redo;
  546. }
  547. ret = DSA_SIG_new();
  548. if (ret == NULL) {
  549. goto err;
  550. }
  551. ret->r = r;
  552. ret->s = s;
  553. err:
  554. if (ret == NULL) {
  555. OPENSSL_PUT_ERROR(DSA, reason);
  556. BN_free(r);
  557. BN_free(s);
  558. }
  559. BN_CTX_free(ctx);
  560. BN_clear_free(&m);
  561. BN_clear_free(&xr);
  562. BN_clear_free(kinv);
  563. return ret;
  564. }
  565. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  566. const DSA *dsa) {
  567. int valid;
  568. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  569. return -1;
  570. }
  571. return valid;
  572. }
  573. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  574. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  575. BN_CTX *ctx;
  576. BIGNUM u1, u2, t1;
  577. int ret = 0;
  578. unsigned i;
  579. *out_valid = 0;
  580. if (!dsa->p || !dsa->q || !dsa->g) {
  581. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  582. return 0;
  583. }
  584. i = BN_num_bits(dsa->q);
  585. // FIPS 186-3 allows only different sizes for q.
  586. if (i != 160 && i != 224 && i != 256) {
  587. OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
  588. return 0;
  589. }
  590. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  591. OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
  592. return 0;
  593. }
  594. BN_init(&u1);
  595. BN_init(&u2);
  596. BN_init(&t1);
  597. ctx = BN_CTX_new();
  598. if (ctx == NULL) {
  599. goto err;
  600. }
  601. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  602. BN_ucmp(sig->r, dsa->q) >= 0) {
  603. ret = 1;
  604. goto err;
  605. }
  606. if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
  607. BN_ucmp(sig->s, dsa->q) >= 0) {
  608. ret = 1;
  609. goto err;
  610. }
  611. // Calculate W = inv(S) mod Q
  612. // save W in u2
  613. if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
  614. goto err;
  615. }
  616. // save M in u1
  617. if (digest_len > (i >> 3)) {
  618. // if the digest length is greater than the size of q use the
  619. // BN_num_bits(dsa->q) leftmost bits of the digest, see
  620. // fips 186-3, 4.2
  621. digest_len = (i >> 3);
  622. }
  623. if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
  624. goto err;
  625. }
  626. // u1 = M * w mod q
  627. if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
  628. goto err;
  629. }
  630. // u2 = r * w mod q
  631. if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
  632. goto err;
  633. }
  634. if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  635. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  636. ctx)) {
  637. goto err;
  638. }
  639. if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
  640. dsa->method_mont_p)) {
  641. goto err;
  642. }
  643. // BN_copy(&u1,&t1);
  644. // let u1 = u1 mod q
  645. if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
  646. goto err;
  647. }
  648. // V is now in u1. If the signature is correct, it will be
  649. // equal to R.
  650. *out_valid = BN_ucmp(&u1, sig->r) == 0;
  651. ret = 1;
  652. err:
  653. if (ret != 1) {
  654. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  655. }
  656. BN_CTX_free(ctx);
  657. BN_free(&u1);
  658. BN_free(&u2);
  659. BN_free(&t1);
  660. return ret;
  661. }
  662. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  663. uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) {
  664. DSA_SIG *s;
  665. s = DSA_do_sign(digest, digest_len, dsa);
  666. if (s == NULL) {
  667. *out_siglen = 0;
  668. return 0;
  669. }
  670. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  671. DSA_SIG_free(s);
  672. return 1;
  673. }
  674. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  675. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  676. int valid;
  677. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  678. return -1;
  679. }
  680. return valid;
  681. }
  682. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  683. size_t digest_len, const uint8_t *sig, size_t sig_len,
  684. const DSA *dsa) {
  685. DSA_SIG *s = NULL;
  686. int ret = 0;
  687. uint8_t *der = NULL;
  688. s = DSA_SIG_new();
  689. if (s == NULL) {
  690. goto err;
  691. }
  692. const uint8_t *sigp = sig;
  693. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  694. goto err;
  695. }
  696. // Ensure that the signature uses DER and doesn't have trailing garbage.
  697. int der_len = i2d_DSA_SIG(s, &der);
  698. if (der_len < 0 || (size_t)der_len != sig_len ||
  699. OPENSSL_memcmp(sig, der, sig_len)) {
  700. goto err;
  701. }
  702. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  703. err:
  704. OPENSSL_free(der);
  705. DSA_SIG_free(s);
  706. return ret;
  707. }
  708. // der_len_len returns the number of bytes needed to represent a length of |len|
  709. // in DER.
  710. static size_t der_len_len(size_t len) {
  711. if (len < 0x80) {
  712. return 1;
  713. }
  714. size_t ret = 1;
  715. while (len > 0) {
  716. ret++;
  717. len >>= 8;
  718. }
  719. return ret;
  720. }
  721. int DSA_size(const DSA *dsa) {
  722. size_t order_len = BN_num_bytes(dsa->q);
  723. // Compute the maximum length of an |order_len| byte integer. Defensively
  724. // assume that the leading 0x00 is included.
  725. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
  726. if (integer_len < order_len) {
  727. return 0;
  728. }
  729. // A DSA signature is two INTEGERs.
  730. size_t value_len = 2 * integer_len;
  731. if (value_len < integer_len) {
  732. return 0;
  733. }
  734. // Add the header.
  735. size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
  736. if (ret < value_len) {
  737. return 0;
  738. }
  739. return ret;
  740. }
  741. static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
  742. BIGNUM **out_r) {
  743. BN_CTX *ctx;
  744. BIGNUM k, *kinv = NULL, *r = NULL;
  745. int ret = 0;
  746. if (!dsa->p || !dsa->q || !dsa->g) {
  747. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  748. return 0;
  749. }
  750. BN_init(&k);
  751. ctx = ctx_in;
  752. if (ctx == NULL) {
  753. ctx = BN_CTX_new();
  754. if (ctx == NULL) {
  755. goto err;
  756. }
  757. }
  758. r = BN_new();
  759. kinv = BN_new();
  760. if (r == NULL || kinv == NULL ||
  761. // Get random k
  762. !BN_rand_range_ex(&k, 1, dsa->q) ||
  763. !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  764. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  765. ctx) ||
  766. !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
  767. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
  768. ctx) ||
  769. // Compute r = (g^k mod p) mod q
  770. !BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx,
  771. dsa->method_mont_p) ||
  772. // Note |BN_mod| below is not constant-time and may leak information about
  773. // |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not
  774. // easily performed in constant-time with Montgomery reduction.
  775. //
  776. // However, |r| at this point is g^k (mod p). It is almost the value of
  777. // |r| revealed in the signature anyway (g^k (mod p) (mod q)), going from
  778. // it to |k| would require computing a discrete log.
  779. !BN_mod(r, r, dsa->q, ctx) ||
  780. // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
  781. // Theorem.
  782. !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
  783. goto err;
  784. }
  785. BN_clear_free(*out_kinv);
  786. *out_kinv = kinv;
  787. kinv = NULL;
  788. BN_clear_free(*out_r);
  789. *out_r = r;
  790. ret = 1;
  791. err:
  792. if (!ret) {
  793. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  794. if (r != NULL) {
  795. BN_clear_free(r);
  796. }
  797. }
  798. if (ctx_in == NULL) {
  799. BN_CTX_free(ctx);
  800. }
  801. BN_clear_free(&k);
  802. BN_clear_free(kinv);
  803. return ret;
  804. }
  805. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  806. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  807. int index;
  808. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  809. free_func)) {
  810. return -1;
  811. }
  812. return index;
  813. }
  814. int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
  815. return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
  816. }
  817. void *DSA_get_ex_data(const DSA *dsa, int idx) {
  818. return CRYPTO_get_ex_data(&dsa->ex_data, idx);
  819. }
  820. DH *DSA_dup_DH(const DSA *dsa) {
  821. if (dsa == NULL) {
  822. return NULL;
  823. }
  824. DH *ret = DH_new();
  825. if (ret == NULL) {
  826. goto err;
  827. }
  828. if (dsa->q != NULL) {
  829. ret->priv_length = BN_num_bits(dsa->q);
  830. if ((ret->q = BN_dup(dsa->q)) == NULL) {
  831. goto err;
  832. }
  833. }
  834. if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
  835. (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
  836. (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
  837. (dsa->priv_key != NULL &&
  838. (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {
  839. goto err;
  840. }
  841. return ret;
  842. err:
  843. DH_free(ret);
  844. return NULL;
  845. }