PostLAMessaging.cpp 4.3 KB

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