forked from yawn/ykoath
-
Notifications
You must be signed in to change notification settings - Fork 2
/
password.go
69 lines (55 loc) · 1.1 KB
/
password.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
package ykoath
import (
"crypto/hmac"
crand "crypto/rand"
"crypto/sha1"
"fmt"
)
func (o *OATH) SetPassword(key []byte) (err error) {
if key == nil {
_, err = o.send(0x00, 0x03, 0x00, 0x00)
} else {
alg := HmacSha1
chal := make([]byte, 16)
mac := hmac.New(sha1.New, key)
crand.Read(chal)
mac.Write(chal)
resp := mac.Sum(nil)
_, err = o.send(0x00, 0x03, 0x00, 0x00,
write(0x73, []byte{byte(alg)}, key),
write(0x74, chal),
write(0x75, resp),
)
}
return
}
func (o *OATH) Validate(chalFromSelect []byte, key []byte) (err error) {
chal := make([]byte, 16)
crand.Read(chal)
// Host authentication
mac := hmac.New(sha1.New, key)
mac.Write(chalFromSelect)
resp := mac.Sum(nil)
res, err := o.send(0x00, 0xa3, 0x00, 0x00,
write(0x75, resp),
write(0x74, chal),
)
if err != nil {
return
}
success := false
for _, tv := range res {
switch tv.tag {
case 0x75:
// Card authentication
mac = hmac.New(sha1.New, key)
mac.Write(chal)
resp = mac.Sum(nil)
success = hmac.Equal(resp, tv.value)
}
}
if !success {
err = fmt.Errorf(errAuthentication)
}
return
}