-
Notifications
You must be signed in to change notification settings - Fork 14
/
chaincode_sample.go
180 lines (144 loc) · 4.67 KB
/
chaincode_sample.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
/*
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 main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// cryptoChaincode is allows the following transactions
// "put", "key", val - returns "OK" on success
// "get", "key" - returns val stored previously
type cryptoChaincode struct {
}
const (
AESKeyLength = 32 // AESKeyLength is the default AES key length
NonceSize = 24 // NonceSize is the default NonceSize
)
///////////////////////////////////////////////////
// GetRandomByt es returns len random looking bytes
///////////////////////////////////////////////////
func GetRandomBytes(len int) ([]byte, error) {
//TODO: Should we fix the length ?
key := make([]byte, len)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
////////////////////////////////////////////////////////////
// GenAESKey returns a random AES key of length AESKeyLength
// 3 Functions to support Encryption and Decryption
// GENAESKey() - Generates AES symmetric key
func (t *cryptoChaincode) GenAESKey() ([]byte, error) {
return GetRandomBytes(AESKeyLength)
}
//Init implements chaincode's Init interface
func (t *cryptoChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
//Invoke implements chaincode's Invoke interface
func (t *cryptoChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function != "invoke" {
return shim.Error("Unknown function call")
}
if len(args) < 2 {
return shim.Error(fmt.Sprintf("invalid number of args %d", len(args)))
}
method := args[0]
if method == "put" {
if len(args) < 3 {
return shim.Error(fmt.Sprintf("invalid number of args for put %d", len(args)))
}
return t.writeTransaction(stub, args)
} else if method == "get" {
return t.readTransaction(stub, args)
}
return shim.Error(fmt.Sprintf("unknown function %s", method))
}
func (t *cryptoChaincode) encryptAndDecrypt(arg string) []byte {
AES_key, _ := t.GenAESKey()
AES_enc := t.Encrypt(AES_key, []byte(arg))
value := t.Decrypt(AES_key, AES_enc)
return value
}
func (t *cryptoChaincode) Encrypt(key []byte, byteArray []byte) []byte {
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + byteArray length
// Include the IV at the beginning
ciphertext := make([]byte, aes.BlockSize+len(byteArray))
// Slice of first 16 bytes
iv := ciphertext[:aes.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCFBEncrypter(block, iv)
// Encrypt bytes from byteArray to ciphertext
stream.XORKeyStream(ciphertext[aes.BlockSize:], byteArray)
return ciphertext
}
func (t *cryptoChaincode) Decrypt(key []byte, ciphertext []byte) []byte {
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Before even testing the decryption,
// if the text is too small, then it is incorrect
if len(ciphertext) < aes.BlockSize {
panic("Text is too short")
}
// Get the 16 byte IV
iv := ciphertext[:aes.BlockSize]
// Remove the IV from the ciphertext
ciphertext = ciphertext[aes.BlockSize:]
// Return a decrypted stream
stream := cipher.NewCFBDecrypter(block, iv)
// Decrypt bytes from ciphertext
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext
}
func (t *cryptoChaincode) writeTransaction(stub shim.ChaincodeStubInterface, args []string) pb.Response {
cryptoArg := t.encryptAndDecrypt(args[2])
err := stub.PutState(args[1], cryptoArg)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success([]byte("OK"))
}
func (t *cryptoChaincode) readTransaction(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Get the state from the ledger
val, err := stub.GetState(args[1])
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(val)
}
func main() {
err := shim.Start(new(cryptoChaincode))
if err != nil {
fmt.Printf("Error starting New key per invoke: %s", err)
}
}