Transforms.cpp 15 KB

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