dsa_gen.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* crypto/dsa/dsa_gen.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #undef GENUINE_DSA
  59. #ifdef GENUINE_DSA
  60. /*
  61. * Parameter generation follows the original release of FIPS PUB 186,
  62. * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180)
  63. */
  64. # define HASH EVP_sha()
  65. #else
  66. /*
  67. * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
  68. * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
  69. * 180-1)
  70. */
  71. # define HASH EVP_sha1()
  72. #endif
  73. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
  74. #ifndef OPENSSL_NO_SHA
  75. # include <stdio.h>
  76. # include "cryptlib.h"
  77. # include <openssl/evp.h>
  78. # include <openssl/bn.h>
  79. # include <openssl/rand.h>
  80. # include <openssl/sha.h>
  81. # include "dsa_locl.h"
  82. # ifdef OPENSSL_FIPS
  83. /* Workaround bug in prototype */
  84. # define fips_dsa_builtin_paramgen2 fips_dsa_paramgen_bad
  85. # include <openssl/fips.h>
  86. # endif
  87. int DSA_generate_parameters_ex(DSA *ret, int bits,
  88. const unsigned char *seed_in, int seed_len,
  89. int *counter_ret, unsigned long *h_ret,
  90. BN_GENCB *cb)
  91. {
  92. # ifdef OPENSSL_FIPS
  93. if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD)
  94. && !(ret->flags & DSA_FLAG_NON_FIPS_ALLOW)) {
  95. DSAerr(DSA_F_DSA_GENERATE_PARAMETERS_EX, DSA_R_NON_FIPS_DSA_METHOD);
  96. return 0;
  97. }
  98. # endif
  99. if (ret->meth->dsa_paramgen)
  100. return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
  101. counter_ret, h_ret, cb);
  102. # ifdef OPENSSL_FIPS
  103. else if (FIPS_mode()) {
  104. return FIPS_dsa_generate_parameters_ex(ret, bits,
  105. seed_in, seed_len,
  106. counter_ret, h_ret, cb);
  107. }
  108. # endif
  109. else {
  110. const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
  111. size_t qbits = EVP_MD_size(evpmd) * 8;
  112. return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
  113. seed_in, seed_len, NULL, counter_ret,
  114. h_ret, cb);
  115. }
  116. }
  117. int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
  118. const EVP_MD *evpmd, const unsigned char *seed_in,
  119. size_t seed_len, unsigned char *seed_out,
  120. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
  121. {
  122. int ok = 0;
  123. unsigned char seed[SHA256_DIGEST_LENGTH];
  124. unsigned char md[SHA256_DIGEST_LENGTH];
  125. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  126. BIGNUM *r0, *W, *X, *c, *test;
  127. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  128. BN_MONT_CTX *mont = NULL;
  129. int i, k, n = 0, m = 0, qsize = qbits >> 3;
  130. int counter = 0;
  131. int r = 0;
  132. BN_CTX *ctx = NULL;
  133. unsigned int h = 2;
  134. if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
  135. qsize != SHA256_DIGEST_LENGTH)
  136. /* invalid q size */
  137. return 0;
  138. if (evpmd == NULL) {
  139. if (qsize == SHA_DIGEST_LENGTH)
  140. evpmd = EVP_sha1();
  141. else if (qsize == SHA224_DIGEST_LENGTH)
  142. evpmd = EVP_sha224();
  143. else
  144. evpmd = EVP_sha256();
  145. } else {
  146. qsize = EVP_MD_size(evpmd);
  147. }
  148. if (bits < 512)
  149. bits = 512;
  150. bits = (bits + 63) / 64 * 64;
  151. /*
  152. * NB: seed_len == 0 is special case: copy generated seed to seed_in if
  153. * it is not NULL.
  154. */
  155. if (seed_len && (seed_len < (size_t)qsize))
  156. seed_in = NULL; /* seed buffer too small -- ignore */
  157. if (seed_len > (size_t)qsize)
  158. seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger
  159. * SEED, but our internal buffers are
  160. * restricted to 160 bits */
  161. if (seed_in != NULL)
  162. memcpy(seed, seed_in, seed_len);
  163. if ((mont = BN_MONT_CTX_new()) == NULL)
  164. goto err;
  165. if ((ctx = BN_CTX_new()) == NULL)
  166. goto err;
  167. BN_CTX_start(ctx);
  168. r0 = BN_CTX_get(ctx);
  169. g = BN_CTX_get(ctx);
  170. W = BN_CTX_get(ctx);
  171. q = BN_CTX_get(ctx);
  172. X = BN_CTX_get(ctx);
  173. c = BN_CTX_get(ctx);
  174. p = BN_CTX_get(ctx);
  175. test = BN_CTX_get(ctx);
  176. if (test == NULL)
  177. goto err;
  178. if (!BN_lshift(test, BN_value_one(), bits - 1))
  179. goto err;
  180. for (;;) {
  181. for (;;) { /* find q */
  182. int seed_is_random;
  183. /* step 1 */
  184. if (!BN_GENCB_call(cb, 0, m++))
  185. goto err;
  186. if (!seed_len || !seed_in) {
  187. if (RAND_bytes(seed, qsize) <= 0)
  188. goto err;
  189. seed_is_random = 1;
  190. } else {
  191. seed_is_random = 0;
  192. seed_len = 0; /* use random seed if 'seed_in' turns out to
  193. * be bad */
  194. }
  195. memcpy(buf, seed, qsize);
  196. memcpy(buf2, seed, qsize);
  197. /* precompute "SEED + 1" for step 7: */
  198. for (i = qsize - 1; i >= 0; i--) {
  199. buf[i]++;
  200. if (buf[i] != 0)
  201. break;
  202. }
  203. /* step 2 */
  204. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
  205. goto err;
  206. if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
  207. goto err;
  208. for (i = 0; i < qsize; i++)
  209. md[i] ^= buf2[i];
  210. /* step 3 */
  211. md[0] |= 0x80;
  212. md[qsize - 1] |= 0x01;
  213. if (!BN_bin2bn(md, qsize, q))
  214. goto err;
  215. /* step 4 */
  216. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  217. seed_is_random, cb);
  218. if (r > 0)
  219. break;
  220. if (r != 0)
  221. goto err;
  222. /* do a callback call */
  223. /* step 5 */
  224. }
  225. if (!BN_GENCB_call(cb, 2, 0))
  226. goto err;
  227. if (!BN_GENCB_call(cb, 3, 0))
  228. goto err;
  229. /* step 6 */
  230. counter = 0;
  231. /* "offset = 2" */
  232. n = (bits - 1) / 160;
  233. for (;;) {
  234. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  235. goto err;
  236. /* step 7 */
  237. BN_zero(W);
  238. /* now 'buf' contains "SEED + offset - 1" */
  239. for (k = 0; k <= n; k++) {
  240. /*
  241. * obtain "SEED + offset + k" by incrementing:
  242. */
  243. for (i = qsize - 1; i >= 0; i--) {
  244. buf[i]++;
  245. if (buf[i] != 0)
  246. break;
  247. }
  248. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
  249. goto err;
  250. /* step 8 */
  251. if (!BN_bin2bn(md, qsize, r0))
  252. goto err;
  253. if (!BN_lshift(r0, r0, (qsize << 3) * k))
  254. goto err;
  255. if (!BN_add(W, W, r0))
  256. goto err;
  257. }
  258. /* more of step 8 */
  259. if (!BN_mask_bits(W, bits - 1))
  260. goto err;
  261. if (!BN_copy(X, W))
  262. goto err;
  263. if (!BN_add(X, X, test))
  264. goto err;
  265. /* step 9 */
  266. if (!BN_lshift1(r0, q))
  267. goto err;
  268. if (!BN_mod(c, X, r0, ctx))
  269. goto err;
  270. if (!BN_sub(r0, c, BN_value_one()))
  271. goto err;
  272. if (!BN_sub(p, X, r0))
  273. goto err;
  274. /* step 10 */
  275. if (BN_cmp(p, test) >= 0) {
  276. /* step 11 */
  277. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  278. if (r > 0)
  279. goto end; /* found it */
  280. if (r != 0)
  281. goto err;
  282. }
  283. /* step 13 */
  284. counter++;
  285. /* "offset = offset + n + 1" */
  286. /* step 14 */
  287. if (counter >= 4096)
  288. break;
  289. }
  290. }
  291. end:
  292. if (!BN_GENCB_call(cb, 2, 1))
  293. goto err;
  294. /* We now need to generate g */
  295. /* Set r0=(p-1)/q */
  296. if (!BN_sub(test, p, BN_value_one()))
  297. goto err;
  298. if (!BN_div(r0, NULL, test, q, ctx))
  299. goto err;
  300. if (!BN_set_word(test, h))
  301. goto err;
  302. if (!BN_MONT_CTX_set(mont, p, ctx))
  303. goto err;
  304. for (;;) {
  305. /* g=test^r0%p */
  306. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  307. goto err;
  308. if (!BN_is_one(g))
  309. break;
  310. if (!BN_add(test, test, BN_value_one()))
  311. goto err;
  312. h++;
  313. }
  314. if (!BN_GENCB_call(cb, 3, 1))
  315. goto err;
  316. ok = 1;
  317. err:
  318. if (ok) {
  319. if (ret->p)
  320. BN_free(ret->p);
  321. if (ret->q)
  322. BN_free(ret->q);
  323. if (ret->g)
  324. BN_free(ret->g);
  325. ret->p = BN_dup(p);
  326. ret->q = BN_dup(q);
  327. ret->g = BN_dup(g);
  328. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  329. ok = 0;
  330. goto err;
  331. }
  332. if (counter_ret != NULL)
  333. *counter_ret = counter;
  334. if (h_ret != NULL)
  335. *h_ret = h;
  336. if (seed_out)
  337. memcpy(seed_out, seed, qsize);
  338. }
  339. if (ctx) {
  340. BN_CTX_end(ctx);
  341. BN_CTX_free(ctx);
  342. }
  343. if (mont != NULL)
  344. BN_MONT_CTX_free(mont);
  345. return ok;
  346. }
  347. # ifdef OPENSSL_FIPS
  348. # undef fips_dsa_builtin_paramgen2
  349. extern int fips_dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  350. const EVP_MD *evpmd,
  351. const unsigned char *seed_in,
  352. size_t seed_len, int idx,
  353. unsigned char *seed_out,
  354. int *counter_ret, unsigned long *h_ret,
  355. BN_GENCB *cb);
  356. # endif
  357. /*
  358. * This is a parameter generation algorithm for the DSA2 algorithm as
  359. * described in FIPS 186-3.
  360. */
  361. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  362. const EVP_MD *evpmd, const unsigned char *seed_in,
  363. size_t seed_len, int idx, unsigned char *seed_out,
  364. int *counter_ret, unsigned long *h_ret,
  365. BN_GENCB *cb)
  366. {
  367. int ok = -1;
  368. unsigned char *seed = NULL, *seed_tmp = NULL;
  369. unsigned char md[EVP_MAX_MD_SIZE];
  370. int mdsize;
  371. BIGNUM *r0, *W, *X, *c, *test;
  372. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  373. BN_MONT_CTX *mont = NULL;
  374. int i, k, n = 0, m = 0, qsize = N >> 3;
  375. int counter = 0;
  376. int r = 0;
  377. BN_CTX *ctx = NULL;
  378. EVP_MD_CTX mctx;
  379. unsigned int h = 2;
  380. # ifdef OPENSSL_FIPS
  381. if (FIPS_mode())
  382. return fips_dsa_builtin_paramgen2(ret, L, N, evpmd,
  383. seed_in, seed_len, idx,
  384. seed_out, counter_ret, h_ret, cb);
  385. # endif
  386. EVP_MD_CTX_init(&mctx);
  387. /* make sure L > N, otherwise we'll get trapped in an infinite loop */
  388. if (L <= N) {
  389. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  390. goto err;
  391. }
  392. if (evpmd == NULL) {
  393. if (N == 160)
  394. evpmd = EVP_sha1();
  395. else if (N == 224)
  396. evpmd = EVP_sha224();
  397. else
  398. evpmd = EVP_sha256();
  399. }
  400. mdsize = EVP_MD_size(evpmd);
  401. /* If unverificable g generation only don't need seed */
  402. if (!ret->p || !ret->q || idx >= 0) {
  403. if (seed_len == 0)
  404. seed_len = mdsize;
  405. seed = OPENSSL_malloc(seed_len);
  406. if (seed_out)
  407. seed_tmp = seed_out;
  408. else
  409. seed_tmp = OPENSSL_malloc(seed_len);
  410. if (!seed || !seed_tmp)
  411. goto err;
  412. if (seed_in)
  413. memcpy(seed, seed_in, seed_len);
  414. }
  415. if ((ctx = BN_CTX_new()) == NULL)
  416. goto err;
  417. if ((mont = BN_MONT_CTX_new()) == NULL)
  418. goto err;
  419. BN_CTX_start(ctx);
  420. r0 = BN_CTX_get(ctx);
  421. g = BN_CTX_get(ctx);
  422. W = BN_CTX_get(ctx);
  423. X = BN_CTX_get(ctx);
  424. c = BN_CTX_get(ctx);
  425. test = BN_CTX_get(ctx);
  426. /* if p, q already supplied generate g only */
  427. if (ret->p && ret->q) {
  428. p = ret->p;
  429. q = ret->q;
  430. if (idx >= 0)
  431. memcpy(seed_tmp, seed, seed_len);
  432. goto g_only;
  433. } else {
  434. p = BN_CTX_get(ctx);
  435. q = BN_CTX_get(ctx);
  436. if (q == NULL)
  437. goto err;
  438. }
  439. if (!BN_lshift(test, BN_value_one(), L - 1))
  440. goto err;
  441. for (;;) {
  442. for (;;) { /* find q */
  443. unsigned char *pmd;
  444. /* step 1 */
  445. if (!BN_GENCB_call(cb, 0, m++))
  446. goto err;
  447. if (!seed_in) {
  448. if (RAND_bytes(seed, seed_len) <= 0)
  449. goto err;
  450. }
  451. /* step 2 */
  452. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  453. goto err;
  454. /* Take least significant bits of md */
  455. if (mdsize > qsize)
  456. pmd = md + mdsize - qsize;
  457. else
  458. pmd = md;
  459. if (mdsize < qsize)
  460. memset(md + mdsize, 0, qsize - mdsize);
  461. /* step 3 */
  462. pmd[0] |= 0x80;
  463. pmd[qsize - 1] |= 0x01;
  464. if (!BN_bin2bn(pmd, qsize, q))
  465. goto err;
  466. /* step 4 */
  467. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  468. seed_in ? 1 : 0, cb);
  469. if (r > 0)
  470. break;
  471. if (r != 0)
  472. goto err;
  473. /* Provided seed didn't produce a prime: error */
  474. if (seed_in) {
  475. ok = 0;
  476. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  477. goto err;
  478. }
  479. /* do a callback call */
  480. /* step 5 */
  481. }
  482. /* Copy seed to seed_out before we mess with it */
  483. if (seed_out)
  484. memcpy(seed_out, seed, seed_len);
  485. if (!BN_GENCB_call(cb, 2, 0))
  486. goto err;
  487. if (!BN_GENCB_call(cb, 3, 0))
  488. goto err;
  489. /* step 6 */
  490. counter = 0;
  491. /* "offset = 1" */
  492. n = (L - 1) / (mdsize << 3);
  493. for (;;) {
  494. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  495. goto err;
  496. /* step 7 */
  497. BN_zero(W);
  498. /* now 'buf' contains "SEED + offset - 1" */
  499. for (k = 0; k <= n; k++) {
  500. /*
  501. * obtain "SEED + offset + k" by incrementing:
  502. */
  503. for (i = seed_len - 1; i >= 0; i--) {
  504. seed[i]++;
  505. if (seed[i] != 0)
  506. break;
  507. }
  508. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  509. goto err;
  510. /* step 8 */
  511. if (!BN_bin2bn(md, mdsize, r0))
  512. goto err;
  513. if (!BN_lshift(r0, r0, (mdsize << 3) * k))
  514. goto err;
  515. if (!BN_add(W, W, r0))
  516. goto err;
  517. }
  518. /* more of step 8 */
  519. if (!BN_mask_bits(W, L - 1))
  520. goto err;
  521. if (!BN_copy(X, W))
  522. goto err;
  523. if (!BN_add(X, X, test))
  524. goto err;
  525. /* step 9 */
  526. if (!BN_lshift1(r0, q))
  527. goto err;
  528. if (!BN_mod(c, X, r0, ctx))
  529. goto err;
  530. if (!BN_sub(r0, c, BN_value_one()))
  531. goto err;
  532. if (!BN_sub(p, X, r0))
  533. goto err;
  534. /* step 10 */
  535. if (BN_cmp(p, test) >= 0) {
  536. /* step 11 */
  537. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  538. if (r > 0)
  539. goto end; /* found it */
  540. if (r != 0)
  541. goto err;
  542. }
  543. /* step 13 */
  544. counter++;
  545. /* "offset = offset + n + 1" */
  546. /* step 14 */
  547. if (counter >= (int)(4 * L))
  548. break;
  549. }
  550. if (seed_in) {
  551. ok = 0;
  552. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  553. goto err;
  554. }
  555. }
  556. end:
  557. if (!BN_GENCB_call(cb, 2, 1))
  558. goto err;
  559. g_only:
  560. /* We now need to generate g */
  561. /* Set r0=(p-1)/q */
  562. if (!BN_sub(test, p, BN_value_one()))
  563. goto err;
  564. if (!BN_div(r0, NULL, test, q, ctx))
  565. goto err;
  566. if (idx < 0) {
  567. if (!BN_set_word(test, h))
  568. goto err;
  569. } else
  570. h = 1;
  571. if (!BN_MONT_CTX_set(mont, p, ctx))
  572. goto err;
  573. for (;;) {
  574. static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
  575. if (idx >= 0) {
  576. md[0] = idx & 0xff;
  577. md[1] = (h >> 8) & 0xff;
  578. md[2] = h & 0xff;
  579. if (!EVP_DigestInit_ex(&mctx, evpmd, NULL))
  580. goto err;
  581. if (!EVP_DigestUpdate(&mctx, seed_tmp, seed_len))
  582. goto err;
  583. if (!EVP_DigestUpdate(&mctx, ggen, sizeof(ggen)))
  584. goto err;
  585. if (!EVP_DigestUpdate(&mctx, md, 3))
  586. goto err;
  587. if (!EVP_DigestFinal_ex(&mctx, md, NULL))
  588. goto err;
  589. if (!BN_bin2bn(md, mdsize, test))
  590. goto err;
  591. }
  592. /* g=test^r0%p */
  593. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  594. goto err;
  595. if (!BN_is_one(g))
  596. break;
  597. if (idx < 0 && !BN_add(test, test, BN_value_one()))
  598. goto err;
  599. h++;
  600. if (idx >= 0 && h > 0xffff)
  601. goto err;
  602. }
  603. if (!BN_GENCB_call(cb, 3, 1))
  604. goto err;
  605. ok = 1;
  606. err:
  607. if (ok == 1) {
  608. if (p != ret->p) {
  609. if (ret->p)
  610. BN_free(ret->p);
  611. ret->p = BN_dup(p);
  612. }
  613. if (q != ret->q) {
  614. if (ret->q)
  615. BN_free(ret->q);
  616. ret->q = BN_dup(q);
  617. }
  618. if (ret->g)
  619. BN_free(ret->g);
  620. ret->g = BN_dup(g);
  621. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  622. ok = -1;
  623. goto err;
  624. }
  625. if (counter_ret != NULL)
  626. *counter_ret = counter;
  627. if (h_ret != NULL)
  628. *h_ret = h;
  629. }
  630. if (seed)
  631. OPENSSL_free(seed);
  632. if (seed_out != seed_tmp)
  633. OPENSSL_free(seed_tmp);
  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. EVP_MD_CTX_cleanup(&mctx);
  641. return ok;
  642. }
  643. int dsa_paramgen_check_g(DSA *dsa)
  644. {
  645. BN_CTX *ctx;
  646. BIGNUM *tmp;
  647. BN_MONT_CTX *mont = NULL;
  648. int rv = -1;
  649. ctx = BN_CTX_new();
  650. if (!ctx)
  651. return -1;
  652. BN_CTX_start(ctx);
  653. if (BN_cmp(dsa->g, BN_value_one()) <= 0)
  654. return 0;
  655. if (BN_cmp(dsa->g, dsa->p) >= 0)
  656. return 0;
  657. tmp = BN_CTX_get(ctx);
  658. if (!tmp)
  659. goto err;
  660. if ((mont = BN_MONT_CTX_new()) == NULL)
  661. goto err;
  662. if (!BN_MONT_CTX_set(mont, dsa->p, ctx))
  663. goto err;
  664. /* Work out g^q mod p */
  665. if (!BN_mod_exp_mont(tmp, dsa->g, dsa->q, dsa->p, ctx, mont))
  666. goto err;
  667. if (!BN_cmp(tmp, BN_value_one()))
  668. rv = 1;
  669. else
  670. rv = 0;
  671. err:
  672. BN_CTX_end(ctx);
  673. if (mont)
  674. BN_MONT_CTX_free(mont);
  675. BN_CTX_free(ctx);
  676. return rv;
  677. }
  678. #endif