PostLAMessaging.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_headers_fd(int given_fd)
  47. {
  48. headersChannel.set_fd(given_fd);
  49. }
  50. void PostLAMessaging::set_data_fd(int given_fd)
  51. {
  52. dataChannel.set_fd(given_fd);
  53. }
  54. uint32_t PostLAMessaging::encrypt_decrypt_msgs(int encrypt_decrypt, std::vector<std::vector<unsigned char>>& input_msgs,
  55. std::vector<std::vector<unsigned char>>& output_msgs)
  56. {
  57. unsigned char *input, *output;
  58. uint32_t input_size, output_size, ret, counter;
  59. output=NULL;
  60. printf("In encrypt_decrypt_msgs\n"); fflush(stdout);
  61. for (auto msg:input_msgs)
  62. {
  63. printf("In the loop.\n"); fflush(stdout);
  64. input_size = msg.size();
  65. input = (unsigned char*) &msg[0];
  66. printf("Input length %d\n", input_size);
  67. for(counter=0; counter<input_size; counter++)
  68. printf("%02x ", input[counter]);
  69. printf("\n");
  70. output = (unsigned char*) realloc(output, input_size + 28); // 16 for tag, 12 for IV
  71. ret = aes_gcm_wrapper(encrypt_decrypt, input, input_size, output, &output_size );
  72. if(ret!=0)
  73. {
  74. free(output);
  75. printf("Failed to encrypt an input field.\n"); fflush(stdout);
  76. return ret;
  77. }
  78. printf("Output size %d\n", output_size);
  79. for(counter=0; counter<output_size; counter++)
  80. printf("%02x ", output[counter]);
  81. printf("\n");
  82. std::vector<unsigned char> output_field(output, output + output_size);
  83. output_msgs.push_back(output_field);
  84. }
  85. printf("Outside da loop.\n"); fflush(stdout);
  86. if(output != NULL)
  87. free(output);
  88. return 0;
  89. }
  90. void PostLAMessaging::create_vector_from_protobuf(decryptor_to_extension_msg &protobuf_msg,
  91. std::vector<std::vector<unsigned char>> &output_vector)
  92. {
  93. uint32_t counter, no_of_fields;
  94. std::string msg;
  95. no_of_fields=protobuf_msg.fields_size();
  96. for(counter=0; counter<no_of_fields; counter++)
  97. {
  98. msg = protobuf_msg.fields(counter).field();
  99. std::vector<unsigned char> output_field(msg.begin(), msg.end());
  100. output_vector.push_back(output_field);
  101. }
  102. }
  103. void PostLAMessaging::create_protobuf_from_vector(std::vector<std::vector<unsigned char>> &input_vector,
  104. extension_to_decryptor_msg &protobuf_msg)
  105. {
  106. unsigned char* input_ptr;
  107. std::vector<unsigned char> input_field;
  108. size_t input_field_length;
  109. input_field = input_vector.at(0);
  110. input_ptr = &input_field[0];
  111. input_field_length = input_field.size();
  112. protobuf_msg.set_ciphertext_client_public_key(input_ptr, input_field_length);
  113. input_vector.erase(input_vector.begin());
  114. for(auto input_field : input_vector) // counter=0; counter<no_of_fields; counter++
  115. {
  116. input_field_length = input_field.size();
  117. input_ptr = &input_field[0];
  118. protobuf_msg.add_fields()->set_field(input_ptr, input_field_length);
  119. }
  120. }
  121. uint32_t PostLAMessaging::receive_data_from_decryptor(std::vector<std::vector<unsigned char>> &plaintext_msg_list) {
  122. std::vector<std::vector<unsigned char>> ciphertext_msg_list;
  123. decryptor_to_extension_msg msg;
  124. // read encrypted data
  125. if(dataChannel.read_msg(msg) != 0)
  126. {
  127. printf("Not all of the decryptor's message was read\n"); fflush(stdout);
  128. return 0xf3;
  129. }
  130. printf("Read the msg from the decryptor.\n"); fflush(stdout);
  131. create_vector_from_protobuf(msg, ciphertext_msg_list);
  132. printf("created a vector\n"); fflush(stdout);
  133. return encrypt_decrypt_msgs(0, ciphertext_msg_list, plaintext_msg_list);
  134. }
  135. uint32_t PostLAMessaging::send_data_to_decryptor(std::vector<std::vector<unsigned char>> &plaintext_msg_list)
  136. {
  137. uint32_t ret;
  138. extension_to_decryptor_msg msg;
  139. std::vector<std::vector<unsigned char>> ciphertext_msg_list;
  140. ret=encrypt_decrypt_msgs(1, plaintext_msg_list, ciphertext_msg_list);
  141. if(ret!=0)
  142. return ret;
  143. printf("About to create a protobuf vector\n"); fflush(stdout);
  144. create_protobuf_from_vector(ciphertext_msg_list, msg);
  145. printf("About to write the message to the data channel.\n"); fflush(stdout);
  146. if(dataChannel.write_msg(msg) != 0)
  147. {
  148. printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
  149. return 0xfe;
  150. }
  151. return 0;
  152. }
  153. uint32_t PostLAMessaging::receive_header_from_decryptor(std::string &plaintext_header) {
  154. std::vector<std::vector<unsigned char>> ip_list, binary_list;
  155. unsigned char* binary_ptr;
  156. std::vector<unsigned char> binary_header;
  157. size_t binary_header_length;
  158. uint8_t* base64_ciphertext_header;
  159. int base64_ciphertext_header_length;
  160. uint32_t ret_status;
  161. printf("About to send-receive header.\n"); fflush(stdout);
  162. mitigator_header headerMsg;
  163. headerMsg.set_name("Name:", 5);
  164. headerMsg.set_value("!!Value", 7);
  165. if(headersChannel.write_msg(headerMsg) != 0)
  166. {
  167. printf("Error in writing msg for headers.\n"); fflush(stdout); return 0x1;
  168. }
  169. printf("Wrote a placeholder message to the header.\n"); fflush(stdout);
  170. if(headersChannel.read_msg(headerMsg) != 0)
  171. {
  172. printf("Error in reading msg for headers.\n"); fflush(stdout); return 0x2;
  173. }
  174. printf("Read the following header name and value from the decryptor:\nName:-%s-\n", headerMsg.name().c_str()); fflush(stdout);
  175. plaintext_header = headerMsg.name();
  176. std::vector<unsigned char> input_field(headerMsg.value().begin(), headerMsg.value().end());
  177. ip_list.push_back(input_field);
  178. ret_status = encrypt_decrypt_msgs(0, ip_list, binary_list);
  179. if(ret_status != 0)
  180. return ret_status;
  181. binary_header = binary_list.at(0);
  182. binary_ptr = &binary_header[0];
  183. binary_header_length = binary_header.size();
  184. base64_ciphertext_header = (uint8_t* ) malloc(2*binary_header_length);
  185. base64_ciphertext_header_length = base64_encoding_wrapper((unsigned char*) binary_ptr, binary_header_length , base64_ciphertext_header);
  186. if(base64_ciphertext_header_length <= 0)
  187. {
  188. printf("Error encoding the ciphertext header into base64 format.\n"); fflush(stdout); return 0x3;
  189. }
  190. printf("Got this header value:-%s-\n", base64_ciphertext_header);
  191. plaintext_header.append((const char* )base64_ciphertext_header, (size_t) base64_ciphertext_header_length);
  192. printf("Got this header:%s\n", plaintext_header.c_str()); fflush(stdout);
  193. return 0;
  194. }