-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
277 lines (246 loc) · 5.78 KB
/
main.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/awnumar/memguard"
"github.com/docker/go-units"
)
var args = os.Args
func main() {
cleanup := func() {
// Sync and close disk-backed database.
closeDB()
// Purge sensitive information from memory.
memguard.Purge()
fmt.Println("[i] Memory successfully purged... exiting.")
}
memguard.CatchSignal(func(_ os.Signal) {
cleanup() // Call cleanup on catching a signal.
})
defer cleanup() // Run cleanup after returning.
// Parse command line arguments.
if args[1] == "seal" {
if len(args) != 3 {
goto help
}
// Get files.
files, err := Files(args[2])
if err != nil {
outputError(err)
return
}
// Output state.
totalSize := func() (total int64) {
for file := range files {
total += files[file].Size
}
return
}()
fmt.Printf("[i] Encrypting %d files from \"%s\" (%s)\n", len(files), args[2], units.BytesSize(float64(totalSize)))
// Read key from standard input directly into secure buffer.
key := input("[?] Enter master key: ")
// Derive root key from user key.
fmt.Println("[i] Processing key...")
pocket := GetPocket(key)
// Initialise identifier.
id, idMemory, err := pocket.Identifier()
if err != nil {
outputError(err)
return
}
// Get local copy of key
key, err = pocket.Key.Open()
if err != nil {
outputError(err)
return
}
// Process each file
var buffer [4096]byte
for file, fileInfo := range files {
fmt.Printf("[+] Sealing %s (%s)\n", fileInfo.Path, units.BytesSize(float64(fileInfo.Size)))
// Handle metadata
metadata, err := json.Marshal(fileInfo)
if err != nil {
outputError(err)
return
}
for i := 0; i < len(metadata); i += 4095 {
var size int
if i+4095 > len(metadata) {
size = copy(buffer[:], metadata[len(metadata)-(len(metadata)%4095):])
} else {
size = copy(buffer[:], metadata[i:i+4095])
}
buffer[size] = 1 // padding
ct, _ := Encrypt(buffer[:], key.Bytes())
if err := Put(id.Derive(idMemory, uint64(file), uint64(2*i/4095+1)), ct); err != nil {
outputError(err)
return
}
memguard.WipeBytes(buffer[:])
}
// Handle file contents
f, err := os.Open(fileInfo.Path)
if err != nil {
outputError(err)
return
}
for c := uint64(0); ; c += 2 {
n, _ := io.ReadFull(f, buffer[:4095])
if n == 0 {
break
}
buffer[n] = 1 // padding
ct, _ := Encrypt(buffer[:], key.Bytes())
if err := Put(id.Derive(idMemory, uint64(file), c), ct); err != nil {
outputError(err)
return
}
memguard.WipeBytes(buffer[:])
}
}
return
} else if args[1] == "open" {
if len(args) != 3 {
goto help
}
if err := os.Mkdir(args[2], os.ModeDir|os.ModePerm); err != nil {
outputError(err)
return
}
if err := os.Chdir(args[2]); err != nil {
outputError(err)
return
}
// Read key from standard input directly into secure buffer.
key := input("[?] Enter master key: ")
// Derive root key from user key.
fmt.Println("[i] Processing key...")
pocket := GetPocket(key)
// Initialise identifier.
id, idMemory, err := pocket.Identifier()
if err != nil {
outputError(err)
return
}
// Get local copy of key
key, err = pocket.Key.Open()
if err != nil {
outputError(err)
return
}
// Extract data
var buffer [4096]byte
for i := uint64(0); ; i++ { // for every file...
// Handle metadata
var metadata []byte
for j := uint64(1); ; j += 2 {
chunk, err := Get(id.Derive(idMemory, i, j))
if err != nil {
// eof
break
}
n, err := Decrypt(chunk, key.Bytes(), buffer[:])
if err != nil {
outputError(err)
return
}
if n != 4096 {
outputError(errors.New("error invalid plaintext size"))
return
}
for k := len(buffer) - 1; ; k-- {
if buffer[k] == 0 {
continue
} else if buffer[k] == 1 {
metadata = append(metadata, buffer[:k]...)
break
} else {
outputError(errors.New("error invalid padding"))
return
}
}
memguard.WipeBytes(buffer[:])
}
if len(metadata) == 0 {
//eof
break
}
var md FileInfo
if err := json.Unmarshal(metadata, &md); err != nil {
outputError(err)
return
}
fmt.Printf("[+] Extracting %s (%s)\n", md.Path, units.BytesSize(float64(md.Size)))
// Create output directory if needed
if dir := filepath.Dir(md.Path); dir != "" {
if err := os.MkdirAll(dir, os.ModeDir|os.ModePerm); err != nil {
outputError(err)
return
}
}
// Create output file.
file, err := os.Create(md.Path)
if err != nil {
outputError(err)
return
}
// Handle contents
for j := uint64(0); ; j += 2 {
chunk, err := Get(id.Derive(idMemory, i, j))
if err != nil {
// eof
break
}
n, err := Decrypt(chunk, key.Bytes(), buffer[:])
if err != nil {
outputError(err)
return
}
if n != 4096 {
outputError(errors.New("error invalid plaintext size"))
return
}
for k := len(buffer) - 1; ; k-- {
if buffer[k] == 0 {
continue
} else if buffer[k] == 1 {
if _, err := file.Write(buffer[:k]); err != nil {
outputError(err)
return
}
break
} else {
outputError(errors.New("error invalid padding"))
return
}
}
}
file.Close()
}
return
} else if args[1] == "wipe" {
if len(args) != 2 {
goto help
}
// todo
}
help:
help()
}
func help() {
fmt.Printf(`Usage: %s {command}
Commands:
help print help information
seal {path} encrypt and store data at given path
open {path} decrypt and extract data and write to given path
wipe removes all data associated with an entry from the database
`, args[0])
}
func outputError(err error) {
fmt.Fprintln(os.Stderr, "[!]", err)
}