App.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2011-2018 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 __STDC_FORMAT_MACROS
  40. #include <inttypes.h>
  41. #define UNUSED(val) (void)(val)
  42. #define TCHAR char
  43. #define _TCHAR char
  44. #define _T(str) str
  45. #define scanf_s scanf
  46. #define _tmain main
  47. extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  48. sgx_enclave_id_t e1_enclave_id = 0;
  49. sgx_enclave_id_t e2_enclave_id = 0;
  50. sgx_enclave_id_t e3_enclave_id = 0;
  51. #define ENCLAVE1_PATH "libenclave1.so"
  52. #define ENCLAVE2_PATH "libenclave2.so"
  53. #define ENCLAVE3_PATH "libenclave3.so"
  54. void waitForKeyPress()
  55. {
  56. char ch;
  57. int temp;
  58. printf("\n\nHit a key....\n");
  59. temp = scanf_s("%c", &ch);
  60. }
  61. uint32_t load_enclaves()
  62. {
  63. uint32_t enclave_temp_no;
  64. int ret, launch_token_updated;
  65. sgx_launch_token_t launch_token;
  66. enclave_temp_no = 0;
  67. ret = sgx_create_enclave(ENCLAVE1_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e1_enclave_id, NULL);
  68. if (ret != SGX_SUCCESS) {
  69. return ret;
  70. }
  71. enclave_temp_no++;
  72. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e1_enclave_id, enclave_temp_no));
  73. ret = sgx_create_enclave(ENCLAVE2_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  74. if (ret != SGX_SUCCESS) {
  75. return ret;
  76. }
  77. enclave_temp_no++;
  78. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e2_enclave_id, enclave_temp_no));
  79. ret = sgx_create_enclave(ENCLAVE3_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e3_enclave_id, NULL);
  80. if (ret != SGX_SUCCESS) {
  81. return ret;
  82. }
  83. enclave_temp_no++;
  84. g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e3_enclave_id, enclave_temp_no));
  85. return SGX_SUCCESS;
  86. }
  87. int _tmain(int argc, _TCHAR* argv[])
  88. {
  89. uint32_t ret_status;
  90. sgx_status_t status;
  91. UNUSED(argc);
  92. UNUSED(argv);
  93. if(load_enclaves() != SGX_SUCCESS)
  94. {
  95. printf("\nLoad Enclave Failure");
  96. }
  97. printf("\nAvailable Enclaves");
  98. printf("\nEnclave1 - EnclaveID %" PRIx64, e1_enclave_id);
  99. printf("\nEnclave2 - EnclaveID %" PRIx64, e2_enclave_id);
  100. printf("\nEnclave3 - EnclaveID %" PRIx64, e3_enclave_id);
  101. do
  102. {
  103. //Test Create session between Enclave1(Source) and Enclave2(Destination)
  104. status = Enclave1_test_create_session(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  105. if (status!=SGX_SUCCESS)
  106. {
  107. printf("Enclave1_test_create_session Ecall failed: Error code is %x", status);
  108. break;
  109. }
  110. else
  111. {
  112. if(ret_status==0)
  113. {
  114. printf("\n\nSecure Channel Establishment between Source (E1) and Destination (E2) Enclaves successful !!!");
  115. }
  116. else
  117. {
  118. printf("\nSession establishment and key exchange failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  119. break;
  120. }
  121. }
  122. //Test Enclave to Enclave call between Enclave1(Source) and Enclave2(Destination)
  123. status = Enclave1_test_enclave_to_enclave_call(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  124. if (status!=SGX_SUCCESS)
  125. {
  126. printf("Enclave1_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  127. break;
  128. }
  129. else
  130. {
  131. if(ret_status==0)
  132. {
  133. printf("\n\nEnclave to Enclave Call between Source (E1) and Destination (E2) Enclaves successful !!!");
  134. }
  135. else
  136. {
  137. printf("\n\nEnclave to Enclave Call failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  138. break;
  139. }
  140. }
  141. //Test message exchange between Enclave1(Source) and Enclave2(Destination)
  142. status = Enclave1_test_message_exchange(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  143. if (status!=SGX_SUCCESS)
  144. {
  145. printf("Enclave1_test_message_exchange Ecall failed: Error code is %x", status);
  146. break;
  147. }
  148. else
  149. {
  150. if(ret_status==0)
  151. {
  152. printf("\n\nMessage Exchange between Source (E1) and Destination (E2) Enclaves successful !!!");
  153. }
  154. else
  155. {
  156. printf("\n\nMessage Exchange failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  157. break;
  158. }
  159. }
  160. //Test Create session between Enclave1(Source) and Enclave3(Destination)
  161. status = Enclave1_test_create_session(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  162. if (status!=SGX_SUCCESS)
  163. {
  164. printf("Enclave1_test_create_session Ecall failed: Error code is %x", status);
  165. break;
  166. }
  167. else
  168. {
  169. if(ret_status==0)
  170. {
  171. printf("\n\nSecure Channel Establishment between Source (E1) and Destination (E3) Enclaves successful !!!");
  172. }
  173. else
  174. {
  175. printf("\n\nSession establishment and key exchange failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  176. break;
  177. }
  178. }
  179. //Test Enclave to Enclave call between Enclave1(Source) and Enclave3(Destination)
  180. status = Enclave1_test_enclave_to_enclave_call(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  181. if (status!=SGX_SUCCESS)
  182. {
  183. printf("Enclave1_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  184. break;
  185. }
  186. else
  187. {
  188. if(ret_status==0)
  189. {
  190. printf("\n\nEnclave to Enclave Call between Source (E1) and Destination (E3) Enclaves successful !!!");
  191. }
  192. else
  193. {
  194. printf("\n\nEnclave to Enclave Call failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  195. break;
  196. }
  197. }
  198. //Test message exchange between Enclave1(Source) and Enclave3(Destination)
  199. status = Enclave1_test_message_exchange(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  200. if (status!=SGX_SUCCESS)
  201. {
  202. printf("Enclave1_test_message_exchange Ecall failed: Error code is %x", status);
  203. break;
  204. }
  205. else
  206. {
  207. if(ret_status==0)
  208. {
  209. printf("\n\nMessage Exchange between Source (E1) and Destination (E3) Enclaves successful !!!");
  210. }
  211. else
  212. {
  213. printf("\n\nMessage Exchange failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  214. break;
  215. }
  216. }
  217. //Test Create session between Enclave2(Source) and Enclave3(Destination)
  218. status = Enclave2_test_create_session(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  219. if (status!=SGX_SUCCESS)
  220. {
  221. printf("Enclave2_test_create_session Ecall failed: Error code is %x", status);
  222. break;
  223. }
  224. else
  225. {
  226. if(ret_status==0)
  227. {
  228. printf("\n\nSecure Channel Establishment between Source (E2) and Destination (E3) Enclaves successful !!!");
  229. }
  230. else
  231. {
  232. printf("\n\nSession establishment and key exchange failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  233. break;
  234. }
  235. }
  236. //Test Enclave to Enclave call between Enclave2(Source) and Enclave3(Destination)
  237. status = Enclave2_test_enclave_to_enclave_call(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  238. if (status!=SGX_SUCCESS)
  239. {
  240. printf("Enclave2_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  241. break;
  242. }
  243. else
  244. {
  245. if(ret_status==0)
  246. {
  247. printf("\n\nEnclave to Enclave Call between Source (E2) and Destination (E3) Enclaves successful !!!");
  248. }
  249. else
  250. {
  251. printf("\n\nEnclave to Enclave Call failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  252. break;
  253. }
  254. }
  255. //Test message exchange between Enclave2(Source) and Enclave3(Destination)
  256. status = Enclave2_test_message_exchange(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  257. if (status!=SGX_SUCCESS)
  258. {
  259. printf("Enclave2_test_message_exchange Ecall failed: Error code is %x", status);
  260. break;
  261. }
  262. else
  263. {
  264. if(ret_status==0)
  265. {
  266. printf("\n\nMessage Exchange between Source (E2) and Destination (E3) Enclaves successful !!!");
  267. }
  268. else
  269. {
  270. printf("\n\nMessage Exchange failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  271. break;
  272. }
  273. }
  274. //Test Create session between Enclave3(Source) and Enclave1(Destination)
  275. status = Enclave3_test_create_session(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  276. if (status!=SGX_SUCCESS)
  277. {
  278. printf("Enclave3_test_create_session Ecall failed: Error code is %x", status);
  279. break;
  280. }
  281. else
  282. {
  283. if(ret_status==0)
  284. {
  285. printf("\n\nSecure Channel Establishment between Source (E3) and Destination (E1) Enclaves successful !!!");
  286. }
  287. else
  288. {
  289. printf("\n\nSession establishment and key exchange failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  290. break;
  291. }
  292. }
  293. //Test Enclave to Enclave call between Enclave3(Source) and Enclave1(Destination)
  294. status = Enclave3_test_enclave_to_enclave_call(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  295. if (status!=SGX_SUCCESS)
  296. {
  297. printf("Enclave3_test_enclave_to_enclave_call Ecall failed: Error code is %x", status);
  298. break;
  299. }
  300. else
  301. {
  302. if(ret_status==0)
  303. {
  304. printf("\n\nEnclave to Enclave Call between Source (E3) and Destination (E1) Enclaves successful !!!");
  305. }
  306. else
  307. {
  308. printf("\n\nEnclave to Enclave Call failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  309. break;
  310. }
  311. }
  312. //Test message exchange between Enclave3(Source) and Enclave1(Destination)
  313. status = Enclave3_test_message_exchange(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  314. if (status!=SGX_SUCCESS)
  315. {
  316. printf("Enclave3_test_message_exchange Ecall failed: Error code is %x", status);
  317. break;
  318. }
  319. else
  320. {
  321. if(ret_status==0)
  322. {
  323. printf("\n\nMessage Exchange between Source (E3) and Destination (E1) Enclaves successful !!!");
  324. }
  325. else
  326. {
  327. printf("\n\nMessage Exchange failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  328. break;
  329. }
  330. }
  331. //Test Closing Session between Enclave1(Source) and Enclave2(Destination)
  332. status = Enclave1_test_close_session(e1_enclave_id, &ret_status, e1_enclave_id, e2_enclave_id);
  333. if (status!=SGX_SUCCESS)
  334. {
  335. printf("Enclave1_test_close_session Ecall failed: Error code is %x", status);
  336. break;
  337. }
  338. else
  339. {
  340. if(ret_status==0)
  341. {
  342. printf("\n\nClose Session between Source (E1) and Destination (E2) Enclaves successful !!!");
  343. }
  344. else
  345. {
  346. printf("\n\nClose session failure between Source (E1) and Destination (E2): Error code is %x", ret_status);
  347. break;
  348. }
  349. }
  350. //Test Closing Session between Enclave1(Source) and Enclave3(Destination)
  351. status = Enclave1_test_close_session(e1_enclave_id, &ret_status, e1_enclave_id, e3_enclave_id);
  352. if (status!=SGX_SUCCESS)
  353. {
  354. printf("Enclave1_test_close_session Ecall failed: Error code is %x", status);
  355. break;
  356. }
  357. else
  358. {
  359. if(ret_status==0)
  360. {
  361. printf("\n\nClose Session between Source (E1) and Destination (E3) Enclaves successful !!!");
  362. }
  363. else
  364. {
  365. printf("\n\nClose session failure between Source (E1) and Destination (E3): Error code is %x", ret_status);
  366. break;
  367. }
  368. }
  369. //Test Closing Session between Enclave2(Source) and Enclave3(Destination)
  370. status = Enclave2_test_close_session(e2_enclave_id, &ret_status, e2_enclave_id, e3_enclave_id);
  371. if (status!=SGX_SUCCESS)
  372. {
  373. printf("Enclave2_test_close_session Ecall failed: Error code is %x", status);
  374. break;
  375. }
  376. else
  377. {
  378. if(ret_status==0)
  379. {
  380. printf("\n\nClose Session between Source (E2) and Destination (E3) Enclaves successful !!!");
  381. }
  382. else
  383. {
  384. printf("\n\nClose session failure between Source (E2) and Destination (E3): Error code is %x", ret_status);
  385. break;
  386. }
  387. }
  388. //Test Closing Session between Enclave3(Source) and Enclave1(Destination)
  389. status = Enclave3_test_close_session(e3_enclave_id, &ret_status, e3_enclave_id, e1_enclave_id);
  390. if (status!=SGX_SUCCESS)
  391. {
  392. printf("Enclave3_test_close_session Ecall failed: Error code is %x", status);
  393. break;
  394. }
  395. else
  396. {
  397. if(ret_status==0)
  398. {
  399. printf("\n\nClose Session between Source (E3) and Destination (E1) Enclaves successful !!!");
  400. }
  401. else
  402. {
  403. printf("\n\nClose session failure between Source (E3) and Destination (E1): Error code is %x", ret_status);
  404. break;
  405. }
  406. }
  407. #pragma warning (push)
  408. #pragma warning (disable : 4127)
  409. }while(0);
  410. #pragma warning (pop)
  411. sgx_destroy_enclave(e1_enclave_id);
  412. sgx_destroy_enclave(e2_enclave_id);
  413. sgx_destroy_enclave(e3_enclave_id);
  414. waitForKeyPress();
  415. return 0;
  416. }