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 / continuity_resolve.h

160 lines · 7862 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/* continuity_resolve.h -- C port, cut 3: boot resolution of the continuity
18 * objects (INV-4..INV-7, contract section 3), the Recovery Core challenge and
19 * the operator response (INV-15/16 inputs, contract 1.7 and 5.2).
20 *
21 * Contract: native/kernel/CONTINUITY_RECOVERY_CONTRACT.md. Oracle: the Rust
22 * kernel at aienos 4a116e5: continuity.rs `resolve` (:637-710, "rs:" below),
23 * recovery_core.rs `challenge` (:94-104) and `inspect` digest (:109-133),
24 * recovery.rs `OperatorAuth` (:103, :124-140).
25 *
26 * Resolve only. No provision, no commit, no resume (cut 4). Nothing here
27 * writes: the storage interface (cr_source) has no write call.
28 *
29 * Storage access goes through cr_source. continuity_resolve_sealed.c binds it
30 * to the real sealed Store (ss_store); the host test drives the real sealed
31 * Store in a file and also a fake source for cases the real Store cannot make.
32 *
33 * Lookup (contract P-3): the table of (kind, version, logical id) is built once
34 * per resolve by reading every continuity object of kinds 16, 17, 18, 20 and
35 * hashing the plaintext (Store v1 ObjectId rule). The Rust code never sees a
36 * logical id twice (Store v1 dedups); the sealed Store gives every write its
37 * own envelope, so a logical id that appears twice is Corrupt here.
38 *
39 * Memory: the Rust Continuity view keeps every Cortex record. The C view keeps
40 * the record count and the streamed SHA-256 `memory` digest (contract 1.7),
41 * which is all the QEMU markers and a resume need; no record is stored.
42 *
43 * Nothing here is wired into the kernel. */
44#ifndef CK_CONTINUITY_RESOLVE_H
45#define CK_CONTINUITY_RESOLVE_H
46
47#include <stddef.h>
48#include <stdint.h>
49
50#include "continuity_codec.h"
51
52/* TEST-only mutants for the host mutation run (Makefile continuity-mutants). */
53#if defined(CK_HARDWARE_STAGING) && \
54 (defined(CR_MUTANT_NO_ORPHAN_CHECK) || defined(CR_MUTANT_IGNORE_PREVIOUS) || \
55 defined(CR_MUTANT_SKIP_CONFLICT) || defined(CR_MUTANT_CHALLENGE_NO_DIGEST) || \
56 defined(CR_MUTANT_CHALLENGE_NO_ACTION) || defined(CR_MUTANT_COMPARE_16) || \
57 defined(CR_MUTANT_BARE_SHA256) || defined(CR_MUTANT_NO_DOMAIN))
58#error "continuity resolve mutants are TEST-only and refused under CK_HARDWARE_STAGING"
59#endif
60
61/* ---- outcomes (ContinuityError, continuity.rs:40-55) ---- */
62enum {
63 CR_RESOLVED = 0, /* Ok(Continuity) */
64 CR_UNPROVISIONED = 1, /* ContinuityError::Unprovisioned */
65 CR_CONFLICT = 2, /* ContinuityError::Conflict */
66 CR_CORRUPT = 3, /* ContinuityError::Corrupt(why) */
67 CR_LIMIT = 4, /* ContinuityError::Limit(why) */
68 CR_STORE = 5, /* ContinuityError::Store(e): *store_rc has the error */
69 CR_ALREADY_PROVISIONED = 6, /* provision only (cut 4); never returned by resolve */
70 CR_READ_ONLY = 7, /* cr_writable on a mount that is not Valid */
71 CR_E_ARG = 8, /* C only: caller bug (NULL); nothing read */
72 CR_NO_ENTROPY = 9, /* C only: provision found no entropy (no fallback), nothing written */
73};
74const char *cr_outcome_name(int outcome); /* Rust spelling: "Unprovisioned", ... */
75
76/* Entry reason for a degraded mount: the Recovery Core uses these (cut 5). */
77#define CR_MOUNT_VALID 0
78#define CR_MOUNT_DEGRADED 1
79
80/* ---- storage interface (read only) ---- */
81struct cr_source {
82 void *ctx;
83 /* Number of verified application objects (sealed claims). */
84 uint32_t (*count)(void *ctx);
85 /* Application kind and version of object i (no decryption), and nothing
86 * else. Returns 0 or a store error. */
87 int (*entry)(void *ctx, uint32_t i, uint16_t *kind, uint16_t *version);
88 /* Plaintext of object i into out[0..cap). *len is the true length; if it
89 * exceeds cap the object is oversize and out is not meaningful. Returns 0
90 * or a store error (nonzero, passed through as CR_STORE). */
91 int (*read)(void *ctx, uint32_t i, uint8_t *out, size_t cap, size_t *len);
92 /* CR_MOUNT_VALID or CR_MOUNT_DEGRADED. */
93 int (*mount_state)(void *ctx);
94 /* Store generation (rs `store.generation()`); NULL for a read-only fake. Provision
95 * needs it (provisioned_generation = generation + 1) and refuses CR_E_ARG without. */
96 uint64_t (*generation)(void *ctx);
97};
98
99/* Objects this layer tracks: kinds 16, 17, 18, 20 (kind 19 is ignored, as in Rust). */
100#define CR_TABLE_CAP 4096u /* SV1_MAX_CATALOG_ENTRIES (store/v1.rs:12) */
101
102struct cr_entry {
103 uint8_t id[32]; /* logical ObjectId (store v1 rule, kind, claim version) */
104 uint64_t seq; /* manifests: sequence */
105 uint8_t root[32]; /* manifests: root */
106 uint8_t previous[32]; /* manifests: previous */
107 uint32_t src; /* index in the source */
108 uint16_t kind, version;
109};
110
111/* Caller-owned scratch, ~0.5 MiB: static storage. */
112struct cr_work {
113 struct cr_entry t[CR_TABLE_CAP];
114 uint32_t n;
115 uint32_t mi[CR_TABLE_CAP]; /* manifest table indices, sorted by sequence */
116 uint8_t buf[CC_MAX_OBJECT_BYTES];
117 struct cc_wal wal;
118};
119
120/* The verified continuity view (rs `Continuity`, continuity.rs:726-736). */
121struct cr_view {
122 uint8_t root_id[32];
123 struct cc_root root;
124 uint8_t manifest_id[32];
125 struct cc_manifest manifest;
126 struct cc_state state;
127 uint64_t cortex_count; /* records over all segments */
128 uint8_t memory[32]; /* SHA-256 over (len u32 LE || statement) per record; marker prints [0..8) */
129};
130
131/* Locate and verify the continuity view without writing. Never creates an
132 * identity. Returns a CR_* outcome. For CR_CORRUPT / CR_LIMIT *why (optional)
133 * is the Rust reason text (or a C-only reason, see contract section 9); for
134 * CR_STORE *store_rc (optional) is the store error. */
135int cr_resolve(const struct cr_source *src, struct cr_work *w, struct cr_view *v, const char **why,
136 int *store_rc);
137
138/* rs `writable` (continuity.rs:776-781): CR_RESOLVED when the mount is Valid,
139 * else CR_READ_ONLY (INV-11). */
140int cr_writable(const struct cr_source *src);
141
142/* ---- Recovery Core challenge and operator response ---- */
143#define CR_ACTION_REPAIR_DEGRADED_PEER 1u /* recovery_core.rs:38-44 */
144#define CR_ACTION_PROVISION_IDENTITY 2u
145
146/* state_digest = SHA-256(unit 0 || unit 1), 4096 bytes each; a unit that could
147 * not be read is passed as 4096 zero bytes (recovery_core.rs:109-133). */
148void cr_state_digest(const uint8_t unit0[4096], const uint8_t unit1[4096], uint8_t out[32]);
149/* SHA-256("AIENOS-RECOVERY-CHALLENGE-v1\0" || uuid || generation u64 LE ||
150 * action u8 || state_digest), recovery_core.rs:94-104. */
151void cr_challenge(const uint8_t uuid[16], uint64_t generation, uint8_t action,
152 const uint8_t state_digest[32], uint8_t out[32]);
153/* HMAC-SHA256(key, "AIENOS-RECOVERY-OPERATOR-AUTH-v1\0" || challenge),
154 * recovery.rs:103, :124-140. */
155void cr_operator_response(const uint8_t key[32], const uint8_t challenge[32], uint8_t out[32]);
156/* 1 if response is the operator response, compared in constant time (all 32 bytes). */
157int cr_operator_verify(const uint8_t key[32], const uint8_t challenge[32],
158 const uint8_t response[32]);
159
160#endif /* CK_CONTINUITY_RESOLVE_H */