protobufSgxTransformsInitiator.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Created by miti on 21/07/19.
  3. //
  4. #include "protobufSgxTransformsInitiator.h"
  5. namespace protobufSgxTransformsInitiator {
  6. void encode_msg1_to_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  7. {
  8. int counter; google::protobuf::uint32 temp32; // google::protobuf::uint64 temp64;
  9. protobufSgxTransformsHelper::encode_ec256_public_key_to_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a));
  10. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  11. {
  12. temp32=native_dhmsg1->target.mr_enclave.m[counter];
  13. protobuf_dhmsg1.mutable_target()->add_mr_enclave(temp32);
  14. }
  15. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  16. {
  17. temp32=native_dhmsg1->target.reserved1[counter];
  18. protobuf_dhmsg1.mutable_target()->add_reserved1(temp32);
  19. }
  20. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  21. {
  22. temp32=native_dhmsg1->target.reserved2[counter];
  23. protobuf_dhmsg1.mutable_target()->add_reserved2(temp32);
  24. }
  25. protobufSgxTransformsHelper::encode_attributes_to_protobuf(protobuf_dhmsg1.mutable_target()->mutable_attributes(), &(native_dhmsg1->target.attributes));
  26. temp32=native_dhmsg1->target.misc_select ;
  27. protobuf_dhmsg1.mutable_target()->set_misc_select(temp32);
  28. }
  29. void encode_msg3_to_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  30. {
  31. int counter; google::protobuf::uint32 temp32;
  32. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  33. {
  34. temp32=native_dhmsg3->cmac[counter];
  35. protobuf_dhmsg3.add_cmac(temp32);
  36. }
  37. protobufSgxTransformsHelper::encode_report_to_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report));
  38. int max_counter=native_dhmsg3->msg3_body.additional_prop_length;
  39. unsigned char*temp;
  40. for(counter=0,temp=native_dhmsg3->msg3_body.additional_prop;counter<max_counter;counter++,temp++)
  41. {
  42. protobuf_dhmsg3.mutable_msg3_body()->add_additional_prop(*temp);
  43. }
  44. }
  45. int decode_msg2_from_protobuf(protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  46. {
  47. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  48. printf("RECEIVED the following msg2\n"); fflush(stdout);
  49. printf("\ncmac\n");
  50. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  51. {
  52. temp32=protobuf_dhmsg2.cmac(counter);
  53. if(fit_32_into_uint8_t(temp32, &(native_dhmsg2->cmac[counter]))!=0)
  54. return -1;
  55. printf("%02x ",native_dhmsg2->cmac[counter]);
  56. }
  57. if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b)) !=0)
  58. return -1;
  59. if(decode_report_from_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report)) !=0)
  60. return -1;
  61. return 0;
  62. }
  63. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  64. {
  65. int counter;
  66. printf("Printing msg1:\n");
  67. printf("gx\n");
  68. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  69. {
  70. printf("%02x ", protobuf_dhmsg1.g_a().gx(counter));
  71. // printf("%02x ", native_dhmsg1->g_a.gx[counter]);
  72. }
  73. printf("\ngy\n");
  74. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  75. {
  76. printf("%02x ", protobuf_dhmsg1.g_a().gy(counter));
  77. // printf("%02x ", native_dhmsg1->g_a.gy[counter]);
  78. }
  79. printf("\nmrenclave for target\n");
  80. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  81. {
  82. printf("%02x ", protobuf_dhmsg1.target().mr_enclave(counter));
  83. // printf("%02x ", native_dhmsg1->target.mr_enclave.m[counter]);
  84. }
  85. printf("\nreserved1 in target\n");
  86. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  87. {
  88. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  89. printf("%02x ", native_dhmsg1->target.reserved1[counter]);
  90. }
  91. printf("\nreserved2 in target\n");
  92. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  93. {
  94. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  95. printf("%02x ", native_dhmsg1->target.reserved2[counter]);
  96. }
  97. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.flags);
  98. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  99. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.xfrm);
  100. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  101. printf("\n %" PRIu32 "\n", native_dhmsg1->target.misc_select);
  102. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  103. fflush(stdout);
  104. return 0;
  105. }
  106. };