阶段 B 步骤 6:.stb 写侧新格式(头 104 + 型号标识 + SHA-256 文件尾)。

- Stb:kHeaderSize 104、kSha256Size 32;SHA-256 实现(FIPS 180-4,含标准测试向量验证);
  fill_model_id(name+version 拼 32 字节补 '\0');StbView 解析新格式 +
  model_id()/model_matches()/sha_ok()(数据段不含尾、缺尾报错)
- Codegen:assemble_image 写型号标识(偏移 72)+ 文件尾 SHA-256
- main.cpp:--disasm 显示 model/sha;写后自检(ok+sha+型号匹配,失败报错)
- 测试:machine_test +SHA-256 已知向量(空串/abc)与型号标识断言(53 断言);
  codegen_test data_len 断言 +32(SHA 尾);ctest 12/12
- 验证:line1 288 字节 model=STATOR1 sha=ok;篡改一字节 sha=BAD
This commit is contained in:
2026-08-21 22:20:35 +08:00
parent e187ed7b03
commit a308ae713d
7 changed files with 245 additions and 15 deletions
+17 -1
View File
@@ -694,7 +694,8 @@ namespace {
code_total += static_cast<uint32_t>(f.code.size()) * 4;
}
const uint32_t off_data = off_code + code_total;
const uint32_t off_end = off_data + static_cast<uint32_t>(data_.size());
const uint32_t off_end = off_data + static_cast<uint32_t>(data_.size()) +
static_cast<uint32_t>(kSha256Size);
std::vector<uint8_t>& b = *image_;
b.assign(off_end, 0);
@@ -728,6 +729,13 @@ namespace {
put_le32(b, 64, off_data);
put_le32(b, 68, off_data);
// 型号标识[32](偏移 7212.13 修订)
char mid[kModelIdSize];
fill_model_id(cfg_.model_name(), cfg_.version(), mid);
for (size_t i = 0; i < kModelIdSize; ++i) {
b[72 + i] = static_cast<uint8_t>(mid[i]);
}
for (size_t i = 0; i < consts_.size(); ++i) {
const size_t o = off_const + i * kConstEntrySize;
put_le32(b, o, consts_[i].tag);
@@ -755,6 +763,14 @@ namespace {
for (size_t i = 0; i < data_.size(); ++i) {
b[off_data + i] = data_[i];
}
// 文件尾 SHA-256(对文件尾之前全部内容计算)
const size_t content_len = off_data + data_.size();
uint8_t digest[kSha256Size];
sha256(&b[0], content_len, digest);
for (size_t i = 0; i < kSha256Size; ++i) {
b[content_len + i] = digest[i];
}
return true;
}