-
Notifications
You must be signed in to change notification settings - Fork 125
/
enclave.go
59 lines (52 loc) · 1.5 KB
/
enclave.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
package memguard
import (
"github.com/awnumar/memguard/core"
)
/*
Enclave is a sealed and encrypted container for sensitive data.
*/
type Enclave struct {
*core.Enclave
}
/*
NewEnclave seals up some data into an encrypted enclave object. The buffer is wiped after the data is copied. If the length of the buffer is zero, the function will return nil.
A LockedBuffer may alternatively be converted into an Enclave object using its Seal method. This will also have the effect of destroying the LockedBuffer.
*/
func NewEnclave(src []byte) *Enclave {
e, err := core.NewEnclave(src)
if err != nil {
if err == core.ErrNullEnclave {
return nil
}
core.Panic(err)
}
return &Enclave{e}
}
/*
NewEnclaveRandom generates and seals arbitrary amounts of cryptographically-secure random bytes into an encrypted enclave object. If size is not strictly positive the function will return nil.
*/
func NewEnclaveRandom(size int) *Enclave {
// todo: stream data into enclave
b := NewBufferRandom(size)
return b.Seal()
}
/*
Open decrypts an Enclave object and places its contents into an immutable LockedBuffer. An error will be returned if decryption failed.
*/
func (e *Enclave) Open() (*LockedBuffer, error) {
b, err := core.Open(e.Enclave)
if err != nil {
if err != core.ErrDecryptionFailed {
core.Panic(err)
}
return nil, err
}
b.Freeze()
return newBuffer(b), nil
}
/*
Size returns the number of bytes of data stored within an Enclave.
*/
func (e *Enclave) Size() int {
return core.EnclaveSize(e.Enclave)
}