-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_totp.cpp
More file actions
130 lines (118 loc) · 5.39 KB
/
test_totp.cpp
File metadata and controls
130 lines (118 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <gtest/gtest.h>
#include <limits>
#include "hmac_cpp/hmac_utils.hpp"
TEST(HOTPTest, RFC4226Vectors) {
const std::string key = "12345678901234567890";
const int digits = 6;
struct Case { uint64_t counter; int code; } cases[] = {
{0, 755224}, {1, 287082}, {2, 359152}, {3, 969429}, {4, 338314},
{5, 254676}, {6, 287922}, {7, 162583}, {8, 399871}, {9, 520489},
};
for (const auto& c : cases) {
EXPECT_EQ(hmac::get_hotp_code(key.data(), key.size(), c.counter, digits, hmac::TypeHash::SHA1), c.code);
}
}
TEST(TOTPTest, RFC6238SHA1) {
const std::string key = "12345678901234567890";
const int digits = 8;
struct Case { uint64_t time; int code; } cases[] = {
{59, 94287082}, {1111111109, 7081804}, {1111111111, 14050471},
{1234567890, 89005924}, {2000000000, 69279037}, {20000000000ULL, 65353130},
};
for (const auto& c : cases) {
EXPECT_EQ(hmac::get_totp_code_at(key.data(), key.size(), c.time, 30, digits, hmac::TypeHash::SHA1), c.code);
}
}
TEST(TOTPTest, RFC6238SHA256) {
const std::string key = "12345678901234567890123456789012";
const int digits = 8;
struct Case { uint64_t time; int code; } cases[] = {
{59, 46119246}, {1111111109, 68084774}, {1111111111, 67062674},
{1234567890, 91819424}, {2000000000, 90698825}, {20000000000ULL, 77737706},
};
for (const auto& c : cases) {
EXPECT_EQ(hmac::get_totp_code_at(key.data(), key.size(), c.time, 30, digits, hmac::TypeHash::SHA256), c.code);
}
}
TEST(TOTPTest, RFC6238SHA512) {
const std::string key = "1234567890123456789012345678901234567890123456789012345678901234";
const int digits = 8;
struct Case { uint64_t time; int code; } cases[] = {
{59, 90693936}, {1111111109, 25091201}, {1111111111, 99943326},
{1234567890, 93441116}, {2000000000, 38618901}, {20000000000ULL, 47863826},
};
for (const auto& c : cases) {
EXPECT_EQ(hmac::get_totp_code_at(key.data(), key.size(), c.time, 30, digits, hmac::TypeHash::SHA512), c.code);
}
}
TEST(TotpBoundaryTest, EarlyTimestampValidatesCounterZero) {
std::string key = "12345678901234567890";
int digits = 6;
uint64_t period = 30;
uint64_t early_timestamp = 5; // less than one period
int token = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1);
EXPECT_TRUE(hmac::is_totp_token_valid(token, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1));
}
TEST(TotpBoundaryTest, MaxCounterInvalidAtEarlyTimestamp) {
std::string key = "12345678901234567890";
int digits = 6;
uint64_t period = 30;
uint64_t early_timestamp = 5;
uint64_t max_counter = std::numeric_limits<uint64_t>::max();
int token = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1);
EXPECT_FALSE(hmac::is_totp_token_valid(token, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1));
}
TEST(TotpBoundaryTest, MaxTimestampDoesNotValidateCounterZero) {
std::string key = "12345678901234567890";
int digits = 6;
uint64_t max_timestamp = std::numeric_limits<uint64_t>::max();
int token = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1);
EXPECT_FALSE(hmac::is_totp_token_valid(token, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1));
}
TEST(TotpBoundaryTest, MaxTimestampValidatesMaxCounter) {
std::string key = "12345678901234567890";
int digits = 6;
uint64_t max_timestamp = std::numeric_limits<uint64_t>::max();
uint64_t max_counter = std::numeric_limits<uint64_t>::max();
int token = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1);
EXPECT_TRUE(hmac::is_totp_token_valid(token, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1));
}
TEST(HotpTest, RFC4226Vectors) {
std::string key = "12345678901234567890";
int expected[10] = {755224, 287082, 359152, 969429, 338314,
254676, 287922, 162583, 399871, 520489};
for (uint64_t counter = 0; counter < 10; ++counter) {
EXPECT_EQ(hmac::get_hotp_code(key.data(), key.size(), counter,
6, hmac::TypeHash::SHA1),
expected[counter])
<< "Counter " << counter;
}
}
TEST(TotpTest, RFC6238Vectors) {
struct Vector {
uint64_t time;
int sha1;
int sha256;
int sha512;
};
std::vector<Vector> vectors = {
{59, 94287082, 46119246, 90693936},
{1111111109, 7081804, 68084774, 25091201},
{1111111111, 14050471, 67062674, 99943326},
{1234567890, 89005924, 91819424, 93441116},
{2000000000, 69279037, 90698825, 38618901},
{20000000000ULL, 65353130, 77737706, 47863826},
};
std::string key_sha1 = "12345678901234567890";
std::string key_sha256 = "12345678901234567890123456789012";
std::string key_sha512 =
"1234567890123456789012345678901234567890123456789012345678901234";
for (const auto& v : vectors) {
EXPECT_EQ(hmac::get_totp_code_at(key_sha1, v.time, 30, 8, hmac::TypeHash::SHA1),
v.sha1);
EXPECT_EQ(hmac::get_totp_code_at(key_sha256, v.time, 30, 8, hmac::TypeHash::SHA256),
v.sha256);
EXPECT_EQ(hmac::get_totp_code_at(key_sha512, v.time, 30, 8, hmac::TypeHash::SHA512),
v.sha512);
}
}