PostLAMessaging.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Created by miti on 2019-12-24.
  3. //
  4. #include "PostLAMessaging.h"
  5. // The verifier doesn't receive any messages (in the deployment stage or at all)
  6. uint32_t send_secure_msg(uint8_t* input, uint32_t input_size)
  7. {
  8. uint8_t* output;
  9. uint32_t output_size, ret;
  10. google::protobuf::MessageLite protobuf_msg;
  11. output = (unsigned char*) malloc(output, input_size + 28); // 16 for tag, 12 for IV
  12. // TODO: Make sure this is the same as in the PHP extension code.
  13. ret = aes_gcm_wrapper(1, input, input_size, output, output_size );
  14. if(ret != 0)
  15. return ret;
  16. // TODO: Conversion logic to protobuf. Set msg or whatever.
  17. if(!protobufReaderWriter.write_msg(protobuf_msg))
  18. return 0x3;
  19. return 0;
  20. }
  21. void set_la_symmetric_key(uint8_t* given_key) {
  22. uint32_t counter;
  23. for(counter=0; counter<32; counter++)
  24. {
  25. key[counter] = given_key[counter];
  26. }
  27. }
  28. void set_fd(int given_fd)
  29. {
  30. protobufReaderWriter.set_fd(given_fd);
  31. }
  32. /*
  33. int encrypt_decrypt_msgs(int encrypt_decrypt, std::vector<std::string> &input_msgs,
  34. std::vector<std::string> &output_msgs)
  35. {
  36. unsigned char *input; unsigned char *output;
  37. uint32_t input_size, output_size, ret;
  38. output=NULL;
  39. for (std::string msg:input_msgs)
  40. {
  41. input_size = msg.length();
  42. input = (unsigned char*) msg.c_str();
  43. output = (unsigned char*) realloc(output, input_size + 28); // 16 for tag, 12 for IV
  44. ret = aes_gcm_wrapper(encrypt_decrypt, input, input_size, output, &output_size );
  45. if(ret!=0)
  46. {
  47. free(output);
  48. printf("Failed to encrypt an input field.\n"); fflush(stdout);
  49. return 0x2;
  50. }
  51. output_msgs.push_back(std::string(reinterpret_cast<const char *> (output), output_size));
  52. }
  53. free(output);
  54. return 0;
  55. }
  56. */
  57. /*
  58. * virtual void create_vector_from_protobuf(google::protobuf::MessageLite& protobuf_msg,
  59. std::vector<std::string> &native_msg_list) {}
  60. uint32_t receive_secure_msgs(std::vector<std::string> &plaintext_msg_list) {
  61. std::vector<std::string> ciphertext_msg_list;
  62. google::protobuf::MessageLite protobuf_msg;
  63. // read encrypted data
  64. if(!protobufReaderWriter.read_msg(protobuf_msg))
  65. {
  66. printf("Not all of the decryptor's message was read\n"); fflush(stdout);
  67. return 0xf3;
  68. }
  69. create_vector_from_protobuf(protobuf_msg, ciphertext_msg_list);
  70. return encrypt_decrypt_ciphertexts(0, &ciphertext_msg_list, plaintext_msg_list);
  71. }
  72. */
  73. /*
  74. uint32_t send_secure_msgs(std::vector<std::string> &plaintext_msg_list)
  75. {
  76. uint32_t ret;
  77. std::vector<std::string> ciphertext_msg_list;
  78. google::protobuf::MessageLite protobuf_msg;
  79. ret=encrypt_decrypt_msgs(1, plaintext_msg_list, &ciphertext_msg_list);
  80. if(ret!=0)
  81. return ret;
  82. create_protobuf_from_vector(ciphertext_msg_list, protobuf_msg);
  83. // write message to decryptor
  84. if(!protobufReaderWriter.write_msg(protobuf_msg))
  85. {
  86. printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
  87. return 0xfe;
  88. }
  89. return 0;
  90. }
  91. */