-
Notifications
You must be signed in to change notification settings - Fork 127
/
keystore.go
127 lines (110 loc) · 3.18 KB
/
keystore.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
package keystore
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"golang.org/x/crypto/pbkdf2"
)
var (
errDecrypt = errors.New("could not decrypt key with given passphrase")
)
// PlainKeyJSON define a struct TODO
type PlainKeyJSON struct {
Address string `json:"address"`
PrivateKey string `json:"privatekey"`
ID string `json:"id"`
Version int `json:"version"`
}
// EncryptedKeyJSON define a struct TODO
type EncryptedKeyJSON struct {
Address string `json:"address"`
Crypto CryptoJSON `json:"crypto"`
ID string `json:"id"`
Version string `json:"version"`
}
// CryptoJSON define a struct TODO
type CryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
}
type cipherparamsJSON struct {
IV string `json:"iv"`
}
func decryptKey(keyProtected *EncryptedKeyJSON, auth string) ([]byte, error) {
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
if err != nil {
return nil, err
}
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
if err != nil {
return nil, err
}
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
if err != nil {
return nil, err
}
derivedKey, err := getKDFKey(keyProtected.Crypto, auth)
if err != nil {
return nil, err
}
bufferValue := make([]byte, len(cipherText)+16)
copy(bufferValue[0:16], derivedKey[16:32])
copy(bufferValue[16:], cipherText[:])
calculatedMAC := sha256.Sum256((bufferValue))
if !bytes.Equal(calculatedMAC[:], mac) {
return nil, errDecrypt
}
plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
if err != nil {
return nil, err
}
return plainText, err
}
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
authArray := []byte(auth)
if cryptoJSON.KDFParams["salt"] == nil || cryptoJSON.KDFParams["dklen"] == nil ||
cryptoJSON.KDFParams["c"] == nil || cryptoJSON.KDFParams["prf"] == nil {
return nil, errors.New("invalid KDF params, must contains c, dklen, prf and salt")
}
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
if err != nil {
return nil, err
}
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
if cryptoJSON.KDF == "pbkdf2" {
c := ensureInt(cryptoJSON.KDFParams["c"])
prf := cryptoJSON.KDFParams["prf"].(string)
if prf != "hmac-sha256" {
return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
}
key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New)
return key, nil
}
return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF)
}
func ensureInt(x interface{}) int {
res, ok := x.(int)
if !ok {
res = int(x.(float64))
}
return res
}
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
// AES-128 is selected due to size of encryptKey.
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, err
}