@@ -16,6 +16,8 @@ import (
16
16
pb "github.com/hyperledger/fabric/protos/peer"
17
17
)
18
18
19
+ const DECKEY = "DECKEY"
20
+ const VERKEY = "VERKEY"
19
21
const ENCKEY = "ENCKEY"
20
22
const SIGKEY = "SIGKEY"
21
23
const IV = "IV"
@@ -30,101 +32,112 @@ func (t *EncCC) Init(stub shim.ChaincodeStubInterface) pb.Response {
30
32
return shim .Success (nil )
31
33
}
32
34
33
- // Encrypter exposes two functions: "PUT" that shows how to write state to the ledger after having
35
+ // Encrypter exposes how to write state to the ledger after having
34
36
// encrypted it with an AES 256 bit key that has been provided to the chaincode through the
35
- // transient field; and "GET" that shows how to read from the ledger and decrypt using an AES 256
36
- // bit key that has been provided to the chaincode through the transient field.
37
- func (t * EncCC ) Encrypter (stub shim.ChaincodeStubInterface , f string , args []string , encKey , IV []byte ) pb.Response {
37
+ // transient field
38
+ func (t * EncCC ) Encrypter (stub shim.ChaincodeStubInterface , args []string , encKey , IV []byte ) pb.Response {
38
39
// create the encrypter entity - we give it an ID, the bccsp instance, the key and (optionally) the IV
39
40
ent , err := entities .NewAES256EncrypterEntity ("ID" , t .bccspInst , encKey , IV )
40
41
if err != nil {
41
42
return shim .Error (fmt .Sprintf ("entities.NewAES256EncrypterEntity failed, err %s" , err ))
42
43
}
43
44
44
- switch f {
45
- case "PUT" :
46
- if len (args ) != 2 {
47
- return shim .Error ("Expected 2 parameters to PUT" )
48
- }
45
+ if len (args ) != 2 {
46
+ return shim .Error ("Expected 2 parameters to function Encrypter" )
47
+ }
49
48
50
- key := args [0 ]
51
- cleartextValue := []byte (args [1 ])
49
+ key := args [0 ]
50
+ cleartextValue := []byte (args [1 ])
52
51
53
- // here, we encrypt cleartextValue and assign it to key
54
- err = encryptAndPutState (stub , ent , key , cleartextValue )
55
- if err != nil {
56
- return shim .Error (fmt .Sprintf ("encryptAndPutState failed, err %+v" , err ))
57
- }
52
+ // here, we encrypt cleartextValue and assign it to key
53
+ err = encryptAndPutState (stub , ent , key , cleartextValue )
54
+ if err != nil {
55
+ return shim .Error (fmt .Sprintf ("encryptAndPutState failed, err %+v" , err ))
56
+ }
57
+ return shim .Success (nil )
58
+ }
58
59
59
- return shim .Success (nil )
60
- case "GET" :
61
- if len (args ) != 1 {
62
- return shim .Error ("Expected 1 parameters to GET" )
63
- }
60
+ // Decrypter exposes how to read from the ledger and decrypt using an AES 256
61
+ // bit key that has been provided to the chaincode through the transient field.
62
+ func (t * EncCC ) Decrypter (stub shim.ChaincodeStubInterface , args []string , decKey , IV []byte ) pb.Response {
63
+ // create the encrypter entity - we give it an ID, the bccsp instance, the key and (optionally) the IV
64
+ ent , err := entities .NewAES256EncrypterEntity ("ID" , t .bccspInst , decKey , IV )
65
+ if err != nil {
66
+ return shim .Error (fmt .Sprintf ("entities.NewAES256EncrypterEntity failed, err %s" , err ))
67
+ }
64
68
65
- key := args [0 ]
69
+ if len (args ) != 1 {
70
+ return shim .Error ("Expected 1 parameters to function Decrypter" )
71
+ }
66
72
67
- // here we decrypt the state associated to key
68
- cleartextValue , err := getStateAndDecrypt (stub , ent , key )
69
- if err != nil {
70
- return shim .Error (fmt .Sprintf ("getStateAndDecrypt failed, err %+v" , err ))
71
- }
73
+ key := args [0 ]
72
74
73
- // here we return the decrypted value as a result
74
- return shim . Success ( cleartextValue )
75
- default :
76
- return shim .Error (fmt .Sprintf ("Unsupported function %s " , f ))
75
+ // here we decrypt the state associated to key
76
+ cleartextValue , err := getStateAndDecrypt ( stub , ent , key )
77
+ if err != nil {
78
+ return shim .Error (fmt .Sprintf ("getStateAndDecrypt failed, err %+v " , err ))
77
79
}
80
+
81
+ // here we return the decrypted value as a result
82
+ return shim .Success (cleartextValue )
78
83
}
79
84
80
- func (t * EncCC ) EncrypterSigner (stub shim.ChaincodeStubInterface , f string , args []string , encKey , sigKey []byte ) pb.Response {
85
+ // EncrypterSigner exposes how to write state to the ledger after having received keys for
86
+ // encrypting (AES 256 bit key) and signing (X9.62/SECG curve over a 256 bit prime field) that has been provided to the chaincode through the
87
+ // transient field
88
+ func (t * EncCC ) EncrypterSigner (stub shim.ChaincodeStubInterface , args []string , encKey , sigKey []byte ) pb.Response {
81
89
// create the encrypter/signer entity - we give it an ID, the bccsp instance and the keys
82
90
ent , err := entities .NewAES256EncrypterECDSASignerEntity ("ID" , t .bccspInst , encKey , sigKey )
83
91
if err != nil {
84
- return shim .Error (fmt .Sprintf ("entities.NewAES256EncrypterEntity failed, err %+v " , err ))
92
+ return shim .Error (fmt .Sprintf ("entities.NewAES256EncrypterEntity failed, err %s " , err ))
85
93
}
86
94
87
- switch f {
88
- case "PUT" :
89
- if len (args ) != 2 {
90
- return shim .Error ("Expected 2 parameters to PUT" )
91
- }
95
+ if len (args ) != 2 {
96
+ return shim .Error ("Expected 2 parameters to function EncrypterSigner" )
97
+ }
92
98
93
- key := args [0 ]
94
- cleartextValue := []byte (args [1 ])
99
+ key := args [0 ]
100
+ cleartextValue := []byte (args [1 ])
95
101
96
- // here, we sign cleartextValue, encrypt it and assign it to key
97
- err = signEncryptAndPutState (stub , ent , key , cleartextValue )
98
- if err != nil {
99
- return shim .Error (fmt .Sprintf ("signEncryptAndPutState failed, err %+v" , err ))
100
- }
102
+ // here, we sign cleartextValue, encrypt it and assign it to key
103
+ err = signEncryptAndPutState (stub , ent , key , cleartextValue )
104
+ if err != nil {
105
+ return shim .Error (fmt .Sprintf ("signEncryptAndPutState failed, err %+v" , err ))
106
+ }
101
107
102
- return shim .Success (nil )
103
- case "GET" :
104
- if len (args ) != 1 {
105
- return shim .Error ("Expected 1 parameters to GET" )
106
- }
108
+ return shim .Success (nil )
109
+ }
107
110
108
- key := args [0 ]
111
+ // DecrypterVerify exposes how to get state to the ledger after having received keys for
112
+ // decrypting (AES 256 bit key) and verifying (X9.62/SECG curve over a 256 bit prime field) that has been provided to the chaincode through the
113
+ // transient field
114
+ func (t * EncCC ) DecrypterVerify (stub shim.ChaincodeStubInterface , args []string , decKey , verKey []byte ) pb.Response {
115
+ // create the decrypter/verify entity - we give it an ID, the bccsp instance and the keys
116
+ ent , err := entities .NewAES256EncrypterECDSASignerEntity ("ID" , t .bccspInst , decKey , verKey )
117
+ if err != nil {
118
+ return shim .Error (fmt .Sprintf ("entities.NewAES256DecrypterEntity failed, err %s" , err ))
119
+ }
109
120
110
- // here we decrypt the state associated to key and verify it
111
- cleartextValue , err := getStateDecryptAndVerify (stub , ent , key )
112
- if err != nil {
113
- return shim .Error (fmt .Sprintf ("getStateDecryptAndVerify failed, err %+v" , err ))
114
- }
121
+ if len (args ) != 1 {
122
+ return shim .Error ("Expected 1 parameters to function DecrypterVerify" )
123
+ }
124
+ key := args [0 ]
115
125
116
- // here we return the decrypted and verified value as a result
117
- return shim . Success ( cleartextValue )
118
- default :
119
- return shim .Error (fmt .Sprintf ("Unsupported function %s " , f ))
126
+ // here we decrypt the state associated to key and verify it
127
+ cleartextValue , err := getStateDecryptAndVerify ( stub , ent , key )
128
+ if err != nil {
129
+ return shim .Error (fmt .Sprintf ("getStateDecryptAndVerify failed, err %+v " , err ))
120
130
}
131
+
132
+ // here we return the decrypted and verified value as a result
133
+ return shim .Success (cleartextValue )
121
134
}
122
135
123
- // RangeDecrypter shows how range queries may be satisfied by using the encrypter
136
+ // RangeDecrypter shows how range queries may be satisfied by using the decrypter
124
137
// entity directly to decrypt previously encrypted key-value pairs
125
- func (t * EncCC ) RangeDecrypter (stub shim.ChaincodeStubInterface , encKey []byte ) pb.Response {
138
+ func (t * EncCC ) RangeDecrypter (stub shim.ChaincodeStubInterface , decKey []byte ) pb.Response {
126
139
// create the encrypter entity - we give it an ID, the bccsp instance and the key
127
- ent , err := entities .NewAES256EncrypterEntity ("ID" , t .bccspInst , encKey , nil )
140
+ ent , err := entities .NewAES256EncrypterEntity ("ID" , t .bccspInst , decKey , nil )
128
141
if err != nil {
129
142
return shim .Error (fmt .Sprintf ("entities.NewAES256EncrypterEntity failed, err %s" , err ))
130
143
}
@@ -137,9 +150,10 @@ func (t *EncCC) RangeDecrypter(stub shim.ChaincodeStubInterface, encKey []byte)
137
150
return shim .Success (bytes )
138
151
}
139
152
140
- // Invoke for this chaincode exposes two functions: "ENC" to demonstrate
141
- // the use of an Entity that can encrypt, and "SIG" to demonstrate the use
142
- // of an Entity that can encrypt and sign
153
+ // Invoke for this chaincode exposes functions to ENCRYPT, DECRYPT transactional
154
+ // data. It also supports an example to ENCRYPT and SIGN and DECRYPT and
155
+ // VERIFY. The Initialization Vector (IV) can be passed in as a parm to
156
+ // ensure peers have deterministic data.
143
157
func (t * EncCC ) Invoke (stub shim.ChaincodeStubInterface ) pb.Response {
144
158
// get arguments and transient
145
159
f , args := stub .GetFunctionAndParameters ()
@@ -149,15 +163,24 @@ func (t *EncCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
149
163
}
150
164
151
165
switch f {
152
- case "ENC " :
166
+ case "ENCRYPT " :
153
167
// make sure there's a key in transient - the assumption is that
154
168
// it's associated to the string "ENCKEY"
155
169
if _ , in := tMap [ENCKEY ]; ! in {
156
- return shim .Error (fmt .Sprintf ("Expected transient key %s" , ENCKEY ))
170
+ return shim .Error (fmt .Sprintf ("Expected transient encryption key %s" , ENCKEY ))
171
+ }
172
+
173
+ return t .Encrypter (stub , args [0 :], tMap [ENCKEY ], tMap [IV ])
174
+ case "DECRYPT" :
175
+
176
+ // make sure there's a key in transient - the assumption is that
177
+ // it's associated to the string "DECKEY"
178
+ if _ , in := tMap [DECKEY ]; ! in {
179
+ return shim .Error (fmt .Sprintf ("Expected transient decryption key %s" , DECKEY ))
157
180
}
158
181
159
- return t .Encrypter (stub , args [0 ], args [ 1 :], tMap [ENCKEY ], tMap [IV ])
160
- case "SIG " :
182
+ return t .Decrypter (stub , args [0 :], tMap [DECKEY ], tMap [IV ])
183
+ case "ENCRYPTSIGN " :
161
184
// make sure keys are there in the transient map - the assumption is that they
162
185
// are associated to the string "ENCKEY" and "SIGKEY"
163
186
if _ , in := tMap [ENCKEY ]; ! in {
@@ -166,15 +189,25 @@ func (t *EncCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
166
189
return shim .Error (fmt .Sprintf ("Expected transient key %s" , SIGKEY ))
167
190
}
168
191
169
- return t .EncrypterSigner (stub , args [0 ], args [1 :], tMap [ENCKEY ], tMap [SIGKEY ])
170
- case "RANGE" :
192
+ return t .EncrypterSigner (stub , args [0 :], tMap [ENCKEY ], tMap [SIGKEY ])
193
+ case "DECRYPTVERIFY" :
194
+ // make sure keys are there in the transient map - the assumption is that they
195
+ // are associated to the string "DECKEY" and "VERKEY"
196
+ if _ , in := tMap [DECKEY ]; ! in {
197
+ return shim .Error (fmt .Sprintf ("Expected transient key %s" , DECKEY ))
198
+ } else if _ , in := tMap [VERKEY ]; ! in {
199
+ return shim .Error (fmt .Sprintf ("Expected transient key %s" , VERKEY ))
200
+ }
201
+
202
+ return t .DecrypterVerify (stub , args [0 :], tMap [DECKEY ], tMap [VERKEY ])
203
+ case "RANGEQUERY" :
171
204
// make sure there's a key in transient - the assumption is that
172
205
// it's associated to the string "ENCKEY"
173
- if _ , in := tMap [ENCKEY ]; ! in {
174
- return shim .Error (fmt .Sprintf ("Expected transient key %s" , ENCKEY ))
206
+ if _ , in := tMap [DECKEY ]; ! in {
207
+ return shim .Error (fmt .Sprintf ("Expected transient key %s" , DECKEY ))
175
208
}
176
209
177
- return t .RangeDecrypter (stub , tMap [ENCKEY ])
210
+ return t .RangeDecrypter (stub , tMap [DECKEY ])
178
211
default :
179
212
return shim .Error (fmt .Sprintf ("Unsupported function %s" , f ))
180
213
}
0 commit comments