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/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.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 / docs / adr / 0017-m5-key-hierarchy-and-encrypted-store.md` 275 lines · 19229 bytes Raw Blame History Source # ADR 0017: M5 Key Hierarchy, Encrypted Envelopes, and Storage Security Roots Status: Proposed. Normative specification for Milestone M5 (Encryption and Identity). Related: ADR 0006 (Recovery Core), ADR 0007 (Provisioning Once), ADR 0015 (System Store v1), ADR 0016 (Continuity Objects), TRUST-1-IMPLEMENTATION-PLAN.md . ## 1. Context Milestone M4 established persistent agent continuity across cold restarts in QEMU over System Store v1 (ADR 0015 , ADR 0016 ) and qualified deterministic Recovery Core inspection and offline repair (ADR 0006 ). However, Store v1 provides only structural crash integrity over raw NVMe; all application payloads currently reside in plaintext on disk, and Store v1 explicitly disclaims anti-rollback protection against a malicious raw-disk adversary. Milestone M5 establishes the cryptographic security architecture of AIENOS: - Confidentiality at rest: Agent state, Cortex memories, checkpoints, credentials, and artifacts must be unreadable without authorization. - Hardware-sealed unattended boot: Unattended boots unseal storage keys strictly when firmware and kernel measurements satisfy an authorized TPM 2.0 policy. - Attended break-glass recovery: An offline operator must retain cryptographic recovery capability without compromising unattended boot security or relying on vendor backdoors. - Hardware anti-rollback: Replay of a valid earlier raw disk image must be detected and halted. - Preservation of recovery invariants: Storage encryption must never prevent unkeyed Recovery Core diagnosis of a degraded or corrupted physical store. ## 2. Decision ### 2.1 The Separation of Proofs AIENOS strictly decouples physical consistency, keyed authorization, and hardware freshness into three independent verification stages: ``` 1. Store v1 Structural Validation (Unkeyed) "These on-disk bytes form a self-consistent Store v1 transaction history." Evaluates: Dual superblocks, active generation, CommitRecords, Catalogs, CRC32C checksums. Guarantees: Crash safety, tear closure, degraded peer recovery. ↓ 2. M5 Keyed Authentication (Requires K_vol) "This Store history was committed by an authorized holder of K_vol." Evaluates: KeySlotManifest (kind 21) unwrap, SecurityManifest (kind 22) HMAC-SHA256 root tag, AES-256-GCM-SIV per-object chunk authentication. Guarantees: Authenticity and confidentiality of application payloads. ↓ 3. Anti-Rollback Freshness Validation (Requires AntiRollbackSource) "This authorized Store history is not an older replay." Evaluates: Hardware off-disk anchor {store_uuid, epoch, security_root_digest}. Guarantees: Replay protection bounded to the last anchored security epoch. ``` Store v1 superblocks, CommitRecords, Catalogs, CRC32C, and generation semantics remain 100% frozen and unmutated as defined in ADR 0015 . ### 2.2 Key Hierarchy & Separation of Authority - Volume Master Secret (`K_vol`): - An independent, high-entropy 256-bit random secret generated once during initial store provisioning. - `K_vol` is never written to persistent media in plaintext. - Keyslots: - `Slot 0 (Automated TPM Boot)`: `K_vol` wrapped under `K_tpm_kek` via AES-256-GCM-SIV. `K_tpm_kek` is sealed to the TPM 2.0 Storage Root Key (SRK) under an authorized PCR policy. - `Slot 1 (Operator Break-Glass)`: `K_vol` wrapped under `K_recovery_kek` via AES-256-GCM-SIV. `K_recovery_kek` is derived from an offline recovery secret or passphrase. - Subkey Derivation via HKDF-SHA256: All operational keys derive deterministically from `K_vol` with strict domain separation: ``` K_cortex = HKDF-Expand(K_vol, "AIENOS/M5/CORTEX-V1", 32) K_agent = HKDF-Expand(K_vol, "AIENOS/M5/AGENT-STATE-V1", 32) K_artifact = HKDF-Expand(K_vol, "AIENOS/M5/ARTIFACT-V1", 32) K_root_auth = HKDF-Expand(K_vol, "AIENOS/M5/ROOT-AUTH-V1", 32) ``` - Three Independent Cryptographic Roles: - `K_operator_auth`: Proves operator authority to execute Recovery Core commands (ADR 0006 ). - `K_recovery`: Cryptographic break-glass secret used to derive `K_recovery_kek` and unwrap storage. - `K_vol`: Internal volume master key protecting encrypted Store objects. Possessing one credential never grants automatic possession of the others. ### 2.3 Cipher Suite: AES-256-GCM-SIV Chunked Envelopes To prevent catastrophic confidentiality failure under power loss, retries, cold boots, and branch-native execution incarnations, AIENOS standardizes on AES-256-GCM-SIV (RFC 8452) as its primary at-rest AEAD suite. #### Envelope Framing Each encrypted object payload is wrapped in a chunked envelope: ``` Offset Size Field 0 8 magic = "AIENENV1" 8 2 envelope_version = 1 10 2 cipher_suite = 0x0001 (AES_256_GCM_SIV) 12 4 flags = 0 16 4 chunk_size = 65536 (64 KiB) 20 8 total_plaintext_len (u64le) 28 16 envelope_id (128-bit random identifier) 44 8 nonce_prefix (64-bit random prefix) 52 8 key_epoch (u64le) 60 4 reserved = 0 ``` Followed sequentially by $N$ encrypted chunks: ``` [Chunk 0 Ciphertext][Chunk 0 Tag: 16 B] ... [Chunk N Ciphertext][Chunk N Tag: 16 B] ``` #### Deterministic Nonce Derivation For each chunk index $i \in [0, N]$: $$\text{nonce}_i = \text{nonce_prefix}[8] \parallel i\text{:u32le}$$ #### Per-Chunk Additional Authenticated Data (AAD) To prevent cross-object splicing, chunk reordering, truncation, and cross-store replay: $$\text{AAD}_i = \text{"AIENOS-M5-CHUNK-V1\0"} \parallel \text{store_uuid} \parallel \text{object_kind:u16le} \parallel \text{object_version:u16le} \parallel \text{header_64B} \parallel i\text{:u32le} \parallel \text{chunk_plaintext_len}_i\text{:u32le}$$ Store v1 content-addresses the object by computing its standard `ObjectId = SHA-256(complete_ciphertext_envelope)`. Security Invariant: The kernel must verify each 64 KiB chunk's 16-byte authentication tag in a private scratchpad before releasing decrypted plaintext to callers. The scratchpad is securely zeroed on tag verification failure. ### 2.4 KeySlotManifest (`kind = 21`) `KeySlotManifest` is a plaintext Store v1 system object that bootstraps storage decryption. It is not encrypted under `K_vol`. ``` Offset Size Field 0 8 magic = "AIENKSL1" 8 2 format_version = 1 10 2 flags = 0 12 16 store_uuid 28 8 manifest_sequence (u64le) 36 32 previous_keyslot_manifest_id (ObjectId, 0 for genesis) 68 8 active_key_epoch (u64le) 76 4 slot_count (u32le, max 4) 80 48 reserved = 0 128 4×128 KeySlotDescriptors (512 B total) 640 var Payload Arena (max 32 KiB) ``` Each 128-byte `KeySlotDescriptor`: ``` slot_type (u8): 0=Empty, 1=TPM2_PolicyAuthorize, 2=Recovery_Argon2id, 3=Recovery_RawSecret wrap_suite (u8): 0x01 = AES_256_GCM_SIV kdf_suite (u8): 0x00 = None, 0x01 = KDF_ARGON2ID_V1, 0x02 = HKDF_SHA256 flags (u8) slot_id (u32le) key_epoch (u64le) salt (16 B) argon_m_kib (u32le) argon_t_cost (u32le) argon_p_cost (u32le) wrap_nonce (12 B) wrapped_k_vol (32 B) wrap_tag (16 B) payload_offset (u32le, relative to start of Payload Arena) payload_length (u32le) reserved (16 B) ``` #### Recovery KDF Profiles - Passphrase Profile (`KDF_ARGON2ID_V1`): - $m = 65536\text{ KiB}$ (64 MiB), $t = 3$, $p = 4$, salt = 16 bytes, output = 32 bytes (RFC 9106). - Executed inside a reserved Recovery Core memory arena; memory is explicitly zeroed immediately after derivation. - Decoders strictly validate that $m \le 131072\text{ KiB}$ and $t \le 10$ to prevent denial-of-service. - High-Entropy / WebAuthn PRF Profile: $$\text{K_recovery_kek} = \text{HKDF-SHA256}(\text{salt} = \text{slot.salt}, \text{ikm} = \text{raw_secret}, \text{info} = \text{"AIENOS/M5/RECOVERY-KEK-V1"}, \text{len} = 32)$$ #### 2.4.1 Implementation note (C kernel, P04 cut 1, 2026-10-12) - The manifest is committed as a plaintext kind-21 object inside a sealed transaction (`native/store/store_sealed.c`): the keyed transaction record binds its SHA-256, exactly one manifest may be bound at a time, and an unkeyed bootstrap read (`ss_keyslot_bootstrap`) returns the bytes before any key exists. The keyed open re-checks the same bytes. A manifest that is present but unclaimed, duplicated, or whose digest changed is refused. - Codec: `native/m5` `m5_ksm_encode` / `m5_ksm_decode` (fixed 640 bytes + arena; every descriptor through `m5_keyslot_decode`; unused descriptors must be Empty). - Recovery KEK (raw-secret profile): `m5_recovery_kek` = HKDF-SHA256(salt = slot.salt, ikm = 32-byte secret, info = `AIENOS/M5/RECOVERY-KEK-V1`, 32). HKDF-Extract is `HMAC(salt || 0^16, ikm)`, bit-identical to RFC 5869 for a 16-byte salt (test vector in `m5_test.c`). - Keyslot AAD in C binds `store_uuid, slot_id, slot_type, key_epoch` (`AIENOS-M5-KEYSLOT-V2`), a superset of the Rust reference; C is authoritative. - The kernel (`ck_store_unlock_boot`) creates the manifest at genesis in the same first transaction as the boot record (index 0 reserved Empty for the TPM slot, index 1 = Recovery_RawSecret, slot_id 1, `active_key_epoch` 1), and derives PRODUCTION-class subkeys with owner hierarchy generation = `active_key_epoch`. - Not yet: TPM Slot 0 (BLOCKED_HARDWARE), Argon2id profile. The `keyslot_manifest_id` binding into the SecurityManifest is P04 cut 2a (section 2.5.1); recovery secret rotation is P04 cut 2b (section 2.7.1). ### 2.5 SecurityManifest (`kind = 22`) `SecurityManifest` is a 256-byte fixed-size Store v1 object that authenticates authorized logical state under `K_root_auth`: ``` Offset Size Field 0..8 8 magic = "AIENSEC1" 8..10 2 format_version = 1 10..12 2 flags = 0 12..16 4 reserved = 0 16..32 16 store_uuid 32..40 8 generation (u64le, equals Store superblock generation) 40..48 8 security_sequence (u64le, strictly monotonic) 48..56 8 epoch (u64le, anti-rollback security epoch) 56..64 8 key_epoch (u64le) 64..96 32 previous_security_manifest_id (ObjectId, 0 for first) 96..128 32 keyslot_manifest_id (ObjectId) 128..160 32 agent_root_id (ObjectId) 160..192 32 continuity_manifest_id (ObjectId) 192..224 32 migration_manifest_id (ObjectId, 0 if unmigrated) 224..256 32 root_mac (HMAC-SHA256) ``` $$\text{root_mac} = \text{HMAC-SHA256}(K_{\text{root_auth}}, \text{"AIENOS-M5-ROOT-AUTH-V1\0"} \parallel \text{bytes}[0..224])$$ To break the content-addressing dependency cycle, `SecurityManifest` intentionally omits the current generation's `commit_record_id`. The Store transaction engine links `SecurityManifest` into the Catalog, whose root is referenced by the CommitRecord. #### 2.5.1 Implementation note (C kernel, P04 cut 2a, 2026-10-12) - Codec: `native/m5` `m5_secman_seal` / `m5_secman_open` (256 bytes, Rust v1 layout, `root_mac` under `k_root_auth`; identity class and owner hierarchy generation are bound through the key, not wire fields). - The kernel boot stage (`native/kernel/svc/store_boot.c`, `run_policy_ex`) commits one SecurityManifest in every boot transaction, next to the boot record (and the KeySlotManifest at genesis). In the C sealed store it is a kind-22 sealed envelope (the store admits only the KeySlotManifest in plaintext), so the transaction record binds it and `root_mac` authenticates it a second time under `k_root_auth`. - Fields as written: `generation` = the generation the transaction creates; `security_sequence` = previous + 1 (1 at genesis); `previous_security_manifest_id` = the Store ObjectId of the manifest it supersedes (zero at genesis); `key_epoch` = the owner hierarchy generation of the open keys (`active_key_epoch`, 1); `keyslot_manifest_id` = the KeySlotManifest ObjectId (`sv1_object_id` of the kind-21 bytes at genesis, the keyed open's bound `keyslot_sid` later; zero on a TEST-key Store, which has none); `epoch` = `generation`, because the C sealed store's anti-rollback anchor (`torn_slot`, section 2.6 step 3) counts Store generations; `agent_root_id`, `continuity_manifest_id`, `migration_manifest_id` = zero (not yet linked). - On every open the newest manifest is read back and must: verify under the open keys; name this Store; bind exactly the keyslot the keyed open bound; carry the open keys' epoch; carry the generation of the transaction that committed it; carry a `security_sequence` equal to the number of manifests on the Store. A Store with history but no manifest, or any failed check, is refused (`CK_SB_E_SECMAN`, "SecurityManifestRefused") before the boot record is written, and the keys are wiped. Evidence: `svc/tests/stage_test.c` `test_secman`, LIFECYCLE gate rows 182-183. - Limits: only the boot transaction writes a manifest (approve, continuity and journal commits do not advance `security_sequence`); rotation (section 2.7) and TPM Slot 0 (section 2.8) remain open. ### 2.6 Anti-Rollback Semantics & Security Epochs Theorem: To detect replay of committed state against a malicious raw-disk adversary, trusted state outside the replayable disk must advance for every protected commit boundary. - Security Epoch: The smallest group of Class-A commits that AIENOS permits to become externally authoritative before their root is anchored outside replayable storage. - Commit Ordering: ``` Step 1: Commit Store v1 generation G with epoch E+1 and SecurityManifest digest H. Step 2: Flush Store v1 durably to media (NVMe FLUSH). Step 3: Advance AntiRollbackSource to {store_uuid, E+1, H}. Step 4: Release external effects (Effect Broker dispatch, external observation). ``` - Boot Validation Protocol: - $\text{disk_epoch} == \text{anchor_epoch} \land \text{disk_digest} == \text{anchor_digest} \implies \text{Normal resume}$. - $\text{disk_epoch} == \text{anchor_epoch} + 1 \implies \text{Prepared unanchored epoch; freeze external observation, complete anchor update}$. - $\text{disk_epoch} < \text{anchor_epoch} \implies \text{Halt with RecoveryRequired::RollbackDetected}$. - $\text{disk_epoch} == \text{anchor_epoch} \land \text{disk_digest} \ne \text{anchor_digest} \implies \text{Halt with RecoveryRequired::RollbackDetected}$. - $\text{disk_epoch} > \text{anchor_epoch} + 1 \implies \text{Halt with RecoveryRequired::InconsistentEpoch}$. Replay detection halts into `RecoveryRequired::RollbackDetected`, strictly prohibiting automatic agent continuation. ### 2.7 Key Rotation vs. Store Migration - Policy / Credential Rotation ($O(1)$): Updating PCR policy or changing operator passphrases unwraps `K_vol`, writes a new `KeySlotManifest` with updated wrapped keyslots, increments `security_sequence`, and commits a new `SecurityManifest`. No encrypted application objects are re-encrypted. - `K_vol` Compromise (Out-of-Band Migration): In-place re-encryption cannot revoke confidentiality because Store v1's append-only design leaves historical ciphertext on disk. When `K_vol` is compromised: - Live state is decrypted from the source Store. - Transferred to a fresh Store container with a new `store_uuid` and new $K_{\text{vol}}'$. - An authorized `MigrationManifest` (kind = 23) is signed by the TRUST-1 offline Owner Authority. - The target Store imports the identical `AgentRoot` (`genesis_store_uuid` preserved) and resumes continuity at `migration_parent_id`. #### 2.7.1 Implementation note (C kernel, P04 cut 2b, 2026-10-12) Item 1 is implemented for the recovery secret (`native/kernel/svc/store_boot.c` `ck_store_rotate_recovery`, serial verb `rotate ` while READY): - Store v1 is append-only, so a KeySlotManifest is never replaced: manifests form a chain (`native/store/store_sealed.c`). Sequences 1..n appear exactly once, sequence 1 carries a zero `previous_keyslot_manifest_id`, every later manifest names the Store ObjectId of its predecessor, and the newest is the bound one. A transaction may add at most one manifest, only as sequence n+1 naming the bound manifest. The unkeyed bootstrap read walks the same chain and returns the newest; a broken, duplicated or forged chain is `SS_E_KEYSLOT` both unkeyed and keyed. - Rotation first unwraps `K_vol` with the current secret from the bound manifest (proof of possession; a wrong secret writes nothing). One sealed transaction then commits the next KeySlotManifest (same `K_vol`, `active_key_epoch` unchanged, slot 1 re-wrapped under the new secret with a fresh salt and nonce from the entropy service) together with the SecurityManifest of section 2.5 (`security_sequence` + 1, linked to the previous one, `keyslot_manifest_id` = the new manifest). `K_vol` is wiped before the transaction is written and is never kept resident between unlock and rotation. - Afterwards the old secret fails the AES-GCM-SIV tag of the newest manifest (`RecoverySecretRejected`). Superseded manifests stay on disk and readable, as the append-only design requires; they cannot open the Store because the keyed open binds only the newest. - Not in this cut: a change of `K_vol` (item 2, out-of-band migration), TPM Slot 0 (BLOCKED_HARDWARE), the Argon2id profile, the owner-root path. Evidence: `native/kernel/GATES.md` LIFECYCLE rows 184 and 185, host `stage-test` test_rotate and `store_sealed_test` chain tests. ### 2.8 TPM 2.0 `PolicyAuthorize` Integration Slot 0 unsealing uses TPM 2.0 `PolicyAuthorize` to allow firmware and kernel measurements to evolve without invalidating the sealed key: - Dedicated Policy Signing Key: The offline TRUST-1 Owner Root issues an authorized credential for a dedicated TPM Policy Authorization Key whose public area Name is pinned in Slot 0. - Offline Authorization: The operator computes the approved `PolicyPCR` digest for an update bundle, signs the policy digest with the Policy Authorization Key, and embeds the signature, approved policy digest, and `policyRef` (`"AIENOS/M5/STORE-UNLOCK/POLICY-V1" || store_uuid || slot_id`). - Runtime Verification: ``` TPM Policy Session Started ↓ TPM2_PolicyPCR(actual PCRs) ↓ TPM2_VerifySignature(keySign, approvedPolicy, signature) → checkTicket ↓ TPM2_PolicyAuthorize(approvedPolicy, policyRef, keySign, checkTicket) ↓ TPM2_Unseal(Slot 0) → K_tpm_kek ↓ AES-256-GCM-SIV unwrap K_vol ``` - Empirical PCR Policy: The exact PCR selection mask is not hard-coded; it is determined empirically by the TRUST-1 Gate 2 measurement campaign on Machine 1 hardware.