-
Notifications
You must be signed in to change notification settings - Fork 6
/
yubihsm_test.go
65 lines (52 loc) · 1.13 KB
/
yubihsm_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
// +build yubihsm
package seth
import (
"crypto/rand"
"os"
"testing"
"github.com/newalchemylimited/seth/yubihsm"
)
func TestYubiHSM(t *testing.T) {
yubihsm.SetVerbosity(100)
hsm := FindHSM("yubihsm")
if hsm == nil {
t.Skip("didn't find yubihsm device; skipping")
}
t.Log("hsm probe succeeded")
pass := os.Getenv("HSM_PASS")
if pass == "" {
t.Skip("HSM_PASS unset")
}
if err := hsm.Unlock([]byte(pass)); err != nil {
t.Fatalf("couldn't unlock hsm: %s", err)
}
keys, err := hsm.Pubkeys()
if err != nil {
t.Fatalf("listing hsm keys: %s", err)
}
for i := range keys {
t.Logf("found address %s", keys[i].Public.Address())
}
var h Hash
for i := range keys {
rand.Read(h[:])
sign, err := hsm.Signer(&keys[i])
if err != nil {
t.Errorf("getting signer %s: %s", keys[i].ID, err)
continue
}
sig, err := sign(&h)
if err != nil {
t.Errorf("key %s: signing: %s", keys[i].ID, err)
continue
}
addr, err := sig.Recover(&h)
if err != nil {
t.Errorf("key %s: recover: %s", keys[i].ID, err)
continue
}
if *addr != keys[i].Public {
t.Errorf("keys: %s != %s", addr, &keys[i].Public)
}
}
}