-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharchive.go
379 lines (317 loc) · 7.42 KB
/
archive.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (C) 2016 - Will Glozer. All rights reserved.
package main
import (
"bufio"
"crypto/rand"
"errors"
"io"
"github.com/codahale/sss"
"github.com/magical/argon2"
"github.com/wg/arc/archive"
"github.com/wg/arc/binary"
)
const (
Version = 0x01
Password = 0x01
Curve448 = 0x02
Shard = 0x03
KeySize = archive.KeySize
)
type Archiver interface {
Reader() (*Reader, error)
Writer() (*Writer, error)
}
type Reader struct {
buffer *bufio.Reader
files []File
*archive.Reader
}
type Writer struct {
buffer *bufio.Writer
files []File
tagAt int64
*archive.Writer
}
var (
ErrInvalidArchive = errors.New("archive: verify failed")
ErrInvalidVersion = errors.New("archive: unsupported version")
ErrPasswordArchive = errors.New("archive: password archive")
ErrCurve448Archive = errors.New("archive: curve448 archive")
ErrShardArchive = errors.New("archive: shard archive")
)
// A PasswordArchive is encrypted with a key derived from a password,
// cost parameters, and cryptographically secure random salt using the
// Argon2 password hashing function.
type PasswordArchive struct {
Version byte
Type byte
Iterations uint32
Memory uint32
Salt [32]byte
Password []byte
File File
}
func NewPasswordArchive(password []byte, iterations, memory uint32, file File) *PasswordArchive {
return &PasswordArchive{
Version: Version,
Type: Password,
Iterations: iterations,
Memory: memory,
Password: password,
File: file,
}
}
func (a *PasswordArchive) Reader() (*Reader, error) {
err := binary.Read(a.File, binary.LE, a)
if err != nil {
return nil, err
}
switch {
case a.Version != Version:
return nil, ErrInvalidVersion
case a.Type == Curve448:
return nil, ErrCurve448Archive
case a.Type == Shard:
return nil, ErrShardArchive
}
key, err := a.Key()
if err != nil {
return nil, err
}
return newArchiveReader(key, a.File, a.File)
}
func (a *PasswordArchive) Writer() (*Writer, error) {
_, err := rand.Read(a.Salt[:])
if err != nil {
return nil, err
}
err = binary.Write(a.File, binary.LE, a)
if err != nil {
return nil, err
}
key, err := a.Key()
if err != nil {
return nil, err
}
return newArchiveWriter(key, a.File, a.File)
}
func (a *PasswordArchive) Key() ([]byte, error) {
var (
password = a.Password
salt = a.Salt[:]
iterations = int(a.Iterations)
memory = int64(a.Memory)
)
return argon2.Key(password, salt, iterations, 1, memory, KeySize)
}
// A Curve448Archive is encrypted with a key derived from applying
// BLAKE2b to the shared secret derived from an X448 ECDH key exchange
// with an ephemeral private key and static public key.
type Curve448Archive struct {
Version byte
Type byte
Ephemeral PublicKey
PublicKey *PublicKey
PrivateKey *PrivateKey
File File
}
func NewCurve448Archive(public *PublicKey, private *PrivateKey, file File) *Curve448Archive {
return &Curve448Archive{
Version: Version,
Type: Curve448,
PublicKey: public,
PrivateKey: private,
File: file,
}
}
func (a *Curve448Archive) Reader() (*Reader, error) {
err := binary.Read(a.File, binary.LE, a)
if err != nil {
return nil, err
}
switch {
case a.Version != Version:
return nil, ErrInvalidVersion
case a.Type == Password:
return nil, ErrPasswordArchive
case a.Type == Shard:
return nil, ErrShardArchive
}
key, err := ComputeSharedKey(&a.Ephemeral, a.PrivateKey, KeySize)
if err != nil {
return nil, err
}
return newArchiveReader(key, a.File, a.File)
}
func (a *Curve448Archive) Writer() (*Writer, error) {
ephemeralPublicKey, ephemeralPrivateKey, err := GenerateKeypair()
if err != nil {
return nil, err
}
defer ephemeralPrivateKey.Zero()
key, err := ComputeSharedKey(a.PublicKey, ephemeralPrivateKey, KeySize)
if err != nil {
return nil, err
}
a.Ephemeral = *ephemeralPublicKey
err = binary.Write(a.File, binary.LE, a)
if err != nil {
return nil, err
}
return newArchiveWriter(key, a.File, a.File)
}
// A ShardArchive is encrypted with a key consisting of cryptographically
// secure random bytes. That key is split into n shards using Shamir's
// Secret Sharing algorithm and one archive is generate for each shard.
// k shards must be present to recreate the key.
type ShardArchive struct {
Version byte
Type byte
ID byte
Share [KeySize]byte
Threshold int
File File
Shards []*ShardArchive
}
func NewShardArchive(threshold int, files []File) *ShardArchive {
shards := make([]*ShardArchive, len(files))
for i, file := range files {
shards[i] = &ShardArchive{
Version: Version,
Type: Shard,
Threshold: threshold,
File: file,
Shards: shards,
}
}
return shards[0]
}
func (a *ShardArchive) Reader() (*Reader, error) {
shares := make(map[byte][]byte, len(a.Shards))
for _, shard := range a.Shards {
err := binary.Read(shard.File, binary.LE, shard)
if err != nil {
return nil, err
}
switch {
case shard.Version != Version:
return nil, ErrInvalidVersion
case shard.Type == Password:
return nil, ErrPasswordArchive
case shard.Type == Curve448:
return nil, ErrCurve448Archive
}
shares[shard.ID] = shard.Share[:]
}
key := sss.Combine(shares)
return newArchiveReader(key, a.File, a.Files()...)
}
func (a *ShardArchive) Writer() (*Writer, error) {
var key [32]byte
_, err := rand.Read(key[:])
if err != nil {
return nil, err
}
n := byte(len(a.Shards))
k := byte(a.Threshold)
shares, err := sss.Split(n, k, key[:])
if err != nil {
return nil, err
}
writers := make([]io.Writer, len(a.Shards))
for id, share := range shares {
index := id - 1
shard := a.Shards[index]
shard.ID = id
copy(shard.Share[:], share)
err = binary.Write(shard.File, binary.LE, shard)
if err != nil {
return nil, err
}
writers[index] = shard.File
}
w := io.MultiWriter(writers...)
return newArchiveWriter(key[:], w, a.Files()...)
}
func (a *ShardArchive) Files() []File {
files := make([]File, len(a.Shards))
for i, shard := range a.Shards {
files[i] = shard.File
}
return files
}
func newArchiveReader(key []byte, raw io.Reader, files ...File) (*Reader, error) {
switch valid, err := verify(key, files[0]); {
case err != nil:
return nil, err
case !valid:
return nil, ErrInvalidArchive
}
buffer := bufio.NewReader(raw)
r, err := archive.NewReader(buffer, key)
return &Reader{
Reader: r,
buffer: buffer,
files: files,
}, err
}
func newArchiveWriter(key []byte, raw io.Writer, files ...File) (*Writer, error) {
tagAt, err := files[0].Seek(0, 1)
if err != nil {
return nil, err
}
buffer := bufio.NewWriter(raw)
w, err := archive.NewWriter(buffer, key)
return &Writer{
Writer: w,
buffer: buffer,
files: files,
tagAt: tagAt,
}, err
}
func verify(key []byte, file File) (bool, error) {
p, err := file.Seek(0, 1)
if err != nil {
return false, err
}
defer file.Seek(p, 0)
buffer := bufio.NewReader(file)
return archive.Verify(buffer, key)
}
func (r *Reader) Close() error {
for _, f := range r.files {
err := f.Close()
if err != nil {
return err
}
}
return nil
}
func (w *Writer) Close() error {
tag, err := w.Finish()
if err != nil {
return err
}
err = w.buffer.Flush()
if err != nil {
return err
}
for _, f := range w.files {
_, err := f.WriteAt(tag, w.tagAt)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
}
return nil
}
type File interface {
io.Reader
io.Writer
io.WriterAt
io.Seeker
io.Closer
}