PostLAMessaging.cpp 4.2 KB

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