阶段 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:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user