Skip to content

Commit 17ab02e

Browse files
committed
- Splitting the encryption/decryption process into separate methods for level and value bytes.
- Updating the basic encryptor to encrypt/decrypt value lists - Updated the encryption sequencer to split/join the value+level bytes explicitly.
1 parent 211f15d commit 17ab02e

6 files changed

Lines changed: 80 additions & 49 deletions

File tree

src/server/encryption_sequencer.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,10 @@ static std::unique_ptr<DBPSEncryptor> CreateEncryptor(
4848
const std::string& key_id,
4949
const std::string& column_name,
5050
const std::string& user_id,
51-
const std::string& application_context,
52-
const std::string& encryptor_type) {
51+
const std::string& application_context) {
5352

5453
// Return a BasicEncryptor instance.
55-
if (encryptor_type == "basic") {
56-
return std::make_unique<BasicEncryptor>(key_id, column_name, user_id, application_context);
57-
}
58-
// TODO: Add other encryptor types here (e.g., ProtegrityEncryptor).
59-
throw DBPSUnsupportedException("Unsupported encryptor type: " + encryptor_type);
54+
return std::make_unique<BasicEncryptor>(key_id, column_name, user_id, application_context);
6055
}
6156

6257
// Constructor implementation
@@ -83,7 +78,34 @@ DataBatchEncryptionSequencer::DataBatchEncryptionSequencer(
8378
user_id_(user_id),
8479
application_context_(application_context),
8580
encryption_metadata_(encryption_metadata),
86-
encryptor_(CreateEncryptor(key_id, column_name, user_id, application_context, "basic")) {}
81+
encryptor_(CreateEncryptor(key_id, column_name, user_id, application_context)) {}
82+
83+
// Constructor with pre-built encryptor
84+
DataBatchEncryptionSequencer::DataBatchEncryptionSequencer(
85+
const std::string& column_name,
86+
Type::type datatype,
87+
const std::optional<int>& datatype_length,
88+
CompressionCodec::type compression,
89+
Format::type format,
90+
const std::map<std::string, std::string>& encoding_attributes,
91+
CompressionCodec::type encrypted_compression,
92+
const std::string& key_id,
93+
const std::string& user_id,
94+
const std::string& application_context,
95+
const std::map<std::string, std::string>& encryption_metadata,
96+
std::unique_ptr<DBPSEncryptor> encryptor
97+
) : column_name_(column_name),
98+
datatype_(datatype),
99+
datatype_length_(datatype_length),
100+
compression_(compression),
101+
format_(format),
102+
encoding_attributes_(encoding_attributes),
103+
encrypted_compression_(encrypted_compression),
104+
key_id_(key_id),
105+
user_id_(user_id),
106+
application_context_(application_context),
107+
encryption_metadata_(encryption_metadata),
108+
encryptor_(std::move(encryptor)) {}
87109

88110
// Top level encryption/decryption methods.
89111

@@ -108,11 +130,13 @@ bool DataBatchEncryptionSequencer::ConvertAndEncrypt(const std::vector<uint8_t>&
108130
// Parse value bytes into typed list
109131
auto typed_list = ParseValueBytesIntoTypedList(value_bytes, datatype_, datatype_length_, format_);
110132

111-
// Encrypt the typed list and level bytes
112-
auto encrypted_bytes = encryptor_->EncryptValueList(typed_list, level_bytes);
133+
// Encrypt the typed list and level bytes, then join them into a single encrypted byte vector.
134+
auto encrypted_value_bytes = encryptor_->EncryptValueList(typed_list);
135+
auto encrypted_level_bytes = encryptor_->EncryptBlock(level_bytes);
136+
auto joined_encrypted_bytes = JoinWithLengthPrefix(encrypted_level_bytes, encrypted_value_bytes);
113137

114-
// Compress the encrypted bytes
115-
encrypted_result_ = Compress(encrypted_bytes, encrypted_compression_);
138+
// Compress the joined encrypted bytes
139+
encrypted_result_ = Compress(joined_encrypted_bytes, encrypted_compression_);
116140

117141
} catch (const DBPSUnsupportedException& e) {
118142
// If any stage is as of yet unsupported, default to whole payload (per-block) encryption
@@ -175,13 +199,15 @@ bool DataBatchEncryptionSequencer::ConvertAndDecrypt(const std::vector<uint8_t>&
175199
// Decompress the encrypted bytes
176200
auto decompressed_encrypted_bytes = Decompress(ciphertext, encrypted_compression_);
177201

178-
// Decrypt the typed list and level bytes
179-
auto [typed_list, level_bytes] = encryptor_->DecryptValueList(decompressed_encrypted_bytes);
202+
// Split the joined encrypted bytes, then decrypt the level and value bytes separately.
203+
auto [encrypted_level_bytes, encrypted_value_bytes] = SplitWithLengthPrefix(decompressed_encrypted_bytes);
204+
auto level_bytes = encryptor_->DecryptBlock(encrypted_level_bytes);
205+
auto typed_list = encryptor_->DecryptValueList(encrypted_value_bytes);
180206

181-
// Convert typed list back to value bytes
207+
// Convert the decrypted typed list back to value bytes
182208
auto value_bytes = GetTypedListAsValueBytes(typed_list, datatype_, datatype_length_, format_);
183209

184-
// Join level and value bytes and compress to get plaintext
210+
// Join the decrypted level and value bytes, then compress to get plaintext
185211
decrypted_result_ = CompressAndJoin(level_bytes, value_bytes);
186212
}
187213

src/server/encryption_sequencer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ class DataBatchEncryptionSequencer {
7979
const std::map<std::string, std::string>& encryption_metadata
8080
);
8181

82+
// Constructor with pre-built encryptor (for dependency injection)
83+
DataBatchEncryptionSequencer(
84+
const std::string& column_name,
85+
Type::type datatype,
86+
const std::optional<int>& datatype_length,
87+
CompressionCodec::type compression,
88+
Format::type format,
89+
const std::map<std::string, std::string>& encoding_attributes,
90+
CompressionCodec::type encrypted_compression,
91+
const std::string& key_id,
92+
const std::string& user_id,
93+
const std::string& application_context,
94+
const std::map<std::string, std::string>& encryption_metadata,
95+
std::unique_ptr<DBPSEncryptor> encryptor
96+
);
97+
8298
// Default constructor
8399
DataBatchEncryptionSequencer() = default;
84100

src/server/encryptors/basic_encryptor.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,29 @@ std::vector<uint8_t> BasicEncryptor::DecryptBlock(const std::vector<uint8_t>& da
4848
}
4949

5050
std::vector<uint8_t> BasicEncryptor::EncryptValueList(
51-
const TypedListValues& typed_list,
52-
const std::vector<uint8_t>& level_bytes) {
53-
51+
const TypedListValues& typed_list) {
52+
5453
// Printout the typed list.
5554
auto print_result = PrintTypedList(typed_list);
5655
if (print_result.length() > 1000) {
57-
std::cout << "Encrypt value - Decoded plaintext data (first 1000 chars):\n"
58-
<< print_result.substr(0, 1000) << "...";
56+
std::cout << "Encrypt value - Decoded plaintext data (first 1000 chars):\n"
57+
<< print_result.substr(0, 1000) << "...";
5958
} else {
6059
std::cout << "Encrypt value - Decoded plaintext data:\n" << print_result;
6160
}
6261

6362
// Printout the additional context parameters.
6463
std::cout << "Context parameters:\n"
65-
<< " column_name: " << column_name_ << "\n"
66-
<< " user_id: " << user_id_ << "\n"
67-
<< " key_id: " << key_id_ << "\n"
68-
<< " application_context: " << application_context_ << "\n"
69-
<< std::endl;
64+
<< " column_name: " << column_name_ << "\n"
65+
<< " user_id: " << user_id_ << "\n"
66+
<< " key_id: " << key_id_ << "\n"
67+
<< " application_context: " << application_context_ << "\n"
68+
<< std::endl;
7069

7170
throw DBPSUnsupportedException("EncryptTypedList not implemented");
7271
}
7372

74-
std::pair<TypedListValues, std::vector<uint8_t>> BasicEncryptor::DecryptValueList(
73+
TypedListValues BasicEncryptor::DecryptValueList(
7574
const std::vector<uint8_t>& encrypted_bytes) {
7675

7776
throw DBPSUnsupportedException("DecryptTypedList not implemented");

src/server/encryptors/basic_encryptor.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ class DBPS_EXPORT BasicEncryptor : public DBPSEncryptor {
5959

6060
// Value encryption methods
6161
std::vector<uint8_t> EncryptValueList(
62-
const TypedListValues& typed_list,
63-
const std::vector<uint8_t>& level_bytes) override;
62+
const TypedListValues& typed_list) override;
6463

65-
std::pair<TypedListValues, std::vector<uint8_t>> DecryptValueList(
64+
TypedListValues DecryptValueList(
6665
const std::vector<uint8_t>& encrypted_bytes) override;
6766
};
6867

src/server/encryptors/basic_encryptor_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ TEST(BasicEncryptor, EncryptBlock_DifferentKeys) {
5454

5555
TEST(BasicEncryptor, EncryptValueList_ThrowsException) {
5656
BasicEncryptor encryptor("test_key", "test_column", "test_user", "test_context");
57-
57+
5858
std::vector<int32_t> values = {1, 2, 3};
5959
TypedListValues typed_list = values;
60-
std::vector<uint8_t> level_bytes = {0, 1, 0};
61-
62-
EXPECT_THROW(encryptor.EncryptValueList(typed_list, level_bytes), DBPSUnsupportedException);
60+
61+
EXPECT_THROW(encryptor.EncryptValueList(typed_list), DBPSUnsupportedException);
6362
}
6463

6564
TEST(BasicEncryptor, DecryptValueList_ThrowsException) {

src/server/encryptors/dbps_encryptor.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,27 @@ class DBPS_EXPORT DBPSEncryptor {
8585
* The context-rich encryptor can use the additional parameters stored in the constructor:
8686
* column_name, user_id, key_id, application_context.
8787
*
88-
* This method encrypts individual values from a typed list (e.g., integers, floats, strings)
89-
* along with associated level bytes. The encrypted result combines both the encrypted
90-
* typed list values and level bytes into a single encrypted byte vector.
91-
*
92-
* Both the level_bytes AND list of values need to be encrypted and combined as a single encrypted
93-
* vector of bytes.
88+
* This method encrypts individual values from a typed list (e.g., integers, floats, strings).
9489
*
9590
* @param typed_list The typed list of values to encrypt (variant type supporting multiple data types)
96-
* @param level_bytes The level bytes associated with the typed list
97-
* @return The encrypted data as a vector of bytes containing both encrypted typed list and level bytes
91+
* @return The encrypted data as a vector of bytes containing the encrypted typed list values
9892
* @throws InvalidInputException if the input data is invalid or empty
9993
* @throws DBPSUnsupportedException if the encryption operation is not supported
10094
*/
10195
virtual std::vector<uint8_t> EncryptValueList(
102-
const TypedListValues& typed_list,
103-
const std::vector<uint8_t>& level_bytes) = 0;
96+
const TypedListValues& typed_list) = 0;
10497

10598
/**
10699
* Integration point: Decryption function based on encrypted bytes that will be implemented by Protegrity.
107100
*
108-
* This method decrypts the encrypted byte vector back into its constituent parts:
109-
* a typed list of values and associated level bytes.
101+
* This method decrypts the encrypted byte vector containing only the typed list values.
110102
*
111-
* @param encrypted_bytes The encrypted data as a vector of bytes
112-
* @return A pair containing the decrypted TypedListValues and level_bytes
103+
* @param encrypted_bytes The encrypted data as a vector of bytes containing only the encrypted typed list
104+
* @return The decrypted TypedListValues
113105
* @throws InvalidInputException if the input data is invalid, empty, or corrupted
114106
* @throws DBPSUnsupportedException if the decryption operation is not supported
115107
*/
116-
virtual std::pair<TypedListValues, std::vector<uint8_t>> DecryptValueList(
108+
virtual TypedListValues DecryptValueList(
117109
const std::vector<uint8_t>& encrypted_bytes) = 0;
118110

119111
protected:

0 commit comments

Comments
 (0)