OP-TEE 介绍

OP-TEE (Open Portable Trusted Execution Environment) 软件架构图


1. 总体架构概览

 ┌─────────────────────────────────────────────────────────────────────┐
 │  REE (Rich Execution Environment) — Normal World                   │
 │  ┌──────────────────────────────────────────────────────────┐       │
 │  │  User Space                                               │       │
 │  │  ┌──────────┐  ┌──────────────────┐  ┌────────────────┐  │       │
 │  │  │  Client   │  │     libteec      │  │tee-supplicant │  │       │
 │  │  │Application│──│OP-TEE Client API │  │  REE Daemon   │  │       │
 │  │  └──────────┘  └────────┬─────────┘  └───────┬────────┘  │       │
 │  │                          │ ioctl              │ ioctl     │       │
 │  ├──────────────────────────┼────────────────────┼───────────┤       │
 │  │  Kernel Space            │                    │           │       │
 │  │  ┌───────────────────────▼────────────────────▼────────┐  │       │
 │  │  │              optee.ko (OP-TEE Kernel Driver)        │  │       │
 │  │  └───────────────────────┬─────────────────────────────┘  │       │
 │  └──────────────────────────┼────────────────────────────────┘       │
 └─────────────────────────────┼────────────────────────────────────────┘
                               │ SMC (Secure Monitor Call)
 ┌─────────────────────────────┼────────────────────────────────────────┐
 │  Monitor Mode               │                                         │
 │  ┌──────────────────────────▼──────────────────────────────────────┐ │
 │  │                   SMC Handler / Context Switch                  │ │
 │  └──────────────────────────┬──────────────────────────────────────┘ │
 └─────────────────────────────┼────────────────────────────────────────┘
                               │ SMC
 ┌─────────────────────────────┼────────────────────────────────────────┐
 │  TEE (Trusted Execution Environment) — Secure World                  │
 │  ┌──────────────────────────▼──────────────────────────────────────┐ │
 │  │  Secure Kernel Space: OP-TEE OS                                 │ │
 │  │  ┌─────────────┐ ┌──────────────┐ ┌──────────────────────────┐  │ │
 │  │  │ Core        │ │ Cryptographic│ │ Secure Storage           │  │ │
 │  │  │ Framework   │ │ API          │ │ (SFS + RPMB)             │  │ │
 │  │  └──────┬──────┘ └──────────────┘ └──────────────────────────┘  │ │
 │  └─────────┼───────────────────────────────────────────────────────┘ │
 │            │                                                         │
 │  ┌─────────▼──────────────────────────────────────────────────────┐ │
 │  │  Secure User Space: Trusted Applications (TAs)                 │ │
 │  │  ┌────────────────────┐  ┌────────────────────┐                │ │
 │  │  │  Trusted App #1    │  │  Trusted App #2    │   ...          │ │
 │  │  └────────────────────┘  └────────────────────┘                │ │
 │  └────────────────────────────────────────────────────────────────┘ │
 └─────────────────────────────────────────────────────────────────────┘
                               │
 ┌─────────────────────────────┼────────────────────────────────────────┐
 │  Hardware                   │                                         │
 │  ┌──────────────────────────▼──────────────────────────────────────┐ │
 │  │              ARM TrustZone 硬件安全扩展                           │ │
 │  │  (非安全世界 ↔ 安全世界的物理内存/外设隔离)                       │ │
 │  └─────────────────────────────────────────────────────────────────┘ │
 └─────────────────────────────────────────────────────────────────────┘

Secure Monitor

组件 代码位置 说明
TF-A BL31 ARM-software/arm-trusted-firmware — bl31/ 目录 EL3 异常级别运行,负责 SMC 路由、PSCI、世界切换
关键文件: bl31/bl31_main.c, bl31/aarch64/runtime_exceptions.S SMC 异常向量表入口
services/std_svc/ 标准服务 (PSCI)
services/spd/opteed/ OP-TEE 调度器 (OP-TEE Dispatcher) — 专门将 SMC 转发给 OP-TEE

2. 组件交互时序图

CA (REE User)    libteec      optee.ko     Secure Monitor    OP-TEE OS     TA
     │              │             │               │              │          │
     │══════════════════ Open Session ════════════════════════════│          │
     │──TEEC_InitCtx─>│             │               │              │          │
     │              │──ioctl──────>│               │              │          │
     │              │             │─────SMC───────>│              │          │
     │              │             │               │──ctx switch──>│          │
     │              │             │               │              │──load───>│
     │              │             │               │              │<──ack────│
     │              │             │               │<──SMC ret────│          │
     │              │             │<──ctx switch──│              │          │
     │              │<──session──│               │              │          │
     │<──SUCCESS────│             │               │              │          │
     │              │             │               │              │          │
     │══════════════════ Invoke Command ═════════════════════════│          │
     │──InvokeCmd──>│             │               │              │          │
     │              │──ioctl──────>│               │              │          │
     │              │             │─────SMC───────>│              │          │
     │              │             │               │──ctx switch──>│          │
     │              │             │               │              │──exec───>│
     │              │             │               │              │<──done───│
     │              │             │               │<──SMC ret────│          │
     │              │             │<──ctx switch──│              │          │
     │              │<──result───│               │              │          │
     │<──SUCCESS────│             │               │              │          │

3. 内存布局

 ARM Memory Address Space
 ┌──────────────────────────────────────────────────────────────────┐
 │  0x0000_0000                              0xBFFF_FFFF  0xC000_0000
 │  ├────────────────────────────────────────┼────────────────────┤
 │  │         REE Memory (3GB)               │  TEE Memory (1GB)  │
 │  │  ┌────────────────────────────────────┐│  ┌───────────────┐  │
 │  │  │ • Linux Kernel                    ││  │ • OP-TEE OS   │  │
 │  │  │ • User Space Applications         ││  │ • TAs          │  │
 │  │  │ • tee-supplicant                  ││  │ • Secure Stack │  │
 │  │  │ • Shared Memory (data exchange)   ││  │ • Shared Mem   │  │
 │  │  └────────────────────────────────────┘│  └───────────────┘  │
 │  │        NS bit = 1                      │    NS bit = 0        │
 │  └────────────────────────────────────────┴──────────────────────┘
 │                    ↑                                       ↑
 │                    │        ARM TrustZone Hardware Barrier  │
 │                    └──────────────────┬─────────────────────┘
 │                              Shared Memory Buffer
 │                       (用于REE ↔ TEE 数据交换)
 └──────────────────────────────────────────────────────────────────┘

4. 安全存储架构

 Secure World                         REE (Normal World)
 ┌──────────────────────┐             ┌──────────────────────┐
 │  Trusted Application │             │  tee-supplicant      │
 │         │            │             │         │            │
 │         ▼            │             │         │            │
 │  Trusted Storage API │───RPC──────>│   File I/O Handler   │
 │         │            │             │         │            │
 │         ▼            │             │         ▼            │
 │  Encrypted FS (SFS)  │             │  /data/tee/          │
 │  ┌────────────────┐  │             │  (encrypted blobs)   │
 │  │ Crypto Engine  │  │             │                      │
 │  │ (AES-GCM)      │  │             │  ┌────────────────┐  │
 │  └────────────────┘  │             │  │  RPMB Partition│  │
 │         │            │───RPMB──────>│  │ (eMMC/UFS)    │  │
 │  ┌────────────────┐  │             │  └────────────────┘  │
 │  │ HW Unique Key  │  │             └──────────────────────┘
 │  └────────────────┘  │
 └──────────────────────┘

5. 关键组件说明表

层级 组件 说明
REE 用户态 Client Application (CA) 普通世界应用,通过 TEEC API 请求安全服务
libteec OP-TEE Client API 用户态库,提供 TEEC_* 系列接口
tee-supplicant REE 守护进程,处理来自 TEE 的 RPC 请求(文件读写,加载TA文件等)
REE 内核态 optee.ko OP-TEE Linux 内核驱动,SMC 调用与共享内存管理
TEE Framework Linux 内核通用 TEE 子系统框架
Monitor SMC Handler 安全监控调用处理,负责 Normal/Secure World 上下文切换
安全用户态 Trusted Application (TA) 在安全世界运行的可信应用
安全内核态 OP-TEE OS 安全世界操作系统内核
Core Framework TA 管理、内存管理、IPC 等核心服务
Cryptographic API 基于 TEE Internal Core API 的密码学服务
Secure Storage 加密文件系统 (SFS) + RPMB 安全存储

6. 通信机制

 ┌──────────────────────────────────────────────────────────────────┐
 │                     OP-TEE 通信机制                               │
 ├────────────────┬───────────────────────┬─────────────────────────┤
 │    机制        │     方向               │    用途                 │
 ├────────────────┼───────────────────────┼─────────────────────────┤
 │  SMC           │  REE ↔ Monitor ↔ TEE  │  命令调用、上下文切换    │
 │  Shared Memory │  REE ↔ TEE (直接访问)  │  大数据批量传输          │
 │  Notification  │  TEE → REE (异步)      │  异步事件通知           │
 │  RPC           │  TEE → REE → TEE       │  TEE请求REE文件/IO服务  │
 └────────────────┴───────────────────────┴─────────────────────────┘

SMC 调用流程

REE                    Secure Monitor           TEE
  │                        │                      │
  │───────SMC #0──────────>│                      │
  │     (Standard Call)    │───context switch────>│
  │                        │                      │──execute──
  │                        │                      │<──done───
  │<──────SMC return───────│                      │
  │    (context restore)   │                      │

7. 源码参考

组件 仓库 语言
OP-TEE OS optee_os C
Client API optee_client C
Linux Kernel Driver optee_linuxdriver C
测试套件 optee_test C
构建系统 build Makefile

posted @ 2026-07-01 16:41  knight_xsx  阅读(3)  评论(0)    收藏  举报