dsa_gen.c 22 KB

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