Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hmac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace hmac {

bool constant_time_equals(const std::string &a, const std::string &b) {
size_t max_len = a.size() > b.size() ? a.size() : b.size();
unsigned char diff = static_cast<unsigned char>(a.size() ^ b.size());
unsigned int diff = (a.size() != b.size());
for (size_t i = 0; i < max_len; ++i) {
unsigned char ac = i < a.size() ? static_cast<unsigned char>(a[i]) : 0;
unsigned char bc = i < b.size() ? static_cast<unsigned char>(b[i]) : 0;
Expand Down
8 changes: 8 additions & 0 deletions test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ TEST(UtilsTest, ConstantTimeEqualsMismatch) {
EXPECT_FALSE(hmac::constant_time_equals("alpha", "alphabet"));
}

TEST(UtilsTest, ConstantTimeEqualsLengthMultiples256) {
std::string base(256, 'a');
std::string plus256 = base + std::string(256, '\0');
std::string plus512 = base + std::string(512, '\0');
EXPECT_FALSE(hmac::constant_time_equals(base, plus256));
EXPECT_FALSE(hmac::constant_time_equals(base, plus512));
}

TEST(HMACTest, SHA256) {
const std::string key = "12345";
const std::string input = "grape";
Expand Down
Loading