X-Git-Url: https://git.openssl.org/?p=openssl.git;a=blobdiff_plain;f=crypto%2Fdh%2Fdh_key.c;h=df0300402e2dc54113b373c26f7cd2b869ed8154;hp=91af882e43356860459ddd4a91e6025c405b74c2;hb=e5cb2603652b868225adc1db3db531a07c13b562;hpb=c62b26fdc6bb176541ec56498090ff6f2ad4a885 diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c index 91af882e43..df0300402e 100644 --- a/crypto/dh/dh_key.c +++ b/crypto/dh/dh_key.c @@ -101,6 +101,7 @@ const DH_METHOD *DH_OpenSSL(void) static int generate_key(DH *dh) { int ok=0; + unsigned l; BN_CTX *ctx; BN_MONT_CTX *mont; BIGNUM *pub_key=NULL,*priv_key=NULL; @@ -112,9 +113,6 @@ static int generate_key(DH *dh) { priv_key=BN_new(); if (priv_key == NULL) goto err; - do - if (!BN_rand_range(priv_key, dh->p)) goto err; - while (BN_is_zero(priv_key)); } else priv_key=dh->priv_key; @@ -135,9 +133,11 @@ static int generate_key(DH *dh) } mont=(BN_MONT_CTX *)dh->method_mont_p; + l = dh->length ? dh->length : BN_num_bits(dh->p)-1; /* secret exponent length */ + + if (!BN_rand(priv_key, l, 0, 0)) goto err; if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, pub_key, dh->g, - priv_key,dh->p,ctx,mont)) - goto err; + priv_key,dh->p,ctx,mont)) goto err; dh->pub_key=pub_key; dh->priv_key=priv_key;