Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/IO/FileEncryptionCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class Encryptor
/// 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).
Expand Down
8 changes: 4 additions & 4 deletions src/IO/ReadBufferFromEncryptedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ReadBufferFromEncryptedFile::ReadBufferFromEncryptedFile(
, encryptor(header_.algorithm, key_, header_.init_vector)
{
offset = offset_;
encryptor.setOffset(offset_);
need_seek = true;
}

Expand Down Expand Up @@ -59,6 +60,9 @@ 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;
}

Expand Down Expand Up @@ -90,10 +94,6 @@ 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());

pos = working_buffer.begin();
Expand Down
51 changes: 0 additions & 51 deletions src/IO/tests/gtest_file_encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
#include <gtest/gtest.h>
#include <IO/WriteBufferFromString.h>
#include <IO/FileEncryptionCommon.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/WriteBufferFromEncryptedFile.h>
#include <IO/ReadBufferFromEncryptedFile.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/ReadHelpers.h>
#include <Common/getRandomASCIIString.h>
#include <filesystem>


using namespace DB;
Expand Down Expand Up @@ -217,48 +210,4 @@ INSTANTIATE_TEST_SUITE_P(All,
})
);

TEST(FileEncryptionPositionUpdateTest, Decryption)
{
String tmp_path = std::filesystem::current_path() / "test_offset_update";
if (std::filesystem::exists(tmp_path))
std::filesystem::remove(tmp_path);

String key = "1234567812345678";
FileEncryption::Header header;
header.algorithm = Algorithm::AES_128_CTR;
header.key_id = 1;
header.key_hash = calculateKeyHash(key);
header.init_vector = InitVector::random();

auto lwb = std::make_unique<WriteBufferFromFile>(tmp_path);
WriteBufferFromEncryptedFile wb(10, std::move(lwb), key, header);
auto data = getRandomASCIIString(20);
wb.write(data.data(), data.size());
wb.finalize();

auto lrb = std::make_unique<ReadBufferFromFile>(tmp_path);
ReadBufferFromEncryptedFile rb(10, std::move(lrb), key, header);
rb.ignore(5);
rb.ignore(5);
rb.ignore(5);
ASSERT_EQ(rb.getPosition(), 15);

String res;
readStringUntilEOF(res, rb);
ASSERT_EQ(res, data.substr(15));
res.clear();

rb.seek(0, SEEK_SET);
ASSERT_EQ(rb.getPosition(), 0);
res.resize(5);
rb.read(res.data(), res.size());
ASSERT_EQ(res, data.substr(0, 5));
res.clear();

rb.seek(1, SEEK_CUR);
ASSERT_EQ(rb.getPosition(), 6);
readStringUntilEOF(res, rb);
ASSERT_EQ(res, data.substr(6));
}

#endif
18 changes: 0 additions & 18 deletions tests/integration/test_encrypted_disk/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,3 @@ def make_storage_policy_with_keys(policy_name, keys):
# Detach the part encrypted with the wrong key and check that another part containing "(2,'data'),(3,'data')" still can be read.
node.query("ALTER TABLE encrypted_test DETACH PART '{}'".format(FIRST_PART_NAME))
assert node.query(select_query) == "(2,'data'),(3,'data')"


def test_read_in_order():
node.query(
"CREATE TABLE encrypted_test(`a` UInt64, `b` String(150)) ENGINE = MergeTree() ORDER BY (a, b) SETTINGS storage_policy='encrypted_policy'"
)

node.query(
"INSERT INTO encrypted_test SELECT * FROM generateRandom('a UInt64, b FixedString(150)') LIMIT 100000"
)

node.query(
"SELECT * FROM encrypted_test ORDER BY a, b SETTINGS optimize_read_in_order=1 FORMAT Null"
)

node.query(
"SELECT * FROM encrypted_test ORDER BY a, b SETTINGS optimize_read_in_order=0 FORMAT Null"
)