Files
admin 14eb9065de feat(tee): 接入海光 CSV 真实芯片验签,打通报告捕获到验签的端到端闭环
海光 CSV 报告的 user_data / mnonce / measure / pek_cert / sn 被 anonce
按 u32 数组逐个异或混淆,此前框架按明文比对,永远对不上。现按厂商 SDK
(csv_sdk/csv_status.c)规则还原(deobfuscate_csv_field),SNP 走明文分支。

另修两个真实 CLI bug:
- 全局 --report text|json 与 `tee verify --report <文件>` 撞名,导致
  tee verify 永远拿不到文件路径;改为上下文感知。
- tee verify 的 --tcb 默认 0,而 tee plan 用节点描述里的 tcb 算绑定值,
  二者不一致时报「user_data 与本次会话绑定不一致(疑似重放报告)」,
  把实现问题误导成安全事件。现给 tee verify 加 --node,site / measurement /
  tcb / platform 默认与 tee plan 同源,平台冲突直接拒绝。

新增真机可复现资产(deploy/tee/real-machine/):
- csv-attest-request.c:支持自定义挑战值取报告,自带 SM3 实现
  (厂商 get-attestation 把挑战值写死成 "user data",无法与会话绑定)
- csv-report-restore.py:还原 anonce 混淆字段
- csv-verify-attestation.sh:官方验签器 wrapper,堵住退出码恒 0、
  报告路径写死、离线证书 fallback 同目录、截断报告 fail-open 四个坑
- capture-csv-report.sh / verify-csv-report-e2e.sh / attest-init.sh /
  build-attest-initrd.sh

真机(192.168.1.102)实测:捕获 2548 字节真实报告,官方验签器完成
芯片签名与 hrk→hsk→cek→pek 全链验证,框架校验通过(structure_ok /
bound_ok / chip=已验证)。篡改 user_data、篡改 sig1、截断报告三种负向
场景全部 fail-closed。
2026-11-06 21:28:41 +08:00

314 lines
8.6 KiB
C
Raw Permalink 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.
/*
* csv-attest-request —— 海光 CSV guest 内取证明报告,**挑战值可自定义**。
*
* 为什么不用厂商的 get-attestation:它把挑战值写死成 ASCII "user data",
* 报告里的 user_data 就永远是那串固定字节,**无法与框架的会话绑定值对上**,
* 也就没法验证「这份报告确实是本轮会话的」。本工具按同样的 ioctl 契约,
* 把调用方给的 64 字节挑战值原样递给固件。
*
* ioctl 契约(来自 /opt/hygon/csv/attestation/csv-guest.c 与
* csv_sdk/ioctl_get_attestation_report.c):
* - 设备:/dev/csv-guest(misc device,mode 0777)
* - cmd :GET_ATTESTATION_REPORT = _IOWR('D', 1, struct csv_guest_mem)
* - struct csv_guest_mem { unsigned long va; int size; }
* - 用户态缓冲区:一整页 4096 字节,前 112 字节布局为
* user_data[64] + mnonce[16] + hash_block[32]
* 其中 hash = SM3(user_data[64] || mnonce[16]),
* 固件在同一个缓冲区里就地写回完整报告
* (sizeof(struct csv_attestation_report) = 2548 字节)。
*
* SM3 自行实现(GB/T 32905-2016),不依赖 openssl —— guest rootfs 里没有
* sm3.h 头文件,自带实现才能静态编进 initrd。
*
* 用法:
* csv-attest-request --user-data HEX64 [--out report.cert] [--nonce HEX32]
* csv-attest-request --user-data-from-cmdline mpc.tee.user_data=...
*
* 不带参数时沿用厂商默认挑战值 "user data",便于与 get-attestation 的行为对照。
*
* 编译:
* gcc -O2 -static -o csv-attest-request csv-attest-request.c
*/
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define CSV_GUEST_IOC_TYPE 'D'
#define GUEST_ATTESTATION_NONCE_SIZE 16
#define GUEST_ATTESTATION_DATA_SIZE 64
#define HASH_LEN 32
#define CSV_REPORT_LEN 2548
#define PAGE_SIZE 4096
struct csv_guest_mem {
unsigned long va;
int size;
};
#define GET_ATTESTATION_REPORT \
_IOWR(CSV_GUEST_IOC_TYPE, 1, struct csv_guest_mem)
/* ------------------------------------------------------------------ */
/* SM3 (GB/T 32905-2016) */
/* ------------------------------------------------------------------ */
static uint32_t rotl32(uint32_t x, int n)
{
return (x << n) | (x >> (32 - n));
}
static uint32_t sm3_p0(uint32_t x)
{
return x ^ rotl32(x, 9) ^ rotl32(x, 17);
}
static uint32_t sm3_p1(uint32_t x)
{
return x ^ rotl32(x, 15) ^ rotl32(x, 23);
}
static const uint32_t SM3_IV[8] = {
0x7380166fu, 0x4914b2b9u, 0x172442d7u, 0xda8a0600u,
0xa96f30bcu, 0x163138aau, 0xe38dee4du, 0xb0fb0e4eu
};
/* SM3 的 T 常量只有两个:第 0..15 轮用 0x79cc4519,第 16..63 轮用 0x7a879d8a。 */
#define SM3_T0 0x79cc4519u
#define SM3_T1 0x7a879d8au
static void sm3_block(uint32_t *V, const uint8_t *block)
{
uint32_t W[68];
uint32_t W1[64];
uint32_t A, B, C, D, E, F, G, H;
int i;
for (i = 0; i < 16; i++)
W[i] = ((uint32_t)block[i * 4] << 24) |
((uint32_t)block[i * 4 + 1] << 16) |
((uint32_t)block[i * 4 + 2] << 8) |
((uint32_t)block[i * 4 + 3]);
for (i = 16; i < 68; i++) {
/* W[j] = P1(W[j-16] ^ W[j-9] ^ rotl(W[j-3], 15)) ^ rotl(W[j-13], 7) ^ W[j-6] */
uint32_t x = W[i - 16] ^ W[i - 9] ^ rotl32(W[i - 3], 15);
W[i] = sm3_p1(x) ^ rotl32(W[i - 13], 7) ^ W[i - 6];
}
for (i = 0; i < 64; i++)
W1[i] = W[i] ^ W[i + 4];
A = V[0]; B = V[1]; C = V[2]; D = V[3];
E = V[4]; F = V[5]; G = V[6]; H = V[7];
for (i = 0; i < 64; i++) {
uint32_t SS1, SS2, TT1, TT2, T;
uint32_t FF = (i < 16) ? (A ^ B ^ C) : ((A & B) | (A & C) | (B & C));
uint32_t GG = (i < 16) ? (E ^ F ^ G) : ((E & F) | ((~E) & G));
T = (i < 16) ? SM3_T0 : SM3_T1;
SS1 = rotl32(rotl32(A, 12) + E + rotl32(T, i % 32), 7);
SS2 = SS1 ^ rotl32(A, 12);
TT1 = FF + D + SS2 + W1[i];
TT2 = GG + H + SS1 + W[i];
D = C;
C = rotl32(B, 9);
B = A;
A = TT1;
H = G;
G = rotl32(F, 19);
F = E;
E = sm3_p0(TT2);
}
V[0] ^= A; V[1] ^= B; V[2] ^= C; V[3] ^= D;
V[4] ^= E; V[5] ^= F; V[6] ^= G; V[7] ^= H;
}
/* SM3 摘要(输入长度 < 2^29 字节,单块/多块均支持)。 */
static void sm3(const uint8_t *data, size_t len, uint8_t out[32])
{
uint32_t V[8];
uint8_t block[64];
size_t i;
uint64_t bitlen = (uint64_t)len * 8;
size_t off;
memcpy(V, SM3_IV, sizeof(V));
for (off = 0; off + 64 <= len; off += 64)
sm3_block(V, data + off);
memset(block, 0, sizeof(block));
{
size_t rem = len - off;
memcpy(block, data + off, rem);
block[rem] = 0x80;
if (rem + 1 + 8 > 64) {
sm3_block(V, block);
memset(block, 0, sizeof(block));
}
for (i = 0; i < 8; i++)
block[56 + i] = (uint8_t)(bitlen >> (56 - 8 * i));
sm3_block(V, block);
}
for (i = 0; i < 8; i++) {
out[i * 4] = (uint8_t)(V[i] >> 24);
out[i * 4 + 1] = (uint8_t)(V[i] >> 16);
out[i * 4 + 2] = (uint8_t)(V[i] >> 8);
out[i * 4 + 3] = (uint8_t)V[i];
}
}
/* ------------------------------------------------------------------ */
static int hex2bin(const char *hex, unsigned char *out, size_t out_len)
{
size_t i;
size_t n = strlen(hex);
if (n != out_len * 2)
return -1;
for (i = 0; i < out_len; i++) {
unsigned int v;
char b[3] = { hex[i * 2], hex[i * 2 + 1], 0 };
if (sscanf(b, "%02x", &v) != 1)
return -1;
out[i] = (unsigned char)v;
}
return 0;
}
static void usage(const char *p)
{
fprintf(stderr,
"用法: %s [--user-data HEX64] [--out FILE] [--nonce HEX32]\n"
" %s --user-data-from-cmdline KEY\n",
p, p);
}
int main(int argc, char **argv)
{
/* 一整页:固件在同一个缓冲区里就地写回 2548 字节报告。 */
static unsigned char buf[PAGE_SIZE];
unsigned char *ud = buf;
unsigned char *nonce = buf + GUEST_ATTESTATION_DATA_SIZE;
unsigned char *hash = buf + GUEST_ATTESTATION_DATA_SIZE + GUEST_ATTESTATION_NONCE_SIZE;
const char *out_path = "report.cert";
const char *ud_hex = NULL;
const char *nonce_hex = NULL;
const char *cmdline_key = NULL;
struct csv_guest_mem mem;
int fd, i, rc = 1;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--user-data") && i + 1 < argc) {
ud_hex = argv[++i];
} else if (!strcmp(argv[i], "--nonce") && i + 1 < argc) {
nonce_hex = argv[++i];
} else if (!strcmp(argv[i], "--out") && i + 1 < argc) {
out_path = argv[++i];
} else if (!strcmp(argv[i], "--user-data-from-cmdline") && i + 1 < argc) {
cmdline_key = argv[++i];
} else {
usage(argv[0]);
return 2;
}
}
memset(buf, 0, sizeof(buf));
if (cmdline_key) {
FILE *f = fopen("/proc/cmdline", "r");
char line[4096];
size_t klen = strlen(cmdline_key);
int found = 0;
if (!f) {
fprintf(stderr, "打不开 /proc/cmdline: %s\n", strerror(errno));
return 1;
}
if (!fgets(line, sizeof(line), f)) {
fclose(f);
fprintf(stderr, "读 /proc/cmdline 失败\n");
return 1;
}
fclose(f);
{
char *save = NULL;
char *tok = strtok_r(line, " \t\n", &save);
while (tok) {
if (!strncmp(tok, cmdline_key, klen) && tok[klen]) {
ud_hex = tok + klen;
found = 1;
break;
}
tok = strtok_r(NULL, " \t\n", &save);
}
}
if (!found) {
fprintf(stderr, "/proc/cmdline 里没有 %s=...\n", cmdline_key);
return 1;
}
}
if (ud_hex) {
if (hex2bin(ud_hex, ud, GUEST_ATTESTATION_DATA_SIZE) != 0) {
fprintf(stderr, "挑战值必须是 128 位十六进制: %s\n", ud_hex);
return 2;
}
} else {
const char *dflt = "user data";
size_t l = strlen(dflt);
memset(ud, 0, GUEST_ATTESTATION_DATA_SIZE);
memcpy(ud, dflt, l);
}
if (nonce_hex) {
if (hex2bin(nonce_hex, nonce, GUEST_ATTESTATION_NONCE_SIZE) != 0) {
fprintf(stderr, "nonce 必须是 32 位十六进制: %s\n", nonce_hex);
return 2;
}
} else {
memset(nonce, 0, GUEST_ATTESTATION_NONCE_SIZE);
}
/* hash = SM3(user_data[64] || mnonce[16]) —— 与厂商 SDK 同口径。 */
sm3(buf, GUEST_ATTESTATION_DATA_SIZE + GUEST_ATTESTATION_NONCE_SIZE, hash);
fd = open("/dev/csv-guest", O_RDWR);
if (fd < 0) {
fprintf(stderr, "打开 /dev/csv-guest 失败: %s\n", strerror(errno));
return 1;
}
mem.va = (unsigned long)buf;
mem.size = PAGE_SIZE;
if (ioctl(fd, GET_ATTESTATION_REPORT, &mem) != 0) {
fprintf(stderr, "ioctl GET_ATTESTATION_REPORT 失败: %s\n", strerror(errno));
close(fd);
return 1;
}
close(fd);
{
FILE *f = fopen(out_path, "wb");
if (!f) {
fprintf(stderr, "写 %s 失败: %s\n", out_path, strerror(errno));
return 1;
}
if (fwrite(buf, 1, CSV_REPORT_LEN, f) != CSV_REPORT_LEN) {
fprintf(stderr, "写 %s 不完整\n", out_path);
fclose(f);
return 1;
}
fclose(f);
}
fprintf(stderr, "报告已写出: %s (%d 字节)\n", out_path, CSV_REPORT_LEN);
rc = 0;
return rc;
}