PostLAProtobufNativeTransforms.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Created by miti on 27/07/19.
  3. //
  4. #include "UntrustedInclude/PostLAProtobufNativeTransforms.h"
  5. #include <string.h>
  6. void PostLAProtobufNativeTransforms::get_lengths_for_protobuf_serialized_array(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
  7. uint32_t *op_ciphertext_length, uint32_t *op_number_of_ciphertext_fields)
  8. {
  9. uint32_t number_of_ciphertext_fields, counter, total_length;
  10. // Didn't use bytesize() or bytesizelong() for getting the lengths of the public key or the ciphertext string
  11. // as that gives the *serialized* length of the message which should be an upper-bound.
  12. // Can switch to that if necessary for time performance reasons.
  13. total_length=protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
  14. number_of_ciphertext_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
  15. for(counter=0; counter<number_of_ciphertext_fields; counter++)
  16. total_length+=protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
  17. *op_ciphertext_length=total_length;
  18. *op_number_of_ciphertext_fields=number_of_ciphertext_fields;
  19. }
  20. void PostLAProtobufNativeTransforms::create_array_from_protobuf(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
  21. unsigned char* double_ciphertext_client_data, uint32_t* sizes_array,
  22. uint32_t* sizes_array_length)
  23. {
  24. uint32_t counter, size_of_field, number_of_fields;
  25. unsigned char* ptr;
  26. // Set the size of the first element - the public key - and copy it to the output array.
  27. sizes_array[0] = protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
  28. memcpy(double_ciphertext_client_data, protobuf_ext_to_decryptor.ciphertext_client_public_key().c_str(),
  29. sizes_array[0]);
  30. ptr=double_ciphertext_client_data + sizes_array[0];
  31. // Start copying past the length copied above, copy all ciphertext fields to the output string array
  32. // and set their lengths in the output integers array
  33. number_of_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
  34. for(counter=0;counter<number_of_fields;counter++)
  35. {
  36. // First element of the LHS array is the length of the client's public key.
  37. size_of_field = protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
  38. memcpy(ptr, protobuf_ext_to_decryptor.ciphertext_fields(counter).field().c_str(), size_of_field);
  39. sizes_array[counter + 1] = size_of_field;
  40. ptr += sizes_array[counter + 1];
  41. }
  42. *sizes_array_length=number_of_fields+1;
  43. }
  44. void PostLAProtobufNativeTransforms::create_protobuf_from_array( unsigned char* ciphertext_client_data, uint32_t* sizes_array, uint32_t sizes_array_length,
  45. extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg)
  46. {
  47. uint32_t counter;
  48. unsigned char* ptr;
  49. // Note that we don't care about setting the client public key as we don't include that in the outgoing message
  50. // to the extension.
  51. ptr=ciphertext_client_data;
  52. for(counter=0; counter<sizes_array_length; counter++)
  53. {
  54. protobuf_extension_decryptor_msg.mutable_ciphertext_fields(counter)->set_field(ptr, sizes_array[counter]);
  55. ptr+=sizes_array[counter];
  56. }
  57. }