Botan 3.12.0
Crypto and TLS for C&
otp.h
Go to the documentation of this file.
1/*
2* HOTP/TOTP
3* (C) 2017 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_ONE_TIME_PASSWORDS_H_
9#define BOTAN_ONE_TIME_PASSWORDS_H_
10
11#include <botan/mac.h>
12#include <botan/symkey.h>
13#include <chrono>
14
15namespace Botan {
16
17/**
18* HOTP one time passwords (RFC 4226)
19*/
20class BOTAN_PUBLIC_API(2, 2) HOTP final {
21 public:
22 /**
23 * @param key the secret key shared between client and server
24 * @param hash_algo the hash algorithm to use, should be SHA-1 or SHA-256
25 * @param digits the number of digits in the OTP (must be 6, 7, or 8)
26 * TODO(Botan4) remove the default hash param here
27 */
28 BOTAN_FUTURE_EXPLICIT HOTP(const SymmetricKey& key, std::string_view hash_algo = "SHA-1", size_t digits = 6) :
29 HOTP(key.begin(), key.size(), hash_algo, digits) {}
30
31 /**
32 * @param key the secret key shared between client and server
33 * @param key_len length of key param
34 * @param hash_algo the hash algorithm to use, should be SHA-1 or SHA-256
35 * @param digits the number of digits in the OTP (must be 6, 7, or 8)
36 * TODO(Botan4) remove the default hash param here
37 */
38 HOTP(const uint8_t key[], size_t key_len, std::string_view hash_algo = "SHA-1", size_t digits = 6);
39
40 /**
41 * Generate the HOTP for a particular counter value
42 * @warning if the counter value is repeated the OTP ceases to be one-time
43 */
44 uint32_t generate_hotp(uint64_t counter);
45
46 /**
47 * Check an OTP value using a starting counter and a resync range
48 * @param otp the client provided OTP
49 * @param starting_counter the server's guess as to the current counter state
50 * @param resync_range if 0 then only HOTP(starting_counter) is accepted
51 * If larger than 0, up to resync_range values after HOTP are also checked.
52 * @return (valid,next_counter). If the OTP does not validate, always
53 * returns (false,starting_counter). Otherwise returns (true,next_counter)
54 * where next_counter is at most starting_counter + resync_range + 1
55 */
56 std::pair<bool, uint64_t> verify_hotp(uint32_t otp, uint64_t starting_counter, size_t resync_range = 0);
57
58 private:
59 std::unique_ptr<MessageAuthenticationCode> m_mac;
60 size_t m_digits;
61};
62
63/**
64* TOTP (time based) one time passwords (RFC 6238)
65*/
66class BOTAN_PUBLIC_API(2, 2) TOTP final {
67 public:
68 /**
69 * @param key the secret key shared between client and server
70 * @param hash_algo the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
71 * @param digits the number of digits in the OTP (must be 6, 7, or 8)
72 * @param time_step granularity of OTP in seconds
73 * TODO(Botan4) remove the default hash param here
74 */
76 std::string_view hash_algo = "SHA-1",
77 size_t digits = 6,
78 size_t time_step = 30) :
79 TOTP(key.begin(), key.size(), hash_algo, digits, time_step) {}
80
81 /**
82 * @param key the secret key shared between client and server
83 * @param key_len length of key
84 * @param hash_algo the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
85 * @param digits the number of digits in the OTP (must be 6, 7, or 8)
86 * @param time_step granularity of OTP in seconds
87 * TODO(Botan4) remove the default hash param here
88 */
89 TOTP(const uint8_t key[],
90 size_t key_len,
91 std::string_view hash_algo = "SHA-1",
92 size_t digits = 6,
93 size_t time_step = 30);
94
95 /**
96 * Convert the provided time_point to a Unix timestamp and call generate_totp
97 */
98 uint32_t generate_totp(std::chrono::system_clock::time_point time_point);
99
100 /**
101 * Generate the OTP corresponding the the provided "Unix timestamp" (ie
102 * number of seconds since midnight Jan 1, 1970)
103 */
104 uint32_t generate_totp(uint64_t unix_time);
105
106 bool verify_totp(uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted = 0);
107
108 bool verify_totp(uint32_t otp, uint64_t unix_time, size_t clock_drift_accepted = 0);
109
110 private:
111 HOTP m_hotp;
112 size_t m_time_step;
113 std::chrono::system_clock::time_point m_unix_epoch;
114};
115
116} // namespace Botan
117
118#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
BOTAN_FUTURE_EXPLICIT HOTP(const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6)
Definition otp.h:28
BOTAN_FUTURE_EXPLICIT TOTP(const SymmetricKey &key, std::string_view hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
Definition otp.h:75
OctetString SymmetricKey
Definition symkey.h:140