dsa_gen.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
  11. * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
  12. * 180-1)
  13. */
  14. #define xxxHASH EVP_sha1()
  15. #include <openssl/opensslconf.h>
  16. #include <stdio.h>
  17. #include "internal/cryptlib.h"
  18. #include <openssl/evp.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/rand.h>
  21. #include <openssl/sha.h>
  22. #include "dsa_local.h"
  23. int DSA_generate_parameters_ex(DSA *ret, int bits,
  24. const unsigned char *seed_in, int seed_len,
  25. int *counter_ret, unsigned long *h_ret,
  26. BN_GENCB *cb)
  27. {
  28. if (ret->meth->dsa_paramgen)
  29. return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
  30. counter_ret, h_ret, cb);
  31. else {
  32. const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
  33. size_t qbits = EVP_MD_size(evpmd) * 8;
  34. return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
  35. seed_in, seed_len, NULL, counter_ret,
  36. h_ret, cb);
  37. }
  38. }
  39. int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
  40. const EVP_MD *evpmd, const unsigned char *seed_in,
  41. size_t seed_len, unsigned char *seed_out,
  42. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
  43. {
  44. int ok = 0;
  45. unsigned char seed[SHA256_DIGEST_LENGTH];
  46. unsigned char md[SHA256_DIGEST_LENGTH];
  47. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  48. BIGNUM *r0, *W, *X, *c, *test;
  49. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  50. BN_MONT_CTX *mont = NULL;
  51. int i, k, n = 0, m = 0, qsize = qbits >> 3;
  52. int counter = 0;
  53. int r = 0;
  54. BN_CTX *ctx = NULL;
  55. unsigned int h = 2;
  56. if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
  57. qsize != SHA256_DIGEST_LENGTH)
  58. /* invalid q size */
  59. return 0;
  60. if (evpmd == NULL) {
  61. if (qsize == SHA_DIGEST_LENGTH)
  62. evpmd = EVP_sha1();
  63. else if (qsize == SHA224_DIGEST_LENGTH)
  64. evpmd = EVP_sha224();
  65. else
  66. evpmd = EVP_sha256();
  67. } else {
  68. qsize = EVP_MD_size(evpmd);
  69. }
  70. if (bits < 512)
  71. bits = 512;
  72. bits = (bits + 63) / 64 * 64;
  73. if (seed_in != NULL) {
  74. if (seed_len < (size_t)qsize) {
  75. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_SEED_LEN_SMALL);
  76. return 0;
  77. }
  78. if (seed_len > (size_t)qsize) {
  79. /* Only consume as much seed as is expected. */
  80. seed_len = qsize;
  81. }
  82. memcpy(seed, seed_in, seed_len);
  83. }
  84. if ((mont = BN_MONT_CTX_new()) == NULL)
  85. goto err;
  86. if ((ctx = BN_CTX_new()) == NULL)
  87. goto err;
  88. BN_CTX_start(ctx);
  89. r0 = BN_CTX_get(ctx);
  90. g = BN_CTX_get(ctx);
  91. W = BN_CTX_get(ctx);
  92. q = BN_CTX_get(ctx);
  93. X = BN_CTX_get(ctx);
  94. c = BN_CTX_get(ctx);
  95. p = BN_CTX_get(ctx);
  96. test = BN_CTX_get(ctx);
  97. if (test == NULL)
  98. goto err;
  99. if (!BN_lshift(test, BN_value_one(), bits - 1))
  100. goto err;
  101. for (;;) {
  102. for (;;) { /* find q */
  103. int use_random_seed = (seed_in == NULL);
  104. /* step 1 */
  105. if (!BN_GENCB_call(cb, 0, m++))
  106. goto err;
  107. if (use_random_seed) {
  108. if (RAND_bytes(seed, qsize) <= 0)
  109. goto err;
  110. } else {
  111. /* If we come back through, use random seed next time. */
  112. seed_in = NULL;
  113. }
  114. memcpy(buf, seed, qsize);
  115. memcpy(buf2, seed, qsize);
  116. /* precompute "SEED + 1" for step 7: */
  117. for (i = qsize - 1; i >= 0; i--) {
  118. buf[i]++;
  119. if (buf[i] != 0)
  120. break;
  121. }
  122. /* step 2 */
  123. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
  124. goto err;
  125. if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
  126. goto err;
  127. for (i = 0; i < qsize; i++)
  128. md[i] ^= buf2[i];
  129. /* step 3 */
  130. md[0] |= 0x80;
  131. md[qsize - 1] |= 0x01;
  132. if (!BN_bin2bn(md, qsize, q))
  133. goto err;
  134. /* step 4 */
  135. r = BN_check_prime(q, ctx, cb);
  136. if (r > 0)
  137. break;
  138. if (r != 0)
  139. goto err;
  140. /* do a callback call */
  141. /* step 5 */
  142. }
  143. if (!BN_GENCB_call(cb, 2, 0))
  144. goto err;
  145. if (!BN_GENCB_call(cb, 3, 0))
  146. goto err;
  147. /* step 6 */
  148. counter = 0;
  149. /* "offset = 2" */
  150. n = (bits - 1) / 160;
  151. for (;;) {
  152. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  153. goto err;
  154. /* step 7 */
  155. BN_zero(W);
  156. /* now 'buf' contains "SEED + offset - 1" */
  157. for (k = 0; k <= n; k++) {
  158. /*
  159. * obtain "SEED + offset + k" by incrementing:
  160. */
  161. for (i = qsize - 1; i >= 0; i--) {
  162. buf[i]++;
  163. if (buf[i] != 0)
  164. break;
  165. }
  166. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
  167. goto err;
  168. /* step 8 */
  169. if (!BN_bin2bn(md, qsize, r0))
  170. goto err;
  171. if (!BN_lshift(r0, r0, (qsize << 3) * k))
  172. goto err;
  173. if (!BN_add(W, W, r0))
  174. goto err;
  175. }
  176. /* more of step 8 */
  177. if (!BN_mask_bits(W, bits - 1))
  178. goto err;
  179. if (!BN_copy(X, W))
  180. goto err;
  181. if (!BN_add(X, X, test))
  182. goto err;
  183. /* step 9 */
  184. if (!BN_lshift1(r0, q))
  185. goto err;
  186. if (!BN_mod(c, X, r0, ctx))
  187. goto err;
  188. if (!BN_sub(r0, c, BN_value_one()))
  189. goto err;
  190. if (!BN_sub(p, X, r0))
  191. goto err;
  192. /* step 10 */
  193. if (BN_cmp(p, test) >= 0) {
  194. /* step 11 */
  195. r = BN_check_prime(p, ctx, cb);
  196. if (r > 0)
  197. goto end; /* found it */
  198. if (r != 0)
  199. goto err;
  200. }
  201. /* step 13 */
  202. counter++;
  203. /* "offset = offset + n + 1" */
  204. /* step 14 */
  205. if (counter >= 4096)
  206. break;
  207. }
  208. }
  209. end:
  210. if (!BN_GENCB_call(cb, 2, 1))
  211. goto err;
  212. /* We now need to generate g */
  213. /* Set r0=(p-1)/q */
  214. if (!BN_sub(test, p, BN_value_one()))
  215. goto err;
  216. if (!BN_div(r0, NULL, test, q, ctx))
  217. goto err;
  218. if (!BN_set_word(test, h))
  219. goto err;
  220. if (!BN_MONT_CTX_set(mont, p, ctx))
  221. goto err;
  222. for (;;) {
  223. /* g=test^r0%p */
  224. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  225. goto err;
  226. if (!BN_is_one(g))
  227. break;
  228. if (!BN_add(test, test, BN_value_one()))
  229. goto err;
  230. h++;
  231. }
  232. if (!BN_GENCB_call(cb, 3, 1))
  233. goto err;
  234. ok = 1;
  235. err:
  236. if (ok) {
  237. BN_free(ret->p);
  238. BN_free(ret->q);
  239. BN_free(ret->g);
  240. ret->p = BN_dup(p);
  241. ret->q = BN_dup(q);
  242. ret->g = BN_dup(g);
  243. ret->dirty_cnt++;
  244. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  245. ok = 0;
  246. goto err;
  247. }
  248. if (counter_ret != NULL)
  249. *counter_ret = counter;
  250. if (h_ret != NULL)
  251. *h_ret = h;
  252. if (seed_out)
  253. memcpy(seed_out, seed, qsize);
  254. }
  255. BN_CTX_end(ctx);
  256. BN_CTX_free(ctx);
  257. BN_MONT_CTX_free(mont);
  258. return ok;
  259. }
  260. /*
  261. * This is a parameter generation algorithm for the DSA2 algorithm as
  262. * described in FIPS 186-3.
  263. */
  264. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  265. const EVP_MD *evpmd, const unsigned char *seed_in,
  266. size_t seed_len, int idx, unsigned char *seed_out,
  267. int *counter_ret, unsigned long *h_ret,
  268. BN_GENCB *cb)
  269. {
  270. int ok = -1;
  271. unsigned char *seed = NULL, *seed_tmp = NULL;
  272. unsigned char md[EVP_MAX_MD_SIZE];
  273. int mdsize;
  274. BIGNUM *r0, *W, *X, *c, *test;
  275. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  276. BN_MONT_CTX *mont = NULL;
  277. int i, k, n = 0, m = 0, qsize = N >> 3;
  278. int counter = 0;
  279. int r = 0;
  280. BN_CTX *ctx = NULL;
  281. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  282. unsigned int h = 2;
  283. if (mctx == NULL)
  284. goto err;
  285. /* make sure L > N, otherwise we'll get trapped in an infinite loop */
  286. if (L <= N) {
  287. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  288. goto err;
  289. }
  290. if (evpmd == NULL) {
  291. if (N == 160)
  292. evpmd = EVP_sha1();
  293. else if (N == 224)
  294. evpmd = EVP_sha224();
  295. else
  296. evpmd = EVP_sha256();
  297. }
  298. mdsize = EVP_MD_size(evpmd);
  299. /* If unverifiable g generation only don't need seed */
  300. if (!ret->p || !ret->q || idx >= 0) {
  301. if (seed_len == 0)
  302. seed_len = mdsize;
  303. seed = OPENSSL_malloc(seed_len);
  304. if (seed_out)
  305. seed_tmp = seed_out;
  306. else
  307. seed_tmp = OPENSSL_malloc(seed_len);
  308. if (seed == NULL || seed_tmp == NULL)
  309. goto err;
  310. if (seed_in)
  311. memcpy(seed, seed_in, seed_len);
  312. }
  313. if ((ctx = BN_CTX_new()) == NULL)
  314. goto err;
  315. if ((mont = BN_MONT_CTX_new()) == NULL)
  316. goto err;
  317. BN_CTX_start(ctx);
  318. r0 = BN_CTX_get(ctx);
  319. g = BN_CTX_get(ctx);
  320. W = BN_CTX_get(ctx);
  321. X = BN_CTX_get(ctx);
  322. c = BN_CTX_get(ctx);
  323. test = BN_CTX_get(ctx);
  324. if (test == NULL)
  325. goto err;
  326. /* if p, q already supplied generate g only */
  327. if (ret->p && ret->q) {
  328. p = ret->p;
  329. q = ret->q;
  330. if (idx >= 0)
  331. memcpy(seed_tmp, seed, seed_len);
  332. goto g_only;
  333. } else {
  334. p = BN_CTX_get(ctx);
  335. q = BN_CTX_get(ctx);
  336. if (q == NULL)
  337. goto err;
  338. }
  339. if (!BN_lshift(test, BN_value_one(), L - 1))
  340. goto err;
  341. for (;;) {
  342. for (;;) { /* find q */
  343. unsigned char *pmd;
  344. /* step 1 */
  345. if (!BN_GENCB_call(cb, 0, m++))
  346. goto err;
  347. if (!seed_in) {
  348. if (RAND_bytes(seed, seed_len) <= 0)
  349. goto err;
  350. }
  351. /* step 2 */
  352. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  353. goto err;
  354. /* Take least significant bits of md */
  355. if (mdsize > qsize)
  356. pmd = md + mdsize - qsize;
  357. else
  358. pmd = md;
  359. if (mdsize < qsize)
  360. memset(md + mdsize, 0, qsize - mdsize);
  361. /* step 3 */
  362. pmd[0] |= 0x80;
  363. pmd[qsize - 1] |= 0x01;
  364. if (!BN_bin2bn(pmd, qsize, q))
  365. goto err;
  366. /* step 4 */
  367. r = BN_check_prime(q, ctx, cb);
  368. if (r > 0)
  369. break;
  370. if (r != 0)
  371. goto err;
  372. /* Provided seed didn't produce a prime: error */
  373. if (seed_in) {
  374. ok = 0;
  375. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  376. goto err;
  377. }
  378. /* do a callback call */
  379. /* step 5 */
  380. }
  381. /* Copy seed to seed_out before we mess with it */
  382. if (seed_out)
  383. memcpy(seed_out, seed, seed_len);
  384. if (!BN_GENCB_call(cb, 2, 0))
  385. goto err;
  386. if (!BN_GENCB_call(cb, 3, 0))
  387. goto err;
  388. /* step 6 */
  389. counter = 0;
  390. /* "offset = 1" */
  391. n = (L - 1) / (mdsize << 3);
  392. for (;;) {
  393. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  394. goto err;
  395. /* step 7 */
  396. BN_zero(W);
  397. /* now 'buf' contains "SEED + offset - 1" */
  398. for (k = 0; k <= n; k++) {
  399. /*
  400. * obtain "SEED + offset + k" by incrementing:
  401. */
  402. for (i = seed_len - 1; i >= 0; i--) {
  403. seed[i]++;
  404. if (seed[i] != 0)
  405. break;
  406. }
  407. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  408. goto err;
  409. /* step 8 */
  410. if (!BN_bin2bn(md, mdsize, r0))
  411. goto err;
  412. if (!BN_lshift(r0, r0, (mdsize << 3) * k))
  413. goto err;
  414. if (!BN_add(W, W, r0))
  415. goto err;
  416. }
  417. /* more of step 8 */
  418. if (!BN_mask_bits(W, L - 1))
  419. goto err;
  420. if (!BN_copy(X, W))
  421. goto err;
  422. if (!BN_add(X, X, test))
  423. goto err;
  424. /* step 9 */
  425. if (!BN_lshift1(r0, q))
  426. goto err;
  427. if (!BN_mod(c, X, r0, ctx))
  428. goto err;
  429. if (!BN_sub(r0, c, BN_value_one()))
  430. goto err;
  431. if (!BN_sub(p, X, r0))
  432. goto err;
  433. /* step 10 */
  434. if (BN_cmp(p, test) >= 0) {
  435. /* step 11 */
  436. r = BN_check_prime(p, ctx, cb);
  437. if (r > 0)
  438. goto end; /* found it */
  439. if (r != 0)
  440. goto err;
  441. }
  442. /* step 13 */
  443. counter++;
  444. /* "offset = offset + n + 1" */
  445. /* step 14 */
  446. if (counter >= (int)(4 * L))
  447. break;
  448. }
  449. if (seed_in) {
  450. ok = 0;
  451. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  452. goto err;
  453. }
  454. }
  455. end:
  456. if (!BN_GENCB_call(cb, 2, 1))
  457. goto err;
  458. g_only:
  459. /* We now need to generate g */
  460. /* Set r0=(p-1)/q */
  461. if (!BN_sub(test, p, BN_value_one()))
  462. goto err;
  463. if (!BN_div(r0, NULL, test, q, ctx))
  464. goto err;
  465. if (idx < 0) {
  466. if (!BN_set_word(test, h))
  467. goto err;
  468. } else
  469. h = 1;
  470. if (!BN_MONT_CTX_set(mont, p, ctx))
  471. goto err;
  472. for (;;) {
  473. static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
  474. if (idx >= 0) {
  475. md[0] = idx & 0xff;
  476. md[1] = (h >> 8) & 0xff;
  477. md[2] = h & 0xff;
  478. if (!EVP_DigestInit_ex(mctx, evpmd, NULL))
  479. goto err;
  480. if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))
  481. goto err;
  482. if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))
  483. goto err;
  484. if (!EVP_DigestUpdate(mctx, md, 3))
  485. goto err;
  486. if (!EVP_DigestFinal_ex(mctx, md, NULL))
  487. goto err;
  488. if (!BN_bin2bn(md, mdsize, test))
  489. goto err;
  490. }
  491. /* g=test^r0%p */
  492. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  493. goto err;
  494. if (!BN_is_one(g))
  495. break;
  496. if (idx < 0 && !BN_add(test, test, BN_value_one()))
  497. goto err;
  498. h++;
  499. if (idx >= 0 && h > 0xffff)
  500. goto err;
  501. }
  502. if (!BN_GENCB_call(cb, 3, 1))
  503. goto err;
  504. ok = 1;
  505. err:
  506. if (ok == 1) {
  507. if (p != ret->p) {
  508. BN_free(ret->p);
  509. ret->p = BN_dup(p);
  510. }
  511. if (q != ret->q) {
  512. BN_free(ret->q);
  513. ret->q = BN_dup(q);
  514. }
  515. BN_free(ret->g);
  516. ret->g = BN_dup(g);
  517. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  518. ok = -1;
  519. goto err;
  520. }
  521. ret->dirty_cnt++;
  522. if (counter_ret != NULL)
  523. *counter_ret = counter;
  524. if (h_ret != NULL)
  525. *h_ret = h;
  526. }
  527. OPENSSL_free(seed);
  528. if (seed_out != seed_tmp)
  529. OPENSSL_free(seed_tmp);
  530. BN_CTX_end(ctx);
  531. BN_CTX_free(ctx);
  532. BN_MONT_CTX_free(mont);
  533. EVP_MD_CTX_free(mctx);
  534. return ok;
  535. }