-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptomator.go
254 lines (216 loc) · 7.42 KB
/
cryptomator.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
package gocryptomator
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base32"
"encoding/base64"
"encoding/json"
"fmt"
"hash"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
"github.com/jacobsa/crypto/siv"
"golang.org/x/crypto/scrypt"
)
const (
// CryptomatorMasterkey is the default filename for a vault related data
CryptomatorMasterkey = "masterkey.cryptomator"
)
// CryptomatorVault holds the data for a Cryptomator vault
type CryptomatorVault struct {
Version int `json:"version"`
ScryptSaltString string `json:"scryptSalt"`
ScryptSalt []byte
ScryptCostParam int `json:"scryptCostParam"`
ScryptBlockSize int `json:"scryptBlockSize"`
PrimaryMasterKeyString string `json:"primaryMasterKey"`
PrimaryMasterKey []byte
HmacMasterKeyString string `json:"hmacMasterKey"`
HmacMasterKey []byte
VersionMacString string `json:"versionMac"`
VersionMac []byte
hmacAndMasterKey []byte
vaultDirectory string
kek []byte
cipher cipher.Block
sha1Hasher hash.Hash
rootDirectory string
}
// prepareFromMasterKeyData converts all strings to the corresponding bytes values
func (vault *CryptomatorVault) prepareFromMasterKeyData(raw []byte, vaultDirectory string, password []byte) error {
// first convert all strings to bytes
var err error
// decrypt the keys
err = json.Unmarshal(raw, &vault)
if nil != err {
logrus.Errorf("Unable to parse MaskerKey file")
return err
}
// convert the base64 strings into byte slices
vault.ScryptSalt, err = base64.StdEncoding.DecodeString(vault.ScryptSaltString)
if err != nil {
return fmt.Errorf("Error while decoding ScryptSalt string %s, err: %s", vault.ScryptSaltString, err)
}
vault.PrimaryMasterKey, err = base64.StdEncoding.DecodeString(vault.PrimaryMasterKeyString)
if err != nil {
return fmt.Errorf("Error while decoding PrimaryMasterKey string %s, err: %s", vault.PrimaryMasterKeyString, err)
}
vault.HmacMasterKey, err = base64.StdEncoding.DecodeString(vault.HmacMasterKeyString)
if err != nil {
return fmt.Errorf("Error while decoding HmacMasterKey string %s, err: %s", vault.HmacMasterKeyString, err)
}
vault.VersionMac, err = base64.StdEncoding.DecodeString(vault.VersionMacString)
if err != nil {
return fmt.Errorf("Error while decoding VersionMac string %s, err: %s", vault.VersionMacString, err)
}
// save the name of the directory
vault.vaultDirectory = vaultDirectory
// create the KEK to decrypt the keys
vault.kek, err = scrypt.Key(password, vault.ScryptSalt, vault.ScryptCostParam, vault.ScryptBlockSize, 1, 32)
if err != nil {
return err
}
vault.cipher, err = aes.NewCipher(vault.kek)
if err != nil {
return err
}
// unwrap the keys using the KEK
vault.HmacMasterKey, err = KeyUnwrap(vault.cipher, vault.HmacMasterKey)
if err != nil {
return err
}
vault.PrimaryMasterKey, err = KeyUnwrap(vault.cipher, vault.PrimaryMasterKey)
if err != nil {
return err
}
// append the hmac and the masterkey to be used in the AES SIV routine
vault.hmacAndMasterKey = append(vault.HmacMasterKey[:], vault.PrimaryMasterKey[:]...)
return nil
}
// EncryptedPathFor returns the encrypted path for a given uuid
func (vault *CryptomatorVault) EncryptedPathFor(uuid string, fullPath bool) (string, error) {
// encrypt the uuid string
rootDirNameData, err := siv.Encrypt(nil, vault.hmacAndMasterKey, []byte(uuid), nil)
if err != nil {
return "", err
}
// encode it into a string
dirname := base32.StdEncoding.EncodeToString(vault.hashSHA1(rootDirNameData))
// return ...
if fullPath {
// ... a full path
return filepath.Join(vault.vaultDirectory, "d", dirname[:2], dirname[2:]), nil
}
// just the part into the vault
return path.Join("d", dirname[:2], dirname[2:]), nil
}
// hashSHA1 returns a SHA1 hash from a given byte slice
func (vault *CryptomatorVault) hashSHA1(payload []byte) []byte {
vault.sha1Hasher.Reset()
vault.sha1Hasher.Write(payload)
return vault.sha1Hasher.Sum(nil)
}
// EncryptFilename returns the decrypted filenames
func (vault *CryptomatorVault) EncryptFilename(plainFilename string, parentDirID string) (string, error) {
var associatedData = [][]byte{[]byte(parentDirID)}
encryptedFilename, err := siv.Encrypt(nil, vault.hmacAndMasterKey, []byte(plainFilename), associatedData)
if err != nil {
return "", err
}
// encode it into a string
return base32.StdEncoding.EncodeToString(encryptedFilename), nil
}
// EncryptDirectoryName returns the decrypted filenames
func (vault *CryptomatorVault) EncryptDirectoryName(plainFilename string, parentDirID string) (string, error) {
encryptedFilename, err := vault.EncryptFilename(plainFilename, parentDirID)
if err != nil {
return "", err
}
return "0" + encryptedFilename, nil
}
// DecryptFilename returns the decrypted filenames
func (vault *CryptomatorVault) DecryptFilename(encryptedFilename string, parentDirID string) (string, error) {
if encryptedFilename[:1] == "0" {
encryptedFilename = encryptedFilename[1:]
}
var associatedData = [][]byte{[]byte(parentDirID)}
filenameBytes, err := base32.StdEncoding.DecodeString(encryptedFilename)
if err != nil {
return "", err
}
filenameBytes, err = siv.Decrypt(vault.hmacAndMasterKey, filenameBytes, associatedData)
if err != nil {
return "", err
}
// base32(aesSiv(cleartextName, parentDirId, encryptionMasterKey, macMasterKey))
return string(filenameBytes), nil
}
// ReadDir returns the decrypted filenames
func (vault *CryptomatorVault) ReadDir(path string) ([]os.FileInfo, error) {
// pathComponents := strings.Split(path, "/")
// for
return nil, fmt.Errorf("Not implemented")
}
// List returns a list of FileInfo for the given UUID
func (vault *CryptomatorVault) List(pathUUID string) ([]*CryptomatorNode, error) {
encryptedPath, err := vault.EncryptedPathFor(pathUUID, true)
if err != nil {
return nil, err
}
dirItems, err := ioutil.ReadDir(encryptedPath)
if err != nil {
return nil, err
}
var nodesList []*CryptomatorNode
for _, dirItem := range dirItems {
nodesList = append(nodesList, CreateNodeFromFileInfo(dirItem, vault, pathUUID))
}
return nodesList, nil
}
func (vault *CryptomatorVault) walkRec(parentDirID string, depth int) error {
rootItems, err := vault.List(parentDirID)
if err != nil {
return err
}
for _, rootItem := range rootItems {
if rootItem.IsDir() {
childParentDirID, err := rootItem.DirUUID()
if err != nil {
return err
}
fmt.Printf("%s%s:\n", strings.Repeat(" ", depth), rootItem.Name())
vault.walkRec(childParentDirID, depth+1)
} else {
fmt.Printf("%s%s with %d bytes\n", strings.Repeat(" ", depth), rootItem.Name(), rootItem.Size())
}
}
return nil
}
func (vault *CryptomatorVault) Walk() error {
return vault.walkRec("", 0)
}
// OpenCrytomatorVault opens an existing vault in the given directory
func OpenCrytomatorVault(vaultDirectory string, password []byte) (*CryptomatorVault, error) {
masterKeyFilename := path.Join(vaultDirectory, CryptomatorMasterkey)
logrus.Debugf("Trying to open %s\n", masterKeyFilename)
raw, err := ioutil.ReadFile(masterKeyFilename)
if err != nil {
return nil, err
}
var vault CryptomatorVault
err = vault.prepareFromMasterKeyData(raw, vaultDirectory, password)
if err != nil {
return nil, err
}
vault.sha1Hasher = sha1.New()
vault.rootDirectory, err = vault.EncryptedPathFor("", false)
if err != nil {
return nil, err
}
return &vault, nil
}