/
key.go
83 lines (73 loc) · 1.98 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
package main
import (
"crypto/sha256"
"encoding/hex"
"gitlab.com/thorchain/binance-sdk/common/uuid"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/sha3"
)
type cipherParams struct {
IV string `json:"iv"`
}
type CryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherParams `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
}
type EncryptedKey struct {
Crypto CryptoJSON `json:"crypto"`
Id string `json:"id"`
Version int `json:"version"`
}
func exportKeyStore(privKey []byte, password string) (*EncryptedKey, error) {
salt, err := generateRandomBytes(32)
if err != nil {
return nil, err
}
iv, err := generateRandomBytes(16)
if err != nil {
return nil, err
}
scryptParamsJSON := make(map[string]interface{}, 4)
scryptParamsJSON["prf"] = "hmac-sha256"
scryptParamsJSON["dklen"] = 32
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
scryptParamsJSON["c"] = 262144
cipherParamsJSON := cipherParams{IV: hex.EncodeToString(iv)}
derivedKey := pbkdf2.Key([]byte(password), salt, 262144, 32, sha256.New)
encryptKey := derivedKey[:32]
cipherText, err := aesCTRXOR(encryptKey, privKey, iv)
if err != nil {
return nil, err
}
hasher := sha3.NewLegacyKeccak512()
_, err = hasher.Write(derivedKey[16:32])
if err != nil {
return nil, err
}
_, err = hasher.Write(cipherText)
if err != nil {
return nil, err
}
mac := hasher.Sum(nil)
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
cryptoStruct := CryptoJSON{
Cipher: "aes-256-ctr",
CipherText: hex.EncodeToString(cipherText),
CipherParams: cipherParamsJSON,
KDF: "pbkdf2",
KDFParams: scryptParamsJSON,
MAC: hex.EncodeToString(mac),
}
return &EncryptedKey{
Crypto: cryptoStruct,
Id: id.String(),
Version: 1,
}, nil
}