Botan 3.12.0
Crypto and TLS for C&
Botan::HOTP Class Referencefinal

#include <otp.h>

Public Member Functions

uint32_t generate_hotp (uint64_t counter)
BOTAN_FUTURE_EXPLICIT HOTP (const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6)
 HOTP (const uint8_t key[], size_t key_len, std::string_view hash_algo="SHA-1", size_t digits=6)
std::pair< bool, uint64_t > verify_hotp (uint32_t otp, uint64_t starting_counter, size_t resync_range=0)

Detailed Description

HOTP one time passwords (RFC 4226)

Definition at line 20 of file otp.h.

Constructor & Destructor Documentation

◆ HOTP() [1/2]

BOTAN_FUTURE_EXPLICIT Botan::HOTP::HOTP ( const SymmetricKey & key,
std::string_view hash_algo = "SHA-1",
size_t digits = 6 )
inline
Parameters
keythe secret key shared between client and server
hash_algothe hash algorithm to use, should be SHA-1 or SHA-256
digitsthe number of digits in the OTP (must be 6, 7, or 8) TODO(Botan4) remove the default hash param here

Definition at line 28 of file otp.h.

28 :
29 HOTP(key.begin(), key.size(), hash_algo, digits) {}
BOTAN_FUTURE_EXPLICIT HOTP(const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6)
Definition otp.h:28

References BOTAN_FUTURE_EXPLICIT, and HOTP().

Referenced by HOTP().

◆ HOTP() [2/2]

Botan::HOTP::HOTP ( const uint8_t key[],
size_t key_len,
std::string_view hash_algo = "SHA-1",
size_t digits = 6 )
Parameters
keythe secret key shared between client and server
key_lenlength of key param
hash_algothe hash algorithm to use, should be SHA-1 or SHA-256
digitsthe number of digits in the OTP (must be 6, 7, or 8) TODO(Botan4) remove the default hash param here

Definition at line 34 of file hotp.cpp.

34 : m_digits(digits) {
35 BOTAN_ARG_CHECK(m_digits == 6 || m_digits == 7 || m_digits == 8, "Invalid HOTP digits");
36
37 /*
38 RFC 4228 only supports SHA-1 but TOTP allows SHA-256 and SHA-512
39 and some HOTP libs support one or both as extensions
40 */
41 if(hash_algo == "SHA-1") {
42 m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-1)");
43 } else if(hash_algo == "SHA-256") {
44 m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
45 } else if(hash_algo == "SHA-512") {
46 m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
47 } else {
48 throw Invalid_Argument("Unsupported HOTP hash function");
49 }
50
51 m_mac->set_key(key, key_len);
52}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:147

References BOTAN_ARG_CHECK, and Botan::MessageAuthenticationCode::create_or_throw().

Member Function Documentation

◆ generate_hotp()

uint32_t Botan::HOTP::generate_hotp ( uint64_t counter)

Generate the HOTP for a particular counter value

Warning
if the counter value is repeated the OTP ceases to be one-time

Definition at line 54 of file hotp.cpp.

54 {
55 m_mac->update_be(counter);
56 const secure_vector<uint8_t> mac = m_mac->final();
57
58 const size_t offset = mac[mac.size() - 1] & 0x0F;
59 const uint32_t code = load_be<uint32_t>(mac.data() + offset, 0) & 0x7FFFFFFF;
60 return hotp_truncate(code, m_digits);
61}
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::load_be().

Referenced by verify_hotp().

◆ verify_hotp()

std::pair< bool, uint64_t > Botan::HOTP::verify_hotp ( uint32_t otp,
uint64_t starting_counter,
size_t resync_range = 0 )

Check an OTP value using a starting counter and a resync range

Parameters
otpthe client provided OTP
starting_counterthe server's guess as to the current counter state
resync_rangeif 0 then only HOTP(starting_counter) is accepted If larger than 0, up to resync_range values after HOTP are also checked.
Returns
(valid,next_counter). If the OTP does not validate, always returns (false,starting_counter). Otherwise returns (true,next_counter) where next_counter is at most starting_counter + resync_range + 1

Definition at line 63 of file hotp.cpp.

63 {
64 for(size_t i = 0; i <= resync_range; ++i) {
65 if(generate_hotp(starting_counter + i) == otp) {
66 return std::make_pair(true, starting_counter + i + 1);
67 }
68 }
69 return std::make_pair(false, starting_counter);
70}
uint32_t generate_hotp(uint64_t counter)
Definition hotp.cpp:54

References generate_hotp().


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