Files
ssw-mpc/build/build_optee.sh
T
rong b7263bcd26 feat: P3 OP-TEE —— nax-tee 进 TEE(core/proto/TA/CA 三层 + QEMU 脚本)
- nax-tee 转 no_std + alloc(mock 锁 std feature),Cargo.toml 显式写死
  不走 workspace 继承(拷进 teaclave-sdk examples workspace 后解析不崩)
- optee/core.rs:HKDF + AES-GCM + 设备 DNA 的 seal/unseal 逻辑(no_std)
- optee/proto.rs:CA↔TA 参数格式与错误码
- ta/ + ca/:薄壳 TA(参数搬运 + secure storage)与 TEEC 客户端
- build/:OP-TEE QEMU 构建与运行脚本(build_optee.sh / p3_optee_qemu.sh / run_qemu.py)
- CLAUDE.md:项目铁律与快速验证入口
2026-10-29 17:24:47 +08:00

133 lines
5.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 构建 nax-tee TA + CA(armv7 / armhf),签名并校验。
#
# 三层范式(op-tee-test 的 vault-core 模式):
# nax-tee/src/optee/core.rs 密码学与格式(no_std,宿主可测)
# crates/nax-tee/ta/ TA 薄壳(参数搬运 + RNG + secure storage)
# crates/nax-tee/ca/ CA 侧(OpteeTee 实现 Tee trait)
#
# 用法:
# # QEMU / 模拟设备 A
# NAX_SIM_DEV=nax-dev-A bash build/build_optee.sh
#
# # 模拟设备 B(拿到 A 的 blob 去解,应当失败)
# NAX_SIM_DEV=nax-dev-B OUT_PREFIX=nax-devB bash build/build_optee.sh
#
# # 真板:从 SLCR 读 DNA
# TA_FEATURES=board bash build/build_optee.sh
#
# 必需环境变量:
# OPTEE_OS optee_os 源码目录(已构建过,有 export-ta_arm32)
# SDK_DIR teaclave-trustzone-sdk 目录
# OPTEE_CLIENT_EXPORT optee_client 的 out/export(不给就用 $OPTEE_OS/../optee_client/out/export)
set -euo pipefail
REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OPTEE_OS="${OPTEE_OS:?需要指向 optee_os 源码目录}"
SDK_DIR="${SDK_DIR:?需要指向 teaclave-trustzone-sdk 目录}"
OPTEE_CLIENT_EXPORT="${OPTEE_CLIENT_EXPORT:-$(dirname "$OPTEE_OS")/optee_client/out/export}"
UUID="a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
TA_PKG="nax-tee-ta"
CA_PKG="nax-tee-ca"
TARGET="arm-unknown-linux-gnueabihf"
TA_FEATURES="${TA_FEATURES:-}"
OUT_PREFIX="${OUT_PREFIX:-nax-tee}"
OUT_DIR="${OUT_DIR:-$REPO/artifacts/optee}"
SIGN_KEY="${SIGN_KEY:-}" # 留空 = 开发密钥 default_ta.pem
# ---- TA dev kit ----
DK=""
for cand in "$OPTEE_OS/out/zynq7k/export-ta_arm32" "$OPTEE_OS/out/arm/export-ta_arm32" "$OPTEE_OS/out/qemu_virt/export-ta_arm32"; do
[ -d "$cand" ] && DK="$cand" && break
done
[ -n "$DK" ] || { echo "找不到 TA dev kit(先编 optee_os)" >&2; exit 1; }
export TA_DEV_KIT_DIR="$DK"
export CROSS_COMPILE="${CROSS_COMPILE:-arm-linux-gnueabihf-}"
export CARGO_TARGET_ARM_UNKNOWN_LINUX_GNUEABIHF_LINKER="${CROSS_COMPILE}gcc"
export CC_arm_unknown_linux_gnueabihf="${CROSS_COMPILE}gcc"
export RUSTUP_TOOLCHAIN="${RUSTUP_TOOLCHAIN:-nightly-2026-08-05}"
# 模拟设备身份:编译期注入(换值要重编)
if [ -n "${NAX_SIM_DEV:-}" ]; then
export NAX_SIM_DEV
echo "== 模拟设备身份 NAX_SIM_DEV=$NAX_SIM_DEV(编译期常量)=="
fi
echo "== 0. 把源码摆进 SDK 的 workspace =="
# nax-core + nax-tee 都要拷(TA/CA 依赖 nax-tee,nax-tee 依赖 nax-core)
for ws in ta ca; do
rm -rf "$SDK_DIR/examples/$ws/nax-core" "$SDK_DIR/examples/$ws/nax-tee"
cp -a "$REPO/crates/nax-core" "$SDK_DIR/examples/$ws/nax-core"
cp -a "$REPO/crates/nax-tee" "$SDK_DIR/examples/$ws/nax-tee"
rm -rf "$SDK_DIR/examples/$ws/nax-core/target" "$SDK_DIR/examples/$ws/nax-tee/target"
rm -rf "$SDK_DIR/examples/$ws/nax-core/.git" "$SDK_DIR/examples/$ws/nax-tee/.git"
# nax-tee 的 Cargo.toml 里 nax-core 用 workspace 依赖,拷过去后要改成本地 path
sed -i.bak 's|nax-core = { workspace = true.*|nax-core = { path = "../nax-core" }|' \
"$SDK_DIR/examples/$ws/nax-tee/Cargo.toml" 2>/dev/null || true
done
rm -rf "$SDK_DIR/examples/ta/$TA_PKG" "$SDK_DIR/examples/ca/$CA_PKG"
cp -a "$REPO/crates/nax-tee/ta" "$SDK_DIR/examples/ta/$TA_PKG"
cp -a "$REPO/crates/nax-tee/ca" "$SDK_DIR/examples/ca/$CA_PKG"
# TA/CA 的 Cargo.toml 里 nax-tee 用 path = "../..",拷过去后要改
sed -i.bak 's|nax-tee = { path = "../..".*|nax-tee = { path = "../nax-tee" }|' \
"$SDK_DIR/examples/ta/$TA_PKG/Cargo.toml" 2>/dev/null || true
sed -i.bak 's|nax-tee = { path = "../..".*|nax-tee = { path = "../nax-tee" }|' \
"$SDK_DIR/examples/ca/$CA_PKG/Cargo.toml" 2>/dev/null || true
# members 注入
python3 - "$SDK_DIR/examples/ta/Cargo.toml" "nax-core" "nax-tee" "$TA_PKG" <<'PY'
import sys
path = sys.argv[1]
for name in sys.argv[2:]:
s = open(path).read()
if f'"{name}"' not in s and "members" in s:
s = s.replace("members = [", f'members = [ "{name}",', 1)
open(path, "w").write(s)
PY
python3 - "$SDK_DIR/examples/ca/Cargo.toml" "nax-core" "nax-tee" "$CA_PKG" <<'PY'
import sys
path = sys.argv[1]
for name in sys.argv[2:]:
s = open(path).read()
if f'"{name}"' not in s and "members" in s:
s = s.replace("members = [", f'members = [ "{name}",', 1)
open(path, "w").write(s)
PY
echo "== 1. 编 TA =="
cd "$SDK_DIR/examples/ta/$TA_PKG"
FEATURE_ARGS=""
[ -n "$TA_FEATURES" ] && FEATURE_ARGS="--features $TA_FEATURES"
cargo build --target "$TARGET" --release $FEATURE_ARGS 2>&1 | tail -5
TA_ELF="$SDK_DIR/examples/ta/$TA_PKG/target/$TARGET/release/$TA_PKG"
[ -f "$TA_ELF" ] || { echo "TA ELF 没找到: $TA_ELF" >&2; exit 1; }
echo "== 2. 签名 TA =="
mkdir -p "$OUT_DIR"
TA_OUT="$OUT_DIR/${OUT_PREFIX}.ta"
SIGN_ARGS="--uuid $UUID --in $TA_ELF --out $TA_OUT"
[ -n "$SIGN_KEY" ] && SIGN_ARGS="$SIGN_ARGS --key $SIGN_KEY"
python3 "$TA_DEV_KIT_DIR/sign_encrypt.py" sign-enc $SIGN_ARGS
# 校验签名
python3 "$TA_DEV_KIT_DIR/sign_encrypt.py" verify --uuid $UUID --in $TA_OUT ${SIGN_KEY:+--key $SIGN_KEY} && echo "TA 签名校验 OK"
echo "== 3. 编 CA =="
cd "$SDK_DIR/examples/ca/$CA_PKG"
cargo build --target "$TARGET" --release 2>&1 | tail -5
CA_ELF="$SDK_DIR/examples/ca/$CA_PKG/target/$TARGET/release/$CA_PKG"
[ -f "$CA_ELF" ] || { echo "CA ELF 没找到: $CA_ELF" >&2; exit 1; }
cp "$CA_ELF" "$OUT_DIR/${OUT_PREFIX}-ca"
echo "== 产物 =="
ls -lh "$OUT_DIR/"
echo "OK"