阶段 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:
@@ -19,12 +19,14 @@
|
||||
|
||||
namespace compiler {
|
||||
|
||||
// ---- .stb 格式常量(契约)----
|
||||
// ---- .stb 格式常量(契约;12.13 修订:头 104 = 原 72 + 型号标识[32],文件尾 SHA-256[32])----
|
||||
static const uint32_t kMagic = 0x43545353u; // "STSC" 小端
|
||||
static const uint32_t kVersion = 1;
|
||||
static const size_t kHeaderSize = 72;
|
||||
static const size_t kHeaderSize = 104;
|
||||
static const size_t kConstEntrySize = 12;
|
||||
static const size_t kFuncRowSize = 12;
|
||||
static const size_t kSha256Size = 32;
|
||||
static const size_t kModelIdSize = 32;
|
||||
static const uint64_t kFnvBasis = 0xcbf29ce484222325ull;
|
||||
static const uint64_t kFnvPrime = 0x100000001b3ull;
|
||||
|
||||
@@ -38,6 +40,12 @@ namespace compiler {
|
||||
uint64_t fnv1a64_update(uint64_t h, const uint8_t* data, size_t len);
|
||||
uint64_t fnv1a64(const uint8_t* data, size_t len);
|
||||
|
||||
// SHA-256(文件完整性校验,非密码学用途之外的完整性)
|
||||
void sha256(const uint8_t* data, size_t len, uint8_t out[kSha256Size]);
|
||||
|
||||
// 型号标识:name + version 拼 32 字节 ASCII(如 "STATOR1"),不足补 '\0',超长截断
|
||||
void fill_model_id(const std::string& name, uint32_t version, char out[kModelIdSize]);
|
||||
|
||||
// 只读视图(--disasm 用;校验魔数/版本/段边界)
|
||||
class StbView {
|
||||
public:
|
||||
@@ -71,6 +79,11 @@ namespace compiler {
|
||||
const uint8_t* data_bytes() const;
|
||||
size_t data_len() const;
|
||||
|
||||
// 12.13:型号标识与 SHA-256 校验
|
||||
std::string model_id() const; // 头 72..103(补 '\0' 后字符串)
|
||||
bool model_matches(const std::string& name, uint32_t version) const;
|
||||
bool sha_ok() const; // 文件尾 32 字节 SHA-256 校验
|
||||
|
||||
private:
|
||||
StbView() : buf_(0), len_(0) {}
|
||||
|
||||
|
||||
@@ -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](偏移 72;12.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;
|
||||
}
|
||||
|
||||
|
||||
+156
-3
@@ -44,8 +44,129 @@ namespace {
|
||||
return v;
|
||||
}
|
||||
|
||||
// ---- SHA-256(FIPS 180-4)----
|
||||
|
||||
const uint32_t kShaK[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
||||
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
||||
};
|
||||
|
||||
inline uint32_t rotr(uint32_t x, uint32_t n) { return (x >> n) | (x << (32 - n)); }
|
||||
|
||||
struct Sha256 {
|
||||
uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
||||
uint64_t total = 0;
|
||||
uint8_t block[64];
|
||||
size_t block_len = 0;
|
||||
|
||||
void update(const uint8_t* data, size_t len) {
|
||||
total += len;
|
||||
while (len > 0) {
|
||||
const size_t take = (block_len < 64) ? (64 - block_len) : 0;
|
||||
const size_t n = len < take ? len : take;
|
||||
if (n > 0) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
block[block_len + i] = data[i];
|
||||
}
|
||||
block_len += n;
|
||||
data += n;
|
||||
len -= n;
|
||||
if (block_len == 64) {
|
||||
process();
|
||||
block_len = 0;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void process() {
|
||||
uint32_t w[64];
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
w[i] = get_be32(block + i * 4);
|
||||
}
|
||||
for (int i = 16; i < 64; ++i) {
|
||||
const uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
const uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
uint32_t a = h[0], b = h[1], c = h[2], d = h[3];
|
||||
uint32_t e = h[4], f = h[5], g = h[6], hh = h[7];
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
const uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||
const uint32_t ch = (e & f) ^ (~e & g);
|
||||
const uint32_t t1 = hh + s1 + ch + kShaK[i] + w[i];
|
||||
const uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||
const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
const uint32_t t2 = s0 + maj;
|
||||
hh = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
h[0] += a; h[1] += b; h[2] += c; h[3] += d;
|
||||
h[4] += e; h[5] += f; h[6] += g; h[7] += hh;
|
||||
}
|
||||
|
||||
void final(uint8_t out[32]) {
|
||||
const uint64_t bitlen = total * 8;
|
||||
const uint8_t pad = 0x80;
|
||||
update(&pad, 1);
|
||||
const uint8_t zeros[64] = {0};
|
||||
// 补零到 block_len == 56(跨块时先补满当前块再补)
|
||||
while (block_len != 56) {
|
||||
const size_t n = (block_len < 56) ? (56 - block_len) : (64 - block_len);
|
||||
update(zeros, n);
|
||||
}
|
||||
// 64 位大端比特长度
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
const uint8_t b2[1] = {static_cast<uint8_t>((bitlen >> (56 - 8 * i)) & 0xFF)};
|
||||
update(b2, 1);
|
||||
}
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
out[i * 4 + 0] = static_cast<uint8_t>((h[i] >> 24) & 0xFF);
|
||||
out[i * 4 + 1] = static_cast<uint8_t>((h[i] >> 16) & 0xFF);
|
||||
out[i * 4 + 2] = static_cast<uint8_t>((h[i] >> 8) & 0xFF);
|
||||
out[i * 4 + 3] = static_cast<uint8_t>(h[i] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t get_be32(const uint8_t* p) {
|
||||
return (static_cast<uint32_t>(p[0]) << 24) | (static_cast<uint32_t>(p[1]) << 16) |
|
||||
(static_cast<uint32_t>(p[2]) << 8) | static_cast<uint32_t>(p[3]);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void sha256(const uint8_t* data, size_t len, uint8_t out[kSha256Size]) {
|
||||
Sha256 s;
|
||||
s.update(data, len);
|
||||
s.final(out);
|
||||
}
|
||||
|
||||
void fill_model_id(const std::string& name, uint32_t version, char out[kModelIdSize]) {
|
||||
const std::string id = name + std::to_string(version);
|
||||
for (size_t i = 0; i < kModelIdSize; ++i) {
|
||||
out[i] = i < id.size() ? id[i] : '\0';
|
||||
}
|
||||
}
|
||||
|
||||
StbView StbView::from(const uint8_t* buf, size_t len) {
|
||||
StbView v;
|
||||
v.buf_ = buf;
|
||||
@@ -77,11 +198,15 @@ StbView StbView::from(const uint8_t* buf, size_t len) {
|
||||
v.offset_fb_ = get_le32(buf + 64);
|
||||
v.offset_data_ = get_le32(buf + 68);
|
||||
|
||||
// 段校验
|
||||
// 段校验(12.13:文件尾 SHA-256[32] 在数据段之后)
|
||||
if (len < static_cast<size_t>(v.offset_data_) + kSha256Size) {
|
||||
v.err_ = "missing sha256 tail";
|
||||
return v;
|
||||
}
|
||||
const uint64_t offs[5] = {get_le32(buf + 52), get_le32(buf + 56), v.offset_code_,
|
||||
v.offset_fb_, v.offset_data_};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (offs[i] < kHeaderSize || offs[i] > len) {
|
||||
if (offs[i] < kHeaderSize || offs[i] > len - kSha256Size) {
|
||||
v.err_ = "segment offset out of range";
|
||||
return v;
|
||||
}
|
||||
@@ -154,7 +279,35 @@ const uint8_t* StbView::data_bytes() const {
|
||||
}
|
||||
|
||||
size_t StbView::data_len() const {
|
||||
return ok_ ? len_ - offset_data_ : 0;
|
||||
return ok_ ? (len_ - kSha256Size) - offset_data_ : 0;
|
||||
}
|
||||
|
||||
std::string StbView::model_id() const {
|
||||
if (!ok_) {
|
||||
return "";
|
||||
}
|
||||
std::string s(reinterpret_cast<const char*>(buf_ + 72), kModelIdSize);
|
||||
const size_t z = s.find('\0');
|
||||
if (z != std::string::npos) {
|
||||
s.resize(z);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
bool StbView::model_matches(const std::string& name, uint32_t version) const {
|
||||
char want[kModelIdSize];
|
||||
fill_model_id(name, version, want);
|
||||
return std::memcmp(buf_ + 72, want, kModelIdSize) == 0;
|
||||
}
|
||||
|
||||
bool StbView::sha_ok() const {
|
||||
if (!ok_) {
|
||||
return false;
|
||||
}
|
||||
const size_t content_len = len_ - kSha256Size;
|
||||
uint8_t digest[kSha256Size];
|
||||
sha256(buf_, content_len, digest);
|
||||
return std::memcmp(buf_ + content_len, digest, kSha256Size) == 0;
|
||||
}
|
||||
|
||||
bool read_stb_file(const char* path, std::vector<uint8_t>* out, std::string* err) {
|
||||
|
||||
+15
-2
@@ -47,8 +47,10 @@ namespace {
|
||||
}
|
||||
std::printf("image: %s (%zu bytes, %u functions, %u globals, entry fn %u)\n",
|
||||
path, bytes.size(), v.n_funcs(), v.n_globals(), v.entry_fn_id());
|
||||
std::printf(" dt_ms=%u cycle_limit=%u hash=0x%016llx\n", v.dt_ms(),
|
||||
v.cycle_limit(), static_cast<unsigned long long>(v.project_hash()));
|
||||
std::printf(" dt_ms=%u cycle_limit=%u hash=0x%016llx model=%s sha=%s\n",
|
||||
v.dt_ms(), v.cycle_limit(),
|
||||
static_cast<unsigned long long>(v.project_hash()),
|
||||
v.model_id().c_str(), v.sha_ok() ? "ok" : "BAD");
|
||||
|
||||
if (v.n_consts()) {
|
||||
std::printf("constants:\n");
|
||||
@@ -191,6 +193,17 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 写后自检:型号标识与 SHA-256
|
||||
const compiler::StbView self = compiler::StbView::from(image);
|
||||
if (!self.ok() || !self.sha_ok()) {
|
||||
std::fprintf(stderr, "error: self-check failed: %s\n", self.error().c_str());
|
||||
return 1;
|
||||
}
|
||||
if (!self.model_matches(cfg.model_name(), cfg.version())) {
|
||||
std::fprintf(stderr, "error: self-check model mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// sidecar:I/O 绑定 var → 槽号 → channel/bit(不进映像,执行器采样用)
|
||||
std::vector<compiler::IoBinding> bindings;
|
||||
for (const compiler::IoBinding& b : proj.io) {
|
||||
|
||||
Reference in New Issue
Block a user