2
0

dsa_gen.c 16 KB

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