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

#include <hotp.h>

Public Member Functions

uint32_t generate_hotp (uint64_t counter)
 
 HOTP (const SymmetricKey &key, const std::string &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 18 of file hotp.h.

Constructor & Destructor Documentation

◆ HOTP()

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

Definition at line 12 of file hotp.cpp.

References Botan::MessageAuthenticationCode::create_or_throw().

13  {
14  if(digits == 6)
15  m_digit_mod = 1000000;
16  else if(digits == 7)
17  m_digit_mod = 10000000;
18  else if(digits == 8)
19  m_digit_mod = 100000000;
20  else
21  throw Invalid_Argument("Invalid HOTP digits");
22 
23  /*
24  RFC 4228 only supports SHA-1 but TOTP allows SHA-256 and SHA-512
25  and some HOTP libs support one or both as extensions
26  */
27  if(hash_algo == "SHA-1")
28  m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-1)");
29  else if(hash_algo == "SHA-256")
30  m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
31  else if(hash_algo == "SHA-512")
32  m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
33  else
34  throw Invalid_Argument("Unsupported HOTP hash function");
35 
36  m_mac->set_key(key);
37  }
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:140

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 39 of file hotp.cpp.

References Botan::load_be< uint32_t >(), and Botan::store_be().

Referenced by Botan::TOTP::generate_totp(), verify_hotp(), and Botan::TOTP::verify_totp().

40  {
41  uint8_t counter8[8] = { 0 };
42  store_be(counter, counter8);
43  m_mac->update(counter8, sizeof(counter8));
44  const secure_vector<uint8_t> mac = m_mac->final();
45 
46  const size_t offset = mac[mac.size()-1] & 0x0F;
47  const uint32_t code = load_be<uint32_t>(mac.data() + offset, 0) & 0x7FFFFFFF;
48  return code % m_digit_mod;
49  }
void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:434
uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:177

◆ 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 51 of file hotp.cpp.

References generate_hotp().

52  {
53  for(size_t i = 0; i <= resync_range; ++i)
54  {
55  if(generate_hotp(starting_counter + i) == otp)
56  return std::make_pair(true, starting_counter + i + 1);
57  }
58  return std::make_pair(false, starting_counter);
59  }
uint32_t generate_hotp(uint64_t counter)
Definition: hotp.cpp:39

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