/
encoder.go
272 lines (212 loc) · 5.81 KB
/
encoder.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
package zar
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/binary"
"fmt"
"hash"
"io"
"github.com/andybalholm/brotli"
"github.com/dchest/siphash"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/hkdf"
)
// Encoder writes the archive
type Encoder struct {
w io.Writer
// k1 is the key used for encryption and HMACs
k1, k2, k3 []byte
// salt is used in the key KDF
salt []byte
almanac []File
stream *streamCipher
brotilW *brotli.Writer
// fileMac is used to hash the individual files in the repo
fileMac hash.Hash
compressionLevel int
note []byte
cipherBlockSize uint64
}
// New creates a new ZAR encoder
func New(w io.Writer, key []byte) (*Encoder, error) {
// TODO: write header
// TODO: accept options; KDF, MAC, HKDF CHF
// generate salt/IV for KDF and AES
salt := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, err
}
// write salt to file
if _, err := w.Write(salt); err != nil {
return nil, err
}
// Run the key through Argon2Key KDF
k1 := argon2.Key(key, salt, 1, 20, 1, 32)
// derive additional keys from master
kdf := hkdf.New(sha512.New, k1, nil, nil)
// k2 is used for the master mac
k2 := make([]byte, 32)
// k3 is used for SipHash
k3 := make([]byte, 32)
// k4 is used for encryption
k4 := make([]byte, 32)
if _, err := io.ReadFull(kdf, k2); err != nil {
return nil, err
}
if _, err := io.ReadFull(kdf, k3); err != nil {
return nil, err
}
if _, err := io.ReadFull(kdf, k4); err != nil {
return nil, err
}
masterMac := hmac.New(sha512.New, k2)
// Create new AES_256 cipher
block, err := aes.NewCipher(k4)
if err != nil {
return nil, err
}
if _, err := w.Write(salt); err != nil {
return nil, err
}
// Set mode to CTR
c := cipher.NewCTR(block, salt)
// stream output to file
stream := newCipher(masterMac, nil, w, c)
return &Encoder{
w: w,
k1: k1,
k2: k2,
k3: k3,
stream: &stream,
salt: salt,
compressionLevel: brotli.DefaultCompression,
brotilW: brotli.NewWriterLevel(&stream, brotli.DefaultCompression),
fileMac: siphash.New(k3),
cipherBlockSize: uint64(block.BlockSize()),
}, nil
}
// Close must be called to finalise the archive
func (e *Encoder) Close() error {
// e.closeBlock()
return e.writeAlmanac()
}
func (e *Encoder) writeAlmanac() error {
almanacOffset := e.stream.size
w := brotli.NewWriterLevel(e.stream, e.compressionLevel)
// write array size of almanac
fileCount := make([]byte, 8)
binary.BigEndian.PutUint64(fileCount, uint64(len(e.almanac)))
if _, err := w.Write(fileCount); err != nil {
return err
}
e.fileMac.Write(fileCount)
buf := make([]byte, 8)
for i := 0; i < len(e.almanac); i++ {
// write block offset
binary.BigEndian.PutUint64(buf, e.almanac[i].Offset)
if _, err := w.Write(buf); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(buf)
// write file size
binary.BigEndian.PutUint64(buf, e.almanac[i].Size)
if _, err := w.Write(buf); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(buf)
// write modified date
binary.BigEndian.PutUint64(buf, e.almanac[i].Modified)
if _, err := w.Write(buf); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(buf)
// write file name length
binary.BigEndian.PutUint16(buf, uint16(len(e.almanac[i].Name)))
if _, err := w.Write(buf[:2]); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(buf[:2])
// write file name
if _, err := w.Write([]byte(e.almanac[i].Name)); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write([]byte(e.almanac[i].Name))
}
// write note length
binary.BigEndian.PutUint16(buf, uint16(len(e.note)))
if _, err := w.Write(buf[:2]); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(buf[:2])
// write note
if _, err := w.Write(e.note); err != nil {
return err
}
// compute message authentication code
e.fileMac.Write(e.note)
// write almanac mac
if _, err := w.Write([]byte(e.fileMac.Sum(nil))); err != nil {
return err
}
e.fileMac.Reset()
// finalise compression
if err := w.Close(); err != nil {
return err
}
// write almanac offset
binary.BigEndian.PutUint64(buf, almanacOffset)
if _, err := e.stream.Write(buf); err != nil {
return err
}
// pad ciphertext
padding := pkcs5(e.stream.size, e.cipherBlockSize)
if _, err := e.stream.Write(padding); err != nil {
return err
}
// append EtM master Mac
if _, err := e.w.Write(e.stream.MAC()); err != nil {
return err
}
return nil
}
// Add will read a file and add it to the archive
func (e *Encoder) Add(name string, modified uint64, r io.Reader) (int64, error) {
// will be used to calculate the size of the Brotil output
sizeBefore := e.stream.size
fmt.Printf("BEFORE === %-16s %s: %d\n", "Stream Size", name, sizeBefore)
// create new brotil compressor which directs output into AES_256_CTR
// stream
brotilW := brotli.NewWriterLevel(e.stream, e.compressionLevel)
// stream file -> compressor -> AES -> output file
// -> SipHash
n, err := io.Copy(io.MultiWriter(brotilW, e.fileMac), r)
if err != nil {
return n, err
}
mac := e.fileMac.Sum(nil)
fmt.Printf("MAC %X\n", mac)
// Sum MAC and compress it with block
brotilW.Write(mac)
e.fileMac.Reset()
if err := brotilW.Close(); err != nil {
return 0, err
}
e.almanac = append(e.almanac, File{
Name: name,
Modified: modified,
Size: e.stream.size - sizeBefore,
Offset: sizeBefore, //TODO: calculate bytes from block start
})
fmt.Printf("AFTER === %-16s %s: %d\n", "Stream Size", name, e.stream.size)
fmt.Printf("AFTER === %-16s %s: %d\n", "Offset", name, e.stream.size-sizeBefore)
return n, nil
}