-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevervault.go
More file actions
356 lines (304 loc) · 12.8 KB
/
Copy pathevervault.go
File metadata and controls
356 lines (304 loc) · 12.8 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Evervault Go SDK.
// Supported functions are:
// - Encrypt data server-side including files
// - Invoke Functions
// - Invoke Cages
// - Proxy requests through Outbound Relay
//
// For up to date usage docs please refer to [Evervault docs]
//
// [Evervault docs]: https://docs.evervault.com/sdks/go
package evervault
import (
"crypto/ecdh"
"crypto/rand"
"fmt"
"strconv"
"time"
"github.com/evervault/evervault-go/internal/crypto"
"github.com/evervault/evervault-go/internal/datatypes"
)
// MakeClient creates a new Client instance if an API key and Evervault App ID is provided. The client
// will connect to Evervaults API to retrieve the public keys from your Evervault App.
//
// import "github.com/evervault/evervault-go"
// evClient, err := evervault.MakeClient("<API_KEY>", "<APP_UUID>")
//
// If an apiKey is not passed then ErrAppCredentialsRequired is returned. If the client cannot
// be created then nil will be returned.
func MakeClient(appUUID, apiKey string) (*Client, error) {
config := MakeConfig()
return MakeCustomClient(appUUID, apiKey, config)
}
// MakeCustomClient creates a new Client instance but can be specified with a Config. The client
// will connect to Evervaults API to retrieve the public keys from your Evervault App.
//
// If an apiKey or appUUID is not passed then ErrAppCredentialsRequired is returned. If the client cannot
// be created then nil will be returned.
func MakeCustomClient(appUUID, apiKey string, config Config) (*Client, error) {
if apiKey == "" || appUUID == "" {
return nil, ErrAppCredentialsRequired
}
client := &Client{appUUID: appUUID, apiKey: apiKey, Config: config}
if err := client.initClient(); err != nil {
return nil, err
}
return client, nil
}
func (c *Client) getAesKeyAndCompressedEphemeralPublicKey() ([]byte, []byte, error) {
ephemeralECDHCurve := ecdh.P256()
ephemeralECDHKey, err := ephemeralECDHCurve.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, fmt.Errorf("error generating ephemeral curve %w", err)
}
appPublicKeyCurve := ecdh.P256()
appPubKey, err := appPublicKeyCurve.NewPublicKey(c.p256PublicKeyUncompressed)
if err != nil {
return nil, nil, ErrCryptoKeyImportError
}
shared, err := ephemeralECDHKey.ECDH(appPubKey)
if err != nil {
return nil, nil, fmt.Errorf("error generating ephermeral key %w", err)
}
ephemeralPublicECDHKeyBytes := ephemeralECDHKey.PublicKey().Bytes()
compressedEphemeralPublicKey := crypto.CompressPublicKey(ephemeralPublicECDHKeyBytes)
aesKey, err := crypto.DeriveKDFAESKey(ephemeralPublicECDHKeyBytes, shared)
if err != nil {
return nil, nil, err
}
return aesKey, compressedEphemeralPublicKey, nil
}
// EncryptString encrypts the value passed to it using the Evervault Encryption Scheme.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptString("Hello, world!");
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptString(value string) (string, error) {
return c.EncryptStringWithDataRole(value, "")
}
// EncryptString encrypts the value passed to it using the Evervault Encryption Scheme.
// The data role included is embedded in the encrypted string and can be used to control access to the data.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptString("Hello, world!");
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptStringWithDataRole(value, role string) (string, error) {
aesKey, compressedEphemeralPublicKey, err := c.getAesKeyAndCompressedEphemeralPublicKey()
if err != nil {
return "", err
}
return crypto.EncryptValue(aesKey, compressedEphemeralPublicKey, c.p256PublicKeyCompressed, value, role,
datatypes.String)
}
// EncryptInt encrypts the value passed to it using the Evervault Encryption Scheme.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptInt(100);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptInt(value int) (string, error) {
return c.EncryptIntWithDataRole(value, "")
}
// EncryptInt encrypts the value passed to it using the Evervault Encryption Scheme.
// The data role included is embedded in the encrypted string and can be used to control access to the data.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptInt(100);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptIntWithDataRole(value int, role string) (string, error) {
aesKey, compressedEphemeralPublicKey, err := c.getAesKeyAndCompressedEphemeralPublicKey()
if err != nil {
return "", err
}
val := strconv.Itoa(value)
return crypto.EncryptValue(aesKey, compressedEphemeralPublicKey, c.p256PublicKeyCompressed, val,
role, datatypes.Number)
}
// EncryptFloat64 encrypts the value passed to it using the Evervault Encryption Scheme.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptInt(100.1);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptFloat64(value float64) (string, error) {
return c.EncryptFloat64WithDataRole(value, "")
}
// EncryptFloat64 encrypts the value passed to it using the Evervault Encryption Scheme.
// The data role included is embedded in the encrypted string and can be used to control access to the data.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptInt(100.1);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptFloat64WithDataRole(value float64, role string) (string, error) {
aesKey, compressedEphemeralPublicKey, err := c.getAesKeyAndCompressedEphemeralPublicKey()
if err != nil {
return "", err
}
val := strconv.FormatFloat(value, 'f', -1, 64)
return crypto.EncryptValue(aesKey, compressedEphemeralPublicKey, c.p256PublicKeyCompressed, val, role,
datatypes.Number)
}
// EncryptBool encrypts the value passed to it using the Evervault Encryption Scheme.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptBool(true);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptBool(value bool) (string, error) {
return c.EncryptBoolWithDataRole(value, "")
}
// EncryptBool encrypts the value passed to it using the Evervault Encryption Scheme.
// The data role included is embedded in the encrypted string and can be used to control access to the data.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptBool(true);
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For anyother error ErrCryptoUnableToPerformEncryption is returned.
func (c *Client) EncryptBoolWithDataRole(value bool, role string) (string, error) {
aesKey, compressedEphemeralPublicKey, err := c.getAesKeyAndCompressedEphemeralPublicKey()
if err != nil {
return "", err
}
val := strconv.FormatBool(value)
return crypto.EncryptValue(aesKey, compressedEphemeralPublicKey, c.p256PublicKeyCompressed, val, role,
datatypes.Boolean)
}
// EncryptByteArray encrypts the value passed to it using the Evervault Encryption Scheme.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptByteArray([]byte("Hello, world!"));
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For any other error ErrCryptoUnableToPerformEncryption is returned.
//
// Deprecated: Use EncryptString for utf-8 encoded byte arrays.
func (c *Client) EncryptByteArray(value []byte) (string, error) {
return c.EncryptByteArrayWithDataRole(value, "")
}
// EncryptByteArray encrypts the value passed to it using the Evervault Encryption Scheme.
// The data role included is embedded in the encrypted string and can be used to control access to the data.
// The encrypted value is returned as an Evervault formatted encrypted string.
//
// encrypted := evClient.EncryptByteArray([]byte("Hello, world!"));
//
// If an error occurs then nil is returned. If the error is due a problem with Key creation then
// ErrCryptoKeyImportError is returned. For any other error ErrCryptoUnableToPerformEncryption is returned.
//
// Deprecated: Use EncryptString for utf-8 encoded byte arrays.
func (c *Client) EncryptByteArrayWithDataRole(value []byte, role string) (string, error) {
aesKey, compressedEphemeralPublicKey, err := c.getAesKeyAndCompressedEphemeralPublicKey()
if err != nil {
return "", err
}
val := string(value)
return crypto.EncryptValue(aesKey, compressedEphemeralPublicKey, c.p256PublicKeyCompressed, val, role,
datatypes.String)
}
// DecryptString decrypts data previously encrypted with Encrypt or through Relay
//
// decrypted := evClient.Decrypt(encrypted);
func (c *Client) DecryptString(encryptedData string) (string, error) {
decryptResponse, err := c.decrypt(encryptedData)
if err != nil {
return "", err
}
decryptedString, ok := decryptResponse.(string)
if !ok {
return "", ErrInvalidDataType
}
return decryptedString, nil
}
// DecryptInt decrypts data previously encrypted with Encrypt or through Relay
//
// decrypted := evClient.DecryptInt(encrypted);
func (c *Client) DecryptInt(encryptedData string) (int, error) {
decryptResponse, err := c.decrypt(encryptedData)
if err != nil {
return 0, err
}
decryptedFloat, ok := decryptResponse.(float64)
if !ok {
return 0, ErrInvalidDataType
}
return int(decryptedFloat), nil
}
// DecryptFloat64 decrypts data previously encrypted with Encrypt or through Relay
//
// decrypted := evClient.DecryptInt(encrypted);
func (c *Client) DecryptFloat64(encryptedData string) (float64, error) {
decryptResponse, err := c.decrypt(encryptedData)
if err != nil {
return 0, err
}
parsedFloat, ok := decryptResponse.(float64)
if !ok {
return 0, ErrInvalidDataType
}
return parsedFloat, nil
}
// DecryptBool decrypts data previously encrypted with Encrypt or through Relay
//
// decrypted := evClient.DecryptBool(encrypted);
func (c *Client) DecryptBool(encryptedData string) (bool, error) {
decryptResponse, err := c.decrypt(encryptedData)
if err != nil {
return false, err
}
decryptedBool, ok := decryptResponse.(bool)
if !ok {
return false, ErrInvalidDataType
}
return decryptedBool, nil
}
// DecryptByteArray decrypts data previously encrypted with Encrypt or through Relay
//
// decrypted := evClient.DecryptByteArray(encrypted);
//
// Deprecated: Use DecryptString for utf-8 encoded encrypted byte arrays.
func (c *Client) DecryptByteArray(encryptedData string) ([]byte, error) {
decryptResponse, err := c.decrypt(encryptedData)
if err != nil {
return nil, err
}
decryptedString, ok := decryptResponse.(string)
if !ok {
return nil, ErrInvalidDataType
}
return []byte(decryptedString), nil
}
// CreateClientSideDecryptToken creates a time bound token that can be used to perform decrypts.
// The payload is required and ensures the token can only be used to decrypt that specific payload.
//
// The expiry is the time the token should expire.
// The max time is 10 minutes in the future and defaults to 5 minutes if not provided.
//
// # It returns a TokenResponse or an error
//
// token, err := CreateClientSideDecryptToken(payload, timeInFiveMinutes).
func (c *Client) CreateClientSideDecryptToken(payload any, expiry ...time.Time) (TokenResponse, error) {
// Used to check whether payload is the zero value for its type
if payload == nil {
return TokenResponse{}, ErrInvalidDataType
}
var epochTime int64
if expiry != nil {
epochTime = expiry[0].UnixMilli()
}
token, err := c.createToken("api:decrypt", payload, epochTime)
if err != nil {
return TokenResponse{}, err
}
return token, nil
}