diff --git a/src/processing/encryptors/basic_xor_encryptor.cpp b/src/processing/encryptors/basic_xor_encryptor.cpp index 44b8d18..8ac3386 100644 --- a/src/processing/encryptors/basic_xor_encryptor.cpp +++ b/src/processing/encryptors/basic_xor_encryptor.cpp @@ -20,7 +20,6 @@ #include "../../common/exceptions.h" #include "../../common/enum_utils.h" #include -#include #include #include @@ -31,16 +30,12 @@ using namespace dbps::external; // Functions for encrypting and decrypting byte arrays. // --------------------------------------------------------------------------- -std::vector BasicXorEncryptor::XorEncrypt(tcb::span data, const std::string& key_id) { +std::vector BasicXorEncryptor::XorEncrypt(tcb::span data) { if (data.empty()) { return {}; } - if (key_id.empty()) { - throw std::invalid_argument("XorEncrypt: key must not be empty for non-empty data"); - } + size_t key_hash = key_id_hash_; std::vector out(data.size()); - std::hash hasher; - size_t key_hash = hasher(key_id); for (size_t i = 0; i < data.size(); ++i) { out[i] = data[i] ^ (key_hash & 0xFF); key_hash = (key_hash << 1) | (key_hash >> 31); @@ -48,8 +43,8 @@ std::vector BasicXorEncryptor::XorEncrypt(tcb::span data return out; } -std::vector BasicXorEncryptor::XorDecrypt(tcb::span data, const std::string& key_id) { - return XorEncrypt(data, key_id); +std::vector BasicXorEncryptor::XorDecrypt(tcb::span data) { + return XorEncrypt(data); } // --------------------------------------------------------------------------- @@ -57,11 +52,11 @@ std::vector BasicXorEncryptor::XorDecrypt(tcb::span data // --------------------------------------------------------------------------- std::vector BasicXorEncryptor::EncryptBlock(tcb::span data) { - return XorEncrypt(data, key_id_); + return XorEncrypt(data); } std::vector BasicXorEncryptor::DecryptBlock(tcb::span data) { - return XorDecrypt(data, key_id_); + return XorDecrypt(data); } // --------------------------------------------------------------------------- @@ -95,7 +90,7 @@ std::vector BasicXorEncryptor::DecryptBlock(tcb::span da template std::vector BasicXorEncryptor::EncryptTypedElements( - const TypedBuffer& input_buffer, const std::string& key_id) { + const TypedBuffer& input_buffer) { constexpr bool is_fixed = TypedBuffer::is_fixed_sized; constexpr size_t prefix_length = is_fixed ? kFixedHeaderLength : kVariableHeaderLength; const size_t num_elements = input_buffer.GetNumElements(); @@ -122,7 +117,7 @@ std::vector BasicXorEncryptor::EncryptTypedElements( num_elements, prefix_length, RawBytesFixedSizedCodec{element_size}}; size_t output_index = 0; for (const auto raw_bytes : input_buffer.raw_elements()) { - auto encrypted = XorEncrypt(raw_bytes, key_id); + auto encrypted = XorEncrypt(raw_bytes); output_buffer.SetElement(output_index, tcb::span(encrypted)); output_index++; } @@ -136,7 +131,7 @@ std::vector BasicXorEncryptor::EncryptTypedElements( num_elements, reserved_bytes_hint, true, prefix_length}; size_t output_index = 0; for (const auto raw_bytes : input_buffer.raw_elements()) { - auto encrypted = XorEncrypt(raw_bytes, key_id); + auto encrypted = XorEncrypt(raw_bytes); output_buffer.SetElement(output_index, tcb::span(encrypted)); output_index++; } @@ -167,7 +162,7 @@ std::vector BasicXorEncryptor::EncryptValueList( // std::visit extracts the concrete buffer type from the TypedValuesBuffer variant // and forwards it to EncryptTypedElements, which handles all buffer types generically. return std::visit([&](const auto& input_buffer) { - return EncryptTypedElements(input_buffer, key_id_); + return EncryptTypedElements(input_buffer); }, typed_buffer); } @@ -181,10 +176,10 @@ std::vector BasicXorEncryptor::EncryptValueList( // Helper function to decrypt fixed-size elements into the output TypedBuffer type. template TypedBuffer BasicXorEncryptor::DecryptFixedSizedElementsIntoTypedBuffer( - const TypedBufferRawBytesFixedSized& encrypted_buffer, const std::string& key_id, TypedBuffer output_buffer) { + const TypedBufferRawBytesFixedSized& encrypted_buffer, TypedBuffer output_buffer) { size_t output_index = 0; for (const auto raw_bytes : encrypted_buffer.raw_elements()) { - auto decrypted_bytes = XorDecrypt(raw_bytes, key_id); + auto decrypted_bytes = XorDecrypt(raw_bytes); output_buffer.SetRawElement(output_index, tcb::span(decrypted_bytes)); output_index++; } @@ -208,22 +203,22 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList( switch (datatype_) { case Type::INT32: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, TypedBufferI32{num_elements}); + encrypted_buffer, TypedBufferI32{num_elements}); case Type::INT64: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, TypedBufferI64{num_elements}); + encrypted_buffer, TypedBufferI64{num_elements}); case Type::INT96: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, TypedBufferInt96{num_elements}); + encrypted_buffer, TypedBufferInt96{num_elements}); case Type::FLOAT: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, TypedBufferFloat{num_elements}); + encrypted_buffer, TypedBufferFloat{num_elements}); case Type::DOUBLE: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, TypedBufferDouble{num_elements}); + encrypted_buffer, TypedBufferDouble{num_elements}); case Type::FIXED_LEN_BYTE_ARRAY: return DecryptFixedSizedElementsIntoTypedBuffer( - encrypted_buffer, key_id_, + encrypted_buffer, TypedBufferRawBytesFixedSized{num_elements, 0, RawBytesFixedSizedCodec{header.element_size}}); default: throw InvalidInputException( @@ -244,7 +239,7 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList( TypedBufferRawBytesVariableSized output_buffer{num_elements, reserved_bytes_hint, true}; size_t output_index = 0; for (const auto element : encrypted_buffer) { - auto decrypted_bytes = XorDecrypt(element, key_id_); + auto decrypted_bytes = XorDecrypt(element); output_buffer.SetElement(output_index, tcb::span(decrypted_bytes)); output_index++; } diff --git a/src/processing/encryptors/basic_xor_encryptor.h b/src/processing/encryptors/basic_xor_encryptor.h index ef8f1b2..2e30354 100644 --- a/src/processing/encryptors/basic_xor_encryptor.h +++ b/src/processing/encryptors/basic_xor_encryptor.h @@ -48,7 +48,8 @@ class DBPS_EXPORT BasicXorEncryptor : public DBPSEncryptor { const std::string& user_id, const std::string& application_context, dbps::external::Type::type datatype) - : DBPSEncryptor(key_id, column_name, user_id, application_context, datatype) {} + : DBPSEncryptor(key_id, column_name, user_id, application_context, datatype), + key_id_hash_(std::hash{}(key_id)) {} ~BasicXorEncryptor() override = default; @@ -63,16 +64,17 @@ class DBPS_EXPORT BasicXorEncryptor : public DBPSEncryptor { TypedValuesBuffer DecryptValueList(tcb::span encrypted_bytes) override; private: - static std::vector XorEncrypt(tcb::span data, const std::string& key_id); - static std::vector XorDecrypt(tcb::span data, const std::string& key_id); + const size_t key_id_hash_; + + std::vector XorEncrypt(tcb::span data); + std::vector XorDecrypt(tcb::span data); template - static std::vector EncryptTypedElements( - const InputBuffer& input_buffer, const std::string& key_id); + std::vector EncryptTypedElements(const InputBuffer& input_buffer); template - static TypedBuffer DecryptFixedSizedElementsIntoTypedBuffer( + TypedBuffer DecryptFixedSizedElementsIntoTypedBuffer( const TypedBufferRawBytesFixedSized& encrypted_buffer, - const std::string& key_id, TypedBuffer output_buffer); + TypedBuffer output_buffer); };