Fix seeking while reading from encrypted disk.

This commit is contained in:
Vitaly Baranov 2022-07-28 22:43:51 +02:00
parent 3b6bacbf0b
commit 2f3b27d1ac
3 changed files with 6 additions and 5 deletions

View File

@ -80,6 +80,7 @@ public:
/// the initialization vector is increased by an index of the current block
/// and the index of the current block is calculated from this offset.
void setOffset(size_t offset_) { offset = offset_; }
size_t getOffset() const { return offset; }
/// Encrypts some data.
/// Also the function moves `offset` by `size` (for successive encryptions).

View File

@ -21,7 +21,6 @@ ReadBufferFromEncryptedFile::ReadBufferFromEncryptedFile(
, encryptor(header_.algorithm, key_, header_.init_vector)
{
offset = offset_;
encryptor.setOffset(offset_);
need_seek = true;
}
@ -60,9 +59,6 @@ off_t ReadBufferFromEncryptedFile::seek(off_t off, int whence)
assert(!hasPendingData());
}
/// The encryptor always needs to know what the current offset is.
encryptor.setOffset(new_pos);
return new_pos;
}
@ -94,6 +90,10 @@ bool ReadBufferFromEncryptedFile::nextImpl()
/// The used cipher algorithms generate the same number of bytes in output as it were in input,
/// so after deciphering the numbers of bytes will be still `bytes_read`.
working_buffer.resize(bytes_read);
/// The decryptor needs to know what the current offset is (because it's used in the decryption algorithm).
encryptor.setOffset(offset);
encryptor.decrypt(encrypted_buffer.data(), bytes_read, working_buffer.begin());
offset += bytes_read;

View File

@ -242,7 +242,7 @@ TEST(FileEncryptionPositionUpdateTest, Decryption)
rb.ignore(5);
rb.ignore(5);
ASSERT_EQ(rb.getPosition(), 15);
String res;
readStringUntilEOF(res, rb);
ASSERT_EQ(res, data.substr(15));