Transforms.cpp 14 KB

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