dsa_gen.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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_locl.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. if (ctx)
  256. BN_CTX_end(ctx);
  257. BN_CTX_free(ctx);
  258. BN_MONT_CTX_free(mont);
  259. return ok;
  260. }
  261. /*
  262. * This is a parameter generation algorithm for the DSA2 algorithm as
  263. * described in FIPS 186-3.
  264. */
  265. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  266. const EVP_MD *evpmd, const unsigned char *seed_in,
  267. size_t seed_len, int idx, unsigned char *seed_out,
  268. int *counter_ret, unsigned long *h_ret,
  269. BN_GENCB *cb)
  270. {
  271. int ok = -1;
  272. unsigned char *seed = NULL, *seed_tmp = NULL;
  273. unsigned char md[EVP_MAX_MD_SIZE];
  274. int mdsize;
  275. BIGNUM *r0, *W, *X, *c, *test;
  276. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  277. BN_MONT_CTX *mont = NULL;
  278. int i, k, n = 0, m = 0, qsize = N >> 3;
  279. int counter = 0;
  280. int r = 0;
  281. BN_CTX *ctx = NULL;
  282. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  283. unsigned int h = 2;
  284. if (mctx == NULL)
  285. goto err;
  286. /* make sure L > N, otherwise we'll get trapped in an infinite loop */
  287. if (L <= N) {
  288. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  289. goto err;
  290. }
  291. if (evpmd == NULL) {
  292. if (N == 160)
  293. evpmd = EVP_sha1();
  294. else if (N == 224)
  295. evpmd = EVP_sha224();
  296. else
  297. evpmd = EVP_sha256();
  298. }
  299. mdsize = EVP_MD_size(evpmd);
  300. /* If unverifiable g generation only don't need seed */
  301. if (!ret->p || !ret->q || idx >= 0) {
  302. if (seed_len == 0)
  303. seed_len = mdsize;
  304. seed = OPENSSL_malloc(seed_len);
  305. if (seed_out)
  306. seed_tmp = seed_out;
  307. else
  308. seed_tmp = OPENSSL_malloc(seed_len);
  309. if (seed == NULL || seed_tmp == NULL)
  310. goto err;
  311. if (seed_in)
  312. memcpy(seed, seed_in, seed_len);
  313. }
  314. if ((ctx = BN_CTX_new()) == NULL)
  315. goto err;
  316. if ((mont = BN_MONT_CTX_new()) == NULL)
  317. goto err;
  318. BN_CTX_start(ctx);
  319. r0 = BN_CTX_get(ctx);
  320. g = BN_CTX_get(ctx);
  321. W = BN_CTX_get(ctx);
  322. X = BN_CTX_get(ctx);
  323. c = BN_CTX_get(ctx);
  324. test = BN_CTX_get(ctx);
  325. if (test == NULL)
  326. goto err;
  327. /* if p, q already supplied generate g only */
  328. if (ret->p && ret->q) {
  329. p = ret->p;
  330. q = ret->q;
  331. if (idx >= 0)
  332. memcpy(seed_tmp, seed, seed_len);
  333. goto g_only;
  334. } else {
  335. p = BN_CTX_get(ctx);
  336. q = BN_CTX_get(ctx);
  337. if (q == NULL)
  338. goto err;
  339. }
  340. if (!BN_lshift(test, BN_value_one(), L - 1))
  341. goto err;
  342. for (;;) {
  343. for (;;) { /* find q */
  344. unsigned char *pmd;
  345. /* step 1 */
  346. if (!BN_GENCB_call(cb, 0, m++))
  347. goto err;
  348. if (!seed_in) {
  349. if (RAND_bytes(seed, seed_len) <= 0)
  350. goto err;
  351. }
  352. /* step 2 */
  353. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  354. goto err;
  355. /* Take least significant bits of md */
  356. if (mdsize > qsize)
  357. pmd = md + mdsize - qsize;
  358. else
  359. pmd = md;
  360. if (mdsize < qsize)
  361. memset(md + mdsize, 0, qsize - mdsize);
  362. /* step 3 */
  363. pmd[0] |= 0x80;
  364. pmd[qsize - 1] |= 0x01;
  365. if (!BN_bin2bn(pmd, qsize, q))
  366. goto err;
  367. /* step 4 */
  368. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
  369. seed_in ? 1 : 0, cb);
  370. if (r > 0)
  371. break;
  372. if (r != 0)
  373. goto err;
  374. /* Provided seed didn't produce a prime: error */
  375. if (seed_in) {
  376. ok = 0;
  377. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
  378. goto err;
  379. }
  380. /* do a callback call */
  381. /* step 5 */
  382. }
  383. /* Copy seed to seed_out before we mess with it */
  384. if (seed_out)
  385. memcpy(seed_out, seed, seed_len);
  386. if (!BN_GENCB_call(cb, 2, 0))
  387. goto err;
  388. if (!BN_GENCB_call(cb, 3, 0))
  389. goto err;
  390. /* step 6 */
  391. counter = 0;
  392. /* "offset = 1" */
  393. n = (L - 1) / (mdsize << 3);
  394. for (;;) {
  395. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
  396. goto err;
  397. /* step 7 */
  398. BN_zero(W);
  399. /* now 'buf' contains "SEED + offset - 1" */
  400. for (k = 0; k <= n; k++) {
  401. /*
  402. * obtain "SEED + offset + k" by incrementing:
  403. */
  404. for (i = seed_len - 1; i >= 0; i--) {
  405. seed[i]++;
  406. if (seed[i] != 0)
  407. break;
  408. }
  409. if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
  410. goto err;
  411. /* step 8 */
  412. if (!BN_bin2bn(md, mdsize, r0))
  413. goto err;
  414. if (!BN_lshift(r0, r0, (mdsize << 3) * k))
  415. goto err;
  416. if (!BN_add(W, W, r0))
  417. goto err;
  418. }
  419. /* more of step 8 */
  420. if (!BN_mask_bits(W, L - 1))
  421. goto err;
  422. if (!BN_copy(X, W))
  423. goto err;
  424. if (!BN_add(X, X, test))
  425. goto err;
  426. /* step 9 */
  427. if (!BN_lshift1(r0, q))
  428. goto err;
  429. if (!BN_mod(c, X, r0, ctx))
  430. goto err;
  431. if (!BN_sub(r0, c, BN_value_one()))
  432. goto err;
  433. if (!BN_sub(p, X, r0))
  434. goto err;
  435. /* step 10 */
  436. if (BN_cmp(p, test) >= 0) {
  437. /* step 11 */
  438. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  439. if (r > 0)
  440. goto end; /* found it */
  441. if (r != 0)
  442. goto err;
  443. }
  444. /* step 13 */
  445. counter++;
  446. /* "offset = offset + n + 1" */
  447. /* step 14 */
  448. if (counter >= (int)(4 * L))
  449. break;
  450. }
  451. if (seed_in) {
  452. ok = 0;
  453. DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
  454. goto err;
  455. }
  456. }
  457. end:
  458. if (!BN_GENCB_call(cb, 2, 1))
  459. goto err;
  460. g_only:
  461. /* We now need to generate g */
  462. /* Set r0=(p-1)/q */
  463. if (!BN_sub(test, p, BN_value_one()))
  464. goto err;
  465. if (!BN_div(r0, NULL, test, q, ctx))
  466. goto err;
  467. if (idx < 0) {
  468. if (!BN_set_word(test, h))
  469. goto err;
  470. } else
  471. h = 1;
  472. if (!BN_MONT_CTX_set(mont, p, ctx))
  473. goto err;
  474. for (;;) {
  475. static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
  476. if (idx >= 0) {
  477. md[0] = idx & 0xff;
  478. md[1] = (h >> 8) & 0xff;
  479. md[2] = h & 0xff;
  480. if (!EVP_DigestInit_ex(mctx, evpmd, NULL))
  481. goto err;
  482. if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))
  483. goto err;
  484. if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))
  485. goto err;
  486. if (!EVP_DigestUpdate(mctx, md, 3))
  487. goto err;
  488. if (!EVP_DigestFinal_ex(mctx, md, NULL))
  489. goto err;
  490. if (!BN_bin2bn(md, mdsize, test))
  491. goto err;
  492. }
  493. /* g=test^r0%p */
  494. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
  495. goto err;
  496. if (!BN_is_one(g))
  497. break;
  498. if (idx < 0 && !BN_add(test, test, BN_value_one()))
  499. goto err;
  500. h++;
  501. if (idx >= 0 && h > 0xffff)
  502. goto err;
  503. }
  504. if (!BN_GENCB_call(cb, 3, 1))
  505. goto err;
  506. ok = 1;
  507. err:
  508. if (ok == 1) {
  509. if (p != ret->p) {
  510. BN_free(ret->p);
  511. ret->p = BN_dup(p);
  512. }
  513. if (q != ret->q) {
  514. BN_free(ret->q);
  515. ret->q = BN_dup(q);
  516. }
  517. BN_free(ret->g);
  518. ret->g = BN_dup(g);
  519. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  520. ok = -1;
  521. goto err;
  522. }
  523. if (counter_ret != NULL)
  524. *counter_ret = counter;
  525. if (h_ret != NULL)
  526. *h_ret = h;
  527. }
  528. OPENSSL_free(seed);
  529. if (seed_out != seed_tmp)
  530. OPENSSL_free(seed_tmp);
  531. if (ctx)
  532. BN_CTX_end(ctx);
  533. BN_CTX_free(ctx);
  534. BN_MONT_CTX_free(mont);
  535. EVP_MD_CTX_free(mctx);
  536. return ok;
  537. }