-
Notifications
You must be signed in to change notification settings - Fork 5
/
encrypted.go
160 lines (136 loc) · 3.84 KB
/
encrypted.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
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
package db
import (
"fmt"
"sync"
"github.com/awnumar/memguard"
"github.com/dgraph-io/badger"
"github.com/stephane-martin/skewer/utils"
"github.com/stephane-martin/skewer/utils/sbox"
)
type EncryptedDB struct {
p *partitionImpl
secret *memguard.LockedBuffer
}
var bufpool = &sync.Pool{
New: func() interface{} {
return make([]byte, 0, 4096)
},
}
var mappool = &sync.Pool{
New: func() interface{} {
return make(map[utils.MyULID]string, 5000)
},
}
func getTmpMap() map[utils.MyULID]string {
m := mappool.Get().(map[utils.MyULID]string)
for k := range m {
delete(m, k)
}
return m
}
func getTmpBuf() []byte {
return bufpool.Get().([]byte)[:0]
}
func NewEncryptedPartition(p Partition, secret *memguard.LockedBuffer) (Partition, error) {
var impl *partitionImpl
var ok bool
if impl, ok = p.(*partitionImpl); !ok {
return nil, fmt.Errorf("Argument partition is not a partitionImpl")
}
return &EncryptedDB{
p: impl,
secret: secret,
}, nil
}
func (encDB *EncryptedDB) Prefix() string {
return encDB.p.Prefix()
}
func (encDB *EncryptedDB) KeyIterator(txn *NTransaction) *ULIDIterator {
opt := badger.IteratorOptions{
PrefetchValues: false,
PrefetchSize: 100,
}
return &ULIDIterator{iter: txn.NewIterator(opt), secret: encDB.secret, prefix: []byte(encDB.p.prefix)}
}
func (encDB *EncryptedDB) KeyValueIterator(txn *NTransaction) *ULIDIterator {
opt := badger.IteratorOptions{
PrefetchValues: true,
PrefetchSize: 100,
}
return &ULIDIterator{iter: txn.NewIterator(opt), secret: encDB.secret, prefix: []byte(encDB.p.prefix)}
}
func (encDB *EncryptedDB) Exists(key utils.MyULID, txn *NTransaction) (bool, error) {
return encDB.p.Exists(key, txn)
}
func (encDB *EncryptedDB) ListKeys(txn *NTransaction) []utils.MyULID {
return encDB.p.ListKeys(txn)
}
func (encDB *EncryptedDB) ListKeysTo(txn *NTransaction, dest []utils.MyULID) []utils.MyULID {
return encDB.p.ListKeysTo(txn, dest)
}
func (encDB *EncryptedDB) Count(txn *NTransaction) int {
return encDB.p.Count(txn)
}
func (encDB *EncryptedDB) Delete(key utils.MyULID, txn *NTransaction) error {
return encDB.p.Delete(key, txn)
}
func (encDB *EncryptedDB) DeleteMany(keys []utils.MyULID, txn *NTransaction) error {
return encDB.p.DeleteMany(keys, txn)
}
func (encDB *EncryptedDB) Set(key utils.MyULID, value string, txn *NTransaction) error {
encBuf, err := sbox.EncryptTo([]byte(value), encDB.secret, getTmpBuf())
if err != nil {
return err
}
// we can reuse encBuf because we pass a *copy*
err = encDB.p.Set(key, string(encBuf), txn)
bufpool.Put(encBuf)
return err
}
func (encDB *EncryptedDB) AddManyTrueMap(m map[utils.MyULID]string, txn *NTransaction) (err error) {
encValue, err := sbox.EncryptTo(trueBytes, encDB.secret, getTmpBuf())
if err != nil {
return err
}
encStr := string(encValue)
bufpool.Put(encValue)
tmpMap := getTmpMap()
for uid := range m {
tmpMap[uid] = encStr
}
err = encDB.p.AddMany(tmpMap, txn)
mappool.Put(tmpMap)
return err
}
func (encDB *EncryptedDB) AddManySame(uids []utils.MyULID, v string, txn *NTransaction) (err error) {
encValue, err := sbox.Encrypt([]byte(v), encDB.secret)
if err != nil {
return err
}
return encDB.p.AddManySame(uids, string(encValue), txn)
}
func (encDB *EncryptedDB) AddMany(m map[utils.MyULID]string, txn *NTransaction) error {
tmpMap := getTmpMap()
buf := getTmpBuf()
var err error
for uid, val := range m {
buf, err = sbox.EncryptTo([]byte(val), encDB.secret, buf[:0])
if err != nil {
return err
}
tmpMap[uid] = string(buf)
}
err = encDB.p.AddMany(tmpMap, txn)
bufpool.Put(buf)
mappool.Put(tmpMap)
return err
}
func (encDB *EncryptedDB) Get(key utils.MyULID, dst []byte, txn *NTransaction) ([]byte, error) {
encVal, err := encDB.p.Get(key, getTmpBuf(), txn)
if err != nil {
return dst, err
}
ret, err := sbox.DecryptTo(encVal, encDB.secret, dst[:0])
bufpool.Put(encVal)
return ret, err
}