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.
 
 
 
 
 
 

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