-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHOTP.php
269 lines (228 loc) · 6.69 KB
/
HOTP.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
require_once "HOTPError.php";
require_once "bcutil.php";
require_once "base32.php";
/**
* This class implements the algorithm outlined in RFC 4226:
* HOTP: An HMAC-Based One-Time Password Algorithm.
*/
class HOTP
{
const SHA1 = "sha1";
const SHA256 = "sha256";
const SHA512 = "sha512";
/** @var string the shared key */
protected $key;
/** @var string the current counter (as a decimal string) */
protected $counter;
/** @var int number of digits in OTP */
protected $digits;
/** @var int window size for resynchronization protocol */
protected $windowSize = 5;
/**
* The hash function to use for the HMAC. HOTP implementations should use
* SHA-1. SHA-256 and SHA-512 may be used for TOTP.
* @var string
*/
protected $hash = self::SHA1;
public function __construct($key, $counter = "0", $digits = 6)
{
$this->key = $key;
$this->counter = $counter;
$this->digits = $digits;
}
/**
* @param string $counter
*/
public function setCounter($counter)
{
$this->counter = $counter;
}
/**
* @return string
*/
public function getCounter()
{
return $this->counter;
}
/**
* @param int $digits
*/
public function setDigits($digits)
{
$this->digits = $digits;
}
/**
* @return int
*/
public function getDigits()
{
return $this->digits;
}
/**
* @param string $key
*/
public function setKey($key)
{
$this->key = $key;
}
/**
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* @param int $windowSize
*/
public function setWindowSize($windowSize)
{
$this->windowSize = $windowSize;
}
/**
* @return int
*/
public function getWindowSize()
{
return $this->windowSize;
}
/**
* @param string $hash
*/
public function setHash($hash)
{
$this->hash = $hash;
}
/**
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* Get the counter as a 64-bit binary string.
* @return string
*/
public function getBinaryCounter()
{
return bc_to_binary($this->getCounter(), 64);
}
/**
* Increment the stored counter by one.
*/
public function increment()
{
$this->counter = bcadd($this->counter, "1");
}
/**
* Generate a OTP for the given counter. If the counter value is not
* given, the internal counter is used and incremented.
* @param mixed $counter
* @return string
*/
public function generate($counter = null)
{
if ($counter === null)
{
$counter = $this->getBinaryCounter();
$this->increment();
}
return self::generateOTP($this->key, $counter, $this->digits, $this->hash);
}
/**
* Try to validate the user input against the current OTP.
* If validation fails, the counter is not incremented.
* @param string $input
* @return boolean
*/
public function validate($input)
{
$originalCounter = $this->counter;
for ($i = 0; $i < $this->windowSize; $i += 1)
{
$otp = $this->generate();
if ($otp === $input)
return true;
}
/* Validation of the OTP failed even after resynchronization, reset
* counter to previous value. */
$this->counter = $originalCounter;
return false;
}
public function getUri($label, $issuer = null)
{
$type = strtolower(get_class($this));
$uri = sprintf("otpauth://%s/%s?", $type, str_replace("+", "%20", urlencode($label)));
$uri .= "secret=" . base32_encode($this->key);
if ($type == "hotp")
$uri .= "&counter=" . $this->getCounter();
if ($issuer)
$uri .= "&issuer=" . urlencode($issuer);
return $uri;
}
public function getQRUrl($label, $issuer = null)
{
$url = $this->getUri($label, $issuer);
return "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=" . urlencode($url);
}
/**
* Try to convert a counter value to a 64-bit binary string.
* The counter value can be an integer, binary string, hexadecimal string
* or a GMP number resource.
* @param mixed $counter
* @return string
*/
public static function counterToBinary($counter)
{
if (is_integer($counter))
{
if (PHP_INT_SIZE < 8)
throw new HTOPError("Counter specified as integer but PHP_INT_SIZE < 8: counter might be truncated.");
$high = ($counter & 0xFFFFFFFF00000000) >> 32;
$low = ($counter & 0x00000000FFFFFFFF);
return pack("NN", $high, $low);
}
if (is_string($counter))
{
/* If the length is 8 bytes, assume 64-bit binary string. */
if (strlen($counter) == 8)
return $counter;
/* If the length is 16 bytes, assume 64-bit hex string. */
if (strlen($counter) == 16)
return pack("H*", $counter);
throw new HTOPError("Counter specified as string, but length not valid for 64-bit string.");
}
if (is_resource($counter))
return pack("H*", gmp_strval($counter, 16));
throw new HTOPError("Invalid type " . gettype($counter) . " for counter.");
}
public static function dynamicTruncate($string)
{
/*
* DT(String) // String = String[0]...String[n-1]
* Let OffsetBits be the low-order 4 bits of String[n-1]
* Offset = StToNum(OffsetBits) // 0 <= OffSet <= 15
* Let P = String[OffSet]...String[OffSet+3]
* Return the Last 31 bits of P
*/
$offset = ord(substr($string, -1)) & 0x0F;
$p = substr($string, $offset, 4);
$v = unpack("N", $p);
return $v[1] & 0x7FFFFFFF;
}
public static function generateOTP($key, $counter, $digits = 6, $hashAlgo = "sha1")
{
$hs = hash_hmac($hashAlgo, self::counterToBinary($counter), $key, true);
$sbits = self::dynamicTruncate($hs);
$modulo = pow(10, $digits);
$value = $sbits % $modulo;
// printf("\nKey: %d %s\n", strlen($key), bin2hex($key));
// printf("Counter: %s\n", bin2hex(self::counterToBinary($counter)));
// printf("HMAC: %s\n", bin2hex($hs));
// printf("SBits: %08x\n", $sbits);
// printf("%08x %% %d => %d\n", $sbits, $modulo, $sbits % $modulo);
return str_pad($value, $digits, "0", STR_PAD_LEFT);
}
}