25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

997 lines
23 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 CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  78. DSA *DSA_new(void) {
  79. DSA *dsa = OPENSSL_malloc(sizeof(DSA));
  80. if (dsa == NULL) {
  81. OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
  82. return NULL;
  83. }
  84. OPENSSL_memset(dsa, 0, sizeof(DSA));
  85. dsa->references = 1;
  86. CRYPTO_MUTEX_init(&dsa->method_mont_lock);
  87. CRYPTO_new_ex_data(&dsa->ex_data);
  88. return dsa;
  89. }
  90. void DSA_free(DSA *dsa) {
  91. if (dsa == NULL) {
  92. return;
  93. }
  94. if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
  95. return;
  96. }
  97. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  98. BN_clear_free(dsa->p);
  99. BN_clear_free(dsa->q);
  100. BN_clear_free(dsa->g);
  101. BN_clear_free(dsa->pub_key);
  102. BN_clear_free(dsa->priv_key);
  103. BN_clear_free(dsa->kinv);
  104. BN_clear_free(dsa->r);
  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. mont = BN_MONT_CTX_new();
  209. if (mont == NULL) {
  210. goto err;
  211. }
  212. r0 = BN_CTX_get(ctx);
  213. g = BN_CTX_get(ctx);
  214. W = BN_CTX_get(ctx);
  215. q = BN_CTX_get(ctx);
  216. X = BN_CTX_get(ctx);
  217. c = BN_CTX_get(ctx);
  218. p = BN_CTX_get(ctx);
  219. test = BN_CTX_get(ctx);
  220. if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
  221. goto err;
  222. }
  223. for (;;) {
  224. // Find q.
  225. for (;;) {
  226. // step 1
  227. if (!BN_GENCB_call(cb, 0, m++)) {
  228. goto err;
  229. }
  230. int use_random_seed = (seed_in == NULL);
  231. if (use_random_seed) {
  232. if (!RAND_bytes(seed, qsize)) {
  233. goto err;
  234. }
  235. } else {
  236. // If we come back through, use random seed next time.
  237. seed_in = NULL;
  238. }
  239. OPENSSL_memcpy(buf, seed, qsize);
  240. OPENSSL_memcpy(buf2, seed, qsize);
  241. // precompute "SEED + 1" for step 7:
  242. for (i = qsize - 1; i < qsize; i--) {
  243. buf[i]++;
  244. if (buf[i] != 0) {
  245. break;
  246. }
  247. }
  248. // step 2
  249. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
  250. !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
  251. goto err;
  252. }
  253. for (i = 0; i < qsize; i++) {
  254. md[i] ^= buf2[i];
  255. }
  256. // step 3
  257. md[0] |= 0x80;
  258. md[qsize - 1] |= 0x01;
  259. if (!BN_bin2bn(md, qsize, q)) {
  260. goto err;
  261. }
  262. // step 4
  263. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
  264. if (r > 0) {
  265. break;
  266. }
  267. if (r != 0) {
  268. goto err;
  269. }
  270. // do a callback call
  271. // step 5
  272. }
  273. if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
  274. goto err;
  275. }
  276. // step 6
  277. counter = 0;
  278. // "offset = 2"
  279. n = (bits - 1) / 160;
  280. for (;;) {
  281. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
  282. goto err;
  283. }
  284. // step 7
  285. BN_zero(W);
  286. // now 'buf' contains "SEED + offset - 1"
  287. for (k = 0; k <= n; k++) {
  288. // obtain "SEED + offset + k" by incrementing:
  289. for (i = qsize - 1; i < qsize; i--) {
  290. buf[i]++;
  291. if (buf[i] != 0) {
  292. break;
  293. }
  294. }
  295. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
  296. goto err;
  297. }
  298. // step 8
  299. if (!BN_bin2bn(md, qsize, r0) ||
  300. !BN_lshift(r0, r0, (qsize << 3) * k) ||
  301. !BN_add(W, W, r0)) {
  302. goto err;
  303. }
  304. }
  305. // more of step 8
  306. if (!BN_mask_bits(W, bits - 1) ||
  307. !BN_copy(X, W) ||
  308. !BN_add(X, X, test)) {
  309. goto err;
  310. }
  311. // step 9
  312. if (!BN_lshift1(r0, q) ||
  313. !BN_mod(c, X, r0, ctx) ||
  314. !BN_sub(r0, c, BN_value_one()) ||
  315. !BN_sub(p, X, r0)) {
  316. goto err;
  317. }
  318. // step 10
  319. if (BN_cmp(p, test) >= 0) {
  320. // step 11
  321. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  322. if (r > 0) {
  323. goto end; // found it
  324. }
  325. if (r != 0) {
  326. goto err;
  327. }
  328. }
  329. // step 13
  330. counter++;
  331. // "offset = offset + n + 1"
  332. // step 14
  333. if (counter >= 4096) {
  334. break;
  335. }
  336. }
  337. }
  338. end:
  339. if (!BN_GENCB_call(cb, 2, 1)) {
  340. goto err;
  341. }
  342. // We now need to generate g
  343. // Set r0=(p-1)/q
  344. if (!BN_sub(test, p, BN_value_one()) ||
  345. !BN_div(r0, NULL, test, q, ctx)) {
  346. goto err;
  347. }
  348. if (!BN_set_word(test, h) ||
  349. !BN_MONT_CTX_set(mont, p, ctx)) {
  350. goto err;
  351. }
  352. for (;;) {
  353. // g=test^r0%p
  354. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
  355. goto err;
  356. }
  357. if (!BN_is_one(g)) {
  358. break;
  359. }
  360. if (!BN_add(test, test, BN_value_one())) {
  361. goto err;
  362. }
  363. h++;
  364. }
  365. if (!BN_GENCB_call(cb, 3, 1)) {
  366. goto err;
  367. }
  368. ok = 1;
  369. err:
  370. if (ok) {
  371. BN_free(dsa->p);
  372. BN_free(dsa->q);
  373. BN_free(dsa->g);
  374. dsa->p = BN_dup(p);
  375. dsa->q = BN_dup(q);
  376. dsa->g = BN_dup(g);
  377. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  378. ok = 0;
  379. goto err;
  380. }
  381. if (out_counter != NULL) {
  382. *out_counter = counter;
  383. }
  384. if (out_h != NULL) {
  385. *out_h = h;
  386. }
  387. }
  388. if (ctx) {
  389. BN_CTX_end(ctx);
  390. BN_CTX_free(ctx);
  391. }
  392. BN_MONT_CTX_free(mont);
  393. return ok;
  394. }
  395. DSA *DSAparams_dup(const DSA *dsa) {
  396. DSA *ret = DSA_new();
  397. if (ret == NULL) {
  398. return NULL;
  399. }
  400. ret->p = BN_dup(dsa->p);
  401. ret->q = BN_dup(dsa->q);
  402. ret->g = BN_dup(dsa->g);
  403. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  404. DSA_free(ret);
  405. return NULL;
  406. }
  407. return ret;
  408. }
  409. int DSA_generate_key(DSA *dsa) {
  410. int ok = 0;
  411. BN_CTX *ctx = NULL;
  412. BIGNUM *pub_key = NULL, *priv_key = NULL;
  413. ctx = BN_CTX_new();
  414. if (ctx == NULL) {
  415. goto err;
  416. }
  417. priv_key = dsa->priv_key;
  418. if (priv_key == NULL) {
  419. priv_key = BN_new();
  420. if (priv_key == NULL) {
  421. goto err;
  422. }
  423. }
  424. if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
  425. goto err;
  426. }
  427. pub_key = dsa->pub_key;
  428. if (pub_key == NULL) {
  429. pub_key = BN_new();
  430. if (pub_key == NULL) {
  431. goto err;
  432. }
  433. }
  434. if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
  435. dsa->p, ctx) ||
  436. !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
  437. dsa->method_mont_p)) {
  438. goto err;
  439. }
  440. dsa->priv_key = priv_key;
  441. dsa->pub_key = pub_key;
  442. ok = 1;
  443. err:
  444. if (dsa->pub_key == NULL) {
  445. BN_free(pub_key);
  446. }
  447. if (dsa->priv_key == NULL) {
  448. BN_free(priv_key);
  449. }
  450. BN_CTX_free(ctx);
  451. return ok;
  452. }
  453. DSA_SIG *DSA_SIG_new(void) {
  454. DSA_SIG *sig;
  455. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  456. if (!sig) {
  457. return NULL;
  458. }
  459. sig->r = NULL;
  460. sig->s = NULL;
  461. return sig;
  462. }
  463. void DSA_SIG_free(DSA_SIG *sig) {
  464. if (!sig) {
  465. return;
  466. }
  467. BN_free(sig->r);
  468. BN_free(sig->s);
  469. OPENSSL_free(sig);
  470. }
  471. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  472. BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
  473. BIGNUM m;
  474. BIGNUM xr;
  475. BN_CTX *ctx = NULL;
  476. int reason = ERR_R_BN_LIB;
  477. DSA_SIG *ret = NULL;
  478. int noredo = 0;
  479. BN_init(&m);
  480. BN_init(&xr);
  481. if (!dsa->p || !dsa->q || !dsa->g) {
  482. reason = DSA_R_MISSING_PARAMETERS;
  483. goto err;
  484. }
  485. s = BN_new();
  486. if (s == NULL) {
  487. goto err;
  488. }
  489. ctx = BN_CTX_new();
  490. if (ctx == NULL) {
  491. goto err;
  492. }
  493. redo:
  494. if (dsa->kinv == NULL || dsa->r == NULL) {
  495. if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
  496. goto err;
  497. }
  498. } else {
  499. kinv = dsa->kinv;
  500. dsa->kinv = NULL;
  501. r = dsa->r;
  502. dsa->r = NULL;
  503. noredo = 1;
  504. }
  505. if (digest_len > BN_num_bytes(dsa->q)) {
  506. // if the digest length is greater than the size of q use the
  507. // BN_num_bits(dsa->q) leftmost bits of the digest, see
  508. // fips 186-3, 4.2
  509. digest_len = BN_num_bytes(dsa->q);
  510. }
  511. if (BN_bin2bn(digest, digest_len, &m) == NULL) {
  512. goto err;
  513. }
  514. // Compute s = inv(k) (m + xr) mod q
  515. if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
  516. goto err; // s = xr
  517. }
  518. if (!BN_add(s, &xr, &m)) {
  519. goto err; // s = m + xr
  520. }
  521. if (BN_cmp(s, dsa->q) > 0) {
  522. if (!BN_sub(s, s, dsa->q)) {
  523. goto err;
  524. }
  525. }
  526. if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
  527. goto err;
  528. }
  529. // Redo if r or s is zero as required by FIPS 186-3: this is
  530. // very unlikely.
  531. if (BN_is_zero(r) || BN_is_zero(s)) {
  532. if (noredo) {
  533. reason = DSA_R_NEED_NEW_SETUP_VALUES;
  534. goto err;
  535. }
  536. goto redo;
  537. }
  538. ret = DSA_SIG_new();
  539. if (ret == NULL) {
  540. goto err;
  541. }
  542. ret->r = r;
  543. ret->s = s;
  544. err:
  545. if (ret == NULL) {
  546. OPENSSL_PUT_ERROR(DSA, reason);
  547. BN_free(r);
  548. BN_free(s);
  549. }
  550. BN_CTX_free(ctx);
  551. BN_clear_free(&m);
  552. BN_clear_free(&xr);
  553. BN_clear_free(kinv);
  554. return ret;
  555. }
  556. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  557. const DSA *dsa) {
  558. int valid;
  559. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  560. return -1;
  561. }
  562. return valid;
  563. }
  564. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  565. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  566. BN_CTX *ctx;
  567. BIGNUM u1, u2, t1;
  568. int ret = 0;
  569. unsigned i;
  570. *out_valid = 0;
  571. if (!dsa->p || !dsa->q || !dsa->g) {
  572. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  573. return 0;
  574. }
  575. i = BN_num_bits(dsa->q);
  576. // fips 186-3 allows only different sizes for q
  577. if (i != 160 && i != 224 && i != 256) {
  578. OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
  579. return 0;
  580. }
  581. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  582. OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
  583. return 0;
  584. }
  585. BN_init(&u1);
  586. BN_init(&u2);
  587. BN_init(&t1);
  588. ctx = BN_CTX_new();
  589. if (ctx == NULL) {
  590. goto err;
  591. }
  592. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  593. BN_ucmp(sig->r, dsa->q) >= 0) {
  594. ret = 1;
  595. goto err;
  596. }
  597. if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
  598. BN_ucmp(sig->s, dsa->q) >= 0) {
  599. ret = 1;
  600. goto err;
  601. }
  602. // Calculate W = inv(S) mod Q
  603. // save W in u2
  604. if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
  605. goto err;
  606. }
  607. // save M in u1
  608. if (digest_len > (i >> 3)) {
  609. // if the digest length is greater than the size of q use the
  610. // BN_num_bits(dsa->q) leftmost bits of the digest, see
  611. // fips 186-3, 4.2
  612. digest_len = (i >> 3);
  613. }
  614. if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
  615. goto err;
  616. }
  617. // u1 = M * w mod q
  618. if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
  619. goto err;
  620. }
  621. // u2 = r * w mod q
  622. if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
  623. goto err;
  624. }
  625. if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  626. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  627. ctx)) {
  628. goto err;
  629. }
  630. if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
  631. dsa->method_mont_p)) {
  632. goto err;
  633. }
  634. // BN_copy(&u1,&t1);
  635. // let u1 = u1 mod q
  636. if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
  637. goto err;
  638. }
  639. // V is now in u1. If the signature is correct, it will be
  640. // equal to R.
  641. *out_valid = BN_ucmp(&u1, sig->r) == 0;
  642. ret = 1;
  643. err:
  644. if (ret != 1) {
  645. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  646. }
  647. BN_CTX_free(ctx);
  648. BN_free(&u1);
  649. BN_free(&u2);
  650. BN_free(&t1);
  651. return ret;
  652. }
  653. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  654. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  655. DSA_SIG *s;
  656. s = DSA_do_sign(digest, digest_len, dsa);
  657. if (s == NULL) {
  658. *out_siglen = 0;
  659. return 0;
  660. }
  661. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  662. DSA_SIG_free(s);
  663. return 1;
  664. }
  665. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  666. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  667. int valid;
  668. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  669. return -1;
  670. }
  671. return valid;
  672. }
  673. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  674. size_t digest_len, const uint8_t *sig, size_t sig_len,
  675. const DSA *dsa) {
  676. DSA_SIG *s = NULL;
  677. int ret = 0;
  678. uint8_t *der = NULL;
  679. s = DSA_SIG_new();
  680. if (s == NULL) {
  681. goto err;
  682. }
  683. const uint8_t *sigp = sig;
  684. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  685. goto err;
  686. }
  687. // Ensure that the signature uses DER and doesn't have trailing garbage.
  688. int der_len = i2d_DSA_SIG(s, &der);
  689. if (der_len < 0 || (size_t)der_len != sig_len ||
  690. OPENSSL_memcmp(sig, der, sig_len)) {
  691. goto err;
  692. }
  693. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  694. err:
  695. OPENSSL_free(der);
  696. DSA_SIG_free(s);
  697. return ret;
  698. }
  699. // der_len_len returns the number of bytes needed to represent a length of |len|
  700. // in DER.
  701. static size_t der_len_len(size_t len) {
  702. if (len < 0x80) {
  703. return 1;
  704. }
  705. size_t ret = 1;
  706. while (len > 0) {
  707. ret++;
  708. len >>= 8;
  709. }
  710. return ret;
  711. }
  712. int DSA_size(const DSA *dsa) {
  713. size_t order_len = BN_num_bytes(dsa->q);
  714. // Compute the maximum length of an |order_len| byte integer. Defensively
  715. // assume that the leading 0x00 is included.
  716. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
  717. if (integer_len < order_len) {
  718. return 0;
  719. }
  720. // A DSA signature is two INTEGERs.
  721. size_t value_len = 2 * integer_len;
  722. if (value_len < integer_len) {
  723. return 0;
  724. }
  725. // Add the header.
  726. size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
  727. if (ret < value_len) {
  728. return 0;
  729. }
  730. return ret;
  731. }
  732. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
  733. BIGNUM **out_r) {
  734. BN_CTX *ctx;
  735. BIGNUM k, kq, *kinv = NULL, *r = NULL;
  736. int ret = 0;
  737. if (!dsa->p || !dsa->q || !dsa->g) {
  738. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  739. return 0;
  740. }
  741. BN_init(&k);
  742. BN_init(&kq);
  743. ctx = ctx_in;
  744. if (ctx == NULL) {
  745. ctx = BN_CTX_new();
  746. if (ctx == NULL) {
  747. goto err;
  748. }
  749. }
  750. r = BN_new();
  751. if (r == NULL) {
  752. goto err;
  753. }
  754. // Get random k
  755. if (!BN_rand_range_ex(&k, 1, dsa->q)) {
  756. goto err;
  757. }
  758. if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  759. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  760. ctx) ||
  761. !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
  762. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
  763. ctx)) {
  764. goto err;
  765. }
  766. // Compute r = (g^k mod p) mod q
  767. if (!BN_copy(&kq, &k)) {
  768. goto err;
  769. }
  770. // We do not want timing information to leak the length of k,
  771. // so we compute g^k using an equivalent exponent of fixed length.
  772. //
  773. // (This is a kludge that we need because the BN_mod_exp_mont()
  774. // does not let us specify the desired timing behaviour.)
  775. if (!BN_add(&kq, &kq, dsa->q)) {
  776. goto err;
  777. }
  778. if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
  779. goto err;
  780. }
  781. if (!BN_mod_exp_mont_consttime(r, dsa->g, &kq, dsa->p, ctx,
  782. dsa->method_mont_p)) {
  783. goto err;
  784. }
  785. if (!BN_mod(r, r, dsa->q, ctx)) {
  786. goto err;
  787. }
  788. // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
  789. // Theorem.
  790. kinv = BN_new();
  791. if (kinv == NULL ||
  792. !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
  793. goto err;
  794. }
  795. BN_clear_free(*out_kinv);
  796. *out_kinv = kinv;
  797. kinv = NULL;
  798. BN_clear_free(*out_r);
  799. *out_r = r;
  800. ret = 1;
  801. err:
  802. if (!ret) {
  803. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  804. if (r != NULL) {
  805. BN_clear_free(r);
  806. }
  807. }
  808. if (ctx_in == NULL) {
  809. BN_CTX_free(ctx);
  810. }
  811. BN_clear_free(&k);
  812. BN_clear_free(&kq);
  813. BN_clear_free(kinv);
  814. return ret;
  815. }
  816. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  817. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  818. int index;
  819. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  820. free_func)) {
  821. return -1;
  822. }
  823. return index;
  824. }
  825. int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
  826. return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
  827. }
  828. void *DSA_get_ex_data(const DSA *dsa, int idx) {
  829. return CRYPTO_get_ex_data(&dsa->ex_data, idx);
  830. }
  831. DH *DSA_dup_DH(const DSA *dsa) {
  832. if (dsa == NULL) {
  833. return NULL;
  834. }
  835. DH *ret = DH_new();
  836. if (ret == NULL) {
  837. goto err;
  838. }
  839. if (dsa->q != NULL) {
  840. ret->priv_length = BN_num_bits(dsa->q);
  841. if ((ret->q = BN_dup(dsa->q)) == NULL) {
  842. goto err;
  843. }
  844. }
  845. if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
  846. (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
  847. (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
  848. (dsa->priv_key != NULL &&
  849. (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {
  850. goto err;
  851. }
  852. return ret;
  853. err:
  854. DH_free(ret);
  855. return NULL;
  856. }