88 "errors"
99 "fmt"
1010 "os"
11+ "sync/atomic"
1112
1213 "github.com/godaddy/cobhan-go"
1314
@@ -19,7 +20,7 @@ import (
1920)
2021
2122var EstimatedIntermediateKeyOverhead = 0
22- var DisableZeroCopy = false
23+ var disableZeroCopy atomic. Bool
2324
2425func main () {
2526}
@@ -41,32 +42,35 @@ type Env map[string]string
4142 https://github.com/golang/go/issues/27693
4243*/
4344//export SetEnv
44- func SetEnv (envJson unsafe.Pointer ) int32 {
45+ func SetEnv (envJson unsafe.Pointer ) ( result int32 ) {
4546 defer func () {
4647 if r := recover (); r != nil {
4748 log .ErrorLogf ("SetEnv: Panic: %v" , r )
48- panic ( r )
49+ result = ERR_PANIC
4950 }
5051 }()
5152
5253 cobhan .AllowTempFileBuffers (false )
5354 env := Env {}
5455
55- result : = cobhan .BufferToJsonStruct (envJson , & env )
56+ result = cobhan .BufferToJsonStruct (envJson , & env )
5657 if result != cobhan .ERR_NONE {
5758 log .ErrorLogf ("Failed to deserialize environment JSON string %v" , cobhan .CobhanErrorToString (result ))
5859 return result
5960 }
6061
6162 for k , v := range env {
62- os .Setenv (k , v )
63+ if err := os .Setenv (k , v ); err != nil {
64+ log .ErrorLogf ("Failed to set environment variable %v: %v" , k , err )
65+ return ERR_BAD_CONFIG
66+ }
6367 }
6468
6569 return cobhan .ERR_NONE
6670}
6771
6872//export SetupJson
69- func SetupJson (configJson unsafe.Pointer ) int32 {
73+ func SetupJson (configJson unsafe.Pointer ) ( result int32 ) {
7074 defer func () {
7175 if r := recover (); r != nil {
7276 log .ErrorLogf ("SetupJson: Panic: %v" , r )
@@ -76,12 +80,12 @@ func SetupJson(configJson unsafe.Pointer) int32 {
7680
7781 cobhan .AllowTempFileBuffers (false )
7882 options := & asherah.Options {}
79- result : = cobhan .BufferToJsonStruct (configJson , options )
83+ result = cobhan .BufferToJsonStruct (configJson , options )
8084 if result != cobhan .ERR_NONE {
8185 log .ErrorLogf ("Failed to deserialize configuration string %v" , cobhan .CobhanErrorToString (result ))
8286 configString , stringResult := cobhan .BufferToString (configJson )
8387 if stringResult != cobhan .ERR_NONE {
84- log .ErrorLogf ("Could not convert configJson to string: %v" , cobhan .CobhanErrorToString (result ))
88+ log .ErrorLogf ("Could not convert configJson to string: %v" , cobhan .CobhanErrorToString (stringResult ))
8589 return result
8690 }
8791 log .ErrorLogf ("Could not deserialize: %v" , configString )
@@ -93,7 +97,7 @@ func SetupJson(configJson unsafe.Pointer) int32 {
9397 log .DebugLog ("Successfully deserialized config JSON" )
9498
9599 EstimatedIntermediateKeyOverhead = len (options .ProductID ) + len (options .ServiceName )
96- DisableZeroCopy = options .DisableZeroCopy
100+ disableZeroCopy . Store ( options .DisableZeroCopy )
97101
98102 err := asherah .Setup (options )
99103 if err == asherah .ErrAsherahAlreadyInitialized {
@@ -125,27 +129,30 @@ func EstimateBufferInt(dataLen int, partitionLen int) int {
125129
126130//export Decrypt
127131func Decrypt (partitionIdPtr unsafe.Pointer , encryptedDataPtr unsafe.Pointer , encryptedKeyPtr unsafe.Pointer ,
128- created int64 , parentKeyIdPtr unsafe.Pointer , parentKeyCreated int64 , outputDecryptedDataPtr unsafe.Pointer ) int32 {
132+ created int64 , parentKeyIdPtr unsafe.Pointer , parentKeyCreated int64 , outputDecryptedDataPtr unsafe.Pointer ) ( result int32 ) {
129133 defer func () {
130134 if r := recover (); r != nil {
131135 log .ErrorLogf ("Decrypt: Panic: %v" , r )
132- panic ( r )
136+ result = ERR_PANIC
133137 }
134138 }()
135139
136- encryptedData , result := cobhan .BufferToBytes (encryptedDataPtr )
140+ var encryptedData []byte
141+ encryptedData , result = cobhan .BufferToBytes (encryptedDataPtr )
137142 if result != cobhan .ERR_NONE {
138143 log .ErrorLogf ("Decrypt failed: Failed to convert encryptedDataPtr cobhan buffer to bytes %v" , cobhan .CobhanErrorToString (result ))
139144 return result
140145 }
141146
142- encryptedKey , result := cobhan .BufferToBytes (encryptedKeyPtr )
147+ var encryptedKey []byte
148+ encryptedKey , result = cobhan .BufferToBytes (encryptedKeyPtr )
143149 if result != cobhan .ERR_NONE {
144150 log .ErrorLogf ("Decrypt failed: Failed to convert encryptedKeyPtr cobhan buffer to bytes %v" , cobhan .CobhanErrorToString (result ))
145151 return result
146152 }
147153
148- parentKeyId , result := cobhan .BufferToString (parentKeyIdPtr )
154+ var parentKeyId string
155+ parentKeyId , result = cobhan .BufferToString (parentKeyIdPtr )
149156 if result != cobhan .ERR_NONE {
150157 log .ErrorLogf ("Decrypt failed: Failed to convert parentKeyIdPtr cobhan buffer to string %v" , cobhan .CobhanErrorToString (result ))
151158 return result
@@ -163,7 +170,9 @@ func Decrypt(partitionIdPtr unsafe.Pointer, encryptedDataPtr unsafe.Pointer, enc
163170 },
164171 }
165172
166- data , result , err := decryptData (partitionIdPtr , & drr )
173+ var data []byte
174+ var err error
175+ data , result , err = decryptData (partitionIdPtr , & drr )
167176 if result != cobhan .ERR_NONE {
168177 log .ErrorLogf ("Failed to decrypt data %v" , cobhan .CobhanErrorToString (result ))
169178 log .ErrorLogf ("Decrypt: decryptData returned %v" , err )
@@ -176,15 +185,17 @@ func Decrypt(partitionIdPtr unsafe.Pointer, encryptedDataPtr unsafe.Pointer, enc
176185//export Encrypt
177186func Encrypt (partitionIdPtr unsafe.Pointer , dataPtr unsafe.Pointer , outputEncryptedDataPtr unsafe.Pointer ,
178187 outputEncryptedKeyPtr unsafe.Pointer , outputCreatedPtr unsafe.Pointer , outputParentKeyIdPtr unsafe.Pointer ,
179- outputParentKeyCreatedPtr unsafe.Pointer ) int32 {
188+ outputParentKeyCreatedPtr unsafe.Pointer ) ( result int32 ) {
180189 defer func () {
181190 if r := recover (); r != nil {
182191 log .ErrorLogf ("Encrypt: Panic: %v" , r )
183- panic ( r )
192+ result = ERR_PANIC
184193 }
185194 }()
186195
187- drr , result , err := encryptData (partitionIdPtr , dataPtr )
196+ var drr * appencryption.DataRowRecord
197+ var err error
198+ drr , result , err = encryptData (partitionIdPtr , dataPtr )
188199 if result != cobhan .ERR_NONE {
189200 log .ErrorLogf ("Failed to encrypt data %v" , cobhan .CobhanErrorToString (result ))
190201 log .ErrorLogf ("Encrypt failed: encryptData returned %v" , err )
@@ -226,15 +237,17 @@ func Encrypt(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, outputEncryp
226237}
227238
228239//export EncryptToJson
229- func EncryptToJson (partitionIdPtr unsafe.Pointer , dataPtr unsafe.Pointer , jsonPtr unsafe.Pointer ) int32 {
240+ func EncryptToJson (partitionIdPtr unsafe.Pointer , dataPtr unsafe.Pointer , jsonPtr unsafe.Pointer ) ( result int32 ) {
230241 defer func () {
231242 if r := recover (); r != nil {
232243 log .ErrorLogf ("EncryptToJson: Panic: %v" , r )
233- panic ( r )
244+ result = ERR_PANIC
234245 }
235246 }()
236247
237- drr , result , err := encryptData (partitionIdPtr , dataPtr )
248+ var drr * appencryption.DataRowRecord
249+ var err error
250+ drr , result , err = encryptData (partitionIdPtr , dataPtr )
238251 if result != cobhan .ERR_NONE {
239252 log .ErrorLogf ("Failed to encrypt data %v" , cobhan .CobhanErrorToString (result ))
240253 log .ErrorLogf ("EncryptToJson failed: encryptData returned %v" , err )
@@ -258,22 +271,24 @@ func EncryptToJson(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, jsonPt
258271}
259272
260273//export DecryptFromJson
261- func DecryptFromJson (partitionIdPtr unsafe.Pointer , jsonPtr unsafe.Pointer , dataPtr unsafe.Pointer ) int32 {
274+ func DecryptFromJson (partitionIdPtr unsafe.Pointer , jsonPtr unsafe.Pointer , dataPtr unsafe.Pointer ) ( result int32 ) {
262275 defer func () {
263276 if r := recover (); r != nil {
264277 log .ErrorLogf ("DecryptFromJson: Panic: %v" , r )
265- panic ( r )
278+ result = ERR_PANIC
266279 }
267280 }()
268281
269282 var drr appencryption.DataRowRecord
270- result : = cobhan .BufferToJsonStruct (jsonPtr , & drr )
283+ result = cobhan .BufferToJsonStruct (jsonPtr , & drr )
271284 if result != cobhan .ERR_NONE {
272285 log .ErrorLogf ("DecryptFromJson failed: Failed to convert cobhan buffer to JSON structs %v" , cobhan .CobhanErrorToString (result ))
273286 return result
274287 }
275288
276- data , result , err := decryptData (partitionIdPtr , & drr )
289+ var data []byte
290+ var err error
291+ data , result , err = decryptData (partitionIdPtr , & drr )
277292 if result != cobhan .ERR_NONE {
278293 log .ErrorLogf ("Failed to decrypt data %v" , cobhan .CobhanErrorToString (result ))
279294 log .ErrorLogf ("DecryptFromJson failed: decryptData returned %v" , err )
@@ -306,7 +321,7 @@ func encryptData(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer) (*appenc
306321 return nil , result , errors .New (errorMessage )
307322 }
308323
309- if DisableZeroCopy {
324+ if disableZeroCopy . Load () {
310325 dataCopy := make ([]byte , len (data ))
311326 copy (dataCopy , data )
312327 data = dataCopy
0 commit comments