2016-02-06 19:20:54 +01:00
|
|
|
package contentenc
|
2015-11-01 12:11:36 +01:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
import (
|
2016-09-02 21:01:03 +02:00
|
|
|
"log"
|
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-02-06 19:20:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Contentenc methods that translate offsets between ciphertext and plaintext
|
2015-11-01 12:11:36 +01:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// PlainOffToBlockNo converts a plaintext offset to the ciphertext block number.
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) PlainOffToBlockNo(plainOffset uint64) uint64 {
|
2015-11-01 12:11:36 +01:00
|
|
|
return plainOffset / be.plainBS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// CipherOffToBlockNo converts the ciphertext offset to the plaintext block number.
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) CipherOffToBlockNo(cipherOffset uint64) uint64 {
|
2016-10-02 06:14:18 +02:00
|
|
|
if cipherOffset < HeaderLen {
|
2016-09-02 21:01:03 +02:00
|
|
|
log.Panicf("BUG: offset %d is inside the file header", cipherOffset)
|
2016-08-30 00:20:31 +02:00
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
return (cipherOffset - HeaderLen) / be.cipherBS
|
2015-11-01 12:11:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// BlockNoToCipherOff gets the ciphertext offset of block "blockNo"
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) BlockNoToCipherOff(blockNo uint64) uint64 {
|
2016-10-02 06:14:18 +02:00
|
|
|
return HeaderLen + blockNo*be.cipherBS
|
2015-11-01 12:11:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// BlockNoToPlainOff gets the plaintext offset of block "blockNo"
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) BlockNoToPlainOff(blockNo uint64) uint64 {
|
2015-11-01 12:11:36 +01:00
|
|
|
return blockNo * be.plainBS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// CipherSizeToPlainSize calculates the plaintext size from a ciphertext size
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) CipherSizeToPlainSize(cipherSize uint64) uint64 {
|
2016-08-29 21:55:10 +02:00
|
|
|
// Zero-sized files stay zero-sized
|
2015-11-01 12:11:36 +01:00
|
|
|
if cipherSize == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
if cipherSize == HeaderLen {
|
2017-05-01 18:44:18 +02:00
|
|
|
// This can happen between createHeader() and Write() and is harmless.
|
|
|
|
tlog.Debug.Printf("cipherSize %d == header size: interrupted write?\n", cipherSize)
|
2015-11-15 14:15:21 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
if cipherSize < HeaderLen {
|
|
|
|
tlog.Warn.Printf("cipherSize %d < header size %d: corrupt file\n", cipherSize, HeaderLen)
|
2015-11-15 14:15:21 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-11-01 12:11:36 +01:00
|
|
|
// Block number at last byte
|
|
|
|
blockNo := be.CipherOffToBlockNo(cipherSize - 1)
|
|
|
|
blockCount := blockNo + 1
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
overhead := be.BlockOverhead()*blockCount + HeaderLen
|
2015-11-01 12:11:36 +01:00
|
|
|
|
2017-03-06 23:50:17 +01:00
|
|
|
if overhead > cipherSize {
|
|
|
|
tlog.Warn.Printf("cipherSize %d < overhead %d: corrupt file\n", cipherSize, overhead)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-11-01 12:11:36 +01:00
|
|
|
return cipherSize - overhead
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) PlainSizeToCipherSize(plainSize uint64) uint64 {
|
2016-08-29 21:55:10 +02:00
|
|
|
// Zero-sized files stay zero-sized
|
|
|
|
if plainSize == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2015-11-01 12:11:36 +01:00
|
|
|
|
|
|
|
// Block number at last byte
|
|
|
|
blockNo := be.PlainOffToBlockNo(plainSize - 1)
|
|
|
|
blockCount := blockNo + 1
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
overhead := be.BlockOverhead()*blockCount + HeaderLen
|
2015-11-01 12:11:36 +01:00
|
|
|
|
|
|
|
return plainSize + overhead
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ExplodePlainRange splits a plaintext byte range into (possibly partial) blocks
|
2016-07-01 23:29:31 +02:00
|
|
|
// Returns an empty slice if length == 0.
|
2016-10-02 06:14:18 +02:00
|
|
|
func (be *ContentEnc) ExplodePlainRange(offset uint64, length uint64) []IntraBlock {
|
|
|
|
var blocks []IntraBlock
|
|
|
|
var nextBlock IntraBlock
|
2015-11-01 12:11:36 +01:00
|
|
|
nextBlock.fs = be
|
|
|
|
|
|
|
|
for length > 0 {
|
|
|
|
nextBlock.BlockNo = be.PlainOffToBlockNo(offset)
|
|
|
|
nextBlock.Skip = offset - be.BlockNoToPlainOff(nextBlock.BlockNo)
|
|
|
|
|
2016-07-02 15:33:48 +02:00
|
|
|
// Minimum of remaining plaintext data and remaining space in the block
|
2015-11-01 12:11:36 +01:00
|
|
|
nextBlock.Length = MinUint64(length, be.plainBS-nextBlock.Skip)
|
|
|
|
|
|
|
|
blocks = append(blocks, nextBlock)
|
|
|
|
offset += nextBlock.Length
|
|
|
|
length -= nextBlock.Length
|
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ExplodeCipherRange splits a ciphertext byte range into (possibly partial)
|
|
|
|
// blocks This is used in reverse mode when reading files
|
|
|
|
func (be *ContentEnc) ExplodeCipherRange(offset uint64, length uint64) []IntraBlock {
|
|
|
|
var blocks []IntraBlock
|
|
|
|
var nextBlock IntraBlock
|
2016-08-30 00:20:31 +02:00
|
|
|
nextBlock.fs = be
|
|
|
|
|
|
|
|
for length > 0 {
|
|
|
|
nextBlock.BlockNo = be.CipherOffToBlockNo(offset)
|
|
|
|
nextBlock.Skip = offset - be.BlockNoToCipherOff(nextBlock.BlockNo)
|
|
|
|
|
|
|
|
// This block can carry up to "maxLen" payload bytes
|
|
|
|
maxLen := be.cipherBS - nextBlock.Skip
|
|
|
|
nextBlock.Length = maxLen
|
|
|
|
// But if the user requested less, we truncate the block to "length".
|
|
|
|
if length < maxLen {
|
|
|
|
nextBlock.Length = length
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks = append(blocks, nextBlock)
|
|
|
|
offset += nextBlock.Length
|
|
|
|
length -= nextBlock.Length
|
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// BlockOverhead returns the per-block overhead.
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *ContentEnc) BlockOverhead() uint64 {
|
|
|
|
return be.cipherBS - be.plainBS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// MinUint64 returns the minimum of two uint64 values.
|
2015-11-01 12:11:36 +01:00
|
|
|
func MinUint64(x uint64, y uint64) uint64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|