-
Notifications
You must be signed in to change notification settings - Fork 134
/
params_test.go
158 lines (144 loc) · 4.15 KB
/
params_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pkcs11
import (
"bytes"
"testing"
)
const notFound = 0xffffffff
// test whether mech is available; skip the test if it isn't
func needMech(t *testing.T, p *Ctx, sh SessionHandle, mech uint) {
slots, err := p.GetSlotList(true)
if err != nil {
t.Fatal("GetSlotList:", err)
}
_, err = p.GetMechanismInfo(slots[0], []*Mechanism{NewMechanism(mech, nil)})
if err == nil {
return
}
e, ok := err.(Error)
if !ok || e != CKR_MECHANISM_INVALID {
t.Fatal("GetMechanismInfo:", err)
}
t.Skipf("skipping test; mech 0x%X not supported by softhsm", mech)
}
func findObject(t *testing.T, p *Ctx, sh SessionHandle, class uint, label string) ObjectHandle {
template := []*Attribute{
NewAttribute(CKA_CLASS, class),
NewAttribute(CKA_LABEL, label),
}
if err := p.FindObjectsInit(sh, template); err != nil {
t.Fatal("FindObjectsInit:", err)
}
obj, _, err := p.FindObjects(sh, 1)
if err != nil {
t.Fatal("FindObjects:", err)
}
if err := p.FindObjectsFinal(sh); err != nil {
t.Fatal("FindObjectsFinal:", err)
}
if len(obj) > 0 {
return obj[0]
}
return notFound
}
// generate a rsa key if it doesn't exist
func getRSA(t *testing.T, p *Ctx, sh SessionHandle) (pub, priv ObjectHandle) {
pub = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
priv = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
if pub == notFound || priv == notFound {
pub, priv = generateRSAKeyPair(t, p, sh, "paramstest", false)
}
return
}
func TestPSSParams(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
defer finishSession(p, sh)
needMech(t, p, sh, CKM_RSA_PKCS_PSS)
pub, priv := getRSA(t, p, sh)
sum := []byte("1234567890abcdef1234567890abcdef")
params := NewPSSParams(CKM_SHA256, CKG_MGF1_SHA256, 32)
mech := []*Mechanism{NewMechanism(CKM_RSA_PKCS_PSS, params)}
if err := p.SignInit(sh, mech, priv); err != nil {
t.Fatal("SignInit:", err)
}
sig, err := p.Sign(sh, sum)
if err != nil {
t.Fatal("Sign:", err)
}
if err := p.VerifyInit(sh, mech, pub); err != nil {
t.Fatal("VerifyInit:")
}
if err := p.Verify(sh, sum, sig); err != nil {
t.Fatal("Verify:")
}
}
func TestOAEPParams(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
defer finishSession(p, sh)
needMech(t, p, sh, CKM_RSA_PKCS_OAEP)
pub, priv := getRSA(t, p, sh)
msg := []byte("1234567890abcdef1234567890abcdef")
params := NewOAEPParams(CKM_SHA_1, CKG_MGF1_SHA1, CKZ_DATA_SPECIFIED, nil)
mech := []*Mechanism{NewMechanism(CKM_RSA_PKCS_OAEP, params)}
if err := p.EncryptInit(sh, mech, pub); err != nil {
t.Fatal("EncryptInit:", err)
}
ciphertext, err := p.Encrypt(sh, msg)
if err != nil {
t.Fatal("Encrypt:", err)
}
if err := p.DecryptInit(sh, mech, priv); err != nil {
t.Fatal("DecryptInit:")
}
msg2, err := p.Decrypt(sh, ciphertext)
if err != nil {
t.Fatal("Decrypt:")
}
if !bytes.Equal(msg, msg2) {
t.Errorf("plaintext does not match: expected %x != actual %x", msg, msg2)
}
}
func TestGCMParams(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
defer finishSession(p, sh)
needMech(t, p, sh, CKM_AES_GCM)
key, err := p.GenerateKey(sh, []*Mechanism{NewMechanism(CKM_AES_KEY_GEN, nil)}, []*Attribute{
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_VALUE_LEN, 32),
})
if err != nil {
t.Fatal("GenerateKey:", err)
}
iv := []byte("0123456789ab")
msg := []byte("1234567890abcdef1234567890abcdef")
params := NewGCMParams(iv, nil, 128)
defer params.Free()
if err := p.EncryptInit(sh, []*Mechanism{NewMechanism(CKM_AES_GCM, params)}, key); err != nil {
t.Fatal("EncryptInit:", err)
}
ciphertext, err := p.Encrypt(sh, msg)
if err != nil {
t.Fatal("Encrypt:", err)
}
iv = params.IV()
params.Free()
params = NewGCMParams(iv, nil, 128)
defer params.Free()
if err := p.DecryptInit(sh, []*Mechanism{NewMechanism(CKM_AES_GCM, params)}, key); err != nil {
t.Fatal("DecryptInit:", err)
}
msg2, err := p.Decrypt(sh, ciphertext)
if err != nil {
t.Fatal("Decrypt:", err)
}
if !bytes.Equal(msg, msg2) {
t.Errorf("plaintext does not match: expected %x != actual %x", msg, msg2)
}
params.Free()
}