-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptfs.go
181 lines (156 loc) · 4.4 KB
/
cryptfs.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
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package cryptfs
import (
"crypto/hmac"
"crypto/sha256"
"errors"
"fmt"
"io/fs"
"os"
)
type FS struct {
compressor Compressor
cryptor Cryptor
coder Coder
hmacKey []byte
}
// New returns a FS instance with the specified Cryptor used for all operations.
//
// Note: The defaults are to use no compression and no encryption.
func New(cryptor Cryptor) (*FS, error) {
if cryptor == nil {
return nil, errors.New("nil Cryptor")
}
return &FS{
compressor: NoCompression(),
cryptor: cryptor,
coder: NoEncoding(),
}, nil
}
// FromCryptor returns an FS instance and allows passing the results of creating a
// Cryptor directly as the arguments.
func FromCryptor(cryptor Cryptor, err error) (*FS, error) {
if err != nil {
return nil, err
}
return New(cryptor)
}
func (fsys *FS) SetCompression(compressor Compressor) {
if fsys != nil && compressor != nil {
fsys.compressor = compressor
}
}
func (fsys *FS) SetCoder(coder Coder) {
if fsys != nil && coder != nil {
fsys.coder = coder
}
}
func (fsys *FS) SetHMACKey(key []byte) {
if fsys != nil {
fsys.hmacKey = key
}
}
// Open will open a file at the given name
func (fsys *FS) Open(name string) (fs.File, error) {
fd, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("opening %s failed: %w", name, err)
}
return fd, nil
}
// Reveal will decode and then decrypt the bytes its given
func (fsys *FS) Reveal(encodedBytes []byte) ([]byte, error) {
bs, err := fsys.coder.decode(encodedBytes)
if err != nil {
return nil, fmt.Errorf("decoding: %w", err)
}
// Verify MAC if hmacKey is set
if len(fsys.hmacKey) > 1 {
macSize := sha256.Size
if len(bs) < macSize {
return nil, errors.New("data too short to contain valid HMAC")
}
receivedMAC := bs[:macSize]
bs = bs[macSize:]
expectedMAC := fsys.computeHMAC(bs)
if !hmac.Equal(receivedMAC, expectedMAC) {
return nil, errors.New("invalid MAC, data integrity could be compromised")
}
}
bs, err = fsys.cryptor.decrypt(bs)
if err != nil {
return nil, fmt.Errorf("decryption: %w", err)
}
bs, err = fsys.compressor.decompress(bs)
if err != nil {
return nil, fmt.Errorf("decompression: %w", err)
}
return bs, nil
}
// ReadFile will attempt to open, decode, and decrypt a file.
func (fsys *FS) ReadFile(name string) ([]byte, error) {
encodedBytes, err := os.ReadFile(name)
if err != nil {
return nil, err
}
bs, err := fsys.Reveal(encodedBytes)
if err != nil {
return nil, fmt.Errorf("reading %s failed: %w", name, err)
}
return bs, nil
}
// Disfigure will encrypt and encode the plaintext
func (fsys *FS) Disfigure(plaintext []byte) ([]byte, error) {
bs, err := fsys.compressor.compress(plaintext)
if err != nil {
return nil, fmt.Errorf("compression: %w", err)
}
bs, err = fsys.cryptor.encrypt(bs)
if err != nil {
return nil, fmt.Errorf("encryption: %w", err)
}
// Prepend the MAC to the encrypted data
if len(fsys.hmacKey) > 1 {
mac := fsys.computeHMAC(bs)
bs = append(mac, bs...)
}
bs, err = fsys.coder.encode(bs)
if err != nil {
return nil, fmt.Errorf("encoding: %w", err)
}
return bs, nil
}
func (fsys *FS) computeHMAC(data []byte) []byte {
mac := hmac.New(sha256.New, fsys.hmacKey)
mac.Write(data)
return mac.Sum(nil)
}
// WriteFile will attempt to encrypt, encode, and create a file under the given filepath.
func (fsys *FS) WriteFile(filepath string, plaintext []byte, perm fs.FileMode) error {
encodedBytes, err := fsys.Disfigure(plaintext)
if err != nil {
return err
}
err = os.WriteFile(filepath, encodedBytes, perm)
if err != nil {
return fmt.Errorf("writing %s failed: %w", filepath, err)
}
return nil
}
var _ fs.FS = (&FS{})
var _ fs.ReadFileFS = (&FS{})