/
crypto.go
142 lines (119 loc) · 3.63 KB
/
crypto.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
package component
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
crypto2 "github.com/drep-project/DREP-Chain/crypto"
"github.com/drep-project/DREP-Chain/crypto/secp256k1"
"github.com/drep-project/DREP-Chain/crypto/sha3"
"github.com/drep-project/DREP-Chain/types"
"github.com/drep-project/binary"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/scrypt"
"io"
)
type CryptedNode struct {
Version int `json:"version"`
Data []byte `json:"-"`
CipherText []byte `json:"cipherText"`
ChainId types.ChainIdType `json:"chainId"`
ChainCode []byte `json:"chainCode"`
Cipher string `json:"cipher"`
CipherParams CipherParams `json:"cipherParams"`
KDFParams ScryptParams `json:"KDFParams"`
MAC []byte `json:"mac"`
}
type CipherParams struct {
IV []byte `json:"iv"`
}
type ScryptParams struct {
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
Dklen int `json:"dklen"`
Salt []byte `json:"salt"`
}
// Encryptdata encrypts the data given as 'data' with the password 'auth'.
func (node *CryptedNode) EncryptData(auth []byte) error {
salt := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
panic("reading from crypto/rand failed: " + err.Error())
}
derivedKey, err := scrypt.Key(auth, salt, node.KDFParams.N, node.KDFParams.R, node.KDFParams.P, node.KDFParams.Dklen)
if err != nil {
return err
}
encryptKey := derivedKey[:16]
iv := make([]byte, aes.BlockSize) // 16
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic("reading from crypto/rand failed: " + err.Error())
}
cipherText, err := aesCTRXOR(encryptKey, node.Data, iv)
if err != nil {
return err
}
mac := sha3.Keccak256(derivedKey[16:32], cipherText)
node.CipherParams.IV = iv
node.CipherText = cipherText
node.MAC = mac
node.KDFParams.Salt = salt
return nil
}
func DecryptData(cryptoNode CryptedNode, auth string) ([]byte, error) {
if cryptoNode.Cipher != "aes-128-ctr" {
return nil, fmt.Errorf("Cipher not supported: %v", cryptoNode.Cipher)
}
derivedKey, err := getKDFKey(cryptoNode, auth)
if err != nil {
return nil, err
}
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cryptoNode.CipherText)
if !bytes.Equal(calculatedMAC, cryptoNode.MAC) {
return nil, ErrDecrypt
}
plainText, err := aesCTRXOR(derivedKey[:16], cryptoNode.CipherText, cryptoNode.CipherParams.IV)
if err != nil {
return nil, err
}
return plainText, err
}
func getKDFKey(cryptoNode CryptedNode, auth string) ([]byte, error) {
authArray := []byte(auth)
return scrypt.Key(authArray, cryptoNode.KDFParams.Salt, cryptoNode.KDFParams.N, cryptoNode.KDFParams.R, cryptoNode.KDFParams.P, cryptoNode.KDFParams.Dklen)
}
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
}
// BytesToCryptoNode cocnvert given bytes and password to a node
func BytesToCryptoNode(data []byte, auth string) (node *types.Node, errRef error) {
defer func() {
if err := recover(); err != nil {
errRef = ErrDecryptFail
}
}()
cryptoNode := new(CryptedNode)
err := binary.Unmarshal(data, cryptoNode)
if err != nil {
return nil, err
}
privD, errRef := DecryptData(*cryptoNode, auth)
priv, pub := secp256k1.PrivKeyFromScalar(privD)
addr := crypto2.PubkeyToAddress(pub)
node = &types.Node{
Address: &addr,
PrivateKey: priv,
ChainId: cryptoNode.ChainId,
ChainCode: cryptoNode.ChainCode,
}
return
}