dsa_gen.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. /* Parameter generation follows the original release of FIPS PUB 186,
  61. * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180) */
  62. #define HASH EVP_sha()
  63. #else
  64. /* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
  65. * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in
  66. * FIPS PUB 180-1) */
  67. #define HASH EVP_sha1()
  68. #endif
  69. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
  70. #ifndef OPENSSL_NO_SHA
  71. #define OPENSSL_FIPSAPI
  72. #include <stdio.h>
  73. #include "cryptlib.h"
  74. #include <openssl/evp.h>
  75. #include <openssl/bn.h>
  76. #include <openssl/rand.h>
  77. #include <openssl/sha.h>
  78. #ifdef OPENSSL_FIPS
  79. #include <openssl/fips.h>
  80. #include <openssl/fips_rand.h>
  81. #endif
  82. #include "dsa_locl.h"
  83. int DSA_generate_parameters_ex(DSA *ret, int bits,
  84. const unsigned char *seed_in, int seed_len,
  85. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
  86. {
  87. if(ret->meth->dsa_paramgen)
  88. return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
  89. counter_ret, h_ret, cb);
  90. else
  91. {
  92. const EVP_MD *evpmd;
  93. size_t qbits = bits >= 2048 ? 256 : 160;
  94. if (bits >= 2048)
  95. {
  96. qbits = 256;
  97. evpmd = EVP_sha256();
  98. }
  99. else
  100. {
  101. qbits = 160;
  102. evpmd = EVP_sha1();
  103. }
  104. return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
  105. seed_in, seed_len, NULL, counter_ret, h_ret, cb);
  106. }
  107. }
  108. int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
  109. const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
  110. unsigned char *seed_out,
  111. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
  112. {
  113. int ok=0;
  114. unsigned char seed[SHA256_DIGEST_LENGTH];
  115. unsigned char md[SHA256_DIGEST_LENGTH];
  116. unsigned char buf[SHA256_DIGEST_LENGTH],buf2[SHA256_DIGEST_LENGTH];
  117. BIGNUM *r0,*W,*X,*c,*test;
  118. BIGNUM *g=NULL,*q=NULL,*p=NULL;
  119. BN_MONT_CTX *mont=NULL;
  120. int i, k, n=0, m=0, qsize = qbits >> 3;
  121. int counter=0;
  122. int r=0;
  123. BN_CTX *ctx=NULL;
  124. unsigned int h=2;
  125. #ifdef OPENSSL_FIPS
  126. if(FIPS_selftest_failed())
  127. {
  128. FIPSerr(FIPS_F_DSA_BUILTIN_PARAMGEN, FIPS_R_FIPS_SELFTEST_FAILED);
  129. goto err;
  130. }
  131. if (FIPS_module_mode() && !(ret->flags & DSA_FLAG_NON_FIPS_ALLOW)
  132. && (bits < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS))
  133. {
  134. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_KEY_SIZE_TOO_SMALL);
  135. goto err;
  136. }
  137. #endif
  138. if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
  139. qsize != SHA256_DIGEST_LENGTH)
  140. /* invalid q size */
  141. return 0;
  142. if (evpmd == NULL)
  143. /* use SHA1 as default */
  144. evpmd = EVP_sha1();
  145. if (bits < 512)
  146. bits = 512;
  147. bits = (bits+63)/64*64;
  148. /* NB: seed_len == 0 is special case: copy generated seed to
  149. * seed_in if it is not NULL.
  150. */
  151. if (seed_len && (seed_len < (size_t)qsize))
  152. seed_in = NULL; /* seed buffer too small -- ignore */
  153. if (seed_len > (size_t)qsize)
  154. seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger SEED,
  155. * but our internal buffers are restricted to 160 bits*/
  156. if (seed_in != NULL)
  157. memcpy(seed, seed_in, seed_len);
  158. if ((ctx=BN_CTX_new()) == NULL)
  159. goto err;
  160. if ((mont=BN_MONT_CTX_new()) == NULL)
  161. goto err;
  162. BN_CTX_start(ctx);
  163. r0 = BN_CTX_get(ctx);
  164. g = BN_CTX_get(ctx);
  165. W = BN_CTX_get(ctx);
  166. q = BN_CTX_get(ctx);
  167. X = BN_CTX_get(ctx);
  168. c = BN_CTX_get(ctx);
  169. p = BN_CTX_get(ctx);
  170. test = BN_CTX_get(ctx);
  171. if (!BN_lshift(test,BN_value_one(),bits-1))
  172. goto err;
  173. for (;;)
  174. {
  175. for (;;) /* find q */
  176. {
  177. int seed_is_random;
  178. /* step 1 */
  179. if(!BN_GENCB_call(cb, 0, m++))
  180. goto err;
  181. if (!seed_len)
  182. {
  183. if (RAND_pseudo_bytes(seed, qsize) < 0)
  184. goto err;
  185. seed_is_random = 1;
  186. }
  187. else
  188. {
  189. seed_is_random = 0;
  190. seed_len=0; /* use random seed if 'seed_in' turns out to be bad*/
  191. }
  192. memcpy(buf , seed, qsize);
  193. memcpy(buf2, seed, qsize);
  194. /* precompute "SEED + 1" for step 7: */
  195. for (i = qsize-1; i >= 0; i--)
  196. {
  197. buf[i]++;
  198. if (buf[i] != 0)
  199. break;
  200. }
  201. /* step 2 */
  202. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
  203. goto err;
  204. if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
  205. goto err;
  206. for (i = 0; i < qsize; i++)
  207. md[i]^=buf2[i];
  208. /* step 3 */
  209. md[0] |= 0x80;
  210. md[qsize-1] |= 0x01;
  211. if (!BN_bin2bn(md, qsize, q))
  212. goto err;
  213. /* step 4 */
  214. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  215. seed_is_random, cb);
  216. if (r > 0)
  217. break;
  218. if (r != 0)
  219. goto err;
  220. /* do a callback call */
  221. /* step 5 */
  222. }
  223. if(!BN_GENCB_call(cb, 2, 0)) goto err;
  224. if(!BN_GENCB_call(cb, 3, 0)) goto err;
  225. /* step 6 */
  226. counter=0;
  227. /* "offset = 2" */
  228. n=(bits-1)/160;
  229. for (;;)
  230. {
  231. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  232. goto err;
  233. /* step 7 */
  234. BN_zero(W);
  235. /* now 'buf' contains "SEED + offset - 1" */
  236. for (k=0; k<=n; k++)
  237. {
  238. /* obtain "SEED + offset + k" by incrementing: */
  239. for (i = qsize-1; i >= 0; i--)
  240. {
  241. buf[i]++;
  242. if (buf[i] != 0)
  243. break;
  244. }
  245. if (!EVP_Digest(buf, qsize, md ,NULL, evpmd,
  246. NULL))
  247. goto err;
  248. /* step 8 */
  249. if (!BN_bin2bn(md, qsize, r0))
  250. goto err;
  251. if (!BN_lshift(r0,r0,(qsize << 3)*k)) goto err;
  252. if (!BN_add(W,W,r0)) goto err;
  253. }
  254. /* more of step 8 */
  255. if (!BN_mask_bits(W,bits-1)) goto err;
  256. if (!BN_copy(X,W)) goto err;
  257. if (!BN_add(X,X,test)) goto err;
  258. /* step 9 */
  259. if (!BN_lshift1(r0,q)) goto err;
  260. if (!BN_mod(c,X,r0,ctx)) goto err;
  261. if (!BN_sub(r0,c,BN_value_one())) goto err;
  262. if (!BN_sub(p,X,r0)) goto err;
  263. /* step 10 */
  264. if (BN_cmp(p,test) >= 0)
  265. {
  266. /* step 11 */
  267. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks,
  268. 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) break;
  279. }
  280. }
  281. end:
  282. if(!BN_GENCB_call(cb, 2, 1))
  283. goto err;
  284. /* We now need to generate g */
  285. /* Set r0=(p-1)/q */
  286. if (!BN_sub(test,p,BN_value_one())) goto err;
  287. if (!BN_div(r0,NULL,test,q,ctx)) goto err;
  288. if (!BN_set_word(test,h)) goto err;
  289. if (!BN_MONT_CTX_set(mont,p,ctx)) goto err;
  290. for (;;)
  291. {
  292. /* g=test^r0%p */
  293. if (!BN_mod_exp_mont(g,test,r0,p,ctx,mont)) goto err;
  294. if (!BN_is_one(g)) break;
  295. if (!BN_add(test,test,BN_value_one())) goto err;
  296. h++;
  297. }
  298. if(!BN_GENCB_call(cb, 3, 1))
  299. goto err;
  300. ok=1;
  301. err:
  302. if (ok)
  303. {
  304. if(ret->p) BN_free(ret->p);
  305. if(ret->q) BN_free(ret->q);
  306. if(ret->g) BN_free(ret->g);
  307. ret->p=BN_dup(p);
  308. ret->q=BN_dup(q);
  309. ret->g=BN_dup(g);
  310. if (ret->p == NULL || ret->q == NULL || ret->g == NULL)
  311. {
  312. ok=0;
  313. goto err;
  314. }
  315. if (counter_ret != NULL) *counter_ret=counter;
  316. if (h_ret != NULL) *h_ret=h;
  317. if (seed_out)
  318. memcpy(seed_out, seed, qsize);
  319. }
  320. if(ctx)
  321. {
  322. BN_CTX_end(ctx);
  323. BN_CTX_free(ctx);
  324. }
  325. if (mont != NULL) BN_MONT_CTX_free(mont);
  326. return ok;
  327. }
  328. #ifdef OPENSSL_FIPS
  329. /* Security strength of parameter values for (L,N): see FIPS186-3 4.2
  330. * and SP800-131A
  331. */
  332. static int fips_ffc_strength(size_t L, size_t N)
  333. {
  334. if (L >= 15360 && N >= 512)
  335. return 256;
  336. if (L >= 7680 && N >= 384)
  337. return 192;
  338. if (L >= 3072 && N >= 256)
  339. return 128;
  340. if (L >= 2048 && N >= 224)
  341. return 112;
  342. if (L >= 1024 && N >= 160)
  343. return 80;
  344. return 0;
  345. }
  346. /* Valid DSA2 parameters from FIPS 186-3 */
  347. static int dsa2_valid_parameters(size_t L, size_t N)
  348. {
  349. if (L == 1024 && N == 160)
  350. return 80;
  351. if (L == 2048 && N == 224)
  352. return 112;
  353. if (L == 2048 && N == 256)
  354. return 112;
  355. if (L == 3072 && N == 256)
  356. return 128;
  357. return 0;
  358. }
  359. int fips_check_dsa_prng(DSA *dsa, size_t L, size_t N)
  360. {
  361. int strength;
  362. if (!FIPS_module_mode())
  363. return 1;
  364. if (dsa->flags & (DSA_FLAG_NON_FIPS_ALLOW|DSA_FLAG_FIPS_CHECKED))
  365. return 1;
  366. if (!L || !N)
  367. {
  368. L = BN_num_bits(dsa->p);
  369. N = BN_num_bits(dsa->q);
  370. }
  371. if (!dsa2_valid_parameters(L, N))
  372. {
  373. FIPSerr(FIPS_F_FIPS_CHECK_DSA_PRNG, FIPS_R_INVALID_PARAMETERS);
  374. return 0;
  375. }
  376. strength = fips_ffc_strength(L, N);
  377. if (!strength)
  378. {
  379. FIPSerr(FIPS_F_FIPS_CHECK_DSA_PRNG,FIPS_R_KEY_TOO_SHORT);
  380. return 0;
  381. }
  382. if (FIPS_rand_strength() >= strength)
  383. return 1;
  384. FIPSerr(FIPS_F_FIPS_CHECK_DSA_PRNG,FIPS_R_PRNG_STRENGTH_TOO_LOW);
  385. return 0;
  386. }
  387. #endif /* OPENSSL_FIPS */
  388. /* This is a parameter generation algorithm for the DSA2 algorithm as
  389. * described in FIPS 186-3.
  390. */
  391. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  392. const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
  393. int idx, unsigned char *seed_out,
  394. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
  395. {
  396. int ok=-1;
  397. unsigned char *seed = NULL, *seed_tmp = NULL;
  398. unsigned char md[EVP_MAX_MD_SIZE];
  399. int mdsize;
  400. BIGNUM *r0,*W,*X,*c,*test;
  401. BIGNUM *g=NULL,*q=NULL,*p=NULL;
  402. BN_MONT_CTX *mont=NULL;
  403. int i, k, n=0, m=0, qsize = N >> 3;
  404. int counter=0;
  405. int r=0;
  406. BN_CTX *ctx=NULL;
  407. EVP_MD_CTX mctx;
  408. unsigned int h=2;
  409. EVP_MD_CTX_init(&mctx);
  410. #ifdef OPENSSL_FIPS
  411. if(FIPS_selftest_failed())
  412. {
  413. FIPSerr(FIPS_F_DSA_BUILTIN_PARAMGEN2,
  414. FIPS_R_FIPS_SELFTEST_FAILED);
  415. goto err;
  416. }
  417. if (!fips_check_dsa_prng(ret, L, N))
  418. goto err;
  419. #endif
  420. if (evpmd == NULL)
  421. {
  422. if (N == 160)
  423. evpmd = EVP_sha1();
  424. else if (N == 224)
  425. evpmd = EVP_sha224();
  426. else
  427. evpmd = EVP_sha256();
  428. }
  429. mdsize = M_EVP_MD_size(evpmd);
  430. /* If unverificable g generation only don't need seed */
  431. if (!ret->p || !ret->q || idx >= 0)
  432. {
  433. if (seed_len == 0)
  434. seed_len = mdsize;
  435. seed = OPENSSL_malloc(seed_len);
  436. if (seed_out)
  437. seed_tmp = seed_out;
  438. else
  439. seed_tmp = OPENSSL_malloc(seed_len);
  440. if (!seed || !seed_tmp)
  441. goto err;
  442. if (seed_in)
  443. memcpy(seed, seed_in, seed_len);
  444. }
  445. if ((ctx=BN_CTX_new()) == NULL)
  446. goto err;
  447. if ((mont=BN_MONT_CTX_new()) == NULL)
  448. goto err;
  449. BN_CTX_start(ctx);
  450. r0 = BN_CTX_get(ctx);
  451. g = BN_CTX_get(ctx);
  452. W = BN_CTX_get(ctx);
  453. X = BN_CTX_get(ctx);
  454. c = BN_CTX_get(ctx);
  455. test = BN_CTX_get(ctx);
  456. /* if p, q already supplied generate g only */
  457. if (ret->p && ret->q)
  458. {
  459. p = ret->p;
  460. q = ret->q;
  461. if (idx >= 0)
  462. memcpy(seed_tmp, seed, seed_len);
  463. goto g_only;
  464. }
  465. else
  466. {
  467. p = BN_CTX_get(ctx);
  468. q = BN_CTX_get(ctx);
  469. }
  470. if (!BN_lshift(test,BN_value_one(),L-1))
  471. goto err;
  472. for (;;)
  473. {
  474. for (;;) /* find q */
  475. {
  476. unsigned char *pmd;
  477. /* step 1 */
  478. if(!BN_GENCB_call(cb, 0, m++))
  479. goto err;
  480. if (!seed_in)
  481. {
  482. if (RAND_pseudo_bytes(seed, seed_len) < 0)
  483. goto err;
  484. }
  485. /* step 2 */
  486. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  487. goto err;
  488. /* Take least significant bits of md */
  489. if (mdsize > qsize)
  490. pmd = md + mdsize - qsize;
  491. else
  492. pmd = md;
  493. if (mdsize < qsize)
  494. memset(md + mdsize, 0, qsize - mdsize);
  495. /* step 3 */
  496. pmd[0] |= 0x80;
  497. pmd[qsize-1] |= 0x01;
  498. if (!BN_bin2bn(pmd, qsize, q))
  499. goto err;
  500. /* step 4 */
  501. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  502. seed_in ? 1 : 0, cb);
  503. if (r > 0)
  504. break;
  505. if (r != 0)
  506. goto err;
  507. /* Provided seed didn't produce a prime: error */
  508. if (seed_in)
  509. {
  510. ok = 0;
  511. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  512. goto err;
  513. }
  514. /* do a callback call */
  515. /* step 5 */
  516. }
  517. /* Copy seed to seed_out before we mess with it */
  518. if (seed_out)
  519. memcpy(seed_out, seed, seed_len);
  520. if(!BN_GENCB_call(cb, 2, 0)) goto err;
  521. if(!BN_GENCB_call(cb, 3, 0)) goto err;
  522. /* step 6 */
  523. counter=0;
  524. /* "offset = 1" */
  525. n=(L-1)/(mdsize << 3);
  526. for (;;)
  527. {
  528. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  529. goto err;
  530. /* step 7 */
  531. BN_zero(W);
  532. /* now 'buf' contains "SEED + offset - 1" */
  533. for (k=0; k<=n; k++)
  534. {
  535. /* obtain "SEED + offset + k" by incrementing: */
  536. for (i = seed_len-1; i >= 0; i--)
  537. {
  538. seed[i]++;
  539. if (seed[i] != 0)
  540. break;
  541. }
  542. if (!EVP_Digest(seed, seed_len, md ,NULL, evpmd,
  543. NULL))
  544. goto err;
  545. /* step 8 */
  546. if (!BN_bin2bn(md, mdsize, r0))
  547. goto err;
  548. if (!BN_lshift(r0,r0,(mdsize << 3)*k)) goto err;
  549. if (!BN_add(W,W,r0)) goto err;
  550. }
  551. /* more of step 8 */
  552. if (!BN_mask_bits(W,L-1)) goto err;
  553. if (!BN_copy(X,W)) goto err;
  554. if (!BN_add(X,X,test)) goto err;
  555. /* step 9 */
  556. if (!BN_lshift1(r0,q)) goto err;
  557. if (!BN_mod(c,X,r0,ctx)) goto err;
  558. if (!BN_sub(r0,c,BN_value_one())) goto err;
  559. if (!BN_sub(p,X,r0)) goto err;
  560. /* step 10 */
  561. if (BN_cmp(p,test) >= 0)
  562. {
  563. /* step 11 */
  564. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks,
  565. ctx, 1, cb);
  566. if (r > 0)
  567. goto end; /* found it */
  568. if (r != 0)
  569. goto err;
  570. }
  571. /* step 13 */
  572. counter++;
  573. /* "offset = offset + n + 1" */
  574. /* step 14 */
  575. if (counter >= (int)(4 * L)) break;
  576. }
  577. if (seed_in)
  578. {
  579. ok = 0;
  580. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  581. goto err;
  582. }
  583. }
  584. end:
  585. if(!BN_GENCB_call(cb, 2, 1))
  586. goto err;
  587. g_only:
  588. /* We now need to generate g */
  589. /* Set r0=(p-1)/q */
  590. if (!BN_sub(test,p,BN_value_one())) goto err;
  591. if (!BN_div(r0,NULL,test,q,ctx)) goto err;
  592. if (idx < 0)
  593. {
  594. if (!BN_set_word(test,h))
  595. goto err;
  596. }
  597. else
  598. h = 1;
  599. if (!BN_MONT_CTX_set(mont,p,ctx)) goto err;
  600. for (;;)
  601. {
  602. __fips_constseg
  603. static const unsigned char ggen[4] = {0x67,0x67,0x65,0x6e};
  604. if (idx >= 0)
  605. {
  606. md[0] = idx & 0xff;
  607. md[1] = (h >> 8) & 0xff;
  608. md[2] = h & 0xff;
  609. if (!EVP_DigestInit_ex(&mctx, evpmd, NULL))
  610. goto err;
  611. if (!EVP_DigestUpdate(&mctx, seed_tmp, seed_len))
  612. goto err;
  613. if (!EVP_DigestUpdate(&mctx, ggen, sizeof(ggen)))
  614. goto err;
  615. if (!EVP_DigestUpdate(&mctx, md, 3))
  616. goto err;
  617. if (!EVP_DigestFinal_ex(&mctx, md, NULL))
  618. goto err;
  619. if (!BN_bin2bn(md, mdsize, test))
  620. goto err;
  621. }
  622. /* g=test^r0%p */
  623. if (!BN_mod_exp_mont(g,test,r0,p,ctx,mont)) goto err;
  624. if (!BN_is_one(g)) break;
  625. if (idx < 0 && !BN_add(test,test,BN_value_one())) goto err;
  626. h++;
  627. if ( idx >= 0 && h > 0xffff)
  628. goto err;
  629. }
  630. if(!BN_GENCB_call(cb, 3, 1))
  631. goto err;
  632. ok=1;
  633. err:
  634. if (ok == 1)
  635. {
  636. if (p != ret->p)
  637. {
  638. if(ret->p) BN_free(ret->p);
  639. ret->p=BN_dup(p);
  640. }
  641. if (q != ret->q)
  642. {
  643. if(ret->q) BN_free(ret->q);
  644. ret->q=BN_dup(q);
  645. }
  646. if(ret->g) BN_free(ret->g);
  647. ret->g=BN_dup(g);
  648. if (ret->p == NULL || ret->q == NULL || ret->g == NULL)
  649. {
  650. ok=-1;
  651. goto err;
  652. }
  653. if (counter_ret != NULL) *counter_ret=counter;
  654. if (h_ret != NULL) *h_ret=h;
  655. }
  656. if (seed)
  657. OPENSSL_free(seed);
  658. if (seed_out != seed_tmp)
  659. OPENSSL_free(seed_tmp);
  660. if(ctx)
  661. {
  662. BN_CTX_end(ctx);
  663. BN_CTX_free(ctx);
  664. }
  665. if (mont != NULL) BN_MONT_CTX_free(mont);
  666. EVP_MD_CTX_cleanup(&mctx);
  667. return ok;
  668. }
  669. int dsa_paramgen_check_g(DSA *dsa)
  670. {
  671. BN_CTX *ctx;
  672. BIGNUM *tmp;
  673. BN_MONT_CTX *mont = NULL;
  674. int rv = -1;
  675. ctx = BN_CTX_new();
  676. if (!ctx)
  677. return -1;
  678. BN_CTX_start(ctx);
  679. if (BN_cmp(dsa->g, BN_value_one()) <= 0)
  680. return 0;
  681. if (BN_cmp(dsa->g, dsa->p) >= 0)
  682. return 0;
  683. tmp = BN_CTX_get(ctx);
  684. if (!tmp)
  685. goto err;
  686. if ((mont=BN_MONT_CTX_new()) == NULL)
  687. goto err;
  688. if (!BN_MONT_CTX_set(mont,dsa->p,ctx))
  689. goto err;
  690. /* Work out g^q mod p */
  691. if (!BN_mod_exp_mont(tmp,dsa->g,dsa->q, dsa->p, ctx, mont))
  692. goto err;
  693. if (!BN_cmp(tmp, BN_value_one()))
  694. rv = 1;
  695. else
  696. rv = 0;
  697. err:
  698. BN_CTX_end(ctx);
  699. if (mont)
  700. BN_MONT_CTX_free(mont);
  701. BN_CTX_free(ctx);
  702. return rv;
  703. }
  704. #endif