intel SGX学习笔记(8):SGX在安全区Enclave中调用ecall函数实现Hello world 输出
发布时间:2024年01月13日
intel SGX学习笔记(8):SGX在安全区Enclave中调用ecall函数实现Hello world 输出
一、Enclave.edl注册函数
enclave {
include "user_types.h"/* buffer_t *//* Import ECALL/OCALL from sub-directory EDLs.
* [from]: specifies the location of EDL file.
* [import]: specifies the functions to import,
* [*]: implies to import all functions.
*/
from "Edger8rSyntax/Types.edl"import*;
from "Edger8rSyntax/Pointers.edl"import*;
from "Edger8rSyntax/Arrays.edl"import*;
from "Edger8rSyntax/Functions.edl"import*;
from "TrustedLibrary/Libc.edl"import*;
from "TrustedLibrary/Libcxx.edl"importecall_exception, ecall_map;
from "TrustedLibrary/Thread.edl"import*;
trusted{publicvoidprintf_helloworld();};/*
* ocall_print_string - invokes OCALL to display string buffer inside the enclave.
* [in]: copy the string buffer to App outside.
* [string]: specifies 'str' is a NULL terminated buffer.
*/
untrusted {voidocall_print_string([in, string]constchar*str);};};
二、Enclave.cpp实现函数printf_helloworld
#include"Enclave.h"#include"Enclave_t.h"/* print_string */#include<stdarg.h>#include<stdio.h>/* vsnprintf */#include<string.h>/*
* printf:
* Invokes OCALL to display the enclave buffer to the terminal.
*/intprintf(constchar* fmt,...){char buf[BUFSIZ]={'\0'};
va_list ap;va_start(ap, fmt);vsnprintf(buf, BUFSIZ, fmt, ap);va_end(ap);ocall_print_string(buf);return(int)strnlen(buf, BUFSIZ -1)+1;}voidprintf_helloworld(){printf("Hello world !!!!!!!!!\n");}
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<assert.h>#include<unistd.h>#include<pwd.h>#defineMAX_PATHFILENAME_MAX#include"sgx_urts.h"#include"App.h"#include"Enclave_u.h"#defineERROR-1/* Global EID shared by multiple threads */
sgx_enclave_id_t global_eid =0;typedefstruct_sgx_errlist_t{
sgx_status_t err;constchar*msg;constchar*sug;/* Suggestion */} sgx_errlist_t;/* Error code returned by sgx_create_enclave */static sgx_errlist_t sgx_errlist[]={{
SGX_ERROR_UNEXPECTED,"Unexpected error occurred.",NULL},{
SGX_ERROR_INVALID_PARAMETER,"Invalid parameter.",NULL},{
SGX_ERROR_OUT_OF_MEMORY,"Out of memory.",NULL},{
SGX_ERROR_ENCLAVE_LOST,"Power transition occurred.","Please refer to the sample \"PowerTransition\" for details."},{
SGX_ERROR_INVALID_ENCLAVE,"Invalid enclave image.",NULL},{
SGX_ERROR_INVALID_ENCLAVE_ID,"Invalid enclave identification.",NULL},{
SGX_ERROR_INVALID_SIGNATURE,"Invalid enclave signature.",NULL},{
SGX_ERROR_OUT_OF_EPC,"Out of EPC memory.",NULL},{
SGX_ERROR_NO_DEVICE,"Invalid SGX device.","Please make sure SGX module is enabled in the BIOS, and install SGX driver afterwards."},{
SGX_ERROR_MEMORY_MAP_CONFLICT,"Memory map conflicted.",NULL},{
SGX_ERROR_INVALID_METADATA,"Invalid enclave metadata.",NULL},{
SGX_ERROR_DEVICE_BUSY,"SGX device was busy.",NULL},{
SGX_ERROR_INVALID_VERSION,"Enclave version was invalid.",NULL},{
SGX_ERROR_INVALID_ATTRIBUTE,"Enclave was not authorized.",NULL},{
SGX_ERROR_ENCLAVE_FILE_ACCESS,"Can't open enclave file.",NULL},{
SGX_ERROR_MEMORY_MAP_FAILURE,"Failed to reserve memory for the enclave.",NULL},};/* Check error conditions for loading enclave */voidprint_error_message(sgx_status_t ret){
size_t idx =0;
size_t ttl =sizeof sgx_errlist/sizeof sgx_errlist[0];for(idx =0; idx < ttl; idx++){if(ret == sgx_errlist[idx].err){if(NULL!= sgx_errlist[idx].sug)printf("Info: %s\n", sgx_errlist[idx].sug);printf("Error: %s\n", sgx_errlist[idx].msg);break;}}if(idx == ttl)printf("Error code is 0x%X. Please refer to the \"Intel SGX SDK Developer Reference\" for more details.\n", ret);}/* Initialize the enclave:
* Call sgx_create_enclave to initialize an enclave instance
*/intinitialize_enclave(void){
sgx_status_t ret = SGX_ERROR_UNEXPECTED;/* Call sgx_create_enclave to initialize an enclave instance *//* Debug Support: set 2nd parameter to 1 */
ret =sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG,NULL,NULL,&global_eid,NULL);if(ret != SGX_SUCCESS){print_error_message(ret);return-1;}return0;}/* OCall functions */voidocall_print_string(constchar*str){/* Proxy/Bridge will check the length and null-terminate
* the input string to prevent buffer overflow.
*/printf("%s", str);}/* Application entry */int SGX_CDECL main(int argc,char*argv[]){(void)(argc);(void)(argv);/* Initialize the enclave */if(initialize_enclave()<0){printf("Enter a character before exit ...\n");getchar();return-1;}printf("+++++++++++++++++++++\n");printf_helloworld(global_eid);printf_helloworld(global_eid);printf_helloworld(global_eid);sgx_destroy_enclave(global_eid);return0;}