PostLAMessaging.cpp 4.2 KB

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