From 9c47e1076a030652a191ea3f9b16a9e17613c775 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 Nov 2026 20:09:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(tee):=20=E5=A2=9E=E5=8A=A0=20CSV=20?= =?UTF-8?q?=E6=8A=A5=E5=91=8A=E7=A6=BB=E7=BA=BF=E6=A0=A1=E9=AA=8C=E5=99=A8?= =?UTF-8?q?=EF=BC=882548B=20=E5=B8=83=E5=B1=80=20/=20Python=203.6=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - deploy/tee/real-machine/csv-report-check.py:按海光厂商 SDK 实测布局解析 report.cert,打印字段偏移表、核对 measure/sig1/user_pubkey_digest 非零、可选 --user-data 比对 @0x40 绑定;--selftest 自检 2548 布局、长度下限与 4096 页填充;--json 输出。仅做布局核对,不验签,签名/证书链交外部验签器。\n- README 增加「核对真实 CSV 报告」节,写明 2548≠4096、报告须在 CSV guest 内取(宿主无 /dev/csv-guest)、以及离线核对命令。\n- 兼容 Python 3.6(海光节点自带版本),无第三方依赖。 --- deploy/tee/real-machine/README.md | 24 +++ deploy/tee/real-machine/csv-report-check.py | 213 ++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100755 deploy/tee/real-machine/csv-report-check.py diff --git a/deploy/tee/real-machine/README.md b/deploy/tee/real-machine/README.md index 1052919..689053d 100644 --- a/deploy/tee/real-machine/README.md +++ b/deploy/tee/real-machine/README.md @@ -74,3 +74,27 @@ MPC_TEE_DRY_RUN=1 ./pair-run.sh # 先看物料与计划,不连机 - 在线 KBS 的 token 目前是占位值 `offline-demo`(stub 非 strict 模式放行并告警)。 生产必须换成证明代理产物 + 带证明校验的 KBS。 - `pair-run.sh` 只起 qemu,不做芯片证明互验;报告验签走 `mpc tee verify` 的外部验签器。 + +## 核对真实 CSV 报告(2548 字节) + +真机复核(2026-11-06,`192.168.1.102`)确认:海光厂商 SDK +`/opt/hygon/csv/attestation/csv_status.h` 里 `struct csv_attestation_report` 的 +`sizeof` 是 **2548 字节**,**不是 4096**(4096 只是 hypercall/mmap 的页大小)。 +`get-attestation` / `ioctl-get-attestation` 写出的 `report.cert` 就是 2548 字节; +把报告长度按 4096 做下限会拒掉每一份真实报告。字段偏移: +`user_data`@0x40、`measure`@0x90(32B SM3)、`policy`@0xB0、`sig1`@0xC0(144B)、 +`pek_cert`@0x150、`sn`@0x974,结构体内无 `version` 字段。 + +报告必须在 **CSV guest 内**取(宿主机没有 `/dev/csv-guest`,直接跑 +`get-attestation` 会 `open /dev/csv-guest failed`)。拿到 `report.cert` 后离线核对: + +```bash +python3 deploy/tee/real-machine/csv-report-check.py --selftest # 自检布局常量与解析 +python3 deploy/tee/real-machine/csv-report-check.py --report report.cert +python3 deploy/tee/real-machine/csv-report-check.py --report report.cert \ + --user-data <64字节挑战值hex> # 核对 user_data@0x40 与会话绑定一致 +``` + +脚本只做**布局与 `user_data` 绑定**核对,不验签;`sig1` / `pek_cert` 的签名与 +证书链交给厂商 `verify-attestation` 或 `mpc tee verify` 的外部验签器。 +脚本兼容 Python 3.6(海光节点自带版本),不依赖第三方库。 diff --git a/deploy/tee/real-machine/csv-report-check.py b/deploy/tee/real-machine/csv-report-check.py new file mode 100755 index 0000000..8382eba --- /dev/null +++ b/deploy/tee/real-machine/csv-report-check.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""按海光厂商 SDK 实测布局核对 CSV attestation 报告(report.cert)。 + +布局来源(2026-11-06 在 192.168.1.102 实测,不是照抄开源参考): + /opt/hygon/csv/attestation/csv_status.h 的 struct csv_attestation_report + 经本机 gcc 编译 offsetof/sizeof 得到,见 docs/TEE_DIRECT_LAUNCH.md。 + +关键事实:报告结构体 sizeof = 2548 字节;4096 只是 hypercall/mmap 的页大小。 +`get-attestation` / `ioctl-get-attestation` 写出的 report.cert 就是 2548 字节。 + +本脚本只做「字节布局核对」,**不验签**:sig1 / pek_cert 的签名与证书链语义 +由验签器(如厂商 verify-attestation 或框架 mpc tee verify 的外部验签器)负责。 + +用法: + # 在 CSV guest 内先产出报告(需 /dev/csv-guest 驱动): + # ./ioctl-get-attestation -> ./report.cert(2548B) + python3 csv-report-check.py --report ./report.cert + python3 csv-report-check.py --report ./report.cert --user-data <64B hex> --json + python3 csv-report-check.py --selftest # 自检布局常量与解析逻辑 + +兼容 Python 3.6(海光节点自带版本),不依赖第三方库。 +""" + +import argparse +import json +import struct +import sys + +# ---- 实测布局常量(字节偏移 / 长度)---- +REPORT_LEN = 2548 + +FIELDS = [ + ("user_pubkey_digest", 0x0000, 32), + ("vm_id", 0x0020, 16), + ("vm_version", 0x0030, 16), + ("user_data", 0x0040, 64), + ("mnonce", 0x0080, 16), + ("measure", 0x0090, 32), + ("policy", 0x00B0, 4), + ("sig_usage", 0x00B4, 4), + ("sig_algo", 0x00B8, 4), + ("anonce", 0x00BC, 4), + ("sig1", 0x00C0, 144), + ("pek_cert", 0x0150, 2084), + ("sn", 0x0974, 64), + ("reserved2", 0x09B4, 32), + ("mac", 0x09D4, 32), +] + +OFF_USER_DATA = 0x0040 +OFF_MEASURE = 0x0090 +OFF_SIG1 = 0x00C0 +OFF_POLICY = 0x00B0 +OFF_SN = 0x0974 + + +def _hex(b): + return b.hex() + + +def parse_report(buf): + """按实测布局切出字段;长度不足直接抛 ValueError。""" + if len(buf) < REPORT_LEN: + raise ValueError( + "报告长度 %d 不足,真实 CSV 报告应为 %d 字节(4096 是页大小,不是报告长度)" + % (len(buf), REPORT_LEN) + ) + out = {} + for name, off, ln in FIELDS: + out[name] = buf[off:off + ln] + # policy 是小端 u32 + out["policy_u32"] = struct.unpack(" %s" % ", ".join(problems)) + return 3 + print("结构检查: 通过(仅布局/非零;芯片签名未验证,需验签器)") + return 0 + + +def compare_user_data(buf, expected_hex): + try: + want = bytes.fromhex(expected_hex) + except ValueError: + print("ERROR: --user-data 不是合法十六进制", file=sys.stderr) + return 2 + if len(want) != 64: + print("ERROR: --user-data 需 64 字节(128 个十六进制字符),得到 %d 字节" % len(want), + file=sys.stderr) + return 2 + try: + r = parse_report(buf) + except ValueError as e: + print("ERROR: %s" % e, file=sys.stderr) + return 2 + got = r["user_data"] + if got == want: + print("user_data 绑定: 一致(报告 @0x40 等于给定挑战值)") + return 0 + print("user_data 绑定: 不一致") + print(" 期望: %s" % _hex(want)) + print(" 报告: %s" % _hex(got)) + return 4 + + +def selftest(): + # 构造一份 2548 字节的合成报告,填好关键字段,验证偏移与解析。 + buf = bytearray(REPORT_LEN) + ud = bytes(range(64)) + buf[OFF_USER_DATA:OFF_USER_DATA + 64] = ud + buf[OFF_MEASURE:OFF_MEASURE + 32] = b"\x5a" * 32 + buf[OFF_SIG1:OFF_SIG1 + 144] = b"\x33" * 144 + buf[0x0000:0x0000 + 32] = b"\x11" * 32 + struct.pack_into("