-
Notifications
You must be signed in to change notification settings - Fork 8
/
encryption.go
73 lines (62 loc) · 1.9 KB
/
encryption.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"reflect"
"unsafe"
"storj.io/uplink"
)
// EncryptionKey represents a key for encrypting and decrypting data.
type EncryptionKey struct {
*uplink.EncryptionKey
}
// uplink_derive_encryption_key derives a salted encryption key for passphrase using the
// salt.
//
// This function is useful for deriving a salted encryption key for users when
// implementing multitenancy in a single app bucket.
//
//export uplink_derive_encryption_key
func uplink_derive_encryption_key(passphrase *C.uplink_const_char, salt unsafe.Pointer, length C.size_t) C.UplinkEncryptionKeyResult {
if passphrase == nil {
return C.UplinkEncryptionKeyResult{
error: mallocError(ErrNull.New("passphrase")),
}
}
ilength, ok := safeConvertToInt(length)
if !ok {
return C.UplinkEncryptionKeyResult{
error: mallocError(ErrInvalidArg.New("length too large")),
}
}
var goSalt []byte
hGoSalt := (*reflect.SliceHeader)(unsafe.Pointer(&goSalt))
hGoSalt.Data = uintptr(salt)
hGoSalt.Len = ilength
hGoSalt.Cap = ilength
encKey, err := uplink.DeriveEncryptionKey(C.GoString(passphrase), goSalt)
if err != nil {
return C.UplinkEncryptionKeyResult{
error: mallocError(err),
}
}
return C.UplinkEncryptionKeyResult{
encryption_key: (*C.UplinkEncryptionKey)(mallocHandle(universe.Add(&EncryptionKey{encKey}))),
}
}
// uplink_free_encryption_key_result frees the resources associated with encryption key.
//
//export uplink_free_encryption_key_result
func uplink_free_encryption_key_result(result C.UplinkEncryptionKeyResult) {
uplink_free_error(result.error)
freeEncryptionKey(result.encryption_key)
}
func freeEncryptionKey(encryptionKey *C.UplinkEncryptionKey) {
if encryptionKey == nil {
return
}
defer C.free(unsafe.Pointer(encryptionKey))
defer universe.Del(encryptionKey._handle)
}