-
Notifications
You must be signed in to change notification settings - Fork 11
/
siv.go
293 lines (257 loc) · 5.84 KB
/
siv.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
package main
import (
"bytes"
"compress/gzip"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/jacobsa/crypto/siv"
)
var (
keyLoader = key
kr keyRing
prefix = []byte("# STRONGBOX ENCRYPTED RESOURCE ;")
defaultPrefix = []byte("# STRONGBOX ENCRYPTED RESOURCE ; See https://github.com/uw-labs/strongbox\n")
errKeyNotFound = errors.New("key not found")
)
func genKey(desc string) {
err := kr.Load()
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
key := make([]byte, 32)
_, err = rand.Read(key)
if err != nil {
log.Fatal(err)
}
keyID := sha256.Sum256(key)
kr.AddKey(desc, keyID[:], key)
err = kr.Save()
if err != nil {
log.Fatal(err)
}
}
// recursiveDecrypt will try and recursively decrypt files
// if 'key' is provided then it will decrypt all encrypted files with given key
// otherwise it will find key based on file location
// if error is generated in finding key or in decryption then it will continue with next file
// function will only return early if it failed to read/write files
func recursiveDecrypt(target string, givenKey []byte) error {
var decErrors []string
err := filepath.WalkDir(target, func(path string, entry fs.DirEntry, err error) error {
// always return on error
if err != nil {
return err
}
// only process files
if entry.IsDir() {
// skip .git directory
if entry.Name() == ".git" {
return fs.SkipDir
}
return nil
}
file, err := os.OpenFile(path, os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
// for optimisation only read required chunk of the file and verify if encrypted
chunk := make([]byte, len(defaultPrefix))
_, err = file.Read(chunk)
if err != nil && err != io.EOF {
return err
}
if !bytes.HasPrefix(chunk, prefix) {
return nil
}
key := givenKey
if len(key) == 0 {
key, err = keyLoader(path)
if err != nil {
// continue with next file
decErrors = append(decErrors, fmt.Sprintf("unable to find key file:%s err:%s", path, err))
return nil
}
}
// read entire file from the beginning
file.Seek(0, io.SeekStart)
in, err := io.ReadAll(file)
if err != nil {
return err
}
out, err := decrypt(in, key)
if err != nil {
// continue with next file
decErrors = append(decErrors, fmt.Sprintf("unable to decrypt file:%s err:%s", path, err))
return nil
}
if err := file.Truncate(0); err != nil {
return err
}
if _, err := file.Seek(0, io.SeekStart); err != nil {
return err
}
if _, err := file.Write(out); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
if len(decErrors) > 0 {
for _, e := range decErrors {
log.Println(e)
}
return fmt.Errorf("unable to decrypt some files")
}
return nil
}
func encrypt(b, key []byte) ([]byte, error) {
b = compress(b)
out, err := siv.Encrypt(nil, key, b, nil)
if err != nil {
return nil, err
}
var buf []byte
buf = append(buf, defaultPrefix...)
b64 := encode(out)
for len(b64) > 0 {
l := 76
if len(b64) < 76 {
l = len(b64)
}
buf = append(buf, b64[0:l]...)
buf = append(buf, '\n')
b64 = b64[l:]
}
return buf, nil
}
func decrypt(enc []byte, priv []byte) ([]byte, error) {
// strip prefix and any comment up to end of line
spl := bytes.SplitN(enc, []byte("\n"), 2)
if len(spl) != 2 {
return nil, errors.New("couldn't split on end of line")
}
b64encoded := spl[1]
b64decoded, err := decode(b64encoded)
if err != nil {
return nil, err
}
decrypted, err := siv.Decrypt(priv, b64decoded, nil)
if err != nil {
return nil, err
}
decrypted = decompress(decrypted)
return decrypted, nil
}
func compress(b []byte) []byte {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err := zw.Write(b)
if err != nil {
log.Fatal(err)
}
if err := zw.Close(); err != nil {
log.Fatal(err)
}
return buf.Bytes()
}
func decompress(b []byte) []byte {
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
log.Fatal(err)
}
b, err = io.ReadAll(zr)
if err != nil {
log.Fatal(err)
}
if err := zr.Close(); err != nil {
log.Fatal(err)
}
return b
}
func encode(decoded []byte) []byte {
b64 := make([]byte, base64.StdEncoding.EncodedLen(len(decoded)))
base64.StdEncoding.Encode(b64, decoded)
return b64
}
func decode(encoded []byte) ([]byte, error) {
decoded := make([]byte, len(encoded))
i, err := base64.StdEncoding.Decode(decoded, encoded)
if err != nil {
return nil, err
}
return decoded[0:i], nil
}
// key returns private key and error
func key(filename string) ([]byte, error) {
keyID, err := findKey(filename)
if err != nil {
return []byte{}, err
}
err = kr.Load()
if err != nil {
return []byte{}, err
}
key, err := kr.Key(keyID)
if err != nil {
return []byte{}, err
}
return key, nil
}
func findKey(filename string) ([]byte, error) {
path := filepath.Dir(filename)
for {
if fi, err := os.Stat(path); err == nil && fi.IsDir() {
keyFilename := filepath.Join(path, ".strongbox-keyid")
if keyFile, err := os.Stat(keyFilename); err == nil && !keyFile.IsDir() {
return readKeyID(keyFilename)
}
}
if path == "." {
break
}
path = filepath.Dir(path)
}
return []byte{}, fmt.Errorf("failed to find key id for file %s", filename)
}
func readKeyID(filename string) ([]byte, error) {
fp, err := os.ReadFile(filename)
if err != nil {
return []byte{}, err
}
b64 := strings.TrimSpace(string(fp))
b, err := decode([]byte(b64))
if err != nil {
return []byte{}, err
}
if len(b) != 32 {
return []byte{}, fmt.Errorf("unexpected key length %d", len(b))
}
return b, nil
}
func sivFileToKey(filename string) ([]byte, error) {
keyID, err := readKeyID(filename)
if err != nil {
return []byte{}, err
}
err = kr.Load()
if err != nil {
return []byte{}, err
}
key, err := kr.Key(keyID)
if err != nil {
return []byte{}, err
}
return key, nil
}