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 / svc / artifact_store.h

117 lines · 5673 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/* artifact_store.h -- signed P2 artifacts on the boot disk, kept as ordinary
18 * application objects inside the sealed C Store (store_sealed.h). No second
19 * disk format: an artifact is one or more Store objects of kind
20 * CK_ART_CHUNK_KIND plus one index object of kind CK_ART_INDEX_KIND, all
21 * sealed, catalogued and anchored exactly like the boot record.
22 *
23 * Lookup: the index with the highest Store generation names the candidates
24 * (name, byte length, chunk count) in boot order. Each chunk object carries
25 * its artifact's name, total length, chunk number and chunk count; a chunk
26 * is used only if it matches an index entry exactly and is not newer than
27 * that index. Index entries and chunks both carry the SHA-256 of the whole
28 * artifact; a chunk is used only for the entry with the same digest, and the
29 * assembled bytes must hash to it, so chunks of different writes are never
30 * spliced. An entry with any chunk absent is MISSING, one whose assembly does
31 * not hash to its digest is MISMATCH (the loader refuses both at stage
32 * "received", reason FirmwareRead), never partly loaded.
33 *
34 * Trust: the Store keys are TEST keys from a public label (store_boot.h), so
35 * the Store's MACs prove nothing about who wrote an artifact. Bytes read here
36 * are untrusted input: the kernel loader (core/artifact_loader.c) still runs
37 * the full format, signature, digest and admission checks on them.
38 *
39 * index "AIENAIX1" u32 version(1) u32 count, then count entries of 80:
40 * u8 name_len(1..32) u8[3] 0, char name[32] (zero padded),
41 * u32 byte_len(1..CK_ART_MAX_BYTES), u16 chunks, u16 0, u32 0,
42 * u8 sha256[32] of the whole artifact
43 * chunk "AIENACH1" u32 version(1) u32 total_len u16 chunk u16 chunks
44 * u8 name_len u8[3] 0, char name[32], u8 sha256[32] of the whole
45 * artifact, then the chunk's bytes
46 * (CK_ART_CHUNK_DATA per chunk, the last one the remainder)
47 * All integers little endian. */
48#ifndef AIENOS_CK_ARTIFACT_STORE_H
49#define AIENOS_CK_ARTIFACT_STORE_H
50#include <stddef.h>
51#include <stdint.h>
52#include "store_sealed.h"
53
54#define CK_ART_INDEX_KIND 0x0A01u
55#define CK_ART_CHUNK_KIND 0x0A02u
56#define CK_ART_VERSION 1u
57#define CK_ART_NAME_MAX 32u
58#define CK_ART_MAX_ENTRIES 48u /* = loader MAX_CANDIDATES */
59#define CK_ART_MAX_BYTES (160u * 4096u) /* = loader MAX_BATCH pages */
60#define CK_ART_INDEX_HDR 16u
61#define CK_ART_INDEX_ENTRY 80u
62#define CK_ART_CHUNK_HDR 88u
63#define CK_ART_CHUNK_DATA (SS_MAX_PLAINTEXT - CK_ART_CHUNK_HDR)
64#define CK_ART_MAX_CHUNKS ((CK_ART_MAX_BYTES + CK_ART_CHUNK_DATA - 1u) / CK_ART_CHUNK_DATA)
65
66enum { CK_ART_OK = 0, CK_ART_MISSING = 1, CK_ART_TOO_LARGE = 2, CK_ART_MISMATCH = 3 };
67
68typedef struct {
69 char name[CK_ART_NAME_MAX + 1];
70 uint32_t len; /* byte length from the index */
71 uint16_t chunks; /* chunk count from the index */
72 uint16_t present; /* chunks found */
73 int state; /* CK_ART_* */
74 uint8_t *bytes; /* len bytes, CK_ART_OK only (ck_alloc) */
75 uint8_t want[32]; /* artifact SHA-256 named by the index */
76 uint8_t sha256[32]; /* of bytes exactly as read, CK_ART_OK only */
77} ck_art_entry;
78
79typedef struct {
80 int found; /* an index object was read */
81 uint64_t index_generation; /* Store generation of that index */
82 uint8_t index_sha256[32]; /* of the index object's plaintext */
83 uint32_t count;
84 uint32_t chunks_used, chunks_ignored;
85 ck_art_entry e[CK_ART_MAX_ENTRIES];
86} ck_art_set;
87
88/* Read the newest index and every chunk it names from an open sealed store.
89 * 0 with set->found == 0: no index (no artifacts on this disk). 0 with
90 * set->found == 1: entries filled (each OK, MISSING, MISMATCH or TOO_LARGE). < 0: a
91 * malformed index or a Store read error; *why names it; nothing usable. */
92int ck_art_collect(ss_store *s, ck_art_set *set, const char **why);
93void ck_art_set_free(ck_art_set *set);
94void ck_stage_disk_artifacts_free(void);
95
96/* Store stage hook (svc/store_boot.c): s is the open Store after this boot's
97 * commit, or NULL when the Store was refused (refusal names why). Reads and
98 * prints the "artifact_disk:" lines; the core loader picks the result up
99 * through ck_stage_disk_artifacts (ck.h). Must run before the NVMe release. */
100void ck_art_stage_load(ss_store *s, const char *refusal);
101
102#ifdef CK_ART_STORE_WRITER
103/* Host-side writer (image build tool and host tests only; never compiled
104 * into the kernel). Writes every chunk (at most SS_MAX_OBJECTS per Store
105 * transaction), then the index in a final transaction, so an index is only
106 * ever committed after all of its chunks. missing != 0 puts the entry in the
107 * index (byte_len = len, which must still be >= 1) without writing chunks:
108 * a deliberately absent artifact for the hostile disk case. */
109typedef struct {
110 const char *name;
111 const uint8_t *bytes;
112 size_t len;
113 int missing;
114} ck_art_input;
115int ck_art_write(ss_store *s, const ck_art_input *in, size_t n);
116#endif
117#endif