SgxSealer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "sgx.h"
  2. #include "sgx_tseal.h" // For sgx_seal_data, sgx_calc_sealed_data_size, sgx_get_encrypt_txt_len, sgx_unseal_data
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <vector>
  8. //class Sealer {
  9. uint32_t seal_message(std::string& plaintext_str, std::string& sgx_sealed_msg_str)
  10. {
  11. uint32_t ret;
  12. uint8_t* sgx_sealed_msg;
  13. uint32_t expected_sealed_msg_length;
  14. expected_sealed_msg_length = sgx_calc_sealed_data_size(0, 9);
  15. if(expected_sealed_msg_length == 0xFFFFFFFF)
  16. return 1;
  17. sgx_sealed_msg = (uint8_t*)malloc(expected_sealed_msg_length); // Doesn't change with protobufs - convert the data here to protobuf format after it is initialized
  18. ret = sgx_seal_data(0, NULL, 9, (uint8_t*) plaintext_str.c_str(), expected_sealed_msg_length, (sgx_sealed_data_t*) sgx_sealed_msg);
  19. sgx_sealed_msg_str = std::string((char*)sgx_sealed_msg, expected_sealed_msg_length); // TODO: Fishy conversion.
  20. free(sgx_sealed_msg);
  21. return ret;
  22. }
  23. uint32_t unseal_and_verify_sealed_message(std::string& sgx_sealed_msg_str, std::string& plaintext)
  24. {
  25. uint32_t ret = 0;
  26. uint8_t* sgx_sealed_msg;
  27. uint8_t* temp_plaintext;
  28. std::string protobuf_encoded_str; std::string decoded_plaintext;
  29. uint32_t sgx_counter_value;
  30. uint32_t expected_plaintext_msg_length;
  31. std::vector<uint8_t> sgx_sealed_msg_vector(sgx_sealed_msg_str.begin(), sgx_sealed_msg_str.end());// TODO: Add null termination?
  32. sgx_sealed_msg = &sgx_sealed_msg_vector[0];
  33. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sgx_sealed_msg);
  34. if(expected_plaintext_msg_length == 0xffffffff)
  35. return 1;
  36. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  37. ret = sgx_unseal_data((sgx_sealed_data_t*)sgx_sealed_msg, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  38. if(ret != SGX_SUCCESS)
  39. {
  40. free(temp_plaintext);
  41. switch(ret)
  42. {
  43. case SGX_ERROR_MAC_MISMATCH:
  44. // MAC of the sealed data is incorrect. The sealed data has been tampered.
  45. break;
  46. case SGX_ERROR_INVALID_ATTRIBUTE:
  47. // Indicates attribute field of the sealed data is incorrect.
  48. break;
  49. case SGX_ERROR_INVALID_ISVSVN:
  50. // Indicates isv_svn field of the sealed data is greater than the enclave�s ISVSVN. This is a downgraded enclave.
  51. break;
  52. case SGX_ERROR_INVALID_CPUSVN:
  53. // Indicates cpu_svn field of the sealed data is greater than the platform�s cpu_svn. enclave is on a downgraded platform.
  54. break;
  55. case SGX_ERROR_INVALID_KEYNAME:
  56. // Indicates key_name field of the sealed data is incorrect.
  57. break;
  58. default:
  59. // other errors
  60. break;
  61. }
  62. return ret;
  63. }
  64. protobuf_encoded_str = std::string((char*)temp_plaintext, expected_plaintext_msg_length); // TODO: Fishy conversion.
  65. free(temp_plaintext);
  66. plaintext = protobuf_encoded_str;
  67. return ret;
  68. }
  69. //}