User,Utils: move ConstantTimeEquals to CUtils and add tests (#2011)

This commit is contained in:
MarkLee131
2026-04-25 17:37:33 +08:00
parent 794ae6cd60
commit f0c3341e35
5 changed files with 102 additions and 22 deletions
+49
View File
@@ -16,6 +16,7 @@
#include <gtest/gtest.h>
#include <znc/User.h>
#include <znc/Utils.h>
#include <znc/znc.h>
class UserTest : public ::testing::Test {
@@ -149,3 +150,51 @@ TEST_F(UserTest, TestAuthOnlyViaModule) {
CZNC::Get().SetAuthOnlyViaModule(bAuthOnlyViaModuleDefault);
}
// Functional regression for the constant-time CheckPass paths (#2011).
// We can't measure timing in a unit test, but we verify the boolean
// contract for each hash mode: correct password matches, wrong password
// (including same-length and length-mismatch variants) does not.
TEST_F(UserTest, CheckPassPlain) {
CUser user("user");
user.SetPass("plaintext", CUser::HASH_NONE);
EXPECT_FALSE(user.CheckPass(""));
EXPECT_FALSE(user.CheckPass("plaintex")); // shorter
EXPECT_FALSE(user.CheckPass("plaintextX")); // longer
EXPECT_FALSE(user.CheckPass("plaintexT")); // case-sensitive
EXPECT_FALSE(user.CheckPass("Xlaintext")); // first byte differs
EXPECT_FALSE(user.CheckPass("plaintexX")); // last byte differs
EXPECT_TRUE(user.CheckPass("plaintext"));
}
TEST_F(UserTest, CheckPassMD5) {
CUser user("user");
CString sSalt = "the-salt";
CString sCorrect = "correct-password";
user.SetPass(CUtils::SaltedMD5Hash(sCorrect, sSalt), CUser::HASH_MD5,
sSalt);
// Wrong inputs first; the success path may upgrade the stored hash
// to argon2id, after which the hash type is no longer MD5.
EXPECT_FALSE(user.CheckPass(""));
EXPECT_FALSE(user.CheckPass("wrong-password"));
EXPECT_FALSE(user.CheckPass("correct-passworX")); // last byte differs
EXPECT_FALSE(user.CheckPass("Xorrect-password")); // first byte differs
EXPECT_FALSE(user.CheckPass("Correct-password")); // case-sensitive
EXPECT_TRUE(user.CheckPass(sCorrect));
}
TEST_F(UserTest, CheckPassSHA256) {
CUser user("user");
CString sSalt = "another-salt";
CString sCorrect = "another-password";
user.SetPass(CUtils::SaltedSHA256Hash(sCorrect, sSalt),
CUser::HASH_SHA256, sSalt);
EXPECT_FALSE(user.CheckPass(""));
EXPECT_FALSE(user.CheckPass("wrong"));
EXPECT_FALSE(user.CheckPass("another-passworX"));
EXPECT_FALSE(user.CheckPass("Another-password"));
EXPECT_TRUE(user.CheckPass(sCorrect));
}
+28
View File
@@ -141,6 +141,34 @@ TEST(UtilsTest, ServerTime) {
tzset();
}
TEST(UtilsTest, ConstantTimeEquals) {
// Functional correctness for the helper introduced for #2011.
// We can't measure timing in a unit test, so we verify the boolean
// contract: equal inputs match, any difference (length or content)
// does not.
EXPECT_TRUE(CUtils::ConstantTimeEquals("", ""));
EXPECT_TRUE(CUtils::ConstantTimeEquals("abc", "abc"));
EXPECT_TRUE(CUtils::ConstantTimeEquals(CString("\x00\x01\x02", 3),
CString("\x00\x01\x02", 3)));
// Differs in last byte (the hardest case for short-circuit compare).
EXPECT_FALSE(CUtils::ConstantTimeEquals("abc", "abd"));
// Differs in first byte.
EXPECT_FALSE(CUtils::ConstantTimeEquals("abc", "Xbc"));
// Length mismatch on either side.
EXPECT_FALSE(CUtils::ConstantTimeEquals("abc", "abcd"));
EXPECT_FALSE(CUtils::ConstantTimeEquals("abcd", "abc"));
EXPECT_FALSE(CUtils::ConstantTimeEquals("", "x"));
EXPECT_FALSE(CUtils::ConstantTimeEquals("x", ""));
// Case-sensitive (unlike CString::Equals default).
EXPECT_FALSE(CUtils::ConstantTimeEquals("abc", "ABC"));
// Embedded NUL is compared, not used as a terminator.
EXPECT_FALSE(CUtils::ConstantTimeEquals(CString("a\x00""c", 3),
CString("a\x00""d", 3)));
EXPECT_TRUE(CUtils::ConstantTimeEquals(CString("a\x00""c", 3),
CString("a\x00""c", 3)));
}
TEST(UtilsTest, ParseServerTime) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);