forked from ThalesGroup/pkcs11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bip32_test.go
291 lines (251 loc) · 9.18 KB
/
bip32_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pkcs11
import (
"bytes"
"crypto/sha256"
"encoding/asn1"
"encoding/hex"
"strconv"
"strings"
"testing"
)
var (
LIB = "/usr/safenet/lunaclient/lib/libCryptoki2_64.so"
SLOT = uint(0)
PIN = "userpin"
)
func loadLib(t *testing.T) *Ctx {
lib := LIB
t.Logf("loading %s", lib)
p := New(lib)
if p == nil {
t.Fatal("Failed to init lib")
}
return p
}
func setup(t *testing.T) (*Ctx, SessionHandle) {
p := loadLib(t)
p.Initialize()
session, err := p.OpenSession(SLOT, CKF_SERIAL_SESSION|CKF_RW_SESSION)
if err != nil {
t.Fatalf("open session %s\n", err)
}
if err = p.Login(session, CKU_USER, PIN); err != nil {
t.Fatalf("user pin %s\n", err)
}
return p, session
}
func teardown(p *Ctx, session SessionHandle) {
p.CloseSession(session)
p.Finalize()
}
func deriveMasterKeyPair(p *Ctx, session SessionHandle, seed ObjectHandle) (ObjectHandle, ObjectHandle, error) {
publicTemplate := []*Attribute{
NewAttribute(CKA_TOKEN, true),
NewAttribute(CKA_KEY_TYPE, CKK_BIP32),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_VERIFY, true),
NewAttribute(CKA_DERIVE, true),
NewAttribute(CKA_MODIFIABLE, false),
}
privateTemplate := []*Attribute{
NewAttribute(CKA_TOKEN, true),
NewAttribute(CKA_KEY_TYPE, CKK_BIP32),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_SIGN, true),
NewAttribute(CKA_DERIVE, true),
NewAttribute(CKA_MODIFIABLE, false),
NewAttribute(CKA_EXTRACTABLE, false),
}
return p.DeriveBIP32MasterKeys(session, seed, publicTemplate, privateTemplate)
}
func deriveChildKeyPair(p *Ctx, session SessionHandle, masterPrivate ObjectHandle, path []uint32) (ObjectHandle, ObjectHandle, uint, error) {
publicTemplate := []*Attribute{
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_KEY_TYPE, CKK_BIP32),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_VERIFY, true),
NewAttribute(CKA_DERIVE, false),
NewAttribute(CKA_MODIFIABLE, false),
}
privateTemplate := []*Attribute{
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_KEY_TYPE, CKK_BIP32),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_SIGN, true),
NewAttribute(CKA_DERIVE, false),
NewAttribute(CKA_MODIFIABLE, false),
NewAttribute(CKA_EXTRACTABLE, false),
}
return p.DeriveBIP32ChildKeys(session, masterPrivate, publicTemplate, privateTemplate, path)
}
func parsePath(path string) []uint32 {
path = strings.Replace(path, "m/", "", 1)
split := strings.Split(path, "/")
pathUints := make([]uint32, len(split))
for i, index := range split {
var x uint32
if index[len(index)-1] == '\'' {
x = 0x80000000
index = strings.TrimRight(index, "'")
}
x1, _ := strconv.ParseUint(index, 10, 32)
x += uint32(x1)
pathUints[i] = x
}
return pathUints
}
func sign(p *Ctx, session SessionHandle, objectHandle ObjectHandle, data []byte) ([]byte, error) {
err := p.SignInit(session, []*Mechanism{NewMechanism(CKM_ECDSA, nil)}, objectHandle)
if err != nil {
return nil, err
}
return p.Sign(session, data)
}
func verify(p *Ctx, session SessionHandle, objectHandle ObjectHandle, data []byte, signature []byte) error {
err := p.VerifyInit(session, []*Mechanism{NewMechanism(CKM_ECDSA, nil)}, objectHandle)
if err != nil {
return err
}
return p.Verify(session, signature, data)
}
func injectSeed(p *Ctx, session SessionHandle, seed []byte) (ObjectHandle, error) {
mech := []*Mechanism{NewMechanism(CKM_AES_KEY_GEN, nil)}
aesTemplate := []*Attribute{
NewAttribute(CKA_KEY_TYPE, CKK_AES),
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_UNWRAP, true),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_VALUE_LEN, 32),
}
wrappingKey, err := p.GenerateKey(session, mech, aesTemplate)
if err != nil {
return 0, err
}
mech = []*Mechanism{NewMechanism(CKM_AES_CBC, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})}
err = p.EncryptInit(session, mech, wrappingKey)
if err != nil {
return 0, err
}
encrypted, err := p.Encrypt(session, seed)
if err != nil {
return 0, err
}
mech = []*Mechanism{NewMechanism(CKM_AES_CBC, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})}
seedTemplate := []*Attribute{
NewAttribute(CKA_CLASS, CKO_SECRET_KEY),
NewAttribute(CKA_KEY_TYPE, CKK_GENERIC_SECRET),
NewAttribute(CKA_TOKEN, true),
NewAttribute(CKA_DERIVE, true),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_EXTRACTABLE, false),
NewAttribute(CKA_MODIFIABLE, false),
NewAttribute(CKA_VALUE_LEN, len(seed)),
}
return p.UnwrapKey(session, mech, wrappingKey, encrypted, seedTemplate)
}
func getECPoint(p *Ctx, session SessionHandle, objectHandle ObjectHandle) []byte {
attributeTemplate := []*Attribute{
NewAttribute(CKA_EC_POINT, nil),
}
attributes, err := p.GetAttributeValue(session, objectHandle, attributeTemplate)
if err != nil {
return nil
}
return attributes[0].Value
}
func signVerify(t *testing.T, p *Ctx, session SessionHandle, public, private ObjectHandle) {
message := []byte("message bip32")
data := sha256.Sum256(message)
sig, err := sign(p, session, private, data[:])
if err != nil {
t.Fatalf("sign %s\n", err)
}
err = verify(p, session, public, sig, data[:])
if err != nil {
t.Fatalf("verify %s\n", err)
}
}
func TestBIP32DeriveSignVerify(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}
p, session := setup(t)
defer teardown(p, session)
mech := []*Mechanism{NewMechanism(CKM_GENERIC_SECRET_KEY_GEN, nil)}
seedTemplate := []*Attribute{
NewAttribute(CKA_KEY_TYPE, CKK_GENERIC_SECRET),
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_DERIVE, true),
NewAttribute(CKA_PRIVATE, true),
NewAttribute(CKA_EXTRACTABLE, false),
NewAttribute(CKA_MODIFIABLE, false),
NewAttribute(CKA_VALUE_LEN, 32),
}
seed, err := p.GenerateKey(session, mech, seedTemplate)
if err != nil {
t.Fatalf("master seed %s\n", err)
}
_, privateMaster, err := deriveMasterKeyPair(p, session, seed)
if err != nil {
t.Fatalf("master key pair %s\n", err)
}
path := parsePath("m/44'/60'/0'/0/0")
publicChild, privateChild, _, err := deriveChildKeyPair(p, session, privateMaster, path)
if err != nil {
t.Fatalf("child key pair %s\n", err)
}
signVerify(t, p, session, publicChild, privateChild)
}
func TestBIP32VectorInjectDeriveSignVerify(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}
//https://en.bitcoin.it/wiki/BIP_0032_TestVectors
vector := [][]string{
{"000102030405060708090a0b0c0d0e0f", "m/0'", "035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"},
{"000102030405060708090a0b0c0d0e0f", "m/0'/1", "03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"},
{"000102030405060708090a0b0c0d0e0f", "m/0'/1/2'", "0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"},
{"000102030405060708090a0b0c0d0e0f", "m/0'/1/2'/2", "02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"},
{"000102030405060708090a0b0c0d0e0f", "m/0'/1/2'/2/1000000000", "022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011"},
{"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", "m/0", "02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"},
{"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", "m/0/2147483647'", "03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b"},
{"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", "m/0/2147483647'/1", "03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9"},
{"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", "m/0/2147483647'/1/2147483646'", "02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0"},
{"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542", "m/0/2147483647'/1/2147483646'/2", "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"},
}
p, session := setup(t)
defer teardown(p, session)
for _, v := range vector {
seed, _ := hex.DecodeString(v[0])
path := parsePath(v[1])
public, _ := hex.DecodeString(v[2])
masterSeed, err := injectSeed(p, session, seed)
if err != nil {
t.Fatalf("inject seed %s\n", err)
}
_, privateMaster, err := deriveMasterKeyPair(p, session, masterSeed)
if err != nil {
t.Fatalf("master key pair %s\n", err)
}
publicChild, privateChild, _, err := deriveChildKeyPair(p, session, privateMaster, path)
if err != nil {
t.Fatalf("child key pair %s\n", err)
}
ecp := getECPoint(p, session, publicChild)
var ecPoint []byte
asn1.Unmarshal(ecp, &ecPoint)
ecPoint = ecPoint[1:33]
expected := public[1:]
if !bytes.Equal(ecPoint, expected) {
t.Fatalf("ecPoint was %s. Expected %s.", hex.EncodeToString(ecPoint), hex.EncodeToString(expected))
}
signVerify(t, p, session, publicChild, privateChild)
}
}