dh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* $OpenBSD: dh.c,v 1.71 2019/09/06 06:08:11 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000 Niels Provos. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #ifdef WITH_OPENSSL
  27. #include <errno.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <limits.h>
  33. #include <openssl/bn.h>
  34. #include <openssl/dh.h>
  35. #include "dh.h"
  36. #include "pathnames.h"
  37. #include "log.h"
  38. #include "misc.h"
  39. #include "ssherr.h"
  40. #include "openbsd-compat/openssl-compat.h"
  41. static int
  42. parse_prime(int linenum, char *line, struct dhgroup *dhg)
  43. {
  44. char *cp, *arg;
  45. char *strsize, *gen, *prime;
  46. const char *errstr = NULL;
  47. long long n;
  48. dhg->p = dhg->g = NULL;
  49. cp = line;
  50. if ((arg = strdelim(&cp)) == NULL)
  51. return 0;
  52. /* Ignore leading whitespace */
  53. if (*arg == '\0')
  54. arg = strdelim(&cp);
  55. if (!arg || !*arg || *arg == '#')
  56. return 0;
  57. /* time */
  58. if (cp == NULL || *arg == '\0')
  59. goto truncated;
  60. arg = strsep(&cp, " "); /* type */
  61. if (cp == NULL || *arg == '\0')
  62. goto truncated;
  63. /* Ensure this is a safe prime */
  64. n = strtonum(arg, 0, 5, &errstr);
  65. if (errstr != NULL || n != MODULI_TYPE_SAFE) {
  66. error("moduli:%d: type is not %d", linenum, MODULI_TYPE_SAFE);
  67. goto fail;
  68. }
  69. arg = strsep(&cp, " "); /* tests */
  70. if (cp == NULL || *arg == '\0')
  71. goto truncated;
  72. /* Ensure prime has been tested and is not composite */
  73. n = strtonum(arg, 0, 0x1f, &errstr);
  74. if (errstr != NULL ||
  75. (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE)) {
  76. error("moduli:%d: invalid moduli tests flag", linenum);
  77. goto fail;
  78. }
  79. arg = strsep(&cp, " "); /* tries */
  80. if (cp == NULL || *arg == '\0')
  81. goto truncated;
  82. n = strtonum(arg, 0, 1<<30, &errstr);
  83. if (errstr != NULL || n == 0) {
  84. error("moduli:%d: invalid primality trial count", linenum);
  85. goto fail;
  86. }
  87. strsize = strsep(&cp, " "); /* size */
  88. if (cp == NULL || *strsize == '\0' ||
  89. (dhg->size = (int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
  90. errstr) {
  91. error("moduli:%d: invalid prime length", linenum);
  92. goto fail;
  93. }
  94. /* The whole group is one bit larger */
  95. dhg->size++;
  96. gen = strsep(&cp, " "); /* gen */
  97. if (cp == NULL || *gen == '\0')
  98. goto truncated;
  99. prime = strsep(&cp, " "); /* prime */
  100. if (cp != NULL || *prime == '\0') {
  101. truncated:
  102. error("moduli:%d: truncated", linenum);
  103. goto fail;
  104. }
  105. if ((dhg->g = BN_new()) == NULL ||
  106. (dhg->p = BN_new()) == NULL) {
  107. error("parse_prime: BN_new failed");
  108. goto fail;
  109. }
  110. if (BN_hex2bn(&dhg->g, gen) == 0) {
  111. error("moduli:%d: could not parse generator value", linenum);
  112. goto fail;
  113. }
  114. if (BN_hex2bn(&dhg->p, prime) == 0) {
  115. error("moduli:%d: could not parse prime value", linenum);
  116. goto fail;
  117. }
  118. if (BN_num_bits(dhg->p) != dhg->size) {
  119. error("moduli:%d: prime has wrong size: actual %d listed %d",
  120. linenum, BN_num_bits(dhg->p), dhg->size - 1);
  121. goto fail;
  122. }
  123. if (BN_cmp(dhg->g, BN_value_one()) <= 0) {
  124. error("moduli:%d: generator is invalid", linenum);
  125. goto fail;
  126. }
  127. return 1;
  128. fail:
  129. BN_clear_free(dhg->g);
  130. BN_clear_free(dhg->p);
  131. dhg->g = dhg->p = NULL;
  132. return 0;
  133. }
  134. DH *
  135. choose_dh(int min, int wantbits, int max)
  136. {
  137. FILE *f;
  138. char *line = NULL;
  139. size_t linesize = 0;
  140. int best, bestcount, which, linenum;
  141. struct dhgroup dhg;
  142. if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
  143. logit("WARNING: could not open %s (%s), using fixed modulus",
  144. _PATH_DH_MODULI, strerror(errno));
  145. return (dh_new_group_fallback(max));
  146. }
  147. linenum = 0;
  148. best = bestcount = 0;
  149. while (getline(&line, &linesize, f) != -1) {
  150. linenum++;
  151. if (!parse_prime(linenum, line, &dhg))
  152. continue;
  153. BN_clear_free(dhg.g);
  154. BN_clear_free(dhg.p);
  155. if (dhg.size > max || dhg.size < min)
  156. continue;
  157. if ((dhg.size > wantbits && dhg.size < best) ||
  158. (dhg.size > best && best < wantbits)) {
  159. best = dhg.size;
  160. bestcount = 0;
  161. }
  162. if (dhg.size == best)
  163. bestcount++;
  164. }
  165. free(line);
  166. line = NULL;
  167. linesize = 0;
  168. rewind(f);
  169. if (bestcount == 0) {
  170. fclose(f);
  171. logit("WARNING: no suitable primes in %s", _PATH_DH_MODULI);
  172. return (dh_new_group_fallback(max));
  173. }
  174. which = arc4random_uniform(bestcount);
  175. linenum = 0;
  176. bestcount = 0;
  177. while (getline(&line, &linesize, f) != -1) {
  178. linenum++;
  179. if (!parse_prime(linenum, line, &dhg))
  180. continue;
  181. if ((dhg.size > max || dhg.size < min) ||
  182. dhg.size != best ||
  183. bestcount++ != which) {
  184. BN_clear_free(dhg.g);
  185. BN_clear_free(dhg.p);
  186. continue;
  187. }
  188. break;
  189. }
  190. free(line);
  191. line = NULL;
  192. fclose(f);
  193. if (bestcount != which + 1) {
  194. logit("WARNING: selected prime disappeared in %s, giving up",
  195. _PATH_DH_MODULI);
  196. return (dh_new_group_fallback(max));
  197. }
  198. return (dh_new_group(dhg.g, dhg.p));
  199. }
  200. /* diffie-hellman-groupN-sha1 */
  201. int
  202. dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
  203. {
  204. int i;
  205. int n = BN_num_bits(dh_pub);
  206. int bits_set = 0;
  207. BIGNUM *tmp;
  208. const BIGNUM *dh_p;
  209. DH_get0_pqg(dh, &dh_p, NULL, NULL);
  210. if (BN_is_negative(dh_pub)) {
  211. logit("invalid public DH value: negative");
  212. return 0;
  213. }
  214. if (BN_cmp(dh_pub, BN_value_one()) != 1) { /* pub_exp <= 1 */
  215. logit("invalid public DH value: <= 1");
  216. return 0;
  217. }
  218. if ((tmp = BN_new()) == NULL) {
  219. error("%s: BN_new failed", __func__);
  220. return 0;
  221. }
  222. if (!BN_sub(tmp, dh_p, BN_value_one()) ||
  223. BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */
  224. BN_clear_free(tmp);
  225. logit("invalid public DH value: >= p-1");
  226. return 0;
  227. }
  228. BN_clear_free(tmp);
  229. for (i = 0; i <= n; i++)
  230. if (BN_is_bit_set(dh_pub, i))
  231. bits_set++;
  232. debug2("bits set: %d/%d", bits_set, BN_num_bits(dh_p));
  233. /*
  234. * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
  235. */
  236. if (bits_set < 4) {
  237. logit("invalid public DH value (%d/%d)",
  238. bits_set, BN_num_bits(dh_p));
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. int
  244. dh_gen_key(DH *dh, int need)
  245. {
  246. int pbits;
  247. const BIGNUM *dh_p, *pub_key;
  248. DH_get0_pqg(dh, &dh_p, NULL, NULL);
  249. if (need < 0 || dh_p == NULL ||
  250. (pbits = BN_num_bits(dh_p)) <= 0 ||
  251. need > INT_MAX / 2 || 2 * need > pbits)
  252. return SSH_ERR_INVALID_ARGUMENT;
  253. if (need < 256)
  254. need = 256;
  255. /*
  256. * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
  257. * so double requested need here.
  258. */
  259. if (!DH_set_length(dh, MINIMUM(need * 2, pbits - 1))) {
  260. debug("(!DH_set_length(dh, MINIMUM(need * 2, pbits - 1)))");
  261. return SSH_ERR_LIBCRYPTO_ERROR;
  262. }
  263. if (DH_generate_key(dh) == 0) {
  264. debug("(DH_generate_key(dh) == 0)");
  265. return SSH_ERR_LIBCRYPTO_ERROR;
  266. }
  267. DH_get0_key(dh, &pub_key, NULL);
  268. if (!dh_pub_is_valid(dh, pub_key)) {
  269. debug("(!dh_pub_is_valid(dh, pub_key))");
  270. return SSH_ERR_INVALID_FORMAT;
  271. }
  272. return 0;
  273. }
  274. DH *
  275. dh_new_group_asc(const char *gen, const char *modulus)
  276. {
  277. DH *dh;
  278. BIGNUM *dh_p = NULL, *dh_g = NULL;
  279. if ((dh = DH_new()) == NULL)
  280. return NULL;
  281. if (BN_hex2bn(&dh_p, modulus) == 0 ||
  282. BN_hex2bn(&dh_g, gen) == 0)
  283. goto fail;
  284. if (!DH_set0_pqg(dh, dh_p, NULL, dh_g))
  285. goto fail;
  286. return dh;
  287. fail:
  288. DH_free(dh);
  289. BN_clear_free(dh_p);
  290. BN_clear_free(dh_g);
  291. return NULL;
  292. }
  293. /*
  294. * This just returns the group, we still need to generate the exchange
  295. * value.
  296. */
  297. DH *
  298. dh_new_group(BIGNUM *gen, BIGNUM *modulus)
  299. {
  300. DH *dh;
  301. if ((dh = DH_new()) == NULL)
  302. return NULL;
  303. if (!DH_set0_pqg(dh, modulus, NULL, gen)) {
  304. DH_free(dh);
  305. return NULL;
  306. }
  307. return dh;
  308. }
  309. /* rfc2409 "Second Oakley Group" (1024 bits) */
  310. DH *
  311. dh_new_group1(void)
  312. {
  313. static char *gen = "2", *group1 =
  314. "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
  315. "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
  316. "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
  317. "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
  318. "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
  319. "FFFFFFFF" "FFFFFFFF";
  320. return (dh_new_group_asc(gen, group1));
  321. }
  322. /* rfc3526 group 14 "2048-bit MODP Group" */
  323. DH *
  324. dh_new_group14(void)
  325. {
  326. static char *gen = "2", *group14 =
  327. "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
  328. "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
  329. "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
  330. "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
  331. "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
  332. "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
  333. "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
  334. "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
  335. "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
  336. "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
  337. "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF";
  338. return (dh_new_group_asc(gen, group14));
  339. }
  340. /* rfc3526 group 16 "4096-bit MODP Group" */
  341. DH *
  342. dh_new_group16(void)
  343. {
  344. static char *gen = "2", *group16 =
  345. "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
  346. "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
  347. "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
  348. "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
  349. "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
  350. "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
  351. "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
  352. "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
  353. "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
  354. "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
  355. "15728E5A" "8AAAC42D" "AD33170D" "04507A33" "A85521AB" "DF1CBA64"
  356. "ECFB8504" "58DBEF0A" "8AEA7157" "5D060C7D" "B3970F85" "A6E1E4C7"
  357. "ABF5AE8C" "DB0933D7" "1E8C94E0" "4A25619D" "CEE3D226" "1AD2EE6B"
  358. "F12FFA06" "D98A0864" "D8760273" "3EC86A64" "521F2B18" "177B200C"
  359. "BBE11757" "7A615D6C" "770988C0" "BAD946E2" "08E24FA0" "74E5AB31"
  360. "43DB5BFC" "E0FD108E" "4B82D120" "A9210801" "1A723C12" "A787E6D7"
  361. "88719A10" "BDBA5B26" "99C32718" "6AF4E23C" "1A946834" "B6150BDA"
  362. "2583E9CA" "2AD44CE8" "DBBBC2DB" "04DE8EF9" "2E8EFC14" "1FBECAA6"
  363. "287C5947" "4E6BC05D" "99B2964F" "A090C3A2" "233BA186" "515BE7ED"
  364. "1F612970" "CEE2D7AF" "B81BDD76" "2170481C" "D0069127" "D5B05AA9"
  365. "93B4EA98" "8D8FDDC1" "86FFB7DC" "90A6C08F" "4DF435C9" "34063199"
  366. "FFFFFFFF" "FFFFFFFF";
  367. return (dh_new_group_asc(gen, group16));
  368. }
  369. /* rfc3526 group 18 "8192-bit MODP Group" */
  370. DH *
  371. dh_new_group18(void)
  372. {
  373. static char *gen = "2", *group18 =
  374. "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
  375. "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
  376. "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
  377. "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
  378. "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
  379. "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
  380. "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
  381. "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
  382. "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
  383. "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
  384. "15728E5A" "8AAAC42D" "AD33170D" "04507A33" "A85521AB" "DF1CBA64"
  385. "ECFB8504" "58DBEF0A" "8AEA7157" "5D060C7D" "B3970F85" "A6E1E4C7"
  386. "ABF5AE8C" "DB0933D7" "1E8C94E0" "4A25619D" "CEE3D226" "1AD2EE6B"
  387. "F12FFA06" "D98A0864" "D8760273" "3EC86A64" "521F2B18" "177B200C"
  388. "BBE11757" "7A615D6C" "770988C0" "BAD946E2" "08E24FA0" "74E5AB31"
  389. "43DB5BFC" "E0FD108E" "4B82D120" "A9210801" "1A723C12" "A787E6D7"
  390. "88719A10" "BDBA5B26" "99C32718" "6AF4E23C" "1A946834" "B6150BDA"
  391. "2583E9CA" "2AD44CE8" "DBBBC2DB" "04DE8EF9" "2E8EFC14" "1FBECAA6"
  392. "287C5947" "4E6BC05D" "99B2964F" "A090C3A2" "233BA186" "515BE7ED"
  393. "1F612970" "CEE2D7AF" "B81BDD76" "2170481C" "D0069127" "D5B05AA9"
  394. "93B4EA98" "8D8FDDC1" "86FFB7DC" "90A6C08F" "4DF435C9" "34028492"
  395. "36C3FAB4" "D27C7026" "C1D4DCB2" "602646DE" "C9751E76" "3DBA37BD"
  396. "F8FF9406" "AD9E530E" "E5DB382F" "413001AE" "B06A53ED" "9027D831"
  397. "179727B0" "865A8918" "DA3EDBEB" "CF9B14ED" "44CE6CBA" "CED4BB1B"
  398. "DB7F1447" "E6CC254B" "33205151" "2BD7AF42" "6FB8F401" "378CD2BF"
  399. "5983CA01" "C64B92EC" "F032EA15" "D1721D03" "F482D7CE" "6E74FEF6"
  400. "D55E702F" "46980C82" "B5A84031" "900B1C9E" "59E7C97F" "BEC7E8F3"
  401. "23A97A7E" "36CC88BE" "0F1D45B7" "FF585AC5" "4BD407B2" "2B4154AA"
  402. "CC8F6D7E" "BF48E1D8" "14CC5ED2" "0F8037E0" "A79715EE" "F29BE328"
  403. "06A1D58B" "B7C5DA76" "F550AA3D" "8A1FBFF0" "EB19CCB1" "A313D55C"
  404. "DA56C9EC" "2EF29632" "387FE8D7" "6E3C0468" "043E8F66" "3F4860EE"
  405. "12BF2D5B" "0B7474D6" "E694F91E" "6DBE1159" "74A3926F" "12FEE5E4"
  406. "38777CB6" "A932DF8C" "D8BEC4D0" "73B931BA" "3BC832B6" "8D9DD300"
  407. "741FA7BF" "8AFC47ED" "2576F693" "6BA42466" "3AAB639C" "5AE4F568"
  408. "3423B474" "2BF1C978" "238F16CB" "E39D652D" "E3FDB8BE" "FC848AD9"
  409. "22222E04" "A4037C07" "13EB57A8" "1A23F0C7" "3473FC64" "6CEA306B"
  410. "4BCBC886" "2F8385DD" "FA9D4B7F" "A2C087E8" "79683303" "ED5BDD3A"
  411. "062B3CF5" "B3A278A6" "6D2A13F8" "3F44F82D" "DF310EE0" "74AB6A36"
  412. "4597E899" "A0255DC1" "64F31CC5" "0846851D" "F9AB4819" "5DED7EA1"
  413. "B1D510BD" "7EE74D73" "FAF36BC3" "1ECFA268" "359046F4" "EB879F92"
  414. "4009438B" "481C6CD7" "889A002E" "D5EE382B" "C9190DA6" "FC026E47"
  415. "9558E447" "5677E9AA" "9E3050E2" "765694DF" "C81F56E8" "80B96E71"
  416. "60C980DD" "98EDD3DF" "FFFFFFFF" "FFFFFFFF";
  417. return (dh_new_group_asc(gen, group18));
  418. }
  419. /* Select fallback group used by DH-GEX if moduli file cannot be read. */
  420. DH *
  421. dh_new_group_fallback(int max)
  422. {
  423. debug3("%s: requested max size %d", __func__, max);
  424. if (max < 3072) {
  425. debug3("using 2k bit group 14");
  426. return dh_new_group14();
  427. } else if (max < 6144) {
  428. debug3("using 4k bit group 16");
  429. return dh_new_group16();
  430. }
  431. debug3("using 8k bit group 18");
  432. return dh_new_group18();
  433. }
  434. /*
  435. * Estimates the group order for a Diffie-Hellman group that has an
  436. * attack complexity approximately the same as O(2**bits).
  437. * Values from NIST Special Publication 800-57: Recommendation for Key
  438. * Management Part 1 (rev 3) limited by the recommended maximum value
  439. * from RFC4419 section 3.
  440. */
  441. u_int
  442. dh_estimate(int bits)
  443. {
  444. if (bits <= 112)
  445. return 2048;
  446. if (bits <= 128)
  447. return 3072;
  448. if (bits <= 192)
  449. return 7680;
  450. return 8192;
  451. }
  452. #endif /* WITH_OPENSSL */