• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 26995937053

04 Jun 2026 09:38PM UTC coverage: 89.394% (-2.3%) from 91.672%
26995937053

push

github

web-flow
Merge pull request #5642 from randombit/jack/prefetch-in-ks

Improve prefetching for table based implementations

110588 of 123708 relevant lines covered (89.39%)

11056434.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.77
/src/tests/test_otp.cpp
1
/*
2
* OTP tests
3
* (C) 2017 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_HOTP) && defined(BOTAN_HAS_TOTP)
11
   #include <botan/hash.h>
12
   #include <botan/otp.h>
13
   #include <botan/internal/calendar.h>
14
   #include <botan/internal/parsing.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
namespace {
20

21
#if defined(BOTAN_HAS_HOTP) && defined(BOTAN_HAS_TOTP)
22

23
class HOTP_KAT_Tests final : public Text_Based_Test {
×
24
   public:
25
      HOTP_KAT_Tests() : Text_Based_Test("otp/hotp.vec", "Key,Digits,Counter,OTP") {}
2✔
26

27
      bool clear_between_callbacks() const override { return false; }
32✔
28

29
      Test::Result run_one_test(const std::string& hash_algo, const VarMap& vars) override {
32✔
30
         Test::Result result("HOTP " + hash_algo);
32✔
31

32
         auto hash_test = Botan::HashFunction::create(hash_algo);
32✔
33
         if(!hash_test) {
32✔
34
            return {result};
×
35
         }
36

37
         const auto key = Botan::SymmetricKey(vars.get_req_bin("Key"));
32✔
38
         const uint32_t otp = static_cast<uint32_t>(vars.get_req_sz("OTP"));
32✔
39
         const uint64_t counter = vars.get_req_sz("Counter");
32✔
40
         const size_t digits = vars.get_req_sz("Digits");
32✔
41

42
         Botan::HOTP hotp(key, hash_algo, digits);
32✔
43

44
         result.test_u32_eq("OTP", hotp.generate_hotp(counter), otp);
32✔
45

46
         std::pair<bool, uint64_t> otp_res = hotp.verify_hotp(otp, counter, 0);
32✔
47
         result.test_is_true("OTP verify result", otp_res.first);
32✔
48
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter + 1);
32✔
49

50
         // Test invalid OTP
51
         otp_res = hotp.verify_hotp(otp + 1, counter, 0);
32✔
52
         result.test_is_false("OTP verify result", otp_res.first);
32✔
53
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter);
32✔
54

55
         // Test invalid OTP with long range
56
         otp_res = hotp.verify_hotp(otp + 1, counter, 100);
32✔
57
         result.test_is_false("OTP verify result", otp_res.first);
32✔
58
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter);
32✔
59

60
         // Test valid OTP with long range
61
         otp_res = hotp.verify_hotp(otp, counter - 90, 100);
32✔
62
         result.test_is_true("OTP verify result", otp_res.first);
32✔
63
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter + 1);
32✔
64

65
         return result;
32✔
66
      }
96✔
67
};
68

69
BOTAN_REGISTER_TEST("otp", "otp_hotp", HOTP_KAT_Tests);
70

71
class TOTP_KAT_Tests final : public Text_Based_Test {
×
72
   public:
73
      TOTP_KAT_Tests() : Text_Based_Test("otp/totp.vec", "Key,Digits,Timestep,Timestamp,OTP") {}
2✔
74

75
      bool clear_between_callbacks() const override { return false; }
4✔
76

77
      Test::Result run_one_test(const std::string& hash_algo, const VarMap& vars) override {
4✔
78
         Test::Result result("TOTP " + hash_algo);
4✔
79

80
         auto hash_test = Botan::HashFunction::create(hash_algo);
4✔
81
         if(!hash_test) {
4✔
82
            return {result};
×
83
         }
84

85
         const auto key = Botan::SymmetricKey(vars.get_req_bin("Key"));
4✔
86
         const uint32_t otp = static_cast<uint32_t>(vars.get_req_sz("OTP"));
4✔
87
         const size_t digits = vars.get_req_sz("Digits");
4✔
88
         const size_t timestep = vars.get_req_sz("Timestep");
4✔
89
         const std::string timestamp = vars.get_req_str("Timestamp");
4✔
90

91
         Botan::TOTP totp(key, hash_algo, digits, timestep);
4✔
92

93
         const std::chrono::system_clock::time_point time = from_timestring(timestamp);
4✔
94
         const std::chrono::system_clock::time_point later_time = time + std::chrono::seconds(timestep);
4✔
95
         const std::chrono::system_clock::time_point too_late = time + std::chrono::seconds(2 * timestep);
4✔
96

97
         result.test_u32_eq("TOTP generate", totp.generate_totp(time), otp);
4✔
98

99
         result.test_is_true("TOTP verify valid", totp.verify_totp(otp, time, 0));
4✔
100
         result.test_is_false("TOTP verify invalid", totp.verify_totp(otp ^ 1, time, 0));
4✔
101
         result.test_is_false("TOTP verify time slip", totp.verify_totp(otp, later_time, 0));
4✔
102
         result.test_is_true("TOTP verify time slip allowed", totp.verify_totp(otp, later_time, 1));
4✔
103
         result.test_is_false("TOTP verify time slip out of range", totp.verify_totp(otp, too_late, 1));
4✔
104

105
         return result;
4✔
106
      }
12✔
107

108
   private:
109
      static std::chrono::system_clock::time_point from_timestring(const std::string& time_str) {
4✔
110
         if(time_str.size() != 19) {
4✔
111
            throw Test_Error("Invalid TOTP timestamp string " + time_str);
×
112
         }
113
         // YYYY-MM-DDTHH:MM:SS
114
         // 0123456789012345678
115
         const auto year = Botan::parse_u32(time_str.substr(0, 4));
4✔
116
         const auto month = Botan::parse_u32(time_str.substr(5, 2));
4✔
117
         const auto day = Botan::parse_u32(time_str.substr(8, 2));
4✔
118
         const auto hour = Botan::parse_u32(time_str.substr(11, 2));
4✔
119
         const auto minute = Botan::parse_u32(time_str.substr(14, 2));
4✔
120
         const auto second = Botan::parse_u32(time_str.substr(17, 2));
4✔
121

122
         if(year && month && day && hour && minute && second) {
4✔
123
            return Botan::calendar_point(*year, *month, *day, *hour, *minute, *second).to_std_timepoint();
4✔
124
         } else {
125
            throw Test_Error("Invalid TOTP timestamp string " + time_str);
×
126
         }
127
      }
128
};
129

130
BOTAN_REGISTER_TEST("otp", "otp_totp", TOTP_KAT_Tests);
131

132
#endif
133

134
}  // namespace
135

136
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc