aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / continuity_resolve.c
| 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.c -- see continuity_resolve.h. Resolve only; no write. */ |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "continuity_resolve.h" |
| 21 | #include "../../argus/sha256.h" |
| 22 | #include "../../crypto/aienos_crypto.h" |
| 23 | #include "../../store/store_v1.h" |
| 24 | |
| 25 | const char *cr_outcome_name(int o) |
| 26 | { |
| 27 | switch (o) { |
| 28 | case CR_RESOLVED: return "Resolved"; |
| 29 | case CR_UNPROVISIONED: return "Unprovisioned"; |
| 30 | case CR_CONFLICT: return "Conflict"; |
| 31 | case CR_CORRUPT: return "Corrupt"; |
| 32 | case CR_LIMIT: return "Limit"; |
| 33 | case CR_STORE: return "Store"; |
| 34 | case CR_ALREADY_PROVISIONED: return "AlreadyProvisioned"; |
| 35 | case CR_READ_ONLY: return "ReadOnly"; |
| 36 | case CR_E_ARG: return "Arg"; |
| 37 | case CR_NO_ENTROPY: return "NoEntropy"; |
| 38 | } |
| 39 | return "?"; |
| 40 | } |
| 41 | |
| 42 | struct ctx { |
| 43 | const struct cr_source *src; |
| 44 | struct cr_work *w; |
| 45 | const char *why; |
| 46 | int store_rc; |
| 47 | }; |
| 48 | |
| 49 | static int fail(struct ctx *c, int outcome, const char *why) |
| 50 | { |
| 51 | c->why = why; |
| 52 | return outcome; |
| 53 | } |
| 54 | |
| 55 | static int is_tracked(uint16_t k) |
| 56 | { |
| 57 | return k == CC_KIND_AGENT_ROOT || k == CC_KIND_MANIFEST || k == CC_KIND_AGENT_STATE || |
| 58 | k == CC_KIND_CORTEX_WAL; |
| 59 | } |
| 60 | |
| 61 | static int is_zero32(const uint8_t *p) |
| 62 | { |
| 63 | uint8_t a = 0; |
| 64 | for (int i = 0; i < 32; i++) a |= p[i]; |
| 65 | return a == 0; |
| 66 | } |
| 67 | |
| 68 | /* Read source object i into w->buf. 0, or a CR_* outcome with c->why / c->store_rc set. */ |
| 69 | static int read_obj(struct ctx *c, uint32_t i, size_t *len) |
| 70 | { |
| 71 | int rc = c->src->read(c->src->ctx, i, c->w->buf, sizeof c->w->buf, len); |
| 72 | if (rc != 0) { |
| 73 | c->store_rc = rc; |
| 74 | return CR_STORE; |
| 75 | } |
| 76 | /* K-1 (contract 5.3): never truncate, never decode past the bound. */ |
| 77 | if (*len > CC_MAX_OBJECT_BYTES) return fail(c, CR_LIMIT, "continuity object exceeds the size bound"); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* rs read_kind (continuity.rs:619-633): the entry for id must exist with the |
| 82 | * kind and Store version 1; its plaintext lands in w->buf. */ |
| 83 | static int read_kind(struct ctx *c, const uint8_t id[32], uint16_t kind, size_t *len) |
| 84 | { |
| 85 | const struct cr_entry *e = NULL; |
| 86 | for (uint32_t i = 0; i < c->w->n; i++) |
| 87 | if (memcmp(c->w->t[i].id, id, 32) == 0) { |
| 88 | e = &c->w->t[i]; |
| 89 | break; |
| 90 | } |
| 91 | if (!e) return fail(c, CR_CORRUPT, "referenced object is absent"); |
| 92 | if (e->kind != kind || e->version != CC_STORE_OBJECT_VERSION) |
| 93 | return fail(c, CR_CORRUPT, "referenced object has the wrong kind"); |
| 94 | int r = read_obj(c, e->src, len); |
| 95 | if (r) return r; |
| 96 | uint8_t chk[32]; |
| 97 | if (sv1_object_id(e->kind, e->version, c->w->buf, *len, chk) != 0 || memcmp(chk, id, 32) != 0) |
| 98 | return fail(c, CR_CORRUPT, "object changed since lookup"); /* C only */ |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int map_cc(struct ctx *c, int rc, const char *why) |
| 103 | { |
| 104 | if (rc == CC_OK) return 0; |
| 105 | c->why = why ? why : "continuity object"; |
| 106 | if (rc == CC_E_CORRUPT) return CR_CORRUPT; |
| 107 | if (rc == CC_E_LIMIT) return CR_LIMIT; |
| 108 | return CR_E_ARG; |
| 109 | } |
| 110 | |
| 111 | static int build_table(struct ctx *c) |
| 112 | { |
| 113 | struct cr_work *w = c->w; |
| 114 | uint32_t cnt = c->src->count(c->src->ctx); |
| 115 | w->n = 0; |
| 116 | for (uint32_t i = 0; i < cnt; i++) { |
| 117 | uint16_t k = 0, ver = 0; |
| 118 | int rc = c->src->entry(c->src->ctx, i, &k, &ver); |
| 119 | if (rc != 0) { |
| 120 | c->store_rc = rc; |
| 121 | return CR_STORE; |
| 122 | } |
| 123 | if (!is_tracked(k)) continue; |
| 124 | if (w->n >= CR_TABLE_CAP) return fail(c, CR_LIMIT, "too many continuity objects"); |
| 125 | size_t len = 0; |
| 126 | int r = read_obj(c, i, &len); |
| 127 | if (r) return r; |
| 128 | struct cr_entry *e = &w->t[w->n]; |
| 129 | memset(e, 0, sizeof *e); |
| 130 | if (sv1_object_id(k, ver, w->buf, len, e->id) != 0) |
| 131 | return fail(c, CR_CORRUPT, "continuity object has no valid id"); /* C only: empty */ |
| 132 | /* P-3: a logical id seen twice is Corrupt. */ |
| 133 | for (uint32_t j = 0; j < w->n; j++) |
| 134 | if (memcmp(w->t[j].id, e->id, 32) == 0) |
| 135 | return fail(c, CR_CORRUPT, "duplicate logical object id"); /* C only */ |
| 136 | e->kind = k; |
| 137 | e->version = ver; |
| 138 | e->src = i; |
| 139 | w->n++; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static uint32_t count_kind(const struct cr_work *w, uint16_t kind) |
| 145 | { |
| 146 | uint32_t n = 0; |
| 147 | for (uint32_t i = 0; i < w->n; i++) n += w->t[i].kind == kind; |
| 148 | return n; |
| 149 | } |
| 150 | |
| 151 | static void put32(uint8_t *p, uint32_t v) |
| 152 | { |
| 153 | for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); |
| 154 | } |
| 155 | |
| 156 | int cr_resolve(const struct cr_source *src, struct cr_work *w, struct cr_view *v, const char **why, |
| 157 | int *store_rc) |
| 158 | { |
| 159 | struct ctx c = {src, w, NULL, 0}; |
| 160 | int out; |
| 161 | if (why) *why = NULL; |
| 162 | if (store_rc) *store_rc = 0; |
| 163 | if (!src || !src->count || !src->entry || !src->read || !w || !v) return CR_E_ARG; |
| 164 | memset(v, 0, sizeof *v); |
| 165 | |
| 166 | out = build_table(&c); |
| 167 | if (out) goto done; |
| 168 | |
| 169 | /* INV-5, in this order (rs :638-653). */ |
| 170 | uint32_t roots = count_kind(w, CC_KIND_AGENT_ROOT); |
| 171 | if (roots == 0) { |
| 172 | #ifndef CR_MUTANT_NO_ORPHAN_CHECK |
| 173 | if (count_kind(w, CC_KIND_MANIFEST) || count_kind(w, CC_KIND_AGENT_STATE) || |
| 174 | count_kind(w, CC_KIND_CORTEX_WAL)) { |
| 175 | out = fail(&c, CR_CORRUPT, "continuity objects without an agent root"); |
| 176 | goto done; |
| 177 | } |
| 178 | #endif |
| 179 | out = CR_UNPROVISIONED; |
| 180 | goto done; |
| 181 | } |
| 182 | #ifndef CR_MUTANT_SKIP_CONFLICT |
| 183 | if (roots > 1) { |
| 184 | out = CR_CONFLICT; |
| 185 | goto done; |
| 186 | } |
| 187 | #endif |
| 188 | const struct cr_entry *re = NULL; |
| 189 | for (uint32_t i = 0; i < w->n && !re; i++) |
| 190 | if (w->t[i].kind == CC_KIND_AGENT_ROOT) re = &w->t[i]; |
| 191 | memcpy(v->root_id, re->id, 32); |
| 192 | size_t len = 0; |
| 193 | if ((out = read_kind(&c, v->root_id, CC_KIND_AGENT_ROOT, &len))) goto done; |
| 194 | { |
| 195 | const char *y = NULL; |
| 196 | int rc = cc_root_decode(w->buf, len, &v->root, &y); |
| 197 | if ((out = map_cc(&c, rc, y))) goto done; |
| 198 | } |
| 199 | |
| 200 | /* INV-6: every manifest names this root; sorted by sequence they are 1..n. */ |
| 201 | uint32_t nm = 0; |
| 202 | for (uint32_t i = 0; i < w->n; i++) { |
| 203 | struct cr_entry *e = &w->t[i]; |
| 204 | if (e->kind != CC_KIND_MANIFEST) continue; |
| 205 | if ((out = read_kind(&c, e->id, CC_KIND_MANIFEST, &len))) goto done; |
| 206 | const char *y = NULL; |
| 207 | int rc = cc_manifest_decode(w->buf, len, &v->manifest, &y); |
| 208 | if ((out = map_cc(&c, rc, y))) goto done; |
| 209 | if (memcmp(v->manifest.root, v->root_id, 32) != 0) { |
| 210 | out = fail(&c, CR_CORRUPT, "manifest names a foreign root"); |
| 211 | goto done; |
| 212 | } |
| 213 | e->seq = v->manifest.sequence; |
| 214 | memcpy(e->root, v->manifest.root, 32); |
| 215 | memcpy(e->previous, v->manifest.previous, 32); |
| 216 | w->mi[nm++] = i; |
| 217 | } |
| 218 | if (nm == 0) { |
| 219 | out = fail(&c, CR_CORRUPT, "agent root has no manifest"); |
| 220 | goto done; |
| 221 | } |
| 222 | for (uint32_t i = 1; i < nm; i++) { /* stable insertion sort by sequence */ |
| 223 | uint32_t x = w->mi[i]; |
| 224 | uint32_t j = i; |
| 225 | while (j > 0 && w->t[w->mi[j - 1]].seq > w->t[x].seq) { |
| 226 | w->mi[j] = w->mi[j - 1]; |
| 227 | j--; |
| 228 | } |
| 229 | w->mi[j] = x; |
| 230 | } |
| 231 | static const uint8_t ZERO[32]; |
| 232 | for (uint32_t i = 0; i < nm; i++) { |
| 233 | const struct cr_entry *m = &w->t[w->mi[i]]; |
| 234 | if (m->seq != (uint64_t)i + 1) { |
| 235 | out = fail(&c, CR_CORRUPT, "manifest sequence gap or fork"); |
| 236 | goto done; |
| 237 | } |
| 238 | const uint8_t *expect = i ? w->t[w->mi[i - 1]].id : ZERO; |
| 239 | #ifndef CR_MUTANT_IGNORE_PREVIOUS |
| 240 | if (memcmp(m->previous, expect, 32) != 0) { |
| 241 | out = fail(&c, CR_CORRUPT, "manifest chain is broken"); |
| 242 | goto done; |
| 243 | } |
| 244 | #else |
| 245 | (void)expect; |
| 246 | #endif |
| 247 | } |
| 248 | const struct cr_entry *cur = &w->t[w->mi[nm - 1]]; |
| 249 | memcpy(v->manifest_id, cur->id, 32); |
| 250 | if ((out = read_kind(&c, v->manifest_id, CC_KIND_MANIFEST, &len))) goto done; |
| 251 | { |
| 252 | const char *y = NULL; |
| 253 | int rc = cc_manifest_decode(w->buf, len, &v->manifest, &y); |
| 254 | if ((out = map_cc(&c, rc, y))) goto done; |
| 255 | } |
| 256 | |
| 257 | /* INV-7: agent state of the current manifest. */ |
| 258 | if (is_zero32(v->manifest.agent_state)) { |
| 259 | out = fail(&c, CR_CORRUPT, "manifest has no agent state"); |
| 260 | goto done; |
| 261 | } |
| 262 | if ((out = read_kind(&c, v->manifest.agent_state, CC_KIND_AGENT_STATE, &len))) goto done; |
| 263 | { |
| 264 | const char *y = NULL; |
| 265 | int rc = cc_state_decode(w->buf, len, &v->state, &y); |
| 266 | if ((out = map_cc(&c, rc, y))) goto done; |
| 267 | } |
| 268 | if (memcmp(v->state.agent_id, v->root.agent_id, 32) != 0) { |
| 269 | out = fail(&c, CR_CORRUPT, "agent state belongs to another agent"); |
| 270 | goto done; |
| 271 | } |
| 272 | |
| 273 | /* WAL segments in manifest order, strictly increasing, <= manifest.sequence. */ |
| 274 | sha256_ctx mem; |
| 275 | sha256_init(&mem); |
| 276 | uint64_t last = 0; |
| 277 | for (uint32_t s = 0; s < v->manifest.n_wal; s++) { |
| 278 | if ((out = read_kind(&c, v->manifest.wal[s], CC_KIND_CORTEX_WAL, &len))) goto done; |
| 279 | const char *y = NULL; |
| 280 | int rc = cc_wal_decode(w->buf, len, &w->wal, &y); |
| 281 | if ((out = map_cc(&c, rc, y))) goto done; |
| 282 | if (w->wal.sequence <= last || w->wal.sequence > v->manifest.sequence) { |
| 283 | out = fail(&c, CR_CORRUPT, "WAL segments out of order"); |
| 284 | goto done; |
| 285 | } |
| 286 | last = w->wal.sequence; |
| 287 | for (uint32_t r = 0; r < w->wal.n; r++) { |
| 288 | uint8_t l4[4]; |
| 289 | put32(l4, w->wal.rec[r].len); |
| 290 | sha256_update(&mem, l4, 4); |
| 291 | sha256_update(&mem, w->wal.rec[r].statement, w->wal.rec[r].len); |
| 292 | v->cortex_count++; |
| 293 | } |
| 294 | } |
| 295 | sha256_final(&mem, v->memory); |
| 296 | out = CR_RESOLVED; |
| 297 | |
| 298 | done: |
| 299 | if (out != CR_RESOLVED) memset(v, 0, sizeof *v); |
| 300 | if (why) *why = c.why; |
| 301 | if (store_rc) *store_rc = c.store_rc; |
| 302 | return out; |
| 303 | } |
| 304 | |
| 305 | int cr_writable(const struct cr_source *src) |
| 306 | { |
| 307 | if (!src || !src->mount_state) return CR_READ_ONLY; /* fail closed */ |
| 308 | return src->mount_state(src->ctx) == CR_MOUNT_VALID ? CR_RESOLVED : CR_READ_ONLY; |
| 309 | } |
| 310 | |
| 311 | /* ---- challenge and operator response ---- */ |
| 312 | |
| 313 | void cr_state_digest(const uint8_t unit0[4096], const uint8_t unit1[4096], uint8_t out[32]) |
| 314 | { |
| 315 | sha256_ctx h; |
| 316 | sha256_init(&h); |
| 317 | sha256_update(&h, unit0, 4096); |
| 318 | sha256_update(&h, unit1, 4096); |
| 319 | sha256_final(&h, out); |
| 320 | } |
| 321 | |
| 322 | void cr_challenge(const uint8_t uuid[16], uint64_t generation, uint8_t action, |
| 323 | const uint8_t state_digest[32], uint8_t out[32]) |
| 324 | { |
| 325 | static const char D[] = "AIENOS-RECOVERY-CHALLENGE-v1"; /* + NUL: 29 bytes */ |
| 326 | uint8_t g[8]; |
| 327 | for (int i = 0; i < 8; i++) g[i] = (uint8_t)(generation >> (8 * i)); |
| 328 | sha256_ctx h; |
| 329 | sha256_init(&h); |
| 330 | sha256_update(&h, (const uint8_t *)D, sizeof D); /* includes the NUL */ |
| 331 | sha256_update(&h, uuid, 16); |
| 332 | sha256_update(&h, g, 8); |
| 333 | #ifndef CR_MUTANT_CHALLENGE_NO_ACTION |
| 334 | sha256_update(&h, &action, 1); |
| 335 | #else |
| 336 | (void)action; |
| 337 | #endif |
| 338 | #ifndef CR_MUTANT_CHALLENGE_NO_DIGEST |
| 339 | sha256_update(&h, state_digest, 32); |
| 340 | #else |
| 341 | (void)state_digest; |
| 342 | #endif |
| 343 | sha256_final(&h, out); |
| 344 | } |
| 345 | |
| 346 | void cr_operator_response(const uint8_t key[32], const uint8_t challenge[32], uint8_t out[32]) |
| 347 | { |
| 348 | static const char D[] = "AIENOS-RECOVERY-OPERATOR-AUTH-v1"; /* + NUL: 33 bytes */ |
| 349 | uint8_t msg[sizeof D + 32]; |
| 350 | #if defined(CR_MUTANT_BARE_SHA256) |
| 351 | sha256_ctx h; |
| 352 | sha256_init(&h); |
| 353 | sha256_update(&h, key, 32); |
| 354 | sha256_update(&h, challenge, 32); |
| 355 | sha256_final(&h, out); |
| 356 | (void)msg; |
| 357 | return; |
| 358 | #elif defined(CR_MUTANT_NO_DOMAIN) |
| 359 | (void)msg; |
| 360 | aienos_hmac_sha256(key, challenge, 32, out); |
| 361 | return; |
| 362 | #else |
| 363 | memcpy(msg, D, sizeof D); /* includes the NUL */ |
| 364 | memcpy(msg + sizeof D, challenge, 32); |
| 365 | aienos_hmac_sha256(key, msg, sizeof msg, out); |
| 366 | aienos_wipe(msg, sizeof msg); |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | int cr_operator_verify(const uint8_t key[32], const uint8_t challenge[32], const uint8_t response[32]) |
| 371 | { |
| 372 | uint8_t expected[32]; |
| 373 | cr_operator_response(key, challenge, expected); |
| 374 | #ifdef CR_MUTANT_COMPARE_16 |
| 375 | int ok = aienos_ct_equal(expected, response, 16); |
| 376 | #else |
| 377 | int ok = aienos_ct_equal(expected, response, 32); |
| 378 | #endif |
| 379 | aienos_wipe(expected, sizeof expected); |
| 380 | return ok; |
| 381 | } |