SgxProtobufSealerTransforms.cpp 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //#include "ProtobufSgxTransforms.h"
  2. #include "sgx_tae_service.h"
  3. #include "ProtobufSealerMessages.pb.h"
  4. #include <string>
  5. namespace ProtobufSgxTransforms {
  6. namespace {
  7. void encode_counter_id_and_value_to_protobuf(sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
  8. protobuf_plaintext.mutable_monotonic_counter_id()->set_nonce(sgx_monotonic_counter_id->nonce, SGX_MC_UUID_NONCE_SIZE);
  9. protobuf_plaintext.mutable_monotonic_counter_id()->set_counter_id(sgx_monotonic_counter_id->counter_id, SGX_MC_UUID_COUNTER_ID_SIZE);
  10. protobuf_plaintext.set_monotonic_counter_value(*sgx_monotonic_counter_value);
  11. }
  12. void encode_message_to_protobuf(std::string& plaintext, protobuf_plaintext_seal_message& protobuf_plaintext) {
  13. protobuf_plaintext.set_message(plaintext);
  14. }
  15. void encode_message_and_counter_to_protobuf(std::string& plaintext, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
  16. encode_counter_id_and_value_to_protobuf(sgx_counter_id, sgx_counter_value, protobuf_plaintext);
  17. encode_message_to_protobuf(plaintext, protobuf_plaintext);
  18. }
  19. }
  20. uint32_t encode_message_and_counter_to_protobuf_string(std::string& plaintext, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value, std::string& serialized_plaintext) {
  21. uint32_t ret;
  22. protobuf_plaintext_seal_message protobuf_plaintext;
  23. uint32_t total_plaintext_length;
  24. encode_message_and_counter_to_protobuf(plaintext, sgx_counter_id, sgx_counter_value, protobuf_plaintext);
  25. total_plaintext_length = protobuf_plaintext.ByteSize(); // TODO: This assignment could be problematic on platforms where the max. positive value for signed int is greater than that for unsigned int?
  26. if(total_plaintext_length<=0)
  27. return 1;
  28. if(!protobuf_plaintext.SerializeToString(&serialized_plaintext))
  29. return 1;
  30. return 0;
  31. }
  32. void decode_protobuf_message_to_counter_id_and_value(std::string& temp_plaintext_str, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value) {
  33. /* protobuf_plaintext_seal_message protobuf_encoded_msg; protobuf_encoded_msg.ParseFromString(temp_plaintext_str);
  34. std::string protobuf_nonce = protobuf_encoded_msg.mutable_monotonic_counter_id()->nonce();
  35. std::vector<uint8_t> sgx_nonce_vector(protobuf_nonce.begin(), protobuf_nonce.end());
  36. uint8_t* id_char = &sgx_nonce_vector[0]; int counter;
  37. for(counter=0; counter<SGX_MC_UUID_NONCE_SIZE; counter++)
  38. {
  39. sgx_counter_id->nonce[counter]=*id_char; id_char++;
  40. }
  41. std::string protobuf_counter_id = protobuf_encoded_msg.mutable_monotonic_counter_id()->counter_id();
  42. std::vector<uint8_t> sgx_counter_id_vector(protobuf_counter_id.begin(), protobuf_counter_id.end());
  43. id_char = &sgx_counter_id_vector[0];
  44. for(counter=0; counter<SGX_MC_UUID_COUNTER_ID_SIZE; counter++)
  45. {
  46. sgx_counter_id->counter_id[counter]=*id_char; id_char++;
  47. }
  48. *sgx_counter_value=protobuf_encoded_msg.monotonic_counter_value();
  49. */
  50. }
  51. void decode_protobuf_message_to_plaintext(std::string& temp_plaintext_str , std::string& plaintext) {
  52. protobuf_plaintext_seal_message protobuf_encoded_msg; protobuf_encoded_msg.ParseFromString(temp_plaintext_str);
  53. plaintext = protobuf_encoded_msg.message();
  54. }
  55. /* namespace {
  56. void encode_counter_id_and_value_to_protobuf(sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
  57. protobuf_plaintext->mutable_monotonic_counter_id()->set_nonce(sgx_monotonic_counter_id->nonce, SGX_MC_UUID_NONCE_SIZE);
  58. protobuf_plaintext->mutable_monotonic_counter_id()->set_counter_id(sgx_monotonic_counter_id->counter_id, SGX_MC_UUID_COUNTER_ID_SIZE);
  59. protobuf_plaintext->set_monotonic_counter_value(*sgx_monotonic_counter_value);
  60. }
  61. void encode_message_to_protobuf(string& plaintext, protobuf_plaintext_seal_message& protobuf_plaintext) {
  62. protobuf_plaintext->set_message(plaintext);
  63. }
  64. void encode_message_and_counter_to_protobuf(std::string& plaintext, sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
  65. encode_counter_id_and_value_to_protobuf(&sgx_counter_id, &sgx_counter_value, protobuf_plaintext);
  66. encode_message_to_protobuf(plaintext, protobuf_plaintext);
  67. }
  68. }*/
  69. }