PostLAMessaging.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // The verifier doesn't receive any messages (in the deployment stage or at all)
  40. uint32_t PostLAMessaging::send_secure_msg(uint8_t* input, uint32_t input_size)
  41. {
  42. uint8_t* output;
  43. uint32_t output_size, ret;
  44. output = (unsigned char*) malloc(input_size + 28); // 16 for tag, 12 for IV
  45. ret = aes_gcm_wrapper(1, input, input_size, output, &output_size );
  46. if(ret != 0)
  47. return ret;
  48. size_t post_la_bytes_written = write(fd, output, output_size);
  49. printf("Wrote the hash and the tag to the decryptor socket.\n Wrote this many bytes: %d\n", post_la_bytes_written); fflush(stdout);
  50. if(close(fd)!= 0)
  51. {
  52. printf("Error in closing the socket connection.\n"); fflush(stdout); return 0xfd;
  53. }
  54. // TODO: Conversion logic to protobuf. Set msg or whatever.
  55. /* google::protobuf::MessageLite protobuf_msg;
  56. * if(protobufReaderWriter->write_msg(protobuf_msg) != 0)
  57. return 0x3;
  58. */
  59. return 0;
  60. }
  61. void PostLAMessaging::set_la_symmetric_key(uint8_t* given_key) {
  62. uint32_t counter;
  63. for(counter=0; counter<16; counter++)
  64. {
  65. key[counter] = given_key[counter];
  66. }
  67. }
  68. void PostLAMessaging::set_fd(int given_fd)
  69. {
  70. // protobufReaderWriter.set_fd(given_fd);
  71. fd = given_fd;
  72. }
  73. /*
  74. int encrypt_decrypt_msgs(int encrypt_decrypt, std::vector<std::string> &input_msgs,
  75. std::vector<std::string> &output_msgs)
  76. {
  77. unsigned char *input; unsigned char *output;
  78. uint32_t input_size, output_size, ret;
  79. output=NULL;
  80. for (std::string msg:input_msgs)
  81. {
  82. input_size = msg.length();
  83. input = (unsigned char*) msg.c_str();
  84. output = (unsigned char*) realloc(output, input_size + 28); // 16 for tag, 12 for IV
  85. ret = aes_gcm_wrapper(encrypt_decrypt, input, input_size, output, &output_size );
  86. if(ret!=0)
  87. {
  88. free(output);
  89. printf("Failed to encrypt an input field.\n"); fflush(stdout);
  90. return 0x2;
  91. }
  92. output_msgs.push_back(std::string(reinterpret_cast<const char *> (output), output_size));
  93. }
  94. free(output);
  95. return 0;
  96. }
  97. */
  98. /*
  99. * virtual void create_vector_from_protobuf(google::protobuf::MessageLite& protobuf_msg,
  100. std::vector<std::string> &native_msg_list) {}
  101. uint32_t receive_secure_msgs(std::vector<std::string> &plaintext_msg_list) {
  102. std::vector<std::string> ciphertext_msg_list;
  103. google::protobuf::MessageLite protobuf_msg;
  104. // read encrypted data
  105. if(!protobufReaderWriter.read_msg(protobuf_msg))
  106. {
  107. printf("Not all of the decryptor's message was read\n"); fflush(stdout);
  108. return 0xf3;
  109. }
  110. create_vector_from_protobuf(protobuf_msg, ciphertext_msg_list);
  111. return encrypt_decrypt_ciphertexts(0, &ciphertext_msg_list, plaintext_msg_list);
  112. }
  113. */
  114. /*
  115. uint32_t send_secure_msgs(std::vector<std::string> &plaintext_msg_list)
  116. {
  117. uint32_t ret;
  118. std::vector<std::string> ciphertext_msg_list;
  119. google::protobuf::MessageLite protobuf_msg;
  120. ret=encrypt_decrypt_msgs(1, plaintext_msg_list, &ciphertext_msg_list);
  121. if(ret!=0)
  122. return ret;
  123. create_protobuf_from_vector(ciphertext_msg_list, protobuf_msg);
  124. // write message to decryptor
  125. if(!protobufReaderWriter.write_msg(protobuf_msg))
  126. {
  127. printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
  128. return 0xfe;
  129. }
  130. return 0;
  131. }
  132. */