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/README.md # 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 / README.md` 174 lines ยท 13075 bytes Raw Blame History Rendered 1 # native/m5: M5 encrypted objects and security layer in C 2 3 C port (gnu11, no outside libraries, no heap/clock/I/O in the library) of the 4 M5 reference in `crates/aienos-kernel/src/crypto/envelope.rs` and 5 `crates/aienos-kernel/src/security.rs`, built on `native/crypto` 6 (AES-256-GCM-SIV, HMAC-SHA-256, constant-time compare, wipe) and 7 `native/argus/sha256.c`. Target: the M5 table in 8 `docs/TRUST-1-M5-GATE-MATRIX.md` and the Q rows of 9 `docs/M5_KEY_HIERARCHY_DESIGN_TREE.md`. 10 11 ``` 12 make test # rule tests, corruption matrix, header fuzz, owner-signed migration 13 # (AIENOS_M5_MIGRATION_SIG: PASS, M5_MIGSIG_TOOL: PASS); last line M5_NATIVE: PASS 14 make sanitize # same, and the tool test, under ASan + UBSan 15 make mutants # each GUARD-tagged refusal removed in turn; tests must fail 16 make clean 17 ``` 18 19 All tests in this folder are host-only (hosted libc test program), and nothing 20 measures time or energy. The kernel links `m5_keys.c`, `m5_envelope.c` and 21 `m5_state.c` through the sealed Store (`native/kernel/stage.mk`), so those 22 run in the QEMU Store gates and on the Raspberry Pi 5 with throwaway secrets 23 (`docs/native/PI5-4-UNLOCK-RUNS.md`). `m5_owner_sig.c` stays host-only. None 24 of this has run on the Spark. 25 26 ## What is covered 27 28 | Requirement | Code | Tests | 29 |---|---|---| 30 | Owner-controlled key hierarchy | `m5_derive_subkeys` (K_vol -> identity/owner-generation root -> K_cortex, K_agent, K_artifact, K_root_auth), `m5_derive_object_key`, `m5_keyslot_wrap/unwrap`, `m5_secman_seal/open` (root-auth MAC, `owner_hierarchy_generation` field) | `t_hkdf_rfc5869_case1`, `t_subkeys_deterministic_and_separated`, `t_object_key_binds_every_field`, `t_keyslot_wrap_unwrap`, `t_keyslot_refusals`, `t_secman_*` (incl. `t_secman_roundtrip_rust_v1_layout`) | 31 | AES-256-GCM-SIV object envelopes | `m5_envelope_seal/open/parse_header` | `t_env_roundtrip_sizes`, `t_env_rust_v1_header_and_standard_aad`, `t_env_header_bounds_each_field`, `t_env_hostile_header_fuzz` (20 000 random/edge headers, exact-size heap buffers under ASan) | 32 | All-or-nothing open | `m5_envelope_open`, `m5_recover_object` zero the whole caller buffer on every error | `t_all_or_nothing_late_chunk_failure_zeroes_output`; every refusal test asserts an all-zero buffer | 33 | Corruption matrix | | `t_corrupt_header`, `t_corrupt_first_chunk`, `t_corrupt_middle_chunk`, `t_corrupt_last_chunk`, `t_corrupt_commit_record`, `t_corrupt_rollback_anchor`, `t_corrupt_key_generation`, `t_corrupt_store_generation`, `t_commit_check_object`, `t_truncate_drop_last_chunk`, `t_extend_append_chunk`, `t_reorder_chunks`, `t_splice_cross_object` | 34 | Anti-rollback anchors | `m5_anchor_seal/open`, `m5_evaluate_anti_rollback` | `t_anchor_caller_buffer_roundtrip`, `t_rb_*` | 35 | Production/test identity separation | identity class in the hierarchy key (so in every object key and K_root_auth MAC), plus an explicit class byte in commit record, anchor and migration; open/verify take a mode | `t_identity_production_refuses_test`, `t_identity_test_refuses_production`, `t_identity_relabel_breaks_mac` | 36 | Migration authorization (MAC-bound) | `m5_migration_seal/authorize` | `t_mig_*` | 37 | Migration authorization (owner-signed, Ed25519) | `m5_owner_migration_body/sign_body/sign/verify`, `m5_envelope_set_digest`, `m5_migration_authorize_owner`; CLI `tools/m5_migsig.c` | `tests/m5_migsig_test.c` (13 tests, `AIENOS_M5_MIGRATION_SIG: PASS`), `tests/m5_migsig_tool_test.sh` (`M5_MIGSIG_TOOL: PASS`) | 38 | Deterministic recovery (object level) | `m5_recover_object` | `t_recovery_*` | 39 40 ## Formats and where they differ from the Rust reference 41 42 All integers little-endian, all reserved bytes must be zero, every MAC is 43 HMAC-SHA-256 under K_root_auth over `domain || bytes`, compared in constant time. 44 45 - **Subkeys.** `m5_derive_subkeys_v1` is byte-compatible with Rust 46 `derive_subkeys` (HKDF-Expand of K_vol with the same labels). The M5 path, 47 `m5_derive_subkeys`, first derives 48 `K_class = HKDF-Expand(K_vol, "AIENOS/M5/OWNER-HIERARCHY-V2\0" || class || owner_hierarchy_generation)` 49 and applies the v1 labels to K_class. Rust has no identity class or owner 50 generation. 51 - **Per-object keys** (C only; Rust `seal_envelope` takes the key directly): 52 `HKDF-Expand(domain subkey, "AIENOS/M5/OBJECT-KEY-V2\0" || store_uuid || kind || version || envelope_id || key_generation || class)`. 53 No ObjectId and no Store generation, per AGENTS.md section 3. 54 - **Keyslot descriptor**: same 128-byte layout and decode bounds as Rust. 55 The wrap AAD is stronger: `"AIENOS-M5-KEYSLOT-V2\0" || store_uuid || slot_id || slot_type || key_epoch` 56 (Rust: `slot_id` only). Decode and unwrap also refuse a wrap_suite other than 57 0x01 (Empty may hold 0), kdf_suite above 0x02 and any flag bit (ADR 0017; 58 Rust does not check these). KeySlotManifest (kind 21) is ported: 59 `m5_ksm_encode` / `m5_ksm_decode` (640-byte fixed part, `t_ksm_roundtrip`, 60 `t_ksm_refusals`). The manifest chain is checked in 61 `native/store/store_sealed.c`, not in this library. 62 - **SecurityManifest**: byte-compatible with Rust kind-22 v1 (`AIENSEC1`, 63 version 1, 256 bytes, fields 16..224, MAC at 224 with domain 64 `"AIENOS-M5-ROOT-AUTH-V1\0"`; `t_secman_roundtrip_rust_v1_layout` checks the 65 layout and recomputes the MAC by hand). Decision: a separate `AIENSEC2` is 66 not needed, because the two M5 additions (identity class and owner hierarchy 67 generation) are already bound by the key: K_root_auth is derived from 68 `class || owner_hierarchy_generation`, so a manifest from the other class or 69 another owner generation fails its MAC (`t_identity_*`). 70 - **Envelope**: byte-compatible with Rust V1. 64-byte header (`AIENENV1`, 71 version 1, suite 1, flags 0, chunk_size at 16, total at 20, envelope_id at 72 28, nonce_prefix at 44, key_epoch = key generation at 52, reserved at 60), 73 nonce `nonce_prefix || chunk_index`, chunk count derived from length and 74 chunk size. Chunk AAD is exactly the AGENTS.md Standard Envelope AAD (111 75 bytes, `t_env_rust_v1_header_and_standard_aad`): 76 `"AIENOS-M5-CHUNK-V1\0" || store_uuid || kind u16 || version u16 || 64-byte header || chunk_index u32 || chunk_len u32`. 77 The whole header (total length, envelope id, key generation) sits in every 78 chunk's AAD and the key is per object, so reorder, truncate, extend, header 79 edits and splices across objects, kinds, stores, classes or key generations 80 fail. Bounds before use: chunk size 1..65536, plaintext <= 64 MiB, at most 81 4096 chunks (C-only cap; Rust has none), total length must match exactly. 82 Like Rust, each chunk is decrypted into a private 64 KiB stack scratchpad 83 and copied out only after its tag verifies; on any failure the whole caller 84 buffer is wiped, so a verified prefix never survives as partial success. 85 - **Commit record** (C only, `AIENCMT1` version 2, 144 bytes): binds, after 86 sealing, the object binding (class, kind, version, store, envelope id, key 87 generation), the **Store generation**, counter, object sequence and the 88 **ObjectId = SHA-256 of the encrypted bytes**, MAC 89 `"AIENOS-M5-COMMIT-V2\0"`. `m5_commit_check_object` refuses bytes whose 90 digest differs (TORN) or whose header names another envelope id or key 91 generation (BINDING). Store and key generation corruption is refused here 92 and by the anchor (`t_corrupt_key_generation`, `t_corrupt_store_generation`, 93 `t_commit_check_object`). An immutable envelope can be referenced by a 94 commit of a later Store generation without re-encryption. 95 - **Rollback anchor** (120 bytes, `AIENRBA1`): store generation, key 96 generation, monotonic counter, SHA-256 of the commit record, MAC 97 (`"AIENOS-M5-ANCHOR-V1\0"`). Rust `RollbackAnchor` was an unauthenticated 98 in-memory struct keyed on epoch only. `m5_evaluate_anti_rollback` refuses a 99 forged anchor, an older store or key generation, a lower counter, a fork at 100 the anchored counter, and a counter more than one ahead; a missing anchor is 101 a rollback unless the caller explicitly allows genesis. 102 - **MigrationManifest**: version 2, 192 bytes, magic `AIENMIG1`. Binds 103 identity class, agent root, genesis/source/destination store, source store 104 generation, source anchor counter, migration counter, owner hierarchy 105 generation and source commit digest under the source K_root_auth 106 (`"AIENOS-M5-MIGRATION-V2\0"`). This is a post-seal attestation, so the 107 store generation in it does not conflict with AGENTS.md section 3. Refuses 108 wrong key, any binding mismatch, source == destination, and a counter not 109 above the last accepted one. The Rust 64-byte `offline_signature` field is 110 dropped; the owner signature is the separate record below. 111 - **Owner-signed migration record** (C only, `AIENOMG1` version 1, 272 112 bytes, layout in `m5.h`): identity class, owner key id (SHA-256 of the 113 owner public key), agent root, source and destination store, source store 114 generation, store format version, envelope count and envelope-set digest 115 (`SHA-256("AIENOS-M5-ENVSET-V1\0" || n || ObjectIds in strictly ascending 116 order)`), SHA-256 of the MAC'd migration manifest, migration counter, owner 117 hierarchy generation, then a pure Ed25519 signature (native/sig) over 118 `"AIENOS-M5-OWNER-MIGRATION-V1\0" || bytes[0..208]`. Verification refuses 119 a wrong length (BOUNDS), bad magic/version/flags/reserved/class (FORMAT, 120 even under a valid signature), another class (IDENTITY), another key, a 121 key id that does not match the trusted key, an unsigned or tampered record 122 (AUTH), any field the verifier does not expect or source == destination 123 (BINDING), and a counter not above the last accepted one (REPLAY). 124 `m5_migration_authorize_owner` requires both records: the manifest MAC 125 under the source K_root_auth and an owner record naming that exact 126 manifest digest and the same counter, stores, agent root and generations. 127 - **`tools/m5_migsig`** (hosted CLI): `pubkey SECRET_KEY_FILE PUBLIC_KEY_OUT`, 128 `body SPEC_FILE PUBLIC_KEY_FILE BODY_OUT` (the spec is key=value), 129 `sign SECRET_KEY_FILE BODY_FILE RECORD_OUT`, `verify PUBLIC_KEY_FILE 130 RECORD_FILE SPEC_FILE LAST_COUNTER`. It never creates a key. The secret key is read only 131 from the given path (32 raw bytes, 64 hex digits or unencrypted Ed25519 132 PKCS#8 DER, mode 0600 or tighter), so the real signature can be made only 133 in the offline Gate 3 ceremony, for example after 134 `openssl pkey -in owner_root.pem -outform DER -out `. 135 Tests use freshly generated random keys named `TEST-ONLY-*` and delete 136 them; the tool test also cross-checks the public key and the signature 137 against openssl when it is installed (an outside tool used only as a test 138 oracle, never linked). 139 140 ## Host-only, and what remains 141 142 - **Anchor persistence** is a caller-provided buffer only. On hardware it 143 must be TPM NV, which is blocked on TRUST-1 Gate 6. 144 - **Sealed volume keys**: keyslot wrap/unwrap is done in software with a 145 caller KEK. Real TPM sealing of K_vol (PolicyAuthorize) is TRUST-1 Gate 6 146 (policy from Gate 5). 147 - **Owner-signature binding: host-tested with TEST keys only; the real 148 owner signature is BLOCKED_OPERATOR on the Gate 3 offline key ceremony.** 149 The signature primitive is `native/sig` (in-house Ed25519, #188). The 150 record and the tool exist and are tested. An owner key was created on a 151 YubiKey on 2026-10-14 for the Pi 5 work (`docs/native/PI5-8-OWNER-CEREMONY.md`, 152 run online by owner decision); that document does not say it is the Gate 3 153 ceremony. No migration record has been signed with an owner key, and nothing 154 in the Store or boot path calls `m5_migration_authorize_owner` (callers are 155 the host tests in `native/m5`). Hardware-staging images bake an owner public 156 key from `CK_OWNER_PUBKEYS` (`native/kernel/svc/store_boot.c`, 157 `docs/native/PI5-8-OWNER-CEREMONY.md`); that key is not passed to 158 `m5_migration_authorize_owner`. 159 - **Binding to the Gate 3 Owner Root**: the hierarchy carries an owner 160 hierarchy generation, but nothing ties K_vol to the Gate 3 Owner Root yet. 161 - **Store integration** lives outside this library: recovery here works on a 162 commit record, an anchor and in-memory candidate envelopes; it does not read 163 or repair a disk and does not touch `native/store`. The C disk layer now exists (#190, #191) and the C sealed Store uses this library on the host, in the QEMU C kernel with TEST keys, and on the Raspberry Pi 5 with throwaway secrets (`docs/native/PI5-4-UNLOCK-RUNS.md`). The qualification row `m5_store_kernel_binding` (a real device with real keys) is still MISSING_IMPLEMENTATION (`scripts/trust1_m5_qualify.sh`). 164 - Recovery KDF (Argon2id / WebAuthn PRF) is not ported. The recovery KEK is 165 HKDF (`m5_recovery_kek`). KeySlotManifest lineage is the chain in 166 `native/store/store_sealed.c`. 167 168 ## AGENTS.md section 3 (resolved) 169 170 Chunk AAD is the V1 Standard Envelope AAD, and no ObjectId or Store generation 171 enters any chunk AAD, object key or MAC body before sealing. Store and key 172 generation are bound after sealing by the commit record and the anchor; 173 ObjectId is the SHA-256 of the encrypted bytes and is referenced from the 174 commit record.