From: Dr. Stephen Henson Date: Thu, 7 Apr 2011 15:01:48 +0000 (+0000) Subject: DH keys have an (until now) unused 'q' parameter. When creating X-Git-Tag: OpenSSL-fips-2_0-rc1~584 X-Git-Url: https://git.openssl.org/?p=openssl.git;a=commitdiff_plain;h=31360957fb866264a82d0aa63a18a76740c32cb0 DH keys have an (until now) unused 'q' parameter. When creating from DSA copy q across and if q present generate DH key in the correct range. --- diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c index 6c7a457267..50e8011c83 100644 --- a/crypto/dh/dh_key.c +++ b/crypto/dh/dh_key.c @@ -166,8 +166,21 @@ static int generate_key(DH *dh) if (generate_new_key) { - 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 (dh->q) + { + do + { + if (!BN_rand_range(priv_key, dh->q)) + goto err; + } + while (BN_is_zero(priv_key) || BN_is_one(priv_key)); + } + else + { + /* secret exponent length */ + l = dh->length ? dh->length : BN_num_bits(dh->p)-1; + if (!BN_rand(priv_key, l, 0, 0)) goto err; + } } { diff --git a/crypto/dsa/dsa_lib.c b/crypto/dsa/dsa_lib.c index e9b75902db..12f83ed848 100644 --- a/crypto/dsa/dsa_lib.c +++ b/crypto/dsa/dsa_lib.c @@ -276,7 +276,8 @@ void *DSA_get_ex_data(DSA *d, int idx) DH *DSA_dup_DH(const DSA *r) { /* DSA has p, q, g, optional pub_key, optional priv_key. - * DH has p, optional length, g, optional pub_key, optional priv_key. + * DH has p, optional length, g, optional pub_key, optional priv_key, + * optional q. */ DH *ret = NULL; @@ -290,7 +291,11 @@ DH *DSA_dup_DH(const DSA *r) if ((ret->p = BN_dup(r->p)) == NULL) goto err; if (r->q != NULL) + { ret->length = BN_num_bits(r->q); + if ((ret->q = BN_dup(r->q)) == NULL) + goto err; + } if (r->g != NULL) if ((ret->g = BN_dup(r->g)) == NULL) goto err;