SgxProtobufLAInitiator_Transforms.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "sgx_eid.h"
  2. #include "error_codes.h"
  3. #include "datatypes.h"
  4. #include "sgx_urts.h"
  5. #include "sgx_dh.h"
  6. // For msg1
  7. #define SGX_TARGET_INFO_RESERVED1_BYTES 4
  8. #define SGX_TARGET_INFO_RESERVED2_BYTES 456
  9. #define SGX_ECP256_KEY_SIZE 32
  10. #define SGX_HASH_SIZE 32 /* SHA256 */
  11. // For msg2
  12. #define SGX_REPORT_DATA_SIZE 64
  13. #define SGX_KEYID_SIZE 32
  14. #define SGX_DH_MAC_SIZE 16
  15. #define SGX_REPORT_BODY_RESERVED1 28
  16. #define SGX_REPORT_BODY_RESERVED2 32
  17. #define SGX_REPORT_BODY_RESERVED3 96
  18. #define SGX_REPORT_BODY_RESERVED4 60
  19. #include <stdio.h>
  20. // For google protobufs and deserialization/serialization
  21. #include "ProtobufLAMessages.pb.h"
  22. #include <google/protobuf/io/coded_stream.h>
  23. #include <google/protobuf/io/zero_copy_stream_impl.h>
  24. using namespace google::protobuf::io;
  25. #include <inttypes.h>
  26. // TODO: PRIVATE
  27. int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  28. {
  29. if(temp32 > UINT8_MAX)
  30. return -1;
  31. else
  32. {
  33. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  34. *temp8 = (uint8_t)temp32;
  35. return 0;
  36. }
  37. }
  38. // TODO: PRIVATE
  39. int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  40. {
  41. if(temp32 > UINT16_MAX)
  42. return -1;
  43. else
  44. {
  45. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  46. *temp16 = (uint16_t)temp32;
  47. return 0;
  48. }
  49. }
  50. // TODO: PRIVATE
  51. void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  52. {
  53. printf("\n ec256 public key gx and gy \n");
  54. int counter; google::protobuf::uint32 temp32;
  55. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  56. {
  57. temp32 = g_a->gx[counter];
  58. protobuf_g_a->add_gx(temp32);
  59. printf("%d ", temp32);
  60. temp32 = g_a->gy[counter];
  61. protobuf_g_a->add_gy(temp32);
  62. printf("%d ", temp32);
  63. }
  64. printf("\n");
  65. }
  66. // TODO: PRIVATE
  67. void 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. // TODO: PRIVATE
  73. void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  74. {
  75. printf("\n report \n key id \n");
  76. int counter; google::protobuf::uint32 temp32;
  77. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  78. {
  79. temp32=report->key_id.id[counter];
  80. protobuf_report->add_key_id(temp32);
  81. printf("%d ",temp32);
  82. }
  83. printf("\n mac \n");
  84. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  85. {
  86. temp32=report->mac[counter];
  87. protobuf_report->add_mac(temp32);
  88. printf("%d ", temp32);
  89. }
  90. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  91. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  92. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  93. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  94. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  95. {
  96. temp32=report->body.cpu_svn.svn[counter];
  97. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  98. }
  99. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  100. {
  101. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  102. protobuf_report->mutable_body()->add_reserved1(temp32);
  103. }
  104. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  105. {
  106. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  107. protobuf_report->mutable_body()->add_reserved2(temp32);
  108. }
  109. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  110. {
  111. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  112. protobuf_report->mutable_body()->add_reserved3(temp32);
  113. }
  114. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  115. {
  116. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  117. protobuf_report->mutable_body()->add_reserved4(temp32);
  118. }
  119. printf("\nmr enclave\n");
  120. fflush(stdout);
  121. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  122. {
  123. temp32=report->body.mr_enclave.m[counter];
  124. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  125. printf("%x ", temp32);
  126. }
  127. printf("\n mr signer\n"); fflush(stdout);
  128. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  129. {
  130. temp32=report->body.mr_signer.m[counter];
  131. protobuf_report->mutable_body()->add_mr_signer(temp32);
  132. printf("%x ", temp32);
  133. }
  134. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  135. {
  136. temp32=report->body.report_data.d[counter];
  137. protobuf_report->mutable_body()->add_report_data(temp32);
  138. }
  139. }
  140. // TODO: PRIVATE
  141. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  142. {
  143. attributes->flags = protobuf_attributes->flags();
  144. printf("\n flags %" PRIu64 " \n", attributes->flags);
  145. attributes->xfrm = protobuf_attributes->xfrm();
  146. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  147. return 0;
  148. }
  149. // TODO: PRIVATE
  150. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  151. {
  152. int counter; google::protobuf::uint32 temp32;
  153. printf("\n----------------------Decoding received msg3 ------------------------\n");
  154. printf("\nreport body keyid\n");
  155. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  156. {
  157. temp32=protobuf_report->key_id(counter);
  158. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  159. return -1;
  160. printf("%d ", report->key_id.id[counter]);
  161. }
  162. printf("\nreport mac\n");
  163. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  164. {
  165. temp32=protobuf_report->mac(counter);
  166. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  167. return -1;
  168. printf("%d ", report->mac[counter]);
  169. }
  170. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  171. temp32=protobuf_report->mutable_body()->isv_svn();
  172. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  173. return -1;
  174. printf("\nmisc select %d \n", report->body.misc_select);
  175. temp32=protobuf_report->mutable_body()->isv_prod_id();
  176. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  177. return -1;
  178. printf("\nprod id %d \n", report->body.isv_prod_id);
  179. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  180. printf("\n cpu svn\n");
  181. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  182. {
  183. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  184. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  185. return -1;
  186. printf("%d ", report->body.cpu_svn.svn[counter]);
  187. }
  188. printf("\n reserved1 \n");
  189. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  190. {
  191. temp32=protobuf_report->mutable_body()->reserved1(counter);
  192. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  193. return -1;
  194. printf("%d ", report->body.reserved1[counter]);
  195. }
  196. printf("\n reserved2 \n");
  197. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  198. {
  199. temp32=protobuf_report->mutable_body()->reserved2(counter);
  200. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  201. return -1;
  202. printf("%d ", report->body.reserved2[counter]);
  203. }
  204. printf("\n reserved3 \n");
  205. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  206. {
  207. temp32=protobuf_report->mutable_body()->reserved3(counter);
  208. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  209. return -1;
  210. printf("%d ", report->body.reserved3[counter]);
  211. }
  212. printf("\n reserved4 \n");
  213. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  214. {
  215. temp32=protobuf_report->mutable_body()->reserved4(counter);
  216. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  217. return -1;
  218. printf("%d ", report->body.reserved4[counter]);
  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("%x ", 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("%x ", 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("%d ", report->body.report_data.d[counter]);
  243. }
  244. printf("\n------------------------ end of msg3 --------------------------\n");
  245. return 0;
  246. }
  247. int decode_msg1_from_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  248. {
  249. int counter; google::protobuf::uint32 temp32;// google::protobuf::uint64 temp64;
  250. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  251. {
  252. temp32 = protobuf_dhmsg1.mutable_g_a()->gx(counter);
  253. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->g_a.gx[counter]))!=0)
  254. return -1;
  255. temp32 = protobuf_dhmsg1.mutable_g_a()->gy(counter);
  256. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->g_a.gy[counter]))!=0)
  257. return -1;
  258. }
  259. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  260. {
  261. temp32 = protobuf_dhmsg1.mutable_target()->mr_enclave(counter);
  262. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.mr_enclave.m[counter]))!=0)
  263. return -1;
  264. }
  265. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  266. {
  267. temp32 = protobuf_dhmsg1.mutable_target()->reserved1(counter);
  268. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved1[counter]))!=0)
  269. return -1;
  270. }
  271. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  272. {
  273. temp32 = protobuf_dhmsg1.mutable_target()->reserved2(counter);
  274. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved2[counter]))!=0)
  275. return -1;
  276. }
  277. native_dhmsg1->target.attributes.flags = protobuf_dhmsg1.mutable_target()->mutable_attributes()->flags();
  278. native_dhmsg1->target.attributes.xfrm = protobuf_dhmsg1.mutable_target()->mutable_attributes()->xfrm();
  279. native_dhmsg1->target.misc_select = protobuf_dhmsg1.mutable_target()->misc_select();
  280. return 0;
  281. }
  282. int decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  283. {
  284. int counter; google::protobuf::uint32 temp32;
  285. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  286. {
  287. temp32=protobuf_dhmsg3.cmac(counter);
  288. if(fit_32_into_uint8_t(temp32, &(native_dhmsg3->cmac[counter]))!=0)
  289. return -1;
  290. }
  291. if(decode_report_from_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report))==-1)
  292. return -1;
  293. int max_counter=protobuf_dhmsg3.mutable_msg3_body()->additional_prop_size();
  294. native_dhmsg3->msg3_body.additional_prop_length=max_counter;
  295. // 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
  296. // 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.
  297. if(max_counter!=0)
  298. return -1;
  299. return 0;
  300. }
  301. // TODO: PRIVATE - OR EVEN GET RID OF IT
  302. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1 /*, sgx_dh_msg1_t* native_dhmsg1*/)
  303. {
  304. int counter;
  305. printf("gx\n");
  306. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  307. {
  308. printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
  309. }
  310. printf("\ngy\n");
  311. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  312. {
  313. printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
  314. }
  315. printf("\nmrenclave in target\n");
  316. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  317. {
  318. printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
  319. }
  320. printf("\nreserved1 in target\n");
  321. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  322. {
  323. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  324. }
  325. printf("\nreserved2 in target\n");
  326. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  327. {
  328. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  329. }
  330. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  331. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  332. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  333. return 0;
  334. }
  335. void encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  336. {
  337. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  338. printf("\n msg2 cmac \n");
  339. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  340. {
  341. temp32=native_dhmsg2->cmac[counter];
  342. protobuf_dhmsg2.add_cmac(temp32);
  343. printf("%d ", temp32);
  344. }
  345. encode_ec256_public_key_to_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b));
  346. encode_report_to_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report));
  347. }
  348. /*
  349. // Got rid of the session ID - figure out its role.
  350. //message1 from the destination enclave through a socket set up before.
  351. // TODO: What do we do about session id?
  352. int session_request_call(int fd, sgx_dh_msg1_t* dh_msg1) //, uint32_t* session_id)
  353. {
  354. protobuf_sgx_dh_msg1_t protobuf_msg1;
  355. printf("reading msg1\n");
  356. fflush(stdout);
  357. if(read_protobuf_msg_from_fd(fd, protobuf_msg1)!=0)
  358. return -1;
  359. print_initialized_msg1(protobuf_msg1);
  360. printf("\n done reading msg1 --------------------\n");
  361. fflush(stdout);
  362. if(decode_msg1_from_protobuf(protobuf_msg1, dh_msg1)!=0)
  363. return -1;
  364. return 0;
  365. }
  366. // Source enclave for exchange_report_ocall (like other ocalls) will be the PHP enclave and the destination enclave will be the decryptor one.
  367. //Makes an sgx_ecall to the destination enclave sends message2 from the source enclave and gets message 3 from the destination enclave
  368. // TODO: What do we do about session id?
  369. int exchange_report_call(int fd, sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3) // , uint32_t* session_id)
  370. {
  371. protobuf_sgx_dh_msg2_t protobuf_msg2;
  372. protobuf_sgx_dh_msg3_t protobuf_msg3;
  373. printf("\n------------------------------------- generating msg2 --------\n");
  374. // Fill protobuf class for dhmsg2 with contents from its native C struct.
  375. encode_msg2_to_protobuf(protobuf_msg2, dh_msg2);
  376. // Write msg length and then write the raw msg.
  377. if(write_protobuf_msg_to_fd(fd, protobuf_msg2)!=0)
  378. return -1;
  379. printf("Wrote msg2 to protobuf ------------------------------------------\n");
  380. fflush(stdout);
  381. // Read from socket dh_msg3
  382. if(read_protobuf_msg_from_fd(fd, protobuf_msg3)!=0)
  383. return -1;
  384. // Decode msg3 from protobuf to native structs
  385. if(decode_msg3_from_protobuf(protobuf_msg3, dh_msg3)!=0)
  386. return -1;
  387. return 0;
  388. }
  389. //Make an sgx_ecall to the destination enclave to close the session
  390. int end_session_ocall()
  391. {
  392. return SGX_SUCCESS;
  393. }
  394. */