aboutsummaryrefslogtreecommitdiff
path: root/cryptfs
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-09-06 09:47:01 +0200
committerJakob Unterwurzacher2015-09-06 09:47:01 +0200
commit448e88490bedb1efcaa6bc19f2347a24ed27579a (patch)
tree2c8179ed5aaaf280b1d14ce115a10b103100c89f /cryptfs
parentbaa837b7881a87f342c67096013bdf078d11af99 (diff)
Bundle up blocks for bigger reads from the backing filesystem
Diffstat (limited to 'cryptfs')
-rw-r--r--cryptfs/cryptfs_content.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index 0ebf34d..512bca9 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -3,6 +3,7 @@ package cryptfs
// File content encryption / decryption
import (
+ "bytes"
"os"
"errors"
"crypto/cipher"
@@ -13,7 +14,23 @@ type CryptFile struct {
gcm cipher.AEAD
}
-// decryptBlock - Verify and decrypt GCM block
+// DecryptBlocks - Decrypt a number of blocks
+func (be *CryptFS) DecryptBlocks(ciphertext []byte) ([]byte, error) {
+ cBuf := bytes.NewBuffer(ciphertext)
+ var err error
+ var pBuf bytes.Buffer
+ for cBuf.Len() > 0 {
+ cBlock := cBuf.Next(int(be.cipherBS))
+ pBlock, err := be.DecryptBlock(cBlock)
+ if err != nil {
+ break
+ }
+ pBuf.Write(pBlock)
+ }
+ return pBuf.Bytes(), err
+}
+
+// DecryptBlock - Verify and decrypt GCM block
func (be *CryptFS) DecryptBlock(ciphertext []byte) ([]byte, error) {
// Empty block?
@@ -92,3 +109,29 @@ func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
}
return y
}
+
+// Get the byte range in the ciphertext corresponding to blocks
+// (full blocks!)
+func (be *CryptFS) JoinCiphertextRange(blocks []intraBlock) (uint64, uint64) {
+
+ offset, _ := blocks[0].CiphertextRange()
+ last := blocks[len(blocks)-1]
+ length := (last.BlockNo - blocks[0].BlockNo + 1) * be.cipherBS
+
+ return offset, length
+}
+
+// Crop plaintext that correspons to complete cipher blocks down to what is
+// requested according to "iblocks"
+func (be *CryptFS) CropPlaintext(plaintext []byte, blocks []intraBlock) []byte {
+ offset := blocks[0].Offset
+ last := blocks[len(blocks)-1]
+ length := (last.BlockNo - blocks[0].BlockNo + 1) * be.plainBS
+ var cropped []byte
+ if offset + length > uint64(len(plaintext)) {
+ cropped = plaintext[offset:len(plaintext)]
+ } else {
+ cropped = plaintext[offset:offset+length]
+ }
+ return cropped
+}