-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtotp.cpp
175 lines (147 loc) · 5.16 KB
/
totp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
namespace {
std::vector<uint8_t> base32Decode(const std::string& encoded) {
std::vector<uint8_t> ret;
unsigned int curByte = 0;
int bits = 0;
for (auto ch : encoded) {
if (ch >= 'A' && ch <= 'Z') {
ch -= 'A';
} else if (ch >= '2' && ch <= '7') {
ch -= '2';
ch += 26;
}
curByte = (curByte << 5) | ch;
bits += 5;
if (bits >= 8) {
ret.push_back((curByte >> (bits - 8)) & 255);
bits -= 8;
}
}
return std::move(ret);
}
class HMACWrapper {
private:
std::function<void(HMAC_CTX*)> hmacFreeFn = [](HMAC_CTX* ptr) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
abort();
#else
HMAC_CTX_free(ptr);
#endif
};
public:
HMACWrapper(const EVP_MD* digest, const std::vector<uint8_t>& key) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
ctx = std::unique_ptr<HMAC_CTX>(new HMAC_CTX);
HMAC_CTX_init(ctx.get());
#else
ctx = std::unique_ptr<HMAC_CTX, decltype(hmacFreeFn)>(HMAC_CTX_new(), hmacFreeFn);
#endif
HMAC_Init_ex(ctx.get(), key.data(), key.size(), digest, nullptr);
}
void update(const std::vector<uint8_t>& s) {
HMAC_Update(ctx.get(),
reinterpret_cast<const unsigned char*>(s.data()),
static_cast<unsigned int>(s.size()));
}
std::vector<uint8_t> finalize() {
std::vector<uint8_t> finalHash(HMAC_size(ctx.get()));
unsigned int size;
HMAC_Final(ctx.get(), finalHash.data(), &size);
if (size != finalHash.size()) {
throw std::runtime_error("Overflow while finalizing HMAC");
}
return std::move(finalHash);
}
private:
std::unique_ptr<HMAC_CTX, decltype(hmacFreeFn)> ctx;
};
std::string calculateTOTPInternal(const EVP_MD* algo,
uint64_t counter,
int digits,
const std::vector<uint8_t>& key) {
std::vector<uint8_t> counterArr(8);
for (int i = 7; i >= 0; i--) {
counterArr[i] = counter & 0xff;
counter >>= 8;
}
HMACWrapper hmac(algo, key);
hmac.update(counterArr);
auto finalHmac = hmac.finalize();
const auto offset = finalHmac[19] & 0xf;
uint32_t truncated = (finalHmac[offset] & 0x7f) << 24 | (finalHmac[offset + 1] & 0xff) << 16 |
(finalHmac[offset + 2] & 0xff) << 8 | (finalHmac[offset + 3] & 0xff);
std::stringstream ss;
const int digitsPow = pow(10, digits);
ss << std::setw(digits) << std::setfill('0') << truncated % digitsPow;
return ss.str();
}
const auto kURIPattern = "otpauth://(totp|hotp)/(?:[^\\?]+)\\?((?:(?:[^=]+)=(?:[^\\&]+)\\&?)+)";
const auto kURIRegex = std::regex(kURIPattern, std::regex::ECMAScript | std::regex::optimize);
const auto kQueryComponentPattern = "([^=]+)=([^\\&]+)&?";
const auto kQueryComponentRegex =
std::regex(kQueryComponentPattern, std::regex::ECMAScript | std::regex::optimize);
} // namespace
bool isTOTPURI(const std::string& uri) {
std::smatch match;
return std::regex_match(uri, match, kURIRegex);
}
std::string calculateTOTP(const std::string& uri) {
std::smatch match;
if (!std::regex_match(uri, match, kURIRegex)) {
throw std::runtime_error("Error parsing OTP URI");
}
const auto type = match[1].str();
if (type != "totp") {
std::stringstream ss;
ss << "Unsupported OTP format: " << type;
throw std::runtime_error(ss.str());
}
const auto query = match[2].str();
auto begin = std::sregex_iterator(query.begin(), query.end(), kQueryComponentRegex);
const auto end = std::sregex_iterator();
std::unordered_map<std::string, std::string> params;
for (auto it = begin; it != end; ++it) {
auto curParam = *it;
params.emplace(curParam[1], curParam[2]);
}
if (params.find("secret") == params.end()) {
throw std::runtime_error("OTP URI is missing a key");
}
const EVP_MD* algo = EVP_sha1();
if (params.find("algorithm") != params.end()) {
const auto& algoStr = params["algorithm"];
if (algoStr == "SHA256") {
algo = EVP_sha256();
} else if (algoStr == "SHA512") {
algo = EVP_sha512();
} else if (algoStr != "SHA1") {
std::stringstream ss;
ss << "Unsurpported algorithm in OTP URI: " << algoStr;
throw std::runtime_error(ss.str());
}
}
auto now = std::time(nullptr);
int period = 30;
if (params.find("period") != params.end()) {
period = std::stoi(params["period"]);
}
const auto key = base32Decode(params["secret"]);
uint64_t counter = now / period;
int digits = 6;
if (params.find("digits") != params.end()) {
digits = std::stoi(params["digits"]);
}
return calculateTOTPInternal(algo, counter, digits, key);
}