Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::TOTP Class Referencefinal

#include <totp.h>

Public Member Functions

uint32_t generate_totp (std::chrono::system_clock::time_point time_point)
 
uint32_t generate_totp (uint64_t unix_time)
 
 TOTP (const SymmetricKey &key, const std::string &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 18 of file totp.h.

Constructor & Destructor Documentation

◆ TOTP()

Botan::TOTP::TOTP ( const SymmetricKey key,
const std::string &  hash_algo = "SHA-1",
size_t  digits = 6,
size_t  time_step = 30 
)
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

Definition at line 13 of file totp.cpp.

References BOTAN_ARG_CHECK.

15  : m_hotp(key, hash_algo, digits)
16  , m_time_step(time_step)
17  , m_unix_epoch(calendar_point(1970, 1, 1, 0, 0, 0).to_std_timepoint())
18  {
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:37
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:23

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.

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

◆ generate_totp() [2/2]

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

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

Definition at line 33 of file totp.cpp.

References Botan::HOTP::generate_hotp().

34  {
35  return m_hotp.generate_hotp(unix_time / m_time_step);
36  }
uint32_t generate_hotp(uint64_t counter)
Definition: hotp.cpp:40

◆ 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 
)

Definition at line 38 of file totp.cpp.

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

◆ verify_totp() [2/2]

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

Definition at line 46 of file totp.cpp.

References Botan::HOTP::generate_hotp().

48  {
49  uint64_t t = unix_time / m_time_step;
50 
51  for(size_t i = 0; i <= clock_drift_accepted; ++i)
52  {
53  if(m_hotp.generate_hotp(t-i) == otp)
54  {
55  return true;
56  }
57  }
58 
59  return false;
60  }
uint32_t generate_hotp(uint64_t counter)
Definition: hotp.cpp:40

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