2
0

dsa_gen.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  136. use_random_seed, cb);
  137. if (r > 0)
  138. break;
  139. if (r != 0)
  140. goto err;
  141. /* do a callback call */
  142. /* step 5 */
  143. }
  144. if (!BN_GENCB_call(cb, 2, 0))
  145. goto err;
  146. if (!BN_GENCB_call(cb, 3, 0))
  147. goto err;
  148. /* step 6 */
  149. counter = 0;
  150. /* "offset = 2" */
  151. n = (bits - 1) / 160;
  152. for (;;) {
  153. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  154. goto err;
  155. /* step 7 */
  156. BN_zero(W);
  157. /* now 'buf' contains "SEED + offset - 1" */
  158. for (k = 0; k <= n; k++) {
  159. /*
  160. * obtain "SEED + offset + k" by incrementing:
  161. */
  162. for (i = qsize - 1; i >= 0; i--) {
  163. buf[i]++;
  164. if (buf[i] != 0)
  165. break;
  166. }
  167. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
  168. goto err;
  169. /* step 8 */
  170. if (!BN_bin2bn(md, qsize, r0))
  171. goto err;
  172. if (!BN_lshift(r0, r0, (qsize << 3) * k))
  173. goto err;
  174. if (!BN_add(W, W, r0))
  175. goto err;
  176. }
  177. /* more of step 8 */
  178. if (!BN_mask_bits(W, bits - 1))
  179. goto err;
  180. if (!BN_copy(X, W))
  181. goto err;
  182. if (!BN_add(X, X, test))
  183. goto err;
  184. /* step 9 */
  185. if (!BN_lshift1(r0, q))
  186. goto err;
  187. if (!BN_mod(c, X, r0, ctx))
  188. goto err;
  189. if (!BN_sub(r0, c, BN_value_one()))
  190. goto err;
  191. if (!BN_sub(p, X, r0))
  192. goto err;
  193. /* step 10 */
  194. if (BN_cmp(p, test) >= 0) {
  195. /* step 11 */
  196. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  197. if (r > 0)
  198. goto end; /* found it */
  199. if (r != 0)
  200. goto err;
  201. }
  202. /* step 13 */
  203. counter++;
  204. /* "offset = offset + n + 1" */
  205. /* step 14 */
  206. if (counter >= 4096)
  207. break;
  208. }
  209. }
  210. end:
  211. if (!BN_GENCB_call(cb, 2, 1))
  212. goto err;
  213. /* We now need to generate g */
  214. /* Set r0=(p-1)/q */
  215. if (!BN_sub(test, p, BN_value_one()))
  216. goto err;
  217. if (!BN_div(r0, NULL, test, q, ctx))
  218. goto err;
  219. if (!BN_set_word(test, h))
  220. goto err;
  221. if (!BN_MONT_CTX_set(mont, p, ctx))
  222. goto err;
  223. for (;;) {
  224. /* g=test^r0%p */
  225. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  226. goto err;
  227. if (!BN_is_one(g))
  228. break;
  229. if (!BN_add(test, test, BN_value_one()))
  230. goto err;
  231. h++;
  232. }
  233. if (!BN_GENCB_call(cb, 3, 1))
  234. goto err;
  235. ok = 1;
  236. err:
  237. if (ok) {
  238. BN_free(ret->p);
  239. BN_free(ret->q);
  240. BN_free(ret->g);
  241. ret->p = BN_dup(p);
  242. ret->q = BN_dup(q);
  243. ret->g = BN_dup(g);
  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_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  368. seed_in ? 1 : 0, cb);
  369. if (r > 0)
  370. break;
  371. if (r != 0)
  372. goto err;
  373. /* Provided seed didn't produce a prime: error */
  374. if (seed_in) {
  375. ok = 0;
  376. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  377. goto err;
  378. }
  379. /* do a callback call */
  380. /* step 5 */
  381. }
  382. /* Copy seed to seed_out before we mess with it */
  383. if (seed_out)
  384. memcpy(seed_out, seed, seed_len);
  385. if (!BN_GENCB_call(cb, 2, 0))
  386. goto err;
  387. if (!BN_GENCB_call(cb, 3, 0))
  388. goto err;
  389. /* step 6 */
  390. counter = 0;
  391. /* "offset = 1" */
  392. n = (L - 1) / (mdsize << 3);
  393. for (;;) {
  394. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  395. goto err;
  396. /* step 7 */
  397. BN_zero(W);
  398. /* now 'buf' contains "SEED + offset - 1" */
  399. for (k = 0; k <= n; k++) {
  400. /*
  401. * obtain "SEED + offset + k" by incrementing:
  402. */
  403. for (i = seed_len - 1; i >= 0; i--) {
  404. seed[i]++;
  405. if (seed[i] != 0)
  406. break;
  407. }
  408. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  409. goto err;
  410. /* step 8 */
  411. if (!BN_bin2bn(md, mdsize, r0))
  412. goto err;
  413. if (!BN_lshift(r0, r0, (mdsize << 3) * k))
  414. goto err;
  415. if (!BN_add(W, W, r0))
  416. goto err;
  417. }
  418. /* more of step 8 */
  419. if (!BN_mask_bits(W, L - 1))
  420. goto err;
  421. if (!BN_copy(X, W))
  422. goto err;
  423. if (!BN_add(X, X, test))
  424. goto err;
  425. /* step 9 */
  426. if (!BN_lshift1(r0, q))
  427. goto err;
  428. if (!BN_mod(c, X, r0, ctx))
  429. goto err;
  430. if (!BN_sub(r0, c, BN_value_one()))
  431. goto err;
  432. if (!BN_sub(p, X, r0))
  433. goto err;
  434. /* step 10 */
  435. if (BN_cmp(p, test) >= 0) {
  436. /* step 11 */
  437. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  438. if (r > 0)
  439. goto end; /* found it */
  440. if (r != 0)
  441. goto err;
  442. }
  443. /* step 13 */
  444. counter++;
  445. /* "offset = offset + n + 1" */
  446. /* step 14 */
  447. if (counter >= (int)(4 * L))
  448. break;
  449. }
  450. if (seed_in) {
  451. ok = 0;
  452. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  453. goto err;
  454. }
  455. }
  456. end:
  457. if (!BN_GENCB_call(cb, 2, 1))
  458. goto err;
  459. g_only:
  460. /* We now need to generate g */
  461. /* Set r0=(p-1)/q */
  462. if (!BN_sub(test, p, BN_value_one()))
  463. goto err;
  464. if (!BN_div(r0, NULL, test, q, ctx))
  465. goto err;
  466. if (idx < 0) {
  467. if (!BN_set_word(test, h))
  468. goto err;
  469. } else
  470. h = 1;
  471. if (!BN_MONT_CTX_set(mont, p, ctx))
  472. goto err;
  473. for (;;) {
  474. static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
  475. if (idx >= 0) {
  476. md[0] = idx & 0xff;
  477. md[1] = (h >> 8) & 0xff;
  478. md[2] = h & 0xff;
  479. if (!EVP_DigestInit_ex(mctx, evpmd, NULL))
  480. goto err;
  481. if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))
  482. goto err;
  483. if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))
  484. goto err;
  485. if (!EVP_DigestUpdate(mctx, md, 3))
  486. goto err;
  487. if (!EVP_DigestFinal_ex(mctx, md, NULL))
  488. goto err;
  489. if (!BN_bin2bn(md, mdsize, test))
  490. goto err;
  491. }
  492. /* g=test^r0%p */
  493. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  494. goto err;
  495. if (!BN_is_one(g))
  496. break;
  497. if (idx < 0 && !BN_add(test, test, BN_value_one()))
  498. goto err;
  499. h++;
  500. if (idx >= 0 && h > 0xffff)
  501. goto err;
  502. }
  503. if (!BN_GENCB_call(cb, 3, 1))
  504. goto err;
  505. ok = 1;
  506. err:
  507. if (ok == 1) {
  508. if (p != ret->p) {
  509. BN_free(ret->p);
  510. ret->p = BN_dup(p);
  511. }
  512. if (q != ret->q) {
  513. BN_free(ret->q);
  514. ret->q = BN_dup(q);
  515. }
  516. BN_free(ret->g);
  517. ret->g = BN_dup(g);
  518. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  519. ok = -1;
  520. goto err;
  521. }
  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. }