]> andersk Git - openssh.git/blobdiff - dh.c
- (tim) [configure.ac] set TEST_SHELL for OpenServer 6
[openssh.git] / dh.c
diff --git a/dh.c b/dh.c
index 6c53b0038d0175c7c8ff8be46d6151eba9691f78..044d869fb8838934bbc88cb8fc635bbbfd1235f1 100644 (file)
--- a/dh.c
+++ b/dh.c
@@ -23,7 +23,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: dh.c,v 1.11 2001/03/29 21:17:39 markus Exp $");
+RCSID("$OpenBSD: dh.c,v 1.31 2004/08/04 10:37:52 djm Exp $");
 
 #include "xmalloc.h"
 
@@ -39,7 +39,7 @@ RCSID("$OpenBSD: dh.c,v 1.11 2001/03/29 21:17:39 markus Exp $");
 #include "log.h"
 #include "misc.h"
 
-int
+static int
 parse_prime(int linenum, char *line, struct dhgroup *dhg)
 {
        char *cp, *arg;
@@ -50,7 +50,7 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
        /* Ignore leading whitespace */
        if (*arg == '\0')
                arg = strdelim(&cp);
-       if (!*arg || *arg == '#')
+       if (!arg || !*arg || *arg == '#')
                return 0;
 
        /* time */
@@ -78,22 +78,27 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
        if (cp != NULL || *prime == '\0')
                goto fail;
 
-       dhg->g = BN_new();
-       dhg->p = BN_new();
-       if (BN_hex2bn(&dhg->g, gen) < 0)
+       if ((dhg->g = BN_new()) == NULL)
+               fatal("parse_prime: BN_new failed");
+       if ((dhg->p = BN_new()) == NULL)
+               fatal("parse_prime: BN_new failed");
+       if (BN_hex2bn(&dhg->g, gen) == 0)
                goto failclean;
 
-       if (BN_hex2bn(&dhg->p, prime) < 0)
+       if (BN_hex2bn(&dhg->p, prime) == 0)
                goto failclean;
 
        if (BN_num_bits(dhg->p) != dhg->size)
                goto failclean;
 
+       if (BN_is_zero(dhg->g) || BN_is_one(dhg->g))
+               goto failclean;
+
        return (1);
 
  failclean:
-       BN_free(dhg->g);
-       BN_free(dhg->p);
+       BN_clear_free(dhg->g);
+       BN_clear_free(dhg->p);
  fail:
        error("Bad prime description in line %d", linenum);
        return (0);
@@ -103,15 +108,16 @@ DH *
 choose_dh(int min, int wantbits, int max)
 {
        FILE *f;
-       char line[1024];
+       char line[4096];
        int best, bestcount, which;
        int linenum;
        struct dhgroup dhg;
 
-       f = fopen(_PATH_DH_PRIMES, "r");
-       if (!f) {
-               log("WARNING: %s does not exist, using old prime", _PATH_DH_PRIMES);
-               return (dh_new_group1());
+       if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL &&
+           (f = fopen(_PATH_DH_PRIMES, "r")) == NULL) {
+               logit("WARNING: %s does not exist, using fixed modulus",
+                   _PATH_DH_MODULI);
+               return (dh_new_group14());
        }
 
        linenum = 0;
@@ -120,8 +126,8 @@ choose_dh(int min, int wantbits, int max)
                linenum++;
                if (!parse_prime(linenum, line, &dhg))
                        continue;
-               BN_free(dhg.g);
-               BN_free(dhg.p);
+               BN_clear_free(dhg.g);
+               BN_clear_free(dhg.p);
 
                if (dhg.size > max || dhg.size < min)
                        continue;
@@ -134,16 +140,12 @@ choose_dh(int min, int wantbits, int max)
                if (dhg.size == best)
                        bestcount++;
        }
-       fclose (f);
+       rewind(f);
 
        if (bestcount == 0) {
-               log("WARNING: no suitable primes in %s", _PATH_DH_PRIMES);
-               return (NULL);
-       }
-
-       f = fopen(_PATH_DH_PRIMES, "r");
-       if (!f) {
-               fatal("WARNING: %s disappeared, giving up", _PATH_DH_PRIMES);
+               fclose(f);
+               logit("WARNING: no suitable primes in %s", _PATH_DH_PRIMES);
+               return (dh_new_group14());
        }
 
        linenum = 0;
@@ -151,23 +153,24 @@ choose_dh(int min, int wantbits, int max)
        while (fgets(line, sizeof(line), f)) {
                if (!parse_prime(linenum, line, &dhg))
                        continue;
-               if (dhg.size > max || dhg.size < min)
-                       continue;
-               if (dhg.size != best)
-                       continue;
-               if (linenum++ != which) {
-                       BN_free(dhg.g);
-                       BN_free(dhg.p);
+               if ((dhg.size > max || dhg.size < min) ||
+                   dhg.size != best ||
+                   linenum++ != which) {
+                       BN_clear_free(dhg.g);
+                       BN_clear_free(dhg.p);
                        continue;
                }
                break;
        }
        fclose(f);
+       if (linenum != which+1)
+               fatal("WARNING: line %d disappeared in %s, giving up",
+                   which, _PATH_DH_PRIMES);
 
        return (dh_new_group(dhg.g, dhg.p));
 }
 
-/* diffie-hellman-group1-sha1 */
+/* diffie-hellman-groupN-sha1 */
 
 int
 dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
@@ -177,46 +180,45 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
        int bits_set = 0;
 
        if (dh_pub->neg) {
-               log("invalid public DH value: negativ");
+               logit("invalid public DH value: negativ");
                return 0;
        }
        for (i = 0; i <= n; i++)
                if (BN_is_bit_set(dh_pub, i))
                        bits_set++;
-       debug("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
+       debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
 
        /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */
        if (bits_set > 1 && (BN_cmp(dh_pub, dh->p) == -1))
                return 1;
-       log("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
+       logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
        return 0;
 }
 
 void
 dh_gen_key(DH *dh, int need)
 {
-       int i, bits_set = 0, tries = 0;
+       int i, bits_set, tries = 0;
 
        if (dh->p == NULL)
                fatal("dh_gen_key: dh->p == NULL");
-       if (2*need >= BN_num_bits(dh->p))
+       if (need > INT_MAX / 2 || 2 * need >= BN_num_bits(dh->p))
                fatal("dh_gen_key: group too small: %d (2*need %d)",
                    BN_num_bits(dh->p), 2*need);
        do {
                if (dh->priv_key != NULL)
-                       BN_free(dh->priv_key);
-               dh->priv_key = BN_new();
-               if (dh->priv_key == NULL)
+                       BN_clear_free(dh->priv_key);
+               if ((dh->priv_key = BN_new()) == NULL)
                        fatal("dh_gen_key: BN_new failed");
                /* generate a 2*need bits random private exponent */
                if (!BN_rand(dh->priv_key, 2*need, 0, 0))
                        fatal("dh_gen_key: BN_rand failed");
                if (DH_generate_key(dh) == 0)
                        fatal("DH_generate_key");
-               for (i = 0; i <= BN_num_bits(dh->priv_key); i++)
+               for (i = 0, bits_set = 0; i <= BN_num_bits(dh->priv_key); i++)
                        if (BN_is_bit_set(dh->priv_key, i))
                                bits_set++;
-               debug("dh_gen_key: priv key bits set: %d/%d",
+               debug2("dh_gen_key: priv key bits set: %d/%d",
                    bits_set, BN_num_bits(dh->priv_key));
                if (tries++ > 10)
                        fatal("dh_gen_key: too many bad keys: giving up");
@@ -227,15 +229,13 @@ DH *
 dh_new_group_asc(const char *gen, const char *modulus)
 {
        DH *dh;
-       int ret;
 
-       dh = DH_new();
-       if (dh == NULL)
-               fatal("DH_new");
+       if ((dh = DH_new()) == NULL)
+               fatal("dh_new_group_asc: DH_new");
 
-       if ((ret = BN_hex2bn(&dh->p, modulus)) < 0)
+       if (BN_hex2bn(&dh->p, modulus) == 0)
                fatal("BN_hex2bn p");
-       if ((ret = BN_hex2bn(&dh->g, gen)) < 0)
+       if (BN_hex2bn(&dh->g, gen) == 0)
                fatal("BN_hex2bn g");
 
        return (dh);
@@ -251,9 +251,8 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulus)
 {
        DH *dh;
 
-       dh = DH_new();
-       if (dh == NULL)
-               fatal("DH_new");
+       if ((dh = DH_new()) == NULL)
+               fatal("dh_new_group: DH_new");
        dh->p = modulus;
        dh->g = gen;
 
@@ -273,3 +272,39 @@ dh_new_group1(void)
 
        return (dh_new_group_asc(gen, group1));
 }
+
+DH *
+dh_new_group14(void)
+{
+       static char *gen = "2", *group14 =
+           "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
+           "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
+           "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
+           "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
+           "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
+           "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
+           "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
+           "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
+           "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
+           "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
+           "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF";
+
+       return (dh_new_group_asc(gen, group14));
+}
+
+/*
+ * Estimates the group order for a Diffie-Hellman group that has an
+ * attack complexity approximately the same as O(2**bits).  Estimate
+ * with:  O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
+ */
+
+int
+dh_estimate(int bits)
+{
+
+       if (bits <= 128)
+               return (1024);  /* O(2**86) */
+       if (bits <= 192)
+               return (2048);  /* O(2**116) */
+       return (4096);          /* O(2**156) */
+}
This page took 0.044388 seconds and 4 git commands to generate.