Intel SGX学习笔记(2):用数组向Enclave传递5个数实现自增操作

写在前面

1、实现一个简单的Intel SGX的应用:非安全区定义初始化一个数组,数组里面存储5个数,然后向安全区(enclave)传入,在安全区中进行加减乘除,然后返回。

2、Intel SGX开发初学整体思路:
(1)首先在App.cpp中编写自己的需求,这里是应用程序区,也可以说是不可信区;
(2)App.cpp中需要Enclave进行安全计算的,在Enclave.edl中定义相应的函数接口,对特定形参要明确 in 和 out ;
(3)在Enclave.cpp中实现定义的接口,在安全区中进行具体编码操作;
(4)在Enclave.h进行一些头文件的函数声明。

一、App.cpp(应用程序区或非安全区)

1、在App.cpp文件中做两件事:
(1)定义和初始化数组;
(2)向安全区内传数组,调用Enclave函数。

在App.cpp的 int SGX_CDECL main(int argc, char *argv[]) 里面进行编写
// An highlighted block
int SGX_CDECL main(int argc, char *argv[])
{
    (void)(argc);
    (void)(argv);
    int array[5] = {1, 2, 3, 4, 5};//定义数组和初始化
    /* Initialize the enclave */
    if(initialize_enclave() < 0){
        printf("Enter a character before exit ...\n");
        getchar();
        return -1; 
    }
     /* Utilize edger8r attributes */
    edger8r_array_attributes();
    edger8r_type_attributes();
    edger8r_function_attributes();
      /* Utilize trusted libraries */
    ecall_libc_functions();
    ecall_libcxx_functions();
    ecall_thread_functions();
    printf("---------------------\n");
    printf(" 非安全区array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
    enclave_increment_array(global_eid,array,5);//调用Enclave函数
    printf("Modified array: ");//打印出修改后的数组
    for (int i = 0; i < 5; i++) {
        printf("%d ", array[i]);
       }
    printf("\n");
    /* Destroy the enclave */
    sgx_destroy_enclave(global_eid);
    return 0;
}

二、Enclave.edl(应用程序和enclave的接口文件,也就是我们上面用到的函数)

// An highlighted block
     trusted{
    public void enclave_increment_array([in,out] int array[5], int len);
}; 

注意:这里对于函数的注册,要注意 in 和out 的使用,用于指定是输入参数还是输出参数:
(1)in:表示该参数是输入参数,也就是说,该参数从应用程序传递到Enclave,并在Enclave中进行处理、修改或访问,但不会对应用程序产生任何影响;
(2)out:表示该参数是输出参数,也就是说,Enclave函数会使用该参数作为返回值,并将其传递回应用程序;
2、向安全区传递数组的时候,要明确数组的大小,不能定义一个自适应的空数组,这在SGX是不允许的。

三、Enclave.cpp(安全区函数实现,也就是我们具体的操作)

// An highlighted block
void enclave_increment_array(int array[5], int len) {
    for (int i = 0; i < len; i++) {
        printf("%d\n",array[i]);
    }
    for (int i = 0; i < len; i++) {
        //array[i] += 100;
        if(i==0){
           array[i]= array[i]+1;
        }else if(i==1){
           array[i]= array[i]*5;
        }else if(i==2){
           array[i]= array[i]+4; 
        }else if(i==3){
            array[i]= array[i]+3;
        }else if(i==4){
            array[i]= array[i]+10;
        }
    }
      printf("\n");
    for (int i = 0; i < len; i++) {
        printf("%d\n",array[i]);
    }
}

四、Enclave.h(头文件的函数声明)

#ifndef _ENCLAVE_H_
#define _ENCLAVE_H_
#include <assert.h>
#include <stdlib.h>
#if defined(__cplusplus)
extern "C" {
#endif

void enclave_increment_array(int array[5],  int len);

#if defined(__cplusplus)
}
#endif
#endif /* !_ENCLAVE_H_ */

五、运行结果

在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值