aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#317 (code only)
Apply it
git checkout -b patch-1a8fea3651681d82 d86cfcfa504cbe0252da184351a1563ee1d1ac16 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/1a8fea3651681d82.patch | git am
Provenance
github-import 1
aien-dev/aienux
317
b94d6da62ffb6bf2028d18fdc991b3ebd25b1084
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":317,"url":"https://github.com/aien-dev/aienux/pull/317","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:12Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 1f1965c5..aa3bb33f 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "c5b8b0f4bc3287195f8af9d001dcacfbbcc2247ff59505c6cc8f9fd078c6baaf", - "last_commit": "b9baac8", + "digest": "9b91380635da227d4a2fb7667c961879f81445e00ec6e3784c1955e8a28dd1e6", + "last_commit": "56d125f", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "722aa527ceed838b63623647060db036571772cfd40864677e37621679baa417", - "last_commit": "b9baac8", + "digest": "9b1a3dd490ab95dd51bd1b94a314eae35800c62fba5bb05b6e13189df7420738", + "last_commit": "56d125f", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "2e11a9899bf4b8d979c08da48c4ab1610e58b4fcd6d33c190a9f9980147c4a71", - "last_commit": "b9baac8", + "digest": "8f2ba6515d449096eccaa96e1d710d5556e36425d1b82eb8ef096d85200059f9", + "last_commit": "56d125f", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:4f007f02a0d70e763ba16618d9a80840ade185d66b9156c8d8f39266c8d82dd4", - "compiled_at": "2026-10-12T09:21:41Z", + "children_root": "sha256:63a3bb1e28d23b5ae7225d2de0a34e9e3f75c14863c36a3d70c7c84ca848dc6f", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:1aec764b9fb6dfe44de533ef107ba5addbaded666a7bdbe1564407b5fc791c7c", + "digest": "sha256:4962c66d02b68960e451bc7f64f82adc0548dff726b72ee9cd15cbde3312fc3a", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", "files": 3085, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:63e4b1ba16160c6c7da976cbdd0ffda15c06a12ac93cdcdc83e68f38a3d83143" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:f11fd859fa3c14ad139fc2dbced21d5c39d4b66ff17fbe09e97662c3a2e6ab22" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index b3f5bab7..6599176c 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -17,8 +17,8 @@ "generated": { "children": [ { - "digest": "3f2495475b3364d5c9fbf79ebe0bfef1b86202f5cea6e85f931148f8101b5809", - "last_commit": "b9baac8", + "digest": "a845beb92a810b2349a8fc52048276ca6949076e4343912bbb9b70859036bf8f", + "last_commit": "56d125f", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, @@ -29,15 +29,15 @@ "purpose": "Directory docs/crumbs. Purpose not yet described by a human." } ], - "children_root": "sha256:1ccad6e28aaeef112ad4a1a3bb25117a4e0730e1796af18c611c579ced66f198", - "compiled_at": "2026-10-12T09:21:41Z", + "children_root": "sha256:baea130f2c52779b57d90139beb66ac01b21ab499d113471a334be5500f3e369", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:c5b8b0f4bc3287195f8af9d001dcacfbbcc2247ff59505c6cc8f9fd078c6baaf", + "digest": "sha256:9b91380635da227d4a2fb7667c961879f81445e00ec6e3784c1955e8a28dd1e6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 69, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:c17458ecf76cb17d6f5fabed27cb98a2bb6609ca4632e07a77b8e93eaa677c1e" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:64d2069510df3b1bc132881c896185b6448ad71c489cca801a9cfd82065788c2" }, "owner_lane": "", "provenance": { diff --git a/docs/adr/.crumb b/docs/adr/.crumb index 2282c601..953f8e46 100644 --- a/docs/adr/.crumb +++ b/docs/adr/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T09:21:41Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:3f2495475b3364d5c9fbf79ebe0bfef1b86202f5cea6e85f931148f8101b5809", + "digest": "sha256:a845beb92a810b2349a8fc52048276ca6949076e4343912bbb9b70859036bf8f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 22, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:43de31d39aa29f4eb258d1cb160b42ab5cb6076bf2ce41e9100e7c41994ab909" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:a545e3c9b4b62eeda5f39afcb71b7cdecf2da41e11413598afe8cdd0787de90f" }, "owner_lane": "", "provenance": { diff --git a/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md b/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md index 4a21888a..acd69203 100644 --- a/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md +++ b/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md @@ -165,7 +165,7 @@ reserved (16 B) - 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), key rotation (section 2.7), Argon2id profile. The `keyslot_manifest_id` binding into the SecurityManifest is P04 cut 2a (section 2.5.1). +- 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`) @@ -238,6 +238,15 @@ Replay detection halts into `RecoveryRequired::RollbackDetected`, strictly prohi - 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 <current 64-hex> <new 64-hex>` 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 diff --git a/native/.crumb b/native/.crumb index aa33fc84..bccf05c0 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "e360d366f6232a156213aa656e6b6487ed49286ba424f070b65c933fb64d4c5e", - "last_commit": "b9baac8", + "digest": "58ca40969a50080dfe0c0add70eb0d38635b6abb39418f72a0872d4e84a2da67", + "last_commit": "56d125f", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -103,21 +103,21 @@ "purpose": "Directory native/sig. Purpose not yet described by a human." }, { - "digest": "9146f4be830fe28ed14ce6bfac89cd97bd2043c825a60a00451152309e1f87ea", - "last_commit": "1feaca1", + "digest": "eeec17e754d15d5723d77d953711620c568c160f47e6c90a4c927c85ec5bb32d", + "last_commit": "56d125f", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:e926c5cbd12204494a193fa90dd78dddb55c6c0f507eefd707d3651d45a67fb3", - "compiled_at": "2026-10-12T09:21:41Z", + "children_root": "sha256:4073411743a525add34b064d8fc07e9882b7aecf69b3cde42f47b9dc84182b48", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:722aa527ceed838b63623647060db036571772cfd40864677e37621679baa417", + "digest": "sha256:9b1a3dd490ab95dd51bd1b94a314eae35800c62fba5bb05b6e13189df7420738", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 469, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:54b67a29df90dbe9074b7e226658e49b64f3ff6c1a2ad7e4a2df31bd72e1cf4f" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:3bc13822d57a405451ef88e000a5e109b64e815ef9747e64d65a431b390645a3" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 4433e4ab..9f9df203 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,8 +57,8 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "d442b49555533f077c87c907efa0fea22b5b20ef93bb1650adcfcce4d4ce50ed", - "last_commit": "10c2ea7", + "digest": "13c7a827e3ccb3c9395a86080f2a7c7540037fc7c4fb6039c80fbcc6b7413bdf", + "last_commit": "56d125f", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, @@ -81,14 +81,14 @@ "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "28548cc5bc6f6d137cfb2bbf7a3bbba09ac92ee3d0c2f34eb08772b92ac64e8b", - "last_commit": "b9baac8", + "digest": "df0e798c909b72635d6b3aaba97462da29f468723857ee4fd055110c8eb07c1d", + "last_commit": "56d125f", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "83457528f2132fa686bb428663db2585a32f3a731dddb30ea0c2295a28c45656", - "last_commit": "10c2ea7", + "digest": "c65a92ef6ebb21a41f8148c443f9e87d142734e5f08b3d0402644060ff07c4bf", + "last_commit": "56d125f", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:3bb81381da85d2c00d2ea1e9faaf1cd112557410ebe1a5e5c647300395531069", - "compiled_at": "2026-10-12T09:21:41Z", + "children_root": "sha256:3b61ea92b741789db9a3fbade47fea1d4ee7819cbee5b2abfa809b879a4ad717", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:e360d366f6232a156213aa656e6b6487ed49286ba424f070b65c933fb64d4c5e", + "digest": "sha256:58ca40969a50080dfe0c0add70eb0d38635b6abb39418f72a0872d4e84a2da67", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 305, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:3dfe809f98dfe05d9b04385b81abacbd80827e977fbd5ddc4cf4cef243bcbae2" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:ddded0ac959d45d27f08036f51b4818ba4fb70705b1d83851e278b91360adc04" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index d2eba1e4..558ff991 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -832,7 +832,7 @@ are this adapter's choices (UNVERIFIED against a frozen contract). (5) The reque bytes; the contract's exact digest input is not defined (UNVERIFIED). (6) The "task" is the adapter's session; the EL0 shell units hold no capability (caps=0) and the check is made by the kernel-side adapter. -## LIFECYCLE: resident production lifecycle (native v1 plan P03, P04 cuts 1-2a, P05 cut 1, P06 cuts 2-4; C-only) -> CK `LIFECYCLE` (scripts/qemu_ck_lifecycle_test.sh) +## LIFECYCLE: resident production lifecycle (native v1 plan P03, P04 cuts 1-2b, P05 cut 1, P06 cuts 2-4; C-only) -> CK `LIFECYCLE` (scripts/qemu_ck_lifecycle_test.sh) The product boundary. A `CK_SERVICE=1` image (`full-service/`, allowed with `CK_HARDWARE_STAGING`, refused with every TEST-only flag) links `core/lifecycle.c` + `core/lifecycle_fsm.c`: after every boot stage the kernel serves bounded operator requests on the serial console (states init, locked, ready, serve, quiesce, stop; verbs status, unlock, task <text>, prompt <text>, approve, deny, cancel, recall, evidence, help, shutdown; one 96-byte line, one 48-byte task or prompt, one 256-byte reply, one pending proposal at a time) until `shutdown`, then runs the stop order (Store closed, NVMe normal shutdown + bus master off + SMMU stream to abort, final report, reset) instead of resetting at once. The diagnostic images are unchanged (the weak `ck_lifecycle_run` is absent). The production key source still refuses (no in-kernel K_vol unwrap, P04), so the production service image comes up LOCKED. The TEST-ONLY `CK_SERVICE_TEST_KEYS=1` image (`full-service-testkeys/`, refused with `CK_HARDWARE_STAGING`) opens the Store with the TEST keys so the ready/serve/commit paths run in QEMU. Host test: `tests/test_lifecycle.c` (`make test`, 79 checks on the state machine and decoder). P06 cut 2 (rows 174-176): a service image built with `CK_INFER_LIB` keeps the model and one bounded session resident from boot to the stop order (`core/infer.c` resident mode, `CK_INFER_RESIDENT`); `prompt <text>` generates a bounded reply between whose steps only `cancel` is accepted, the reply becomes the pending proposal and `approve` commits it as one sealed reply object (kind 0x5231 'R1': `aienos.reply.v1`, the prompt and the reply); `recall` reads the newest approved note or reply back through the sealed Store, so a cold boot answers from the disk and never from memory. QEMU only: the model arrives over the TEST-ONLY fw_cfg channel and `CK_INFER_LIB` cannot combine with `CK_HARDWARE_STAGING`, so the hardware service image still has no inference (the MODEL.MAP boot-disk source is the hardware path, P06 next cut). QEMU qualifies nothing physical. @@ -844,7 +844,7 @@ The product boundary. A `CK_SERVICE=1` image (`full-service/`, allowed with `CK_ | 163 | test-keys image: TEST-ONLY banner; Store open and NVMe kept bound for the service lifetime (no bus master revoke before `lifecycle: ready state=ready store=open`) | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | | 164 | serve: `task <text>` -> `task pending ... proposed="note: <text>"`; `approve` -> `approve committed gen=N id=...` (one sealed Store object, kind 0x4e31 'N1'), state back to ready | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | | 165 | one task at a time: a second `task` while one is pending is refused; `approve` with nothing pending is refused; `deny` and `cancel` drop the pending task | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | -| 166 | bounds: a 97-byte line (limit 96, room for `unlock ` + 64 hex) is refused whole and never run; `evidence` counts agree (`tasks=3 approved=1 denied=1 cancelled=1 overlong=1`) with the last commit generation and object id | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 166 | bounds: a 145-byte line (limit 144, room for `rotate ` + two 64-hex secrets) is refused whole and never run; `evidence` counts agree (`tasks=3 approved=1 denied=1 cancelled=1 overlong=1`) with the last commit generation and object id | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | | 167 | stop order: `lifecycle: ready` < `dma_gate: nvme bus master revoked` < `stop order store=closed nvme=released` < `stage lifecycle: ok` < final report; no panic or fault; QEMU exit 0; M1 checks | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | | 167a | build shape: the default image carries no lifecycle; `CK_SERVICE` refused with `CK_CONSOLE_SESSION`, `CK_QEMU_UNSAFE_DMA` and any `CK_TEST_*`; `CK_SERVICE_TEST_KEYS` refused with `CK_HARDWARE_STAGING` | LIFECYCLE (build) | PASS | | 167b | limits: operator input is serial only (USB keyboard binding stays with the xHCI session code, later cut); a committed note or reply is read back by `recall` (row 175) but the memory-only fact of N3 (the Omega CxStore adapter record) is the next P06 cut; the lifecycle loop polls with interrupts as the boot stages left them (no scheduler integration yet) | LIFECYCLE | LIMIT (declared) | @@ -863,4 +863,6 @@ The product boundary. A `CK_SERVICE=1` image (`full-service/`, allowed with `CK_ | 180 | the same two verbs on the production Store (boot 3, recovery keyslot genesis, after the approved note at generation 4): journal header at generation 5 (`cortex open ... store_gen=5`), the fact at generation 6 (`remembered id=1 t=1 store_gen=6`), `facts records=1 shown=1`, evidence `requests=8 refused=1 ... last_commit_gen=4 ... remembers=1 facts=1 cortex=open records=1`, stop order `requests=9`; no `REFUSED verb=(remember|facts)` | LIFECYCLE | QEMU PASS (2026-10-12, tree 61f4e66: `cortex open records=0 chunks=1 journal_bytes=32 store_gen=5`, `remembered id=1 t=1 store_gen=6`, evidence `remembers=1 facts=1 cortex=open records=1`; hardware NOT_RUN) | | 181 | cold production boot on the same disk (boot 4): the Store opens at generation 6, commits the boot record at 7 and the continuity resume at 8 (rows 169/172 unchanged otherwise); `facts` alone (`cortex open records=1 chunks=2 journal_bytes=192 store_gen=8 ... chain=ok`) answers `fact id=1 t=1 store_gen=6 text="prefer short notes"` from the disk; the fact is never typed in that boot (no `> remember`, no `lifecycle: remembered`); evidence `requests=6 refused=1 ... remembers=0 facts=1 cortex=open records=1`; `cortex closed records=1 appends=0` before the stop order; a disk without a journal answers `facts none cortex=none records=0` without creating one (host FSM test: 95 checks) | LIFECYCLE | QEMU PASS (2026-10-12, tree 61f4e66: `cortex open records=1 chunks=2 journal_bytes=192 store_gen=8 chain=ok`, `fact id=1 t=1 store_gen=6`, no `remember` typed, evidence `remembers=0 facts=1`, `cortex closed appends=0 reads=1`; hardware NOT_RUN) | | 182 | P04 cut 2a, SecurityManifest (ADR 0017 section 2.5) on every boot transaction (`svc/store_boot.c` run_policy_ex: kind 22 sealed envelope, root_mac under k_root_auth; generation = the generation the transaction creates, security_sequence = previous + 1, previous_security_manifest_id = the superseded manifest, key_epoch = the open keys' owner hierarchy generation, keyslot_manifest_id = the KeySlotManifest ObjectId or zero, epoch = generation): host `test_secman` in `svc/tests/stage_test.c`: TEST-key Store sequences 1, 2, 3 over three boots with `keyslot=none`; a forged newer manifest sealed under the right keys with a sequence that is not the manifest count, another key_epoch, or a keyslot the Store does not have refuses the next boot (`CK_SB_E_SECMAN`, proof keyed, keys wiped, disk bytes unchanged); a well-formed newer manifest is accepted and the next boot writes sequence 3; the production Store binds the keyslot at genesis (`sv1_object_id` of the kind-21 bytes committed in the same transaction) and on every resume (`keyslot_sid` of the keyed open). QEMU: the TEST-key boot 2 prints `store: security manifest sequence=1 generation=2 keyslot=none previous=genesis` | LIFECYCLE (host + QEMU) | PASS (host 2026-10-12: CK_STAGE_HOST checks=817, stage-sanitize 817, stage-free objects=45; QEMU 2026-10-12 on tree 10c2ea7 + this cut: 154 rows, row 182 observed exactly; hardware NOT_RUN) | -| 183 | production Store: genesis boot 3 prints `store: security manifest sequence=1 generation=2 keyslot=bound previous=genesis`; cold resume boot 4 prints `sequence=2 generation=7 keyslot=bound previous=linked` (the manifest read back from the disk verified under K_root_auth, bound exactly the keyslot the keyed open bound, carried key_epoch 1, the generation that committed it and the manifest count); no `SecurityManifest` refusal in any boot. Limits: only the boot transaction writes a manifest (approve, continuity and journal commits do not advance security_sequence); agent_root_id, continuity_manifest_id and migration_manifest_id stay zero; TPM Slot 0 BLOCKED_HARDWARE; no key rotation; hardware NOT_RUN | LIFECYCLE | QEMU PASS (2026-10-12: 154 rows, AIENOS_CK_LIFECYCLE and AIENOS_CK_LIFECYCLE_INFER PASS; boot 3 `sequence=1 generation=2 keyslot=bound previous=genesis`, boot 4 `sequence=2 generation=7 keyslot=bound previous=linked`, no refusal line; hardware NOT_RUN) | +| 183 | production Store: genesis boot 3 prints `store: security manifest sequence=1 generation=2 keyslot=bound previous=genesis`; cold resume boot 4 prints `sequence=2 generation=7 keyslot=bound previous=linked` (the manifest read back from the disk verified under K_root_auth, bound exactly the keyslot the keyed open bound, carried key_epoch 1, the generation that committed it and the manifest count); no `SecurityManifest` refusal in any boot. Limits: only the boot transaction writes a manifest (approve, continuity and journal commits do not advance security_sequence); agent_root_id, continuity_manifest_id and migration_manifest_id stay zero; TPM Slot 0 BLOCKED_HARDWARE; key rotation is row 184; hardware NOT_RUN | LIFECYCLE | QEMU PASS (2026-10-12: 154 rows, AIENOS_CK_LIFECYCLE and AIENOS_CK_LIFECYCLE_INFER PASS; boot 3 `sequence=1 generation=2 keyslot=bound previous=genesis`, boot 4 `sequence=2 generation=7 keyslot=bound previous=linked`, no refusal line; hardware NOT_RUN) | +| 184 | P04 cut 2b, recovery secret rotation (ADR 0017 section 2.7 item 1, `rotate <current 64-hex> <new 64-hex>` while READY; `svc/store_boot.c` ck_store_rotate_recovery): the current secret must unwrap K_vol from the bound KeySlotManifest first (proof of possession; a wrong secret writes nothing, `RecoverySecretRejected`); then one Store transaction commits the next KeySlotManifest (sequence n+1 naming the bound one, same K_vol and key epoch, slot 1 re-wrapped under the new secret with a fresh salt and nonce from the entropy service) plus a SecurityManifest (security_sequence+1, linked, binding the new manifest). Store v1 is append-only, so manifests form a chain (`store_sealed.c`: sequences 1..n exactly once, genesis has no link, each later one names its predecessor; the newest is bound; at most one new manifest per transaction, only as n+1 naming the bound one). K_vol is never kept resident between unlock and rotation. Boot 7 (the boot-3 disk again, a second cold resume at generation 6; boot record 7, continuity resume 8): `store: rotated recovery keyslot manifest_sequence=2 security_sequence=3 generation=9 (previous secret retired)`, `lifecycle: rotate ok state=ready`, the typed line echoed as 129 `*`, neither secret in the serial log. Host: stage-test 839 checks (test_rotate: wrong current refused with the disk byte-identical, S1->S2->S3 chain of three, each retired secret refused, TEST-key Store refuses `NoKeySlot`), stage-sanitize 839, stage-free 45 objects, store_sealed chain tests (sequence skip, duplicate sequence, link to an envelope, link to a non-newest manifest, forged link, claimed genesis with a link all refused; bootstrap returns the newest of a verified chain; superseded manifests stay readable). Limits: active_key_epoch stays 1 (rotation re-wraps K_vol, it does not change it; a K_vol compromise is section 2.7 item 2 migration, not done); TPM Slot 0 BLOCKED_HARDWARE; Argon2id profile and owner-root path not in this cut; hardware NOT_RUN | LIFECYCLE | QEMU PASS (2026-10-12: 183 rows on d2f8400, AIENOS_CK_LIFECYCLE and AIENOS_CK_LIFECYCLE_INFER PASS; boot 7 `store: rotated recovery keyslot manifest_sequence=2 security_sequence=3 generation=9 (previous secret retired)`, `lifecycle: rotate ok state=ready`, echo of 129 `*`, no secret in the log; hardware NOT_RUN) | +| 185 | after rotation (boot 8, the boot-7 disk): the old secret is refused by the keyslot tag of the newest manifest (`RecoverySecretRejected`, disk not opened, not reformatted); the new secret opens the Store at generation 9, the keyed open binds the chain of two manifests, `store: security manifest sequence=4 generation=10 keyslot=bound previous=linked`, continuity RESUMES incarnation 3, the memory-only fact still answers from the disk; no SecurityManifest refusal; neither secret in the serial log | LIFECYCLE | QEMU PASS (2026-10-12: boot 8 old secret `RecoverySecretRejected` still LOCKED, new secret `opened generation=9`, `security manifest sequence=4 generation=10 keyslot=bound previous=linked`, `continuity: RESUMED incarnation=3`, fact answered from the disk; hardware NOT_RUN) | diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 4ef87e2c..69f30a86 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T08:47:00Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:d442b49555533f077c87c907efa0fea22b5b20ef93bb1650adcfcce4d4ce50ed", + "digest": "sha256:13c7a827e3ccb3c9395a86080f2a7c7540037fc7c4fb6039c80fbcc6b7413bdf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 42, - "generated_at_commit": "638d78e94611546f909e559c035e640d06d2857f", - "source_tree": "sha256:787dbd3bdd1de24aa7f87a795ce75b145e39c7b1a758bacddafad52fc452c7be" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:30523c9d1ccd0f870c17ab6a7903d0d43df25d4ca13f7cae9d601d045320085a" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c index 9d4e1fba..42ec92fa 100644 --- a/native/kernel/core/lifecycle.c +++ b/native/kernel/core/lifecycle.c @@ -69,6 +69,7 @@ void ck_dev_nvme_release(void); int ck_store_service_ready(void); int ck_store_service_close(void); int ck_store_service_unlock(const char *arg, size_t arg_len); +int ck_store_service_rotate(const char *arg, size_t arg_len); static ck_lc g_lc; static uint8_t g_last_id[32]; @@ -88,7 +89,7 @@ static uint32_t g_cx_refused; /* remember/facts refused by the Store or Corte static void help(void) { - ck_puts("lifecycle: verbs: status unlock [<64-hex recovery secret>] task <text> prompt <text> approve deny cancel recall remember <text> facts evidence help shutdown\n"); + ck_puts("lifecycle: verbs: status unlock [<64-hex recovery secret>] rotate <current 64-hex> <new 64-hex> task <text> prompt <text> approve deny cancel recall remember <text> facts evidence help shutdown\n"); } static void status(void) @@ -446,6 +447,13 @@ static void handle_line(const char *line, size_t len) else ck_printf("lifecycle: REFUSED verb=unlock state=%s reason=\"%s\"\n", ck_lc_state_name(g_lc.state), g_lc.reason); return; } + case CK_LC_R_ROTATE: { + int ok = ck_store_service_rotate(arg, arg_len); + ck_lc_rotate_done(&g_lc, ok); + if (ok) ck_printf("lifecycle: rotate ok state=%s\n", ck_lc_state_name(g_lc.state)); + else ck_printf("lifecycle: REFUSED verb=rotate state=%s reason=\"%s\"\n", ck_lc_state_name(g_lc.state), g_lc.reason); + return; + } case CK_LC_R_COMMIT: { int rc = commit_task(); ck_lc_commit_done(&g_lc, rc); @@ -484,18 +492,21 @@ static void handle_line(const char *line, size_t len) } } -/* 1 when the line so far is "unlock" followed by its argument: those bytes - * are the recovery secret and are echoed as '*' so the serial log never - * carries them. */ -static int in_secret(const char *buf, size_t len) +/* 1 when the line so far is "unlock" or "rotate" followed by its argument: + * those bytes are recovery secrets and are echoed as '*' so the serial log + * never carries them. */ +static int after_verb(const char *buf, size_t len, const char *verb) { - static const char verb[] = "unlock"; size_t i = 0, j; while (i < len && buf[i] == ' ') i++; - for (j = 0; j < sizeof verb - 1; j++) + for (j = 0; verb[j]; j++) if (i + j >= len || buf[i + j] != verb[j]) return 0; return i + j < len && buf[i + j] == ' '; } +static int in_secret(const char *buf, size_t len) +{ + return after_verb(buf, len, "unlock") || after_verb(buf, len, "rotate"); +} /* Bounded serial line editor: printable bytes collected, Backspace edits, * CR or LF ends the line; a line past CK_LC_LINE_MAX is refused whole (the diff --git a/native/kernel/core/lifecycle_fsm.c b/native/kernel/core/lifecycle_fsm.c index df49137c..18502bd4 100644 --- a/native/kernel/core/lifecycle_fsm.c +++ b/native/kernel/core/lifecycle_fsm.c @@ -50,6 +50,8 @@ enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, siz /* unlock carries the optional recovery secret (the binding checks its * shape and never echoes it); every other verb takes no argument. */ if (word_is(w, n, "unlock")) return CK_LC_V_UNLOCK; + /* rotate carries "<current 64-hex> <new 64-hex>" (the binding checks it) */ + if (word_is(w, n, "rotate")) return CK_LC_V_ROTATE; if (*arg_len != 0) return CK_LC_V_INVALID; if (word_is(w, n, "help")) return CK_LC_V_HELP; if (word_is(w, n, "status")) return CK_LC_V_STATUS; @@ -82,6 +84,7 @@ const char *ck_lc_verb_name(enum ck_lc_verb v) case CK_LC_V_RECALL: return "recall"; case CK_LC_V_REMEMBER: return "remember"; case CK_LC_V_FACTS: return "facts"; + case CK_LC_V_ROTATE: return "rotate"; } return "?"; } @@ -157,6 +160,11 @@ enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, siz case CK_LC_V_UNLOCK: if (lc->state != CK_LC_LOCKED) return refuse(lc, "already unlocked"); return CK_LC_R_UNLOCK; + case CK_LC_V_ROTATE: + if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: unlock before rotating the recovery secret"); + if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); + if (arg_len == 0) return refuse(lc, "rotate needs <current 64-hex> <new 64-hex>"); + return CK_LC_R_ROTATE; case CK_LC_V_TASK: if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); @@ -227,6 +235,15 @@ void ck_lc_commit_done(ck_lc *lc, int rc) } } +void ck_lc_rotate_done(ck_lc *lc, int ok) +{ + if (lc->state != CK_LC_READY) return; + if (!ok) { + lc->refused++; + lc->reason = "recovery secret rotation refused: Store unchanged"; + } +} + void ck_lc_unlock_done(ck_lc *lc, int opened) { if (lc->state != CK_LC_LOCKED) return; diff --git a/native/kernel/core/lifecycle_fsm.h b/native/kernel/core/lifecycle_fsm.h index ff1d0265..9179e73c 100644 --- a/native/kernel/core/lifecycle_fsm.h +++ b/native/kernel/core/lifecycle_fsm.h @@ -40,10 +40,11 @@ enum ck_lc_verb { CK_LC_V_RECALL, /* read back the newest approved object from the Store */ CK_LC_V_REMEMBER, /* P06 cut 4: record one owner fact in the Cortex journal (sealed Store) */ CK_LC_V_FACTS, /* list the remembered facts read back from the journal */ + CK_LC_V_ROTATE, /* P04 cut 2b: rotate the recovery secret (current + new, 64 hex each) */ }; /* Bounds: one request line, and the task text that a line may carry. */ -#define CK_LC_LINE_MAX 96 /* "unlock " + 64 hex = 71 bytes fits */ +#define CK_LC_LINE_MAX 144 /* "rotate " + 64 hex + " " + 64 hex = 136 bytes fits */ #define CK_LC_TASK_MAX 48 #define CK_LC_REPLY_MAX 256 /* generated reply bytes kept for approval (INFER_TEXT_BYTES) */ /* What is pending in SERVE: an operator note (task) or a generated reply. */ @@ -61,6 +62,7 @@ enum ck_lc_result { CK_LC_R_RECALL, /* recall accepted: the binding must read the newest approved object */ CK_LC_R_REMEMBER, /* remember accepted: the binding must append the fact (task holds the text) */ CK_LC_R_FACTS, /* facts accepted: the binding must list the journal */ + CK_LC_R_ROTATE, /* rotate accepted: the binding must re-wrap K_vol under the new secret */ }; typedef struct { @@ -96,6 +98,9 @@ enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, siz * with reason), unlock outcome (1 opened: LOCKED -> READY). */ void ck_lc_commit_done(ck_lc *lc, int rc); void ck_lc_unlock_done(ck_lc *lc, int opened); +/* Rotation outcome after CK_LC_R_ROTATE: the state never changes; a refusal + * by the Store (wrong current secret, no keyslot) counts as refused. */ +void ck_lc_rotate_done(ck_lc *lc, int ok); /* Generation outcome after CK_LC_R_GENERATE: a reply of n bytes (1..CK_LC_REPLY_MAX) * becomes the pending proposal (READY -> SERVE, replies++); cancelled (the * operator typed cancel between steps) or failed/refused (n == 0) drops the diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index c7620545..47186434 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -23,21 +23,21 @@ "purpose": "Directory native/kernel/svc/compat. Purpose not yet described by a human." }, { - "digest": "c9cfdfae82f6668d32a1378c6ce99fbc5fd7b3a0f47cf3986ee0a8cf3ba40032", - "last_commit": "b9baac8", + "digest": "44ea8525d341bcebcb307fb1743de084b124745bd7c46d85761e554183ee02cc", + "last_commit": "56d125f", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:5a1483582a1fbfc20be8ad0417b7e930888cf40344fe2dca199fa0b7c4643b3c", - "compiled_at": "2026-10-12T09:21:41Z", + "children_root": "sha256:d835471b1405ca482a41beb34ed339d21e11064a59138599ee5754d198926878", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:28548cc5bc6f6d137cfb2bbf7a3bbba09ac92ee3d0c2f34eb08772b92ac64e8b", + "digest": "sha256:df0e798c909b72635d6b3aaba97462da29f468723857ee4fd055110c8eb07c1d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 41, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:4285732e09f59ff61427199f2360237047dd00f26c296d9db0f0a96b0964904a" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:47a0244de9d55e4ffee6978121c7b9df86fe8d0bbd55ac7971ec7f519c41212f" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index 91e0a622..03e45ed2 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -162,6 +162,10 @@ static const char *classify(int rc) /* Workspace lives across boots of the host test; the kernel uses it once. */ static ss_workspace *g_ws; static ss_store *g_ss; +/* The newest SecurityManifest after a committed boot or rotation (P04 cut + * 2a/2b): sequence and Store ObjectId the next manifest links to. */ +static uint64_t g_secman_seq; +static uint8_t g_secman_sid[32]; /* SecurityManifest refusal: the Store is not served and the keys are wiped. */ static int secman_refuse(ck_store_report *r, const char *step) @@ -350,12 +354,15 @@ static int run_policy_ex(const disk_dev *d, const ss_keys *keys, const uint8_t u #ifdef CK_TEST_STORE_CRASH ck_store_crash_opened(r->gen_open); #endif - rc = ss_transact(g_ss, objs, nobjs, CK_SB_HOOK, 0, 0); + uint8_t ids[3][32]; + rc = ss_transact(g_ss, objs, nobjs, CK_SB_HOOK, 0, ids); if (rc) return fail(r, "commit", "ss_transact", rc); rc = disk_flush(d); if (rc) return fail(r, "io", "flush", rc); r->gen_commit = ss_generation(g_ss); if (r->gen_commit != r->gen_open + 1) return fail(r, "commit", "generation did not advance by one", -1); + g_secman_seq = r->secman_seq; + bcp(g_secman_sid, ids[1], 32); r->verdict = CK_SB_COMMITTED; return 0; } @@ -499,6 +506,7 @@ int ck_store_unlock_boot(const disk_dev *d, const uint8_t secret[CK_SB_SECRET_LE rc = run_policy_ex(d, &keys, uuid, commit, hardware_staging, blank ? &ks : 0, r); r->key_source = "recovery-keyslot"; r->unlocked_genesis = genesis; + r->keyslot_seq = m.manifest_sequence; if (rc == 0 && !blank && !g_ss->has_keyslot) { /* Opened, but the keyed open did not bind the manifest we * bootstrapped from: refuse rather than serve an unbound keyslot. */ @@ -511,6 +519,106 @@ int ck_store_unlock_boot(const disk_dev *d, const uint8_t secret[CK_SB_SECRET_LE return rc; } +/* The Recovery_RawSecret (HKDF, GCM-SIV) descriptor of a decoded manifest. */ +static m5_keyslot *recovery_slot(m5_ksm *m) +{ + for (uint32_t i = 0; i < m->slot_count && i < M5_KSM_SLOTS; i++) + if (m->slots[i].slot_type == M5_SLOT_RECOVERY_RAW_SECRET && m->slots[i].kdf_suite == 0x02 && + m->slots[i].wrap_suite == M5_WRAP_SUITE_GCMSIV) + return &m->slots[i]; + return 0; +} + +int ck_store_rotate_recovery(const disk_dev *d, const uint8_t current[CK_SB_SECRET_LEN], + const uint8_t next[CK_SB_SECRET_LEN], ck_store_report *r) +{ + m5_ksm m; + m5_keyslot *sl; + uint8_t k_vol[32], kek[32], fresh[16 + 12], smb[M5_SECMAN_LEN], ids[2][32]; + size_t mlen = 0; + uint16_t kind = 0; + int rc; + + bz(r, sizeof *r); + bcp(r->prev_commit, "none", 5); + r->identity_class = M5_ID_PRODUCTION; + r->key_source = "recovery-keyslot"; + if (!d || !current || !next) return fail(r, "geometry", "arguments", -1); + if (!g_ss || !g_ws || !g_ksm_buf || g_ss->keys.identity_class != M5_ID_PRODUCTION || !g_ss->has_keyslot) + return fail(r, "keyed", "no open production Store with a KeySlotManifest to rotate", CK_SB_E_NO_KEYSLOT); + r->gen_open = ss_generation(g_ss); + rc = ss_read(g_ss, g_ss->keyslot_sid, g_ksm_buf, SS_KEYSLOT_MAX, &mlen, &kind); + if (rc || kind != M5_KSM_KIND) return fail(r, "keyed", "read the bound KeySlotManifest", rc ? rc : CK_SB_E_KEYSLOT_BAD); + bz(&m, sizeof m); + if (m5_ksm_decode(g_ksm_buf, mlen, &m) != 0 || m.arena_len != 0 || mlen != M5_KSM_FIXED_LEN) + return fail(r, "keyed", "KeySlotManifest decode (raw-secret profile, no arena)", CK_SB_E_KEYSLOT_BAD); + if (m.manifest_sequence != g_ss->nkeyslot) + return fail(r, "keyed", "bound manifest sequence is not the chain length", CK_SB_E_KEYSLOT_BAD); + sl = recovery_slot(&m); + if (!sl) return fail(r, "keyed", "no Recovery_RawSecret (HKDF) descriptor in the manifest", CK_SB_E_KEYSLOT_BAD); + /* proof of possession: the current secret must unwrap K_vol */ + rc = m5_recovery_kek(sl->salt, current, CK_SB_SECRET_LEN, kek); + if (rc == 0) rc = m5_keyslot_unwrap(sl, g_ss->uuid, kek, k_vol); + bz(kek, sizeof kek); + if (rc) { + bz(k_vol, sizeof k_vol); + return fail(r, "keyed", "current recovery secret rejected (K_vol unwrap tag failed); nothing written", + CK_SB_E_SECRET_REJECTED); + } + if (ck_entropy_fill(fresh, sizeof fresh) != 0) { + bz(k_vol, sizeof k_vol); + bz(fresh, sizeof fresh); + return fail(r, "keyed", "rotation entropy (salt, nonce) refused by the entropy service", CK_SB_E_ENTROPY); + } + /* next link of the chain: same K_vol and epoch, new salt, K_vol re-wrapped under the new KEK */ + m.manifest_sequence += 1; + bcp(m.previous_keyslot_manifest_id, g_ss->keyslot_sid, 32); + bcp(sl->salt, fresh, 16); + rc = m5_recovery_kek(sl->salt, next, CK_SB_SECRET_LEN, kek); + if (rc == 0) rc = m5_keyslot_wrap(sl, g_ss->uuid, kek, k_vol, fresh + 16); + bz(kek, sizeof kek); + bz(k_vol, sizeof k_vol); + bz(fresh, sizeof fresh); + if (rc) { + bz(&m, sizeof m); + return fail(r, "keyed", "wrap K_vol under the new recovery secret", rc); + } + m5_ksm_encode(&m, g_ksm_buf); + r->keyslot_seq = m.manifest_sequence; + bz(&m, sizeof m); + /* the SecurityManifest of the rotation transaction binds the new manifest */ + { + m5_secman n; + bz(&n, sizeof n); + bcp(n.store_uuid, g_ss->uuid, 16); + n.generation = r->gen_open + 1; + n.security_sequence = g_secman_seq + 1; + n.epoch = n.generation; + n.key_epoch = g_ss->keys.key_generation; + bcp(n.previous_security_manifest_id, g_secman_sid, 32); + if (sv1_object_id(M5_KSM_KIND, 1, g_ksm_buf, M5_KSM_FIXED_LEN, n.keyslot_manifest_id) != 0) + return fail(r, "commit", "KeySlotManifest ObjectId", -1); + if (m5_secman_seal(&n, g_ss->keys.k_root_auth, smb) != 0) return fail(r, "commit", "seal SecurityManifest", -1); + r->secman_seq = n.security_sequence; + r->secman_keyslot = 1; + r->secman_prev = 1; + } + { + ss_object objs[2] = {{M5_KSM_KIND, 1, g_ksm_buf, M5_KSM_FIXED_LEN}, {CK_SECMAN_KIND, 1, smb, M5_SECMAN_LEN}}; + rc = ss_transact(g_ss, objs, 2, CK_SB_HOOK, 0, ids); + } + bz(g_ksm_buf, SS_KEYSLOT_MAX); + if (rc) return fail(r, "commit", "ss_transact", rc); + rc = disk_flush(d); + if (rc) return fail(r, "io", "flush", rc); + r->gen_commit = ss_generation(g_ss); + if (r->gen_commit != r->gen_open + 1) return fail(r, "commit", "generation did not advance by one", -1); + g_secman_seq = r->secman_seq; + bcp(g_secman_sid, ids[1], 32); + r->verdict = CK_SB_COMMITTED; + return 0; +} + static const char *rb_name(int rb) { return rb == SS_RB_VALID_RESUME ? "valid-resume" : rb == SS_RB_PREPARED_ADVANCE ? "prepared-advance" : @@ -787,6 +895,54 @@ int ck_store_service_unlock(const char *arg, size_t arg_len) } return 1; } + +/* 64 hex digits -> 32 secret bytes; 0 ok. out is wiped on refusal. */ +static int parse_secret(const char *a, size_t n, uint8_t out[CK_SB_SECRET_LEN]) +{ + if (n != 2 * CK_SB_SECRET_LEN) return -1; + for (size_t i = 0; i < CK_SB_SECRET_LEN; i++) { + int hi = hex_nibble(a[2 * i]), lo = hex_nibble(a[2 * i + 1]); + if (hi < 0 || lo < 0) { + bz(out, CK_SB_SECRET_LEN); + return -1; + } + out[i] = (uint8_t)((hi << 4) | lo); + } + return 0; +} + +/* "rotate <current 64-hex> <new 64-hex>" while READY (P04 cut 2b). Both + * secrets are wiped before return and never printed. Returns 1 only when + * the rotation transaction committed. */ +int ck_store_service_rotate(const char *arg, size_t arg_len) +{ + uint8_t cur[CK_SB_SECRET_LEN], nxt[CK_SB_SECRET_LEN]; + ck_store_report r; + if (!g_service_open || !g_disk) { + ck_printf("store: rotate REFUSED proof=keyed step=\"Store not open\" rc=%d\n", CK_SB_E_NO_KEYSLOT); + return 0; + } + if (arg_len != 4 * CK_SB_SECRET_LEN + 1 || arg[2 * CK_SB_SECRET_LEN] != ' ' || + parse_secret(arg, 2 * CK_SB_SECRET_LEN, cur) != 0 || + parse_secret(arg + 2 * CK_SB_SECRET_LEN + 1, 2 * CK_SB_SECRET_LEN, nxt) != 0) { + bz(cur, sizeof cur); + bz(nxt, sizeof nxt); + ck_printf("store: rotate REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (two 64-hex secrets expected, got %u bytes)\n", + CK_SB_E_SECRET_REJECTED, (unsigned)arg_len); + return 0; + } + int rc = ck_store_rotate_recovery(g_disk, cur, nxt, &r); + bz(cur, sizeof cur); + bz(nxt, sizeof nxt); + if (rc || r.verdict != CK_SB_COMMITTED) { + ck_printf("store: rotate REFUSED proof=%s step=\"%s\" rc=%d (%s); Store unchanged, still open\n", r.proof, r.step, + r.rc, err_name(r.rc)); + return 0; + } + ck_printf("store: rotated recovery keyslot manifest_sequence=%llu security_sequence=%llu generation=%llu (previous secret retired)\n", + (unsigned long long)r.keyslot_seq, (unsigned long long)r.secman_seq, (unsigned long long)r.gen_commit); + return 1; +} #endif ss_store *store_boot_store(void) diff --git a/native/kernel/svc/store_boot.h b/native/kernel/svc/store_boot.h index 8843a2c1..5d3640e0 100644 --- a/native/kernel/svc/store_boot.h +++ b/native/kernel/svc/store_boot.h @@ -50,11 +50,28 @@ * equal to the number of manifests on the Store. A Store with history but * no manifest, or a manifest failing any check, is refused (CK_SB_E_SECMAN) * before the boot record is written and the keys are wiped. - * Limits (cut 2a): TPM Slot 0 stays BLOCKED_HARDWARE, no key rotation - * (active_key_epoch 1); only the boot transaction writes a manifest - * (approve, continuity and journal commits do not advance - * security_sequence); agent_root_id, continuity_manifest_id and - * migration_manifest_id stay zero (not yet linked). */ + * + * KEY ROTATION (P04 cut 2b, ADR 0017 section 2.7 item 1, credential + * rotation). ck_store_rotate_recovery takes the current and the new raw + * recovery secret while the production Store is open: the current secret + * must unwrap K_vol from the bound manifest (proof of possession, nothing is + * written otherwise), a fresh salt and nonce come from the entropy service, + * and one transaction commits the next KeySlotManifest of the chain + * (manifest_sequence + 1, previous_keyslot_manifest_id = the bound manifest, + * same K_vol and active_key_epoch, the Recovery_RawSecret descriptor + * re-wrapped under the new KEK) together with a SecurityManifest + * (security_sequence + 1) that binds the new manifest. The sealed store + * admits a manifest only as the next link of the chain and binds the newest + * one, so the next unlock accepts the new secret only: the old descriptor is + * superseded, its ciphertext stays on the append-only Store (ADR 0017 + * section 2.7 item 2: a K_vol compromise needs migration, not rotation). + * K_vol lives in a stack buffer for the rewrap only and is wiped. + * Limits (cut 2b): TPM Slot 0 stays BLOCKED_HARDWARE (slot 0 Empty); + * active_key_epoch stays 1 (K_vol never changes in place); only the boot + * and rotation transactions write a SecurityManifest (approve, continuity + * and journal commits do not advance security_sequence); agent_root_id, + * continuity_manifest_id and migration_manifest_id stay zero (not yet + * linked). */ #ifndef AIENOS_CK_STORE_BOOT_H #define AIENOS_CK_STORE_BOOT_H #include <stdint.h> @@ -84,6 +101,7 @@ typedef struct { int unlocked_genesis; /* ck_store_unlock_boot: this run created the keyslot manifest */ const char *key_source; /* ck_store_unlock_boot: "recovery-keyslot" */ uint64_t secman_seq; /* security_sequence of the SecurityManifest this boot committed */ + uint64_t keyslot_seq; /* manifest_sequence of the KeySlotManifest bound (unlock) or committed (rotate) */ int secman_keyslot; /* 1: it binds the KeySlotManifest ObjectId (production Store) */ int secman_prev; /* 1: it links the manifest it supersedes (0 at genesis) */ } ck_store_report; @@ -129,6 +147,12 @@ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t * hardware_staging as store_boot_run_policy. Prints nothing. */ int ck_store_unlock_boot(const disk_dev *d, const uint8_t secret[CK_SB_SECRET_LEN], const uint8_t *expect_uuid, const char *commit, int hardware_staging, ck_store_report *r); +/* Recovery secret rotation on the open production Store (see the header + * comment): current must unwrap K_vol from the bound KeySlotManifest, next is + * the new raw secret. One transaction (next manifest + SecurityManifest) or + * nothing. Prints nothing. */ +int ck_store_rotate_recovery(const disk_dev *d, const uint8_t current[CK_SB_SECRET_LEN], + const uint8_t next[CK_SB_SECRET_LEN], ck_store_report *r); /* Print the report lines (also used by the host test). */ void store_boot_print(const ck_store_report *r); /* The open Store of the last store_boot_run (valid only after it returned 0; diff --git a/native/kernel/svc/tests/.crumb b/native/kernel/svc/tests/.crumb index 1101afc8..e18924b2 100644 --- a/native/kernel/svc/tests/.crumb +++ b/native/kernel/svc/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T09:21:41Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:c9cfdfae82f6668d32a1378c6ce99fbc5fd7b3a0f47cf3986ee0a8cf3ba40032", + "digest": "sha256:44ea8525d341bcebcb307fb1743de084b124745bd7c46d85761e554183ee02cc", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:3280a019c3894e8cd2621902dffef6cf605ba9c769131e815a8a13bdfa6b3820" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:0dd845f9e709078f6366a7917056263c5148eb84f38aa92c70490f0246099f57" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index fd0b8137..bee78a22 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -1281,6 +1281,64 @@ static void test_secman(void) free(after); } +/* P04 cut 2b: recovery secret rotation (ADR 0017 section 2.7 item 1) on the + * production Store: proof of possession, one transaction (next + * KeySlotManifest + SecurityManifest), old secret retired, chain of three. */ +static int rotate(const uint8_t *cur, const uint8_t *nxt, ck_store_report *r) +{ + if (dopen(0)) return -999; + int rc = ck_store_rotate_recovery(&g_d, cur, nxt, r); + dclose(); + return rc; +} +static void test_rotate(void) +{ + ck_store_report r; + ss_keys k; + uint8_t S1[32], S2[32], S3[32]; + for (int i = 0; i < 32; i++) { S1[i] = (uint8_t)(i * 7 + 1); S2[i] = (uint8_t)(i * 5 + 2); S3[i] = (uint8_t)(i * 13 + 3); } + uint8_t *now = malloc(T_BYTES), *after = malloc(T_BYTES); + CHECK(now && after); + if (!now || !after) return; + + printf(" [rotate: genesis under S1, rotate S1->S2 in one transaction, S1 retired, S2 opens]\n"); + CHECK(dopen(1) == 0); + dclose(); + CHECK(unlock(S1, 0, "rt-g", 0, &r) == 0 && r.secman_seq == 1 && r.keyslot_seq == 1 && store_boot_store()->nkeyslot == 1); + CHECK(load(now) == 0); + CHECK(rotate(S2, S3, &r) == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED && r.rc == CK_SB_E_SECRET_REJECTED); + CHECK(r.proof && strcmp(r.proof, "keyed") == 0); + CHECK(load(after) == 0 && memcmp(now, after, T_BYTES) == 0); /* wrong current secret: nothing written */ + CHECK(store_boot_store()->nkeyslot == 1 && ss_generation(store_boot_store()) == 2); + CHECK(rotate(S1, S2, &r) == 0 && r.verdict == CK_SB_COMMITTED && r.keyslot_seq == 2 && r.secman_seq == 2 && + r.secman_keyslot == 1 && r.secman_prev == 1 && r.gen_commit == 3); + CHECK(store_boot_store()->nkeyslot == 2); + CHECK(load(now) == 0); + CHECK(unlock(S1, 0, "rt-old", 1, &r) == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED); + CHECK(load(after) == 0 && memcmp(now, after, T_BYTES) == 0); /* retired secret: disk as found */ + CHECK(unlock(S2, 0, "rt-new", 1, &r) == 0 && r.secman_seq == 3 && r.secman_keyslot == 1 && r.secman_prev == 1 && + r.keyslot_seq == 2 && r.gen_commit == 4 && store_boot_store()->nkeyslot == 2); + + printf(" [rotate: a second rotation S2->S3 extends the chain to 3; S3 opens, S2 and S1 refused]\n"); + CHECK(rotate(S2, S3, &r) == 0 && r.keyslot_seq == 3 && r.secman_seq == 4 && r.gen_commit == 5); + CHECK(unlock(S2, 0, "rt-2", 1, &r) == CK_SB_E_SECRET_REJECTED); + CHECK(unlock(S1, 0, "rt-1", 1, &r) == CK_SB_E_SECRET_REJECTED); + CHECK(unlock(S3, 0, "rt-3", 1, &r) == 0 && r.secman_seq == 5 && r.keyslot_seq == 3 && r.gen_commit == 6 && + store_boot_store()->nkeyslot == 3); + CHECK(unlock(S3, 0, "rt-3b", 1, &r) == 0 && r.secman_seq == 6 && r.gen_commit == 7); + + printf(" [rotate: a TEST-key Store has no keyslot to rotate]\n"); + ck_store_test_keys(&k); + CHECK(dopen(1) == 0); + dclose(); + CHECK(boot(&k, "rt-t", &r) == 0); + CHECK(rotate(S1, S2, &r) == CK_SB_E_NO_KEYSLOT && r.verdict == CK_SB_REFUSED); + + unlink(img); + free(now); + free(after); +} + /* P05 cut 1: production continuity on the service lifecycle * (svc/continuity_service.c) over the real sealed Store in a file, opened the * production way (recovery keyslot unlock). Identity + ALLEN subject are @@ -2562,6 +2620,7 @@ int main(void) test_store(); test_unlock_recovery(); test_secman(); + test_rotate(); test_continuity_service(); test_cortex_journal(); test_artifact_store(); diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index e1d97bb6..e59657c2 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T08:47:00Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:83457528f2132fa686bb428663db2585a32f3a731dddb30ea0c2295a28c45656", + "digest": "sha256:c65a92ef6ebb21a41f8148c443f9e87d142734e5f08b3d0402644060ff07c4bf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 150, - "generated_at_commit": "638d78e94611546f909e559c035e640d06d2857f", - "source_tree": "sha256:8483be0dfe530af053090ec861972ffd7733e43dfcad9ff1c3c30cece0c1bc4f" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:ff91f3553e2525cde42fd32c98bac1bf33d22e47248747152c01cd437aa6c55c" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_lifecycle.c b/native/kernel/tests/test_lifecycle.c index db550dc4..4959867e 100644 --- a/native/kernel/tests/test_lifecycle.c +++ b/native/kernel/tests/test_lifecycle.c @@ -27,6 +27,9 @@ int main(void) CHECK(dec("unlock", &a, &al) == CK_LC_V_UNLOCK && al == 0); CHECK(dec("unlock deadbeef ", &a, &al) == CK_LC_V_UNLOCK && al == 8 && memcmp(a, "deadbeef", 8) == 0); CHECK(CK_LC_LINE_MAX >= 7 + 64); /* "unlock " + a 64-hex recovery secret fits in one line */ + CHECK(CK_LC_LINE_MAX >= 7 + 64 + 1 + 64); /* "rotate " + current + " " + new */ + CHECK(dec("rotate aa bb", &a, &al) == CK_LC_V_ROTATE && al == 5 && memcmp(a, "aa bb", 5) == 0); + CHECK(dec("rotate", &a, &al) == CK_LC_V_ROTATE && al == 0); CHECK(dec("frobnicate", &a, &al) == CK_LC_V_UNKNOWN); CHECK(dec("prompt hi", &a, &al) == CK_LC_V_PROMPT && al == 2 && memcmp(a, "hi", 2) == 0); CHECK(dec("prompt", &a, &al) == CK_LC_V_INVALID); /* prompt needs text */ @@ -56,6 +59,7 @@ int main(void) CHECK(lc.state == CK_LC_LOCKED); CHECK(req(&lc, "status") == CK_LC_R_OK && req(&lc, "help") == CK_LC_R_OK && req(&lc, "evidence") == CK_LC_R_OK); CHECK(req(&lc, "task hello") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.tasks == 0); + CHECK(req(&lc, "rotate aa bb") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED); /* unlock first */ CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "deny") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); ck_lc_unlock_done(&lc, 0); @@ -64,6 +68,13 @@ int main(void) ck_lc_unlock_done(&lc, 1); CHECK(lc.state == CK_LC_READY); CHECK(req(&lc, "unlock") == CK_LC_R_REFUSED); /* already unlocked */ + CHECK(req(&lc, "rotate") == CK_LC_R_REFUSED && lc.state == CK_LC_READY); /* needs both secrets */ + CHECK(req(&lc, "rotate aa bb") == CK_LC_R_ROTATE); + ck_lc_rotate_done(&lc, 0); + CHECK(lc.state == CK_LC_READY && strstr(lc.reason, "rotation refused")); + CHECK(req(&lc, "rotate aa bb") == CK_LC_R_ROTATE); + ck_lc_rotate_done(&lc, 1); + CHECK(lc.state == CK_LC_READY); CHECK(req(&lc, "shutdown") == CK_LC_R_STOP && lc.state == CK_LC_QUIESCE); CHECK(req(&lc, "status") == CK_LC_R_REFUSED && strcmp(lc.reason, "stopping") == 0); ck_lc_stopped(&lc); diff --git a/native/store/.crumb b/native/store/.crumb index 399f4ffc..1e437996 100644 --- a/native/store/.crumb +++ b/native/store/.crumb @@ -13,21 +13,21 @@ "generated": { "children": [ { - "digest": "68639cafb167da617ac29e4caf4a1f7da653f9629d764c096e1992979cb557ce", - "last_commit": "a9d120b", + "digest": "0a09ffa7f22b22f32d3c23ca76645b1858d972e8575c3236120f0b747c856c05", + "last_commit": "56d125f", "name": "tests", "purpose": "Directory native/store/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:e7b4e9b800c80b1534fb10f83005e1a39d7515212c0e0aa423c00ec22aa92c45", - "compiled_at": "2026-10-12T05:32:10Z", + "children_root": "sha256:24a9259b30657d17f3cab3901246fb8cd28441d8a929839fd58a86aa951a66a3", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:9146f4be830fe28ed14ce6bfac89cd97bd2043c825a60a00451152309e1f87ea", + "digest": "sha256:eeec17e754d15d5723d77d953711620c568c160f47e6c90a4c927c85ec5bb32d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 22, - "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", - "source_tree": "sha256:2e3ff42747a95c42fc2d164f6a46834c835e51f41bd85820dc1f174740fcb847" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:b243a2c9f993a61cda3bd5602ae99a6f22e954a37e85d20a92c4c90b13ba5368" }, "owner_lane": "", "provenance": { diff --git a/native/store/store_sealed.c b/native/store/store_sealed.c index a214b668..cecbcba0 100644 --- a/native/store/store_sealed.c +++ b/native/store/store_sealed.c @@ -224,6 +224,43 @@ static int keyslot_shape(const uint8_t *b, size_t len, const uint8_t uuid[16]) return 0; } +/* manifest_sequence and previous_keyslot_manifest_id of a shape-checked + * manifest (m5_ksm_encode layout: sequence at 28, previous id at 36). */ +static void ks_fields(const uint8_t *b, uint64_t *seq, uint8_t prev[32]) +{ + *seq = 0; + for (int i = 7; i >= 0; i--) *seq = (*seq << 8) | b[28 + i]; + memcpy(prev, b + 36, 32); +} + +/* The chain rule: the n manifests carry sequences 1..n exactly once, the + * sequence-1 manifest has a zero previous id and every later one names the + * Store ObjectId of its predecessor. Returns the index of the newest + * (sequence n) or -1. */ +static int ks_chain(const ss_workspace *ws, uint32_t n) +{ + uint8_t zero[32]; + int newest = -1; + memset(zero, 0, 32); + if (n == 0 || n > SS_KS_CHAIN_MAX) return -1; + for (uint32_t i = 0; i < n; i++) { + uint64_t q = ws->ks[i].seq; + if (q < 1 || q > n) return -1; + for (uint32_t j = 0; j < i; j++) + if (ws->ks[j].seq == q) return -1; /* GUARD:keyslot-chain-duplicate */ + if (q == 1) { + if (memcmp(ws->ks[i].prev, zero, 32) != 0) return -1; /* GUARD:keyslot-chain-genesis */ + } else { + int found = 0; + for (uint32_t j = 0; j < n; j++) + if (ws->ks[j].seq == q - 1 && memcmp(ws->ks[j].sid, ws->ks[i].prev, 32) == 0) found = 1; + if (!found) return -1; /* GUARD:keyslot-chain-link */ + } + if (q == n) newest = (int)i; + } + return newest; +} + int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *ws, uint8_t *out, size_t cap, size_t *len) { @@ -232,20 +269,28 @@ int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *w memset(scratch, 0, sizeof *scratch); int rc = st_open(scratch, store, &ws->st); if (rc != 0) return rc; - uint32_t n; + uint32_t n, nk = 0; const sv1_entry *e = st_catalog(scratch, &n); int at = -1; + size_t l; for (uint32_t i = 0; i < n; i++) { if (e[i].kind != SS_KIND_KEYSLOT || e[i].version != 1) continue; - if (at >= 0) return SS_E_KEYSLOT; /* GUARD:bootstrap-single */ - at = (int)i; + if (nk >= SS_KS_CHAIN_MAX) return SS_E_KEYSLOT; /* GUARD:bootstrap-chain-cap */ + if (e[i].byte_length > SS_KEYSLOT_MAX || e[i].byte_length > cap) return SS_E_KEYSLOT; + rc = st_read_object(scratch, e[i].object_id, out, cap, &l); + if (rc != 0) return rc; + if (keyslot_shape(out, l, NULL) != 0) { memset(out, 0, cap); return SS_E_KEYSLOT; } /* GUARD:bootstrap-shape */ + memcpy(ws->ks[nk].sid, e[i].object_id, 32); + ks_fields(out, &ws->ks[nk].seq, ws->ks[nk].prev); + nk++; } - if (at < 0) return SS_E_KEYSLOT; - if (e[at].byte_length > SS_KEYSLOT_MAX || e[at].byte_length > cap) return SS_E_KEYSLOT; - size_t l; + if (nk == 0) return SS_E_KEYSLOT; + int newest = ks_chain(ws, nk); + if (newest < 0) { memset(out, 0, cap); return SS_E_KEYSLOT; } /* GUARD:bootstrap-chain */ + at = find_entry(e, n, ws->ks[newest].sid); + if (at < 0) { memset(out, 0, cap); return SS_E_KEYSLOT; } rc = st_read_object(scratch, e[at].object_id, out, cap, &l); if (rc != 0) return rc; - if (keyslot_shape(out, l, NULL) != 0) { memset(out, 0, cap); return SS_E_KEYSLOT; } /* GUARD:bootstrap-shape */ *len = l; return 0; } @@ -264,7 +309,7 @@ static int verify_records(ss_store *s, uint64_t G) continue; } if (e[i].kind == SS_KIND_KEYSLOT && e[i].version == 1) { - if (nks) return SS_E_KEYSLOT; /* GUARD:keyslot-single */ + if (nks >= SS_KS_CHAIN_MAX) return SS_E_KEYSLOT; /* GUARD:keyslot-chain-cap */ nks++; continue; } @@ -298,6 +343,7 @@ static int verify_records(ss_store *s, uint64_t G) memcpy(s->last_tx_digest, G >= 2 ? ws->txs[ws->bygen[G - 2]].digest : zero, 32); /* envelopes: claims and envelope entries are in one-to-one correspondence */ memset(ws->used, 0, n); + uint32_t nk = 0; for (uint32_t k = 0; k < nc; k++) { int ks = ws->claims[k].c.obj.object_kind == SS_KIND_KEYSLOT; int at = find_entry(e, n, ws->claims[k].sid); @@ -312,13 +358,23 @@ static int verify_records(ss_store *s, uint64_t G) uint8_t dg[32]; sha256_hash(ws->rd, len, dg); if (!aienos_ct_equal(dg, ws->claims[k].c.object_id, 32)) return SS_E_KEYSLOT; /* GUARD:keyslot-digest */ - s->has_keyslot = 1; - memcpy(s->keyslot_sid, ws->claims[k].sid, 32); + if (keyslot_shape(ws->rd, len, s->uuid) != 0) return SS_E_KEYSLOT; /* GUARD:keyslot-open-shape */ + if (nk >= SS_KS_CHAIN_MAX) return SS_E_KEYSLOT; + memcpy(ws->ks[nk].sid, ws->claims[k].sid, 32); + ks_fields(ws->rd, &ws->ks[nk].seq, ws->ks[nk].prev); + nk++; continue; } if (m5_commit_check_object(&ws->claims[k].c, ws->rd, len) != M5_OK) return SS_E_ENVELOPE; /* GUARD:envelope-digest */ } if (nc != nenv + nks) return SS_E_ENVELOPE; /* GUARD:unclaimed-envelope */ + if (nk) { + int newest = ks_chain(ws, nk); + if (newest < 0) return SS_E_KEYSLOT; /* GUARD:keyslot-chain */ + s->has_keyslot = 1; + s->nkeyslot = nk; + memcpy(s->keyslot_sid, ws->ks[newest].sid, 32); + } s->nclaims = nc; return 0; } @@ -443,10 +499,21 @@ int ss_prepare(ss_store *s, const ss_object *o, size_t n) if (o[i].kind == 0 || o[i].version == 0) return SS_E_ARG; if (o[i].kind == SS_KIND_KEYSLOT) { /* the KeySlotManifest travels in plaintext: shape-checked here, - * one per store (rotation is a later cut), digest-bound by the - * commit record exactly like an envelope */ + * one per transaction, admitted only as the next link of the + * chain (sequence count + 1 naming the bound manifest; sequence + * 1 with a zero previous id on a Store without one), digest-bound + * by the commit record exactly like an envelope */ if (o[i].version != 1 || keyslot_shape(o[i].bytes, o[i].len, s->uuid) != 0) return SS_E_KEYSLOT; /* GUARD:keyslot-prepare-shape */ - if (s->has_keyslot || nks++) return SS_E_KEYSLOT; /* GUARD:keyslot-once */ + if (nks++) return SS_E_KEYSLOT; /* GUARD:keyslot-once-per-tx */ + { + uint64_t q; + uint8_t prev[32], zero[32]; + memset(zero, 0, 32); + ks_fields(o[i].bytes, &q, prev); + if (s->nkeyslot >= SS_KS_CHAIN_MAX) return SS_E_KEYSLOT; /* GUARD:keyslot-chain-full */ + if (q != (uint64_t)s->nkeyslot + 1) return SS_E_KEYSLOT; /* GUARD:keyslot-next-sequence */ + if (memcmp(prev, s->has_keyslot ? s->keyslot_sid : zero, 32) != 0) return SS_E_KEYSLOT; /* GUARD:keyslot-next-previous */ + } uint8_t *e = tx + SS_TX_HDR + i * SS_TX_ENTRY; if (sv1_object_id(SS_KIND_KEYSLOT, 1, o[i].bytes, o[i].len, e) != 0) return SS_E_CRYPTO; m5_commit mc; @@ -543,6 +610,7 @@ int ss_commit_prepared(ss_store *s, st_hook hook, void *arg) return SS_E_CRYPTO; if (c->c.obj.object_kind == SS_KIND_KEYSLOT) { s->has_keyslot = 1; + s->nkeyslot++; memcpy(s->keyslot_sid, c->sid, 32); } } diff --git a/native/store/store_sealed.h b/native/store/store_sealed.h index 4d083e05..6d69f3c8 100644 --- a/native/store/store_sealed.h +++ b/native/store/store_sealed.h @@ -42,11 +42,16 @@ #define SS_KIND_ENVELOPE 0x0510u /* M5 envelope of one application object */ #define SS_KIND_TXREC 0x0511u /* sealed transaction record */ -/* ADR 0017 KeySlotManifest: the one plaintext object a sealed store admits. - * It is committed inside a sealed transaction, so the keyed transaction - * record binds its digest; the unkeyed bootstrap read happens before keys - * exist and the keyed open re-checks the same bytes. */ +/* ADR 0017 KeySlotManifest: the one plaintext object kind a sealed store + * admits. It is committed inside a sealed transaction, so the keyed + * transaction record binds its digest; the unkeyed bootstrap read happens + * before keys exist and the keyed open re-checks the same bytes. Rotation + * (ADR 0017 section 2.7) appends a new manifest whose manifest_sequence is + * the count + 1 and whose previous_keyslot_manifest_id is the Store ObjectId + * of the manifest it supersedes: the manifests on a Store form one chain + * 1..N and the newest (N) is the bound one. At most one per transaction. */ #define SS_KIND_KEYSLOT 21u +#define SS_KS_CHAIN_MAX 64u /* manifests (genesis + rotations) a Store may hold */ #define SS_KEYSLOT_MIN 640u /* header + 4 descriptors */ #define SS_KEYSLOT_MAX SS_MAX_PLAINTEXT #define SS_MAX_OBJECTS 8u /* application objects per transaction */ @@ -73,7 +78,7 @@ enum { SS_E_CRYPTO = -312, /* sealing failed */ SS_E_NEEDS_REOPEN = -313, /* an anchor write failed; reopen */ SS_E_FORMAT_VERSION = -314, /* version-1 record (predictable envelope ids): reformat */ - SS_E_KEYSLOT = -315, /* keyslot manifest malformed, duplicate, unbound, or absent */ + SS_E_KEYSLOT = -315, /* keyslot manifest malformed, chain broken, unbound, or absent */ }; const char *ss_strerror(int e); @@ -121,6 +126,7 @@ typedef struct { size_t nobjs; ss_claim claims[ST_CAT_CAP]; ss_txinfo txs[ST_CAT_CAP]; + struct { uint8_t sid[32]; uint8_t prev[32]; uint64_t seq; } ks[SS_KS_CHAIN_MAX]; /* keyslot chain */ uint32_t bygen[ST_CAT_CAP]; /* generation - 2 -> index in txs */ uint8_t used[ST_CAT_CAP]; /* catalog entry already claimed */ } ss_workspace; @@ -136,15 +142,18 @@ typedef struct { uint32_t nclaims; int rb; /* SS_RB_* from the last open */ int needs_reopen; - int has_keyslot; /* a kind-21 KeySlotManifest is committed and bound */ - uint8_t keyslot_sid[32]; /* its Store object id */ + int has_keyslot; /* a kind-21 KeySlotManifest chain is committed and bound */ + uint8_t keyslot_sid[32]; /* Store object id of the newest manifest (sequence nkeyslot) */ + uint32_t nkeyslot; /* manifests in the chain (0 on a Store without one) */ } ss_store; -/* Unkeyed bootstrap read of the KeySlotManifest (kind 21) before any key - * exists: structural Store open into scratch, exactly one kind-21 object, - * magic and format version checked. SS_E_KEYSLOT when there is none. The - * keyed ss_open later re-binds the same bytes through the transaction - * record that committed them (has_keyslot, keyslot_sid). */ +/* Unkeyed bootstrap read of the newest KeySlotManifest (kind 21) before any + * key exists: structural Store open into scratch, every kind-21 object + * shape-checked (magic, format version) and the chain 1..N verified by + * manifest_sequence and previous_keyslot_manifest_id; out receives the + * newest. SS_E_KEYSLOT when there is none or the chain is broken. The keyed + * ss_open later re-binds the same bytes through the transaction records + * that committed them (has_keyslot, keyslot_sid, nkeyslot). */ int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *ws, uint8_t *out, size_t cap, size_t *len); diff --git a/native/store/tests/.crumb b/native/store/tests/.crumb index 0b6ea044..90015b16 100644 --- a/native/store/tests/.crumb +++ b/native/store/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T05:32:10Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:68639cafb167da617ac29e4caf4a1f7da653f9629d764c096e1992979cb557ce", + "digest": "sha256:0a09ffa7f22b22f32d3c23ca76645b1858d972e8575c3236120f0b747c856c05", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", - "source_tree": "sha256:416d15ec9b91a7c7688dcbf9a9d512f6c5061fbba59fbb70a82c0892e1ff0a67" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:4a6c64596771e62033c86d4cc05c120ffedcba85e3d7d2fe306ac9f01cb287c7" }, "owner_lane": "", "provenance": { diff --git a/native/store/tests/store_sealed_test.c b/native/store/tests/store_sealed_test.c index 3bfbd796..424d5ec4 100644 --- a/native/store/tests/store_sealed_test.c +++ b/native/store/tests/store_sealed_test.c @@ -600,7 +600,7 @@ static void cut_before_anchor(void *arg, int cp) /* ---- KeySlotManifest (kind 21): the one plaintext object, digest-bound ---- */ -static uint8_t KSM[SS_KEYSLOT_MIN], KSM2[SS_KEYSLOT_MIN], KSM_BAD[SS_KEYSLOT_MIN]; +static uint8_t KSM[SS_KEYSLOT_MIN], KSM2[SS_KEYSLOT_MIN], KSM3[SS_KEYSLOT_MIN], KSM_BAD[SS_KEYSLOT_MIN]; static void ksm_fill(uint8_t *m, uint64_t seq, const uint8_t uuid[16]) { memset(m, 0, SS_KEYSLOT_MIN); @@ -610,6 +610,18 @@ static void ksm_fill(uint8_t *m, uint64_t seq, const uint8_t uuid[16]) put64le(m + 28, seq); m[76] = 2; /* slot_count; descriptors stay Empty: the sealed layer checks shape only */ } +static void ksm_link(uint8_t *m, const uint8_t prev[32]) { memcpy(m + 36, prev, 32); } +/* Forgery: after prepare, flip the genesis manifest's previous id to nonzero + * and re-derive the Store ObjectId and the commit digest so every other + * guard passes and only the chain rule can refuse it. */ +static void fx_ks_prev_digest(m5_commit *c) { sha256_hash(KSM, sizeof KSM, c->object_id); } +static void ed_ks_prev_forged(ss_store *s, void *a) +{ + (void)a; + KSM[36] = 1; + sv1_object_id(SS_KIND_KEYSLOT, 1, KSM, sizeof KSM, s->ws->tx + SS_TX_HDR); + reseal(s, 0, fx_ks_prev_digest); +} static st_store ks_scratch; static int bootstrap_rc(uint8_t *out, size_t cap, size_t *len) { @@ -694,8 +706,41 @@ static void test_keyslot(void) SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, &kind), 0, "keyed read of the manifest"); CHECK(kind == SS_KIND_KEYSLOT && len == sizeof KSM && memcmp(out, KSM, len) == 0, "read returns plaintext"); SCHECK_EQ(check_read(&s, kid[1], P4, sizeof P4, 42), 0, "the envelope beside it opens"); + CHECK(s.nkeyslot == 1, "chain length 1"); + /* rotation chain (ADR 0017 section 2.7): the next manifest must be + * sequence count + 1 and name the bound manifest; anything else refused */ ss_object again = {SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}; - SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "a second manifest is refused while one is bound"); + SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "sequence 2 without the link to sequence 1 refused"); + ksm_fill(KSM2, 1, RIG_UUID); + SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "a second sequence-1 manifest refused"); + ksm_fill(KSM2, 2, RIG_UUID); + ksm_link(KSM2, kid[1]); + SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "sequence 2 naming an envelope, not the manifest, refused"); + ksm_link(KSM2, kid[0]); + uint8_t kid2[1][32], kid3[1][32]; + SCHECK_EQ(ss_transact(&s, &again, 1, NULL, NULL, kid2), 0, "gen 4 commits the sequence-2 manifest"); + CHECK(s.nkeyslot == 2 && memcmp(s.keyslot_sid, kid2[0], 32) == 0, "the newest manifest is bound after rotation"); + ss_object third = {SS_KIND_KEYSLOT, 1, KSM3, sizeof KSM3}; + ksm_fill(KSM3, 4, RIG_UUID); + ksm_link(KSM3, kid2[0]); + SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence skip (4 after 2) refused"); + ksm_fill(KSM3, 3, RIG_UUID); + ksm_link(KSM3, kid[0]); + SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence 3 naming sequence 1 refused"); + ksm_fill(KSM3, 2, RIG_UUID); + ksm_link(KSM3, kid2[0]); + SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence 2 again refused"); + ksm_fill(KSM3, 3, RIG_UUID); + ksm_link(KSM3, kid2[0]); + SCHECK_EQ(ss_transact(&s, &third, 1, NULL, NULL, kid3), 0, "gen 5 commits the sequence-3 manifest"); + CHECK(s.nkeyslot == 3 && memcmp(s.keyslot_sid, kid3[0], 32) == 0, "sequence 3 bound"); + rig_close(&r); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), 0, "bootstrap reads the chain"); + CHECK(len == sizeof KSM3 && memcmp(out, KSM3, len) == 0, "bootstrap returns the newest manifest"); + SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 5"); + CHECK(s.has_keyslot && s.nkeyslot == 3 && memcmp(s.keyslot_sid, kid3[0], 32) == 0, "chain of 3 bound at open"); + SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, &kind), 0, "the superseded manifest stays readable"); + CHECK(len == sizeof KSM && memcmp(out, KSM, len) == 0, "superseded bytes unchanged"); /* defence in depth at read: a claim whose digest no longer matches */ for (uint32_t k = 0; k < s.nclaims; k++) if (memcmp(g_ws.claims[k].sid, kid[0], 32) == 0) g_ws.claims[k].c.object_id[0] ^= 1; @@ -712,8 +757,16 @@ static void test_keyslot(void) CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); { ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {44, 1, P1, 300}}; - SCHECK_EQ(forge_objs(ed_ks_second, NULL, o, 2), SS_E_KEYSLOT, "two manifests in the catalog"); - SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses two manifests"); + ksm_fill(KSM2, 2, RIG_UUID); /* sequence 2 with no link */ + SCHECK_EQ(forge_objs(ed_ks_second, NULL, o, 2), SS_E_ENVELOPE, "a second manifest unclaimed in the catalog"); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses a broken chain (sequence 2 without its link)"); + } + CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); + { + ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {44, 1, P1, 300}}; + SCHECK_EQ(forge_objs(ed_ks_prev_forged, NULL, o, 2), SS_E_KEYSLOT, "claimed genesis manifest with a nonzero previous id (chain rule at the keyed open)"); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses it too"); + KSM[36] = 0; } CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); SCHECK_EQ(forge(ed_ks_unclaimed, NULL), SS_E_ENVELOPE, "unclaimed manifest"); diff --git a/scripts/.crumb b/scripts/.crumb index 6cb37738..dd38a699 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T09:21:41Z", + "compiled_at": "2026-10-12T10:28:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:2e11a9899bf4b8d979c08da48c4ab1610e58b4fcd6d33c190a9f9980147c4a71", + "digest": "sha256:8f2ba6515d449096eccaa96e1d710d5556e36425d1b82eb8ef096d85200059f9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 83, - "generated_at_commit": "b9baac8f34f2d2672c47de1ad892ad9bb16c174f", - "source_tree": "sha256:1e0e35389e09ebd623b2b785183347318fbfe3b704e9415d851cbcdf1c10d9bc" + "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", + "source_tree": "sha256:7988f7a7c826487fa7709995435d980b07c43f7788773a03e3394fc1be0c3517" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_lifecycle_test.sh b/scripts/qemu_ck_lifecycle_test.sh index 330dcdf1..d79c3fd4 100755 --- a/scripts/qemu_ck_lifecycle_test.sh +++ b/scripts/qemu_ck_lifecycle_test.sh @@ -38,7 +38,7 @@ # with a Store generation and object id; state back to ready # row 165 deny and cancel drop pending tasks; a second task while pending is # refused; approve with nothing pending is refused -# row 166 bounds: a 97-byte line is refused whole and never run; evidence +# row 166 bounds: a 145-byte line is refused whole and never run; evidence # shows tasks=3 approved=1 denied=1 cancelled=1 overlong=1 # row 167 stop order: store=closed, then dma_gate nvme bus master revoked, # then smmu stream back to abort, then stage lifecycle ok, then the @@ -99,6 +99,20 @@ # (boot 4) `sequence=2 generation=7 keyslot=bound previous=linked` (the # manifest read back verified under K_root_auth, bound the keyslot the # keyed open bound, carried key_epoch 1 and the manifest count) +# row 184 P04 cut 2b recovery secret rotation (boot 7, the boot-3 disk, so a +# second cold resume at generation 6): after `unlock <ok>` (boot +# record 7, continuity resume 8), `rotate <ok> <new>` proves +# possession of the current secret, then one transaction (generation +# 9) commits KeySlotManifest sequence 2 (K_vol re-wrapped under the +# new secret, linked to manifest 1) and SecurityManifest sequence 3 +# binding it; `lifecycle: rotate ok state=ready`; the typed line is +# echoed as 129 '*'; neither secret appears in the serial log +# row 185 P04 cut 2b after rotation (boot 8, the boot-7 disk): the old secret +# is refused by the keyslot tag (RecoverySecretRejected, disk not +# opened); the new secret opens the Store at generation 9, the keyed +# open binds the chain of two manifests, SecurityManifest sequence 4 +# links the rotation one (keyslot=bound previous=linked), continuity +# RESUMES incarnation 3; no SecurityManifest refusal # build the default (diagnostic) image carries no lifecycle; CK_SERVICE is # refused with CK_CONSOLE_SESSION, CK_QEMU_UNSAFE_DMA and a CK_TEST_* # flag; CK_SERVICE_TEST_KEYS is refused with CK_HARDWARE_STAGING @@ -166,6 +180,7 @@ ser_wait() { wait_for 180 "$1" '^report_kind: (panic|fault|final)' || true; } # QEMU-only recovery secrets (public test values, 32 bytes as 64 hex; never a production secret) secret_ok="000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" secret_bad="000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1eff" +secret_new="202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f" boot() { # name efi driver [nvme.img to reuse] work="${top}/$1" @@ -218,7 +233,7 @@ drive_ready() { ser_line approve ser_line "task to be denied"; ser_line deny ser_line "task to be cancelled"; ser_line cancel - ser_line "$(printf 'x%.0s' $(seq 97))" + ser_line "$(printf 'x%.0s' $(seq 145))" ser_line "remember prefer short notes"; ser_wait '^lifecycle: (remembered|REFUSED verb=remember)' ser_line facts; ser_wait '^lifecycle: (facts records=|facts none|REFUSED verb=facts)' ser_line evidence @@ -254,6 +269,22 @@ drive_infer() { drive_infer_recall() { ser_line status; ser_line recall; ser_line evidence; ser_line shutdown } +drive_rotate() { + ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' + ser_line "rotate ${secret_ok} ${secret_new}"; ser_wait '^(lifecycle: rotate ok|lifecycle: REFUSED verb=rotate)' + ser_line status + ser_line evidence + ser_line shutdown +} +drive_rotated() { + ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' + ser_line status + ser_line "unlock ${secret_new}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' + ser_line status + ser_line facts; ser_wait '^lifecycle: (facts records=|facts none|REFUSED verb=facts)' + ser_line evidence + ser_line shutdown +} drive_relock() { ser_line "unlock ${secret_bad}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' ser_line status @@ -292,7 +323,7 @@ rcheck 161 "task refused while locked" '^lifecycle: REFUSED verb=task state=lock rcheck 161 "unlock refused, still locked" '^lifecycle: REFUSED verb=unlock state=locked reason="production key source refused: still locked"' rcheck 161 "unlock asked the production key source again" '^store: unlock REFUSED proof=keyed step="production Store key source" rc=-2002' rcheck 161 "status answered" '^lifecycle: state=locked store=locked pending=- requests=' -rcheck 161 "help answered" '^lifecycle: verbs: status unlock \[<64-hex recovery secret>\] task <text> prompt <text> approve deny cancel recall remember <text> facts evidence help shutdown$' +rcheck 161 "help answered" '^lifecycle: verbs: status unlock \[<64-hex recovery secret>\] rotate <current 64-hex> <new 64-hex> task <text> prompt <text> approve deny cancel recall remember <text> facts evidence help shutdown$' rcheck 161 "evidence answered" '^lifecycle: evidence requests=5 refused=2 invalid=0 tasks=0 approved=0' rcheck 162 "stop order with no Store" '^lifecycle: stop order store=was-locked nvme=released state=stop requests=6$' before 162 "locked lifecycle ready < NVMe revoke (revoke only at the stop order)" '^lifecycle: ready state=locked' '^dma_gate: nvme bus master revoked' @@ -319,7 +350,7 @@ rcheck 165 "second task while pending refused" '^lifecycle: REFUSED verb=task st rcheck 165 "approve with nothing pending refused" '^lifecycle: REFUSED verb=approve state=ready reason="nothing pending"' rcheck 165 "deny drops" '^lifecycle: denied state=ready$' rcheck 165 "cancel drops" '^lifecycle: cancelled state=ready$' -rcheck 166 "overlong line refused whole" '^lifecycle: REFUSED verb=invalid reason="line longer than 96 bytes"$' +rcheck 166 "overlong line refused whole" '^lifecycle: REFUSED verb=invalid reason="line longer than 144 bytes"$' rcheck 166 "evidence counts" '^lifecycle: evidence requests=12 refused=2 invalid=0 tasks=3 approved=1 denied=1 cancelled=1 overlong=1 last_commit_gen=[0-9]+ last_id=[0-9a-f]{8} prompts=0 replies=0 gen_cancelled=0 gen_failed=0 recalls=0 inference=none remembers=1 facts=1 cortex=open records=1$' rcheck 167 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=13$' rcheck 179 "remember created the journal (header transaction, no records yet)" '^lifecycle: cortex open records=0 chunks=1 journal_bytes=32 store_gen=[0-9]+ heap_used_kib=[0-9]+ chain=ok$' @@ -410,6 +441,49 @@ m1 169 rabsent 170 "secret hex never in the serial log (resume boot)" "${secret_ok}" rabsent 170 "wrong secret hex never in the serial log" "${secret_bad}" +# --- boot 7 (runs right after boot 4): same disk, rotate the recovery secret (P04 cut 2b) --- +boot rotate "${service_efi}" drive_rotate "${unlock_img}" +rcheck 184 "lifecycle came up locked" '^lifecycle: ready state=locked store=locked' +rcheck 184 "store opened at generation 6 (the boot-3 disk), boot_count 1" '^store: opened generation=6 boot_count=1 prev_commit=[0-9a-f]+ anchor=valid-resume$' +rcheck 184 "boot transaction: security manifest sequence 2" '^store: security manifest sequence=2 generation=7 keyslot=bound previous=linked$' +rcheck 184 "unlock ok with the current secret" '^store: unlock ok source=recovery-keyslot slot=1 genesis=0 generation=7 boot_count=2$' +rcheck 184 "continuity RESUMED incarnation 2" "^continuity: RESUMED agent=${unlock_agent} incarnation=2 sequence=2 cortex=0 branches=1\$" +rcheck 184 "rotation committed: KeySlotManifest sequence 2, SecurityManifest sequence 3, one generation (9)" '^store: rotated recovery keyslot manifest_sequence=2 security_sequence=3 generation=9 \(previous secret retired\)$' +rcheck 184 "lifecycle rotate ok, state ready" '^lifecycle: rotate ok state=ready$' +rabsent 184 "rotate not refused" '^(lifecycle: REFUSED verb=rotate|store: rotate REFUSED)' +rcheck 184 "typed rotate line echoed as 129 '*'" '^> rotate \*{129}$' +rcheck 184 "status ready after rotation" "^lifecycle: state=ready store=open pending=- requests=3 continuity=resumed agent=${unlock_agent:0:16} incarnation=2 subject_seq=1\$" +rcheck 184 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=[0-9]+$' +rabsent 184 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 184 "QEMU exit 0"; else bad 184 "QEMU exit ${qemu_status}"; fi +m1 184 +rabsent 184 "current secret hex never in the serial log (rotation boot)" "${secret_ok}" +rabsent 184 "new secret hex never in the serial log (rotation boot)" "${secret_new}" +rotate_img="${top}/rotate/nvme.img" + +# --- boot 8 (before the inference boots 5 and 6): same disk, the old secret is retired, the new one resumes --- +boot rotated "${service_efi}" drive_rotated "${rotate_img}" +rcheck 185 "lifecycle came up locked" '^lifecycle: ready state=locked store=locked' +rcheck 185 "old secret refused by the keyslot tag of the newest manifest" '^store: unlock REFUSED proof=keyed step="recovery secret rejected \(K_vol unwrap tag failed\); disk not opened" rc=-2004 \(RecoverySecretRejected\); still LOCKED, disk not reformatted$' +rcheck 185 "lifecycle still locked after the old secret" '^lifecycle: REFUSED verb=unlock state=locked reason="production key source refused: still locked"$' +rcheck 185 "status still locked" '^lifecycle: state=locked store=locked pending=- requests=2 continuity=none$' +rcheck 185 "store opened at generation 9 (rotation committed), boot_count 2" '^store: opened generation=9 boot_count=2 prev_commit=[0-9a-f]+ anchor=valid-resume$' +rcheck 185 "security manifest sequence 4 links the rotation one, keyslot bound" '^store: security manifest sequence=4 generation=10 keyslot=bound previous=linked$' +rabsent 185 "no SecurityManifest refusal after rotation" '^store: (unlock )?REFUSED proof=keyed step="SecurityManifest' +rcheck 185 "new secret unlock ok, no genesis" '^store: unlock ok source=recovery-keyslot slot=1 genesis=0 generation=10 boot_count=3$' +rcheck 185 "continuity RESUMED incarnation 3" "^continuity: RESUMED agent=${unlock_agent} incarnation=3 sequence=3 cortex=0 branches=1\$" +rabsent 185 "no second genesis" '^continuity: (PROVISIONED|genesis)' +rabsent 185 "not reformatted" '^store: blank disk' +rcheck 185 "lifecycle unlocked to ready" '^lifecycle: unlock ok state=ready$' +rcheck 185 "the fact still answers from the disk after rotation" '^lifecycle: fact id=1 t=1 store_gen=6 text="prefer short notes"$' +rcheck 185 "evidence: one refused unlock" '^lifecycle: evidence requests=6 refused=1 invalid=0 ' +rcheck 185 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=7$' +rabsent 185 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 185 "QEMU exit 0"; else bad 185 "QEMU exit ${qemu_status}"; fi +m1 185 +rabsent 185 "old secret hex never in the serial log (post-rotation boot)" "${secret_ok}" +rabsent 185 "new secret hex never in the serial log (post-rotation boot)" "${secret_new}" + # --- boots 5 and 6: test-keys image with the resident inference session (P06 cut 2) --- infer_tag="AIENOS_CK_LIFECYCLE_INFER" infer_boots="" @@ -484,7 +558,7 @@ else fi if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then - for b in service testkeys unlock relock ${infer_boots}; do echo "---- ${b} serial (lifecycle lines) ----"; grep -E '^(lifecycle|store|continuity|allen|infer|stage lifecycle|dma_gate|smmu: nvme|report_kind|mm_usage)' "${top}/${b}/serial.txt" | head -80 || true; done + for b in service testkeys unlock relock rotate rotated ${infer_boots}; do echo "---- ${b} serial (lifecycle lines) ----"; grep -E '^(lifecycle|store|continuity|allen|infer|stage lifecycle|dma_gate|smmu: nvme|report_kind|mm_usage)' "${top}/${b}/serial.txt" | head -80 || true; done fi if [[ "${fail}" == 0 ]]; then echo "${final_tag}: PASS (QEMU only; hardware NOT_RUN)"; exit 0; fi echo "${final_tag}: FAIL"; exit 1