Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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