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 / store / store_sealed.h

203 lines · 9937 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/* store_sealed.h -- M5 "sealed store" over the C Store v1 engine.
18 *
19 * Every application object is stored as an M5 envelope (AES-256-GCM-SIV,
20 * native/m5) in a Store object of kind SS_KIND_ENVELOPE. Each Store
21 * transaction carries exactly one sealed transaction record (kind
22 * SS_KIND_TXREC, magic "AIENSTX1") that holds one M5 commit record per
23 * envelope and binds them to the Store generation, the Store ObjectIds, the
24 * identity class and the previous transaction record (a hash chain). The
25 * record is MACed with k_root_auth.
26 *
27 * The anti-rollback anchor (m5_anchor, counter = Store generation) lives in
28 * a separate 4-unit torn_slot region outside the Store region.
29 *
30 * Mount: st_open -> ts_recover -> m5 open (verify every transaction record,
31 * the chain 2..G and every envelope digest) -> m5_evaluate_anti_rollback.
32 * Commit: (catch the anchor up to G if it lags) -> seal envelopes + record
33 * -> st_transact (Store generation G+1 durable) -> ts_commit anchor G+1.
34 * A crash after the Store commit and before the anchor commit leaves the
35 * anchor one behind: mount reports SS_RB_PREPARED_ADVANCE and the next
36 * transaction first catches the anchor up. Deterministic either way.
37 *
38 * Envelope ids and nonce prefixes are a keyed function of the plaintext
39 * (record version 2, see README), so a rewrite after a whole-disk rollback
40 * never reuses an AES-GCM-SIV (key, nonce) pair for different bytes.
41 *
42 * Limitation: the anchor sits on the same disk as the Store. It refuses a
43 * Store-only rollback, a stale anchor more than one step behind and any
44 * forged or replayed record, but an attacker who rolls back BOTH regions
45 * together is not detected. That needs TPM NV (TRUST-1), not done here.
46 *
47 * No heap, no clock, no I/O except through disk_dev / st_dev.
48 */
49#ifndef AIENOS_STORE_SEALED_H
50#define AIENOS_STORE_SEALED_H
51
52#include <stddef.h>
53#include <stdint.h>
54
55#include "../m5/m5.h"
56#include "store_engine.h"
57#include "torn_slot.h"
58
59#define SS_KIND_ENVELOPE 0x0510u /* M5 envelope of one application object */
60#define SS_KIND_TXREC 0x0511u /* sealed transaction record */
61/* ADR 0017 KeySlotManifest: the one plaintext object kind a sealed store
62 * admits. It is committed inside a sealed transaction, so the keyed
63 * transaction record binds its digest; the unkeyed bootstrap read happens
64 * before keys exist and the keyed open re-checks the same bytes. Rotation
65 * (ADR 0017 section 2.7) appends a new manifest whose manifest_sequence is
66 * the count + 1 and whose previous_keyslot_manifest_id is the Store ObjectId
67 * of the manifest it supersedes: the manifests on a Store form one chain
68 * 1..N and the newest (N) is the bound one. At most one per transaction. */
69#define SS_KIND_KEYSLOT 21u
70#define SS_KS_CHAIN_MAX 64u /* manifests (genesis + rotations) a Store may hold */
71#define SS_KEYSLOT_MIN 640u /* header + 4 descriptors */
72#define SS_KEYSLOT_MAX SS_MAX_PLAINTEXT
73#define SS_MAX_OBJECTS 8u /* application objects per transaction */
74#define SS_MAX_PLAINTEXT 16384u /* bytes per application object */
75#define SS_CHUNK 4096u /* envelope chunk size */
76#define SS_ENV_CAP (M5_ENV_HEADER_LEN + SS_MAX_PLAINTEXT + (SS_MAX_PLAINTEXT / SS_CHUNK) * M5_TAG_LEN)
77#define SS_TX_HDR 88u
78#define SS_TX_ENTRY (32u + M5_COMMIT_LEN)
79#define SS_TX_MAX (SS_TX_HDR + SS_MAX_OBJECTS * SS_TX_ENTRY + 32u)
80
81/* Sealed errors. Store errors (ST_M_*, ST_E_*) pass through unchanged. */
82enum {
83 SS_E_ARG = -301, /* NULL, bad count/size, unknown identity class */
84 SS_E_IDENTITY = -302, /* PRODUCTION/TEST class or store identity mismatch */
85 SS_E_ROLLBACK = -303, /* Store older than the anchor, fork, or missing anchor */
86 SS_E_INCONSISTENT = -304, /* Store more than one generation ahead of the anchor */
87 SS_E_ANCHOR = -305, /* anchor region corrupt, conflicting, or record malformed */
88 SS_E_IO = -306, /* anchor region device error */
89 SS_E_ENVELOPE = -307, /* envelope torn, unbound, unclaimed or failed to open */
90 SS_E_TXREC = -308, /* transaction record malformed or MAC failed */
91 SS_E_ORDER = -309, /* generation skip, gap, stale record, broken chain */
92 SS_E_DUPLICATE = -310, /* two records for one generation, envelope claimed twice */
93 SS_E_FOREIGN_KIND = -311, /* plaintext (non-sealed) object inside a sealed store */
94 SS_E_CRYPTO = -312, /* sealing failed */
95 SS_E_NEEDS_REOPEN = -313, /* an anchor write failed; reopen */
96 SS_E_FORMAT_VERSION = -314, /* version-1 record (predictable envelope ids): reformat */
97 SS_E_KEYSLOT = -315, /* keyslot manifest malformed, chain broken, unbound, or absent */
98};
99const char *ss_strerror(int e);
100
101/* Anti-rollback decision reported by ss_open. */
102enum { SS_RB_VALID_RESUME = 1, SS_RB_PREPARED_ADVANCE = 2, SS_RB_GENESIS = 3 };
103
104/* Extra checkpoints passed to the hook around the anchor commit. */
105enum { SS_CP_BEFORE_ANCHOR = 100, SS_CP_AFTER_ANCHOR = 101 };
106
107typedef struct {
108 uint8_t identity_class; /* M5_ID_PRODUCTION or M5_ID_TEST */
109 uint64_t key_generation;
110 uint8_t k_root_auth[32]; /* MAC key: records, anchor */
111 uint8_t k_domain[32]; /* envelope domain key (k_artifact) */
112} ss_keys;
113
114typedef struct {
115 uint16_t kind; /* application kind, carried in the M5 binding */
116 uint16_t version;
117 const uint8_t *bytes;
118 size_t len; /* 0 .. SS_MAX_PLAINTEXT */
119} ss_object;
120
121/* One verified envelope: its Store ObjectId and its M5 commit record. */
122typedef struct {
123 uint8_t sid[32];
124 m5_commit c; /* verified at open or commit */
125} ss_claim;
126
127typedef struct {
128 uint64_t gen;
129 uint8_t digest[32];
130 uint8_t prev[32];
131} ss_txinfo;
132
133typedef struct {
134 st_workspace st;
135 uint8_t rd[SS_ENV_CAP];
136 uint8_t tsrec[TS_RECORD_BYTES];
137 uint8_t env[SS_MAX_OBJECTS][SS_ENV_CAP];
138 size_t env_len[SS_MAX_OBJECTS];
139 uint8_t tx[SS_TX_MAX];
140 size_t tx_len;
141 st_object objs[SS_MAX_OBJECTS + 1];
142 size_t nobjs;
143 ss_claim claims[ST_CAT_CAP];
144 ss_txinfo txs[ST_CAT_CAP];
145 struct { uint8_t sid[32]; uint8_t prev[32]; uint64_t seq; } ks[SS_KS_CHAIN_MAX]; /* keyslot chain */
146 uint32_t bygen[ST_CAT_CAP]; /* generation - 2 -> index in txs */
147 uint8_t used[ST_CAT_CAP]; /* catalog entry already claimed */
148} ss_workspace;
149
150typedef struct {
151 st_store st;
152 ts_region ts;
153 ss_keys keys;
154 ss_workspace *ws;
155 uint8_t uuid[16];
156 uint64_t anchor_gen; /* 0: anchor region empty */
157 uint8_t last_tx_digest[32]; /* SHA-256 of the record of generation G (zero at G = 1) */
158 uint32_t nclaims;
159 int rb; /* SS_RB_* from the last open */
160 int needs_reopen;
161 int has_keyslot; /* a kind-21 KeySlotManifest chain is committed and bound */
162 uint8_t keyslot_sid[32]; /* Store object id of the newest manifest (sequence nkeyslot) */
163 uint32_t nkeyslot; /* manifests in the chain (0 on a Store without one) */
164} ss_store;
165
166/* Unkeyed bootstrap read of the newest KeySlotManifest (kind 21) before any
167 * key exists: structural Store open into scratch, every kind-21 object
168 * shape-checked (magic, format version) and the chain 1..N verified by
169 * manifest_sequence and previous_keyslot_manifest_id; out receives the
170 * newest. SS_E_KEYSLOT when there is none or the chain is broken. The keyed
171 * ss_open later re-binds the same bytes through the transaction records
172 * that committed them (has_keyslot, keyslot_sid, nkeyslot). */
173int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *ws, uint8_t *out, size_t cap,
174 size_t *len);
175
176/* torn_slot device over a disk_dev (adds the disk layer's I/O splitting). */
177void ss_ts_device(const disk_dev *disk, ts_device *out);
178
179/* Zero the anchor region, write a fresh Store at genesis, then commit a
180 * generation-1 anchor. anchor_lba is the first
181 * block of the 4-unit anchor region (outside the Store region). */
182int ss_format(const st_dev *store, const ts_device *anchor_dev, uint64_t anchor_lba,
183 const uint8_t uuid[16], const ss_keys *keys, ss_workspace *ws);
184/* Mount. Never writes. Returns 0 or an ST_M_* / SS_E_* error. */
185int ss_open(ss_store *s, const st_dev *store, const ts_device *anchor_dev, uint64_t anchor_lba,
186 const ss_keys *keys, ss_workspace *ws);
187/* Seal and commit n objects as one Store transaction. ids_out (optional)
188 * receives each envelope's Store ObjectId. */
189int ss_transact(ss_store *s, const ss_object *objs, size_t n, st_hook hook, void *arg,
190 uint8_t (*ids_out)[32]);
191/* Decrypt one envelope by its Store ObjectId. */
192int ss_read(ss_store *s, const uint8_t sid[32], uint8_t *out, size_t cap, size_t *len,
193 uint16_t *kind);
194static inline uint64_t ss_generation(const ss_store *s) { return st_generation(&s->st); }
195
196/* Split form of ss_transact, exposed for tests that forge records:
197 * ss_prepare seals into ws (env[], tx); ss_tx_remac recomputes the record
198 * MAC after a test edits ws->tx; ss_commit_prepared writes it. */
199int ss_prepare(ss_store *s, const ss_object *objs, size_t n);
200void ss_tx_remac(ss_store *s);
201int ss_commit_prepared(ss_store *s, st_hook hook, void *arg);
202
203#endif /* AIENOS_STORE_SEALED_H */