From 69cfd1fca42f14ea764e91856b0d8a37af268944 Mon Sep 17 00:00:00 2001 From: asonnino Date: Sat, 28 Apr 2018 19:45:07 +0100 Subject: [PATCH] Destroy enclave before program exit & added simple Enclave Addition example --- App/App.cpp | 18 ++++++++++++++++++ Enclave/Add/Add.cpp | 14 ++++++++++++++ Enclave/Add/Add.edl | 18 ++++++++++++++++++ Enclave/Enclave.edl | 1 + Makefile | 2 +- 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Enclave/Add/Add.cpp create mode 100644 Enclave/Add/Add.edl diff --git a/App/App.cpp b/App/App.cpp index c0e9ebb..9502724 100644 --- a/App/App.cpp +++ b/App/App.cpp @@ -25,6 +25,16 @@ int main(int argc, char const *argv[]) { } printf("Random number: %d\n", ptr); + // Perform a simple addition in the enclave + int a = ptr; + int b = 100; + status = ecall_add(global_eid, &ptr, a, b); + if (status != SGX_SUCCESS) { + std::cout << "Enclave addition failed :(" << std::endl; + return 1; + } + std::cout << "Enclave addition success! " << a << " + " << b << " = " << ptr << std::endl; + // Seal the random number size_t sealed_size = sizeof(sgx_sealed_data_t) + sizeof(ptr); uint8_t* sealed_data = (uint8_t*)malloc(sealed_size); @@ -49,5 +59,13 @@ int main(int argc, char const *argv[]) { std::cout << "Seal round trip success! Receive back " << unsealed << std::endl; + + // Destroy enclave + status = sgx_destroy_enclave(global_eid); + if(status != SGX_SUCCESS) { + std::cout << "Fail to destroy enclave.\n" << std::endl; + return 1; + } + return 0; } diff --git a/Enclave/Add/Add.cpp b/Enclave/Add/Add.cpp new file mode 100644 index 0000000..e0f60d9 --- /dev/null +++ b/Enclave/Add/Add.cpp @@ -0,0 +1,14 @@ +#include "Enclave_t.h" + + +/* + *@brief Performs a simple addition in the enclave + * + * @param a The first input for our simple addition + * @param b The second input for our simple addition + * + * @return Truthy if addition successful, falsy otherwise. + */ +int ecall_add(int a, int b) { + return a + b; +} diff --git a/Enclave/Add/Add.edl b/Enclave/Add/Add.edl new file mode 100644 index 0000000..bb91d6d --- /dev/null +++ b/Enclave/Add/Add.edl @@ -0,0 +1,18 @@ +enclave { + + // ECALLs + trusted { + + /* + *@brief Performs a simple addition in the enclave + * + * @param a The first input for our simple addition + * @param b The second input for our simple addition + * + * @return Truthy if addition successful, falsy otherwise. + */ + public int ecall_add(int a, int b); + + }; + +}; diff --git a/Enclave/Enclave.edl b/Enclave/Enclave.edl index de78cab..703aaa3 100644 --- a/Enclave/Enclave.edl +++ b/Enclave/Enclave.edl @@ -1,4 +1,5 @@ enclave { + from "Add/Add.edl" import *; from "Sealing/Sealing.edl" import *; trusted { diff --git a/Makefile b/Makefile index d0ea32b..c9c59bb 100644 --- a/Makefile +++ b/Makefile @@ -117,7 +117,7 @@ endif Crypto_Library_Name := sgx_tcrypto # Enclave_Cpp_Files := Enclave/Enclave.cpp $(wildcard Enclave/Edger8rSyntax/*.cpp) $(wildcard Enclave/TrustedLibrary/*.cpp) -Enclave_Cpp_Files := Enclave/Enclave.cpp Enclave/Sealing/Sealing.cpp +Enclave_Cpp_Files := Enclave/Enclave.cpp Enclave/Sealing/Sealing.cpp Enclave/Add/Add.cpp # Enclave_Include_Paths := -IInclude -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport Enclave_Include_Paths := -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport