App.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. // App.cpp : Defines the entry point for the console application.
  32. #include <stdio.h>
  33. #include <map>
  34. #include "../Enclave1/Enclave1_u.h"
  35. #include "../Enclave2/Enclave2_u.h"
  36. #include "../Enclave3/Enclave3_u.h"
  37. #include "sgx_eid.h"
  38. #include "sgx_urts.h"
  39. #define UNUSED(val) (void)(val)
  40. #define TCHAR char
  41. #define _TCHAR char
  42. #define _T(str) str
  43. #define scanf_s scanf
  44. #define _tmain main
  45. extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  46. sgx_enclave_id_t e1_enclave_id = 0;
  47. sgx_enclave_id_t e2_enclave_id = 0;
  48. sgx_enclave_id_t e3_enclave_id = 0;
  49. #define ENCLAVE1_PATH "libenclave1.so"
  50. #define ENCLAVE2_PATH "libenclave2.so"
  51. #define ENCLAVE3_PATH "libenclave3.so"
  52. void waitForKeyPress()
  53. {
  54. uint8_t ch;
  55. printf("\n\nHit a key....\n");
  56. scanf_s("%c", &ch);
  57. }
  58. uint32_t load_enclaves()
  59. {
  60. uint32_t enclave_temp_no;
  61. int ret, launch_token_updated;
  62. sgx_launch_token_t launch_token;
  63. enclave_temp_no = 0;
  64. ret = sgx_create_enclave(ENCLAVE1_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e1_enclave_id, NULL);
  65. if (ret != SGX_SUCCESS) {
  66. return ret;
  67. }
  68. enclave_temp_no++;
  69. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e1_enclave_id, enclave_temp_no));
  70. ret = sgx_create_enclave(ENCLAVE2_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  71. if (ret != SGX_SUCCESS) {
  72. return ret;
  73. }
  74. enclave_temp_no++;
  75. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e2_enclave_id, enclave_temp_no));
  76. ret = sgx_create_enclave(ENCLAVE3_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e3_enclave_id, NULL);
  77. if (ret != SGX_SUCCESS) {
  78. return ret;
  79. }
  80. enclave_temp_no++;
  81. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e3_enclave_id, enclave_temp_no));
  82. return SGX_SUCCESS;
  83. }
  84. int _tmain(int argc, _TCHAR* argv[])
  85. {
  86. uint32_t ret_status;
  87. sgx_status_t status;
  88. UNUSED(argc);
  89. UNUSED(argv);
  90. if(load_enclaves() != SGX_SUCCESS)
  91. {
  92. printf("\nLoad Enclave Failure");
  93. }
  94. printf("\nAvaliable Enclaves");
  95. printf("\nEnclave1 - EnclaveID %llx",e1_enclave_id);
  96. printf("\nEnclave2 - EnclaveID %llx",e2_enclave_id);
  97. printf("\nEnclave3 - EnclaveID %llx",e3_enclave_id);
  98. do
  99. {
  100. //Test Create session between Enclave1(Source) and Enclave2(Destination)
  101. status = Enclave1_test_create_session(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  102. if (status!=SGX_SUCCESS)
  103. {
  104. printf("Enclave1_test_create_session Ecall failed: Error code is %x", status);
  105. break;
  106. }
  107. else
  108. {
  109. if(ret_status==0)
  110. {
  111. printf("\n\nSecure Channel Establishment between Source (E1) and Destination (E2) Enclaves successful !!!");
  112. }
  113. else
  114. {
  115. printf("\nSession establishment and key exchange failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  116. break;
  117. }
  118. }
  119. //Test Enclave to Enclave call between Enclave1(Source) and Enclave2(Destination)
  120. status = Enclave1_test_enclave_to_enclave_call(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  121. if (status!=SGX_SUCCESS)
  122. {
  123. printf("Enclave1_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  124. break;
  125. }
  126. else
  127. {
  128. if(ret_status==0)
  129. {
  130. printf("\n\nEnclave to Enclave Call between Source (E1) and Destination (E2) Enclaves successful !!!");
  131. }
  132. else
  133. {
  134. printf("\n\nEnclave to Enclave Call failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  135. break;
  136. }
  137. }
  138. //Test message exchange between Enclave1(Source) and Enclave2(Destination)
  139. status = Enclave1_test_message_exchange(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  140. if (status!=SGX_SUCCESS)
  141. {
  142. printf("Enclave1_test_message_exchange Ecall failed: Error code is %x", status);
  143. break;
  144. }
  145. else
  146. {
  147. if(ret_status==0)
  148. {
  149. printf("\n\nMessage Exchange between Source (E1) and Destination (E2) Enclaves successful !!!");
  150. }
  151. else
  152. {
  153. printf("\n\nMessage Exchange failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  154. break;
  155. }
  156. }
  157. //Test Create session between Enclave1(Source) and Enclave3(Destination)
  158. status = Enclave1_test_create_session(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  159. if (status!=SGX_SUCCESS)
  160. {
  161. printf("Enclave1_test_create_session Ecall failed: Error code is %x", status);
  162. break;
  163. }
  164. else
  165. {
  166. if(ret_status==0)
  167. {
  168. printf("\n\nSecure Channel Establishment between Source (E1) and Destination (E3) Enclaves successful !!!");
  169. }
  170. else
  171. {
  172. printf("\n\nSession establishment and key exchange failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  173. break;
  174. }
  175. }
  176. //Test Enclave to Enclave call between Enclave1(Source) and Enclave3(Destination)
  177. status = Enclave1_test_enclave_to_enclave_call(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  178. if (status!=SGX_SUCCESS)
  179. {
  180. printf("Enclave1_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  181. break;
  182. }
  183. else
  184. {
  185. if(ret_status==0)
  186. {
  187. printf("\n\nEnclave to Enclave Call between Source (E1) and Destination (E3) Enclaves successful !!!");
  188. }
  189. else
  190. {
  191. printf("\n\nEnclave to Enclave Call failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  192. break;
  193. }
  194. }
  195. //Test message exchange between Enclave1(Source) and Enclave3(Destination)
  196. status = Enclave1_test_message_exchange(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  197. if (status!=SGX_SUCCESS)
  198. {
  199. printf("Enclave1_test_message_exchange Ecall failed: Error code is %x", status);
  200. break;
  201. }
  202. else
  203. {
  204. if(ret_status==0)
  205. {
  206. printf("\n\nMessage Exchange between Source (E1) and Destination (E3) Enclaves successful !!!");
  207. }
  208. else
  209. {
  210. printf("\n\nMessage Exchange failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  211. break;
  212. }
  213. }
  214. //Test Create session between Enclave2(Source) and Enclave3(Destination)
  215. status = Enclave2_test_create_session(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  216. if (status!=SGX_SUCCESS)
  217. {
  218. printf("Enclave2_test_create_session Ecall failed: Error code is %x", status);
  219. break;
  220. }
  221. else
  222. {
  223. if(ret_status==0)
  224. {
  225. printf("\n\nSecure Channel Establishment between Source (E2) and Destination (E3) Enclaves successful !!!");
  226. }
  227. else
  228. {
  229. printf("\n\nSession establishment and key exchange failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  230. break;
  231. }
  232. }
  233. //Test Enclave to Enclave call between Enclave2(Source) and Enclave3(Destination)
  234. status = Enclave2_test_enclave_to_enclave_call(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  235. if (status!=SGX_SUCCESS)
  236. {
  237. printf("Enclave2_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  238. break;
  239. }
  240. else
  241. {
  242. if(ret_status==0)
  243. {
  244. printf("\n\nEnclave to Enclave Call between Source (E2) and Destination (E3) Enclaves successful !!!");
  245. }
  246. else
  247. {
  248. printf("\n\nEnclave to Enclave Call failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  249. break;
  250. }
  251. }
  252. //Test message exchange between Enclave2(Source) and Enclave3(Destination)
  253. status = Enclave2_test_message_exchange(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  254. if (status!=SGX_SUCCESS)
  255. {
  256. printf("Enclave2_test_message_exchange Ecall failed: Error code is %x", status);
  257. break;
  258. }
  259. else
  260. {
  261. if(ret_status==0)
  262. {
  263. printf("\n\nMessage Exchange between Source (E2) and Destination (E3) Enclaves successful !!!");
  264. }
  265. else
  266. {
  267. printf("\n\nMessage Exchange failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  268. break;
  269. }
  270. }
  271. //Test Create session between Enclave3(Source) and Enclave1(Destination)
  272. status = Enclave3_test_create_session(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  273. if (status!=SGX_SUCCESS)
  274. {
  275. printf("Enclave3_test_create_session Ecall failed: Error code is %x", status);
  276. break;
  277. }
  278. else
  279. {
  280. if(ret_status==0)
  281. {
  282. printf("\n\nSecure Channel Establishment between Source (E3) and Destination (E1) Enclaves successful !!!");
  283. }
  284. else
  285. {
  286. printf("\n\nSession establishment and key exchange failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  287. break;
  288. }
  289. }
  290. //Test Enclave to Enclave call between Enclave3(Source) and Enclave1(Destination)
  291. status = Enclave3_test_enclave_to_enclave_call(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  292. if (status!=SGX_SUCCESS)
  293. {
  294. printf("Enclave3_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  295. break;
  296. }
  297. else
  298. {
  299. if(ret_status==0)
  300. {
  301. printf("\n\nEnclave to Enclave Call between Source (E3) and Destination (E1) Enclaves successful !!!");
  302. }
  303. else
  304. {
  305. printf("\n\nEnclave to Enclave Call failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  306. break;
  307. }
  308. }
  309. //Test message exchange between Enclave3(Source) and Enclave1(Destination)
  310. status = Enclave3_test_message_exchange(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  311. if (status!=SGX_SUCCESS)
  312. {
  313. printf("Enclave3_test_message_exchange Ecall failed: Error code is %x", status);
  314. break;
  315. }
  316. else
  317. {
  318. if(ret_status==0)
  319. {
  320. printf("\n\nMessage Exchange between Source (E3) and Destination (E1) Enclaves successful !!!");
  321. }
  322. else
  323. {
  324. printf("\n\nMessage Exchange failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  325. break;
  326. }
  327. }
  328. //Test Closing Session between Enclave1(Source) and Enclave2(Destination)
  329. status = Enclave1_test_close_session(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  330. if (status!=SGX_SUCCESS)
  331. {
  332. printf("Enclave1_test_close_session Ecall failed: Error code is %x", status);
  333. break;
  334. }
  335. else
  336. {
  337. if(ret_status==0)
  338. {
  339. printf("\n\nClose Session between Source (E1) and Destination (E2) Enclaves successful !!!");
  340. }
  341. else
  342. {
  343. printf("\n\nClose session failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  344. break;
  345. }
  346. }
  347. //Test Closing Session between Enclave1(Source) and Enclave3(Destination)
  348. status = Enclave1_test_close_session(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  349. if (status!=SGX_SUCCESS)
  350. {
  351. printf("Enclave1_test_close_session Ecall failed: Error code is %x", status);
  352. break;
  353. }
  354. else
  355. {
  356. if(ret_status==0)
  357. {
  358. printf("\n\nClose Session between Source (E1) and Destination (E3) Enclaves successful !!!");
  359. }
  360. else
  361. {
  362. printf("\n\nClose session failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  363. break;
  364. }
  365. }
  366. //Test Closing Session between Enclave2(Source) and Enclave3(Destination)
  367. status = Enclave2_test_close_session(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  368. if (status!=SGX_SUCCESS)
  369. {
  370. printf("Enclave2_test_close_session Ecall failed: Error code is %x", status);
  371. break;
  372. }
  373. else
  374. {
  375. if(ret_status==0)
  376. {
  377. printf("\n\nClose Session between Source (E2) and Destination (E3) Enclaves successful !!!");
  378. }
  379. else
  380. {
  381. printf("\n\nClose session failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  382. break;
  383. }
  384. }
  385. //Test Closing Session between Enclave3(Source) and Enclave1(Destination)
  386. status = Enclave3_test_close_session(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  387. if (status!=SGX_SUCCESS)
  388. {
  389. printf("Enclave3_test_close_session Ecall failed: Error code is %x", status);
  390. break;
  391. }
  392. else
  393. {
  394. if(ret_status==0)
  395. {
  396. printf("\n\nClose Session between Source (E3) and Destination (E1) Enclaves successful !!!");
  397. }
  398. else
  399. {
  400. printf("\n\nClose session failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  401. break;
  402. }
  403. }
  404. #pragma warning (push)
  405. #pragma warning (disable : 4127)
  406. }while(0);
  407. #pragma warning (pop)
  408. sgx_destroy_enclave(e1_enclave_id);
  409. sgx_destroy_enclave(e2_enclave_id);
  410. sgx_destroy_enclave(e3_enclave_id);
  411. waitForKeyPress();
  412. return 0;
  413. }