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