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
|
package fusefrontend_reverse
import (
"bytes"
"io"
"sync"
"github.com/rfjakob/gocryptfs/internal/contentenc"
"github.com/rfjakob/gocryptfs/internal/pathiv"
"github.com/rfjakob/gocryptfs/internal/tlog"
)
var inodeTable sync.Map
// encryptBlocks - encrypt "plaintext" into a number of ciphertext blocks.
// "plaintext" must already be block-aligned.
func (rf *File) encryptBlocks(plaintext []byte, firstBlockNo uint64, fileID []byte, block0IV []byte) []byte {
inBuf := bytes.NewBuffer(plaintext)
var outBuf bytes.Buffer
bs := int(rf.contentEnc.PlainBS())
for blockNo := firstBlockNo; inBuf.Len() > 0; blockNo++ {
inBlock := inBuf.Next(bs)
iv := pathiv.BlockIV(block0IV, blockNo)
outBlock := rf.contentEnc.EncryptBlockNonce(inBlock, blockNo, fileID, iv)
outBuf.Write(outBlock)
}
return outBuf.Bytes()
}
// readBackingFile: read from the backing plaintext file, encrypt it, return the
// ciphertext.
// "off" ... ciphertext offset (must be >= HEADER_LEN)
// "length" ... ciphertext length
func (f *File) readBackingFile(off uint64, length uint64) (out []byte, err error) {
blocks := f.contentEnc.ExplodeCipherRange(off, length)
// Read the backing plaintext in one go
alignedOffset, alignedLength := contentenc.JointPlaintextRange(blocks)
plaintext := make([]byte, int(alignedLength))
n, err := f.fd.ReadAt(plaintext, int64(alignedOffset))
if err != nil && err != io.EOF {
tlog.Warn.Printf("readBackingFile: ReadAt: %s", err.Error())
return nil, err
}
// Truncate buffer down to actually read bytes
plaintext = plaintext[0:n]
// Encrypt blocks
ciphertext := f.encryptBlocks(plaintext, blocks[0].BlockNo, f.header.ID, f.block0IV)
// Crop down to the relevant part
lenHave := len(ciphertext)
skip := blocks[0].Skip
endWant := int(skip + length)
if lenHave > endWant {
out = ciphertext[skip:endWant]
} else if lenHave > int(skip) {
out = ciphertext[skip:lenHave]
} // else: out stays empty, file was smaller than the requested offset
return out, nil
}
|