*/
#include "includes.h"
-RCSID("$OpenBSD: dh.c,v 1.13 2001/04/04 23:09:17 markus Exp $");
+RCSID("$OpenBSD: dh.c,v 1.23 2002/11/21 22:22:50 markus Exp $");
#include "xmalloc.h"
#include "log.h"
#include "misc.h"
-int
+static int
parse_prime(int linenum, char *line, struct dhgroup *dhg)
{
char *cp, *arg;
/* Ignore leading whitespace */
if (*arg == '\0')
arg = strdelim(&cp);
- if (!*arg || *arg == '#')
+ if (!arg || !*arg || *arg == '#')
return 0;
/* time */
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)
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);
choose_dh(int min, int wantbits, int max)
{
FILE *f;
- char line[1024];
+ char line[2048];
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);
+ if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL &&
+ (f = fopen(_PATH_DH_PRIMES, "r")) == NULL) {
+ log("WARNING: %s does not exist, using old modulus", _PATH_DH_MODULI);
return (dh_new_group1());
}
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;
if (dhg.size == best)
bestcount++;
}
- fclose (f);
+ rewind(f);
if (bestcount == 0) {
+ fclose(f);
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);
- }
-
linenum = 0;
which = arc4random() % bestcount;
while (fgets(line, sizeof(line), f)) {
if ((dhg.size > max || dhg.size < min) ||
dhg.size != best ||
linenum++ != which) {
- BN_free(dhg.g);
- BN_free(dhg.p);
+ BN_clear_free(dhg.g);
+ BN_clear_free(dhg.p);
continue;
}
break;
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))
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))
for (i = 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");
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);
{
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;