/
key.go
206 lines (181 loc) · 4.9 KB
/
key.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package file
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"github.com/wssll789/bee/pkg/crypto"
"github.com/wssll789/bee/pkg/keystore"
"golang.org/x/crypto/scrypt"
"golang.org/x/crypto/sha3"
)
var _ keystore.Service = (*Service)(nil)
const (
keyHeaderKDF = "scrypt"
keyVersion = 3
scryptN = 1 << 15
scryptR = 8
scryptP = 1
scryptDKLen = 32
)
// This format is compatible with Ethereum JSON v3 key file format.
type encryptedKey struct {
Address string `json:"address"`
Crypto keyCripto `json:"crypto"`
Version int `json:"version"`
}
type keyCripto struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherParams `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams kdfParams `json:"kdfparams"`
MAC string `json:"mac"`
}
type cipherParams struct {
IV string `json:"iv"`
}
type kdfParams struct {
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
DKLen int `json:"dklen"`
Salt string `json:"salt"`
}
func encryptKey(k *ecdsa.PrivateKey, password string) ([]byte, error) {
data := crypto.EncodeSecp256k1PrivateKey(k)
kc, err := encryptData(data, []byte(password))
if err != nil {
return nil, err
}
addr, err := crypto.NewEthereumAddress(k.PublicKey)
if err != nil {
return nil, err
}
return json.Marshal(encryptedKey{
Address: hex.EncodeToString(addr),
Crypto: *kc,
Version: keyVersion,
})
}
func decryptKey(data []byte, password string) (*ecdsa.PrivateKey, error) {
var k encryptedKey
if err := json.Unmarshal(data, &k); err != nil {
return nil, err
}
if k.Version != keyVersion {
return nil, fmt.Errorf("unsupported key version: %v", k.Version)
}
d, err := decryptData(k.Crypto, password)
if err != nil {
return nil, err
}
return crypto.DecodeSecp256k1PrivateKey(d)
}
func encryptData(data, password []byte) (*keyCripto, error) {
salt := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, fmt.Errorf("read random data: %w", err)
}
derivedKey, err := scrypt.Key(password, salt, scryptN, scryptR, scryptP, scryptDKLen)
if err != nil {
return nil, err
}
encryptKey := derivedKey[:16]
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, fmt.Errorf("read random data: %w", err)
}
cipherText, err := aesCTRXOR(encryptKey, data, iv)
if err != nil {
return nil, err
}
mac := sha3.Sum256(append(derivedKey[16:32], cipherText...))
return &keyCripto{
Cipher: "aes-128-ctr",
CipherText: hex.EncodeToString(cipherText),
CipherParams: cipherParams{
IV: hex.EncodeToString(iv),
},
KDF: keyHeaderKDF,
KDFParams: kdfParams{
N: scryptN,
R: scryptR,
P: scryptP,
DKLen: scryptDKLen,
Salt: hex.EncodeToString(salt),
},
MAC: hex.EncodeToString(mac[:]),
}, nil
}
func decryptData(v keyCripto, password string) ([]byte, error) {
if v.Cipher != "aes-128-ctr" {
return nil, fmt.Errorf("unsupported cipher: %v", v.Cipher)
}
mac, err := hex.DecodeString(v.MAC)
fmt.Println("hex decode mac: %s", mac)
if err != nil {
return nil, fmt.Errorf("hex decode mac: %s", err)
}
cipherText, err := hex.DecodeString(v.CipherText)
fmt.Println("cipherText decode mac: %s", cipherText)
if err != nil {
return nil, fmt.Errorf("hex decode cipher text: %s", err)
}
derivedKey, err := getKDFKey(v, []byte(password))
fmt.Println("derivedKey decode mac: %s", derivedKey)
if err != nil {
return nil, err
}
calculatedMAC := sha3.Sum256(append(derivedKey[16:32], cipherText...))
fmt.Println("calculatedMAC decode mac: %s", calculatedMAC)
if !bytes.Equal(calculatedMAC[:], mac) {
return nil, keystore.ErrInvalidPassword
}
iv, err := hex.DecodeString(v.CipherParams.IV)
fmt.Println("iv decode mac: %s", iv)
if err != nil {
return nil, fmt.Errorf("hex decode IV cipher parameter: %s", err)
}
data, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
fmt.Println("data decode mac: %s", data)
if err != nil {
return nil, err
}
return data, nil
}
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
aesBlock, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
stream := cipher.NewCTR(aesBlock, iv)
outText := make([]byte, len(inText))
stream.XORKeyStream(outText, inText)
return outText, nil
}
func getKDFKey(v keyCripto, password []byte) ([]byte, error) {
if v.KDF != keyHeaderKDF {
return nil, fmt.Errorf("unsupported KDF: %s", v.KDF)
}
salt, err := hex.DecodeString(v.KDFParams.Salt)
if err != nil {
return nil, fmt.Errorf("hex decode salt: %s", err)
}
return scrypt.Key(
password,
salt,
v.KDFParams.N,
v.KDFParams.R,
v.KDFParams.P,
v.KDFParams.DKLen,
)
}