summaryrefslogtreecommitdiff
path: root/cryptfs
diff options
context:
space:
mode:
Diffstat (limited to 'cryptfs')
-rw-r--r--cryptfs/content_test.go26
-rw-r--r--cryptfs/cryptfs_content.go9
2 files changed, 31 insertions, 4 deletions
diff --git a/cryptfs/content_test.go b/cryptfs/content_test.go
index 1900818..b53eeeb 100644
--- a/cryptfs/content_test.go
+++ b/cryptfs/content_test.go
@@ -32,3 +32,29 @@ func TestSplitRange(t *testing.T) {
}
}
}
+
+func TestCiphertextRange(t *testing.T) {
+ var ranges []testRange
+
+ ranges = append(ranges, testRange{0, 70000},
+ testRange{0, 10},
+ testRange{234, 6511},
+ testRange{65444, 54},
+ testRange{6654, 8945})
+
+ var key [16]byte
+ f := NewCryptFS(key, true)
+
+ for _, r := range(ranges) {
+ alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length)
+ if alignedLength < r.length {
+ t.Fail()
+ }
+ if alignedOffset % f.cipherBS != 0 {
+ t.Fail()
+ }
+ if r.offset % f.plainBS != 0 && skipBytes == 0 {
+ t.Fail()
+ }
+ }
+}
diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index e23d9b5..b9dd61f 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -115,7 +115,7 @@ func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
// CiphertextRange - Get byte range in backing ciphertext corresponding
// to plaintext range. Returns a range aligned to ciphertext blocks.
-func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64, int) {
+func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (alignedOffset uint64, alignedLength uint64, skipBytes int) {
// Decrypting the ciphertext will yield too many plaintext bytes. Skip this number
// of bytes from the front.
skip := offset % be.plainBS
@@ -123,10 +123,11 @@ func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64
firstBlockNo := offset / be.plainBS
lastBlockNo := ( offset + length - 1 ) / be.plainBS
- alignedOffset := firstBlockNo * be.cipherBS
- alignedLength := (lastBlockNo - firstBlockNo + 1) * be.cipherBS
+ alignedOffset = firstBlockNo * be.cipherBS
+ alignedLength = (lastBlockNo - firstBlockNo + 1) * be.cipherBS
- return alignedOffset, alignedLength, int(skip)
+ skipBytes = int(skip)
+ return alignedOffset, alignedLength, skipBytes
}
// Get the byte range in the ciphertext corresponding to blocks