[BACK]Return to dh.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/dh.c, Revision 1.52

1.52    ! dtucker     1: /* $OpenBSD: dh.c,v 1.51 2013/07/02 12:31:43 markus 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:
                     26: #include <sys/param.h>
1.1       provos     27:
                     28: #include <openssl/bn.h>
                     29: #include <openssl/dh.h>
1.38      stevesk    30:
1.41      stevesk    31: #include <stdio.h>
1.40      stevesk    32: #include <stdlib.h>
1.38      stevesk    33: #include <string.h>
1.1       provos     34:
                     35: #include "dh.h"
1.5       markus     36: #include "pathnames.h"
1.6       markus     37: #include "log.h"
                     38: #include "misc.h"
1.1       provos     39:
1.17      itojun     40: static int
1.1       provos     41: parse_prime(int linenum, char *line, struct dhgroup *dhg)
                     42: {
                     43:        char *cp, *arg;
                     44:        char *strsize, *gen, *prime;
1.35      deraadt    45:        const char *errstr = NULL;
1.47      djm        46:        long long n;
1.1       provos     47:
1.51      markus     48:        dhg->p = dhg->g = NULL;
1.1       provos     49:        cp = line;
1.32      djm        50:        if ((arg = strdelim(&cp)) == NULL)
                     51:                return 0;
1.1       provos     52:        /* Ignore leading whitespace */
                     53:        if (*arg == '\0')
                     54:                arg = strdelim(&cp);
1.22      markus     55:        if (!arg || !*arg || *arg == '#')
1.1       provos     56:                return 0;
                     57:
                     58:        /* time */
                     59:        if (cp == NULL || *arg == '\0')
1.50      djm        60:                goto truncated;
1.1       provos     61:        arg = strsep(&cp, " "); /* type */
                     62:        if (cp == NULL || *arg == '\0')
1.50      djm        63:                goto truncated;
1.47      djm        64:        /* Ensure this is a safe prime */
                     65:        n = strtonum(arg, 0, 5, &errstr);
1.50      djm        66:        if (errstr != NULL || n != MODULI_TYPE_SAFE) {
                     67:                error("moduli:%d: type is not %d", linenum, MODULI_TYPE_SAFE);
1.47      djm        68:                goto fail;
1.50      djm        69:        }
1.1       provos     70:        arg = strsep(&cp, " "); /* tests */
                     71:        if (cp == NULL || *arg == '\0')
1.50      djm        72:                goto truncated;
1.47      djm        73:        /* Ensure prime has been tested and is not composite */
                     74:        n = strtonum(arg, 0, 0x1f, &errstr);
                     75:        if (errstr != NULL ||
1.50      djm        76:            (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE)) {
                     77:                error("moduli:%d: invalid moduli tests flag", linenum);
1.47      djm        78:                goto fail;
1.50      djm        79:        }
1.1       provos     80:        arg = strsep(&cp, " "); /* tries */
                     81:        if (cp == NULL || *arg == '\0')
1.50      djm        82:                goto truncated;
1.47      djm        83:        n = strtonum(arg, 0, 1<<30, &errstr);
1.50      djm        84:        if (errstr != NULL || n == 0) {
                     85:                error("moduli:%d: invalid primality trial count", linenum);
1.1       provos     86:                goto fail;
1.50      djm        87:        }
1.1       provos     88:        strsize = strsep(&cp, " "); /* size */
                     89:        if (cp == NULL || *strsize == '\0' ||
1.48      grunk      90:            (dhg->size = (int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
1.50      djm        91:            errstr) {
                     92:                error("moduli:%d: invalid prime length", linenum);
1.1       provos     93:                goto fail;
1.50      djm        94:        }
1.9       provos     95:        /* The whole group is one bit larger */
                     96:        dhg->size++;
1.1       provos     97:        gen = strsep(&cp, " "); /* gen */
                     98:        if (cp == NULL || *gen == '\0')
1.50      djm        99:                goto truncated;
1.1       provos    100:        prime = strsep(&cp, " "); /* prime */
1.50      djm       101:        if (cp != NULL || *prime == '\0') {
                    102:  truncated:
                    103:                error("moduli:%d: truncated", linenum);
1.1       provos    104:                goto fail;
1.50      djm       105:        }
1.1       provos    106:
1.18      markus    107:        if ((dhg->g = BN_new()) == NULL)
                    108:                fatal("parse_prime: BN_new failed");
                    109:        if ((dhg->p = BN_new()) == NULL)
                    110:                fatal("parse_prime: BN_new failed");
1.50      djm       111:        if (BN_hex2bn(&dhg->g, gen) == 0) {
                    112:                error("moduli:%d: could not parse generator value", linenum);
                    113:                goto fail;
                    114:        }
                    115:        if (BN_hex2bn(&dhg->p, prime) == 0) {
                    116:                error("moduli:%d: could not parse prime value", linenum);
                    117:                goto fail;
                    118:        }
                    119:        if (BN_num_bits(dhg->p) != dhg->size) {
                    120:                error("moduli:%d: prime has wrong size: actual %d listed %d",
                    121:                    linenum, BN_num_bits(dhg->p), dhg->size - 1);
                    122:                goto fail;
                    123:        }
                    124:        if (BN_cmp(dhg->g, BN_value_one()) <= 0) {
                    125:                error("moduli:%d: generator is invalid", linenum);
                    126:                goto fail;
                    127:        }
1.1       provos    128:
1.50      djm       129:        return 1;
1.10      provos    130:
1.1       provos    131:  fail:
1.50      djm       132:        if (dhg->g != NULL)
                    133:                BN_clear_free(dhg->g);
                    134:        if (dhg->p != NULL)
                    135:                BN_clear_free(dhg->p);
                    136:        dhg->g = dhg->p = NULL;
1.7       millert   137:        error("Bad prime description in line %d", linenum);
1.50      djm       138:        return 0;
1.1       provos    139: }
                    140:
                    141: DH *
1.9       provos    142: choose_dh(int min, int wantbits, int max)
1.1       provos    143: {
                    144:        FILE *f;
1.28      dtucker   145:        char line[4096];
1.1       provos    146:        int best, bestcount, which;
                    147:        int linenum;
                    148:        struct dhgroup dhg;
                    149:
1.15      provos    150:        if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL &&
                    151:            (f = fopen(_PATH_DH_PRIMES, "r")) == NULL) {
1.30      djm       152:                logit("WARNING: %s does not exist, using fixed modulus",
                    153:                    _PATH_DH_MODULI);
                    154:                return (dh_new_group14());
1.1       provos    155:        }
                    156:
                    157:        linenum = 0;
                    158:        best = bestcount = 0;
                    159:        while (fgets(line, sizeof(line), f)) {
                    160:                linenum++;
                    161:                if (!parse_prime(linenum, line, &dhg))
                    162:                        continue;
1.19      markus    163:                BN_clear_free(dhg.g);
                    164:                BN_clear_free(dhg.p);
1.1       provos    165:
1.9       provos    166:                if (dhg.size > max || dhg.size < min)
                    167:                        continue;
                    168:
                    169:                if ((dhg.size > wantbits && dhg.size < best) ||
                    170:                    (dhg.size > best && best < wantbits)) {
1.1       provos    171:                        best = dhg.size;
                    172:                        bestcount = 0;
                    173:                }
                    174:                if (dhg.size == best)
                    175:                        bestcount++;
                    176:        }
1.16      provos    177:        rewind(f);
1.1       provos    178:
                    179:        if (bestcount == 0) {
1.16      provos    180:                fclose(f);
1.24      itojun    181:                logit("WARNING: no suitable primes in %s", _PATH_DH_PRIMES);
1.31      djm       182:                return (dh_new_group14());
1.1       provos    183:        }
                    184:
                    185:        linenum = 0;
1.46      djm       186:        which = arc4random_uniform(bestcount);
1.1       provos    187:        while (fgets(line, sizeof(line), f)) {
                    188:                if (!parse_prime(linenum, line, &dhg))
1.9       provos    189:                        continue;
1.13      markus    190:                if ((dhg.size > max || dhg.size < min) ||
                    191:                    dhg.size != best ||
                    192:                    linenum++ != which) {
1.19      markus    193:                        BN_clear_free(dhg.g);
                    194:                        BN_clear_free(dhg.p);
1.1       provos    195:                        continue;
                    196:                }
                    197:                break;
                    198:        }
                    199:        fclose(f);
1.13      markus    200:        if (linenum != which+1)
                    201:                fatal("WARNING: line %d disappeared in %s, giving up",
                    202:                    which, _PATH_DH_PRIMES);
1.1       provos    203:
                    204:        return (dh_new_group(dhg.g, dhg.p));
1.11      markus    205: }
                    206:
1.30      djm       207: /* diffie-hellman-groupN-sha1 */
1.11      markus    208:
                    209: int
                    210: dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
                    211: {
                    212:        int i;
                    213:        int n = BN_num_bits(dh_pub);
                    214:        int bits_set = 0;
1.36      djm       215:        BIGNUM *tmp;
1.11      markus    216:
                    217:        if (dh_pub->neg) {
1.45      ray       218:                logit("invalid public DH value: negative");
1.11      markus    219:                return 0;
                    220:        }
1.36      djm       221:        if (BN_cmp(dh_pub, BN_value_one()) != 1) {      /* pub_exp <= 1 */
                    222:                logit("invalid public DH value: <= 1");
                    223:                return 0;
                    224:        }
                    225:
1.45      ray       226:        if ((tmp = BN_new()) == NULL) {
                    227:                error("%s: BN_new failed", __func__);
                    228:                return 0;
                    229:        }
1.36      djm       230:        if (!BN_sub(tmp, dh->p, BN_value_one()) ||
                    231:            BN_cmp(dh_pub, tmp) != -1) {                /* pub_exp > p-2 */
                    232:                BN_clear_free(tmp);
                    233:                logit("invalid public DH value: >= p-1");
                    234:                return 0;
                    235:        }
                    236:        BN_clear_free(tmp);
                    237:
1.11      markus    238:        for (i = 0; i <= n; i++)
                    239:                if (BN_is_bit_set(dh_pub, i))
                    240:                        bits_set++;
1.23      markus    241:        debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
1.11      markus    242:
                    243:        /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */
1.36      djm       244:        if (bits_set > 1)
1.11      markus    245:                return 1;
1.36      djm       246:
1.24      itojun    247:        logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
1.11      markus    248:        return 0;
                    249: }
                    250:
                    251: void
                    252: dh_gen_key(DH *dh, int need)
                    253: {
1.29      dtucker   254:        int i, bits_set, tries = 0;
1.11      markus    255:
1.49      djm       256:        if (need < 0)
                    257:                fatal("dh_gen_key: need < 0");
1.11      markus    258:        if (dh->p == NULL)
                    259:                fatal("dh_gen_key: dh->p == NULL");
1.25      miod      260:        if (need > INT_MAX / 2 || 2 * need >= BN_num_bits(dh->p))
1.11      markus    261:                fatal("dh_gen_key: group too small: %d (2*need %d)",
                    262:                    BN_num_bits(dh->p), 2*need);
                    263:        do {
                    264:                if (dh->priv_key != NULL)
1.19      markus    265:                        BN_clear_free(dh->priv_key);
1.18      markus    266:                if ((dh->priv_key = BN_new()) == NULL)
1.11      markus    267:                        fatal("dh_gen_key: BN_new failed");
                    268:                /* generate a 2*need bits random private exponent */
1.21      markus    269:                if (!BN_rand(dh->priv_key, 2*need, 0, 0))
                    270:                        fatal("dh_gen_key: BN_rand failed");
1.11      markus    271:                if (DH_generate_key(dh) == 0)
                    272:                        fatal("DH_generate_key");
1.29      dtucker   273:                for (i = 0, bits_set = 0; i <= BN_num_bits(dh->priv_key); i++)
1.11      markus    274:                        if (BN_is_bit_set(dh->priv_key, i))
                    275:                                bits_set++;
1.23      markus    276:                debug2("dh_gen_key: priv key bits set: %d/%d",
1.11      markus    277:                    bits_set, BN_num_bits(dh->priv_key));
                    278:                if (tries++ > 10)
                    279:                        fatal("dh_gen_key: too many bad keys: giving up");
                    280:        } while (!dh_pub_is_valid(dh, dh->pub_key));
                    281: }
                    282:
                    283: DH *
                    284: dh_new_group_asc(const char *gen, const char *modulus)
                    285: {
                    286:        DH *dh;
                    287:
1.18      markus    288:        if ((dh = DH_new()) == NULL)
                    289:                fatal("dh_new_group_asc: DH_new");
1.11      markus    290:
1.44      markus    291:        if (BN_hex2bn(&dh->p, modulus) == 0)
1.11      markus    292:                fatal("BN_hex2bn p");
1.44      markus    293:        if (BN_hex2bn(&dh->g, gen) == 0)
1.11      markus    294:                fatal("BN_hex2bn g");
                    295:
                    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:
1.18      markus    309:        if ((dh = DH_new()) == NULL)
                    310:                fatal("dh_new_group: DH_new");
1.11      markus    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));
1.30      djm       329: }
                    330:
                    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));
1.12      markus    348: }
                    349:
                    350: /*
                    351:  * Estimates the group order for a Diffie-Hellman group that has an
1.52    ! dtucker   352:  * attack complexity approximately the same as O(2**bits).
        !           353:  * Values from NIST Special Publication 800-57: Recommendation for Key
        !           354:  * Management Part 1 (rev 3) limited by the recommended maximum value
        !           355:  * from RFC4419 section 3.
1.12      markus    356:  */
                    357:
                    358: int
                    359: dh_estimate(int bits)
                    360: {
1.52    ! dtucker   361:        if (bits <= 112)
        !           362:                return 2048;
1.26      markus    363:        if (bits <= 128)
1.52    ! dtucker   364:                return 3072;
1.26      markus    365:        if (bits <= 192)
1.52    ! dtucker   366:                return 7680;
        !           367:        return 8192;
1.1       provos    368: }