Skip to content

Commit

Permalink
Fix Excessive time spent checking DH q parameter value (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail authored Aug 1, 2023
1 parent a818ddf commit 1bb574f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 21 additions & 0 deletions crypto/dh_extra/dh_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ TEST(DHTest, OversizedModulus) {
ASSERT_EQ(DH_R_MODULUS_TOO_LARGE, ERR_GET_REASON(error));
}

TEST(DHTest, LargeQ) {
bssl::UniquePtr<DH> a(DH_new());
ASSERT_TRUE(a);
ASSERT_TRUE(DH_generate_parameters_ex(a.get(), 64, DH_GENERATOR_5, nullptr));

bssl::UniquePtr<BIGNUM> q(BN_new());
ASSERT_TRUE(q);
BN_set_word(q.get(), 2039L);

a.get()->q = q.release();

ASSERT_TRUE(DH_generate_key(a.get()));

ASSERT_TRUE(BN_copy(a.get()->q, a.get()->p));
ASSERT_TRUE(BN_add(a.get()->q, a.get()->q, BN_value_one()));

int check_result;
ASSERT_TRUE(DH_check(a.get(), &check_result));
ASSERT_TRUE(check_result & DH_CHECK_INVALID_Q_VALUE);
}

// The following parameters are taken from RFC 5114, section 2.2. This is not a
// safe prime. Do not use these parameters.
static const uint8_t kRFC5114_2048_224P[] = {
Expand Down
10 changes: 9 additions & 1 deletion crypto/fipsmodule/dh/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int DH_check(const DH *dh, int *out_flags) {
// for 3, p mod 12 == 5
// for 5, p mod 10 == 3 or 7
// should hold.
int ok = 0, r;
int ok = 0, r, q_good = 0;
BN_CTX *ctx = NULL;
BN_ULONG l;
BIGNUM *t1 = NULL, *t2 = NULL;
Expand Down Expand Up @@ -148,6 +148,14 @@ int DH_check(const DH *dh, int *out_flags) {
}

if (dh->q) {
if (BN_ucmp(dh->p, dh->q) > 0) {
q_good = 1;
} else {
*out_flags |= DH_CHECK_INVALID_Q_VALUE;
}
}

if (q_good) {
if (BN_cmp(dh->g, BN_value_one()) <= 0) {
*out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR;
} else if (BN_cmp(dh->g, dh->p) >= 0) {
Expand Down

0 comments on commit 1bb574f

Please sign in to comment.