feat(tee): 增加 CSV 报告离线校验器(2548B 布局 / Python 3.6 兼容)

- 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(海光节点自带版本),无第三方依赖。
This commit is contained in:
admin committed 2026-11-06 20:09:18 +08:00
1 parent eba460b4ff
commit 9c47e1076a
2 files changed
+237

No files matched your search

+24
View File
@@ -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(海光节点自带版本),不依赖第三方库。
+213
View File
@@ -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("<I", buf[OFF_POLICY:OFF_POLICY + 4])[0]
out["sig_algo_u32"] = struct.unpack("<I", buf[0x00B8:0x00BC])[0]
out["sig_usage_u32"] = struct.unpack("<I", buf[0x00B4:0x00B8])[0]
out["total_len"] = len(buf)
return out
def print_fields(buf, as_json):
try:
r = parse_report(buf)
except ValueError as e:
print("ERROR: %s" % e, file=sys.stderr)
return 2
if as_json:
doc = {
"total_len": r["total_len"],
"expected_len": REPORT_LEN,
"policy": r["policy_u32"],
"sig_usage": r["sig_usage_u32"],
"sig_algo": r["sig_algo_u32"],
"fields": {},
}
for name, off, ln in FIELDS:
doc["fields"][name] = {
"offset": "0x%04X" % off,
"len": ln,
"hex_head": _hex(r[name][:16]),
}
print(json.dumps(doc, ensure_ascii=False, indent=2))
return 0
print("报告总长 : %d 字节(期望 %d)" % (r["total_len"], REPORT_LEN))
print("policy : 0x%08X sig_algo: 0x%08X sig_usage: 0x%08X"
% (r["policy_u32"], r["sig_algo_u32"], r["sig_usage_u32"]))
print("-" * 78)
print("%-22s %-8s %-5s %s" % ("字段", "偏移", "长度", "内容(前 16B)"))
print("-" * 78)
for name, off, ln in FIELDS:
print("%-22s 0x%04X %-5d %s" % (name, off, ln, _hex(r[name][:16])))
print("-" * 78)
# 关键字段做非全零检查(与 Rust 解析器的判定口径一致)
problems = []
if not any(r["measure"]):
problems.append("measure 全零")
if not any(r["sig1"]):
problems.append("sig1 全零")
if not any(r["user_pubkey_digest"]):
problems.append("user_pubkey_digest 全零")
if problems:
print("结构检查: 不完整 -> %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("<I", buf, OFF_POLICY, 7)
struct.pack_into("<I", buf, 0x00B8, 0x0001) # sig_algo 占位
r = parse_report(bytes(buf))
assert r["total_len"] == 2548
assert r["user_data"] == ud, "user_data 偏移错误"
assert len(r["measure"]) == 32, "measure 长度应为 32"
assert len(r["sig1"]) == 144, "sig1 长度应为 144"
assert r["policy_u32"] == 7, "policy 偏移/端序错误"
# 少 1 字节必须被拒
try:
parse_report(bytes(buf)[:REPORT_LEN - 1])
except ValueError:
pass
else:
raise AssertionError("长度不足应被拒")
# 页填充到 4096 仍接受(下限语义)
padded = bytes(buf) + b"\x00" * (4096 - REPORT_LEN)
assert parse_report(padded)["user_data"] == ud
print("selftest: PASS(2548 布局、字段偏移、长度下限、4096 页填充)")
return 0
def main(argv=None):
ap = argparse.ArgumentParser(description="按海光 CSV 实测布局核对 attestation 报告")
ap.add_argument("--report", default="./report.cert", help="report.cert 路径(默认当前目录)")
ap.add_argument("--user-data", default=None, help="可选:64 字节十六进制挑战值,用于绑定比对")
ap.add_argument("--json", action="store_true", help="以 JSON 输出字段表")
ap.add_argument("--selftest", action="store_true", help="自检布局常量与解析逻辑")
args = ap.parse_args(argv)
if args.selftest:
return selftest()
try:
with open(args.report, "rb") as f:
buf = f.read()
except OSError as e:
print("ERROR: 读取 %s 失败: %s" % (args.report, e), file=sys.stderr)
return 2
rc = print_fields(buf, args.json)
if rc != 0:
return rc
if args.user_data is not None:
rc = compare_user_data(buf, args.user_data)
return rc
if __name__ == "__main__":
sys.exit(main())