physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m3 / tools / m3tool.c
| 1 | /* |
| 2 | * m3tool -- PHYSICS M3 host tool (no Python). Host: aarch64 little-endian. |
| 3 | * |
| 4 | * Built by m3/build.sh with the host gcc, linking the repo-root |
| 5 | * sha256_clean.c (the same audited source the image uses). Results must |
| 6 | * still be cross-checked against coreutils sha256sum so a bug shared by the |
| 7 | * image and this tool cannot hide. |
| 8 | * |
| 9 | * Subcommands: |
| 10 | * sha256 <file> canonical SHA-256 hex (as sha256sum) |
| 11 | * hexfield <file> <offset> <width> LE unsigned field, width 1/2/4/8 -> 0x... |
| 12 | * bytes <file> <offset> <len> raw bytes as contiguous lowercase hex |
| 13 | * |
| 14 | * Adding a subcommand: write `static int cmd_x(int argc, char **argv)` (argv[0] |
| 15 | * is the subcommand name; return 0 on success, nonzero on failure) and add a |
| 16 | * row to CMDS[]. Shared helpers: read_file(), parse_u64(), sha256_file_buf(), |
| 17 | * put_hex(). Every failure prints "m3tool: ..." to stderr and exits nonzero. |
| 18 | */ |
| 19 | #include <errno.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | /* From the repo-root sha256_clean.c. data must be 4-byte aligned. */ |
| 26 | void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], |
| 27 | uint32_t out_digest[8]); |
| 28 | |
| 29 | #define M3TOOL_MAX_FILE (1ull << 30) /* 1 GiB: keeps len*8 far from overflow */ |
| 30 | |
| 31 | #include <stdarg.h> |
| 32 | |
| 33 | static int fail(const char *fmt, ...) { |
| 34 | va_list ap; |
| 35 | va_start(ap, fmt); |
| 36 | fprintf(stderr, "m3tool: "); |
| 37 | vfprintf(stderr, fmt, ap); |
| 38 | fputc('\n', stderr); |
| 39 | va_end(ap); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | /* Read a whole file into a malloc'd (16-byte aligned) buffer. */ |
| 44 | static int read_file(const char *path, uint8_t **out, uint64_t *out_len) { |
| 45 | FILE *f = fopen(path, "rb"); |
| 46 | if (!f) return fail("cannot open %s", path); |
| 47 | uint64_t cap = 65536, len = 0; |
| 48 | uint8_t *buf = malloc(cap); |
| 49 | if (!buf) { fclose(f); return fail("out of memory reading %s", path); } |
| 50 | for (;;) { |
| 51 | if (len == cap) { |
| 52 | if (cap >= M3TOOL_MAX_FILE) { free(buf); fclose(f); return fail("file too large: %s", path); } |
| 53 | uint8_t *nb = realloc(buf, cap * 2); |
| 54 | if (!nb) { free(buf); fclose(f); return fail("out of memory reading %s", path); } |
| 55 | buf = nb; cap *= 2; |
| 56 | } |
| 57 | size_t n = fread(buf + len, 1, cap - len, f); |
| 58 | len += n; |
| 59 | if (n == 0) break; |
| 60 | } |
| 61 | int err = ferror(f); |
| 62 | fclose(f); |
| 63 | if (err) { free(buf); return fail("read error on %s", path); } |
| 64 | *out = buf; *out_len = len; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* Strict unsigned parse: decimal or 0x hex, whole string, no sign. */ |
| 69 | static int parse_u64(const char *s, uint64_t *v) { |
| 70 | if (!s || !*s || *s == '-' || *s == '+' || *s == ' ') return fail("bad number: %s", s ? s : "(null)"); |
| 71 | char *end; errno = 0; |
| 72 | unsigned long long x = strtoull(s, &end, 0); |
| 73 | if (errno || *end) return fail("bad number: %s", s); |
| 74 | *v = x; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static void put_hex(const uint8_t *p, uint64_t n) { |
| 79 | for (uint64_t i = 0; i < n; i++) printf("%02x", p[i]); |
| 80 | } |
| 81 | |
| 82 | /* Canonical big-endian digest bytes. */ |
| 83 | static void sha256_file_buf(const uint8_t *buf, uint64_t len, uint8_t out[32]) { |
| 84 | uint8_t scratch[128] __attribute__((aligned(16))); |
| 85 | uint32_t st[8]; |
| 86 | sha256_compute(buf, len, scratch, st); |
| 87 | for (int i = 0; i < 8; i++) { |
| 88 | out[4 * i + 0] = (uint8_t)(st[i] >> 24); |
| 89 | out[4 * i + 1] = (uint8_t)(st[i] >> 16); |
| 90 | out[4 * i + 2] = (uint8_t)(st[i] >> 8); |
| 91 | out[4 * i + 3] = (uint8_t)(st[i]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* Load file and bounds-check [off, off+n) without overflow. */ |
| 96 | static int load_range(const char *path, const char *off_s, uint64_t n, |
| 97 | uint8_t **buf, uint64_t *off) { |
| 98 | uint64_t len; |
| 99 | if (parse_u64(off_s, off)) return 1; |
| 100 | if (read_file(path, buf, &len)) return 1; |
| 101 | if (*off > len || n > len - *off) { free(*buf); return fail("range outside file %s", path); } |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int cmd_sha256(int argc, char **argv) { |
| 106 | if (argc != 2) return fail("usage: %s <file>", argv[0]); |
| 107 | uint8_t *buf; uint64_t len; uint8_t d[32]; |
| 108 | if (read_file(argv[1], &buf, &len)) return 1; |
| 109 | sha256_file_buf(buf, len, d); |
| 110 | free(buf); |
| 111 | put_hex(d, 32); putchar('\n'); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int cmd_hexfield(int argc, char **argv) { |
| 116 | if (argc != 4) return fail("usage: %s <file> <offset> <width 1|2|4|8>", argv[0]); |
| 117 | uint64_t w, off, v = 0; uint8_t *buf; |
| 118 | if (parse_u64(argv[3], &w)) return 1; |
| 119 | if (w != 1 && w != 2 && w != 4 && w != 8) return fail("width must be 1, 2, 4 or 8: %s", argv[3]); |
| 120 | if (load_range(argv[1], argv[2], w, &buf, &off)) return 1; |
| 121 | for (uint64_t i = 0; i < w; i++) v |= (uint64_t)buf[off + i] << (8 * i); |
| 122 | free(buf); |
| 123 | printf("0x%0*llx\n", (int)(2 * w), (unsigned long long)v); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int cmd_bytes(int argc, char **argv) { |
| 128 | if (argc != 4) return fail("usage: %s <file> <offset> <len>", argv[0]); |
| 129 | uint64_t n, off; uint8_t *buf; |
| 130 | if (parse_u64(argv[3], &n)) return 1; |
| 131 | if (load_range(argv[1], argv[2], n, &buf, &off)) return 1; |
| 132 | put_hex(buf + off, n); putchar('\n'); |
| 133 | free(buf); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int cmd_pin_inc(int argc, char **argv) { |
| 138 | if (argc != 3) return fail("usage: %s <binary> <out-inc-file>", argv[0]); |
| 139 | uint8_t *buf; uint64_t len; uint8_t d[32]; |
| 140 | if (read_file(argv[1], &buf, &len)) return 1; |
| 141 | sha256_file_buf(buf, len, d); |
| 142 | free(buf); |
| 143 | |
| 144 | FILE *f = fopen(argv[2], "w"); |
| 145 | if (!f) return fail("cannot open %s for writing", argv[2]); |
| 146 | fprintf(f, "/* Generated by m3tool pin-inc */\n"); |
| 147 | fprintf(f, " .word "); |
| 148 | for (int i = 0; i < 4; i++) { |
| 149 | uint32_t w = ((uint32_t)d[4*i] << 24) | ((uint32_t)d[4*i+1] << 16) | |
| 150 | ((uint32_t)d[4*i+2] << 8) | ((uint32_t)d[4*i+3]); |
| 151 | fprintf(f, "0x%08x%s", w, i < 3 ? ", " : "\n"); |
| 152 | } |
| 153 | fprintf(f, " .word "); |
| 154 | for (int i = 4; i < 8; i++) { |
| 155 | uint32_t w = ((uint32_t)d[4*i] << 24) | ((uint32_t)d[4*i+1] << 16) | |
| 156 | ((uint32_t)d[4*i+2] << 8) | ((uint32_t)d[4*i+3]); |
| 157 | fprintf(f, "0x%08x%s", w, i < 7 ? ", " : "\n"); |
| 158 | } |
| 159 | fclose(f); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int cmd_verify_receipt(int argc, char **argv) { |
| 164 | if (argc != 3) return fail("usage: %s <dump-file> <offset>", argv[0]); |
| 165 | uint64_t off; uint8_t *buf; |
| 166 | if (load_range(argv[1], argv[2], 192, &buf, &off)) return 1; |
| 167 | uint8_t *rcpt = buf + off; |
| 168 | |
| 169 | uint8_t computed[32]; |
| 170 | sha256_file_buf(rcpt, 128, computed); |
| 171 | |
| 172 | if (memcmp(computed, rcpt + 0x80, 32) != 0) { |
| 173 | free(buf); |
| 174 | return fail("receipt digest mismatch at offset %s", argv[2]); |
| 175 | } |
| 176 | uint32_t decision = *(uint32_t *)(rcpt + 4); |
| 177 | uint64_t effect = *(uint64_t *)(rcpt + 0x38); |
| 178 | printf("PASS receipt at %s decision=0x%08x effect=0x%016llx digest=", argv[2], decision, (unsigned long long)effect); |
| 179 | put_hex(computed, 32); |
| 180 | putchar('\n'); |
| 181 | free(buf); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int cmd_verify_chain(int argc, char **argv) { |
| 186 | if (argc != 2) return fail("usage: %s <receipt-ledger-dump>", argv[0]); |
| 187 | uint8_t *buf; uint64_t len; |
| 188 | if (read_file(argv[1], &buf, &len)) return 1; |
| 189 | if (len < 128) { free(buf); return fail("dump too small for ledger header: %s", argv[1]); } |
| 190 | |
| 191 | uint64_t magic = *(uint64_t *)(buf + 0); |
| 192 | if (magic != 0x524350544C444752ULL) { free(buf); return fail("bad ledger magic 0x%llx", (unsigned long long)magic); } |
| 193 | |
| 194 | uint32_t cap = *(uint32_t *)(buf + 8); |
| 195 | uint32_t count = *(uint32_t *)(buf + 12); |
| 196 | if (cap != 32 || count > 32) { free(buf); return fail("invalid cap=%u or count=%u", (unsigned)cap, (unsigned)count); } |
| 197 | |
| 198 | uint8_t prev_digest[32]; |
| 199 | memset(prev_digest, 0, 32); |
| 200 | |
| 201 | for (uint32_t i = 0; i < count; i++) { |
| 202 | uint64_t roff = 128 + i * 192; |
| 203 | if (roff + 192 > len) { free(buf); return fail("truncated receipt %u at offset %llu", i, (unsigned long long)roff); } |
| 204 | uint8_t *rcpt = buf + roff; |
| 205 | |
| 206 | /* Verify previous_receipt_digest */ |
| 207 | if (memcmp(rcpt + 0x60, prev_digest, 32) != 0) { |
| 208 | free(buf); |
| 209 | return fail("broken chain at receipt %u: previous digest mismatch", (unsigned)i); |
| 210 | } |
| 211 | |
| 212 | /* Verify receipt_digest */ |
| 213 | uint8_t comp[32]; |
| 214 | sha256_file_buf(rcpt, 128, comp); |
| 215 | if (memcmp(comp, rcpt + 0x80, 32) != 0) { |
| 216 | free(buf); |
| 217 | return fail("tampered receipt %u: computed digest does not match seal", (unsigned)i); |
| 218 | } |
| 219 | memcpy(prev_digest, comp, 32); |
| 220 | } |
| 221 | |
| 222 | if (count > 0) { |
| 223 | if (memcmp(buf + 0x20, prev_digest, 32) != 0) { |
| 224 | free(buf); |
| 225 | return fail("%s", "header.latest_receipt_digest does not match last receipt seal"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | printf("PASS receipt chain: %u receipts verified (chain intact)\n", count); |
| 230 | free(buf); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static const struct { |
| 235 | const char *name; |
| 236 | int (*fn)(int, char **); |
| 237 | const char *help; |
| 238 | } CMDS[] = { |
| 239 | { "sha256", cmd_sha256, "sha256 <file>" }, |
| 240 | { "hexfield", cmd_hexfield, "hexfield <file> <offset> <width 1|2|4|8>" }, |
| 241 | { "bytes", cmd_bytes, "bytes <file> <offset> <len>" }, |
| 242 | { "pin-inc", cmd_pin_inc, "pin-inc <binary> <out-inc-file>" }, |
| 243 | { "verify-receipt", cmd_verify_receipt, "verify-receipt <dump-file> <offset>" }, |
| 244 | { "verify-chain", cmd_verify_chain, "verify-chain <receipt-ledger-dump>" }, |
| 245 | }; |
| 246 | |
| 247 | int main(int argc, char **argv) { |
| 248 | if (argc >= 2) { |
| 249 | for (size_t i = 0; i < sizeof CMDS / sizeof CMDS[0]; i++) { |
| 250 | if (strcmp(argv[1], CMDS[i].name) == 0) { |
| 251 | int rc = CMDS[i].fn(argc - 1, argv + 1); |
| 252 | if (fflush(stdout) != 0 || ferror(stdout)) return fail("%s", "write error on stdout"); |
| 253 | return rc ? 1 : 0; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | fprintf(stderr, "usage: m3tool <subcommand> ...\n"); |
| 258 | for (size_t i = 0; i < sizeof CMDS / sizeof CMDS[0]; i++) fprintf(stderr, " m3tool %s\n", CMDS[i].help); |
| 259 | return 2; |
| 260 | } |