POST
HelloWorld: my first SGX application
A simple sample code to get started with the SGX application development.
- New create VC++
Win32 Console Application
.The project name is
HelloWorld
. - Create new VC++
Intel SGX Enclave project
.Use default project name
Enclave1
. - Edit
Enclave1.edl
file with below code.This piece of code declares the
foo()
method as a trusted method and executes at trusted zone.
enclave {
from "sgx_tstdc.edl" import *;
trusted {
/* define ECALLs here. */
public void foo([out,size=len] char* buf,size_t len);
};
untrusted {
/* define OCALLs here. */
};
};
Edit Enclave1.cpp
. Realize the foo()
method.
#include "sgx_trts.h"
#include "Enclave1_t.h"
#include "sgx_trts.h"
#include <string.h>
void foo(char *buf, size_t len)
{
const char *secret = "Hello App!";
if (len > strlen(secret))
{
memcpy(buf, secret, strlen(secret) + 1);
}
}
- Set the
Enclave1
project configuration as below. - Build the
Enclave1
project.Above result shows the
Enclave1
project has been build successfully. Next I need to add it into theHelloWorld
project and call thefoo()
methods. - Edit the
main()
method ofHelloWorld.cpp
file.
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include "sgx_urts.h"
#include <string.h>
#include "Enclave1_u.h"
#define ENCLAVE_FILE _T("Enclave1.signed.dll")
#define MAX_BUF_LEN 100
int main()
{
sgx_enclave_id_t eid;
sgx_status_t ret = SGX_SUCCESS;
sgx_launch_token_t token = { 0 };
int updated = 0;
char buffer[MAX_BUF_LEN] = "Hello World!";
//create a enclave container
ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token,
&updated, &eid, NULL);
if (ret != SGX_SUCCESS)
{
printf("APP:error %#x ,failed to create enclave .\n", ret);
return -1;
}
//Enclave CALL(ECALL)
foo(eid, buffer, MAX_BUF_LEN);
printf("%s\n", buffer);
getchar();
//distory enclave container
if (SGX_SUCCESS != sgx_destroy_enclave(eid))
return -1;
system("pause");
getchar();
return 0;
}
- Set the
HelloWorld
project configuration. - Add the
Enclave1
project into theHelloWorld
project. Right click ‘Solution HelloWorld’ -> add -> existing project and selectEnclave1
project. Now there are two projects under the ‘Solution HelloWorld’.right click
HelloWorld
project -> Intel SGX Configuration -> Import EnclaveSelect
Enclave1.edl
.
TheEnclave1.edl
file will be envoloved into the source ofHelloWorld
project. - Since the
HelloWorld
project is the main project, I need to add the dependency ofEnclave1
project. Set the main project.Set the dependency.
- The configuration is done!
Build the main project and start to run. - Unfortunately, the trying is failed with the below error.
That is because my CPU cannot support SGX at present. I have to change the running mode to Simulation.
I got below result, which means my first app was runing well.