SgxProtobufLAInitiator_Transforms.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 OWN 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("%02x", 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("0x%02x,", temp32);
  133. }
  134. printf("\n"); fflush(stdout);
  135. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  136. {
  137. temp32=report->body.report_data.d[counter];
  138. protobuf_report->mutable_body()->add_report_data(temp32);
  139. }
  140. }
  141. // TODO: PRIVATE
  142. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  143. {
  144. attributes->flags = protobuf_attributes->flags();
  145. printf("\n flags %" PRIu64 " \n", attributes->flags);
  146. attributes->xfrm = protobuf_attributes->xfrm();
  147. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  148. return 0;
  149. }
  150. // TODO: PRIVATE
  151. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  152. {
  153. int counter; google::protobuf::uint32 temp32;
  154. printf("\n----------------------Decoding received msg3 ------------------------\n");
  155. printf("\nreport body keyid\n");
  156. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  157. {
  158. temp32=protobuf_report->key_id(counter);
  159. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  160. return -1;
  161. printf("%d ", report->key_id.id[counter]);
  162. }
  163. printf("\nreport mac\n");
  164. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  165. {
  166. temp32=protobuf_report->mac(counter);
  167. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  168. return -1;
  169. printf("%d ", report->mac[counter]);
  170. }
  171. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  172. temp32=protobuf_report->mutable_body()->isv_svn();
  173. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  174. return -1;
  175. printf("\nmisc select %d \n", report->body.misc_select);
  176. temp32=protobuf_report->mutable_body()->isv_prod_id();
  177. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  178. return -1;
  179. printf("\nprod id %d \n", report->body.isv_prod_id);
  180. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  181. printf("\n cpu svn\n");
  182. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  183. {
  184. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  185. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  186. return -1;
  187. printf("%d ", report->body.cpu_svn.svn[counter]);
  188. }
  189. printf("\n reserved1 \n");
  190. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  191. {
  192. temp32=protobuf_report->mutable_body()->reserved1(counter);
  193. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  194. return -1;
  195. printf("%d ", report->body.reserved1[counter]);
  196. }
  197. printf("\n reserved2 \n");
  198. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  199. {
  200. temp32=protobuf_report->mutable_body()->reserved2(counter);
  201. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  202. return -1;
  203. printf("%d ", report->body.reserved2[counter]);
  204. }
  205. printf("\n reserved3 \n");
  206. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  207. {
  208. temp32=protobuf_report->mutable_body()->reserved3(counter);
  209. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  210. return -1;
  211. printf("%d ", report->body.reserved3[counter]);
  212. }
  213. printf("\n reserved4 \n");
  214. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  215. {
  216. temp32=protobuf_report->mutable_body()->reserved4(counter);
  217. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  218. return -1;
  219. printf("%d ", report->body.reserved4[counter]);
  220. }
  221. printf("\n mrenclave \n");
  222. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  223. {
  224. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  225. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  226. return -1;
  227. printf("%x ", report->body.mr_enclave.m[counter]);
  228. }
  229. printf("\n mrsigner \n");
  230. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  231. {
  232. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  233. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  234. return -1;
  235. printf("%x ", report->body.mr_signer.m[counter]);
  236. }
  237. printf("\n report data\n");
  238. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  239. {
  240. temp32=protobuf_report->mutable_body()->report_data(counter);
  241. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  242. return -1;
  243. printf("%d ", report->body.report_data.d[counter]);
  244. }
  245. printf("\n------------------------ end of msg3 --------------------------\n");
  246. return 0;
  247. }
  248. int decode_msg1_from_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  249. {
  250. int counter; google::protobuf::uint32 temp32;// google::protobuf::uint64 temp64;
  251. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  252. {
  253. temp32 = protobuf_dhmsg1.mutable_g_a()->gx(counter);
  254. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->g_a.gx[counter]))!=0)
  255. return -1;
  256. temp32 = protobuf_dhmsg1.mutable_g_a()->gy(counter);
  257. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->g_a.gy[counter]))!=0)
  258. return -1;
  259. }
  260. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  261. {
  262. temp32 = protobuf_dhmsg1.mutable_target()->mr_enclave(counter);
  263. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.mr_enclave.m[counter]))!=0)
  264. return -1;
  265. }
  266. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  267. {
  268. temp32 = protobuf_dhmsg1.mutable_target()->reserved1(counter);
  269. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved1[counter]))!=0)
  270. return -1;
  271. }
  272. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  273. {
  274. temp32 = protobuf_dhmsg1.mutable_target()->reserved2(counter);
  275. if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved2[counter]))!=0)
  276. return -1;
  277. }
  278. native_dhmsg1->target.attributes.flags = protobuf_dhmsg1.mutable_target()->mutable_attributes()->flags();
  279. native_dhmsg1->target.attributes.xfrm = protobuf_dhmsg1.mutable_target()->mutable_attributes()->xfrm();
  280. native_dhmsg1->target.misc_select = protobuf_dhmsg1.mutable_target()->misc_select();
  281. return 0;
  282. }
  283. int decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  284. {
  285. int counter; google::protobuf::uint32 temp32;
  286. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  287. {
  288. temp32=protobuf_dhmsg3.cmac(counter);
  289. if(fit_32_into_uint8_t(temp32, &(native_dhmsg3->cmac[counter]))!=0)
  290. return -1;
  291. }
  292. if(decode_report_from_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report))==-1)
  293. return -1;
  294. int max_counter=protobuf_dhmsg3.mutable_msg3_body()->additional_prop_size();
  295. native_dhmsg3->msg3_body.additional_prop_length=max_counter;
  296. // 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
  297. // 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.
  298. if(max_counter!=0)
  299. return -1;
  300. return 0;
  301. }
  302. /*
  303. // TODO: PRIVATE - OR EVEN GET RID OF IT
  304. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1)
  305. {
  306. int counter;
  307. printf("gx\n");
  308. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  309. {
  310. printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
  311. }
  312. printf("\ngy\n");
  313. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  314. {
  315. printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
  316. }
  317. printf("\nmrenclave in target\n");
  318. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  319. {
  320. printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
  321. }
  322. printf("\nreserved1 in target\n");
  323. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  324. {
  325. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  326. }
  327. printf("\nreserved2 in target\n");
  328. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  329. {
  330. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  331. }
  332. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  333. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  334. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  335. return 0;
  336. }
  337. */
  338. void encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  339. {
  340. int counter; google::protobuf::uint32 temp32;
  341. printf("\n msg2 cmac \n");
  342. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  343. {
  344. temp32=native_dhmsg2->cmac[counter];
  345. protobuf_dhmsg2.add_cmac(temp32);
  346. printf("%d ", temp32);
  347. }
  348. encode_ec256_public_key_to_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b));
  349. encode_report_to_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report));
  350. }
  351. /*
  352. // Got rid of the session ID - figure out its role.
  353. //message1 from the destination enclave through a socket set up before.
  354. // TODO: What do we do about session id?
  355. int session_request_call(int fd, sgx_dh_msg1_t* dh_msg1) //, uint32_t* session_id)
  356. {
  357. protobuf_sgx_dh_msg1_t protobuf_msg1;
  358. printf("reading msg1\n");
  359. fflush(stdout);
  360. if(read_protobuf_msg_from_fd(fd, protobuf_msg1)!=0)
  361. return -1;
  362. print_initialized_msg1(protobuf_msg1);
  363. printf("\n done reading msg1 --------------------\n");
  364. fflush(stdout);
  365. if(decode_msg1_from_protobuf(protobuf_msg1, dh_msg1)!=0)
  366. return -1;
  367. return 0;
  368. }
  369. // Source enclave for exchange_report_ocall (like other ocalls) will be the PHP enclave and the destination enclave will be the decryptor one.
  370. //Makes an sgx_ecall to the destination enclave sends message2 from the source enclave and gets message 3 from the destination enclave
  371. // TODO: What do we do about session id?
  372. int exchange_report_call(int fd, sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3) // , uint32_t* session_id)
  373. {
  374. protobuf_sgx_dh_msg2_t protobuf_msg2;
  375. protobuf_sgx_dh_msg3_t protobuf_msg3;
  376. printf("\n------------------------------------- generating msg2 --------\n");
  377. // Fill protobuf class for dhmsg2 with contents from its native C struct.
  378. encode_msg2_to_protobuf(protobuf_msg2, dh_msg2);
  379. // Write msg length and then write the raw msg.
  380. if(write_protobuf_msg_to_fd(fd, protobuf_msg2)!=0)
  381. return -1;
  382. printf("Wrote msg2 to protobuf ------------------------------------------\n");
  383. fflush(stdout);
  384. // Read from socket dh_msg3
  385. if(read_protobuf_msg_from_fd(fd, protobuf_msg3)!=0)
  386. return -1;
  387. // Decode msg3 from protobuf to native structs
  388. if(decode_msg3_from_protobuf(protobuf_msg3, dh_msg3)!=0)
  389. return -1;
  390. return 0;
  391. }
  392. //Make an sgx_ecall to the destination enclave to close the session
  393. int end_session_ocall()
  394. {
  395. return SGX_SUCCESS;
  396. }
  397. */