Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/m5/m5.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / m5 / m5.h` 388 lines ยท 19450 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 /* AIENOS M5 encrypted-object envelope and security layer, in C. 18 * 19 * Port of crates/aienos-kernel/src/crypto/envelope.rs and 20 * crates/aienos-kernel/src/security.rs on top of native/crypto 21 * (AES-256-GCM-SIV, HMAC-SHA-256) and native/argus/sha256. 22 * No heap, no clock, no I/O, no randomness: nonces and buffers come from the 23 * caller. Secret scratch is wiped before returning. See README.md for where 24 * the byte formats differ from the Rust reference. 25 */ 26 #ifndef AIENOS_M5_H 27 #define AIENOS_M5_H 28 29 #include 30 #include 31 32 /* ---- return codes ---- */ 33 #define M5_OK 0 34 #define M5_ERR_ARG (-1) /* NULL pointer, bad parameter */ 35 #define M5_ERR_FORMAT (-2) /* magic/version/flags/reserved/enum invalid */ 36 #define M5_ERR_BOUNDS (-3) /* a length or count field out of bounds */ 37 #define M5_ERR_AUTH (-4) /* MAC or AEAD tag did not verify */ 38 #define M5_ERR_BINDING (-5) /* authentic but bound to another object/store/generation */ 39 #define M5_ERR_IDENTITY (-6) /* PRODUCTION/TEST identity class mismatch */ 40 #define M5_ERR_ROLLBACK (-7) /* older generation, lower counter, fork, or missing anchor */ 41 #define M5_ERR_INCONSISTENT (-8) /* counter jumped more than one ahead of the anchor */ 42 #define M5_ERR_TORN (-9) /* committed object version not present intact */ 43 #define M5_ERR_AMBIGUOUS (-10) /* conflicting copies claim the committed version */ 44 #define M5_ERR_REPLAY (-11) /* migration counter not above the last accepted one */ 45 #define M5_ERR_SPACE (-12) /* caller buffer too small */ 46 47 /* ---- identity class (production vs test) ---- */ 48 #define M5_ID_PRODUCTION 1 49 #define M5_ID_TEST 2 50 51 /* ---- HKDF-SHA-256 expand (RFC 5869 section 2.3), 32-byte PRK ---- */ 52 int m5_hkdf_expand(const uint8_t prk[32], const uint8_t *info, size_t info_len, 53 uint8_t *out, size_t out_len); 54 55 /* ---- key hierarchy ---- */ 56 typedef struct { 57 uint8_t k_cortex[32]; 58 uint8_t k_agent[32]; 59 uint8_t k_artifact[32]; 60 uint8_t k_root_auth[32]; 61 } m5_subkeys; 62 63 /* Rust-compatible derive_subkeys: HKDF-Expand(K_vol, "AIENOS/M5/-V1"). */ 64 void m5_derive_subkeys_v1(const uint8_t k_vol[32], m5_subkeys *out); 65 /* Owner hierarchy (C v2): K_class = HKDF-Expand(K_vol, "AIENOS/M5/OWNER-HIERARCHY-V2\0" 66 * || identity_class || owner_hierarchy_generation:u64le), then the v1 labels 67 * under K_class. Returns M5_ERR_ARG for an unknown identity class. */ 68 int m5_derive_subkeys(const uint8_t k_vol[32], uint8_t identity_class, 69 uint64_t owner_hierarchy_generation, m5_subkeys *out); 70 void m5_subkeys_wipe(m5_subkeys *k); 71 72 /* Pre-seal binding of one envelope. Per AGENTS.md section 3 it holds no 73 * ObjectId (SHA-256 of the sealed bytes, which does not exist yet) and no 74 * Store generation: those are bound after sealing, in the commit record. 75 * envelope_id is the 16-byte logical id carried in the envelope header; 76 * key_generation is the header key_epoch. */ 77 typedef struct { 78 uint8_t store_uuid[16]; 79 uint16_t object_kind; 80 uint16_t object_version; 81 uint8_t envelope_id[16]; 82 uint64_t key_generation; 83 uint8_t identity_class; 84 } m5_object_binding; 85 86 /* Per-object key from a domain subkey (k_cortex/k_agent/k_artifact): 87 * HKDF-Expand(domain, "AIENOS/M5/OBJECT-KEY-V2\0" || store_uuid || kind || 88 * version || envelope_id || key_generation || identity_class). */ 89 int m5_derive_object_key(const uint8_t domain_key[32], const m5_object_binding *b, 90 uint8_t out[32]); 91 92 /* ---- keyslot (128-byte descriptor, same layout as Rust KeySlotDescriptor) ---- */ 93 #define M5_KEYSLOT_LEN 128 94 #define M5_SLOT_EMPTY 0 95 #define M5_SLOT_TPM2_POLICY_AUTHORIZE 1 96 #define M5_SLOT_RECOVERY_ARGON2ID 2 97 #define M5_SLOT_RECOVERY_RAW_SECRET 3 98 #define M5_WRAP_SUITE_GCMSIV 0x01 99 100 typedef struct { 101 uint8_t slot_type, wrap_suite, kdf_suite, flags; 102 uint32_t slot_id; 103 uint64_t key_epoch; 104 uint8_t salt[16]; 105 uint32_t argon_m_kib, argon_t_cost, argon_p_cost; 106 uint8_t wrap_nonce[12]; 107 uint8_t wrapped_k_vol[32]; 108 uint8_t wrap_tag[16]; 109 uint32_t payload_offset, payload_length; 110 } m5_keyslot; 111 112 void m5_keyslot_encode(const m5_keyslot *s, uint8_t out[M5_KEYSLOT_LEN]); 113 int m5_keyslot_decode(const uint8_t *buf, size_t len, m5_keyslot *out); 114 /* Wrap K_vol under kek. AAD binds store_uuid, slot_id, slot_type, key_epoch. */ 115 int m5_keyslot_wrap(m5_keyslot *s, const uint8_t store_uuid[16], const uint8_t kek[32], 116 const uint8_t k_vol[32], const uint8_t nonce[12]); 117 /* On any failure k_vol_out is zero. */ 118 int m5_keyslot_unwrap(const m5_keyslot *s, const uint8_t store_uuid[16], 119 const uint8_t kek[32], uint8_t k_vol_out[32]); 120 121 /* ---- KeySlotManifest (kind 21, ADR 0017 section 2.4): the plaintext Store 122 * object that bootstraps K_vol. Fixed part: 128-byte header + 4 descriptors 123 * (640 bytes), then a payload arena the raw-secret profile leaves empty. 124 * Layout is byte-compatible with the Rust KeySlotManifest. ---- */ 125 #define M5_KSM_KIND 21 126 #define M5_KSM_SLOTS 4 127 #define M5_KSM_FIXED_LEN 640 128 #define M5_KSM_ARENA_MAX 32768 129 typedef struct { 130 uint8_t store_uuid[16]; 131 uint64_t manifest_sequence; 132 uint8_t previous_keyslot_manifest_id[32]; /* zero at genesis */ 133 uint64_t active_key_epoch; 134 uint32_t slot_count; /* descriptors in use, at most M5_KSM_SLOTS */ 135 m5_keyslot slots[M5_KSM_SLOTS]; 136 uint32_t arena_len; /* payload bytes after the fixed part (decode only) */ 137 } m5_ksm; 138 /* Fixed part only; the caller appends the arena bytes. */ 139 void m5_ksm_encode(const m5_ksm *m, uint8_t out[M5_KSM_FIXED_LEN]); 140 /* Bounded decode of a whole manifest (fixed part + arena). Every descriptor 141 * passes m5_keyslot_decode; payload ranges must lie inside the arena. */ 142 int m5_ksm_decode(const uint8_t *b, size_t len, m5_ksm *m); 143 /* Recovery KEK, high-entropy profile (ADR 0017 section 2.4): 144 * HKDF-SHA256(salt = slot.salt, ikm = raw secret, info = 145 * "AIENOS/M5/RECOVERY-KEK-V1", 32 bytes). On error out is zero. */ 146 #define M5_RECOVERY_KEK_INFO "AIENOS/M5/RECOVERY-KEK-V1" 147 int m5_recovery_kek(const uint8_t salt[16], const uint8_t *secret, size_t secret_len, uint8_t out[32]); 148 149 /* ---- SecurityManifest (kind 22): byte-compatible with Rust v1 ---- 150 * 256 bytes, magic "AIENSEC1", format_version 1, flags 0, reserved 0, 151 * root_mac = HMAC(k_root_auth, "AIENOS-M5-ROOT-AUTH-V1\0" || bytes[0..224]). 152 * Identity class and owner hierarchy generation are not wire fields: they are 153 * bound through k_root_auth, which m5_derive_subkeys derives from (K_vol, 154 * identity_class, owner_hierarchy_generation). A manifest sealed under one 155 * class or owner generation fails the MAC under any other. */ 156 #define M5_SECMAN_LEN 256 157 typedef struct { 158 uint8_t store_uuid[16]; 159 uint64_t generation; 160 uint64_t security_sequence; 161 uint64_t epoch; 162 uint64_t key_epoch; 163 uint8_t previous_security_manifest_id[32]; 164 uint8_t keyslot_manifest_id[32]; 165 uint8_t agent_root_id[32]; 166 uint8_t continuity_manifest_id[32]; 167 uint8_t migration_manifest_id[32]; 168 uint8_t root_mac[32]; 169 } m5_secman; 170 171 /* Compute root_mac under k_root_auth and encode. */ 172 int m5_secman_seal(m5_secman *m, const uint8_t k_root_auth[32], uint8_t out[M5_SECMAN_LEN]); 173 /* Bounded decode; the MAC must verify under k_root_auth. */ 174 int m5_secman_open(const uint8_t *buf, size_t len, const uint8_t k_root_auth[32], m5_secman *out); 175 176 /* ---- chunked AES-256-GCM-SIV envelope: Rust V1 header, 64 bytes ---- 177 * 0 magic "AIENENV1" | 8 version u16 = 1 | 10 cipher_suite u16 = 1 178 * | 12 flags u32 = 0 | 16 chunk_size u32 | 20 total_plaintext_len u64 179 * | 28 envelope_id[16] | 44 nonce_prefix[8] | 52 key_epoch u64 | 60 reserved u32 = 0 180 * Chunk AAD (AGENTS.md section 3, envelope.rs compute_chunk_aad, 111 bytes): 181 * "AIENOS-M5-CHUNK-V1\0" || store_uuid || kind u16 || version u16 || header[64] 182 * || chunk_index u32 || chunk_plaintext_length u32. Nonce: prefix[8] || index u32. */ 183 #define M5_ENV_HEADER_LEN 64 184 #define M5_ENV_MAX_CHUNK 65536u 185 #define M5_ENV_MAX_CHUNKS 4096u /* C-only cap; Rust seals 64 KiB chunks, <= 1024 */ 186 #define M5_ENV_MAX_PLAINTEXT (UINT64_C(64) * 1024 * 1024) 187 #define M5_TAG_LEN 16 188 189 typedef struct { 190 uint16_t version; 191 uint16_t cipher_suite; 192 uint32_t flags; 193 uint32_t chunk_size; 194 uint64_t total_plaintext_len; 195 uint8_t envelope_id[16]; 196 uint8_t nonce_prefix[8]; 197 uint64_t key_epoch; 198 uint32_t chunk_count; /* derived, not a wire field */ 199 } m5_env_header; 200 201 /* Envelope length for pt_len bytes in chunk_size chunks, 0 if out of bounds. */ 202 size_t m5_envelope_len(uint64_t pt_len, uint32_t chunk_size); 203 /* Bounded header parse: every length field is checked before use and 204 * env_len must equal the length the header implies. */ 205 int m5_envelope_parse_header(const uint8_t *env, size_t env_len, m5_env_header *h); 206 int m5_envelope_seal(const uint8_t domain_key[32], const m5_object_binding *b, 207 const uint8_t nonce_prefix[8], uint32_t chunk_size, 208 const uint8_t *pt, size_t pt_len, 209 uint8_t *out, size_t out_cap, size_t *out_len); 210 /* All-or-nothing: on any error out[0..out_cap) is all zero and *out_len is 0. */ 211 int m5_envelope_open(const uint8_t domain_key[32], uint8_t mode, const m5_object_binding *expect, 212 const uint8_t *env, size_t env_len, 213 uint8_t *out, size_t out_cap, size_t *out_len); 214 215 /* ---- commit record (object level, written after sealing), 144 bytes, 216 * MAC by k_root_auth. Binds the ObjectId (SHA-256 of the sealed envelope 217 * bytes) to its store generation, key generation and security counter. ---- */ 218 #define M5_COMMIT_LEN 144 219 typedef struct { 220 m5_object_binding obj; /* store_uuid, kind, version, envelope_id, key generation, class */ 221 uint64_t store_generation; 222 uint64_t counter; /* monotonic security counter (anchor counter) */ 223 uint64_t object_sequence; 224 uint8_t object_id[32]; /* ObjectId = SHA-256 of the committed envelope bytes */ 225 } m5_commit; 226 227 int m5_commit_seal(const m5_commit *c, const uint8_t k_root_auth[32], uint8_t out[M5_COMMIT_LEN]); 228 int m5_commit_open(const uint8_t *buf, size_t len, const uint8_t k_root_auth[32], 229 uint8_t mode, m5_commit *out); 230 /* Post-seal check of envelope bytes against a verified commit record: 231 * SHA-256(env) must equal object_id (M5_ERR_TORN), and the header's 232 * envelope_id and key_epoch must equal the commit's (M5_ERR_BINDING). */ 233 int m5_commit_check_object(const m5_commit *c, const uint8_t *env, size_t env_len); 234 235 /* ---- anti-rollback anchor, 120 bytes, MAC by k_root_auth ---- 236 * Persistence is the caller's buffer only. On hardware this is TPM NV, 237 * blocked on TRUST-1 Gate 6. */ 238 #define M5_ANCHOR_LEN 120 239 typedef struct { 240 uint8_t identity_class; 241 uint8_t store_uuid[16]; 242 uint64_t store_generation; 243 uint64_t key_generation; 244 uint64_t counter; 245 uint8_t commit_digest[32]; /* SHA-256 of the commit record at this counter */ 246 } m5_anchor; 247 248 int m5_anchor_seal(const m5_anchor *a, const uint8_t k_root_auth[32], uint8_t *buf, size_t buf_len); 249 int m5_anchor_open(const uint8_t *buf, size_t len, const uint8_t k_root_auth[32], 250 uint8_t mode, m5_anchor *out); 251 252 typedef struct { 253 uint64_t store_generation; 254 uint64_t key_generation; 255 uint64_t counter; 256 uint8_t commit_digest[32]; 257 } m5_disk_state; 258 259 #define M5_RB_VALID_RESUME 1 260 #define M5_RB_PREPARED_ADVANCE 2 261 #define M5_RB_GENESIS 3 262 /* Returns a positive decision or a negative error. An empty anchor buffer is 263 * genesis only when allow_genesis is set, otherwise a rollback. */ 264 int m5_evaluate_anti_rollback(const uint8_t k_root_auth[32], uint8_t mode, 265 const uint8_t store_uuid[16], 266 const uint8_t *anchor_buf, size_t anchor_len, 267 int allow_genesis, const m5_disk_state *disk); 268 269 /* ---- migration manifest (kind 23), C v2 layout, 192 bytes, MAC by the 270 * source store's k_root_auth. The owner public-key signature is a separate 271 * record, m5_owner_migration below (native/sig Ed25519). ---- */ 272 #define M5_MIGRATION_LEN 192 273 typedef struct { 274 uint8_t identity_class; 275 uint8_t agent_root_id[32]; 276 uint8_t genesis_store_uuid[16]; 277 uint8_t source_store_uuid[16]; 278 uint8_t dest_store_uuid[16]; 279 uint64_t source_store_generation; 280 uint64_t source_anchor_counter; 281 uint64_t migration_counter; 282 uint64_t owner_hierarchy_generation; 283 uint8_t source_commit_digest[32]; 284 } m5_migration; 285 286 int m5_migration_seal(const m5_migration *m, const uint8_t k_root_auth[32], 287 uint8_t out[M5_MIGRATION_LEN]); 288 /* expect carries the source/destination/generation the verifier holds; 289 * migration_counter must be above last_migration_counter. */ 290 int m5_migration_authorize(const uint8_t *buf, size_t len, const uint8_t k_root_auth[32], 291 uint8_t mode, const m5_migration *expect, 292 uint64_t last_migration_counter, m5_migration *out); 293 294 /* ---- deterministic recovery (object/envelope level, no disk layer) ---- */ 295 typedef struct { 296 const uint8_t *bytes; 297 size_t len; 298 } m5_candidate; 299 300 /* Picks the candidate whose SHA-256 equals the verified commit record's 301 * object_id (ObjectId), after the commit is checked against the anchor. Refuses 302 * (no silent repair) when the committed version is absent or torn 303 * (M5_ERR_TORN) or when another copy claims the same object version with 304 * different bytes (M5_ERR_AMBIGUOUS). On success the plaintext is in out and 305 * *chosen is the index of the first matching candidate. All-or-nothing on out. */ 306 int m5_recover_object(const uint8_t k_root_auth[32], const uint8_t domain_key[32], 307 uint8_t mode, const uint8_t store_uuid[16], 308 const uint8_t *commit_buf, size_t commit_len, 309 const uint8_t *anchor_buf, size_t anchor_len, 310 const m5_candidate *cands, size_t n_cands, 311 uint8_t *out, size_t out_cap, size_t *out_len, size_t *chosen); 312 313 /* ---- owner-signed migration record (C v1), 272 bytes, Ed25519 by the 314 * owner key (native/sig, pure Ed25519, RFC 8032). The 192-byte migration 315 * manifest above is MAC-bound to the source store's K_root_auth, which proves 316 * the store approved the move; this record proves the OWNER approved it. 317 * Layout (little endian, reserved bytes zero): 318 * 0 "AIENOMG1" | 8 version u16 = 1 | 10 flags u16 = 0 | 12 identity_class | 13..16 reserved 319 * | 16 owner_key_id[32] = SHA-256(owner public key) 320 * | 48 agent_root_id[32] | 80 source_store_uuid[16] | 96 dest_store_uuid[16] 321 * | 112 source_store_generation u64 | 120 store_format_version u32 322 * | 124 envelope_count u32 | 128 envelope_set_digest[32] 323 * | 160 migration_manifest_digest[32] = SHA-256 of the 192-byte MAC'd manifest 324 * | 192 migration_counter u64 | 200 owner_hierarchy_generation u64 325 * | 208 signature[64] = Ed25519(owner, "AIENOS-M5-OWNER-MIGRATION-V1\0" || bytes[0..208]) 326 * An unsigned body is the same 272 bytes with an all-zero signature. ---- */ 327 #define M5_OWNER_MIG_LEN 272 328 #define M5_OWNER_MIG_BODY_LEN 208 329 typedef struct { 330 uint8_t identity_class; 331 uint8_t owner_key_id[32]; /* SHA-256 of the owner public key */ 332 uint8_t agent_root_id[32]; 333 uint8_t source_store_uuid[16]; 334 uint8_t dest_store_uuid[16]; 335 uint64_t source_store_generation; 336 uint32_t store_format_version; 337 uint32_t envelope_count; 338 uint8_t envelope_set_digest[32]; /* m5_envelope_set_digest of the migrated ObjectIds */ 339 uint8_t migration_manifest_digest[32]; 340 uint64_t migration_counter; /* monotonic; must exceed the last accepted one */ 341 uint64_t owner_hierarchy_generation; 342 } m5_owner_migration; 343 344 /* SHA-256("AIENOS-M5-ENVSET-V1\0" || n u32 || ids[0] || ... || ids[n-1]). 345 * ids must be strictly ascending (canonical set: sorted, no duplicates), 346 * else M5_ERR_FORMAT. ids may be NULL when n is 0. */ 347 int m5_envelope_set_digest(const uint8_t (*ids)[32], uint32_t n, uint8_t out[32]); 348 349 /* Unsigned body for an offline signer: m's fields, owner_key_id computed from 350 * owner_pk (m->owner_key_id is ignored), signature all zero. */ 351 int m5_owner_migration_body(const m5_owner_migration *m, const uint8_t owner_pk[32], 352 uint8_t out[M5_OWNER_MIG_LEN]); 353 /* Signs an unsigned body in place. Refuses a malformed body, a body whose 354 * signature field is not all zero (M5_ERR_FORMAT) and a key whose public key 355 * does not match owner_key_id (M5_ERR_BINDING). */ 356 int m5_owner_migration_sign_body(uint8_t rec[M5_OWNER_MIG_LEN], const uint8_t owner_sk[32]); 357 /* body + sign in one step (owner_pk derived from owner_sk). */ 358 int m5_owner_migration_sign(const m5_owner_migration *m, const uint8_t owner_sk[32], 359 uint8_t out[M5_OWNER_MIG_LEN]); 360 /* Verifies a signed record against the trusted owner public key and the 361 * verifier's expectations (every field except owner_key_id and 362 * migration_counter must equal expect's), then refuses a counter not above 363 * last_migration_counter (M5_ERR_REPLAY). Wrong length: M5_ERR_BOUNDS; bad 364 * magic/version/flags/reserved/class: M5_ERR_FORMAT; another class than 365 * mode: M5_ERR_IDENTITY; another owner key or a bad or missing signature: 366 * M5_ERR_AUTH; field mismatch or source == destination: M5_ERR_BINDING. 367 * out is all zero on every error. */ 368 int m5_owner_migration_verify(const uint8_t *buf, size_t len, const uint8_t owner_pk[32], 369 uint8_t mode, const m5_owner_migration *expect, 370 uint64_t last_migration_counter, m5_owner_migration *out); 371 /* Full migration authorization: the MAC'd manifest under the source 372 * K_root_auth (m5_migration_authorize) AND the owner signature over a record 373 * that names that exact manifest (digest) with the same identity class, 374 * agent root, source, destination, source store generation, migration 375 * counter and owner hierarchy generation. expect_own supplies the fields the 376 * manifest does not carry (store_format_version, envelope_count, 377 * envelope_set_digest); its other fields are taken from expect_mig. Both 378 * outputs are all zero on every error. */ 379 int m5_migration_authorize_owner(const uint8_t *mig_buf, size_t mig_len, 380 const uint8_t k_root_auth[32], 381 const uint8_t *own_buf, size_t own_len, 382 const uint8_t owner_pk[32], uint8_t mode, 383 const m5_migration *expect_mig, 384 const m5_owner_migration *expect_own, 385 uint64_t last_migration_counter, 386 m5_migration *out_mig, m5_owner_migration *out_own); 387 388 #endif /* AIENOS_M5_H */