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
20 changes: 14 additions & 6 deletions hmac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ namespace hmac {
}
std::time_t rounded = (now / interval_sec) * interval_sec;
if (constant_time_equals(token, get_hmac(key, std::to_string(rounded), hash_type))) return true;
if (constant_time_equals(token, get_hmac(key, std::to_string(rounded - interval_sec), hash_type))) return true;
if (constant_time_equals(token, get_hmac(key, std::to_string(rounded + interval_sec), hash_type))) return true;
if (rounded >= std::numeric_limits<std::time_t>::min() + interval_sec) {
if (constant_time_equals(token, get_hmac(key, std::to_string(rounded - interval_sec), hash_type))) return true;
}
if (rounded <= std::numeric_limits<std::time_t>::max() - interval_sec) {
if (constant_time_equals(token, get_hmac(key, std::to_string(rounded + interval_sec), hash_type))) return true;
}
return false;
}

Expand Down Expand Up @@ -66,10 +70,14 @@ namespace hmac {
std::string prefix = "|" + fingerprint;
std::string payload = std::to_string(rounded) + prefix;
if (constant_time_equals(token, get_hmac(key, payload, hash_type))) return true;
payload = std::to_string(rounded - interval_sec) + prefix;
if (constant_time_equals(token, get_hmac(key, payload, hash_type))) return true;
payload = std::to_string(rounded + interval_sec) + prefix;
if (constant_time_equals(token, get_hmac(key, payload, hash_type))) return true;
if (rounded >= std::numeric_limits<std::time_t>::min() + interval_sec) {
payload = std::to_string(rounded - interval_sec) + prefix;
if (constant_time_equals(token, get_hmac(key, payload, hash_type))) return true;
}
if (rounded <= std::numeric_limits<std::time_t>::max() - interval_sec) {
payload = std::to_string(rounded + interval_sec) + prefix;
if (constant_time_equals(token, get_hmac(key, payload, hash_type))) return true;
}
return false;
}

Expand Down
57 changes: 57 additions & 0 deletions test_all.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include <gtest/gtest.h>
#include <string>
#include <stdexcept>
#include <limits>
#include "hmac.hpp"
#include "hmac_utils.hpp"

static std::time_t mock_time_value = 0;
extern "C" std::time_t time(std::time_t* t) {
if (t) *t = mock_time_value;
return mock_time_value;
}

TEST(HashTest, SHA1) {
EXPECT_EQ(hmac_hash::sha1("grape"),
"bc8a2f8cdedb005b5c787692853709b060db75ff");
Expand Down Expand Up @@ -65,6 +72,56 @@ TEST(TokenTest, InvalidInterval) {
EXPECT_THROW(hmac::is_token_valid(token, key, 0), std::invalid_argument);
}

TEST(TokenBoundaryTest, MaxTime) {
const std::string key = "12345";
const int interval = 30;
mock_time_value = std::numeric_limits<std::time_t>::max();
std::string token = hmac::generate_time_token(key, interval);
EXPECT_TRUE(hmac::is_token_valid(token, key, interval));
mock_time_value = std::numeric_limits<std::time_t>::max() - interval;
std::string token_prev = hmac::generate_time_token(key, interval);
mock_time_value = std::numeric_limits<std::time_t>::max();
EXPECT_TRUE(hmac::is_token_valid(token_prev, key, interval));
}

TEST(TokenBoundaryTest, MinTime) {
const std::string key = "12345";
const int interval = 30;
mock_time_value = std::numeric_limits<std::time_t>::min();
std::string token = hmac::generate_time_token(key, interval);
EXPECT_TRUE(hmac::is_token_valid(token, key, interval));
mock_time_value = std::numeric_limits<std::time_t>::min() + interval;
std::string token_next = hmac::generate_time_token(key, interval);
mock_time_value = std::numeric_limits<std::time_t>::min();
EXPECT_TRUE(hmac::is_token_valid(token_next, key, interval));
}

TEST(TokenBoundaryFingerprintTest, MaxTime) {
const std::string key = "12345";
const std::string fingerprint = "fp";
const int interval = 30;
mock_time_value = std::numeric_limits<std::time_t>::max();
std::string token = hmac::generate_time_token(key, fingerprint, interval);
EXPECT_TRUE(hmac::is_token_valid(token, key, fingerprint, interval));
mock_time_value = std::numeric_limits<std::time_t>::max() - interval;
std::string token_prev = hmac::generate_time_token(key, fingerprint, interval);
mock_time_value = std::numeric_limits<std::time_t>::max();
EXPECT_TRUE(hmac::is_token_valid(token_prev, key, fingerprint, interval));
}

TEST(TokenBoundaryFingerprintTest, MinTime) {
const std::string key = "12345";
const std::string fingerprint = "fp";
const int interval = 30;
mock_time_value = std::numeric_limits<std::time_t>::min();
std::string token = hmac::generate_time_token(key, fingerprint, interval);
EXPECT_TRUE(hmac::is_token_valid(token, key, fingerprint, interval));
mock_time_value = std::numeric_limits<std::time_t>::min() + interval;
std::string token_next = hmac::generate_time_token(key, fingerprint, interval);
mock_time_value = std::numeric_limits<std::time_t>::min();
EXPECT_TRUE(hmac::is_token_valid(token_next, key, fingerprint, interval));
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
Loading