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.
 
 
 
 
 
 

768 lines
17 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/digest.h>
  63. #include <openssl/err.h>
  64. #include <openssl/rand.h>
  65. #include <openssl/sha.h>
  66. #include <openssl/thread.h>
  67. #include "internal.h"
  68. #define OPENSSL_DSA_MAX_MODULUS_BITS 10000
  69. /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
  70. * Rabin-Miller */
  71. #define DSS_prime_checks 50
  72. static int sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
  73. BIGNUM **rp, const uint8_t *digest, size_t digest_len) {
  74. BN_CTX *ctx;
  75. BIGNUM k, kq, *K, *kinv = NULL, *r = NULL;
  76. int ret = 0;
  77. if (!dsa->p || !dsa->q || !dsa->g) {
  78. OPENSSL_PUT_ERROR(DSA, sign_setup, DSA_R_MISSING_PARAMETERS);
  79. return 0;
  80. }
  81. BN_init(&k);
  82. BN_init(&kq);
  83. ctx = ctx_in;
  84. if (ctx == NULL) {
  85. ctx = BN_CTX_new();
  86. if (ctx == NULL) {
  87. goto err;
  88. }
  89. }
  90. r = BN_new();
  91. if (r == NULL) {
  92. goto err;
  93. }
  94. /* Get random k */
  95. do {
  96. /* If possible, we'll include the private key and message digest in the k
  97. * generation. The |digest| argument is only empty if |DSA_sign_setup| is
  98. * being used. */
  99. int ok;
  100. if (digest_len > 0) {
  101. ok = BN_generate_dsa_nonce(&k, dsa->q, dsa->priv_key, digest, digest_len,
  102. ctx);
  103. } else {
  104. ok = BN_rand_range(&k, dsa->q);
  105. }
  106. if (!ok) {
  107. goto err;
  108. }
  109. } while (BN_is_zero(&k));
  110. BN_set_flags(&k, BN_FLG_CONSTTIME);
  111. if (BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  112. (CRYPTO_MUTEX *)&dsa->method_mont_p_lock, dsa->p,
  113. ctx) == NULL) {
  114. goto err;
  115. }
  116. /* Compute r = (g^k mod p) mod q */
  117. if (!BN_copy(&kq, &k)) {
  118. goto err;
  119. }
  120. /* We do not want timing information to leak the length of k,
  121. * so we compute g^k using an equivalent exponent of fixed length.
  122. *
  123. * (This is a kludge that we need because the BN_mod_exp_mont()
  124. * does not let us specify the desired timing behaviour.) */
  125. if (!BN_add(&kq, &kq, dsa->q)) {
  126. goto err;
  127. }
  128. if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
  129. goto err;
  130. }
  131. K = &kq;
  132. if (!BN_mod_exp_mont(r, dsa->g, K, dsa->p, ctx, dsa->method_mont_p)) {
  133. goto err;
  134. }
  135. if (!BN_mod(r, r, dsa->q, ctx)) {
  136. goto err;
  137. }
  138. /* Compute part of 's = inv(k) (m + xr) mod q' */
  139. kinv = BN_mod_inverse(NULL, &k, dsa->q, ctx);
  140. if (kinv == NULL) {
  141. goto err;
  142. }
  143. if (*kinvp != NULL) {
  144. BN_clear_free(*kinvp);
  145. }
  146. *kinvp = kinv;
  147. kinv = NULL;
  148. if (*rp != NULL) {
  149. BN_clear_free(*rp);
  150. }
  151. *rp = r;
  152. ret = 1;
  153. err:
  154. if (!ret) {
  155. OPENSSL_PUT_ERROR(DSA, sign_setup, ERR_R_BN_LIB);
  156. if (r != NULL) {
  157. BN_clear_free(r);
  158. }
  159. }
  160. if (ctx_in == NULL) {
  161. BN_CTX_free(ctx);
  162. }
  163. BN_clear_free(&k);
  164. BN_clear_free(&kq);
  165. return ret;
  166. }
  167. static DSA_SIG *sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  168. BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
  169. BIGNUM m;
  170. BIGNUM xr;
  171. BN_CTX *ctx = NULL;
  172. int reason = ERR_R_BN_LIB;
  173. DSA_SIG *ret = NULL;
  174. int noredo = 0;
  175. BN_init(&m);
  176. BN_init(&xr);
  177. if (!dsa->p || !dsa->q || !dsa->g) {
  178. reason = DSA_R_MISSING_PARAMETERS;
  179. goto err;
  180. }
  181. s = BN_new();
  182. if (s == NULL) {
  183. goto err;
  184. }
  185. ctx = BN_CTX_new();
  186. if (ctx == NULL) {
  187. goto err;
  188. }
  189. redo:
  190. if (dsa->kinv == NULL || dsa->r == NULL) {
  191. if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
  192. goto err;
  193. }
  194. } else {
  195. kinv = dsa->kinv;
  196. dsa->kinv = NULL;
  197. r = dsa->r;
  198. dsa->r = NULL;
  199. noredo = 1;
  200. }
  201. if (digest_len > BN_num_bytes(dsa->q)) {
  202. /* if the digest length is greater than the size of q use the
  203. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  204. * fips 186-3, 4.2 */
  205. digest_len = BN_num_bytes(dsa->q);
  206. }
  207. if (BN_bin2bn(digest, digest_len, &m) == NULL) {
  208. goto err;
  209. }
  210. /* Compute s = inv(k) (m + xr) mod q */
  211. if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
  212. goto err; /* s = xr */
  213. }
  214. if (!BN_add(s, &xr, &m)) {
  215. goto err; /* s = m + xr */
  216. }
  217. if (BN_cmp(s, dsa->q) > 0) {
  218. if (!BN_sub(s, s, dsa->q)) {
  219. goto err;
  220. }
  221. }
  222. if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
  223. goto err;
  224. }
  225. ret = DSA_SIG_new();
  226. if (ret == NULL) {
  227. goto err;
  228. }
  229. /* Redo if r or s is zero as required by FIPS 186-3: this is
  230. * very unlikely. */
  231. if (BN_is_zero(r) || BN_is_zero(s)) {
  232. if (noredo) {
  233. reason = DSA_R_NEED_NEW_SETUP_VALUES;
  234. goto err;
  235. }
  236. goto redo;
  237. }
  238. ret->r = r;
  239. ret->s = s;
  240. err:
  241. if (!ret) {
  242. OPENSSL_PUT_ERROR(DSA, sign, reason);
  243. BN_free(r);
  244. BN_free(s);
  245. }
  246. if (ctx != NULL) {
  247. BN_CTX_free(ctx);
  248. }
  249. BN_clear_free(&m);
  250. BN_clear_free(&xr);
  251. if (kinv != NULL) {
  252. /* dsa->kinv is NULL now if we used it */
  253. BN_clear_free(kinv);
  254. }
  255. return ret;
  256. }
  257. static int verify(int *out_valid, const uint8_t *dgst, size_t digest_len,
  258. DSA_SIG *sig, const DSA *dsa) {
  259. BN_CTX *ctx;
  260. BIGNUM u1, u2, t1;
  261. BN_MONT_CTX *mont = NULL;
  262. int ret = 0;
  263. unsigned i;
  264. *out_valid = 0;
  265. if (!dsa->p || !dsa->q || !dsa->g) {
  266. OPENSSL_PUT_ERROR(DSA, verify, DSA_R_MISSING_PARAMETERS);
  267. return 0;
  268. }
  269. i = BN_num_bits(dsa->q);
  270. /* fips 186-3 allows only different sizes for q */
  271. if (i != 160 && i != 224 && i != 256) {
  272. OPENSSL_PUT_ERROR(DSA, verify, DSA_R_BAD_Q_VALUE);
  273. return 0;
  274. }
  275. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  276. OPENSSL_PUT_ERROR(DSA, verify, DSA_R_MODULUS_TOO_LARGE);
  277. return 0;
  278. }
  279. BN_init(&u1);
  280. BN_init(&u2);
  281. BN_init(&t1);
  282. ctx = BN_CTX_new();
  283. if (ctx == NULL) {
  284. goto err;
  285. }
  286. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  287. BN_ucmp(sig->r, dsa->q) >= 0) {
  288. ret = 1;
  289. goto err;
  290. }
  291. if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
  292. BN_ucmp(sig->s, dsa->q) >= 0) {
  293. ret = 1;
  294. goto err;
  295. }
  296. /* Calculate W = inv(S) mod Q
  297. * save W in u2 */
  298. if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
  299. goto err;
  300. }
  301. /* save M in u1 */
  302. if (digest_len > (i >> 3)) {
  303. /* if the digest length is greater than the size of q use the
  304. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  305. * fips 186-3, 4.2 */
  306. digest_len = (i >> 3);
  307. }
  308. if (BN_bin2bn(dgst, digest_len, &u1) == NULL) {
  309. goto err;
  310. }
  311. /* u1 = M * w mod q */
  312. if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
  313. goto err;
  314. }
  315. /* u2 = r * w mod q */
  316. if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
  317. goto err;
  318. }
  319. mont = BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  320. (CRYPTO_MUTEX *)&dsa->method_mont_p_lock,
  321. dsa->p, ctx);
  322. if (!mont) {
  323. goto err;
  324. }
  325. if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
  326. mont)) {
  327. goto err;
  328. }
  329. /* BN_copy(&u1,&t1); */
  330. /* let u1 = u1 mod q */
  331. if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
  332. goto err;
  333. }
  334. /* V is now in u1. If the signature is correct, it will be
  335. * equal to R. */
  336. *out_valid = BN_ucmp(&u1, sig->r) == 0;
  337. ret = 1;
  338. err:
  339. if (ret != 1) {
  340. OPENSSL_PUT_ERROR(DSA, verify, ERR_R_BN_LIB);
  341. }
  342. if (ctx != NULL) {
  343. BN_CTX_free(ctx);
  344. }
  345. BN_free(&u1);
  346. BN_free(&u2);
  347. BN_free(&t1);
  348. return ret;
  349. }
  350. static int keygen(DSA *dsa) {
  351. int ok = 0;
  352. BN_CTX *ctx = NULL;
  353. BIGNUM *pub_key = NULL, *priv_key = NULL;
  354. BIGNUM prk;
  355. ctx = BN_CTX_new();
  356. if (ctx == NULL) {
  357. goto err;
  358. }
  359. priv_key = dsa->priv_key;
  360. if (priv_key == NULL) {
  361. priv_key = BN_new();
  362. if (priv_key == NULL) {
  363. goto err;
  364. }
  365. }
  366. do {
  367. if (!BN_rand_range(priv_key, dsa->q)) {
  368. goto err;
  369. }
  370. } while (BN_is_zero(priv_key));
  371. pub_key = dsa->pub_key;
  372. if (pub_key == NULL) {
  373. pub_key = BN_new();
  374. if (pub_key == NULL) {
  375. goto err;
  376. }
  377. }
  378. BN_init(&prk);
  379. BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME);
  380. if (!BN_mod_exp(pub_key, dsa->g, &prk, dsa->p, ctx)) {
  381. goto err;
  382. }
  383. dsa->priv_key = priv_key;
  384. dsa->pub_key = pub_key;
  385. ok = 1;
  386. err:
  387. if (pub_key != NULL && dsa->pub_key == NULL) {
  388. BN_free(pub_key);
  389. }
  390. if (priv_key != NULL && dsa->priv_key == NULL) {
  391. BN_free(priv_key);
  392. }
  393. if (ctx != NULL) {
  394. BN_CTX_free(ctx);
  395. }
  396. return ok;
  397. }
  398. static int paramgen(DSA *ret, unsigned bits, const uint8_t *seed_in,
  399. size_t seed_len, int *counter_ret, unsigned long *h_ret,
  400. BN_GENCB *cb) {
  401. int ok = 0;
  402. unsigned char seed[SHA256_DIGEST_LENGTH];
  403. unsigned char md[SHA256_DIGEST_LENGTH];
  404. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  405. BIGNUM *r0, *W, *X, *c, *test;
  406. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  407. BN_MONT_CTX *mont = NULL;
  408. int k, n = 0, m = 0;
  409. unsigned i;
  410. int counter = 0;
  411. int r = 0;
  412. BN_CTX *ctx = NULL;
  413. unsigned int h = 2;
  414. unsigned qbits, qsize;
  415. const EVP_MD *evpmd;
  416. if (bits >= 2048) {
  417. qbits = 256;
  418. evpmd = EVP_sha256();
  419. } else {
  420. qbits = 160;
  421. evpmd = EVP_sha1();
  422. }
  423. qsize = qbits / 8;
  424. if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
  425. qsize != SHA256_DIGEST_LENGTH) {
  426. /* invalid q size */
  427. return 0;
  428. }
  429. if (bits < 512) {
  430. bits = 512;
  431. }
  432. bits = (bits + 63) / 64 * 64;
  433. /* NB: seed_len == 0 is special case: copy generated seed to
  434. * seed_in if it is not NULL. */
  435. if (seed_len && (seed_len < (size_t)qsize)) {
  436. seed_in = NULL; /* seed buffer too small -- ignore */
  437. }
  438. if (seed_len > (size_t)qsize) {
  439. seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger SEED,
  440. * but our internal buffers are restricted to 160 bits*/
  441. }
  442. if (seed_in != NULL) {
  443. memcpy(seed, seed_in, seed_len);
  444. }
  445. ctx = BN_CTX_new();
  446. mont = BN_MONT_CTX_new();
  447. if (ctx == NULL || mont == NULL) {
  448. goto err;
  449. }
  450. BN_CTX_start(ctx);
  451. r0 = BN_CTX_get(ctx);
  452. g = BN_CTX_get(ctx);
  453. W = BN_CTX_get(ctx);
  454. q = BN_CTX_get(ctx);
  455. X = BN_CTX_get(ctx);
  456. c = BN_CTX_get(ctx);
  457. p = BN_CTX_get(ctx);
  458. test = BN_CTX_get(ctx);
  459. if (!BN_lshift(test, BN_value_one(), bits - 1)) {
  460. goto err;
  461. }
  462. for (;;) {
  463. /* Find q. */
  464. for (;;) {
  465. int seed_is_random;
  466. /* step 1 */
  467. if (!BN_GENCB_call(cb, 0, m++)) {
  468. goto err;
  469. }
  470. if (!seed_len) {
  471. if (!RAND_bytes(seed, qsize)) {
  472. goto err;
  473. }
  474. seed_is_random = 1;
  475. } else {
  476. seed_is_random = 0;
  477. seed_len = 0; /* use random seed if 'seed_in' turns out to be bad*/
  478. }
  479. memcpy(buf, seed, qsize);
  480. memcpy(buf2, seed, qsize);
  481. /* precompute "SEED + 1" for step 7: */
  482. for (i = qsize - 1; i < qsize; i--) {
  483. buf[i]++;
  484. if (buf[i] != 0) {
  485. break;
  486. }
  487. }
  488. /* step 2 */
  489. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
  490. !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
  491. goto err;
  492. }
  493. for (i = 0; i < qsize; i++) {
  494. md[i] ^= buf2[i];
  495. }
  496. /* step 3 */
  497. md[0] |= 0x80;
  498. md[qsize - 1] |= 0x01;
  499. if (!BN_bin2bn(md, qsize, q)) {
  500. goto err;
  501. }
  502. /* step 4 */
  503. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, seed_is_random, cb);
  504. if (r > 0) {
  505. break;
  506. }
  507. if (r != 0) {
  508. goto err;
  509. }
  510. /* do a callback call */
  511. /* step 5 */
  512. }
  513. if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
  514. goto err;
  515. }
  516. /* step 6 */
  517. counter = 0;
  518. /* "offset = 2" */
  519. n = (bits - 1) / 160;
  520. for (;;) {
  521. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
  522. goto err;
  523. }
  524. /* step 7 */
  525. BN_zero(W);
  526. /* now 'buf' contains "SEED + offset - 1" */
  527. for (k = 0; k <= n; k++) {
  528. /* obtain "SEED + offset + k" by incrementing: */
  529. for (i = qsize - 1; i < qsize; i--) {
  530. buf[i]++;
  531. if (buf[i] != 0) {
  532. break;
  533. }
  534. }
  535. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
  536. goto err;
  537. }
  538. /* step 8 */
  539. if (!BN_bin2bn(md, qsize, r0) ||
  540. !BN_lshift(r0, r0, (qsize << 3) * k) ||
  541. !BN_add(W, W, r0)) {
  542. goto err;
  543. }
  544. }
  545. /* more of step 8 */
  546. if (!BN_mask_bits(W, bits - 1) ||
  547. !BN_copy(X, W) ||
  548. !BN_add(X, X, test)) {
  549. goto err;
  550. }
  551. /* step 9 */
  552. if (!BN_lshift1(r0, q) ||
  553. !BN_mod(c, X, r0, ctx) ||
  554. !BN_sub(r0, c, BN_value_one()) ||
  555. !BN_sub(p, X, r0)) {
  556. goto err;
  557. }
  558. /* step 10 */
  559. if (BN_cmp(p, test) >= 0) {
  560. /* step 11 */
  561. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  562. if (r > 0) {
  563. goto end; /* found it */
  564. }
  565. if (r != 0) {
  566. goto err;
  567. }
  568. }
  569. /* step 13 */
  570. counter++;
  571. /* "offset = offset + n + 1" */
  572. /* step 14 */
  573. if (counter >= 4096) {
  574. break;
  575. }
  576. }
  577. }
  578. end:
  579. if (!BN_GENCB_call(cb, 2, 1)) {
  580. goto err;
  581. }
  582. /* We now need to generate g */
  583. /* Set r0=(p-1)/q */
  584. if (!BN_sub(test, p, BN_value_one()) ||
  585. !BN_div(r0, NULL, test, q, ctx)) {
  586. goto err;
  587. }
  588. if (!BN_set_word(test, h) ||
  589. !BN_MONT_CTX_set(mont, p, ctx)) {
  590. goto err;
  591. }
  592. for (;;) {
  593. /* g=test^r0%p */
  594. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
  595. goto err;
  596. }
  597. if (!BN_is_one(g)) {
  598. break;
  599. }
  600. if (!BN_add(test, test, BN_value_one())) {
  601. goto err;
  602. }
  603. h++;
  604. }
  605. if (!BN_GENCB_call(cb, 3, 1)) {
  606. goto err;
  607. }
  608. ok = 1;
  609. err:
  610. if (ok) {
  611. if (ret->p) {
  612. BN_free(ret->p);
  613. }
  614. if (ret->q) {
  615. BN_free(ret->q);
  616. }
  617. if (ret->g) {
  618. BN_free(ret->g);
  619. }
  620. ret->p = BN_dup(p);
  621. ret->q = BN_dup(q);
  622. ret->g = BN_dup(g);
  623. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  624. ok = 0;
  625. goto err;
  626. }
  627. if (counter_ret != NULL) {
  628. *counter_ret = counter;
  629. }
  630. if (h_ret != NULL) {
  631. *h_ret = h;
  632. }
  633. }
  634. if (ctx) {
  635. BN_CTX_end(ctx);
  636. BN_CTX_free(ctx);
  637. }
  638. if (mont != NULL) {
  639. BN_MONT_CTX_free(mont);
  640. }
  641. return ok;
  642. }
  643. static int finish(DSA *dsa) {
  644. BN_MONT_CTX_free(dsa->method_mont_p);
  645. dsa->method_mont_p = NULL;
  646. return 1;
  647. }
  648. const struct dsa_method DSA_default_method = {
  649. {
  650. 0 /* references */,
  651. 1 /* is_static */,
  652. },
  653. NULL /* app_data */,
  654. NULL /* init */,
  655. finish /* finish */,
  656. sign,
  657. sign_setup,
  658. verify,
  659. paramgen,
  660. keygen,
  661. };