forked from ThalesGroup/pkcs11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bip32.go
169 lines (145 loc) · 6.2 KB
/
bip32.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
// Copyright 2013 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pkcs11
/*
#cgo windows CFLAGS: -DREPACK_STRUCTURES
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "pkcs11go.h"
struct ctx {
#ifdef _WIN32
HINSTANCE handle;
#else
void *handle;
#endif
CK_FUNCTION_LIST_PTR sym;
};
#define CKM_BIP32_MASTER_DERIVE (CKM_VENDOR_DEFINED + 0xE00)
#define CKM_BIP32_CHILD_DERIVE (CKM_VENDOR_DEFINED + 0xE01)
typedef struct CK_BIP32_MASTER_DERIVE_PARAMS {
CK_ATTRIBUTE_PTR pPublicKeyTemplate;
CK_ULONG ulPublicKeyAttributeCount;
CK_ATTRIBUTE_PTR pPrivateKeyTemplate;
CK_ULONG ulPrivateKeyAttributeCount;
CK_OBJECT_HANDLE hPublicKey; // output parameter
CK_OBJECT_HANDLE hPrivateKey; // output parameter
} CK_BIP32_MASTER_DERIVE_PARAMS;
typedef struct CK_BIP32_CHILD_DERIVE_PARAMS {
CK_ATTRIBUTE_PTR pPublicKeyTemplate;
CK_ULONG ulPublicKeyAttributeCount;
CK_ATTRIBUTE_PTR pPrivateKeyTemplate;
CK_ULONG ulPrivateKeyAttributeCount;
CK_ULONG_PTR pulPath;
CK_ULONG ulPathLen;
CK_OBJECT_HANDLE hPublicKey; // output parameter
CK_OBJECT_HANDLE hPrivateKey; // output parameter
CK_ULONG ulPathErrorIndex; // output parameter
} CK_BIP32_CHILD_DERIVE_PARAMS;
CK_RV DeriveBIP32Master(struct ctx * c, CK_SESSION_HANDLE session,
CK_OBJECT_HANDLE basekey,
CK_ATTRIBUTE_PTR aPub, CK_ULONG alenPub,
CK_ATTRIBUTE_PTR aPriv, CK_ULONG alenPriv,
CK_OBJECT_HANDLE_PTR publicKey, CK_OBJECT_HANDLE_PTR privateKey)
{
CK_MECHANISM mechanism;
mechanism.mechanism = CKM_BIP32_MASTER_DERIVE;
CK_BIP32_MASTER_DERIVE_PARAMS params;
params.pPublicKeyTemplate = aPub;
params.ulPublicKeyAttributeCount = alenPub;
params.pPrivateKeyTemplate = aPriv;
params.ulPrivateKeyAttributeCount = alenPriv;
params.hPublicKey = 0;
params.hPrivateKey = 0;
mechanism.pParameter = ¶ms;
mechanism.ulParameterLen = sizeof(params);
CK_RV e = c->sym->C_DeriveKey(session, &mechanism, basekey, NULL, 0, NULL);
*publicKey = params.hPublicKey;
*privateKey = params.hPrivateKey;
return e;
}
CK_RV DeriveBIP32Child(struct ctx * c, CK_SESSION_HANDLE session,
CK_OBJECT_HANDLE basekey,
CK_ATTRIBUTE_PTR aPub, CK_ULONG alenPub,
CK_ATTRIBUTE_PTR aPriv, CK_ULONG alenPriv, CK_ULONG_PTR path, CK_ULONG pathLen,
CK_OBJECT_HANDLE_PTR publicKey, CK_OBJECT_HANDLE_PTR privateKey, CK_ULONG_PTR pathErrorIndex)
{
CK_MECHANISM mechanism;
mechanism.mechanism = CKM_BIP32_CHILD_DERIVE;
CK_BIP32_CHILD_DERIVE_PARAMS params;
params.pPublicKeyTemplate = aPub;
params.ulPublicKeyAttributeCount = alenPub;
params.pPrivateKeyTemplate = aPriv;
params.ulPrivateKeyAttributeCount = alenPriv;
params.pulPath = path;
params.ulPathLen = pathLen;
params.hPublicKey = 0;
params.hPrivateKey = 0;
params.ulPathErrorIndex = 0;
mechanism.pParameter = ¶ms;
mechanism.ulParameterLen = sizeof(params);
CK_RV e = c->sym->C_DeriveKey(session, &mechanism, basekey, NULL, 0, NULL);
*publicKey = params.hPublicKey;
*privateKey = params.hPrivateKey;
*pathErrorIndex = params.ulPathErrorIndex;
return e;
}
CK_RV OpenSessionWithPartition(struct ctx * c, CK_ULONG slotID, CK_ULONG partitionID,
CK_ULONG flags, CK_SESSION_HANDLE_PTR session)
{
CK_RV e =
c->sym->CA_OpenSession((CK_SLOT_ID) slotID, partitionID, (CK_FLAGS) flags,
NULL, NULL, session);
return e;
}
*/
import "C"
// CKK_BIP32 should be assigned to the CKA_KEY_TYPE attribute of templates for derived keys
const CKK_BIP32 = CKK_VENDOR_DEFINED + 0x14
const CKA_DERIVE_TEMPLATE = (CKF_ARRAY_ATTRIBUTE | 0x00000213)
const CKA_BIP32_VERSION_BYTES = (CKA_VENDOR_DEFINED | 0x1101)
const CKA_BIP32_CHILD_INDEX = (CKA_VENDOR_DEFINED | 0x1102)
const CKA_BIP32_CHILD_DEPTH = (CKA_VENDOR_DEFINED | 0x1103)
const CKA_BIP32_ID = (CKA_VENDOR_DEFINED | 0x1104)
const CKA_BIP32_FINGERPRINT = (CKA_VENDOR_DEFINED | 0x1105)
const CKA_BIP32_PARENT_FINGERPRINT = (CKA_VENDOR_DEFINED | 0x1106)
// for Ed25519
const CKK_EC_EDWARDS = (CKK_VENDOR_DEFINED | 0x00008003)
const CKM_EC_EDWARDS_KEY_PAIR_GEN = (CKM_VENDOR_DEFINED | 0x00009040)
const CKM_EDDSA = (CKM_VENDOR_DEFINED | 0x00009041)
func (c *Ctx) DeriveBIP32MasterKeys(sh SessionHandle, basekey ObjectHandle, publicAttr []*Attribute, privateAttr []*Attribute) (ObjectHandle, ObjectHandle, error) {
var publicKey C.CK_OBJECT_HANDLE
var privateKey C.CK_OBJECT_HANDLE
publicAttrArena, publicAttrC, publicAttrLen := cAttributeList(publicAttr)
defer publicAttrArena.Free()
privateAttrArena, privateAttrC, privateAttrLen := cAttributeList(privateAttr)
defer privateAttrArena.Free()
e := C.DeriveBIP32Master(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(basekey), publicAttrC, publicAttrLen, privateAttrC, privateAttrLen, &publicKey, &privateKey)
return ObjectHandle(publicKey), ObjectHandle(privateKey), toError(e)
}
func (c *Ctx) DeriveBIP32ChildKeys(sh SessionHandle, basekey ObjectHandle, publicAttr []*Attribute, privateAttr []*Attribute, path []uint32) (ObjectHandle, ObjectHandle, uint, error) {
var publicKey C.CK_OBJECT_HANDLE
var privateKey C.CK_OBJECT_HANDLE
var pathErrorIndex C.CK_ULONG
publicAttrArena, publicAttrC, publicAttrLen := cAttributeList(publicAttr)
defer publicAttrArena.Free()
privateAttrArena, privateAttrC, privateAttrLen := cAttributeList(privateAttr)
defer privateAttrArena.Free()
cPath := make([]C.CK_ULONG, len(path))
for i, pathVal := range path {
cPath[i] = C.CK_ULONG(pathVal)
}
e := C.DeriveBIP32Child(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(basekey), publicAttrC, publicAttrLen, privateAttrC, privateAttrLen, &cPath[0], C.CK_ULONG(len(path)), &publicKey, &privateKey, &pathErrorIndex)
return ObjectHandle(publicKey), ObjectHandle(privateKey), uint(pathErrorIndex), toError(e)
}
// OpenSessionWithPartition opens a session between an application and a token and a partition.
func (c *Ctx) OpenSessionWithPartition(slotID uint, partitionID uint, flags uint) (SessionHandle, error) {
var s C.CK_SESSION_HANDLE
e := C.OpenSessionWithPartition(c.ctx, C.CK_ULONG(slotID), C.CK_ULONG(partitionID), C.CK_ULONG(flags), C.CK_SESSION_HANDLE_PTR(&s))
return SessionHandle(s), toError(e)
}