protobufSgxLATransformsInitiator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "ProtobufLAMessages.pb.h"
  2. #include "../Decryptor/Decryptor_u.h"
  3. #include "sgx_eid.h"
  4. #include "sgx_urts.h"
  5. #define __STDC_FORMAT_MACROS
  6. #include <inttypes.h>
  7. #include <stdio.h>
  8. #include "sgx_report.h"
  9. #define SGX_REPORT_BODY_RESERVED1 28
  10. #define SGX_REPORT_BODY_RESERVED2 32
  11. #define SGX_REPORT_BODY_RESERVED3 96
  12. #define SGX_REPORT_BODY_RESERVED4 60
  13. // TODO: private
  14. int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  15. {
  16. if(temp32 > UINT8_MAX)
  17. return -1;
  18. else
  19. {
  20. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  21. *temp8 = (uint8_t)temp32;
  22. return 0;
  23. }
  24. }
  25. // TODO: private
  26. int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  27. {
  28. if(temp32 > UINT16_MAX)
  29. return -1;
  30. else
  31. {
  32. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  33. *temp16 = (uint16_t)temp32;
  34. return 0;
  35. }
  36. }
  37. // TODO: private
  38. void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  39. {
  40. int counter; google::protobuf::uint32 temp32;
  41. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  42. {
  43. temp32 = g_a->gx[counter];
  44. protobuf_g_a->add_gx(temp32);
  45. temp32 = g_a->gy[counter];
  46. protobuf_g_a->add_gy(temp32);
  47. }
  48. }
  49. // TODO: private
  50. int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  51. {
  52. printf("\n ec256 pub key\n");
  53. int counter; google::protobuf::uint32 temp32;
  54. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  55. {
  56. temp32 = protobuf_g_a->gx(counter);
  57. if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0)
  58. return -1;
  59. printf("%d ",g_a->gx[counter]);
  60. temp32 = protobuf_g_a->gy(counter);
  61. if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0)
  62. return -1;
  63. printf("%d ",g_a->gy[counter]);
  64. }
  65. return 0;
  66. }
  67. // TODO: private
  68. void encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  69. {
  70. protobuf_attributes->set_flags(attributes->flags); // 64 bit
  71. protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit
  72. }
  73. // TODO: private
  74. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  75. {
  76. attributes->flags = protobuf_attributes->flags();
  77. printf("\n flags %" PRIu64 " \n", attributes->flags);
  78. attributes->xfrm = protobuf_attributes->xfrm();
  79. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  80. return 0;
  81. }
  82. // TODO: private
  83. void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  84. {
  85. int counter; google::protobuf::uint32 temp32;
  86. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  87. {
  88. temp32=report->key_id.id[counter];
  89. protobuf_report->add_key_id(temp32);
  90. }
  91. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  92. {
  93. temp32=report->mac[counter];
  94. protobuf_report->add_mac(temp32);
  95. }
  96. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  97. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  98. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  99. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  100. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  101. {
  102. temp32=report->body.cpu_svn.svn[counter];
  103. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  104. }
  105. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  106. {
  107. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  108. protobuf_report->mutable_body()->add_reserved1(temp32);
  109. }
  110. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  111. {
  112. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  113. protobuf_report->mutable_body()->add_reserved2(temp32);
  114. }
  115. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  116. {
  117. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  118. protobuf_report->mutable_body()->add_reserved3(temp32);
  119. }
  120. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  121. {
  122. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  123. protobuf_report->mutable_body()->add_reserved4(temp32);
  124. }
  125. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  126. {
  127. temp32=report->body.mr_enclave.m[counter];
  128. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  129. }
  130. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  131. {
  132. temp32=report->body.mr_signer.m[counter];
  133. protobuf_report->mutable_body()->add_mr_signer(temp32);
  134. }
  135. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  136. {
  137. temp32=report->body.report_data.d[counter];
  138. protobuf_report->mutable_body()->add_report_data(temp32);
  139. }
  140. }
  141. // TODO: private
  142. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  143. {
  144. int counter; google::protobuf::uint32 temp32;
  145. printf("\nreport body keyid\n");
  146. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  147. {
  148. temp32=protobuf_report->key_id(counter);
  149. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  150. return -1;
  151. printf("%d ", report->key_id.id[counter]);
  152. }
  153. printf("\nreport mac\n");
  154. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  155. {
  156. temp32=protobuf_report->mac(counter);
  157. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  158. return -1;
  159. printf("%d ", report->mac[counter]);
  160. }
  161. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  162. temp32=protobuf_report->mutable_body()->isv_svn();
  163. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  164. return -1;
  165. printf("\nmisc select %d \n", report->body.misc_select);
  166. temp32=protobuf_report->mutable_body()->isv_prod_id();
  167. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  168. return -1;
  169. printf("\nprod id %d \n", report->body.isv_prod_id);
  170. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  171. printf("\n cpu svn\n");
  172. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  173. {
  174. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  175. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  176. return -1;
  177. printf("%d ", report->body.cpu_svn.svn[counter]);
  178. }
  179. printf("\n reserved1 \n");
  180. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  181. {
  182. temp32=protobuf_report->mutable_body()->reserved1(counter);
  183. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  184. return -1;
  185. printf("%d ", report->body.reserved1[counter]);
  186. }
  187. printf("\n reserved2 \n");
  188. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  189. {
  190. temp32=protobuf_report->mutable_body()->reserved2(counter);
  191. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  192. return -1;
  193. printf("%d ", report->body.reserved2[counter]);
  194. }
  195. printf("\n reserved3 \n");
  196. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  197. {
  198. temp32=protobuf_report->mutable_body()->reserved3(counter);
  199. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  200. return -1;
  201. printf("%d ", report->body.reserved3[counter]);
  202. }
  203. printf("\n reserved4 \n");
  204. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  205. {
  206. temp32=protobuf_report->mutable_body()->reserved4(counter);
  207. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  208. return -1;
  209. printf("%d ", report->body.reserved4[counter]);
  210. }
  211. printf("\n mrenclave \n");
  212. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  213. {
  214. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  215. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  216. return -1;
  217. printf("%x ", report->body.mr_enclave.m[counter]);
  218. }
  219. printf("\n mrsigner \n");
  220. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  221. {
  222. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  223. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  224. return -1;
  225. printf("%x ", report->body.mr_signer.m[counter]);
  226. }
  227. printf("\n report data\n");
  228. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  229. {
  230. temp32=protobuf_report->mutable_body()->report_data(counter);
  231. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  232. return -1;
  233. printf("%d ", report->body.report_data.d[counter]);
  234. }
  235. return 0;
  236. }
  237. void encode_msg1_to_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  238. {
  239. int counter; google::protobuf::uint32 temp32; // google::protobuf::uint64 temp64;
  240. encode_ec256_public_key_to_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a));
  241. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  242. {
  243. temp32=native_dhmsg1->target.mr_enclave.m[counter];
  244. protobuf_dhmsg1.mutable_target()->add_mr_enclave(temp32);
  245. }
  246. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  247. {
  248. temp32=native_dhmsg1->target.reserved1[counter];
  249. protobuf_dhmsg1.mutable_target()->add_reserved1(temp32);
  250. }
  251. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  252. {
  253. temp32=native_dhmsg1->target.reserved2[counter];
  254. protobuf_dhmsg1.mutable_target()->add_reserved2(temp32);
  255. }
  256. encode_attributes_to_protobuf(protobuf_dhmsg1.mutable_target()->mutable_attributes(), &(native_dhmsg1->target.attributes));
  257. temp32=native_dhmsg1->target.misc_select ;
  258. protobuf_dhmsg1.mutable_target()->set_misc_select(temp32);
  259. }
  260. void encode_msg3_to_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  261. {
  262. int counter; google::protobuf::uint32 temp32;
  263. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  264. {
  265. temp32=native_dhmsg3->cmac[counter];
  266. protobuf_dhmsg3.add_cmac(temp32);
  267. }
  268. encode_report_to_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report));
  269. int max_counter=native_dhmsg3->msg3_body.additional_prop_length;
  270. unsigned char*temp;
  271. for(counter=0,temp=native_dhmsg3->msg3_body.additional_prop;counter<max_counter;counter++,temp++)
  272. {
  273. protobuf_dhmsg3.mutable_msg3_body()->add_additional_prop(*temp);
  274. }
  275. }
  276. int decode_msg2_from_protobuf(protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  277. {
  278. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  279. printf("\ncmac\n");
  280. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  281. {
  282. temp32=protobuf_dhmsg2.cmac(counter);
  283. if(fit_32_into_uint8_t(temp32, &(native_dhmsg2->cmac[counter]))!=0)
  284. return -1;
  285. printf("%d ",native_dhmsg2->cmac[counter]);
  286. }
  287. if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b)) !=0)
  288. return -1;
  289. if(decode_report_from_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report)) !=0)
  290. return -1;
  291. return 0;
  292. }
  293. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  294. {
  295. int counter;
  296. printf("gx\n");
  297. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  298. {
  299. printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
  300. printf("%d ", native_dhmsg1->g_a.gx[counter]);
  301. }
  302. printf("\ngy\n");
  303. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  304. {
  305. printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
  306. printf("%d ", native_dhmsg1->g_a.gy[counter]);
  307. }
  308. printf("\nmrenclave in target\n");
  309. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  310. {
  311. printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
  312. printf("%d ", native_dhmsg1->target.mr_enclave.m[counter]);
  313. }
  314. printf("\nreserved1 in target\n");
  315. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  316. {
  317. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  318. printf("%d ", native_dhmsg1->target.reserved1[counter]);
  319. }
  320. printf("\nreserved2 in target\n");
  321. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  322. {
  323. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  324. printf("%d ", native_dhmsg1->target.reserved2[counter]);
  325. }
  326. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.flags);
  327. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  328. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.xfrm);
  329. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  330. printf("\n %" PRIu32 "\n", native_dhmsg1->target.misc_select);
  331. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  332. return 0;
  333. }