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 / m5 / m5_state.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 | /* M5 commit record, anti-rollback anchor, migration manifest and |
| 18 | * deterministic recovery. Port of security.rs sections 4-5 (C v2 layouts). */ |
| 19 | #include "m5_internal.h" |
| 20 | #include "../argus/sha256.h" |
| 21 | |
| 22 | /* ---- commit record (written after sealing) ---- |
| 23 | * 0 "AIENCMT1" | 8 version u16 = 2 | 10 identity_class | 11 reserved | 12 kind u16 |
| 24 | * | 14 object_version u16 | 16 store_uuid | 32 envelope_id[16] | 48 store_generation |
| 25 | * | 56 key_generation | 64 counter | 72 object_sequence | 80 object_id[32] |
| 26 | * | 112 mac = HMAC(k_root_auth, "AIENOS-M5-COMMIT-V2\0" || bytes[0..112]) |
| 27 | * object_id is SHA-256 of the sealed envelope, so Store generation and the |
| 28 | * ObjectId are bound here, after sealing, never inside the envelope. */ |
| 29 | #define COMMIT_DOMAIN "AIENOS-M5-COMMIT-V2" |
| 30 | #define COMMIT_MAC_OFF 112 |
| 31 | |
| 32 | int m5_commit_seal(const m5_commit *c, const uint8_t k_root_auth[32], uint8_t out[M5_COMMIT_LEN]) |
| 33 | { |
| 34 | if (!c || !k_root_auth || !out) return M5_ERR_ARG; |
| 35 | if (!m5_valid_class(c->obj.identity_class)) return M5_ERR_ARG; |
| 36 | memset(out, 0, M5_COMMIT_LEN); |
| 37 | memcpy(out, "AIENCMT1", 8); |
| 38 | m5_put16(out + 8, 2); |
| 39 | out[10] = c->obj.identity_class; |
| 40 | m5_put16(out + 12, c->obj.object_kind); |
| 41 | m5_put16(out + 14, c->obj.object_version); |
| 42 | memcpy(out + 16, c->obj.store_uuid, 16); |
| 43 | memcpy(out + 32, c->obj.envelope_id, 16); |
| 44 | m5_put64(out + 48, c->store_generation); |
| 45 | m5_put64(out + 56, c->obj.key_generation); |
| 46 | m5_put64(out + 64, c->counter); |
| 47 | m5_put64(out + 72, c->object_sequence); |
| 48 | memcpy(out + 80, c->object_id, 32); |
| 49 | m5_mac(k_root_auth, COMMIT_DOMAIN, sizeof(COMMIT_DOMAIN), out, COMMIT_MAC_OFF, out + COMMIT_MAC_OFF); |
| 50 | return M5_OK; |
| 51 | } |
| 52 | |
| 53 | int m5_commit_open(const uint8_t *b, size_t len, const uint8_t k_root_auth[32], |
| 54 | uint8_t mode, m5_commit *c) |
| 55 | { |
| 56 | if (!b || !k_root_auth || !c) return M5_ERR_ARG; |
| 57 | memset(c, 0, sizeof *c); |
| 58 | if (len != M5_COMMIT_LEN) return M5_ERR_BOUNDS; |
| 59 | if (memcmp(b, "AIENCMT1", 8) != 0 || m5_get16(b + 8) != 2 || b[11] != 0) return M5_ERR_FORMAT; |
| 60 | if (!m5_valid_class(b[10])) return M5_ERR_FORMAT; |
| 61 | uint8_t mac[32]; |
| 62 | m5_mac(k_root_auth, COMMIT_DOMAIN, sizeof(COMMIT_DOMAIN), b, COMMIT_MAC_OFF, mac); |
| 63 | int ok = aienos_ct_equal(mac, b + COMMIT_MAC_OFF, 32); |
| 64 | if (!ok) return M5_ERR_AUTH; /* GUARD:commit-mac */ |
| 65 | if (b[10] != mode) return M5_ERR_IDENTITY; /* GUARD:commit-class */ |
| 66 | c->obj.identity_class = b[10]; |
| 67 | c->obj.object_kind = m5_get16(b + 12); |
| 68 | c->obj.object_version = m5_get16(b + 14); |
| 69 | memcpy(c->obj.store_uuid, b + 16, 16); |
| 70 | memcpy(c->obj.envelope_id, b + 32, 16); |
| 71 | c->store_generation = m5_get64(b + 48); |
| 72 | c->obj.key_generation = m5_get64(b + 56); |
| 73 | c->counter = m5_get64(b + 64); |
| 74 | c->object_sequence = m5_get64(b + 72); |
| 75 | memcpy(c->object_id, b + 80, 32); |
| 76 | return M5_OK; |
| 77 | } |
| 78 | |
| 79 | int m5_commit_check_object(const m5_commit *c, const uint8_t *env, size_t env_len) |
| 80 | { |
| 81 | if (!c || !env) return M5_ERR_ARG; |
| 82 | uint8_t dg[32]; |
| 83 | sha256_hash(env, env_len, dg); |
| 84 | if (!aienos_ct_equal(dg, c->object_id, 32)) return M5_ERR_TORN; /* GUARD:commit-objid */ |
| 85 | m5_env_header h; |
| 86 | int rc = m5_envelope_parse_header(env, env_len, &h); |
| 87 | if (rc != M5_OK) return rc; |
| 88 | if (memcmp(h.envelope_id, c->obj.envelope_id, 16) != 0) return M5_ERR_BINDING; /* GUARD:commit-envid */ |
| 89 | if (h.key_epoch != c->obj.key_generation) return M5_ERR_BINDING; /* GUARD:commit-keygen */ |
| 90 | return M5_OK; |
| 91 | } |
| 92 | |
| 93 | /* ---- anti-rollback anchor ---- |
| 94 | * 0 "AIENRBA1" | 8 version u16 = 1 | 10 identity_class | 11..16 reserved |
| 95 | * | 16 store_uuid | 32 store_generation | 40 key_generation | 48 counter |
| 96 | * | 56 commit_digest[32] | 88 mac = HMAC(k_root_auth, "AIENOS-M5-ANCHOR-V1\0" || bytes[0..88]) */ |
| 97 | #define ANCHOR_DOMAIN "AIENOS-M5-ANCHOR-V1" |
| 98 | #define ANCHOR_MAC_OFF 88 |
| 99 | |
| 100 | int m5_anchor_seal(const m5_anchor *a, const uint8_t k_root_auth[32], uint8_t *out, size_t out_len) |
| 101 | { |
| 102 | if (!a || !k_root_auth || !out) return M5_ERR_ARG; |
| 103 | if (out_len < M5_ANCHOR_LEN) return M5_ERR_SPACE; |
| 104 | if (!m5_valid_class(a->identity_class)) return M5_ERR_ARG; |
| 105 | memset(out, 0, M5_ANCHOR_LEN); |
| 106 | memcpy(out, "AIENRBA1", 8); |
| 107 | m5_put16(out + 8, 1); |
| 108 | out[10] = a->identity_class; |
| 109 | memcpy(out + 16, a->store_uuid, 16); |
| 110 | m5_put64(out + 32, a->store_generation); |
| 111 | m5_put64(out + 40, a->key_generation); |
| 112 | m5_put64(out + 48, a->counter); |
| 113 | memcpy(out + 56, a->commit_digest, 32); |
| 114 | m5_mac(k_root_auth, ANCHOR_DOMAIN, sizeof(ANCHOR_DOMAIN), out, ANCHOR_MAC_OFF, out + ANCHOR_MAC_OFF); |
| 115 | return M5_OK; |
| 116 | } |
| 117 | |
| 118 | int m5_anchor_open(const uint8_t *b, size_t len, const uint8_t k_root_auth[32], |
| 119 | uint8_t mode, m5_anchor *a) |
| 120 | { |
| 121 | if (!b || !k_root_auth || !a) return M5_ERR_ARG; |
| 122 | memset(a, 0, sizeof *a); |
| 123 | if (len != M5_ANCHOR_LEN) return M5_ERR_BOUNDS; |
| 124 | if (memcmp(b, "AIENRBA1", 8) != 0 || m5_get16(b + 8) != 1) return M5_ERR_FORMAT; |
| 125 | if (!m5_all_zero(b + 11, 5) || !m5_valid_class(b[10])) return M5_ERR_FORMAT; |
| 126 | uint8_t mac[32]; |
| 127 | m5_mac(k_root_auth, ANCHOR_DOMAIN, sizeof(ANCHOR_DOMAIN), b, ANCHOR_MAC_OFF, mac); |
| 128 | if (!aienos_ct_equal(mac, b + ANCHOR_MAC_OFF, 32)) return M5_ERR_AUTH; /* GUARD:anchor-mac */ |
| 129 | if (b[10] != mode) return M5_ERR_IDENTITY; /* GUARD:anchor-class */ |
| 130 | a->identity_class = b[10]; |
| 131 | memcpy(a->store_uuid, b + 16, 16); |
| 132 | a->store_generation = m5_get64(b + 32); |
| 133 | a->key_generation = m5_get64(b + 40); |
| 134 | a->counter = m5_get64(b + 48); |
| 135 | memcpy(a->commit_digest, b + 56, 32); |
| 136 | return M5_OK; |
| 137 | } |
| 138 | |
| 139 | int m5_evaluate_anti_rollback(const uint8_t k_root_auth[32], uint8_t mode, |
| 140 | const uint8_t store_uuid[16], |
| 141 | const uint8_t *anchor_buf, size_t anchor_len, |
| 142 | int allow_genesis, const m5_disk_state *d) |
| 143 | { |
| 144 | if (!k_root_auth || !store_uuid || !d) return M5_ERR_ARG; |
| 145 | if (anchor_len == 0) return allow_genesis ? M5_RB_GENESIS : M5_ERR_ROLLBACK; /* GUARD:anchor-missing */ |
| 146 | if (!anchor_buf) return M5_ERR_ARG; |
| 147 | m5_anchor a; |
| 148 | int rc = m5_anchor_open(anchor_buf, anchor_len, k_root_auth, mode, &a); |
| 149 | if (rc != M5_OK) return rc; |
| 150 | if (memcmp(a.store_uuid, store_uuid, 16) != 0) return M5_ERR_BINDING; /* GUARD:anchor-store */ |
| 151 | if (d->store_generation < a.store_generation) return M5_ERR_ROLLBACK; /* GUARD:rb-storegen */ |
| 152 | if (d->key_generation < a.key_generation) return M5_ERR_ROLLBACK; /* GUARD:rb-keygen */ |
| 153 | if (d->counter < a.counter) return M5_ERR_ROLLBACK; /* GUARD:rb-counter */ |
| 154 | if (d->counter == a.counter) { |
| 155 | int same = aienos_ct_equal(d->commit_digest, a.commit_digest, 32) && |
| 156 | d->store_generation == a.store_generation && |
| 157 | d->key_generation == a.key_generation; |
| 158 | return same ? M5_RB_VALID_RESUME : M5_ERR_ROLLBACK; /* fork at the anchored counter */ |
| 159 | } |
| 160 | if (d->counter - a.counter == 1) return M5_RB_PREPARED_ADVANCE; /* d > a here: no wrap */ |
| 161 | return M5_ERR_INCONSISTENT; |
| 162 | } |
| 163 | |
| 164 | /* ---- migration manifest ---- |
| 165 | * 0 "AIENMIG1" | 8 version u16 = 2 | 10 flags u16 = 0 | 12 identity_class | 13..16 reserved |
| 166 | * | 16 agent_root_id[32] | 48 genesis_store_uuid | 64 source_store_uuid | 80 dest_store_uuid |
| 167 | * | 96 source_store_generation | 104 source_anchor_counter | 112 migration_counter |
| 168 | * | 120 owner_hierarchy_generation | 128 source_commit_digest[32] |
| 169 | * | 160 mac = HMAC(k_root_auth(source), "AIENOS-M5-MIGRATION-V2\0" || bytes[0..160]) */ |
| 170 | #define MIG_DOMAIN "AIENOS-M5-MIGRATION-V2" |
| 171 | #define MIG_MAC_OFF 160 |
| 172 | |
| 173 | int m5_migration_seal(const m5_migration *m, const uint8_t k_root_auth[32], uint8_t out[M5_MIGRATION_LEN]) |
| 174 | { |
| 175 | if (!m || !k_root_auth || !out) return M5_ERR_ARG; |
| 176 | if (!m5_valid_class(m->identity_class)) return M5_ERR_ARG; |
| 177 | memset(out, 0, M5_MIGRATION_LEN); |
| 178 | memcpy(out, "AIENMIG1", 8); |
| 179 | m5_put16(out + 8, 2); |
| 180 | out[12] = m->identity_class; |
| 181 | memcpy(out + 16, m->agent_root_id, 32); |
| 182 | memcpy(out + 48, m->genesis_store_uuid, 16); |
| 183 | memcpy(out + 64, m->source_store_uuid, 16); |
| 184 | memcpy(out + 80, m->dest_store_uuid, 16); |
| 185 | m5_put64(out + 96, m->source_store_generation); |
| 186 | m5_put64(out + 104, m->source_anchor_counter); |
| 187 | m5_put64(out + 112, m->migration_counter); |
| 188 | m5_put64(out + 120, m->owner_hierarchy_generation); |
| 189 | memcpy(out + 128, m->source_commit_digest, 32); |
| 190 | m5_mac(k_root_auth, MIG_DOMAIN, sizeof(MIG_DOMAIN), out, MIG_MAC_OFF, out + MIG_MAC_OFF); |
| 191 | return M5_OK; |
| 192 | } |
| 193 | |
| 194 | int m5_migration_authorize(const uint8_t *b, size_t len, const uint8_t k_root_auth[32], |
| 195 | uint8_t mode, const m5_migration *e, |
| 196 | uint64_t last_migration_counter, m5_migration *m) |
| 197 | { |
| 198 | if (!b || !k_root_auth || !e || !m) return M5_ERR_ARG; |
| 199 | memset(m, 0, sizeof *m); |
| 200 | if (len != M5_MIGRATION_LEN) return M5_ERR_BOUNDS; |
| 201 | if (memcmp(b, "AIENMIG1", 8) != 0 || m5_get16(b + 8) != 2 || m5_get16(b + 10) != 0) |
| 202 | return M5_ERR_FORMAT; |
| 203 | if (!m5_all_zero(b + 13, 3) || !m5_valid_class(b[12])) return M5_ERR_FORMAT; |
| 204 | uint8_t mac[32]; |
| 205 | m5_mac(k_root_auth, MIG_DOMAIN, sizeof(MIG_DOMAIN), b, MIG_MAC_OFF, mac); |
| 206 | if (!aienos_ct_equal(mac, b + MIG_MAC_OFF, 32)) return M5_ERR_AUTH; /* GUARD:mig-mac */ |
| 207 | if (b[12] != mode || e->identity_class != mode) return M5_ERR_IDENTITY; /* GUARD:mig-class */ |
| 208 | m->identity_class = b[12]; |
| 209 | memcpy(m->agent_root_id, b + 16, 32); |
| 210 | memcpy(m->genesis_store_uuid, b + 48, 16); |
| 211 | memcpy(m->source_store_uuid, b + 64, 16); |
| 212 | memcpy(m->dest_store_uuid, b + 80, 16); |
| 213 | m->source_store_generation = m5_get64(b + 96); |
| 214 | m->source_anchor_counter = m5_get64(b + 104); |
| 215 | m->migration_counter = m5_get64(b + 112); |
| 216 | m->owner_hierarchy_generation = m5_get64(b + 120); |
| 217 | memcpy(m->source_commit_digest, b + 128, 32); |
| 218 | int rc = M5_OK; |
| 219 | if (rc == M5_OK && memcmp(m->source_store_uuid, m->dest_store_uuid, 16) == 0) rc = M5_ERR_BINDING; /* GUARD:mig-self */ |
| 220 | if (rc == M5_OK && memcmp(m->source_store_uuid, e->source_store_uuid, 16) != 0) rc = M5_ERR_BINDING; /* GUARD:mig-source */ |
| 221 | if (rc == M5_OK && memcmp(m->dest_store_uuid, e->dest_store_uuid, 16) != 0) rc = M5_ERR_BINDING; /* GUARD:mig-dest */ |
| 222 | if (rc == M5_OK && memcmp(m->agent_root_id, e->agent_root_id, 32) != 0) rc = M5_ERR_BINDING; |
| 223 | if (rc == M5_OK && memcmp(m->genesis_store_uuid, e->genesis_store_uuid, 16) != 0) rc = M5_ERR_BINDING; |
| 224 | if (rc == M5_OK && m->source_store_generation != e->source_store_generation) rc = M5_ERR_BINDING; /* GUARD:mig-gen */ |
| 225 | if (rc == M5_OK && m->source_anchor_counter != e->source_anchor_counter) rc = M5_ERR_BINDING; /* GUARD:mig-anchor */ |
| 226 | if (rc == M5_OK && m->owner_hierarchy_generation != e->owner_hierarchy_generation) rc = M5_ERR_BINDING; |
| 227 | if (rc == M5_OK && !aienos_ct_equal(m->source_commit_digest, e->source_commit_digest, 32)) rc = M5_ERR_BINDING; /* GUARD:mig-digest */ |
| 228 | if (rc == M5_OK && m->migration_counter <= last_migration_counter) rc = M5_ERR_REPLAY; /* GUARD:mig-replay */ |
| 229 | if (rc != M5_OK) memset(m, 0, sizeof *m); |
| 230 | return rc; |
| 231 | } |
| 232 | |
| 233 | /* ---- deterministic recovery ---- */ |
| 234 | |
| 235 | int m5_recover_object(const uint8_t k_root_auth[32], const uint8_t domain_key[32], |
| 236 | uint8_t mode, const uint8_t store_uuid[16], |
| 237 | const uint8_t *commit_buf, size_t commit_len, |
| 238 | const uint8_t *anchor_buf, size_t anchor_len, |
| 239 | const m5_candidate *cands, size_t n_cands, |
| 240 | uint8_t *out, size_t out_cap, size_t *out_len, size_t *chosen) |
| 241 | { |
| 242 | if (out && out_cap) aienos_wipe(out, out_cap); |
| 243 | if (out_len) *out_len = 0; |
| 244 | if (chosen) *chosen = SIZE_MAX; |
| 245 | if (!k_root_auth || !domain_key || !store_uuid || !commit_buf || !out_len || !chosen || |
| 246 | (!cands && n_cands) || (!out && out_cap)) |
| 247 | return M5_ERR_ARG; |
| 248 | |
| 249 | m5_commit c; |
| 250 | int rc = m5_commit_open(commit_buf, commit_len, k_root_auth, mode, &c); |
| 251 | if (rc != M5_OK) return rc; |
| 252 | if (memcmp(c.obj.store_uuid, store_uuid, 16) != 0) return M5_ERR_BINDING; /* GUARD:rec-store */ |
| 253 | |
| 254 | m5_disk_state d; |
| 255 | d.store_generation = c.store_generation; |
| 256 | d.key_generation = c.obj.key_generation; |
| 257 | d.counter = c.counter; |
| 258 | sha256_hash(commit_buf, M5_COMMIT_LEN, d.commit_digest); |
| 259 | rc = m5_evaluate_anti_rollback(k_root_auth, mode, store_uuid, anchor_buf, anchor_len, 0, &d); |
| 260 | if (rc < 0) return rc; |
| 261 | |
| 262 | /* The commit record is the only authority. Scan every candidate (order |
| 263 | * independent): exact digest matches are the committed version; any other |
| 264 | * copy that claims the committed object version is a conflict. */ |
| 265 | size_t first = SIZE_MAX; |
| 266 | int conflict = 0; |
| 267 | for (size_t i = 0; i < n_cands; i++) { |
| 268 | if (!cands[i].bytes) continue; |
| 269 | uint8_t dg[32]; |
| 270 | sha256_hash(cands[i].bytes, cands[i].len, dg); |
| 271 | if (aienos_ct_equal(dg, c.object_id, 32)) { |
| 272 | if (first == SIZE_MAX) first = i; |
| 273 | continue; |
| 274 | } |
| 275 | /* Raw header fields only: a torn copy still names the object it claims. */ |
| 276 | if (cands[i].len >= 60 && memcmp(cands[i].bytes, "AIENENV1", 8) == 0 && |
| 277 | memcmp(cands[i].bytes + 28, c.obj.envelope_id, 16) == 0 && |
| 278 | m5_get64(cands[i].bytes + 52) == c.obj.key_generation) |
| 279 | conflict = 1; |
| 280 | } |
| 281 | if (first == SIZE_MAX) return M5_ERR_TORN; /* GUARD:rec-torn */ |
| 282 | if (conflict) return M5_ERR_AMBIGUOUS; /* GUARD:rec-conflict */ |
| 283 | |
| 284 | rc = m5_envelope_open(domain_key, mode, &c.obj, cands[first].bytes, cands[first].len, |
| 285 | out, out_cap, out_len); |
| 286 | if (rc != M5_OK) return rc; |
| 287 | *chosen = first; |
| 288 | return M5_OK; |
| 289 | } |