-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp_test.go
346 lines (328 loc) · 7.38 KB
/
otp_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
package otp_test
import (
"crypto"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"strconv"
"testing"
"time"
"code.soquee.net/otp"
)
const (
secret = "12345678901234567890"
secret256 = "12345678901234567890123456789012"
secret512 = "1234567890123456789012345678901234567890123456789012345678901234"
)
var urlTestCases = [...]struct {
key []byte
step time.Duration
l int
hash crypto.Hash
domain string
email string
out string
}{
0: {
key: []byte(secret),
step: 30 * time.Second,
l: 8,
hash: crypto.SHA1,
domain: "example.net",
email: "me@example.net",
out: "otpauth://totp/example.net:me@example.net?algorithm=SHA1&digits=8&issuer=example.net&period=30&secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
},
1: {
key: []byte(secret256),
step: time.Second,
l: 6,
hash: crypto.SHA256,
domain: "example.com",
email: "me@example.com",
out: "otpauth://totp/example.com:me@example.com?algorithm=SHA256&digits=6&issuer=example.com&period=1&secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA",
},
2: {
key: []byte(secret512),
step: 0,
l: 6,
hash: crypto.SHA512,
domain: "example.com",
email: "me@example.com",
out: "otpauth://totp/example.com:me@example.com?algorithm=SHA512&digits=6&issuer=example.com&period=0&secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA",
},
3: {
key: []byte(secret),
step: 30 * time.Second,
l: 8,
hash: crypto.RIPEMD160,
domain: "example.net",
email: "me@example.net",
out: "otpauth://totp/example.net:me@example.net?algorithm=SHA1&digits=8&issuer=example.net&period=30&secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
},
}
func TestURL(t *testing.T) {
for i, tc := range urlTestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
u := otp.URL(tc.key, tc.step, tc.l, tc.hash, tc.domain, tc.email).String()
if u != tc.out {
t.Errorf("Got invalid URL: want=%q, got=%q", tc.out, u)
}
})
}
}
// See RFC 6238 Appendix B
var rfc6238TestCases = [...]struct {
t string
h func() hash.Hash
totp int32
secret []byte
offset int
}{
0: {
t: "1970-01-01 00:00:59",
h: sha1.New,
totp: 94287082,
secret: []byte(secret),
},
1: {
t: "1970-01-01 00:00:59",
h: sha256.New,
totp: 46119246,
secret: []byte(secret256),
},
2: {
t: "1970-01-01 00:00:59",
h: sha512.New,
totp: 90693936,
secret: []byte(secret512),
},
3: {
t: "2005-03-18 01:58:29",
h: sha1.New,
totp: 7081804,
secret: []byte(secret),
},
4: {
t: "2005-03-18 01:58:29",
h: sha256.New,
totp: 68084774,
secret: []byte(secret256),
},
5: {
t: "2005-03-18 01:58:29",
h: sha512.New,
totp: 25091201,
secret: []byte(secret512),
},
6: {
t: "2005-03-18 01:58:31",
h: sha1.New,
totp: 14050471,
secret: []byte(secret),
},
7: {
t: "2005-03-18 01:58:31",
h: sha256.New,
totp: 67062674,
secret: []byte(secret256),
},
8: {
t: "2005-03-18 01:58:31",
h: sha512.New,
totp: 99943326,
secret: []byte(secret512),
},
9: {
t: "2009-02-13 23:31:30",
h: sha1.New,
totp: 89005924,
secret: []byte(secret),
},
10: {
t: "2009-02-13 23:31:30",
h: sha256.New,
totp: 91819424,
secret: []byte(secret256),
},
11: {
t: "2009-02-13 23:31:30",
h: sha512.New,
totp: 93441116,
secret: []byte(secret512),
},
12: {
t: "2033-05-18 03:33:20",
h: sha1.New,
totp: 69279037,
secret: []byte(secret),
},
13: {
t: "2033-05-18 03:33:20",
h: sha256.New,
totp: 90698825,
secret: []byte(secret256),
},
14: {
t: "2033-05-18 03:33:20",
h: sha512.New,
totp: 38618901,
secret: []byte(secret512),
},
15: {
t: "2603-10-11 11:33:20",
h: sha1.New,
totp: 65353130,
secret: []byte(secret),
},
16: {
t: "2603-10-11 11:33:20",
h: sha256.New,
totp: 77737706,
secret: []byte(secret256),
},
17: {
t: "2603-10-11 11:33:20",
h: sha512.New,
totp: 47863826,
secret: []byte(secret512),
},
// Non-RFC cases
18: {
t: "2603-10-11 11:33:30",
h: sha512.New,
totp: 47863826,
secret: []byte(secret512),
offset: -1,
},
19: {
t: "2603-10-11 11:34:00",
h: sha512.New,
totp: 47863826,
secret: []byte(secret512),
offset: -2,
},
20: {
t: "2603-10-11 11:32:40",
h: sha512.New,
totp: 47863826,
secret: []byte(secret512),
offset: 1,
},
}
func TestRFC6238Vectors(t *testing.T) {
for i, tc := range rfc6238TestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
o := otp.NewOTP(tc.secret, 8, tc.h, otp.TOTP(0, func() time.Time {
tt, err := time.Parse("2006-01-02 15:04:05", tc.t)
if err != nil {
panic(err)
}
return tt
}))
dst := make([]byte, 0, 64)
otp := o(tc.offset, dst)
if otp != tc.totp {
t.Errorf("Unexpected TOTP: got=%d, want=%d", otp, tc.totp)
}
})
}
}
// See RFC 4226 Appendix D
var rfc4226TestCases = [...]int32{
0: 755224,
1: 287082,
2: 359152,
3: 969429,
4: 338314,
5: 254676,
6: 287922,
7: 162583,
8: 399871,
9: 520489,
}
func TestRFC4226Vectors(t *testing.T) {
var i int
o := otp.NewOTP([]byte(secret), 6, sha1.New, func(offset int) uint64 {
return uint64(i + offset)
})
var tc int32
for i, tc = range rfc4226TestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
dst := make([]byte, 0, 20)
hotp := o(0, dst)
if hotp != tc {
t.Errorf("Unexpected HOTP: got=%d, want=%d", hotp, tc)
}
// Run each test twice, once with a fresh generator to make sure the hmac
// is being reset properly between each use.
o := otp.NewOTP([]byte(secret), 6, sha1.New, func(offset int) uint64 {
return uint64(i + offset)
})
hotp = o(0, nil)
if hotp != tc {
t.Errorf("Unexpected fresh HOTP: got=%d, want=%d", hotp, tc)
}
})
}
}
func TestExpectedPanics(t *testing.T) {
t.Run("0l", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected 0 l-value to panic")
}
}()
otp.NewOTP([]byte(secret), 0, sha1.New, otp.TOTP(0, nil))
})
t.Run("negl", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected negative l-value to panic")
}
}()
otp.NewOTP([]byte(secret), -1, sha1.New, otp.TOTP(0, nil))
})
t.Run("nilcounter", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected nil counter func to panic")
}
}()
otp.NewOTP([]byte(secret), 8, sha1.New, nil)
})
t.Run("nilhash", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected nil hash to panic")
}
}()
otp.NewOTP([]byte(secret), 8, nil, otp.TOTP(0, nil))
})
t.Run("nilkey", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected nil key to panic")
}
}()
otp.NewOTP(nil, 8, sha1.New, otp.TOTP(0, nil))
})
t.Run("emptykey", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected empty key to panic")
}
}()
otp.NewOTP([]byte{}, 8, sha1.New, otp.TOTP(0, nil))
})
}
func TestMallocs(t *testing.T) {
o := otp.NewOTP([]byte(secret), 6, sha1.New, otp.TOTP(0, nil))
buf := make([]byte, 0, 20)
n := testing.AllocsPerRun(1000, func() {
_ = o(0, buf)
})
if n != 2 {
t.Errorf("Want 2 allocs, got %f", n)
}
}