[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.47

1.47    ! djm         1: /* $OpenBSD: dh.c,v 1.46 2008/04/13 00:22:17 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:
                     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:
                     48:        cp = line;
1.32      djm        49:        if ((arg = strdelim(&cp)) == NULL)
                     50:                return 0;
1.1       provos     51:        /* Ignore leading whitespace */
                     52:        if (*arg == '\0')
                     53:                arg = strdelim(&cp);
1.22      markus     54:        if (!arg || !*arg || *arg == '#')
1.1       provos     55:                return 0;
                     56:
                     57:        /* time */
                     58:        if (cp == NULL || *arg == '\0')
                     59:                goto fail;
                     60:        arg = strsep(&cp, " "); /* type */
                     61:        if (cp == NULL || *arg == '\0')
                     62:                goto fail;
1.47    ! djm        63:        /* Ensure this is a safe prime */
        !            64:        n = strtonum(arg, 0, 5, &errstr);
        !            65:        if (errstr != NULL || n != MODULI_TYPE_SAFE)
        !            66:                goto fail;
1.1       provos     67:        arg = strsep(&cp, " "); /* tests */
                     68:        if (cp == NULL || *arg == '\0')
                     69:                goto fail;
1.47    ! djm        70:        /* Ensure prime has been tested and is not composite */
        !            71:        n = strtonum(arg, 0, 0x1f, &errstr);
        !            72:        if (errstr != NULL ||
        !            73:            (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE))
        !            74:                goto fail;
1.1       provos     75:        arg = strsep(&cp, " "); /* tries */
                     76:        if (cp == NULL || *arg == '\0')
1.47    ! djm        77:                goto fail;
        !            78:        n = strtonum(arg, 0, 1<<30, &errstr);
        !            79:        if (errstr != NULL || n == 0)
1.1       provos     80:                goto fail;
                     81:        strsize = strsep(&cp, " "); /* size */
                     82:        if (cp == NULL || *strsize == '\0' ||
1.35      deraadt    83:            (dhg->size = (u_int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
                     84:            errstr)
1.1       provos     85:                goto fail;
1.9       provos     86:        /* The whole group is one bit larger */
                     87:        dhg->size++;
1.1       provos     88:        gen = strsep(&cp, " "); /* gen */
                     89:        if (cp == NULL || *gen == '\0')
                     90:                goto fail;
                     91:        prime = strsep(&cp, " "); /* prime */
                     92:        if (cp != NULL || *prime == '\0')
                     93:                goto fail;
                     94:
1.18      markus     95:        if ((dhg->g = BN_new()) == NULL)
                     96:                fatal("parse_prime: BN_new failed");
                     97:        if ((dhg->p = BN_new()) == NULL)
                     98:                fatal("parse_prime: BN_new failed");
1.14      markus     99:        if (BN_hex2bn(&dhg->g, gen) == 0)
1.10      provos    100:                goto failclean;
                    101:
1.14      markus    102:        if (BN_hex2bn(&dhg->p, prime) == 0)
1.10      provos    103:                goto failclean;
                    104:
                    105:        if (BN_num_bits(dhg->p) != dhg->size)
1.27      dtucker   106:                goto failclean;
                    107:
                    108:        if (BN_is_zero(dhg->g) || BN_is_one(dhg->g))
1.10      provos    109:                goto failclean;
1.1       provos    110:
                    111:        return (1);
1.10      provos    112:
                    113:  failclean:
1.19      markus    114:        BN_clear_free(dhg->g);
                    115:        BN_clear_free(dhg->p);
1.1       provos    116:  fail:
1.7       millert   117:        error("Bad prime description in line %d", linenum);
1.1       provos    118:        return (0);
                    119: }
                    120:
                    121: DH *
1.9       provos    122: choose_dh(int min, int wantbits, int max)
1.1       provos    123: {
                    124:        FILE *f;
1.28      dtucker   125:        char line[4096];
1.1       provos    126:        int best, bestcount, which;
                    127:        int linenum;
                    128:        struct dhgroup dhg;
                    129:
1.15      provos    130:        if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL &&
                    131:            (f = fopen(_PATH_DH_PRIMES, "r")) == NULL) {
1.30      djm       132:                logit("WARNING: %s does not exist, using fixed modulus",
                    133:                    _PATH_DH_MODULI);
                    134:                return (dh_new_group14());
1.1       provos    135:        }
                    136:
                    137:        linenum = 0;
                    138:        best = bestcount = 0;
                    139:        while (fgets(line, sizeof(line), f)) {
                    140:                linenum++;
                    141:                if (!parse_prime(linenum, line, &dhg))
                    142:                        continue;
1.19      markus    143:                BN_clear_free(dhg.g);
                    144:                BN_clear_free(dhg.p);
1.1       provos    145:
1.9       provos    146:                if (dhg.size > max || dhg.size < min)
                    147:                        continue;
                    148:
                    149:                if ((dhg.size > wantbits && dhg.size < best) ||
                    150:                    (dhg.size > best && best < wantbits)) {
1.1       provos    151:                        best = dhg.size;
                    152:                        bestcount = 0;
                    153:                }
                    154:                if (dhg.size == best)
                    155:                        bestcount++;
                    156:        }
1.16      provos    157:        rewind(f);
1.1       provos    158:
                    159:        if (bestcount == 0) {
1.16      provos    160:                fclose(f);
1.24      itojun    161:                logit("WARNING: no suitable primes in %s", _PATH_DH_PRIMES);
1.31      djm       162:                return (dh_new_group14());
1.1       provos    163:        }
                    164:
                    165:        linenum = 0;
1.46      djm       166:        which = arc4random_uniform(bestcount);
1.1       provos    167:        while (fgets(line, sizeof(line), f)) {
                    168:                if (!parse_prime(linenum, line, &dhg))
1.9       provos    169:                        continue;
1.13      markus    170:                if ((dhg.size > max || dhg.size < min) ||
                    171:                    dhg.size != best ||
                    172:                    linenum++ != which) {
1.19      markus    173:                        BN_clear_free(dhg.g);
                    174:                        BN_clear_free(dhg.p);
1.1       provos    175:                        continue;
                    176:                }
                    177:                break;
                    178:        }
                    179:        fclose(f);
1.13      markus    180:        if (linenum != which+1)
                    181:                fatal("WARNING: line %d disappeared in %s, giving up",
                    182:                    which, _PATH_DH_PRIMES);
1.1       provos    183:
                    184:        return (dh_new_group(dhg.g, dhg.p));
1.11      markus    185: }
                    186:
1.30      djm       187: /* diffie-hellman-groupN-sha1 */
1.11      markus    188:
                    189: int
                    190: dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
                    191: {
                    192:        int i;
                    193:        int n = BN_num_bits(dh_pub);
                    194:        int bits_set = 0;
1.36      djm       195:        BIGNUM *tmp;
1.11      markus    196:
                    197:        if (dh_pub->neg) {
1.45      ray       198:                logit("invalid public DH value: negative");
1.11      markus    199:                return 0;
                    200:        }
1.36      djm       201:        if (BN_cmp(dh_pub, BN_value_one()) != 1) {      /* pub_exp <= 1 */
                    202:                logit("invalid public DH value: <= 1");
                    203:                return 0;
                    204:        }
                    205:
1.45      ray       206:        if ((tmp = BN_new()) == NULL) {
                    207:                error("%s: BN_new failed", __func__);
                    208:                return 0;
                    209:        }
1.36      djm       210:        if (!BN_sub(tmp, dh->p, BN_value_one()) ||
                    211:            BN_cmp(dh_pub, tmp) != -1) {                /* pub_exp > p-2 */
                    212:                BN_clear_free(tmp);
                    213:                logit("invalid public DH value: >= p-1");
                    214:                return 0;
                    215:        }
                    216:        BN_clear_free(tmp);
                    217:
1.11      markus    218:        for (i = 0; i <= n; i++)
                    219:                if (BN_is_bit_set(dh_pub, i))
                    220:                        bits_set++;
1.23      markus    221:        debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
1.11      markus    222:
                    223:        /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */
1.36      djm       224:        if (bits_set > 1)
1.11      markus    225:                return 1;
1.36      djm       226:
1.24      itojun    227:        logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
1.11      markus    228:        return 0;
                    229: }
                    230:
                    231: void
                    232: dh_gen_key(DH *dh, int need)
                    233: {
1.29      dtucker   234:        int i, bits_set, tries = 0;
1.11      markus    235:
                    236:        if (dh->p == NULL)
                    237:                fatal("dh_gen_key: dh->p == NULL");
1.25      miod      238:        if (need > INT_MAX / 2 || 2 * need >= BN_num_bits(dh->p))
1.11      markus    239:                fatal("dh_gen_key: group too small: %d (2*need %d)",
                    240:                    BN_num_bits(dh->p), 2*need);
                    241:        do {
                    242:                if (dh->priv_key != NULL)
1.19      markus    243:                        BN_clear_free(dh->priv_key);
1.18      markus    244:                if ((dh->priv_key = BN_new()) == NULL)
1.11      markus    245:                        fatal("dh_gen_key: BN_new failed");
                    246:                /* generate a 2*need bits random private exponent */
1.21      markus    247:                if (!BN_rand(dh->priv_key, 2*need, 0, 0))
                    248:                        fatal("dh_gen_key: BN_rand failed");
1.11      markus    249:                if (DH_generate_key(dh) == 0)
                    250:                        fatal("DH_generate_key");
1.29      dtucker   251:                for (i = 0, bits_set = 0; i <= BN_num_bits(dh->priv_key); i++)
1.11      markus    252:                        if (BN_is_bit_set(dh->priv_key, i))
                    253:                                bits_set++;
1.23      markus    254:                debug2("dh_gen_key: priv key bits set: %d/%d",
1.11      markus    255:                    bits_set, BN_num_bits(dh->priv_key));
                    256:                if (tries++ > 10)
                    257:                        fatal("dh_gen_key: too many bad keys: giving up");
                    258:        } while (!dh_pub_is_valid(dh, dh->pub_key));
                    259: }
                    260:
                    261: DH *
                    262: dh_new_group_asc(const char *gen, const char *modulus)
                    263: {
                    264:        DH *dh;
                    265:
1.18      markus    266:        if ((dh = DH_new()) == NULL)
                    267:                fatal("dh_new_group_asc: DH_new");
1.11      markus    268:
1.44      markus    269:        if (BN_hex2bn(&dh->p, modulus) == 0)
1.11      markus    270:                fatal("BN_hex2bn p");
1.44      markus    271:        if (BN_hex2bn(&dh->g, gen) == 0)
1.11      markus    272:                fatal("BN_hex2bn g");
                    273:
                    274:        return (dh);
                    275: }
                    276:
                    277: /*
                    278:  * This just returns the group, we still need to generate the exchange
                    279:  * value.
                    280:  */
                    281:
                    282: DH *
                    283: dh_new_group(BIGNUM *gen, BIGNUM *modulus)
                    284: {
                    285:        DH *dh;
                    286:
1.18      markus    287:        if ((dh = DH_new()) == NULL)
                    288:                fatal("dh_new_group: DH_new");
1.11      markus    289:        dh->p = modulus;
                    290:        dh->g = gen;
                    291:
                    292:        return (dh);
                    293: }
                    294:
                    295: DH *
                    296: dh_new_group1(void)
                    297: {
                    298:        static char *gen = "2", *group1 =
                    299:            "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
                    300:            "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
                    301:            "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
                    302:            "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
                    303:            "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
                    304:            "FFFFFFFF" "FFFFFFFF";
                    305:
                    306:        return (dh_new_group_asc(gen, group1));
1.30      djm       307: }
                    308:
                    309: DH *
                    310: dh_new_group14(void)
                    311: {
                    312:        static char *gen = "2", *group14 =
                    313:            "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
                    314:            "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
                    315:            "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
                    316:            "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
                    317:            "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
                    318:            "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
                    319:            "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
                    320:            "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
                    321:            "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
                    322:            "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
                    323:            "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF";
                    324:
                    325:        return (dh_new_group_asc(gen, group14));
1.12      markus    326: }
                    327:
                    328: /*
                    329:  * Estimates the group order for a Diffie-Hellman group that has an
                    330:  * attack complexity approximately the same as O(2**bits).  Estimate
                    331:  * with:  O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
                    332:  */
                    333:
                    334: int
                    335: dh_estimate(int bits)
                    336: {
                    337:
1.26      markus    338:        if (bits <= 128)
1.12      markus    339:                return (1024);  /* O(2**86) */
1.26      markus    340:        if (bits <= 192)
1.12      markus    341:                return (2048);  /* O(2**116) */
                    342:        return (4096);          /* O(2**156) */
1.1       provos    343: }