interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tools / ck_artifact_tool.c

866 lines · 34078 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ck_artifact_tool.c -- host tool for the C kernel P2_ARTIFACT gate.
18 * C port of the commands of crates/aienos-artifact-tool that the gate uses
19 * (pack, sign, id, verify, negative-corpus, receipt from-hex|check|sign-test|
20 * verify), plus "bundle", which packs candidates into the fw_cfg file the C
21 * kernel reads (opt/aienos/artifacts).
22 *
23 * TEST ONLY: sign / negative-corpus / receipt sign-test use the RFC 8032
24 * TEST 1 (artifact) and TEST 2 (receipt) seeds. These are public test
25 * vectors, never owner key material, and the tool has no other keys. */
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include "aienos_sig.h"
33#include "ck_artifact.h"
34#include "sha256.h"
35
36static const uint8_t test1_seed[32] = {
37 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4,
38 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60,
39};
40static const uint8_t test2_seed[32] = {
41 0x4c, 0xcd, 0x08, 0x9b, 0x28, 0xff, 0x96, 0xda, 0x9d, 0xb6, 0xc3, 0x46, 0xec, 0x11, 0x4e, 0x0f,
42 0x5b, 0x8a, 0x31, 0x9f, 0x35, 0xab, 0xa6, 0x24, 0xda, 0x8c, 0xf6, 0xed, 0x4f, 0xb8, 0xa6, 0xfb,
43};
44/* RFC 8032 TEST 3 seed: a well-formed key no anchor set trusts (H15). */
45static const uint8_t untrusted_seed[32] = {
46 0xc5, 0xaa, 0x8d, 0xf4, 0x3f, 0x9f, 0x83, 0x7b, 0xed, 0xb7, 0x44, 0x2f, 0x31, 0xdc, 0xb7, 0xb1,
47 0x66, 0xd3, 0x85, 0x35, 0x07, 0x6f, 0x09, 0x4b, 0x85, 0xce, 0x3a, 0x2e, 0x0b, 0x44, 0x58, 0xf7,
48};
49
50static void die(const char *m)
51{
52 fprintf(stderr, "error: %s\n", m);
53 exit(1);
54}
55
56struct buf {
57 uint8_t *p;
58 size_t n;
59};
60
61static struct buf readf(const char *path)
62{
63 FILE *f = fopen(path, "rb");
64 if (!f) {
65 fprintf(stderr, "error: cannot open %s: %s\n", path, strerror(errno));
66 exit(1);
67 }
68 struct buf b = {0, 0};
69 size_t cap = 0;
70 for (;;) {
71 if (b.n == cap) {
72 cap = cap ? cap * 2 : 4096;
73 if (cap > ((size_t)64 << 20))
74 die("input too large");
75 b.p = realloc(b.p, cap);
76 if (!b.p)
77 die("out of memory");
78 }
79 size_t r = fread(b.p + b.n, 1, cap - b.n, f);
80 b.n += r;
81 if (r == 0)
82 break;
83 }
84 if (ferror(f))
85 die("read error");
86 fclose(f);
87 return b;
88}
89
90static void writef(const char *path, const uint8_t *p, size_t n)
91{
92 int f_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
93 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "wb");
94 if (!f || (n && fwrite(p, 1, n, f) != n) || fclose(f) != 0) {
95 fprintf(stderr, "error: cannot write %s\n", path);
96 exit(1);
97 }
98}
99
100static void hex(const uint8_t *b, size_t n, char *out)
101{
102 static const char d[] = "0123456789abcdef";
103 for (size_t i = 0; i < n; i++) {
104 out[2 * i] = d[b[i] >> 4];
105 out[2 * i + 1] = d[b[i] & 15];
106 }
107 out[2 * n] = 0;
108}
109
110static void phex(const char *label, const uint8_t *b)
111{
112 char h[65];
113 hex(b, 32, h);
114 printf("%s%s\n", label, h);
115}
116
117/* ---- minimal manifest JSON (the Rust Manifest, deny_unknown_fields) ---- */
118struct manifest {
119 uint32_t entry;
120 unsigned ncaps;
121 struct cka_cap caps[CKA_MAX_CAPS];
122 struct cka_env env;
123};
124
125struct js {
126 const char *s;
127 size_t i, n;
128};
129
130static void ws(struct js *j)
131{
132 while (j->i < j->n && (j->s[j->i] == ' ' || j->s[j->i] == '\n' || j->s[j->i] == '\r' ||
133 j->s[j->i] == '\t'))
134 j->i++;
135}
136
137static void expect(struct js *j, char c)
138{
139 ws(j);
140 if (j->i >= j->n || j->s[j->i] != c)
141 die("manifest: malformed JSON");
142 j->i++;
143}
144
145static int peek(struct js *j, char c)
146{
147 ws(j);
148 return j->i < j->n && j->s[j->i] == c;
149}
150
151static void key(struct js *j, char *out, size_t cap)
152{
153 expect(j, '"');
154 size_t k = 0;
155 while (j->i < j->n && j->s[j->i] != '"') {
156 if (j->s[j->i] == '\\' || k + 1 >= cap)
157 die("manifest: unsupported key");
158 out[k++] = j->s[j->i++];
159 }
160 out[k] = 0;
161 expect(j, '"');
162 expect(j, ':');
163}
164
165static uint64_t num(struct js *j, uint64_t max)
166{
167 ws(j);
168 uint64_t v = 0;
169 size_t start = j->i;
170 while (j->i < j->n && j->s[j->i] >= '0' && j->s[j->i] <= '9') {
171 unsigned d = (unsigned)(j->s[j->i++] - '0');
172 if (v > (UINT64_MAX - d) / 10)
173 die("manifest: number overflow");
174 v = v * 10 + d;
175 }
176 if (j->i == start || (j->i - start > 1 && j->s[start] == '0'))
177 die("manifest: expected an unsigned integer");
178 if (v > max)
179 die("manifest: number out of range for its field");
180 return v;
181}
182
183#define U16 0xffffu
184#define U32 0xffffffffu
185
186static void parse_cap(struct js *j, struct cka_cap *c)
187{
188 unsigned seen = 0;
189 expect(j, '{');
190 while (!peek(j, '}')) {
191 char k[40];
192 key(j, k, sizeof k);
193 unsigned bit;
194 if (!strcmp(k, "resource_kind")) c->kind = (uint16_t)num(j, U16), bit = 1;
195 else if (!strcmp(k, "resource_id")) c->id = (uint32_t)num(j, U32), bit = 2;
196 else if (!strcmp(k, "rights")) c->rights = (uint32_t)num(j, U32), bit = 4;
197 else if (!strcmp(k, "bounds_kind")) c->bounds = (uint32_t)num(j, U32), bit = 8;
198 else if (!strcmp(k, "max_operations")) c->max_ops = (uint32_t)num(j, U32), bit = 16;
199 else if (!strcmp(k, "max_bytes")) c->max_bytes = num(j, UINT64_MAX), bit = 32;
200 else if (!strcmp(k, "byte_offset")) c->off = num(j, UINT64_MAX), bit = 64;
201 else if (!strcmp(k, "byte_length")) c->len = num(j, UINT64_MAX), bit = 128;
202 else die("manifest: unknown capability field");
203 if (seen & bit)
204 die("manifest: duplicate field");
205 seen |= bit;
206 if (!peek(j, '}'))
207 expect(j, ',');
208 }
209 expect(j, '}');
210 if (seen != 255)
211 die("manifest: missing capability field");
212}
213
214static void parse_env(struct js *j, struct cka_env *e)
215{
216 unsigned seen = 0;
217 expect(j, '{');
218 while (!peek(j, '}')) {
219 char k[40];
220 key(j, k, sizeof k);
221 unsigned bit;
222 if (!strcmp(k, "code_pages")) e->code_pages = (uint32_t)num(j, U32), bit = 1;
223 else if (!strcmp(k, "data_pages")) e->data_pages = (uint32_t)num(j, U32), bit = 2;
224 else if (!strcmp(k, "stack_pages")) e->stack_pages = (uint32_t)num(j, U32), bit = 4;
225 else if (!strcmp(k, "max_capabilities")) e->max_caps = (uint16_t)num(j, U16), bit = 8;
226 else if (!strcmp(k, "ipc_messages")) e->ipc_msgs = (uint32_t)num(j, U32), bit = 16;
227 else if (!strcmp(k, "ipc_bytes")) e->ipc_bytes = (uint32_t)num(j, U32), bit = 32;
228 else if (!strcmp(k, "cpu_ticks")) e->cpu = num(j, UINT64_MAX), bit = 64;
229 else if (!strcmp(k, "elapsed_ticks")) e->elapsed = num(j, UINT64_MAX), bit = 128;
230 else if (!strcmp(k, "syscall_count")) e->syscalls = (uint32_t)num(j, U32), bit = 256;
231 else die("manifest: unknown resources field");
232 if (seen & bit)
233 die("manifest: duplicate field");
234 seen |= bit;
235 if (!peek(j, '}'))
236 expect(j, ',');
237 }
238 expect(j, '}');
239 if (seen != 511)
240 die("manifest: missing resources field");
241}
242
243static void parse_manifest(const struct buf *b, struct manifest *m)
244{
245 struct js j = {(const char *)b->p, 0, b->n};
246 unsigned seen = 0;
247 memset(m, 0, sizeof *m);
248 expect(&j, '{');
249 while (!peek(&j, '}')) {
250 char k[40];
251 key(&j, k, sizeof k);
252 unsigned bit;
253 if (!strcmp(k, "entry_offset")) {
254 m->entry = (uint32_t)num(&j, U32);
255 bit = 1;
256 } else if (!strcmp(k, "capabilities")) {
257 expect(&j, '[');
258 while (!peek(&j, ']')) {
259 if (m->ncaps >= CKA_MAX_CAPS)
260 die("code, data, and capability count are outside v0 bounds");
261 parse_cap(&j, &m->caps[m->ncaps++]);
262 if (!peek(&j, ']'))
263 expect(&j, ',');
264 }
265 expect(&j, ']');
266 bit = 2;
267 } else if (!strcmp(k, "resources")) {
268 parse_env(&j, &m->env);
269 bit = 4;
270 } else {
271 die("manifest: unknown field");
272 }
273 if (seen & bit)
274 die("manifest: duplicate field");
275 seen |= bit;
276 if (!peek(&j, '}'))
277 expect(&j, ',');
278 }
279 expect(&j, '}');
280 ws(&j);
281 if (j.i != j.n || seen != 7)
282 die("manifest: missing field or trailing data");
283}
284
285/* ---- pack (main.rs pack_bytes, same byte layout) ---- */
286static uint16_t pack_target = CKA_TARGET_AARCH64_LE, pack_abi = CKA_ABI_V1;
287static struct buf pack(const struct manifest *m, const uint8_t *code, size_t clen,
288 const uint8_t *data, size_t dlen)
289{
290 if (clen == 0 || dlen == 0 || m->ncaps > 16)
291 die("code, data, and capability count are outside v0 bounds");
292 size_t res = CKA_CAPS_OFFSET + (size_t)m->ncaps * CKA_CAP_SIZE;
293 size_t pay = (res + CKA_ENV_SIZE + 15) & ~(size_t)15;
294 size_t plen = clen + dlen;
295 size_t sig = pay + plen;
296 size_t total = sig + CKA_SIG_SIZE;
297 if (total > CKA_MAX_ARTIFACT || plen > CKA_MAX_PAYLOAD)
298 die("artifact exceeds v0 maximum size");
299 if ((uint64_t)m->env.data_pages * 4096u > 0xffffffffu)
300 die("data page overflow");
301 struct buf b = {calloc(1, total), total};
302 if (!b.p)
303 die("out of memory");
304 uint8_t *p = b.p;
305 memcpy(p, "AIENART\0", 8);
306 cka_wr16(p + 8, 0);
307 cka_wr16(p + 10, CKA_HEADER_SIZE);
308 cka_wr16(p + 12, pack_target);
309 cka_wr16(p + 14, pack_abi);
310 cka_wr32(p + 20, (uint32_t)total);
311 cka_wr32(p + 24, 128);
312 cka_wr16(p + 28, 2);
313 cka_wr16(p + 30, CKA_SECTION_SIZE);
314 cka_wr32(p + 36, m->entry);
315 cka_wr32(p + 40, CKA_CAPS_OFFSET);
316 cka_wr16(p + 44, (uint16_t)m->ncaps);
317 cka_wr16(p + 46, CKA_CAP_SIZE);
318 cka_wr32(p + 48, (uint32_t)res);
319 cka_wr16(p + 52, CKA_ENV_SIZE);
320 cka_wr32(p + 56, (uint32_t)pay);
321 cka_wr32(p + 60, (uint32_t)plen);
322 cka_wr32(p + 64, (uint32_t)sig);
323 cka_wr16(p + 68, CKA_SIG_SIZE);
324 cka_wr16(p + 70, 1);
325 /* sections: kind, perms, rel, flen, mlen, align 4096 */
326 uint8_t *s = p + 128;
327 cka_wr16(s, 1), cka_wr16(s + 2, 5), cka_wr32(s + 8, 0), cka_wr32(s + 12, (uint32_t)clen);
328 cka_wr32(s + 16, (uint32_t)clen), cka_wr32(s + 20, 4096);
329 s += 32;
330 cka_wr16(s, 2), cka_wr16(s + 2, 3), cka_wr32(s + 8, (uint32_t)clen);
331 cka_wr32(s + 12, (uint32_t)dlen), cka_wr32(s + 16, m->env.data_pages * 4096u);
332 cka_wr32(s + 20, 4096);
333 for (unsigned i = 0; i < m->ncaps; i++)
334 cka_cap_encode(&m->caps[i], p + CKA_CAPS_OFFSET + i * CKA_CAP_SIZE);
335 cka_env_encode(&m->env, p + res);
336 memcpy(p + pay, code, clen);
337 memcpy(p + pay + clen, data, dlen);
338 cka_wr16(p + sig, 1);
339 struct cka_parsed pa;
340 int e = cka_parse_profile(p, total, &pa, pack_target, pack_abi);
341 for (unsigned i = 0; !e && i < pa.ncaps; i++)
342 e = cka_cap_validate(&pa.caps[i]);
343 if (!e)
344 e = cka_env_validate_profile(&pa.env, pa.ncaps, pack_target, pack_abi);
345 if (e) {
346 fprintf(stderr, "error: packed artifact refused: %s\n", cka_error_name((unsigned)e));
347 exit(1);
348 }
349 return b;
350}
351
352static void sign_with(struct buf *b, const uint8_t seed[32])
353{
354 struct cka_parsed p;
355 struct cka_ident id;
356 int e = cka_identify_profile(b->p, b->n, &p, &id, pack_target, pack_abi);
357 if (e) {
358 fprintf(stderr, "error: does not parse: %s\n", cka_error_name((unsigned)e));
359 exit(1);
360 }
361 uint8_t pk[32], msg[64];
362 size_t mlen;
363 if (aienos_ed25519_public_key(pk, seed) != AIENOS_SIG_OK)
364 die("public key");
365 uint8_t *blk = b->p + p.sig_off;
366 sha256_hash(pk, 32, blk + 4);
367 cka_signature_message(id.id, msg, &mlen);
368 if (aienos_ed25519_sign(blk + 36, msg, mlen, seed) != AIENOS_SIG_OK)
369 die("sign");
370}
371
372static int verify_test(const struct buf *b, struct cka_parsed *p, struct cka_ident *id,
373 uint8_t fp[32])
374{
375 const uint8_t (*anchors)[32] = &cka_test1_pk;
376 return cka_verify(b->p, b->n, anchors, 1, p, id, fp);
377}
378
379/* ---- negative corpus (corpus.rs, same cases, names and expectations) ---- */
380#define MOV_X0_0 0xd2800000u
381#define MOV_X8_2 0xd2800048u
382#define MOV_X8_6 0xd28000c8u
383#define MOV_X8_99 0xd2800c68u
384#define SVC_0 0xd4000001u
385#define B_SELF 0x14000000u
386#define BR_X2 0xd61f0040u
387#define SUB_X9_SP_16 0xd10043e9u
388#define BR_X9 0xd61f0120u
389
390static size_t words(uint8_t *out, const uint32_t *w, size_t n)
391{
392 for (size_t i = 0; i < n; i++)
393 cka_wr32(out + 4 * i, w[i]);
394 return 4 * n;
395}
396
397static struct cka_cap seed_read(uint32_t rights)
398{
399 struct cka_cap c = {3, 1, rights, 1, 4, 32, 0, 32};
400 return c;
401}
402
403static struct cka_env res(uint32_t code, uint32_t data, uint32_t stack, uint16_t caps,
404 uint32_t sys)
405{
406 struct cka_env e = {code, data, stack, caps, 0, 0, 100000000, 100000000, sys};
407 return e;
408}
409
410static struct buf signed_art(unsigned ncaps, struct cka_cap cap, struct cka_env env,
411 const uint32_t *w, size_t nw, const char *data8)
412{
413 struct manifest m;
414 memset(&m, 0, sizeof m);
415 m.ncaps = ncaps;
416 m.caps[0] = cap;
417 m.env = env;
418 uint8_t code[64];
419 size_t cl = words(code, w, nw);
420 struct buf b = pack(&m, code, cl, (const uint8_t *)data8, 8);
421 sign_with(&b, test1_seed);
422 return b;
423}
424
425static const uint32_t exit0_w[] = {MOV_X0_0, MOV_X8_2, SVC_0, B_SELF};
426
427static struct buf clone(const struct buf *b, size_t extra)
428{
429 struct buf c = {calloc(1, b->n + extra + 1), b->n};
430 if (!c.p)
431 die("out of memory");
432 memcpy(c.p, b->p, b->n);
433 return c;
434}
435
436static void corpus(const char *dir)
437{
438 struct buf base = signed_art(1, seed_read(1), res(1, 1, 1, 1, 8), exit0_w, 4, "BASELINE");
439 size_t payload = cka_rd32(base.p + 56);
440 size_t resource = CKA_CAPS_OFFSET + CKA_CAP_SIZE;
441 size_t sig = base.n - 100;
442 char path[4096], exp[8192];
443 size_t el = 0;
444 exp[0] = 0;
445#define EMIT(NAME, BUF, EXPECT, WHAT) \
446 do { \
447 snprintf(path, sizeof path, "%s/%s", dir, NAME); \
448 writef(path, (BUF).p, (BUF).n); \
449 el += (size_t)snprintf(exp + el, sizeof exp - el, "%s %s # %s\n", NAME, EXPECT, WHAT); \
450 free((BUF).p); \
451 } while (0)
452#define MUT(NAME, STMT, EXPECT, WHAT) \
453 do { \
454 struct buf b = clone(&base, 1); \
455 STMT; \
456 EMIT(NAME, b, EXPECT, WHAT); \
457 } while (0)
458#define V(R, C) "rejected verified " R " " C
459 MUT("H01-BAD-MAGIC.AIEN", b.p[0] = 'X', V("BadMagic", "0x1"), "magic is not AIENART\\0");
460 MUT("H02-VERSION.AIEN", cka_wr16(b.p + 8, 1), V("UnsupportedVersion", "0x2"),
461 "format_version 1 (unsupported)");
462 MUT("H03-TARGET.AIEN", cka_wr16(b.p + 12, 2), V("WrongTarget", "0x3"),
463 "target_arch 2 (not AArch64 LE)");
464 MUT("H04-ABI.AIEN", cka_wr16(b.p + 14, 2), V("WrongAbi", "0x4"), "abi_version 2 (not ABI v1)");
465 MUT("H05-TRUNCATED.AIEN", b.n -= 10, V("WrongLength", "0x7"), "last 10 bytes cut off");
466 MUT("H06-TRAILING.AIEN", b.p[b.n++] = 0, V("WrongLength", "0x7"),
467 "one byte appended after the signature block");
468 MUT("H07-TOTAL-LENGTH.AIEN", cka_wr32(b.p + 20, cka_rd32(b.p + 20) + 1),
469 V("WrongLength", "0x7"), "total_length field one larger than the file");
470 MUT("H08-OFFSET-OVERFLOW.AIEN", cka_wr32(b.p + 60, 0xffffffffu), V("ResourceLimit", "0xe"),
471 "payload_length 0xffffffff (offset + length overflows)");
472 MUT("H09-SECTION-OVERLAP.AIEN", cka_wr32(b.p + 160 + 8, 4), V("BadSection", "0xb"),
473 "data section starts inside the code section");
474 MUT("H10-ENTRY-OUTSIDE.AIEN", cka_wr32(b.p + 36, 0x1000), V("BadEntryPoint", "0xc"),
475 "entry_offset beyond the code bytes");
476 MUT("H11-PAYLOAD-MUTATED.AIEN", b.p[payload] ^= 1, V("BadSignature", "0x12"),
477 "one code byte changed after signing");
478 MUT("H12-CAPS-MUTATED.AIEN", cka_wr32(b.p + CKA_CAPS_OFFSET + 8, 3), V("BadSignature", "0x12"),
479 "requested rights READ -> READ|WRITE after signing");
480 MUT("H13-ENVELOPE-MUTATED.AIEN", cka_wr32(b.p + resource + 8, 2), V("BadSignature", "0x12"),
481 "stack_pages 1 -> 2 after signing");
482 MUT("H14-SIGNATURE-MUTATED.AIEN", b.p[sig + 40] ^= 1, V("BadSignature", "0x12"),
483 "one signature byte changed");
484 MUT("H15-UNKNOWN-SIGNER.AIEN", sign_with(&b, untrusted_seed), V("UntrustedSigner", "0x11"),
485 "valid Ed25519 signature by a key no anchor trusts");
486 MUT("H16-SIG-FORMAT.AIEN", cka_wr16(b.p + sig, 2), V("BadSignatureFormat", "0xf"),
487 "signature block algorithm 2");
488 MUT("H17-RESERVED-HEADER.AIEN", b.p[100] = 1, V("ReservedNonZero", "0x8"),
489 "reserved header byte 100 nonzero");
490 MUT("H18-RIGHTS-UNKNOWN.AIEN", cka_wr32(b.p + CKA_CAPS_OFFSET + 8, 1u | 1u << 6),
491 V("MalformedCapability", "0xd"), "requested rights bit 6 (undefined)");
492 MUT("H19-CAP-COUNT.AIEN", cka_wr16(b.p + 44, 17), V("ResourceLimit", "0xe"),
493 "capability_count 17 (> 16)");
494 MUT("H20-ENVELOPE-LIMIT.AIEN", cka_wr32(b.p + resource, 65), V("ResourceLimit", "0xe"),
495 "code_pages 65 (> 64); refused while parsing, before signature checks");
496 {
497 struct buf b = signed_art(0, seed_read(0), res(64, 64, 16, 0, 4), exit0_w, 4, "LARGEST!");
498 EMIT("H21-LARGEST-ENVELOPE.AIEN", b, "admitted exited:0x0 0",
499 "largest valid envelope: 64 code, 64 data, 16 stack pages");
500 }
501 MUT("H22-WX-SECTION.AIEN", cka_wr16(b.p + 128 + 2, 7), V("BadSection", "0xb"),
502 "code section permissions R|W|X");
503 {
504 static const uint32_t w[] = {BR_X2};
505 struct buf b = signed_art(0, seed_read(0), res(1, 1, 1, 0, 4), w, 1, "DATAPAGE");
506 EMIT("H23-EXEC-DATA.AIEN", b, "admitted fault:exec-nx 0",
507 "branches into its own data page (mapped RW+NX)");
508 }
509 {
510 static const uint32_t w[] = {SUB_X9_SP_16, BR_X9};
511 struct buf b = signed_art(0, seed_read(0), res(1, 1, 1, 0, 4), w, 2, "STACKPAG");
512 EMIT("H24-EXEC-STACK.AIEN", b, "admitted fault:exec-nx 0",
513 "branches onto its own stack (mapped RW+NX)");
514 }
515 {
516 static const uint32_t w[] = {MOV_X8_99, SVC_0, B_SELF};
517 struct buf b = signed_art(0, seed_read(0), res(1, 1, 1, 0, 4), w, 3, "BADSYSCL");
518 EMIT("H25-BAD-SYSCALL.AIEN", b, "admitted bad-syscall:99 0", "invokes undefined syscall 99");
519 }
520 {
521 static const uint32_t w[] = {MOV_X0_0, MOV_X8_6, SVC_0, SVC_0, SVC_0, MOV_X8_2, SVC_0, B_SELF};
522 struct buf b = signed_art(0, seed_read(0), res(1, 1, 1, 0, 2), w, 8, "OVERRUN!");
523 EMIT("H26-SYSCALL-BUDGET.AIEN", b, "admitted resource-overrun 0",
524 "third syscall exceeds syscall_count 2");
525 }
526 {
527 struct cka_env r = res(1, 1, 1, 1, 8);
528 r.ipc_msgs = 1;
529 r.ipc_bytes = 64;
530 struct buf b = signed_art(1, seed_read(1), r, exit0_w, 4, "IPCWANTS");
531 EMIT("H27-POLICY-IPC.AIEN", b, "rejected authorized ResourceLimit 0xe",
532 "requests 1 IPC message; local policy allows none");
533 }
534 {
535 struct buf b = signed_art(1, seed_read(2), res(1, 1, 1, 1, 8), exit0_w, 4, "WRITEREQ");
536 EMIT("H28-WRITE-ONLY-REQUEST.AIEN", b, "admitted exited:0x0 0",
537 "requests only WRITE on the SEED object; policy grants READ at most");
538 }
539 {
540 struct buf b = signed_art(1, seed_read(3), res(1, 1, 1, 1, 8), exit0_w, 4, "RWREQUST");
541 EMIT("H29-READ-WRITE-REQUEST.AIEN", b, "admitted exited:0x0 1",
542 "requests READ|WRITE; granted READ only");
543 }
544 snprintf(path, sizeof path, "%s/expected.txt", dir);
545 writef(path, (const uint8_t *)exp, el);
546 free(base.p);
547 printf("NEGATIVE_CORPUS: TEST ONLY SEED-0B qualification inputs written\n");
548}
549
550/* ---- fw_cfg bundle: "AIENBND\0", u32 count, {u16 nlen, name, u32 len, bytes} ---- */
551static void bundle(int argc, char **argv)
552{
553 const char *out = argv[0];
554 size_t cap = 12, n = 12;
555 uint8_t *b = malloc(cap);
556 if (!b)
557 die("out of memory");
558 memcpy(b, "AIENBND\0", 8);
559 unsigned count = 0;
560 for (int i = 1; i < argc; i++) {
561 const char *name = strrchr(argv[i], '/');
562 name = name ? name + 1 : argv[i];
563 size_t nl = strlen(name);
564 if (nl == 0 || nl > 32)
565 die("bundle: candidate names are 1..32 bytes");
566 struct buf f = readf(argv[i]);
567 size_t need = n + 2 + nl + 4 + f.n;
568 if (need > cap) {
569 cap = need * 2;
570 b = realloc(b, cap);
571 if (!b)
572 die("out of memory");
573 }
574 cka_wr16(b + n, (uint16_t)nl);
575 memcpy(b + n + 2, name, nl);
576 cka_wr32(b + n + 2 + nl, (uint32_t)f.n);
577 memcpy(b + n + 6 + nl, f.p, f.n);
578 n = need;
579 count++;
580 free(f.p);
581 }
582 cka_wr32(b + 8, count);
583 writef(out, b, n);
584 free(b);
585 printf("BUNDLE: %u candidates\n", count);
586}
587
588/* ---- receipts (receipt_cmd.rs) ---- */
589static void read_receipt(const char *path, uint8_t r[CKA_RECEIPT_SIZE])
590{
591 struct buf b = readf(path);
592 if (b.n != CKA_RECEIPT_SIZE) {
593 fprintf(stderr, "error: receipt must be exactly 512 bytes, got %zu\n", b.n);
594 exit(1);
595 }
596 memcpy(r, b.p, CKA_RECEIPT_SIZE);
597 free(b.p);
598}
599
600static void decoded(const uint8_t *b, struct cka_receipt *r)
601{
602 int e = cka_receipt_decode(b, r);
603 if (e) {
604 fprintf(stderr, "error: receipt rejected: %s\n", cka_error_name((unsigned)e));
605 exit(1);
606 }
607}
608
609static const char *tier_name(uint16_t t)
610{
611 return t == CKR_TIER_SEED0B_PI5 ? "SEED-0B-PI5" : "SEED-0B-QEMU";
612}
613
614static const char *decision(const struct cka_receipt *r)
615{
616 return r->decision == CKR_ADMITTED ? "Admitted" : "Rejected";
617}
618
619static int receipt(int argc, char **argv)
620{
621 static const uint8_t zero[32];
622 uint8_t rec[CKA_RECEIPT_SIZE];
623 struct cka_receipt r;
624 char h[65];
625 if (argc == 3 && !strcmp(argv[0], "from-hex")) {
626 const char *t = argv[1];
627 if (strlen(t) != 2 * CKA_RECEIPT_SIZE)
628 die("expected 1024 lowercase hex characters");
629 for (unsigned i = 0; i < CKA_RECEIPT_SIZE; i++) {
630 unsigned v = 0;
631 for (int k = 0; k < 2; k++) {
632 char c = t[2 * i + k];
633 if (c >= '0' && c <= '9') v = v * 16 + (unsigned)(c - '0');
634 else if (c >= 'a' && c <= 'f') v = v * 16 + (unsigned)(c - 'a' + 10);
635 else die("expected 1024 lowercase hex characters");
636 }
637 rec[i] = (uint8_t)v;
638 }
639 writef(argv[2], rec, CKA_RECEIPT_SIZE);
640 printf("FROM_HEX: PASS\n");
641 return 0;
642 }
643 if (argc == 3 && !strcmp(argv[0], "check")) {
644 read_receipt(argv[1], rec);
645 if (cka_receipt_is_signed(rec))
646 die("kernel-emitted receipts must be unsigned");
647 decoded(rec, &r);
648 int admitted = r.decision == CKR_ADMITTED;
649 struct buf a = readf(argv[2]);
650 struct cka_parsed p;
651 struct cka_ident id;
652 const uint8_t *fields[5] = {r.id, r.payload, r.signer, r.requested, r.resources};
653 static const char *names[5] = {"artifact_id", "payload_digest",
654 "artifact_signer_fingerprint",
655 "requested_capability_digest", "resource_envelope_digest"};
656 if (cka_identify(a.p, a.n, &p, &id) != 0) {
657 if (admitted)
658 die("admitted receipt for an artifact the host cannot parse");
659 for (int i = 0; i < 5; i++)
660 if (memcmp(fields[i], zero, 32)) {
661 fprintf(stderr, "error: %s must be zero for an unparseable artifact\n",
662 names[i]);
663 return 1;
664 }
665 } else {
666 const uint8_t *want[5] = {id.id, id.payload, p.fp, id.requested, id.resources};
667 for (int i = 0; i < 5; i++)
668 if (memcmp(fields[i], want[i], 32) &&
669 !(!admitted && !memcmp(fields[i], zero, 32))) {
670 fprintf(stderr, "error: %s does not bind the supplied artifact\n", names[i]);
671 return 1;
672 }
673 if (admitted && !memcmp(r.granted, zero, 32))
674 die("admitted receipt without a granted-capability digest");
675 }
676 free(a.p);
677 uint8_t d[32];
678 cka_receipt_digest(rec, d);
679 hex(d, 32, h);
680 printf("RECEIPT_CHECK: PASS digest=%s decision=%s tier=%s stage=%u reason=0x%x "
681 "status=%s exit=%d syscalls=%u reads_ok=%u denials=%u flags=0x%02x\n",
682 h, decision(&r), tier_name(r.tier), r.stage, r.reason, cka_status_name(r.status), r.exit, r.syscalls,
683 r.reads, r.denials, r.result_flags);
684 return 0;
685 }
686 if (argc == 3 && !strcmp(argv[0], "sign-test")) {
687 read_receipt(argv[1], rec);
688 decoded(rec, &r);
689 if (cka_receipt_sign(rec, test2_seed) != 0)
690 die("cannot sign receipt");
691 writef(argv[2], rec, CKA_RECEIPT_SIZE);
692 uint8_t fp[32];
693 sha256_hash(cka_test2_pk, 32, fp);
694 printf("RECEIPT_SIGN: TEST ONLY SEED-0B QUALIFICATION RECEIPT KEY\n");
695 phex("RECEIPT_SIGNER_FINGERPRINT: ", fp);
696 return 0;
697 }
698 if (argc == 2 && !strcmp(argv[0], "verify")) {
699 read_receipt(argv[1], rec);
700 const uint8_t (*anchors)[32] = &cka_test2_pk;
701 int e = cka_receipt_verify(rec, anchors, 1);
702 if (e) {
703 fprintf(stderr, "error: receipt verification failed: %s\n", cka_error_name((unsigned)e));
704 return 1;
705 }
706 decoded(rec, &r);
707 uint8_t d[32];
708 cka_receipt_digest(rec, d);
709 hex(d, 32, h);
710 printf("RECEIPT_VERIFY: PASS digest=%s decision=%s tier=%s "
711 "trust=SEED-0B-TEST-ONLY\n", h, decision(&r), tier_name(r.tier));
712 return 0;
713 }
714 die("usage: receipt from-hex HEX OUT | check REC ARTIFACT | sign-test IN OUT | verify REC");
715 return 1;
716}
717
718static void cbytes(FILE *f, const uint8_t *p, size_t n)
719{
720 fputs("{", f);
721 for (size_t i = 0; i < n; i++) fprintf(f, "%s%u", i ? "," : "", p[i]);
722 fputs("}", f);
723}
724
725int main(int argc, char **argv)
726{
727 if (argc == 7 && !strcmp(argv[1], "model-bundle")) {
728 struct buf key = readf(argv[2]), raw = readf(argv[3]);
729 struct buf weights = readf(argv[4]), tokenizer = readf(argv[5]);
730 if (key.n != 32 || !raw.n || raw.n > 131072 || weights.n != 60816028)
731 die("model bundle requires owner seed, bounded code, pinned 15M weights");
732 uint8_t bindings[64], digest[32], pk[32];
733 sha256_hash(weights.p, weights.n, bindings);
734 sha256_hash(tokenizer.p, tokenizer.n, bindings + 32);
735 if (aienos_ed25519_public_key(pk, key.p)) die("public key");
736 pack_target = CKA_TARGET_X86_64_LE; pack_abi = CKA_ABI_OMEGA_X86_MODEL_V1;
737 struct manifest m = {0};
738 m.env = (struct cka_env){.code_pages=32, .data_pages=1024, .stack_pages=16,
739 .cpu=120000000, .elapsed=180000000, .syscalls=1};
740 struct buf art = pack(&m, raw.p, raw.n, bindings, sizeof bindings);
741 sign_with(&art, key.p); sha256_hash(art.p, art.n, digest);
742 int f_fd = open(argv[6], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
743 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "w");
744 if (!f) die("model bundle output");
745 fputs("/* Generated owner-signed native Omega model. Public bytes only. */\n", f);
746 fputs("static const uint8_t ck_model_key[32] = ", f); cbytes(f, pk, 32); fputs(";\n", f);
747 fputs("static const uint8_t ck_model_art_bytes[] = ", f);
748 cbytes(f, art.p, art.n); fputs(";\n", f);
749 fputs("static const struct ck_local_artifact ck_model_artifact = {ck_model_art_bytes, sizeof ck_model_art_bytes, ", f);
750 cbytes(f, digest, 32); fputs("};\n", f);
751 volatile uint8_t *wipe = key.p;
752 for (unsigned i = 0; i < 32; i++) wipe[i] = 0;
753 free(key.p); free(raw.p); free(weights.p); free(tokenizer.p); free(art.p);
754 if (fclose(f)) die("model bundle close");
755 puts("MODEL_BUNDLE: PASS owner-signed code and weight/tokenizer bindings");
756 return 0;
757 }
758
759 /* Explicit owner-signed build bundle, no runtime upload mechanism. */
760 if (argc == 7 && !strcmp(argv[1], "local-bundle")) {
761 struct buf key = readf(argv[2]);
762 if (key.n != 32) die("local-bundle needs a 32-byte owner seed");
763 uint8_t pk[32], hashes[3][32];
764 if (aienos_ed25519_public_key(pk, key.p)) die("public key");
765 int f_fd = open(argv[6], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
766 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "w");
767 if (!f) die("bundle output");
768 fputs("/* Generated by ck_artifact_tool local-bundle; public artifacts only. */\n", f);
769 fputs("struct ck_local_artifact { const uint8_t *bytes; size_t length; uint8_t hash[32]; };\n", f);
770 fputs("static const uint8_t ck_local_keys[1][32] = {", f); cbytes(f, pk, 32); fputs("};\n", f);
771 pack_target = CKA_TARGET_X86_64_LE; pack_abi = CKA_ABI_OMEGA_X86_LOCAL_V1;
772 for (unsigned i = 0; i < 3; i++) {
773 struct buf raw = readf(argv[3 + i]);
774 if (!raw.n || raw.n > 65536) die("local code limit");
775 struct manifest m = {0};
776 m.env = (struct cka_env){.code_pages=16, .data_pages=32, .stack_pages=16,
777 .cpu=100000, .elapsed=500000, .syscalls=1};
778 const uint8_t zero = 0;
779 struct buf art = pack(&m, raw.p, raw.n, &zero, 1);
780 sign_with(&art, key.p);
781 sha256_hash(art.p, art.n, hashes[i]);
782 fprintf(f, "static const uint8_t ck_local_art%u[] = ", i);
783 cbytes(f, art.p, art.n); fputs(";\n", f);
784 free(raw.p); free(art.p);
785 }
786 fputs("static const struct ck_local_artifact ck_local_artifacts[3] = {\n", f);
787 for (unsigned i = 0; i < 3; i++) {
788 fprintf(f, "{ck_local_art%u, sizeof ck_local_art%u, ", i, i);
789 cbytes(f, hashes[i], 32); fputs("},\n", f);
790 }
791 fputs("};\n", f);
792 volatile uint8_t *wipe = key.p;
793 for (unsigned i = 0; i < 32; i++) wipe[i] = 0;
794 free(key.p);
795 if (fclose(f)) die("bundle output close");
796 puts("LOCAL_BUNDLE: PASS owner-signed x86 artifacts");
797 return 0;
798 }
799
800 if (argc == 6 && !strcmp(argv[1], "pack")) {
801 struct buf mj = readf(argv[2]), c = readf(argv[3]), d = readf(argv[4]);
802 struct manifest m;
803 parse_manifest(&mj, &m);
804 struct buf o = pack(&m, c.p, c.n, d.p, d.n);
805 writef(argv[5], o.p, o.n);
806 printf("PACK: PASS\n");
807 return 0;
808 }
809 if (argc == 4 && !strcmp(argv[1], "sign")) {
810 struct buf b = readf(argv[2]);
811 struct cka_parsed p;
812 if (cka_parse(b.p, b.n, &p) != 0)
813 die("does not parse");
814 sign_with(&b, test1_seed);
815 if (cka_parse(b.p, b.n, &p) != 0)
816 die("signed artifact does not parse");
817 writef(argv[3], b.p, b.n);
818 uint8_t fp[32];
819 sha256_hash(cka_test1_pk, 32, fp);
820 printf("SIGN: TEST ONLY SEED-0B QUALIFICATION IDENTITY\n");
821 phex("SIGNER_FINGERPRINT: ", fp);
822 return 0;
823 }
824 if (argc == 3 && !strcmp(argv[1], "id")) {
825 struct buf b = readf(argv[2]);
826 struct cka_parsed p;
827 struct cka_ident id;
828 int e = cka_identify(b.p, b.n, &p, &id);
829 if (e) {
830 fprintf(stderr, "error: %s\n", cka_error_name((unsigned)e));
831 return 1;
832 }
833 phex("", id.id);
834 return 0;
835 }
836 if (argc == 3 && !strcmp(argv[1], "verify")) {
837 struct buf b = readf(argv[2]);
838 struct cka_parsed p;
839 struct cka_ident id;
840 uint8_t fp[32];
841 int e = verify_test(&b, &p, &id, fp);
842 if (e) {
843 fprintf(stderr, "error: %s\n", cka_error_name((unsigned)e));
844 return 1;
845 }
846 printf("VERIFY: PASS\n");
847 phex("ARTIFACT_ID: ", id.id);
848 phex("PAYLOAD_SHA256: ", id.payload);
849 phex("SIGNER_FINGERPRINT: ", fp);
850 printf("TRUST_TIER: SEED-0B-QUALIFICATION-TEST-ONLY\n");
851 return 0;
852 }
853 if (argc == 3 && !strcmp(argv[1], "negative-corpus")) {
854 corpus(argv[2]);
855 return 0;
856 }
857 if (argc >= 3 && !strcmp(argv[1], "bundle")) {
858 bundle(argc - 2, argv + 2);
859 return 0;
860 }
861 if (argc >= 2 && !strcmp(argv[1], "receipt"))
862 return receipt(argc - 2, argv + 2);
863 fprintf(stderr, "usage: ck_artifact_tool pack MANIFEST CODE DATA OUT | sign IN OUT | id F | "
864 "verify F | negative-corpus DIR | bundle OUT FILE... | receipt ...\n");
865 return 2;
866}