Botan 3.13.0
Crypto and TLS for C&
Botan::TOTP Class Referencefinal

#include <otp.h>

Public Member Functions

uint32_t generate_totp (std::chrono::system_clock::time_point time_point)
uint32_t generate_totp (uint64_t unix_time)
BOTAN_FUTURE_EXPLICIT TOTP (const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
 TOTP (const uint8_t key[], size_t key_len, std::string_view hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
bool verify_totp (uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted=0)
bool verify_totp (uint32_t otp, uint64_t unix_time, size_t clock_drift_accepted=0)

Detailed Description

TOTP (time based) one time passwords (RFC 6238)

Definition at line 68 of file otp.h.

Constructor & Destructor Documentation

◆ TOTP() [1/2]

BOTAN_FUTURE_EXPLICIT Botan::TOTP::TOTP ( const SymmetricKey & key,
std::string_view hash_algo = "SHA-1",
size_t digits = 6,
size_t time_step = 30 )
inline

Create a TOTP instance

Parameters
keythe secret key shared between client and server
hash_algothe hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
digitsthe number of digits in the OTP (must be 6, 7, or 8)
time_stepgranularity of OTP in seconds TODO(Botan4) remove the default hash param here

Definition at line 78 of file otp.h.

81 :
82 TOTP(key.begin(), key.size(), hash_algo, digits, time_step) {}
BOTAN_FUTURE_EXPLICIT TOTP(const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
Definition otp.h:78

References BOTAN_FUTURE_EXPLICIT, and TOTP().

Referenced by TOTP().

◆ TOTP() [2/2]

Botan::TOTP::TOTP ( const uint8_t key[],
size_t key_len,
std::string_view hash_algo = "SHA-1",
size_t digits = 6,
size_t time_step = 30 )

Create a TOTP instance

Parameters
keythe secret key shared between client and server
key_lenlength of key
hash_algothe hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
digitsthe number of digits in the OTP (must be 6, 7, or 8)
time_stepgranularity of OTP in seconds TODO(Botan4) remove the default hash param here

Definition at line 15 of file totp.cpp.

15 :
16 m_hotp(key, key_len, hash_algo, digits),
17 m_time_step(time_step),
18 m_unix_epoch(calendar_point(1970, 1, 1, 0, 0, 0).to_std_timepoint()) {
19 /*
20 * Technically any time step except 0 is valid, but 30 is typical
21 * and over 5 minutes seems unlikely.
22 */
23 BOTAN_ARG_CHECK(m_time_step > 0 && m_time_step <= 300, "Invalid TOTP time step");
24}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK.

Member Function Documentation

◆ generate_totp() [1/2]

uint32_t Botan::TOTP::generate_totp ( std::chrono::system_clock::time_point time_point)

Convert the provided time_point to a Unix timestamp and call generate_totp

Definition at line 26 of file totp.cpp.

26 {
27 const uint64_t unix_time = std::chrono::duration_cast<std::chrono::seconds>(current_time - m_unix_epoch).count();
28 return this->generate_totp(unix_time);
29}
uint32_t generate_totp(std::chrono::system_clock::time_point time_point)
Definition totp.cpp:26

References generate_totp().

Referenced by generate_totp().

◆ generate_totp() [2/2]

uint32_t Botan::TOTP::generate_totp ( uint64_t unix_time)

Generate the OTP corresponding the the provided "Unix timestamp" (ie number of seconds since midnight Jan 1, 1970)

Definition at line 31 of file totp.cpp.

31 {
32 return m_hotp.generate_hotp(unix_time / m_time_step);
33}

◆ verify_totp() [1/2]

bool Botan::TOTP::verify_totp ( uint32_t otp,
std::chrono::system_clock::time_point time,
size_t clock_drift_accepted = 0 )

Verify a TOTP against the provided time point

Parameters
otpthe presented OTP
timethe current local time
clock_drift_acceptedthe acceptable clock drift, in time steps
Returns
true if the OTP is valid

Definition at line 35 of file totp.cpp.

35 {
36 const uint64_t unix_time = std::chrono::duration_cast<std::chrono::seconds>(current_time - m_unix_epoch).count();
37 return verify_totp(otp, unix_time, clock_drift_accepted);
38}
bool verify_totp(uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted=0)
Definition totp.cpp:35

References verify_totp().

Referenced by verify_totp().

◆ verify_totp() [2/2]

bool Botan::TOTP::verify_totp ( uint32_t otp,
uint64_t unix_time,
size_t clock_drift_accepted = 0 )

Verify a TOTP against the provided Unix timestamp

Parameters
otpthe presented OTP
unix_timethe current local time as a Unix timestamp
clock_drift_acceptedthe acceptable clock drift, in time steps
Returns
true if the OTP is valid

Definition at line 40 of file totp.cpp.

40 {
41 /*
42 clock_drift_accepted is denominated in time steps. This bounds the loop to
43 far more iterations than any reasonable clock drift would require. For the
44 almost universally used 30 second time step, this limit is about 3.5 days
45 */
46 BOTAN_ARG_CHECK(clock_drift_accepted <= 10000, "TOTP clock_drift_accepted too large");
47
48 // This was on Sep 9, 2001
49 BOTAN_ARG_CHECK(unix_time >= 1000000000, "TOTP unix_time argument is implausibly small");
50
51 const uint64_t t = unix_time / m_time_step;
52
53 for(size_t i = 0; i <= clock_drift_accepted; ++i) {
54 BOTAN_ASSERT_NOMSG(t >= i);
55 if(m_hotp.generate_hotp(t - i) == otp) {
56 return true;
57 }
58 }
59
60 return false;
61}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75

References BOTAN_ARG_CHECK, and BOTAN_ASSERT_NOMSG.


The documentation for this class was generated from the following files: