-
Notifications
You must be signed in to change notification settings - Fork 4
/
yk_test.go
330 lines (297 loc) · 6.81 KB
/
yk_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
// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package yubikey
import (
"bytes"
"encoding/hex"
"reflect"
"testing"
)
var crcTests = []struct {
in []byte
out uint16
}{
{
[]byte{
0x87, 0x92, 0xeb, 0xfe, 0x26, 0xcc, 0x13, 0x00,
0x30, 0xc2, 0x00, 0x11, 0xc8, 0x9f, 0x23, 0xc8,
},
61624,
},
}
var modHexEncodeTests = []struct {
in string
out string
}{
{"test", "ifhgieif"},
{"justanothergotest", "hligieifhbhuhvifhjhgidhihvifhgieif"},
{"foobar", "hhhvhvhdhbid"},
}
var modHexPTests = []struct {
in string
out bool
}{
{"cbdefghijklnrtuv", true},
{"0123Xabc", false},
}
var parseTests = []struct {
token string
key []byte
result []byte
}{
{
"dcflcindvdbrblehecuitvjkjevvehjd",
[]byte{
0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f, 0xbd, 0x0c,
0x33, 0x33, 0x0f, 0x1c, 0x35, 0x48, 0x71, 0xdb,
},
[]byte{
0x87, 0x92, 0xeb, 0xfe, 0x26, 0xcc, 0x13, 0x00,
0xa8, 0xc0, 0x00, 0x10, 0xb4, 0x08, 0x6f, 0x5b,
},
},
{
"hknhfjbrjnlnldnhcujvddbikngjrtgh",
[]byte{
0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f, 0xbd, 0x0c,
0x33, 0x33, 0x0f, 0x1c, 0x35, 0x48, 0x71, 0xdb,
},
[]byte{
0x87, 0x92, 0xeb, 0xfe, 0x26, 0xcc, 0x13, 0x00,
0x30, 0xc2, 0x00, 0x11, 0xc8, 0x9f, 0x23, 0xc8,
},
},
}
var otpTests = []struct {
token []byte
key []byte
capslock bool
counter bool
crc16 bool
}{
{
[]byte{
0x16, 0xe1, 0xe5, 0xd9, 0xd3, 0x99, 0x10, 0x04,
0x45, 0x20, 0x07, 0xe3, 0x02, 0x00, 0x22, 0x6d,
},
[]byte{
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
},
false,
false,
true,
},
{
[]byte{
0x16, 0xe1, 0xe5, 0xd9, 0xd3, 0x99, 0x81, 0xab,
0x45, 0x20, 0x07, 0xe3, 0x02, 0x00, 0x6d, 0x80,
},
[]byte{
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
},
true,
false,
true,
},
{
[]byte{
0x16, 0xe1, 0xe5, 0xd9, 0xd3, 0x99, 0xff, 0x7f,
0x45, 0x20, 0x07, 0xe3, 0x02, 0x00, 0xbd, 0xa3,
},
[]byte{
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
},
false,
true,
true,
},
}
var otpStringTests = []struct {
in string
key []byte
uid Uid
pubid PubID
}{
{
"vvddbuvjvkbhrtkjrdhvlenirbebujtuhfkrnkvbflcr",
[]byte{
0x21, 0x47, 0xe5, 0x88, 0xc5, 0x59, 0xdd, 0xc3,
0x1f, 0x37, 0x26, 0x52, 0x6c, 0x4d, 0xa6, 0xc4,
},
Uid{
0x34, 0xcb, 0x2f, 0xf0, 0x1d, 0xb8,
},
PubID{
0x76, 0x76, 0x64, 0x64, 0x62, 0x75,
0x76, 0x6a, 0x76, 0x6b, 0x62, 0x68,
},
},
{
"ccccccbtirnbccccccccccccccccccccketfufegheibrjcinntgtfvkntlguvug",
[]byte{
0x7a, 0x34, 0x80, 0xbc, 0x19, 0x0d, 0x35, 0xd6,
0xcd, 0xb8, 0x86, 0xe6, 0x63, 0xa5, 0x15, 0xc5,
},
Uid{
0x26, 0x6b, 0x89, 0xdd, 0x4a, 0xc7,
},
PubID{
0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x62, 0x74, 0x69, 0x72, 0x6e, 0x62,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63,
},
},
}
func TestCapslock(t *testing.T) {
for x, test := range otpTests {
token, err := NewTokenFromBytes(test.token)
if err != nil {
t.Errorf("Capslock test #%d failed: %v", x, err)
continue
}
res := token.Capslock()
if res != test.capslock {
t.Errorf("Capslock test #%d failed: got: %v want: %v",
x, res, test.capslock)
}
}
}
func TestCounter(t *testing.T) {
for x, test := range otpTests {
token, err := NewTokenFromBytes(test.token)
if err != nil {
t.Errorf("Counter test #%d failed: %v", x, err)
continue
}
res := token.Counter() == 0x7fff
if res != test.counter {
t.Errorf("Counter test #%d failed: got: %v want: %v",
x, res, test.counter)
}
}
}
func TestCrc(t *testing.T) {
for x, test := range crcTests {
token, err := NewTokenFromBytes(test.in)
if err != nil {
t.Errorf("Crc16 test #%d failed: %v\n", x, err)
continue
}
if res := token.Crc16(); res != test.out {
t.Errorf("Crc16 test #%d failed: got: %v want: %v",
x, res, test.out)
continue
}
}
}
func TestModHex(t *testing.T) {
// Encode and Decode tests
for x, test := range modHexEncodeTests {
// Encode tests
if res := ModHexEncode([]byte(test.in)); !bytes.Equal(res, []byte(test.out)) {
t.Errorf("ModeHexEncode test #%d failed: got: %s want: %s",
x, res, test.out)
continue
}
// Decode tests
if res := ModHexDecode([]byte(test.out)); !bytes.Equal(res, []byte(test.in)) {
t.Errorf("ModeHexDecode test #%d failed: got: %v want: %v",
x, res, test.in)
continue
}
}
// ModHexP tests
for x, test := range modHexPTests {
tmp := []byte(test.in)
if res := ModHexP(tmp); res != test.out {
t.Errorf("ModeHexP test #%d failed: got: %v want: %v",
x, res, test.out)
continue
}
}
}
func TestParse(t *testing.T) {
for x, test := range parseTests {
otp := NewOTP(test.token)
key := NewKey(test.key)
res, err := otp.Parse(key)
if err != nil {
t.Errorf("TestParse test #%d failed: %v", x, err)
continue
}
buf := res.Bytes()
if !bytes.Equal(buf, test.result) {
t.Errorf("TestParse test #%d failed: got: %x want: %x",
x, buf, test.result)
continue
}
}
}
func TestOTP(t *testing.T) {
for x, test := range otpTests {
token, err := NewTokenFromBytes(test.token)
if err != nil {
t.Errorf("TestOTP test #%d failed: %v", x, err)
continue
}
key := NewKey(test.key)
otp := token.Generate(key)
res, err := otp.Parse(key)
if err != nil {
t.Errorf("TestOTP test #%d failed: %v", x, err)
continue
}
buf := res.Bytes()
if !bytes.Equal(test.token, buf) {
t.Errorf("TestOTP test #%d failed: got: %v want: %v",
x, test.token, buf)
continue
}
}
}
func TestOTPString(t *testing.T) {
for x, test := range otpStringTests {
pubid, otp, err := ParseOTPString(test.in)
if err != nil {
t.Errorf("TestOTPString test #%d failed: %v", x, err)
continue
}
key := NewKey(test.key)
tok, err := otp.Parse(key)
if err != nil {
t.Errorf("TestOTPString test #%d failed: %v", x, err)
}
if !tok.CrcOkP() {
t.Errorf("TestOTPString CrcOkP test #%d failed", x)
continue
}
if !bytes.Equal(tok.Uid[:], test.uid[:]) {
t.Errorf("TestOTPString Uid test #%d failed: got: %v want: %v",
x, tok.Uid, test.uid)
continue
}
if !bytes.Equal(pubid, test.pubid) {
t.Errorf("TestOTPString PubID test #%d failed: got: %x want: %x",
x, pubid, test.pubid)
continue
}
}
}
func TestNewToken(t *testing.T) {
// sample key from test-vectors.txt
uid, _ := hex.DecodeString("8792ebfe26cc")
token := NewToken(NewUid(uid), 19, 49712, 0, 17, 40904)
var u Uid
copy(u[:], uid)
tok := &Token{Uid: u, Ctr: 19, Tstpl: 49712, Tstph: 0, Use: 17, Rnd: 40904, Crc: 51235}
if !reflect.DeepEqual(tok, token) {
t.Errorf("TestNewToken failed: got=%#v wanted=%#v", token, tok)
}
}