-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_state.go
320 lines (251 loc) · 8.79 KB
/
validator_state.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package crypto
import (
"errors"
"reflect"
"crypto/aes"
"crypto/cipher"
"encoding/asn1"
"encoding/binary"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/core/crypto/utils"
obc "github.com/hyperledger/fabric/protos"
)
func (validator *validatorImpl) GetStateEncryptor(deployTx, executeTx *obc.Transaction) (StateEncryptor, error) {
switch executeTx.ConfidentialityProtocolVersion {
case "1.2":
return validator.getStateEncryptor1_2(deployTx, executeTx)
}
return nil, utils.ErrInvalidConfidentialityLevel
}
func (validator *validatorImpl) getStateEncryptor1_2(deployTx, executeTx *obc.Transaction) (StateEncryptor, error) {
// Check nonce
if deployTx.Nonce == nil || len(deployTx.Nonce) == 0 {
return nil, errors.New("Invalid deploy nonce.")
}
if executeTx.Nonce == nil || len(executeTx.Nonce) == 0 {
return nil, errors.New("Invalid invoke nonce.")
}
// Check ChaincodeID
if deployTx.ChaincodeID == nil {
return nil, errors.New("Invalid deploy chaincodeID.")
}
if executeTx.ChaincodeID == nil {
return nil, errors.New("Invalid execute chaincodeID.")
}
// Check that deployTx and executeTx refers to the same chaincode
if !reflect.DeepEqual(deployTx.ChaincodeID, executeTx.ChaincodeID) {
return nil, utils.ErrDifferentChaincodeID
}
// Check the confidentiality protocol version
if deployTx.ConfidentialityProtocolVersion != executeTx.ConfidentialityProtocolVersion {
return nil, utils.ErrDifferrentConfidentialityProtocolVersion
}
validator.Debugf("Parsing transaction. Type [%s]. Confidentiality Protocol Version [%s]", executeTx.Type.String(), executeTx.ConfidentialityProtocolVersion)
deployStateKey, err := validator.getStateKeyFromTransaction(deployTx)
if executeTx.Type == obc.Transaction_CHAINCODE_QUERY {
validator.Debug("Parsing Query transaction...")
executeStateKey, err := validator.getStateKeyFromTransaction(executeTx)
// Compute deployTxKey key from the deploy transaction. This is used to decrypt the actual state
// of the chaincode
deployTxKey := primitives.HMAC(deployStateKey, deployTx.Nonce)
// Compute the key used to encrypt the result of the query
//queryKey := utils.HMACTruncated(executeStateKey, append([]byte{6}, executeTx.Nonce...), utils.AESKeyLength)
// Init the state encryptor
se := queryStateEncryptor{}
err = se.init(validator.nodeImpl, executeStateKey, deployTxKey)
if err != nil {
return nil, err
}
return &se, nil
}
// Compute deployTxKey key from the deploy transaction
deployTxKey := primitives.HMAC(deployStateKey, deployTx.Nonce)
// Mask executeTx.Nonce
executeTxNonce := primitives.HMACTruncated(deployTxKey, primitives.Hash(executeTx.Nonce), primitives.NonceSize)
// Compute stateKey to encrypt the states and nonceStateKey to generates IVs. This
// allows validators to reach consesus
stateKey := primitives.HMACTruncated(deployTxKey, append([]byte{3}, executeTxNonce...), primitives.AESKeyLength)
nonceStateKey := primitives.HMAC(deployTxKey, append([]byte{4}, executeTxNonce...))
// Init the state encryptor
se := stateEncryptorImpl{}
err = se.init(validator.nodeImpl, stateKey, nonceStateKey, deployTxKey, executeTxNonce)
if err != nil {
return nil, err
}
return &se, nil
}
func (validator *validatorImpl) getStateKeyFromTransaction(tx *obc.Transaction) ([]byte, error) {
cipher, err := validator.eciesSPI.NewAsymmetricCipherFromPrivateKey(validator.chainPrivateKey)
if err != nil {
validator.Errorf("Failed init decryption engine [%s].", err.Error())
return nil, err
}
msgToValidatorsRaw, err := cipher.Process(tx.ToValidators)
if err != nil {
validator.Errorf("Failed decrypting message to validators [% x]: [%s].", tx.ToValidators, err.Error())
return nil, err
}
msgToValidators := new(chainCodeValidatorMessage1_2)
_, err = asn1.Unmarshal(msgToValidatorsRaw, msgToValidators)
if err != nil {
validator.Errorf("Failed unmarshalling message to validators [% x]: [%s].", msgToValidators, err.Error())
return nil, err
}
return msgToValidators.StateKey, nil
}
type stateEncryptorImpl struct {
node *nodeImpl
deployTxKey []byte
invokeTxNonce []byte
stateKey []byte
nonceStateKey []byte
gcmEnc cipher.AEAD
nonceSize int
counter uint64
}
func (se *stateEncryptorImpl) init(node *nodeImpl, stateKey, nonceStateKey, deployTxKey, invokeTxNonce []byte) error {
// Initi fields
se.counter = 0
se.node = node
se.stateKey = stateKey
se.nonceStateKey = nonceStateKey
se.deployTxKey = deployTxKey
se.invokeTxNonce = invokeTxNonce
// Init aes
c, err := aes.NewCipher(se.stateKey)
if err != nil {
return err
}
// Init gcm for encryption
se.gcmEnc, err = cipher.NewGCM(c)
if err != nil {
return err
}
// Init nonce size
se.nonceSize = se.gcmEnc.NonceSize()
return nil
}
func (se *stateEncryptorImpl) Encrypt(msg []byte) ([]byte, error) {
var b = make([]byte, 8)
binary.BigEndian.PutUint64(b, se.counter)
se.node.Debugf("Encrypting with counter [% x].", b)
// se.log.Infof("Encrypting with txNonce ", utils.EncodeBase64(se.txNonce))
nonce := primitives.HMACTruncated(se.nonceStateKey, b, se.nonceSize)
se.counter++
// Seal will append the output to the first argument; the usage
// here appends the ciphertext to the nonce. The final parameter
// is any additional data to be authenticated.
out := se.gcmEnc.Seal(nonce, nonce, msg, se.invokeTxNonce)
return append(se.invokeTxNonce, out...), nil
}
func (se *stateEncryptorImpl) Decrypt(raw []byte) ([]byte, error) {
if len(raw) == 0 {
// A nil ciphertext decrypts to nil
return nil, nil
}
if len(raw) <= primitives.NonceSize {
return nil, utils.ErrDecrypt
}
// raw consists of (txNonce, ct)
txNonce := raw[:primitives.NonceSize]
// se.log.Infof("Decrypting with txNonce ", utils.EncodeBase64(txNonce))
ct := raw[primitives.NonceSize:]
nonce := make([]byte, se.nonceSize)
copy(nonce, ct)
key := primitives.HMACTruncated(se.deployTxKey, append([]byte{3}, txNonce...), primitives.AESKeyLength)
// se.log.Infof("Decrypting with key ", utils.EncodeBase64(key))
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
se.nonceSize = se.gcmEnc.NonceSize()
out, err := gcm.Open(nil, nonce, ct[se.nonceSize:], txNonce)
if err != nil {
return nil, utils.ErrDecrypt
}
return out, nil
}
type queryStateEncryptor struct {
node *nodeImpl
deployTxKey []byte
gcmEnc cipher.AEAD
nonceSize int
}
func (se *queryStateEncryptor) init(node *nodeImpl, queryKey, deployTxKey []byte) error {
// Initi fields
se.node = node
se.deployTxKey = deployTxKey
// se.log.Infof("QUERY Encrypting with key ", utils.EncodeBase64(queryKey))
// Init aes
c, err := aes.NewCipher(queryKey)
if err != nil {
return err
}
// Init gcm for encryption
se.gcmEnc, err = cipher.NewGCM(c)
if err != nil {
return err
}
// Init nonce size
se.nonceSize = se.gcmEnc.NonceSize()
return nil
}
func (se *queryStateEncryptor) Encrypt(msg []byte) ([]byte, error) {
nonce, err := primitives.GetRandomBytes(se.nonceSize)
if err != nil {
se.node.Errorf("Failed getting randomness [%s].", err.Error())
return nil, err
}
// Seal will append the output to the first argument; the usage
// here appends the ciphertext to the nonce. The final parameter
// is any additional data to be authenticated.
out := se.gcmEnc.Seal(nonce, nonce, msg, nil)
return out, nil
}
func (se *queryStateEncryptor) Decrypt(raw []byte) ([]byte, error) {
if len(raw) == 0 {
// A nil ciphertext decrypts to nil
return nil, nil
}
if len(raw) <= primitives.NonceSize {
return nil, utils.ErrDecrypt
}
// raw consists of (txNonce, ct)
txNonce := raw[:primitives.NonceSize]
// se.log.Infof("Decrypting with txNonce ", utils.EncodeBase64(txNonce))
ct := raw[primitives.NonceSize:]
nonce := make([]byte, se.nonceSize)
copy(nonce, ct)
key := primitives.HMACTruncated(se.deployTxKey, append([]byte{3}, txNonce...), primitives.AESKeyLength)
// se.log.Infof("Decrypting with key ", utils.EncodeBase64(key))
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
se.nonceSize = se.gcmEnc.NonceSize()
out, err := gcm.Open(nil, nonce, ct[se.nonceSize:], txNonce)
if err != nil {
return nil, utils.ErrDecrypt
}
return out, nil
}