dtucker@openbsd.org | fdfbf45 | 2016-03-31 05:24:06 +0000 | [diff] [blame] | 1 | /* $OpenBSD: dh.c,v 1.59 2016/03/31 05:24:06 dtucker Exp $ */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 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 | |
| 26 | #include "includes.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 28 | #include <sys/param.h> /* MIN */ |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 29 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 30 | #include <openssl/bn.h> |
| 31 | #include <openssl/dh.h> |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 32 | |
dtucker@openbsd.org | fdfbf45 | 2016-03-31 05:24:06 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 35 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 36 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 38 | #include <limits.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 39 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 40 | #include "dh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 41 | #include "pathnames.h" |
| 42 | #include "log.h" |
| 43 | #include "misc.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 44 | #include "ssherr.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 45 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 46 | static int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 47 | parse_prime(int linenum, char *line, struct dhgroup *dhg) |
| 48 | { |
| 49 | char *cp, *arg; |
| 50 | char *strsize, *gen, *prime; |
Damien Miller | 5a73c1a | 2006-03-31 23:09:41 +1100 | [diff] [blame] | 51 | const char *errstr = NULL; |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 52 | long long n; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 53 | |
Damien Miller | 0d02c3e | 2013-07-18 16:12:06 +1000 | [diff] [blame] | 54 | dhg->p = dhg->g = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 55 | cp = line; |
Damien Miller | 928b236 | 2006-03-26 13:53:32 +1100 | [diff] [blame] | 56 | if ((arg = strdelim(&cp)) == NULL) |
| 57 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 58 | /* Ignore leading whitespace */ |
| 59 | if (*arg == '\0') |
| 60 | arg = strdelim(&cp); |
Ben Lindstrom | 04f9af7 | 2002-07-04 00:03:56 +0000 | [diff] [blame] | 61 | if (!arg || !*arg || *arg == '#') |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 62 | return 0; |
| 63 | |
| 64 | /* time */ |
| 65 | if (cp == NULL || *arg == '\0') |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 66 | goto truncated; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 67 | arg = strsep(&cp, " "); /* type */ |
| 68 | if (cp == NULL || *arg == '\0') |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 69 | goto truncated; |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 70 | /* Ensure this is a safe prime */ |
| 71 | n = strtonum(arg, 0, 5, &errstr); |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 72 | if (errstr != NULL || n != MODULI_TYPE_SAFE) { |
| 73 | error("moduli:%d: type is not %d", linenum, MODULI_TYPE_SAFE); |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 74 | goto fail; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 75 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 76 | arg = strsep(&cp, " "); /* tests */ |
| 77 | if (cp == NULL || *arg == '\0') |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 78 | goto truncated; |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 79 | /* Ensure prime has been tested and is not composite */ |
| 80 | n = strtonum(arg, 0, 0x1f, &errstr); |
| 81 | if (errstr != NULL || |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 82 | (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE)) { |
| 83 | error("moduli:%d: invalid moduli tests flag", linenum); |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 84 | goto fail; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 85 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 86 | arg = strsep(&cp, " "); /* tries */ |
| 87 | if (cp == NULL || *arg == '\0') |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 88 | goto truncated; |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 89 | n = strtonum(arg, 0, 1<<30, &errstr); |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 90 | if (errstr != NULL || n == 0) { |
| 91 | error("moduli:%d: invalid primality trial count", linenum); |
Damien Miller | 2e9cf49 | 2008-06-29 22:47:04 +1000 | [diff] [blame] | 92 | goto fail; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 93 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 94 | strsize = strsep(&cp, " "); /* size */ |
| 95 | if (cp == NULL || *strsize == '\0' || |
Darren Tucker | 759cb2a | 2009-10-07 09:01:50 +1100 | [diff] [blame] | 96 | (dhg->size = (int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 || |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 97 | errstr) { |
| 98 | error("moduli:%d: invalid prime length", linenum); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 99 | goto fail; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 100 | } |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 101 | /* The whole group is one bit larger */ |
| 102 | dhg->size++; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 103 | gen = strsep(&cp, " "); /* gen */ |
| 104 | if (cp == NULL || *gen == '\0') |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 105 | goto truncated; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 106 | prime = strsep(&cp, " "); /* prime */ |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 107 | if (cp != NULL || *prime == '\0') { |
| 108 | truncated: |
| 109 | error("moduli:%d: truncated", linenum); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 110 | goto fail; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 111 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 112 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 113 | if ((dhg->g = BN_new()) == NULL || |
| 114 | (dhg->p = BN_new()) == NULL) { |
| 115 | error("parse_prime: BN_new failed"); |
| 116 | goto fail; |
| 117 | } |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 118 | if (BN_hex2bn(&dhg->g, gen) == 0) { |
| 119 | error("moduli:%d: could not parse generator value", linenum); |
| 120 | goto fail; |
| 121 | } |
| 122 | if (BN_hex2bn(&dhg->p, prime) == 0) { |
| 123 | error("moduli:%d: could not parse prime value", linenum); |
| 124 | goto fail; |
| 125 | } |
| 126 | if (BN_num_bits(dhg->p) != dhg->size) { |
| 127 | error("moduli:%d: prime has wrong size: actual %d listed %d", |
| 128 | linenum, BN_num_bits(dhg->p), dhg->size - 1); |
| 129 | goto fail; |
| 130 | } |
| 131 | if (BN_cmp(dhg->g, BN_value_one()) <= 0) { |
| 132 | error("moduli:%d: generator is invalid", linenum); |
| 133 | goto fail; |
| 134 | } |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 135 | return 1; |
Damien Miller | 23e526e | 2001-03-30 10:47:43 +1000 | [diff] [blame] | 136 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 137 | fail: |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 138 | if (dhg->g != NULL) |
| 139 | BN_clear_free(dhg->g); |
| 140 | if (dhg->p != NULL) |
| 141 | BN_clear_free(dhg->p); |
| 142 | dhg->g = dhg->p = NULL; |
Damien Miller | bbeb1da | 2013-07-18 16:10:49 +1000 | [diff] [blame] | 143 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | DH * |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 147 | choose_dh(int min, int wantbits, int max) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 148 | { |
| 149 | FILE *f; |
Darren Tucker | c56c7ef | 2004-02-29 20:13:34 +1100 | [diff] [blame] | 150 | char line[4096]; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 151 | int best, bestcount, which; |
| 152 | int linenum; |
| 153 | struct dhgroup dhg; |
| 154 | |
dtucker@openbsd.org | fdfbf45 | 2016-03-31 05:24:06 +0000 | [diff] [blame] | 155 | if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) { |
| 156 | logit("WARNING: could open open %s (%s), using fixed modulus", |
| 157 | _PATH_DH_MODULI, strerror(errno)); |
dtucker@openbsd.org | 40f6429 | 2015-05-27 23:39:18 +0000 | [diff] [blame] | 158 | return (dh_new_group_fallback(max)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | linenum = 0; |
| 162 | best = bestcount = 0; |
| 163 | while (fgets(line, sizeof(line), f)) { |
| 164 | linenum++; |
| 165 | if (!parse_prime(linenum, line, &dhg)) |
| 166 | continue; |
Damien Miller | 9ef95dd | 2002-01-22 23:10:33 +1100 | [diff] [blame] | 167 | BN_clear_free(dhg.g); |
| 168 | BN_clear_free(dhg.p); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 169 | |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 170 | if (dhg.size > max || dhg.size < min) |
| 171 | continue; |
| 172 | |
| 173 | if ((dhg.size > wantbits && dhg.size < best) || |
| 174 | (dhg.size > best && best < wantbits)) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 175 | best = dhg.size; |
| 176 | bestcount = 0; |
| 177 | } |
| 178 | if (dhg.size == best) |
| 179 | bestcount++; |
| 180 | } |
Ben Lindstrom | af73880 | 2001-06-25 04:18:59 +0000 | [diff] [blame] | 181 | rewind(f); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 182 | |
| 183 | if (bestcount == 0) { |
Ben Lindstrom | af73880 | 2001-06-25 04:18:59 +0000 | [diff] [blame] | 184 | fclose(f); |
dtucker@openbsd.org | fdfbf45 | 2016-03-31 05:24:06 +0000 | [diff] [blame] | 185 | logit("WARNING: no suitable primes in %s", _PATH_DH_MODULI); |
dtucker@openbsd.org | 40f6429 | 2015-05-27 23:39:18 +0000 | [diff] [blame] | 186 | return (dh_new_group_fallback(max)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 187 | } |
| 188 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 189 | linenum = 0; |
Damien Miller | 354c48c | 2008-05-19 14:50:00 +1000 | [diff] [blame] | 190 | which = arc4random_uniform(bestcount); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 191 | while (fgets(line, sizeof(line), f)) { |
| 192 | if (!parse_prime(linenum, line, &dhg)) |
| 193 | continue; |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 194 | if ((dhg.size > max || dhg.size < min) || |
| 195 | dhg.size != best || |
| 196 | linenum++ != which) { |
Damien Miller | 9ef95dd | 2002-01-22 23:10:33 +1100 | [diff] [blame] | 197 | BN_clear_free(dhg.g); |
| 198 | BN_clear_free(dhg.p); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | fclose(f); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 204 | if (linenum != which+1) { |
| 205 | logit("WARNING: line %d disappeared in %s, giving up", |
dtucker@openbsd.org | fdfbf45 | 2016-03-31 05:24:06 +0000 | [diff] [blame] | 206 | which, _PATH_DH_MODULI); |
dtucker@openbsd.org | 40f6429 | 2015-05-27 23:39:18 +0000 | [diff] [blame] | 207 | return (dh_new_group_fallback(max)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 208 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 209 | |
| 210 | return (dh_new_group(dhg.g, dhg.p)); |
| 211 | } |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 212 | |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 213 | /* diffie-hellman-groupN-sha1 */ |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 214 | |
| 215 | int |
| 216 | dh_pub_is_valid(DH *dh, BIGNUM *dh_pub) |
| 217 | { |
| 218 | int i; |
| 219 | int n = BN_num_bits(dh_pub); |
| 220 | int bits_set = 0; |
Darren Tucker | 31cde68 | 2006-05-06 17:43:33 +1000 | [diff] [blame] | 221 | BIGNUM *tmp; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 222 | |
| 223 | if (dh_pub->neg) { |
Damien Miller | 603077a | 2007-10-26 14:25:55 +1000 | [diff] [blame] | 224 | logit("invalid public DH value: negative"); |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Darren Tucker | 31cde68 | 2006-05-06 17:43:33 +1000 | [diff] [blame] | 227 | if (BN_cmp(dh_pub, BN_value_one()) != 1) { /* pub_exp <= 1 */ |
| 228 | logit("invalid public DH value: <= 1"); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Damien Miller | 603077a | 2007-10-26 14:25:55 +1000 | [diff] [blame] | 232 | if ((tmp = BN_new()) == NULL) { |
| 233 | error("%s: BN_new failed", __func__); |
| 234 | return 0; |
| 235 | } |
Darren Tucker | 31cde68 | 2006-05-06 17:43:33 +1000 | [diff] [blame] | 236 | if (!BN_sub(tmp, dh->p, BN_value_one()) || |
| 237 | BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */ |
| 238 | BN_clear_free(tmp); |
| 239 | logit("invalid public DH value: >= p-1"); |
| 240 | return 0; |
| 241 | } |
| 242 | BN_clear_free(tmp); |
| 243 | |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 244 | for (i = 0; i <= n; i++) |
| 245 | if (BN_is_bit_set(dh_pub, i)) |
| 246 | bits_set++; |
Ben Lindstrom | 1f53083 | 2002-12-23 02:03:02 +0000 | [diff] [blame] | 247 | debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p)); |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 248 | |
djm@openbsd.org | 6e7f68c | 2016-02-28 22:27:00 +0000 | [diff] [blame] | 249 | /* |
| 250 | * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial |
| 251 | */ |
| 252 | if (bits_set < 4) { |
| 253 | logit("invalid public DH value (%d/%d)", |
| 254 | bits_set, BN_num_bits(dh->p)); |
| 255 | return 0; |
| 256 | } |
| 257 | return 1; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 258 | } |
| 259 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 260 | int |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 261 | dh_gen_key(DH *dh, int need) |
| 262 | { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 263 | int pbits; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 264 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 265 | if (need < 0 || dh->p == NULL || |
| 266 | (pbits = BN_num_bits(dh->p)) <= 0 || |
djm@openbsd.org | b8afbe2 | 2015-03-26 06:59:28 +0000 | [diff] [blame] | 267 | need > INT_MAX / 2 || 2 * need > pbits) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 268 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 6e7f68c | 2016-02-28 22:27:00 +0000 | [diff] [blame] | 269 | if (need < 256) |
| 270 | need = 256; |
| 271 | /* |
| 272 | * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)), |
| 273 | * so double requested need here. |
| 274 | */ |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 275 | dh->length = MIN(need * 2, pbits - 1); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 276 | if (DH_generate_key(dh) == 0 || |
| 277 | !dh_pub_is_valid(dh, dh->pub_key)) { |
| 278 | BN_clear_free(dh->priv_key); |
| 279 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 280 | } |
| 281 | return 0; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | DH * |
| 285 | dh_new_group_asc(const char *gen, const char *modulus) |
| 286 | { |
| 287 | DH *dh; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 288 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 289 | if ((dh = DH_new()) == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | if (BN_hex2bn(&dh->p, modulus) == 0 || |
| 292 | BN_hex2bn(&dh->g, gen) == 0) { |
| 293 | DH_free(dh); |
| 294 | return NULL; |
| 295 | } |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 296 | return (dh); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * This just returns the group, we still need to generate the exchange |
| 301 | * value. |
| 302 | */ |
| 303 | |
| 304 | DH * |
| 305 | dh_new_group(BIGNUM *gen, BIGNUM *modulus) |
| 306 | { |
| 307 | DH *dh; |
| 308 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 309 | if ((dh = DH_new()) == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 310 | return NULL; |
Damien Miller | 9709f90 | 2001-03-30 10:50:10 +1000 | [diff] [blame] | 311 | dh->p = modulus; |
| 312 | dh->g = gen; |
| 313 | |
| 314 | return (dh); |
| 315 | } |
| 316 | |
| 317 | DH * |
| 318 | dh_new_group1(void) |
| 319 | { |
| 320 | static char *gen = "2", *group1 = |
| 321 | "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1" |
| 322 | "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD" |
| 323 | "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245" |
| 324 | "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED" |
| 325 | "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381" |
| 326 | "FFFFFFFF" "FFFFFFFF"; |
| 327 | |
| 328 | return (dh_new_group_asc(gen, group1)); |
| 329 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 330 | |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 331 | DH * |
| 332 | dh_new_group14(void) |
| 333 | { |
| 334 | static char *gen = "2", *group14 = |
| 335 | "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1" |
| 336 | "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD" |
| 337 | "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245" |
| 338 | "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED" |
| 339 | "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D" |
| 340 | "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F" |
| 341 | "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D" |
| 342 | "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B" |
| 343 | "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9" |
| 344 | "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510" |
| 345 | "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF"; |
| 346 | |
| 347 | return (dh_new_group_asc(gen, group14)); |
| 348 | } |
| 349 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 350 | /* |
dtucker@openbsd.org | 40f6429 | 2015-05-27 23:39:18 +0000 | [diff] [blame] | 351 | * 4k bit fallback group used by DH-GEX if moduli file cannot be read. |
| 352 | * Source: MODP group 16 from RFC3526. |
| 353 | */ |
| 354 | DH * |
| 355 | dh_new_group_fallback(int max) |
| 356 | { |
| 357 | static char *gen = "2", *group16 = |
| 358 | "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1" |
| 359 | "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD" |
| 360 | "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245" |
| 361 | "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED" |
| 362 | "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D" |
| 363 | "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F" |
| 364 | "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D" |
| 365 | "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B" |
| 366 | "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9" |
| 367 | "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510" |
| 368 | "15728E5A" "8AAAC42D" "AD33170D" "04507A33" "A85521AB" "DF1CBA64" |
| 369 | "ECFB8504" "58DBEF0A" "8AEA7157" "5D060C7D" "B3970F85" "A6E1E4C7" |
| 370 | "ABF5AE8C" "DB0933D7" "1E8C94E0" "4A25619D" "CEE3D226" "1AD2EE6B" |
| 371 | "F12FFA06" "D98A0864" "D8760273" "3EC86A64" "521F2B18" "177B200C" |
| 372 | "BBE11757" "7A615D6C" "770988C0" "BAD946E2" "08E24FA0" "74E5AB31" |
| 373 | "43DB5BFC" "E0FD108E" "4B82D120" "A9210801" "1A723C12" "A787E6D7" |
| 374 | "88719A10" "BDBA5B26" "99C32718" "6AF4E23C" "1A946834" "B6150BDA" |
| 375 | "2583E9CA" "2AD44CE8" "DBBBC2DB" "04DE8EF9" "2E8EFC14" "1FBECAA6" |
| 376 | "287C5947" "4E6BC05D" "99B2964F" "A090C3A2" "233BA186" "515BE7ED" |
| 377 | "1F612970" "CEE2D7AF" "B81BDD76" "2170481C" "D0069127" "D5B05AA9" |
| 378 | "93B4EA98" "8D8FDDC1" "86FFB7DC" "90A6C08F" "4DF435C9" "34063199" |
| 379 | "FFFFFFFF" "FFFFFFFF"; |
| 380 | |
| 381 | if (max < 4096) { |
| 382 | debug3("requested max size %d, using 2k bit group 14", max); |
| 383 | return dh_new_group14(); |
| 384 | } |
| 385 | debug3("using 4k bit group 16"); |
| 386 | return (dh_new_group_asc(gen, group16)); |
| 387 | } |
| 388 | |
| 389 | /* |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 390 | * Estimates the group order for a Diffie-Hellman group that has an |
Darren Tucker | df62d71 | 2013-10-10 10:32:39 +1100 | [diff] [blame] | 391 | * attack complexity approximately the same as O(2**bits). |
| 392 | * Values from NIST Special Publication 800-57: Recommendation for Key |
| 393 | * Management Part 1 (rev 3) limited by the recommended maximum value |
| 394 | * from RFC4419 section 3. |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 395 | */ |
| 396 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 397 | u_int |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 398 | dh_estimate(int bits) |
| 399 | { |
Darren Tucker | df62d71 | 2013-10-10 10:32:39 +1100 | [diff] [blame] | 400 | if (bits <= 112) |
| 401 | return 2048; |
Damien Miller | 8975ddf | 2003-12-17 16:33:53 +1100 | [diff] [blame] | 402 | if (bits <= 128) |
Darren Tucker | df62d71 | 2013-10-10 10:32:39 +1100 | [diff] [blame] | 403 | return 3072; |
Damien Miller | 8975ddf | 2003-12-17 16:33:53 +1100 | [diff] [blame] | 404 | if (bits <= 192) |
Darren Tucker | df62d71 | 2013-10-10 10:32:39 +1100 | [diff] [blame] | 405 | return 7680; |
| 406 | return 8192; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 407 | } |