dsa_gen.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* crypto/dsa/dsa_gen.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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. /* use SHA1 as default */
  140. evpmd = EVP_sha1();
  141. if (bits < 512)
  142. bits = 512;
  143. bits = (bits + 63) / 64 * 64;
  144. /*
  145. * NB: seed_len == 0 is special case: copy generated seed to seed_in if
  146. * it is not NULL.
  147. */
  148. if (seed_len && (seed_len < (size_t)qsize))
  149. seed_in = NULL; /* seed buffer too small -- ignore */
  150. if (seed_len > (size_t)qsize)
  151. seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger
  152. * SEED, but our internal buffers are
  153. * restricted to 160 bits */
  154. if (seed_in != NULL)
  155. memcpy(seed, seed_in, seed_len);
  156. if ((mont = BN_MONT_CTX_new()) == NULL)
  157. goto err;
  158. if ((ctx = BN_CTX_new()) == NULL)
  159. goto err;
  160. BN_CTX_start(ctx);
  161. r0 = BN_CTX_get(ctx);
  162. g = BN_CTX_get(ctx);
  163. W = BN_CTX_get(ctx);
  164. q = BN_CTX_get(ctx);
  165. X = BN_CTX_get(ctx);
  166. c = BN_CTX_get(ctx);
  167. p = BN_CTX_get(ctx);
  168. test = BN_CTX_get(ctx);
  169. if (!BN_lshift(test, BN_value_one(), bits - 1))
  170. goto err;
  171. for (;;) {
  172. for (;;) { /* find q */
  173. int seed_is_random;
  174. /* step 1 */
  175. if (!BN_GENCB_call(cb, 0, m++))
  176. goto err;
  177. if (!seed_len || !seed_in) {
  178. if (RAND_pseudo_bytes(seed, qsize) < 0)
  179. goto err;
  180. seed_is_random = 1;
  181. } else {
  182. seed_is_random = 0;
  183. seed_len = 0; /* use random seed if 'seed_in' turns out to
  184. * be bad */
  185. }
  186. memcpy(buf, seed, qsize);
  187. memcpy(buf2, seed, qsize);
  188. /* precompute "SEED + 1" for step 7: */
  189. for (i = qsize - 1; i >= 0; i--) {
  190. buf[i]++;
  191. if (buf[i] != 0)
  192. break;
  193. }
  194. /* step 2 */
  195. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
  196. goto err;
  197. if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
  198. goto err;
  199. for (i = 0; i < qsize; i++)
  200. md[i] ^= buf2[i];
  201. /* step 3 */
  202. md[0] |= 0x80;
  203. md[qsize - 1] |= 0x01;
  204. if (!BN_bin2bn(md, qsize, q))
  205. goto err;
  206. /* step 4 */
  207. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  208. seed_is_random, cb);
  209. if (r > 0)
  210. break;
  211. if (r != 0)
  212. goto err;
  213. /* do a callback call */
  214. /* step 5 */
  215. }
  216. if (!BN_GENCB_call(cb, 2, 0))
  217. goto err;
  218. if (!BN_GENCB_call(cb, 3, 0))
  219. goto err;
  220. /* step 6 */
  221. counter = 0;
  222. /* "offset = 2" */
  223. n = (bits - 1) / 160;
  224. for (;;) {
  225. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  226. goto err;
  227. /* step 7 */
  228. BN_zero(W);
  229. /* now 'buf' contains "SEED + offset - 1" */
  230. for (k = 0; k <= n; k++) {
  231. /*
  232. * obtain "SEED + offset + k" by incrementing:
  233. */
  234. for (i = qsize - 1; i >= 0; i--) {
  235. buf[i]++;
  236. if (buf[i] != 0)
  237. break;
  238. }
  239. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
  240. goto err;
  241. /* step 8 */
  242. if (!BN_bin2bn(md, qsize, r0))
  243. goto err;
  244. if (!BN_lshift(r0, r0, (qsize << 3) * k))
  245. goto err;
  246. if (!BN_add(W, W, r0))
  247. goto err;
  248. }
  249. /* more of step 8 */
  250. if (!BN_mask_bits(W, bits - 1))
  251. goto err;
  252. if (!BN_copy(X, W))
  253. goto err;
  254. if (!BN_add(X, X, test))
  255. goto err;
  256. /* step 9 */
  257. if (!BN_lshift1(r0, q))
  258. goto err;
  259. if (!BN_mod(c, X, r0, ctx))
  260. goto err;
  261. if (!BN_sub(r0, c, BN_value_one()))
  262. goto err;
  263. if (!BN_sub(p, X, r0))
  264. goto err;
  265. /* step 10 */
  266. if (BN_cmp(p, test) >= 0) {
  267. /* step 11 */
  268. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  269. if (r > 0)
  270. goto end; /* found it */
  271. if (r != 0)
  272. goto err;
  273. }
  274. /* step 13 */
  275. counter++;
  276. /* "offset = offset + n + 1" */
  277. /* step 14 */
  278. if (counter >= 4096)
  279. break;
  280. }
  281. }
  282. end:
  283. if (!BN_GENCB_call(cb, 2, 1))
  284. goto err;
  285. /* We now need to generate g */
  286. /* Set r0=(p-1)/q */
  287. if (!BN_sub(test, p, BN_value_one()))
  288. goto err;
  289. if (!BN_div(r0, NULL, test, q, ctx))
  290. goto err;
  291. if (!BN_set_word(test, h))
  292. goto err;
  293. if (!BN_MONT_CTX_set(mont, p, ctx))
  294. goto err;
  295. for (;;) {
  296. /* g=test^r0%p */
  297. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  298. goto err;
  299. if (!BN_is_one(g))
  300. break;
  301. if (!BN_add(test, test, BN_value_one()))
  302. goto err;
  303. h++;
  304. }
  305. if (!BN_GENCB_call(cb, 3, 1))
  306. goto err;
  307. ok = 1;
  308. err:
  309. if (ok) {
  310. if (ret->p)
  311. BN_free(ret->p);
  312. if (ret->q)
  313. BN_free(ret->q);
  314. if (ret->g)
  315. BN_free(ret->g);
  316. ret->p = BN_dup(p);
  317. ret->q = BN_dup(q);
  318. ret->g = BN_dup(g);
  319. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  320. ok = 0;
  321. goto err;
  322. }
  323. if (counter_ret != NULL)
  324. *counter_ret = counter;
  325. if (h_ret != NULL)
  326. *h_ret = h;
  327. if (seed_out)
  328. memcpy(seed_out, seed, qsize);
  329. }
  330. if (ctx) {
  331. BN_CTX_end(ctx);
  332. BN_CTX_free(ctx);
  333. }
  334. if (mont != NULL)
  335. BN_MONT_CTX_free(mont);
  336. return ok;
  337. }
  338. # ifdef OPENSSL_FIPS
  339. # undef fips_dsa_builtin_paramgen2
  340. extern int fips_dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  341. const EVP_MD *evpmd,
  342. const unsigned char *seed_in,
  343. size_t seed_len, int idx,
  344. unsigned char *seed_out,
  345. int *counter_ret, unsigned long *h_ret,
  346. BN_GENCB *cb);
  347. # endif
  348. /*
  349. * This is a parameter generation algorithm for the DSA2 algorithm as
  350. * described in FIPS 186-3.
  351. */
  352. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  353. const EVP_MD *evpmd, const unsigned char *seed_in,
  354. size_t seed_len, int idx, unsigned char *seed_out,
  355. int *counter_ret, unsigned long *h_ret,
  356. BN_GENCB *cb)
  357. {
  358. int ok = -1;
  359. unsigned char *seed = NULL, *seed_tmp = NULL;
  360. unsigned char md[EVP_MAX_MD_SIZE];
  361. int mdsize;
  362. BIGNUM *r0, *W, *X, *c, *test;
  363. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  364. BN_MONT_CTX *mont = NULL;
  365. int i, k, n = 0, m = 0, qsize = N >> 3;
  366. int counter = 0;
  367. int r = 0;
  368. BN_CTX *ctx = NULL;
  369. EVP_MD_CTX mctx;
  370. unsigned int h = 2;
  371. # ifdef OPENSSL_FIPS
  372. if (FIPS_mode())
  373. return fips_dsa_builtin_paramgen2(ret, L, N, evpmd,
  374. seed_in, seed_len, idx,
  375. seed_out, counter_ret, h_ret, cb);
  376. # endif
  377. EVP_MD_CTX_init(&mctx);
  378. if (evpmd == NULL) {
  379. if (N == 160)
  380. evpmd = EVP_sha1();
  381. else if (N == 224)
  382. evpmd = EVP_sha224();
  383. else
  384. evpmd = EVP_sha256();
  385. }
  386. mdsize = EVP_MD_size(evpmd);
  387. /* If unverificable g generation only don't need seed */
  388. if (!ret->p || !ret->q || idx >= 0) {
  389. if (seed_len == 0)
  390. seed_len = mdsize;
  391. seed = OPENSSL_malloc(seed_len);
  392. if (seed_out)
  393. seed_tmp = seed_out;
  394. else
  395. seed_tmp = OPENSSL_malloc(seed_len);
  396. if (!seed || !seed_tmp)
  397. goto err;
  398. if (seed_in)
  399. memcpy(seed, seed_in, seed_len);
  400. }
  401. if ((ctx = BN_CTX_new()) == NULL)
  402. goto err;
  403. if ((mont = BN_MONT_CTX_new()) == NULL)
  404. goto err;
  405. BN_CTX_start(ctx);
  406. r0 = BN_CTX_get(ctx);
  407. g = BN_CTX_get(ctx);
  408. W = BN_CTX_get(ctx);
  409. X = BN_CTX_get(ctx);
  410. c = BN_CTX_get(ctx);
  411. test = BN_CTX_get(ctx);
  412. /* if p, q already supplied generate g only */
  413. if (ret->p && ret->q) {
  414. p = ret->p;
  415. q = ret->q;
  416. if (idx >= 0)
  417. memcpy(seed_tmp, seed, seed_len);
  418. goto g_only;
  419. } else {
  420. p = BN_CTX_get(ctx);
  421. q = BN_CTX_get(ctx);
  422. }
  423. if (!BN_lshift(test, BN_value_one(), L - 1))
  424. goto err;
  425. for (;;) {
  426. for (;;) { /* find q */
  427. unsigned char *pmd;
  428. /* step 1 */
  429. if (!BN_GENCB_call(cb, 0, m++))
  430. goto err;
  431. if (!seed_in) {
  432. if (RAND_pseudo_bytes(seed, seed_len) < 0)
  433. goto err;
  434. }
  435. /* step 2 */
  436. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  437. goto err;
  438. /* Take least significant bits of md */
  439. if (mdsize > qsize)
  440. pmd = md + mdsize - qsize;
  441. else
  442. pmd = md;
  443. if (mdsize < qsize)
  444. memset(md + mdsize, 0, qsize - mdsize);
  445. /* step 3 */
  446. pmd[0] |= 0x80;
  447. pmd[qsize - 1] |= 0x01;
  448. if (!BN_bin2bn(pmd, qsize, q))
  449. goto err;
  450. /* step 4 */
  451. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  452. seed_in ? 1 : 0, cb);
  453. if (r > 0)
  454. break;
  455. if (r != 0)
  456. goto err;
  457. /* Provided seed didn't produce a prime: error */
  458. if (seed_in) {
  459. ok = 0;
  460. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  461. goto err;
  462. }
  463. /* do a callback call */
  464. /* step 5 */
  465. }
  466. /* Copy seed to seed_out before we mess with it */
  467. if (seed_out)
  468. memcpy(seed_out, seed, seed_len);
  469. if (!BN_GENCB_call(cb, 2, 0))
  470. goto err;
  471. if (!BN_GENCB_call(cb, 3, 0))
  472. goto err;
  473. /* step 6 */
  474. counter = 0;
  475. /* "offset = 1" */
  476. n = (L - 1) / (mdsize << 3);
  477. for (;;) {
  478. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  479. goto err;
  480. /* step 7 */
  481. BN_zero(W);
  482. /* now 'buf' contains "SEED + offset - 1" */
  483. for (k = 0; k <= n; k++) {
  484. /*
  485. * obtain "SEED + offset + k" by incrementing:
  486. */
  487. for (i = seed_len - 1; i >= 0; i--) {
  488. seed[i]++;
  489. if (seed[i] != 0)
  490. break;
  491. }
  492. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  493. goto err;
  494. /* step 8 */
  495. if (!BN_bin2bn(md, mdsize, r0))
  496. goto err;
  497. if (!BN_lshift(r0, r0, (mdsize << 3) * k))
  498. goto err;
  499. if (!BN_add(W, W, r0))
  500. goto err;
  501. }
  502. /* more of step 8 */
  503. if (!BN_mask_bits(W, L - 1))
  504. goto err;
  505. if (!BN_copy(X, W))
  506. goto err;
  507. if (!BN_add(X, X, test))
  508. goto err;
  509. /* step 9 */
  510. if (!BN_lshift1(r0, q))
  511. goto err;
  512. if (!BN_mod(c, X, r0, ctx))
  513. goto err;
  514. if (!BN_sub(r0, c, BN_value_one()))
  515. goto err;
  516. if (!BN_sub(p, X, r0))
  517. goto err;
  518. /* step 10 */
  519. if (BN_cmp(p, test) >= 0) {
  520. /* step 11 */
  521. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  522. if (r > 0)
  523. goto end; /* found it */
  524. if (r != 0)
  525. goto err;
  526. }
  527. /* step 13 */
  528. counter++;
  529. /* "offset = offset + n + 1" */
  530. /* step 14 */
  531. if (counter >= (int)(4 * L))
  532. break;
  533. }
  534. if (seed_in) {
  535. ok = 0;
  536. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  537. goto err;
  538. }
  539. }
  540. end:
  541. if (!BN_GENCB_call(cb, 2, 1))
  542. goto err;
  543. g_only:
  544. /* We now need to generate g */
  545. /* Set r0=(p-1)/q */
  546. if (!BN_sub(test, p, BN_value_one()))
  547. goto err;
  548. if (!BN_div(r0, NULL, test, q, ctx))
  549. goto err;
  550. if (idx < 0) {
  551. if (!BN_set_word(test, h))
  552. goto err;
  553. } else
  554. h = 1;
  555. if (!BN_MONT_CTX_set(mont, p, ctx))
  556. goto err;
  557. for (;;) {
  558. static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
  559. if (idx >= 0) {
  560. md[0] = idx & 0xff;
  561. md[1] = (h >> 8) & 0xff;
  562. md[2] = h & 0xff;
  563. if (!EVP_DigestInit_ex(&mctx, evpmd, NULL))
  564. goto err;
  565. if (!EVP_DigestUpdate(&mctx, seed_tmp, seed_len))
  566. goto err;
  567. if (!EVP_DigestUpdate(&mctx, ggen, sizeof(ggen)))
  568. goto err;
  569. if (!EVP_DigestUpdate(&mctx, md, 3))
  570. goto err;
  571. if (!EVP_DigestFinal_ex(&mctx, md, NULL))
  572. goto err;
  573. if (!BN_bin2bn(md, mdsize, test))
  574. goto err;
  575. }
  576. /* g=test^r0%p */
  577. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  578. goto err;
  579. if (!BN_is_one(g))
  580. break;
  581. if (idx < 0 && !BN_add(test, test, BN_value_one()))
  582. goto err;
  583. h++;
  584. if (idx >= 0 && h > 0xffff)
  585. goto err;
  586. }
  587. if (!BN_GENCB_call(cb, 3, 1))
  588. goto err;
  589. ok = 1;
  590. err:
  591. if (ok == 1) {
  592. if (p != ret->p) {
  593. if (ret->p)
  594. BN_free(ret->p);
  595. ret->p = BN_dup(p);
  596. }
  597. if (q != ret->q) {
  598. if (ret->q)
  599. BN_free(ret->q);
  600. ret->q = BN_dup(q);
  601. }
  602. if (ret->g)
  603. BN_free(ret->g);
  604. ret->g = BN_dup(g);
  605. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  606. ok = -1;
  607. goto err;
  608. }
  609. if (counter_ret != NULL)
  610. *counter_ret = counter;
  611. if (h_ret != NULL)
  612. *h_ret = h;
  613. }
  614. if (seed)
  615. OPENSSL_free(seed);
  616. if (seed_out != seed_tmp)
  617. OPENSSL_free(seed_tmp);
  618. if (ctx) {
  619. BN_CTX_end(ctx);
  620. BN_CTX_free(ctx);
  621. }
  622. if (mont != NULL)
  623. BN_MONT_CTX_free(mont);
  624. EVP_MD_CTX_cleanup(&mctx);
  625. return ok;
  626. }
  627. int dsa_paramgen_check_g(DSA *dsa)
  628. {
  629. BN_CTX *ctx;
  630. BIGNUM *tmp;
  631. BN_MONT_CTX *mont = NULL;
  632. int rv = -1;
  633. ctx = BN_CTX_new();
  634. if (!ctx)
  635. return -1;
  636. BN_CTX_start(ctx);
  637. if (BN_cmp(dsa->g, BN_value_one()) <= 0)
  638. return 0;
  639. if (BN_cmp(dsa->g, dsa->p) >= 0)
  640. return 0;
  641. tmp = BN_CTX_get(ctx);
  642. if (!tmp)
  643. goto err;
  644. if ((mont = BN_MONT_CTX_new()) == NULL)
  645. goto err;
  646. if (!BN_MONT_CTX_set(mont, dsa->p, ctx))
  647. goto err;
  648. /* Work out g^q mod p */
  649. if (!BN_mod_exp_mont(tmp, dsa->g, dsa->q, dsa->p, ctx, mont))
  650. goto err;
  651. if (!BN_cmp(tmp, BN_value_one()))
  652. rv = 1;
  653. else
  654. rv = 0;
  655. err:
  656. BN_CTX_end(ctx);
  657. if (mont)
  658. BN_MONT_CTX_free(mont);
  659. BN_CTX_free(ctx);
  660. return rv;
  661. }
  662. #endif