1. 隐私计算使用指南

1.1. 说明

  • Graphene 是官方原有的项目名称后来更改为Gramine,下文都改成Gramine

  • Gramine 使用v1.1,仓库地址:https://github.com/gramineproject/gramine

  • 1.1之前的版本对Golang的支持不友好,不能使用

  • 检查设备是否支持sgx

    • 如果设备不支持sgx可以使用simulation mode 运行

    • simulation mode 不能用于生产环境

1.2. 环境部署

1.2.1. 推荐配置

  • 系统:linux (不支持windows,本文教程使用ubuntu20.04)

  • 内核: 5.11+(推荐使用5.11以上版本)

  • 内存: 8G+

1.2.2. Gramine 环境配置

安装教程:https://gramine.readthedocs.io/en/latest/quickstart.html

1.2.3. 下载相关代码

1. git clone -b v2.2.1_private_contract https://git.chainmaker.org.cn/chainmaker/chainmaker-go.git
2. git clone -b v2.2.1_private_contract https://git.chainmaker.org.cn/chainmaker/graphene.git
3. git clone -b v2.2.1_private_contract https://git.chainmaker.org.cn/chainmaker/chainmaker-tee.git

1.2.4. 代码编译

1.2.4.1. CA准备

  • 准备CA

    • 在tee目录下的info_test.go文件中可以生成第三方根CA仅做测试使用

    • 或自行准备第三方CA

1.2.4.2. 编译Enclave-server

1.2.4.2.1. 准备 enclave-server.manifest.template文件
loader.preload = "file:{{ gramine.libos }}"
libos.entrypoint = "{{ entrypoint }}"
loader.log_level = "{{ log_level }}"

loader.env.LD_LIBRARY_PATH = "/lib:{{ arch_libdir }}:/usr/lib:/usr{{ arch_libdir }}"

loader.pal_internal_mem_size = "1G"
loader.insecure__use_cmdline_argv = true

sys.enable_sigterm_injection = true

fs.mount.lib.type = "chroot"
fs.mount.lib.path = "/lib"
fs.mount.lib.uri = "file:{{ gramine.runtimedir() }}"

fs.mount.lib2.type = "chroot"
fs.mount.lib2.path = "{{ arch_libdir }}"
fs.mount.lib2.uri = "file:{{ arch_libdir }}"


fs.mount.tmp.type = "chroot"
fs.mount.tmp.path = "/tmp"
fs.mount.tmp.uri = "file:/tmp"

# fs.mount.libos.path 
# fs.mount.libos.uri
# set Absolute Path
fs.mount.libos.type = "chroot"
fs.mount.libos.path = "/home/XXX/chainmaker-graphene/" # 设置绝对路径
fs.mount.libos.uri = "file:/home/XXX/chainmaker-graphene/"# 设置绝对路径

sgx.nonpie_binary = true
sgx.enclave_size = "16G"  #根据自己的机器配置型改,建议最小分配8G内存
sys.stack.size = "128M"
sgx.thread_num = 256      #根据机器配置优化调整

sgx.trusted_files = [
    "file:{{ entrypoint }}",
    "file:{{ gramine.runtimedir() }}/",
    "file:{{ arch_libdir }}/",
    "file:/usr{{ arch_libdir }}/",
    "file:/etc/mime.types",
    "file:/etc/default/apport",
]


sgx.allowed_files = [
    "file:/etc/nsswitch.conf",
    "file:/etc/ethers",
    "file:/etc/hosts",
    "file:/etc/group",
    "file:/etc/passwd",
    "file:/etc/gai.conf",
    "file:/etc/host.conf",
    "file:/etc/resolv.conf",
    "file:./configs/",
    "file:/tmp",
    "file:/home/XXX/chainmaker-graphene/",  #设置绝对路径
    "file:./logs",
]

其他字段可自行参考https://gramine.readthedocs.io/en/latest/manifest-syntax.html 进行设置enssl/include

  • 生成 enclave-key.pem

openssl genrsa -3 -out /home/XXX/chainmaker-graphene/enclave-key.pem 3072 //替换自己的目录
  • 执行 build.sh 文件编译

  sudo ./build.sh SIM //模拟模式下运行
  sudo ./build.sh SGX=1 DEBUG=1//硬件模式下运行(需要cpu支持)
  DEBUG=1 可选模式

1.2.4.3. 模拟模式

  • gramine-direct ./enclave-server -module=1

1.2.4.4. 硬件模式

  • 运行 gramine-sgx ./enclave-server

1.2.4.5. 签发证书

  • 第一次部署 初始化完成后enclave-server将生成csr文件

  • 使用csr文件在第三方CA处申请签发TEE证书

  • 将签发的TEE证书以PEM格式存于文件(in_teecert.pem)并放在chainmaker-graphene/configs目录下

  • 运行程序会自动校验和加载TEE证书

注: enclave启动成功,使用 netstat -ntlp 查询有 ./loader的占用标识enclave启动成功

1.2.4.6. 验证信息上链

  • 使用以下CMC命令调用系统合约将得到的report信息上链

cmc tee upload_report \
--sdk-conf-path={./testdata/sdk_config.yml(SDK配置文件路径)} \
--report={report路径} \
--admin-key-file-paths={key路径} \
--admin-crt-file-paths={证书路径}

注:若Enclave代码版本发生变化,需要再次执行步骤2将更新过的report信息重新上链

  • 将第三方CA的签名根证书上链

cmc tee upload_ca_cert \
--sdk-conf-path={./testdata/sdk_config.yml(SDK配置文件路径)} \
--ca_cert={根证书地址} \
--admin-key-file-paths={key路径} \
--admin-crt-file-paths={证书路径}

1.2.4.7. 编译隐私计算网关

cd ../gateway
go build main.go
./main start

1.2.4.8. 隐私计算网关配置config.yml

# 服务配置信息
settings:
   # web服务配置信息
   application:
      domain: localhost:9090
      host: 0.0.0.0
      ishttps: false # 是否启用https
      name: sgx      # 服务名称
      port: "8081"   # 服务端口号
      concurrency: 10  # 最大并发数
   # SDK客户端配置信息
   config:
      capaths: # 根证书路径,支持多个
         - cert/ca
      chainid: chain1  # 链ID
      conncnt: 1  # 节点连接数
      nodeaddr: 127.0.0.1:12301  # 节点地址,格式:127.0.0.1:12301
      orgid: wx-org1.chainmaker.org # 归属组织
      tlshostname: consensus1.tls.wx-org1.chainmaker.org  # TLS Hostname
      usercttpath: cert/client1.tls.crt  # 客户端用户私钥路径
      userkeypath: cert/client1.tls.key  # 客户端用户证书
   # 日志配置信息
   log:
      compress: 1  # 是否使用gzip压缩,默认不压缩
      level: debug # 日志等级,默认Info
      localtime: 1  # 日志时间戳是否为本地时间戳,默认UTC时间
      maxage: 30 #  最长保存天数,默认不删除
      maxbackups: 300 # 最多备份几个
      maxsize: 1024 # 日志文件大小,默认100M
      path: ./logs/gateway.log # 日志文件名
   # https 配置信息
   ssl:
      key: keystring  # 证书key
      pem: temp/pem.pem # 证书
   #grpc连接池配置
 internalClient:
    targeturl: ":50053"  #端口
    initcapacity: 20     #初始化连接数
    maxcapacity: 300     #最大连接数
    dialtimeout: 2       #拨号超时时间
    idletimeout: 6       #空闲超时时间
    readtimeout: 5       #读超时时间
    writetimeout: 5      #写超时时间
  internalServer:
    port: ":50052"       #访问server端口

1.3. 隐私计算网关接口

网关是用户调用隐私合约的入口,当前使用http接口方式进行调用。网关提供的接口主要包括远程证明、部署合约和调用合约三个接口。所有接口的请求method均使用post方式。使用方式参考示例,描述如下:

1.3.1. 示例参考

请参考gateway/service/tools/main.go

1.3.2. 部署合约接口

接口地址:http://x.x.x.x:port/private/deploy,其中x.x.x.x:port为服务地址,用户可以在配置里指定。

1.3.3. 执行隐私计算接口

接口地址:http://x.x.x.x:port/private/compute,其中x.x.x.x:port为服务地址,用户可以在配置里指定。

1.3.4. 远程证明接口

接口地址:http://x.x.x.x:port/private/remote_attestation,其中x.x.x.x:port为服务地址,用户可以在配置里指定。

1.4. 附录——推荐的支持隐私合约的服务器CPU型号

CPU序列号 型号及描述 SGX Enclave最大预留内存
6354 Ice Lake SP XCC Intel Xeon Gold 6345 18c 205W 3.0GHz 64GB
8360Y Ice Lake SP XCC Intel Xeon Platinum 8360Y 36c 250W 2.4GHz 64GB
6348 Ice Lake SP XCC Intel Xeon Gold 6348 28c 235W 2.6GHz 64GB
8380 Ice Lake SP XCC Intel Xeon 8380 40c 270W 2.3GHz 512GB
8368 Ice Lake SP XCC Intel Xeon Platinum 8368 38c 270W 2.4GHz 512GB
8368Q Ice Lake SP XCC Intel Xeon Platinum 8368Q 38c 270W 2.6GHz (liquid cooled) 512GB
8358 Ice Lake SP XCC Intel Xeon Platinum 8358 32c 250W 2.6GHz 64GB
8358P Ice Lake SP XCC Intel Xeon Platinum 8358P 32c 240W 2.6GHz 8GB
8352V Ice Lake SP XCC Intel Xeon Platinum 8352V 36c 195W 2.1GHz 8GB
8351N Ice Lake SP XCC Intel Xeon Platinum 8351N 36c 225W 2.4GHz 64GB
6314U Ice Lake SP XCC Intel Xeon Gold 6314U 32c 205W 2.3GHz 64GB
6338 Ice Lake SP XCC Intel Xeon Gold 6338 32c 205W 2.0GHz 64GB
6338N Ice Lake SP XCC Intel Xeon Gold 6338N 32c 185W 2.2GHz 64GB
8352Y Ice Lake SP XCC Intel Xeon Platinum 8352Y 32c 205W 2.2GHz 64GB
8352S Ice Lake SP XCC Intel Xeon Platinum 8352S 32c 205W 2.2GHz 512GB
6330 Ice Lake SP XCC Intel Xeon Gold 6330 28c 205W 2.0GHz 64GB
6330N Ice Lake SP XCC Intel Xeon Gold 6330N 28c 165W 2.2GHz 64GB
6346 Ice Lake SP XCC Intel Xeon Gold 6346 16c 205W 3.1GHz 64GB