@@ -102,38 +102,40 @@ namespace {
102102// Functions for encrypting and decrypting byte arrays.
103103// ---------------------------------------------------------------------------
104104
105- std::vector< uint8_t > BasicXorEncryptor::XorEncrypt (tcb::span<const uint8_t > data) {
106- if (data.empty ()) {
107- return {} ;
105+ void BasicXorEncryptor::XorEncryptInto (tcb::span<const uint8_t > data, tcb::span< uint8_t > out ) {
106+ if (data.size () != out. size ()) {
107+ throw InvalidInputException ( " XorEncryptInto: input and output sizes must match " ) ;
108108 }
109109 size_t key_hash = key_id_hash_;
110- std::vector<uint8_t > out (data.size ());
111110 for (size_t i = 0 ; i < data.size (); ++i) {
112111 out[i] = data[i] ^ (key_hash & 0xFF );
113112 key_hash = (key_hash << 1 ) | (key_hash >> 31 );
114113 }
115- return out;
116114}
117115
118- std::vector< uint8_t > BasicXorEncryptor::XorDecrypt (tcb::span<const uint8_t > data) {
119- return XorEncrypt (data);
116+ void BasicXorEncryptor::XorDecryptInto (tcb::span<const uint8_t > data, tcb::span< uint8_t > out ) {
117+ XorEncryptInto (data, out );
120118}
121119
122120// ---------------------------------------------------------------------------
123121// Block encryption
124122// ---------------------------------------------------------------------------
125123
126124std::vector<uint8_t > BasicXorEncryptor::EncryptBlock (tcb::span<const uint8_t > data) {
127- auto start = std::chrono::steady_clock::now ();
128- auto out = XorEncrypt (data, key_id_);
129- PrintBasicXorBlockTimings (" EncryptBlock" , ElapsedNanosecondsSince (start));
125+ if (data.empty ()) {
126+ return {};
127+ }
128+ std::vector<uint8_t > out (data.size ());
129+ XorEncryptInto (data, tcb::span<uint8_t >(out.data (), out.size ()));
130130 return out;
131131}
132132
133133std::vector<uint8_t > BasicXorEncryptor::DecryptBlock (tcb::span<const uint8_t > data) {
134- auto start = std::chrono::steady_clock::now ();
135- auto out = XorDecrypt (data, key_id_);
136- PrintBasicXorBlockTimings (" DecryptBlock" , ElapsedNanosecondsSince (start));
134+ if (data.empty ()) {
135+ return {};
136+ }
137+ std::vector<uint8_t > out (data.size ());
138+ XorDecryptInto (data, tcb::span<uint8_t >(out.data (), out.size ()));
137139 return out;
138140}
139141
@@ -169,7 +171,6 @@ std::vector<uint8_t> BasicXorEncryptor::DecryptBlock(tcb::span<const uint8_t> da
169171template <typename TypedBuffer>
170172std::vector<uint8_t > BasicXorEncryptor::EncryptTypedElements (
171173 const TypedBuffer& input_buffer) {
172- auto total_start = std::chrono::steady_clock::now ();
173174 constexpr bool is_fixed = TypedBuffer::is_fixed_sized;
174175 constexpr size_t prefix_length = is_fixed ? kFixedHeaderLength : kVariableHeaderLength ;
175176 const size_t num_elements = input_buffer.GetNumElements ();
@@ -188,80 +189,38 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptTypedElements(
188189
189190 std::vector<uint8_t > final_buffer;
190191 size_t element_size = 0 ;
191- int64_t encrypt_elements_loop_ns = 0 ;
192- int64_t get_raw_element_ns = 0 ;
193- int64_t xor_encrypt_ns = 0 ;
194- int64_t set_element_ns = 0 ;
195- int64_t finalize_buffer_ns = 0 ;
196192
197193 // Encrypt fixed-size elements
198194 if constexpr (is_fixed) {
199195 element_size = input_buffer.GetElementSize ();
200196 TypedBufferRawBytesFixedSized output_buffer{
201197 num_elements, prefix_length, RawBytesFixedSizedCodec{element_size}};
202- auto stage_start = std::chrono::steady_clock::now ();
203198 for (size_t i = 0 ; i < num_elements; ++i) {
204- auto op_start = std::chrono::steady_clock::now ();
205199 auto raw_bytes = input_buffer.GetRawElement (i);
206- get_raw_element_ns += ElapsedNanosecondsSince (op_start);
207- op_start = std::chrono::steady_clock::now ();
208- auto encrypted = XorEncrypt (raw_bytes, key_id);
209- xor_encrypt_ns += ElapsedNanosecondsSince (op_start);
210- op_start = std::chrono::steady_clock::now ();
211- output_buffer.SetElement (i, tcb::span<const uint8_t >(encrypted));
212- set_element_ns += ElapsedNanosecondsSince (op_start);
200+ auto write_span = output_buffer.GetWritableRawElement (i, raw_bytes.size ());
201+ XorEncryptInto (raw_bytes, write_span);
213202 }
214- encrypt_elements_loop_ns = ElapsedNanosecondsSince (stage_start);
215- stage_start = std::chrono::steady_clock::now ();
216203 final_buffer = output_buffer.FinalizeAndTakeBuffer ();
217- finalize_buffer_ns = ElapsedNanosecondsSince (stage_start);
218204 }
219205
220206 // Encrypt variable-size elements
221207 else {
222208 auto reserved_bytes_hint = input_buffer.GetRawBufferSize ();
223209 TypedBufferRawBytesVariableSized output_buffer{
224210 num_elements, reserved_bytes_hint, true , prefix_length};
225-
226- auto stage_start = std::chrono::steady_clock::now ();
227- for (size_t i = 0 ; i < num_elements; ++i) {
228-
229- auto op_start = std::chrono::steady_clock::now ();
230- auto raw_bytes = input_buffer.GetRawElement (i);
231- get_raw_element_ns += ElapsedNanosecondsSince (op_start);
232-
233- op_start = std::chrono::steady_clock::now ();
234- auto encrypted = XorEncrypt (raw_bytes, key_id);
235- xor_encrypt_ns += ElapsedNanosecondsSince (op_start);
236-
237- op_start = std::chrono::steady_clock::now ();
238- output_buffer.SetElement (i, tcb::span<const uint8_t >(encrypted));
239- set_element_ns += ElapsedNanosecondsSince (op_start);
211+ size_t output_index = 0 ;
212+ for (const auto raw_bytes : input_buffer.raw_elements ()) {
213+ auto write_span = output_buffer.GetWritableRawElement (output_index, raw_bytes.size ());
214+ XorEncryptInto (raw_bytes, write_span);
215+ output_index++;
240216 }
241- encrypt_elements_loop_ns = ElapsedNanosecondsSince (stage_start);
242- stage_start = std::chrono::steady_clock::now ();
243217 final_buffer = output_buffer.FinalizeAndTakeBuffer ();
244- finalize_buffer_ns = ElapsedNanosecondsSince (stage_start);
245218 }
246219
247220 // Write the header to the final buffer and return it.
248- auto header_start = std::chrono::steady_clock::now ();
249221 WriteHeader (final_buffer, {is_fixed,
250222 static_cast <uint32_t >(num_elements),
251223 static_cast <uint32_t >(element_size)});
252- auto write_header_ns = ElapsedNanosecondsSince (header_start);
253-
254- PrintBasicXorEncryptTypedElementsTimings (
255- is_fixed,
256- num_elements,
257- element_size,
258- encrypt_elements_loop_ns,
259- get_raw_element_ns,
260- xor_encrypt_ns,
261- set_element_ns,
262- finalize_buffer_ns,
263- write_header_ns,
264- ElapsedNanosecondsSince (total_start));
265224 return final_buffer;
266225}
267226
@@ -282,13 +241,9 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptValueList(
282241
283242 // std::visit extracts the concrete buffer type from the TypedValuesBuffer variant
284243 // and forwards it to EncryptTypedElements, which handles all buffer types generically.
285- auto visit_start = std::chrono::steady_clock::now ();
286- auto encrypted = std::visit ([&](const auto & input_buffer) {
287- return EncryptTypedElements (input_buffer, key_id_);
244+ return std::visit ([&](const auto & input_buffer) {
245+ return EncryptTypedElements (input_buffer);
288246 }, typed_buffer);
289- auto visit_dispatch_ns = ElapsedNanosecondsSince (visit_start);
290- PrintBasicXorEncryptValueListTimings (visit_dispatch_ns, ElapsedNanosecondsSince (total_start));
291- return encrypted;
292247}
293248
294249// ---------------------------------------------------------------------------
@@ -304,89 +259,43 @@ TypedBuffer BasicXorEncryptor::DecryptFixedSizedElementsIntoTypedBuffer(
304259 const TypedBufferRawBytesFixedSized& encrypted_buffer, TypedBuffer output_buffer) {
305260 size_t output_index = 0 ;
306261 for (const auto raw_bytes : encrypted_buffer.raw_elements ()) {
307- auto decrypted_bytes = XorDecrypt ( raw_bytes);
308- output_buffer. SetRawElement (output_index, tcb::span< const uint8_t >(decrypted_bytes) );
262+ auto write_span = output_buffer. GetWritableRawElement (output_index, raw_bytes. size () );
263+ XorDecryptInto (raw_bytes, write_span );
309264 output_index++;
310265 }
311266 return output_buffer;
312267}
313268
314269TypedValuesBuffer BasicXorEncryptor::DecryptValueList (
315270 tcb::span<const uint8_t > encrypted_bytes) {
316- auto total_start = std::chrono::steady_clock::now ();
317-
318- auto stage_start = std::chrono::steady_clock::now ();
319271 auto header = ReadHeader (encrypted_bytes);
320- auto read_header_ns = ElapsedNanosecondsSince (stage_start);
321272 auto num_elements = static_cast <size_t >(header.num_elements );
322273
323274 // Decrypt fixed-size elements
324275 if (header.is_fixed ) {
325- stage_start = std::chrono::steady_clock::now ();
326-
327276 // Create a fixed-sized byte buffer for reading the encrypted elements.
328277 TypedBufferRawBytesFixedSized encrypted_buffer{
329278 encrypted_bytes, kFixedHeaderLength , RawBytesFixedSizedCodec{header.element_size }};
330- auto setup_buffer_ns = ElapsedNanosecondsSince (stage_start);
331-
332- // Populate a typed buffer with the decrypted elements in the corresponding type.
333- stage_start = std::chrono::steady_clock::now ();
334279 switch (datatype_) {
335280 case Type::INT32 :
336- {
337- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
338- encrypted_buffer, key_id_, TypedBufferI32{num_elements});
339- PrintBasicXorDecryptValueListTimings (
340- true , num_elements, read_header_ns, setup_buffer_ns,
341- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
342- return out;
343- }
281+ return DecryptFixedSizedElementsIntoTypedBuffer (
282+ encrypted_buffer, TypedBufferI32{num_elements});
344283 case Type::INT64 :
345- {
346- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
347- encrypted_buffer, key_id_, TypedBufferI64{num_elements});
348- PrintBasicXorDecryptValueListTimings (
349- true , num_elements, read_header_ns, setup_buffer_ns,
350- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
351- return out;
352- }
284+ return DecryptFixedSizedElementsIntoTypedBuffer (
285+ encrypted_buffer, TypedBufferI64{num_elements});
353286 case Type::INT96 :
354- {
355- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
356- encrypted_buffer, key_id_, TypedBufferInt96{num_elements});
357- PrintBasicXorDecryptValueListTimings (
358- true , num_elements, read_header_ns, setup_buffer_ns,
359- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
360- return out;
361- }
287+ return DecryptFixedSizedElementsIntoTypedBuffer (
288+ encrypted_buffer, TypedBufferInt96{num_elements});
362289 case Type::FLOAT :
363- {
364- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
365- encrypted_buffer, key_id_, TypedBufferFloat{num_elements});
366- PrintBasicXorDecryptValueListTimings (
367- true , num_elements, read_header_ns, setup_buffer_ns,
368- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
369- return out;
370- }
290+ return DecryptFixedSizedElementsIntoTypedBuffer (
291+ encrypted_buffer, TypedBufferFloat{num_elements});
371292 case Type::DOUBLE :
372- {
373- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
374- encrypted_buffer, key_id_, TypedBufferDouble{num_elements});
375- PrintBasicXorDecryptValueListTimings (
376- true , num_elements, read_header_ns, setup_buffer_ns,
377- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
378- return out;
379- }
293+ return DecryptFixedSizedElementsIntoTypedBuffer (
294+ encrypted_buffer, TypedBufferDouble{num_elements});
380295 case Type::FIXED_LEN_BYTE_ARRAY :
381- {
382- auto out = DecryptFixedSizedElementsIntoTypedBuffer (
383- encrypted_buffer, key_id_,
296+ return DecryptFixedSizedElementsIntoTypedBuffer (
297+ encrypted_buffer,
384298 TypedBufferRawBytesFixedSized{num_elements, 0 , RawBytesFixedSizedCodec{header.element_size }});
385- PrintBasicXorDecryptValueListTimings (
386- true , num_elements, read_header_ns, setup_buffer_ns,
387- ElapsedNanosecondsSince (stage_start), ElapsedNanosecondsSince (total_start));
388- return out;
389- }
390299 default :
391300 throw InvalidInputException (
392301 std::string (" DecryptValueList: unsupported fixed-size datatype: " )
@@ -396,27 +305,20 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList(
396305
397306 // Decrypt variable-size elements
398307 else {
399- stage_start = std::chrono::steady_clock::now ();
400308 // Create a variable-sized byte buffer for reading the encrypted elements.
401309 TypedBufferRawBytesVariableSized encrypted_buffer{ encrypted_bytes, kVariableHeaderLength };
402- auto setup_buffer_ns = ElapsedNanosecondsSince (stage_start);
403310
404311 switch (datatype_) {
405312 // Create a BYTE-ARRAY typed buffer for storing the decrypted elements.
406313 case Type::BYTE_ARRAY : {
407314 auto reserved_bytes_hint = encrypted_buffer.GetRawBufferSize ();
408315 TypedBufferRawBytesVariableSized output_buffer{num_elements, reserved_bytes_hint, true };
409316 size_t output_index = 0 ;
410- stage_start = std::chrono::steady_clock::now ();
411317 for (const auto element : encrypted_buffer) {
412- auto decrypted_bytes = XorDecrypt ( element);
413- output_buffer. SetElement (output_index, tcb::span< const uint8_t >(decrypted_bytes) );
318+ auto write_span = output_buffer. GetWritableRawElement (output_index, element. size () );
319+ XorDecryptInto (element, write_span );
414320 output_index++;
415321 }
416- auto decrypt_elements_ns = ElapsedNanosecondsSince (stage_start);
417- PrintBasicXorDecryptValueListTimings (
418- false , num_elements, read_header_ns, setup_buffer_ns,
419- decrypt_elements_ns, ElapsedNanosecondsSince (total_start));
420322 return output_buffer;
421323 }
422324 default :
0 commit comments