-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprivate.go
351 lines (300 loc) · 9.05 KB
/
private.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package account
import (
"bytes"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/util"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/sha3"
)
// base type for PrivateKey
type PrivateKey struct {
PrivateKeyInterface
}
type PrivateKeyInterface interface {
Account() *Account
KeyType() int
PrivateKeyBytes() []byte
Bytes() []byte
String() string
IsTesting() bool
MarshalText() ([]byte, error)
}
// for ed25519 keys
type ED25519PrivateKey struct {
Test bool
PrivateKey []byte
}
// just for debugging
type NothingPrivateKey struct {
Test bool
PrivateKey []byte
}
// seed parameters
var (
seedHeader = []byte{0x5a, 0xfe, 0x01}
seedHeaderLength = len(seedHeader)
seedNonce = [24]byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
seedCountBM = [16]byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe7,
}
)
const (
seedPrefixLength = 1
seedKeyLength = 32
seedChecksumLength = 4
)
// this converts a Base58 encoded seed string and returns a private key
//
// one of the specific private key types are returned using the base "PrivateKeyInterface"
// interface type to allow individual methods to be called.
func PrivateKeyFromBase58Seed(seedBase58Encoded string) (*PrivateKey, error) {
seed := util.FromBase58(seedBase58Encoded)
if 0 == len(seed) {
return nil, fault.ErrCannotDecodeSeed
}
// Compute seed length
keyLength := len(seed) - seedHeaderLength - seedChecksumLength
if seedKeyLength+seedPrefixLength != keyLength {
return nil, fault.ErrInvalidSeedLength
}
// Check seed header
if !bytes.Equal(seedHeader, seed[:seedHeaderLength]) {
return nil, fault.ErrInvalidSeedHeader
}
// Checksum
checksumStart := len(seed) - checksumLength
checksum := sha3.Sum256(seed[:checksumStart])
if !bytes.Equal(checksum[:seedChecksumLength], seed[checksumStart:]) {
return nil, fault.ErrChecksumMismatch
}
var secretKey [seedKeyLength]byte
copy(secretKey[:], seed[seedHeaderLength+seedPrefixLength:])
prefix := seed[seedHeaderLength : seedHeaderLength+seedPrefixLength]
// first byte of prefix is test/live indication
isTest := prefix[0] == 0x01
encrypted := secretbox.Seal([]byte{}, seedCountBM[:], &seedNonce, &secretKey)
_, priv, err := ed25519.GenerateKey(bytes.NewBuffer(encrypted))
if nil != err {
return nil, err
}
privateKey := &PrivateKey{
PrivateKeyInterface: &ED25519PrivateKey{
Test: isTest,
PrivateKey: priv,
},
}
return privateKey, nil
}
// this converts a Base58 encoded string and returns an private key
//
// one of the specific private key types are returned using the base "PrivateKeyInterface"
// interface type to allow individual methods to be called.
func PrivateKeyFromBase58(privateKeyBase58Encoded string) (*PrivateKey, error) {
// Decode the privateKey
privateKeyDecoded := util.FromBase58(privateKeyBase58Encoded)
if 0 == len(privateKeyDecoded) {
return nil, fault.ErrCannotDecodePrivateKey
}
// Parse the key variant
keyVariant, keyVariantLength := util.FromVarint64(privateKeyDecoded)
// Check key type
if 0 == keyVariantLength || keyVariant&publicKeyCode == publicKeyCode {
return nil, fault.ErrNotPrivateKey
}
// compute algorithm
keyAlgorithm := keyVariant >> algorithmShift
if keyAlgorithm < 0 || keyAlgorithm >= algorithmLimit {
return nil, fault.ErrInvalidKeyType
}
// network selection
isTest := 0 != keyVariant&testKeyCode
// Compute key length
keyLength := len(privateKeyDecoded) - keyVariantLength - checksumLength
if keyLength <= 0 {
return nil, fault.ErrInvalidKeyLength
}
// Checksum
checksumStart := len(privateKeyDecoded) - checksumLength
checksum := sha3.Sum256(privateKeyDecoded[:checksumStart])
if !bytes.Equal(checksum[:checksumLength], privateKeyDecoded[checksumStart:]) {
return nil, fault.ErrChecksumMismatch
}
// return a pointer to the specific private key type
switch keyAlgorithm {
case ED25519:
if keyLength != ed25519.PrivateKeySize {
return nil, fault.ErrInvalidKeyLength
}
priv := privateKeyDecoded[keyVariantLength:checksumStart]
privateKey := &PrivateKey{
PrivateKeyInterface: &ED25519PrivateKey{
Test: isTest,
PrivateKey: priv,
},
}
return privateKey, nil
case Nothing:
if 2 != keyLength {
return nil, fault.ErrInvalidKeyLength
}
priv := privateKeyDecoded[keyVariantLength:checksumStart]
privateKey := &PrivateKey{
PrivateKeyInterface: &NothingPrivateKey{
Test: isTest,
PrivateKey: priv,
},
}
return privateKey, nil
default:
return nil, fault.ErrInvalidKeyType
}
}
// this converts a byte encoded buffer and returns an private key
//
// one of the specific private key types are returned using the base "PrivateKeyInterface"
// interface type to allow individual methods to be called.
func PrivateKeyFromBytes(privateKeyBytes []byte) (*PrivateKey, error) {
// Parse the key variant
keyVariant, keyVariantLength := util.FromVarint64(privateKeyBytes)
// Check key type
if 0 == keyVariantLength || keyVariant&publicKeyCode == publicKeyCode {
return nil, fault.ErrNotPrivateKey
}
// compute algorithm
keyAlgorithm := keyVariant >> algorithmShift
if keyAlgorithm < 0 || keyAlgorithm >= algorithmLimit {
return nil, fault.ErrInvalidKeyType
}
// network selection
isTest := 0 != keyVariant&testKeyCode
// Compute key length
keyLength := len(privateKeyBytes) - keyVariantLength
if keyLength <= 0 {
return nil, fault.ErrInvalidKeyLength
}
// return a pointer to the specific private key type
switch keyAlgorithm {
case ED25519:
if keyLength != ed25519.PrivateKeySize {
return nil, fault.ErrInvalidKeyLength
}
priv := privateKeyBytes[keyVariantLength:]
privateKey := &PrivateKey{
PrivateKeyInterface: &ED25519PrivateKey{
Test: isTest,
PrivateKey: priv,
},
}
return privateKey, nil
case Nothing:
if 2 != keyLength {
return nil, fault.ErrInvalidKeyLength
}
priv := privateKeyBytes[keyVariantLength:]
privateKey := &PrivateKey{
PrivateKeyInterface: &NothingPrivateKey{
Test: isTest,
PrivateKey: priv,
},
}
return privateKey, nil
default:
return nil, fault.ErrInvalidKeyType
}
}
func (privateKey *PrivateKey) UnmarshalText(s []byte) error {
a, err := PrivateKeyFromBase58(string(s))
if nil != err {
return err
}
privateKey.PrivateKeyInterface = a.PrivateKeyInterface
return nil
}
// ED25519
// -------
// return whether the private key is in test mode or not
func (privateKey *ED25519PrivateKey) IsTesting() bool {
return privateKey.Test
}
// key type code (see enumeration in account.go)
func (privateKey *ED25519PrivateKey) KeyType() int {
return ED25519
}
// return the corresponding account
func (privateKey *ED25519PrivateKey) Account() *Account {
return &Account{
AccountInterface: &ED25519Account{
Test: privateKey.Test,
PublicKey: privateKey.PrivateKey[ed25519.PrivateKeySize-ed25519.PublicKeySize:],
},
}
}
// fetch the private key as byte slice
func (privateKey *ED25519PrivateKey) PrivateKeyBytes() []byte {
return privateKey.PrivateKey[:]
}
// byte slice for encoded key
func (privateKey *ED25519PrivateKey) Bytes() []byte {
keyVariant := byte(ED25519 << algorithmShift)
if privateKey.Test {
keyVariant |= testKeyCode
}
return append([]byte{keyVariant}, privateKey.PrivateKey[:]...)
}
// base58 encoding of encoded key
func (privateKey *ED25519PrivateKey) String() string {
buffer := privateKey.Bytes()
checksum := sha3.Sum256(buffer)
buffer = append(buffer, checksum[:checksumLength]...)
return util.ToBase58(buffer)
}
// convert an privateKey to its Base58 JSON form
func (privateKey ED25519PrivateKey) MarshalText() ([]byte, error) {
return []byte(privateKey.String()), nil
}
// Nothing
// -------
// return whether the private key is in test mode or not
func (privateKey *NothingPrivateKey) IsTesting() bool {
return privateKey.Test
}
// key type code (see enumeration in account.go)
func (privateKey *NothingPrivateKey) KeyType() int {
return Nothing
}
// return the corresponding account
func (privateKey *NothingPrivateKey) Account() *Account {
return nil
}
// fetch the private key as byte slice
func (privateKey *NothingPrivateKey) PrivateKeyBytes() []byte {
return privateKey.PrivateKey[:]
}
// byte slice for encoded key
func (privateKey *NothingPrivateKey) Bytes() []byte {
keyVariant := byte(Nothing << algorithmShift)
if privateKey.Test {
keyVariant |= testKeyCode
}
return append([]byte{keyVariant}, privateKey.PrivateKey[:]...)
}
// base58 encoding of encoded key
func (privateKey *NothingPrivateKey) String() string {
buffer := privateKey.Bytes()
checksum := sha3.Sum256(buffer)
buffer = append(buffer, checksum[:checksumLength]...)
return util.ToBase58(buffer)
}
// convert an privateKey to its Base58 JSON form
func (privateKey NothingPrivateKey) MarshalText() ([]byte, error) {
return []byte(privateKey.String()), nil
}