protobufSgxLATransformsInitiator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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("%02x ",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("%02x ",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. printf("message 3 ------------------------------------------------------------ \n"); fflush(stdout);
  86. int counter; google::protobuf::uint32 temp32;
  87. printf("Key ID\n");
  88. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  89. {
  90. temp32=report->key_id.id[counter];
  91. printf("%02x ", temp32);
  92. protobuf_report->add_key_id(temp32);
  93. }
  94. printf("MAC\n");
  95. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  96. {
  97. temp32=report->mac[counter];
  98. printf("%02x ", temp32);
  99. protobuf_report->add_mac(temp32);
  100. }
  101. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  102. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  103. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  104. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  105. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  106. {
  107. temp32=report->body.cpu_svn.svn[counter];
  108. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  109. }
  110. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  111. {
  112. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  113. protobuf_report->mutable_body()->add_reserved1(temp32);
  114. }
  115. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  116. {
  117. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  118. protobuf_report->mutable_body()->add_reserved2(temp32);
  119. }
  120. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  121. {
  122. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  123. protobuf_report->mutable_body()->add_reserved3(temp32);
  124. }
  125. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  126. {
  127. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  128. protobuf_report->mutable_body()->add_reserved4(temp32);
  129. }
  130. printf("\n enclave measurement\n");
  131. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  132. {
  133. temp32=report->body.mr_enclave.m[counter];
  134. printf("%02x ", temp32);
  135. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  136. }
  137. printf("\n signer measurement\n");
  138. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  139. {
  140. temp32=report->body.mr_signer.m[counter];
  141. printf("%02x ", temp32);
  142. protobuf_report->mutable_body()->add_mr_signer(temp32);
  143. }
  144. printf("\n report data\n");
  145. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  146. {
  147. temp32=report->body.report_data.d[counter];
  148. printf("%02x ", temp32);
  149. protobuf_report->mutable_body()->add_report_data(temp32);
  150. }
  151. }
  152. // TODO: private
  153. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  154. {
  155. int counter; google::protobuf::uint32 temp32;
  156. printf("\n decoding report body:\n keyid\n");
  157. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  158. {
  159. temp32=protobuf_report->key_id(counter);
  160. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  161. return -1;
  162. printf("%02x ", report->key_id.id[counter]);
  163. }
  164. printf("\nreport mac\n");
  165. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  166. {
  167. temp32=protobuf_report->mac(counter);
  168. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  169. return -1;
  170. printf("%02x ", report->mac[counter]);
  171. }
  172. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  173. temp32=protobuf_report->mutable_body()->isv_svn();
  174. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  175. return -1;
  176. printf("\nmisc select %02x \n", report->body.misc_select);
  177. temp32=protobuf_report->mutable_body()->isv_prod_id();
  178. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  179. return -1;
  180. printf("\nprod id %02x \n", report->body.isv_prod_id);
  181. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  182. printf("\n cpu svn\n");
  183. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  184. {
  185. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  186. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  187. return -1;
  188. printf("%02x ", report->body.cpu_svn.svn[counter]);
  189. }
  190. /*
  191. printf("\n reserved1 \n");
  192. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  193. {
  194. temp32=protobuf_report->mutable_body()->reserved1(counter);
  195. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  196. return -1;
  197. printf("%02x ", report->body.reserved1[counter]);
  198. }
  199. printf("\n reserved2 \n");
  200. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  201. {
  202. temp32=protobuf_report->mutable_body()->reserved2(counter);
  203. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  204. return -1;
  205. printf("%02x ", report->body.reserved2[counter]);
  206. }
  207. printf("\n reserved3 \n");
  208. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  209. {
  210. temp32=protobuf_report->mutable_body()->reserved3(counter);
  211. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  212. return -1;
  213. printf("%02x ", report->body.reserved3[counter]);
  214. }
  215. printf("\n reserved4 \n");
  216. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  217. {
  218. temp32=protobuf_report->mutable_body()->reserved4(counter);
  219. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  220. return -1;
  221. printf("%02x ", report->body.reserved4[counter]);
  222. }
  223. */
  224. printf("\n mrenclave \n");
  225. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  226. {
  227. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  228. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  229. return -1;
  230. printf("%02x ", report->body.mr_enclave.m[counter]);
  231. }
  232. printf("\n mrsigner \n");
  233. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  234. {
  235. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  236. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  237. return -1;
  238. printf("%02x ", report->body.mr_signer.m[counter]);
  239. }
  240. printf("\n report data\n");
  241. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  242. {
  243. temp32=protobuf_report->mutable_body()->report_data(counter);
  244. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  245. return -1;
  246. printf("%02x ", report->body.report_data.d[counter]);
  247. }
  248. return 0;
  249. }
  250. void encode_msg1_to_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  251. {
  252. int counter; google::protobuf::uint32 temp32; // google::protobuf::uint64 temp64;
  253. encode_ec256_public_key_to_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a));
  254. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  255. {
  256. temp32=native_dhmsg1->target.mr_enclave.m[counter];
  257. protobuf_dhmsg1.mutable_target()->add_mr_enclave(temp32);
  258. }
  259. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  260. {
  261. temp32=native_dhmsg1->target.reserved1[counter];
  262. protobuf_dhmsg1.mutable_target()->add_reserved1(temp32);
  263. }
  264. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  265. {
  266. temp32=native_dhmsg1->target.reserved2[counter];
  267. protobuf_dhmsg1.mutable_target()->add_reserved2(temp32);
  268. }
  269. encode_attributes_to_protobuf(protobuf_dhmsg1.mutable_target()->mutable_attributes(), &(native_dhmsg1->target.attributes));
  270. temp32=native_dhmsg1->target.misc_select ;
  271. protobuf_dhmsg1.mutable_target()->set_misc_select(temp32);
  272. }
  273. void encode_msg3_to_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  274. {
  275. int counter; google::protobuf::uint32 temp32;
  276. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  277. {
  278. temp32=native_dhmsg3->cmac[counter];
  279. protobuf_dhmsg3.add_cmac(temp32);
  280. }
  281. encode_report_to_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report));
  282. int max_counter=native_dhmsg3->msg3_body.additional_prop_length;
  283. unsigned char*temp;
  284. for(counter=0,temp=native_dhmsg3->msg3_body.additional_prop;counter<max_counter;counter++,temp++)
  285. {
  286. protobuf_dhmsg3.mutable_msg3_body()->add_additional_prop(*temp);
  287. }
  288. }
  289. int decode_msg2_from_protobuf(protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  290. {
  291. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  292. printf("RECEIVED the following msg2\n"); fflush(stdout);
  293. printf("\ncmac\n");
  294. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  295. {
  296. temp32=protobuf_dhmsg2.cmac(counter);
  297. if(fit_32_into_uint8_t(temp32, &(native_dhmsg2->cmac[counter]))!=0)
  298. return -1;
  299. printf("%02x ",native_dhmsg2->cmac[counter]);
  300. }
  301. if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b)) !=0)
  302. return -1;
  303. if(decode_report_from_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report)) !=0)
  304. return -1;
  305. return 0;
  306. }
  307. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  308. {
  309. int counter;
  310. printf("Printing msg1:\n");
  311. printf("gx\n");
  312. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  313. {
  314. printf("%02x ", protobuf_dhmsg1.g_a().gx(counter));
  315. // printf("%02x ", native_dhmsg1->g_a.gx[counter]);
  316. }
  317. printf("\ngy\n");
  318. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  319. {
  320. printf("%02x ", protobuf_dhmsg1.g_a().gy(counter));
  321. // printf("%02x ", native_dhmsg1->g_a.gy[counter]);
  322. }
  323. printf("\nmrenclave for target\n");
  324. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  325. {
  326. printf("%02x ", protobuf_dhmsg1.target().mr_enclave(counter));
  327. // printf("%02x ", native_dhmsg1->target.mr_enclave.m[counter]);
  328. }
  329. /*
  330. printf("\nreserved1 in target\n");
  331. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  332. {
  333. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  334. printf("%02x ", native_dhmsg1->target.reserved1[counter]);
  335. }
  336. printf("\nreserved2 in target\n");
  337. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  338. {
  339. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  340. printf("%02x ", native_dhmsg1->target.reserved2[counter]);
  341. }
  342. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.flags);
  343. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  344. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.xfrm);
  345. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  346. printf("\n %" PRIu32 "\n", native_dhmsg1->target.misc_select);
  347. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  348. */
  349. fflush(stdout);
  350. return 0;
  351. }