David Dong

David Dong

Java/C/C#/Python

Java/C/C#/Python

POST

HelloWorld: my first SGX application

A simple sample code to get started with the SGX application development.

  1. New create VC++ Win32 Console Application. project The project name is HelloWorld.
  2. Create new VC++ Intel SGX Enclave project. psw Use default project name Enclave1.
  3. Edit Enclave1.edl file with below code. project 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);
	}
}
  1. Set the Enclave1 project configuration as below. project
  2. Build the Enclave1 project. project Above result shows the Enclave1 project has been build successfully. Next I need to add it into the HelloWorld project and call the foo() methods.
  3. Edit the main() method of HelloWorld.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;
}
  1. Set the HelloWorld project configuration. project
  2. Add the Enclave1 project into the HelloWorld project. Right click ‘Solution HelloWorld’ -> add -> existing project and select Enclave1 project. Now there are two projects under the ‘Solution HelloWorld’. project right click HelloWorld project -> Intel SGX Configuration -> Import Enclave project Select Enclave1.edl. project
    The Enclave1.edl file will be envoloved into the source of HelloWorld project. project
  3. Since the HelloWorld project is the main project, I need to add the dependency of Enclave1 project. Set the main project. project Set the dependency. project
  4. The configuration is done!
    Build the main project and start to run.
  5. Unfortunately, the trying is failed with the below error. project That is because my CPU cannot support SGX at present. I have to change the running mode to Simulation. project I got below result, which means my first app was runing well.:v:
    project

Windows

You may also like

further reading