summaryrefslogtreecommitdiffstats
path: root/crypto/dh/dh_key.c
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-10 15:52:36 +0200
committerBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-24 14:59:52 +0200
commitddd16c2fe988ed9fdd5118c2f2617745438fd675 (patch)
tree08eb4554b256062b40bbc9885edf2153c2b87fcf /crypto/dh/dh_key.c
parent8e747338593f3bafe9798226cddf4edf36bc2de9 (diff)
Change DH parameters to generate the order q subgroup instead of 2q
This avoids leaking bit 0 of the private key. Backport-of: #9363 Reviewed-by: Kurt Roeckx <kurt@roeckx.be> (Merged from https://github.com/openssl/openssl/pull/9435)
Diffstat (limited to 'crypto/dh/dh_key.c')
-rw-r--r--crypto/dh/dh_key.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
index 99c00e5a05..718aa422d9 100644
--- a/crypto/dh/dh_key.c
+++ b/crypto/dh/dh_key.c
@@ -125,6 +125,15 @@ static int generate_key(DH *dh)
l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
goto err;
+ /*
+ * We handle just one known case where g is a quadratic non-residue:
+ * for g = 2: p % 8 == 3
+ */
+ if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
+ /* clear bit 0, since it won't be a secret anyway */
+ if (!BN_clear_bit(priv_key, 0))
+ goto err;
+ }
}
}
@@ -136,11 +145,11 @@ static int generate_key(DH *dh)
BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
- BN_free(prk);
+ BN_clear_free(prk);
goto err;
}
/* We MUST free prk before any further use of priv_key */
- BN_free(prk);
+ BN_clear_free(prk);
}
dh->pub_key = pub_key;