Files
ssw-mpc/build/build_optee.sh
T
admin 54781b3f83
ci / gate (push) Waiting to run
refactor: 项目改名 ssw-mpc,crate 统一到 mpc-* 前缀
- 项目与文档标题:nax-rs → ssw-mpc(尚数网-多方安全计算),二进制 nax → mpc
- 18 个 crate 及目录 nax-* → mpc-*,Rust 引用 nax_* → mpc_*
- 环境变量、C 符号、构建/部署脚本:NAX_* → MPC_*、libnax_ray.so → libmpc_ray.so
- 保留协议域分隔常量与 refs/nax 参考实现路径,维持 golden 向量与历史数据兼容
- 新增 [profile.dev] incremental=false
2026-11-05 15:06:12 +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
# 构建 mpc-tee TA + CA(armv7 / armhf),签名并校验。
#
# 三层范式(op-tee-test 的 vault-core 模式):
# mpc-tee/src/optee/core.rs 密码学与格式(no_std,宿主可测)
# crates/mpc-tee/ta/ TA 薄壳(参数搬运 + RNG + secure storage)
# crates/mpc-tee/ca/ CA 侧(OpteeTee 实现 Tee trait)
#
# 用法:
# # QEMU / 模拟设备 A
# MPC_SIM_DEV=mpc-dev-A bash build/build_optee.sh
#
# # 模拟设备 B(拿到 A 的 blob 去解,应当失败)
# MPC_SIM_DEV=mpc-dev-B OUT_PREFIX=mpc-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="mpc-tee-ta"
CA_PKG="mpc-tee-ca"
TARGET="arm-unknown-linux-gnueabihf"
TA_FEATURES="${TA_FEATURES:-}"
OUT_PREFIX="${OUT_PREFIX:-mpc-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 "${MPC_SIM_DEV:-}" ]; then
export MPC_SIM_DEV
echo "== 模拟设备身份 MPC_SIM_DEV=$MPC_SIM_DEV(编译期常量)=="
fi
echo "== 0. 把源码摆进 SDK 的 workspace =="
# mpc-core + mpc-tee 都要拷(TA/CA 依赖 mpc-tee,mpc-tee 依赖 mpc-core)
for ws in ta ca; do
rm -rf "$SDK_DIR/examples/$ws/mpc-core" "$SDK_DIR/examples/$ws/mpc-tee"
cp -a "$REPO/crates/mpc-core" "$SDK_DIR/examples/$ws/mpc-core"
cp -a "$REPO/crates/mpc-tee" "$SDK_DIR/examples/$ws/mpc-tee"
rm -rf "$SDK_DIR/examples/$ws/mpc-core/target" "$SDK_DIR/examples/$ws/mpc-tee/target"
rm -rf "$SDK_DIR/examples/$ws/mpc-core/.git" "$SDK_DIR/examples/$ws/mpc-tee/.git"
# mpc-tee 的 Cargo.toml 里 mpc-core 用 workspace 依赖,拷过去后要改成本地 path
sed -i.bak 's|mpc-core = { workspace = true.*|mpc-core = { path = "../mpc-core" }|' \
"$SDK_DIR/examples/$ws/mpc-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/mpc-tee/ta" "$SDK_DIR/examples/ta/$TA_PKG"
cp -a "$REPO/crates/mpc-tee/ca" "$SDK_DIR/examples/ca/$CA_PKG"
# TA/CA 的 Cargo.toml 里 mpc-tee 用 path = "../..",拷过去后要改
sed -i.bak 's|mpc-tee = { path = "../..".*|mpc-tee = { path = "../mpc-tee" }|' \
"$SDK_DIR/examples/ta/$TA_PKG/Cargo.toml" 2>/dev/null || true
sed -i.bak 's|mpc-tee = { path = "../..".*|mpc-tee = { path = "../mpc-tee" }|' \
"$SDK_DIR/examples/ca/$CA_PKG/Cargo.toml" 2>/dev/null || true
# members 注入
python3 - "$SDK_DIR/examples/ta/Cargo.toml" "mpc-core" "mpc-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" "mpc-core" "mpc-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"