Botan 2.19.5 Crypto and TLS for C&

Public Member Functions | List of all members
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)
| TOTP (const SymmetricKey &key, const std::string &hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
| TOTP (const uint8_t key[], size_t key_len, 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 65 of file otp.h.

## Constructor & Destructor Documentation

## ◆ TOTP() [1/2]

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

Parameters

  key | the secret key shared between client and server
  hash_algo | the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
  digits | the number of digits in the OTP (must be 6, 7, or 8)
  time_step | granularity of OTP in seconds

Definition at line 74 of file otp.h.

76 :
77 TOTP(key.begin(), key.size(), hash_algo, digits, time_step) {}
Botan::TOTP::TOTP
TOTP(const SymmetricKey &key, const std::string &hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
Definition: otp.h:74
hash_algo
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22

## ◆ TOTP() [2/2]

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

Parameters

  key | the secret key shared between client and server
  key_len | length of key
  hash_algo | the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
  digits | the number of digits in the OTP (must be 6, 7, or 8)
  time_step | granularity of OTP in seconds

Definition at line 13 of file totp.cpp.

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 /*
21 * Technically any time step except 0 is valid, but 30 is typical
22 * and over 5 minutes seems unlikely.
23 */
24 BOTAN_ARG_CHECK(m_time_step > 0 && m_time_step < 300, "Invalid TOTP time step");
25 }
BOTAN_ARG_CHECK
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37

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 27 of file totp.cpp.

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

References generate_totp().

Referenced by botan_totp_generate(), and 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 34 of file totp.cpp.

35 {
36 return m_hotp.generate_hotp(unix_time / m_time_step);
37 }
Botan::HOTP::generate_hotp
uint32_t generate_hotp(uint64_t counter)
Definition: hotp.cpp:42

References Botan::HOTP::generate_hotp().

## ◆ 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 39 of file totp.cpp.

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

References verify_totp().

Referenced by botan_totp_check(), and verify_totp().

## ◆ 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 47 of file totp.cpp.

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

References Botan::HOTP::generate_hotp().

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

- src/lib/misc/hotp/otp.h
- src/lib/misc/hotp/totp.cpp