• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/src/lib/misc/hotp/hotp.cpp
1
/*
2
* HOTP
3
* (C) 2017 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/otp.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/loadstor.h>
12

13
namespace Botan {
14

15
HOTP::HOTP(const uint8_t key[], size_t key_len, std::string_view hash_algo, size_t digits) {
40✔
16
   BOTAN_ARG_CHECK(digits == 6 || digits == 7 || digits == 8, "Invalid HOTP digits");
40✔
17

18
   if(digits == 6)
40✔
19
      m_digit_mod = 1000000;
12✔
20
   else if(digits == 7)
28✔
21
      m_digit_mod = 10000000;
2✔
22
   else if(digits == 8)
26✔
23
      m_digit_mod = 100000000;
26✔
24

25
   /*
26
   RFC 4228 only supports SHA-1 but TOTP allows SHA-256 and SHA-512
27
   and some HOTP libs support one or both as extensions
28
   */
29
   if(hash_algo == "SHA-1")
40✔
30
      m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-1)");
28✔
31
   else if(hash_algo == "SHA-256")
18✔
32
      m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
6✔
33
   else if(hash_algo == "SHA-512")
6✔
34
      m_mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
6✔
35
   else
36
      throw Invalid_Argument("Unsupported HOTP hash function");
×
37

38
   m_mac->set_key(key, key_len);
40✔
39
}
40✔
40

41
uint32_t HOTP::generate_hotp(uint64_t counter) {
6,321✔
42
   m_mac->update_be(counter);
6,321✔
43
   const secure_vector<uint8_t> mac = m_mac->final();
6,321✔
44

45
   const size_t offset = mac[mac.size() - 1] & 0x0F;
6,321✔
46
   const uint32_t code = load_be<uint32_t>(mac.data() + offset, 0) & 0x7FFFFFFF;
6,321✔
47
   return code % m_digit_mod;
6,321✔
48
}
6,321✔
49

50
std::pair<bool, uint64_t> HOTP::verify_hotp(uint32_t otp, uint64_t starting_counter, size_t resync_range) {
136✔
51
   for(size_t i = 0; i <= resync_range; ++i) {
6,296✔
52
      if(generate_hotp(starting_counter + i) == otp)
6,230✔
53
         return std::make_pair(true, starting_counter + i + 1);
70✔
54
   }
55
   return std::make_pair(false, starting_counter);
66✔
56
}
57

58
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc