Transforms.cpp 14 KB

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