-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhotp_test.go
452 lines (403 loc) · 10.9 KB
/
hotp_test.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package hotp
import "fmt"
import "testing"
import "bytes"
import "io"
var testKey = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
func newZeroHOTP() *HOTP {
return NewHOTP(testKey, 0, 6)
}
// This test verifies that the counter increment works as expected. As per the RFC,
// the counter should be treated as an 8-byte big-endian unsigned integer.
func TestIncrement(t *testing.T) {
otp := newZeroHOTP()
for i := 0; i < 32; i++ {
if otp.Counter() != uint64(i) {
fmt.Printf("hotp: counter should be %d, is %d\n",
i, otp.Counter())
fmt.Printf("\tcounter state: %x\n", *otp.counter)
t.FailNow()
}
otp.Increment()
}
}
var sha1Hmac = []byte{
0x1f, 0x86, 0x98, 0x69, 0x0e,
0x02, 0xca, 0x16, 0x61, 0x85,
0x50, 0xef, 0x7f, 0x19, 0xda,
0x8e, 0x94, 0x5b, 0x55, 0x5a,
}
var truncExpect int64 = 0x50ef7f19
// This test runs through the truncation example given in the RFC.
func TestTruncate(t *testing.T) {
if result := truncate(sha1Hmac); result != truncExpect {
fmt.Printf("hotp: expected truncate -> %d, saw %d\n",
truncExpect, result)
t.FailNow()
}
sha1Hmac[19]++
if result := truncate(sha1Hmac); result == truncExpect {
fmt.Println("hotp: expected truncation to fail")
t.FailNow()
}
}
var rfcKey = []byte("12345678901234567890")
var rfcExpected = []string{
"755224",
"287082",
"359152",
"969429",
"338314",
"254676",
"287922",
"162583",
"399871",
"520489",
}
// This test runs through the test cases presented in the RFC, and
// ensures that this implementation is in compliance.
func TestRFC(t *testing.T) {
otp := NewHOTP(rfcKey, 0, 6)
for i := 0; i < len(rfcExpected); i++ {
code := otp.OTP()
if code == "" {
fmt.Printf("hotp: failed to produce an OTP\n")
t.FailNow()
} else if code != rfcExpected[i] {
fmt.Printf("hotp: invalid OTP\n")
fmt.Printf("\tExpected: %s\n", rfcExpected[i])
fmt.Printf("\t Actual: %s\n", code)
t.FailNow()
}
}
}
// This test uses a different key than the test cases in the RFC,
// but runs through the same test cases to ensure that they fail as
// expected.
func TestBadRFC(t *testing.T) {
otp := NewHOTP(testKey, 0, 6)
for i := 0; i < len(rfcExpected); i++ {
code := otp.OTP()
if code == "" {
fmt.Printf("hotp: failed to produce an OTP\n")
t.FailNow()
} else if code == rfcExpected[i] {
fmt.Printf("hotp: should not have received a valid OTP\n")
t.FailNow()
}
}
}
// This test takes input from a test YubiKey and ensures that the
// YubiKey functionality works as expected.
func TestYubiKey(t *testing.T) {
ykKey := []byte{
0xd4, 0xbe, 0x97, 0xac, 0xe3,
0x31, 0x72, 0x95, 0xd8, 0x95,
0xeb, 0xd6, 0xb2, 0xec, 0xa6,
0x78, 0x49, 0x79, 0x4d, 0xb3,
}
otp := NewHOTP(ykKey, 0, 6)
out := []string{
"cccc52345777705179",
"cccc52345777404068",
"cccc52345777490143",
"cccc52345777739740",
"cccc52345777043269",
"cccc52345777035666",
"cccc52345777725326",
}
codes := []string{
"705179",
"404068",
"490143",
"739740",
"043269",
"035666",
"725326",
}
ykpub := "cccc52345777"
if _, _, ok := otp.YubiKey("abcd"); ok {
fmt.Println("hotp: accepted invalid YubiKey input")
t.FailNow()
}
for i := 0; i < len(out); i++ {
code := otp.OTP()
if ykCode, pubid, ok := otp.YubiKey(out[i]); !ok {
fmt.Printf("hotp: invalid YubiKey OTP\n")
t.FailNow()
} else if ykCode != code && code != codes[i] {
fmt.Printf("hotp: YubiKey did not produce valid OTP\n")
t.FailNow()
} else if ykpub != pubid {
fmt.Printf("hotp: invalid YubiKey public ID\n")
t.FailNow()
}
}
code, counter := otp.IntegrityCheck()
if code != codes[0] {
fmt.Println("hotp: YubiKey integrity check fails (bad code)")
t.FailNow()
} else if counter != uint64(len(out)) {
fmt.Println("hotp: YubiKey integrity check fails (bad counter)")
t.FailNow()
}
}
// This test generates a new HOTP, outputs the URL for that HOTP,
// and attempts to parse that URL. It verifies that the two HOTPs
// are the same, and that they produce the same output.
func TestURL(t *testing.T) {
var ident = "testuser@foo"
otp := NewHOTP(testKey, 0, 6)
url := otp.URL("testuser@foo")
otp2, id, err := FromURL(url)
if err != nil {
fmt.Printf("hotp: failed to parse HOTP URL\n")
t.FailNow()
} else if id != ident {
fmt.Printf("hotp: bad label\n")
fmt.Printf("\texpected: %s\n", ident)
fmt.Printf("\t actual: %s\n", id)
t.FailNow()
} else if otp2.Counter() != otp.Counter() {
fmt.Printf("hotp: OTP counters aren't synced\n")
fmt.Printf("\toriginal: %d\n", otp.Counter())
fmt.Printf("\t second: %d\n", otp2.Counter())
t.FailNow()
}
code1 := otp.OTP()
code2 := otp2.OTP()
if code1 != code2 {
fmt.Printf("hotp: mismatched OTPs\n")
fmt.Printf("\texpected: %s\n", code1)
fmt.Printf("\t actual: %s\n", code2)
}
// There's not much we can do test the QR code, except to
// ensure it doesn't fail.
_, err = otp.QR(ident)
if err != nil {
fmt.Printf("hotp: failed to generate QR code PNG (%v)\n", err)
t.FailNow()
}
// This should fail because the maximum size of an alphanumeric
// QR code with the lowest-level of error correction should
// max out at 4296 bytes. 8k may be a bit overkill... but it
// gets the job done. The value is read from the PRNG to
// increase the likelihood that the returned data is
// uncompressible.
var tooBigIdent = make([]byte, 8192)
_, err = io.ReadFull(PRNG, tooBigIdent)
if err != nil {
fmt.Printf("hotp: failed to read identity (%v)\n", err)
t.FailNow()
} else if _, err = otp.QR(string(tooBigIdent)); err == nil {
fmt.Println("hotp: QR code should fail to encode oversized URL")
t.FailNow()
}
}
// This test attempts a variety of invalid urls against the parser
// to ensure they fail.
func TestBadURL(t *testing.T) {
var urlList = []string{
"http://google.com",
"",
"-",
"foo",
"otpauth:/foo/bar/baz",
"://",
"otpauth://totp/foo@bar?secret=ABCD",
"otpauth://hotp/secret=bar",
"otpauth://hotp/?secret=QUJDRA&algorithm=SHA256",
"otpauth://hotp/?digits=",
"otpauth://hotp/?secret=123",
"otpauth://hotp/?secret=MFRGGZDF&digits=ABCD",
"otpauth://hotp/?secret=MFRGGZDF&counter=ABCD",
}
for i := range urlList {
if _, _, err := FromURL(urlList[i]); err == nil {
fmt.Println("hotp: URL should not have parsed successfully")
fmt.Printf("\turl was: %s\n", urlList[i])
t.FailNow()
}
}
}
// This test uses a url generated with the `hotpgen` tool; this url
// was imported into the Google Authenticator app and the resulting
// codes generated by the app are checked here to verify interoperability.
func TestGAuth(t *testing.T) {
url := "otpauth://hotp/kyle?counter=0&secret=EXZLUP7IGHQ673ZCP32RTLRU2N427Z6L"
expected := []string{
"023667",
"641344",
"419615",
"692589",
"237233",
"711695",
"620195",
}
otp, label, err := FromURL(url)
if err != nil {
fmt.Printf("hotp: failed to parse HOTP URL\n")
t.FailNow()
} else if label != "kyle" {
fmt.Printf("hotp: invalid label")
t.FailNow()
}
otp.Increment()
// Validate codes
for i := 1; i < len(expected); i++ {
code := otp.OTP()
if otp.Counter() != uint64(i+1) {
fmt.Printf("hotp: invalid OTP counter (should be %d but is %d)",
i, otp.Counter())
t.FailNow()
} else if code != expected[i] {
fmt.Println("hotp: invalid OTP")
t.FailNow()
}
}
// Validate integrity check
code, counter := otp.IntegrityCheck()
if code != expected[0] {
fmt.Println("hotp: invalid integrity code")
t.FailNow()
} else if counter != uint64(len(expected)) {
fmt.Println("hotp: invalid integrity counter")
t.FailNow()
}
}
// This test verifies that a scan will successfully sync and update
// the OTP counter.
func TestScan(t *testing.T) {
url := "otpauth://hotp/kyle?counter=0&secret=EXZLUP7IGHQ673ZCP32RTLRU2N427Z6L"
expected := []string{
"023667",
"641344",
"419615",
"692589",
"237233",
"711695",
"620195",
}
otp, _, err := FromURL(url)
if err != nil {
fmt.Printf("hotp: failed to parse HOTP URL\n")
t.FailNow()
}
if !otp.Scan(expected[4], 10) {
fmt.Println("hotp: scan should have found code")
t.FailNow()
}
if otp.Counter() != 5 {
fmt.Println("hotp: counter was not properly synced")
t.FailNow()
}
}
// This test verifies that a scan that does not successfully sync
// will not update the OTP counter.
func TestBadScan(t *testing.T) {
url := "otpauth://hotp/kyle?counter=0&secret=EXZLUP7IGHQ673ZCP32RTLRU2N427Z6L"
expected := []string{
"023667",
"641344",
"419615",
"692589",
"237233",
"711695",
"620195",
}
otp, _, err := FromURL(url)
if err != nil {
fmt.Printf("hotp: failed to parse HOTP URL\n")
t.FailNow()
}
if otp.Scan(expected[6], 3) {
fmt.Println("hotp: scan should not have found code")
t.FailNow()
}
if otp.Counter() != 0 {
fmt.Println("hotp: counter was not properly synced")
t.FailNow()
}
}
// This test ensures that a valid code is recognised and increments
// the counter.
func TestCheck(t *testing.T) {
otp := NewHOTP(rfcKey, 0, 6)
if !otp.Check(rfcExpected[0]) {
fmt.Println("hotp: Check failed when it should have succeeded")
t.FailNow()
}
if otp.Counter() != 1 {
fmt.Println("hotp: counter should have been incremented")
t.FailNow()
}
}
// This test ensures that an invalid code is recognised as such and
// does not increment the counter.
func TestBadCheck(t *testing.T) {
otp := NewHOTP(rfcKey, 0, 6)
if otp.Check(rfcExpected[1]) {
fmt.Println("hotp: Check succeeded when it should have failed")
t.FailNow()
}
if otp.Counter() != 0 {
fmt.Println("hotp: counter should not have been incremented")
t.FailNow()
}
}
func TestSerialisation(t *testing.T) {
otp := NewHOTP(rfcKey, 123456, 8)
out, err := Marshal(otp)
if err != nil {
fmt.Printf("hotp: failed to marshal HOTP (%v)\n", err)
t.FailNow()
}
otp2, err := Unmarshal(out)
if err != nil {
fmt.Printf("hotp: failed to unmarshal HOTP (%v)\n", err)
t.FailNow()
}
if otp.Counter() != otp2.Counter() {
fmt.Println("hotp: serialisation failed to preserve counter")
t.FailNow()
}
if _, err = Unmarshal([]byte{0x0}); err == nil {
fmt.Println("hotp: Unmarshal should have failed")
t.FailNow()
}
}
func TestGenerateHOTP(t *testing.T) {
_, err := GenerateHOTP(6, false)
if err != nil {
fmt.Printf("hotp: failed to generate random key value (%v)\n", err)
t.FailNow()
}
_, err = GenerateHOTP(8, true)
if err != nil {
fmt.Printf("hotp: failed to generate random key value (%v)\n", err)
t.FailNow()
}
PRNG = new(bytes.Buffer)
_, err = GenerateHOTP(8, true)
if err == nil {
fmt.Println("hotp: should have failed to generate random key value")
t.FailNow()
}
// should read just enough for the key
PRNG = bytes.NewBufferString("abcdeabcdeabcdeabcde")
_, err = GenerateHOTP(8, true)
if err == nil {
fmt.Println("hotp: should have failed to generate random key value")
t.FailNow()
}
}
func BenchmarkFromURL(b *testing.B) {
url := "otpauth://hotp/kyle?counter=0&secret=EXZLUP7IGHQ673ZCP32RTLRU2N427Z6L"
for i := 0; i < b.N; i++ {
_, _, err := FromURL(url)
if err != nil {
fmt.Printf("hotp: failed to parse url (%v)\n", err)
b.FailNow()
}
}
}