阶段 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
+35
View File
@@ -6,10 +6,12 @@
*/
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <string>
#include "compiler/MachineConfig.h"
#include "compiler/Stb.h"
#include "compiler/TypeInfo.h"
#ifndef REPO_ROOT
@@ -284,11 +286,44 @@ static bool test_type_meta() {
return true;
}
// ---- 5. SHA-256 已知向量 + 型号标识 ----
static bool test_sha256() {
// 标准测试向量
{
uint8_t out[compiler::kSha256Size];
compiler::sha256(nullptr, 0, out);
const uint8_t want[32] = {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
CHECK(std::memcmp(out, want, 32) == 0);
}
{
const uint8_t abc[] = {'a', 'b', 'c'};
uint8_t out[compiler::kSha256Size];
compiler::sha256(abc, 3, out);
const uint8_t want[32] = {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
CHECK(std::memcmp(out, want, 32) == 0);
}
// 型号标识:STATOR + 1 → "STATOR1" 补 '\0'
char mid[compiler::kModelIdSize];
compiler::fill_model_id("STATOR", 1, mid);
CHECK(std::strncmp(mid, "STATOR1", 7) == 0);
CHECK(mid[7] == '\0');
CHECK(mid[31] == '\0');
return true;
}
int main() {
if (!test_positive()) return 1;
if (!test_negative()) return 1;
if (!test_positive_min()) return 1;
if (!test_type_meta()) return 1;
if (!test_sha256()) return 1;
std::printf("machine_test: %d checks passed\n", g_checks);
return 0;
}