protobufSgxTransformsHelper.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // Created by miti on 21/07/19.
  3. //
  4. #include "protobufSgxTransformsHelper.h"
  5. namespace protobufSgxTransformsHelper {
  6. namespace {
  7. int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  8. {
  9. if(temp32 > UINT16_MAX)
  10. return -1;
  11. *temp16 = (uint16_t)temp32;
  12. return 0;
  13. }
  14. }
  15. int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  16. {
  17. if(temp32 > UINT8_MAX)
  18. return -1;
  19. *temp8 = (uint8_t)temp32;
  20. return 0;
  21. }
  22. void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  23. {
  24. int counter; google::protobuf::uint32 temp32;
  25. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  26. {
  27. temp32 = g_a->gx[counter];
  28. protobuf_g_a->add_gx(temp32);
  29. temp32 = g_a->gy[counter];
  30. protobuf_g_a->add_gy(temp32);
  31. }
  32. }
  33. int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  34. {
  35. printf("\n ec256 pub key\n");
  36. int counter; google::protobuf::uint32 temp32;
  37. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  38. {
  39. temp32 = protobuf_g_a->gx(counter);
  40. if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0)
  41. return -1;
  42. printf("%02x ",g_a->gx[counter]);
  43. temp32 = protobuf_g_a->gy(counter);
  44. if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0)
  45. return -1;
  46. printf("%02x ",g_a->gy[counter]);
  47. }
  48. return 0;
  49. }
  50. void encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  51. {
  52. protobuf_attributes->set_flags(attributes->flags); // 64 bit
  53. protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit
  54. }
  55. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  56. {
  57. attributes->flags = protobuf_attributes->flags();
  58. printf("\n flags %" PRIu64 " \n", attributes->flags);
  59. attributes->xfrm = protobuf_attributes->xfrm();
  60. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  61. return 0;
  62. }
  63. void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  64. {
  65. printf("message 3 ------------------------------------------------------------ \n"); fflush(stdout);
  66. int counter; google::protobuf::uint32 temp32;
  67. printf("Key ID\n");
  68. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  69. {
  70. temp32=report->key_id.id[counter];
  71. printf("%02x ", temp32);
  72. protobuf_report->add_key_id(temp32);
  73. }
  74. printf("MAC\n");
  75. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  76. {
  77. temp32=report->mac[counter];
  78. printf("%02x ", temp32);
  79. protobuf_report->add_mac(temp32);
  80. }
  81. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  82. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  83. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  84. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  85. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  86. {
  87. temp32=report->body.cpu_svn.svn[counter];
  88. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  89. }
  90. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  91. {
  92. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  93. protobuf_report->mutable_body()->add_reserved1(temp32);
  94. }
  95. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  96. {
  97. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  98. protobuf_report->mutable_body()->add_reserved2(temp32);
  99. }
  100. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  101. {
  102. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  103. protobuf_report->mutable_body()->add_reserved3(temp32);
  104. }
  105. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  106. {
  107. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  108. protobuf_report->mutable_body()->add_reserved4(temp32);
  109. }
  110. printf("\n enclave measurement\n");
  111. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  112. {
  113. temp32=report->body.mr_enclave.m[counter];
  114. printf("%02x ", temp32);
  115. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  116. }
  117. printf("\n signer measurement\n");
  118. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  119. {
  120. temp32=report->body.mr_signer.m[counter];
  121. printf("%02x ", temp32);
  122. protobuf_report->mutable_body()->add_mr_signer(temp32);
  123. }
  124. printf("\n report data\n");
  125. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  126. {
  127. temp32=report->body.report_data.d[counter];
  128. printf("%02x ", temp32);
  129. protobuf_report->mutable_body()->add_report_data(temp32);
  130. }
  131. }
  132. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  133. {
  134. int counter; google::protobuf::uint32 temp32;
  135. printf("\n decoding report body:\n keyid\n");
  136. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  137. {
  138. temp32=protobuf_report->key_id(counter);
  139. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  140. return -1;
  141. printf("%02x ", report->key_id.id[counter]);
  142. }
  143. printf("\nreport mac\n");
  144. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  145. {
  146. temp32=protobuf_report->mac(counter);
  147. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  148. return -1;
  149. printf("%02x ", report->mac[counter]);
  150. }
  151. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  152. temp32=protobuf_report->mutable_body()->isv_svn();
  153. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  154. return -1;
  155. printf("\nmisc select %02x \n", report->body.misc_select);
  156. temp32=protobuf_report->mutable_body()->isv_prod_id();
  157. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  158. return -1;
  159. printf("\nprod id %02x \n", report->body.isv_prod_id);
  160. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  161. printf("\n cpu svn\n");
  162. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  163. {
  164. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  165. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  166. return -1;
  167. printf("%02x ", report->body.cpu_svn.svn[counter]);
  168. }
  169. /*
  170. printf("\n reserved1 \n");
  171. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  172. {
  173. temp32=protobuf_report->mutable_body()->reserved1(counter);
  174. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  175. return -1;
  176. printf("%02x ", report->body.reserved1[counter]);
  177. }
  178. printf("\n reserved2 \n");
  179. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  180. {
  181. temp32=protobuf_report->mutable_body()->reserved2(counter);
  182. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  183. return -1;
  184. printf("%02x ", report->body.reserved2[counter]);
  185. }
  186. printf("\n reserved3 \n");
  187. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  188. {
  189. temp32=protobuf_report->mutable_body()->reserved3(counter);
  190. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  191. return -1;
  192. printf("%02x ", report->body.reserved3[counter]);
  193. }
  194. printf("\n reserved4 \n");
  195. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  196. {
  197. temp32=protobuf_report->mutable_body()->reserved4(counter);
  198. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  199. return -1;
  200. printf("%02x ", report->body.reserved4[counter]);
  201. }
  202. */
  203. printf("\n mrenclave \n");
  204. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  205. {
  206. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  207. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  208. return -1;
  209. printf("%02x ", report->body.mr_enclave.m[counter]);
  210. }
  211. printf("\n mrsigner \n");
  212. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  213. {
  214. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  215. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  216. return -1;
  217. printf("%02x ", report->body.mr_signer.m[counter]);
  218. }
  219. printf("\n report data\n");
  220. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  221. {
  222. temp32=protobuf_report->mutable_body()->report_data(counter);
  223. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  224. return -1;
  225. printf("%02x ", report->body.report_data.d[counter]);
  226. }
  227. return 0;
  228. }
  229. }