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#310 (code only)
Apply it
git checkout -b patch-f827b84e4c7ced63 6a1ec34a99d1fb735f148fe73449fb65ae539eb4 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/f827b84e4c7ced63.patch | git am
Provenance
github-import 1
aien-dev/aienux
310
c6c0d5c77fb4e79bcdb991230ecc935ed51d4c24
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":310,"url":"https://github.com/aien-dev/aienux/pull/310","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:11Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 2a217235..86c59a29 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "fabe7edbdb8877e2b15cd29bee691f924d8d502cd202b60686c046b3a5ca7aa2", - "last_commit": "e01be85", + "digest": "6098d439031bb73f0defd23222fe41ed8786929160f9285c456c10c70891361d", + "last_commit": "a9d120b", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "771f766c7b0d59e22e653b67bd60c4f7c8cb068f5ee7220be0e317aa1d385802", - "last_commit": "e01be85", + "digest": "bd21781b57471cd22c7d3a37f5ce588145ecc0ac05f5a75fde1028d406bd2d9c", + "last_commit": "a9d120b", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "220b980dcb68f58f243c7334f554634ff22438a535a770bee491dd57f0f8dc69", - "last_commit": "e01be85", + "digest": "9f409f70dc736ec25cc1fb97a221db84f7ac631b613bf50563740171fabc6983", + "last_commit": "a9d120b", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:3757c7204f15b29864d08af18bb37f141df1e300d53eda784115c46bda89fbac", - "compiled_at": "2026-10-12T04:51:22Z", + "children_root": "sha256:4bc63395eee8ae0cf3f86afe19d41c378a83395f8556f75d89a44cf9d1333f5f", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:208ee92a5e07cce0b7c4fb06c7b5e9e18c3f26c2402ead772df383d6eb67b9fe", + "digest": "sha256:2fbe81c18985c09dcff20c9db146d01f743621ea65ef720ce2a919b33d9b3a58", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", "files": 3073, - "generated_at_commit": "e01be85155f959705f2534cf07a7b66a03567404", - "source_tree": "sha256:befb2f29a6a28d8bed2ef14f02f70a3c6b180c8341c4f96412b78432c072587f" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:c24d4e737b9c52f845b889c98ac767f2bda59b9028d8542324b7b3e9b2836cb8" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index c9d5bc93..63965560 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -17,8 +17,8 @@ "generated": { "children": [ { - "digest": "3de6d16c0e46eb82e0eeb6de63973cc3ab3d995f18d9ad30eceac263d0209fa6", - "last_commit": "bf475a0", + "digest": "1a285e00bf8cae48459d19cda7b8915b086070f5cc165750ae739c319af7139c", + "last_commit": "a9d120b", "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:55bcc89f5282eb2e3f4e86d882abdfb5f4ea39e427ef97ef620b2267e5701eb1", - "compiled_at": "2026-10-12T04:06:57Z", + "children_root": "sha256:f8947b77a3ad5a0cef155aa9d8503c539aaad3bd8eac05404b62342ee7117cef", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:fabe7edbdb8877e2b15cd29bee691f924d8d502cd202b60686c046b3a5ca7aa2", + "digest": "sha256:6098d439031bb73f0defd23222fe41ed8786929160f9285c456c10c70891361d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 69, - "generated_at_commit": "51c05daff49b0836692eb1c777ec2758cf0746ae", - "source_tree": "sha256:24c323cc22254c282467f5877a44a60c1e7dd79161d5ba2b53c2193e02ab83db" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:c84b5e0fc18e3574fc7c03a837309c6f8d34576c3e204363010d1b0feabb1d3b" }, "owner_lane": "", "provenance": { diff --git a/docs/TRUST-1-M5-GATE-MATRIX.md b/docs/TRUST-1-M5-GATE-MATRIX.md index 079eff3a..39e79a3a 100644 --- a/docs/TRUST-1-M5-GATE-MATRIX.md +++ b/docs/TRUST-1-M5-GATE-MATRIX.md @@ -88,7 +88,7 @@ worst open item. | Requirement | Class now | Evidence and tier | Still owed | |---|---|---|---| | Owner-controlled key hierarchy | BLOCKED_OPERATOR | `m5_native_test` PASS (host; sources unchanged since 130aa20; CI Native suites success at 9d41efc) | Gate 3 ceremony and the real Owner Root | -| Sealed volume keys | MISSING_IMPLEMENTATION (the real TPM part is BLOCKED_HARDWARE) | swTPM policy model only. The kernel's production key source `ck_store_production_keys` is a refusing seam that returns `CK_SB_E_BLOCKED_OPERATOR` (`native/kernel/svc/store_boot.c`) | Product seal/unseal of K_vol; Gate 6 on real TPM | +| Sealed volume keys | PARTIAL: Slot 1 recovery unlock IMPLEMENTED in-kernel (P04 cut 1); TPM Slot 0 BLOCKED_HARDWARE | `ck_store_unlock_boot` (`native/kernel/svc/store_boot.c`): KeySlotManifest kind 21 committed inside the sealed Store, HKDF-SHA256 recovery KEK, K_vol wrapped/unwrapped with AES-256-GCM-SIV, PRODUCTION-class subkeys; host test `stage_test` `test_unlock_recovery`, QEMU rows 168-170. The TPM Slot 0 source `ck_store_production_keys` still refuses `CK_SB_E_BLOCKED_OPERATOR` | TPM seal/unseal of K_vol (Gate 6 on real TPM); key rotation; secman `keyslot_manifest_id` binding | | AES-256-GCM-SIV object envelopes | MISSING_IMPLEMENTATION for the production row; mechanism DONE on host and in QEMU with TEST keys | Host: `m5_store_encrypted_objects` PASS. QEMU: `M4_STORE` PASS (640522a `ck_gates`) | Real device, real keys | | Anti-rollback anchors | MISSING_IMPLEMENTATION | Anchor in its own region, host and QEMU (`M4_STORE`); the kernel opens it at every boot | TPM NV anchor (Gate 6); rolling back Store and anchor regions together still mounts | | Migration authorization | BLOCKED_OPERATOR | `m5_migration_sig_test_key` PASS (host, TEST keys) | Real owner signature from Gate 3 | @@ -172,7 +172,7 @@ The C sealed Store (`native/store/store_sealed.c`, lane 11) wires `native/m5` in | Requirement | Implementation on main | Evidence on main | Hardware / operator | Verdict | Remaining blocker | |---|---|---|---|---|---| | Owner-controlled key hierarchy | C: `native/m5` (HKDF subkeys, keyslot wrap/unwrap with suite/flag checks, root-auth MAC over the Rust-v1-layout SecurityManifest); Rust reference unchanged | `make -C native/m5 test` (t_subkeys_*, t_keyslot_*, t_secman_*) | Owner Root from Gate 3 | BLOCKED_OPERATOR | software done on the host; binding the store root-auth key to the real Gate 3 Owner Root needs Operator Step 8 (offline ceremony) | -| Sealed volume keys | swTPM policy model in `trust1_gate5_policy_sim.sh` (seal/unseal under PolicyAuthorize + PolicyNV); no product code seals K_vol | Gate 5 simulation selftest | real TPM (Gate 6) | MISSING_IMPLEMENTATION | product-side TPM seal/unseal of K_vol not written; real sealing also needs Gate 6 (attended) | +| Sealed volume keys | swTPM policy model in `trust1_gate5_policy_sim.sh` (seal/unseal under PolicyAuthorize + PolicyNV); Slot 1 recovery keyslot (raw secret, HKDF KEK) written in `native/kernel/svc/store_boot.c` `ck_store_unlock_boot` + `native/m5` manifest codec + `native/store` kind-21 binding (P04 cut 1); no product code seals K_vol under the TPM | Gate 5 simulation selftest; `stage_test` `test_unlock_recovery`; `native/m5` t_ksm_*, t_recovery_kek_vector; `native/store` test_keyslot; QEMU lifecycle rows 168-170 | real TPM (Gate 6) | PARTIAL (Slot 1 done, Slot 0 BLOCKED_HARDWARE) | product-side TPM seal/unseal of K_vol not written; real sealing also needs Gate 6 (attended) | | AES-256-GCM-SIV object envelopes | C: `native/crypto` (AES-256, POLYVAL, GCM-SIV, RFC 8452) + `native/m5` envelope (Rust V1 header, Standard Envelope AAD, all-or-nothing open) | `native/crypto` 1820 checks incl. 26 RFC 8452 vectors; `native/m5` corruption matrix (header, first/middle/last chunk, commit record, anchor, key generation, store generation, truncate, extend, reorder, splice) (host only; ASan/UBSan and mutants run at PR time, not in the receipt) | none | MISSING_IMPLEMENTATION | envelope mechanism done and host-tested; wired into the C sealed Store on the host (envelope corruption refused at mount and read, `evidence/trust1_m5_qualification_6fd36c3012ad792862e6098492ae78d7b091c5fada8af73c7c60e6a8f544a650.json`); QEMU-tested in the C kernel: the sealed Store opens in the C kernel boot path on QEMU virtual NVMe (TEST keys, `evidence/trust1_m5_qualification_3358bc2585f0982e1dfbcd0f5ed2fe077847332591782edff8974f0da9caeded.json`, row `ck_store_kernel_qemu`); envelope corruption is tested on the host only; physical NOT_RUN, not on Machine 1 | | Anti-rollback anchors | C: MAC'd anchor + evaluation in `native/m5`; persistence only through a caller buffer | t_rb_* (9 tests), t_corrupt_rollback_anchor | TPM NV for a hardware anchor | MISSING_IMPLEMENTATION | anchor persisted through torn_slot in its own region by the C sealed Store (host file-backed, power cut at every block, `evidence/trust1_m5_qualification_6fd36c3012ad792862e6098492ae78d7b091c5fada8af73c7c60e6a8f544a650.json`); QEMU-tested in the C kernel (anchor opened at every boot, `evidence/trust1_m5_qualification_3358bc2585f0982e1dfbcd0f5ed2fe077847332591782edff8974f0da9caeded.json`; the anchor shares the disk, rollback not tested there), physical NOT_RUN; rolling back the Store and anchor regions together still mounts, which needs a TPM NV anchor (Gate 6) | | Migration authorization | C: MAC-bound migration manifest (source store, generation, destination, counter) in `native/m5`; owner-signed migration record `native/m5/m5_owner_sig.c` (pure Ed25519 from `native/sig`) binding identity class, owner key id, agent root, source and destination store, source store generation, store format version, envelope count + envelope-set digest, manifest digest, monotonic counter, owner hierarchy generation; `m5_migration_authorize_owner` needs both; signing CLI `native/m5/tools/m5_migsig` takes a key file path and never creates keys | `make -C native/m5 test`: `m5_migsig_test` 13 tests (`AIENOS_M5_MIGRATION_SIG: PASS`; wrong key, every byte flip, field mismatch, unsigned, replayed/older counter, every truncation refused) + `m5_migsig_tool_test.sh` (fresh random TEST keys, OpenSSL cross-check) + t_mig_* (5 tests); host only, TEST keys only (ASan/UBSan and 57/57 mutants at PR time, not in the receipt); receipt `evidence/trust1_m5_qualification_f21f2c038946093a01f3d6553b5bc1a82d8e6ac0bfbb6b36ed4e7a932a9df709.json` (row `m5_migration_sig_test_key` PASS) | Owner Root from Gate 3 | BLOCKED_OPERATOR | host-tested with TEST keys only; the real owner signature needs the Gate 3 offline key ceremony (Operator Step 8), which also yields the trusted owner public key to provision; not yet called by the Store or boot path (needs the C disk layer) | diff --git a/docs/adr/.crumb b/docs/adr/.crumb index 42a058cb..9fbabf11 100644 --- a/docs/adr/.crumb +++ b/docs/adr/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-08T14:56:09Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:3de6d16c0e46eb82e0eeb6de63973cc3ab3d995f18d9ad30eceac263d0209fa6", + "digest": "sha256:1a285e00bf8cae48459d19cda7b8915b086070f5cc165750ae739c319af7139c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 22, - "generated_at_commit": "c4101915eb9f0babc01b25f62a86c5a2f00708e5", - "source_tree": "sha256:b0a6020e0f462cce3b3deeba994e2184a1def7e0c9608233312c5763c49920fe" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:5b7d0ec9086f3b8d5e0b557bb5eba5c26d64517bb3b28d0cc4aba61f35227b0e" }, "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 7b593004..c62dd5a9 100644 --- a/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md +++ b/docs/adr/0017-m5-key-hierarchy-and-encrypted-store.md @@ -158,6 +158,15 @@ reserved (16 B) --- +#### 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), key rotation (section 2.7), binding `keyslot_manifest_id` into the SecurityManifest, Argon2id profile. + ### 2.5 SecurityManifest (`kind = 22`) `SecurityManifest` is a 256-byte fixed-size Store v1 object that authenticates authorized logical state under `K_root_auth`: diff --git a/native/.crumb b/native/.crumb index 1d2e5fe4..d6c26210 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,14 +79,14 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "0da8cdbe6a21cf428fdf4f163e4bc2e80bcae3e2917a649b8b1c0d8280b425a2", - "last_commit": "e01be85", + "digest": "43d5aaa5344f8b3514ebe464c12e3ba1b74b2d3ded7e7dbed891b5d884567de2", + "last_commit": "a9d120b", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, { - "digest": "2a90bf4a27e33298c83bd86fe20ea532f113e4162f10dce8f37130afef0eeefd", - "last_commit": "181ea41", + "digest": "3883ad91cdafdb9158b14c4d8b11d4dcbfe1205ad8071c988b8f7c4b959a4004", + "last_commit": "a9d120b", "name": "m5", "purpose": "Directory native/m5. Purpose not yet described by a human." }, @@ -103,21 +103,21 @@ "purpose": "Directory native/sig. Purpose not yet described by a human." }, { - "digest": "8c0d84e2ac3642d2bf59a7faf91c6ddf7c27b4038ee020f243fade5385044efb", - "last_commit": "8555049", + "digest": "9146f4be830fe28ed14ce6bfac89cd97bd2043c825a60a00451152309e1f87ea", + "last_commit": "a9d120b", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:0ec3e69bfed1c735f349c6f640f590ef5611da4baaf18b9190ede741dd734cdb", - "compiled_at": "2026-10-12T04:51:22Z", + "children_root": "sha256:ac40341f41da03e1d5079c91920b80ac3be094e6035abf4d81d78b33192827d4", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:771f766c7b0d59e22e653b67bd60c4f7c8cb068f5ee7220be0e317aa1d385802", + "digest": "sha256:bd21781b57471cd22c7d3a37f5ce588145ecc0ac05f5a75fde1028d406bd2d9c", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 460, - "generated_at_commit": "e01be85155f959705f2534cf07a7b66a03567404", - "source_tree": "sha256:32b067f8cdec9462a479dd2c21816ca93c6f6d83da4d7e93541b4807f250888b" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:31c62df5a9e06b6886576242287883d70704900b1cae722c12116f22b51471c2" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index c5916675..f4c4660d 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": "6c183da30a2362e1e5c86b485cc6f6be46cfe9a9e30960f8f395e47659b18d42", - "last_commit": "adb4dd0", + "digest": "2c67028de54aa45d784364bf04b531a9160802bdea23fc1ee521b9e53173a00c", + "last_commit": "a9d120b", "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": "97067439083188400b87693964d28f9a06fb8b6cf3ff1271c4016ecbe80e5133", - "last_commit": "adb4dd0", + "digest": "3bca3564005f4581d6646e0b3ef797ec8157b80af1f1389e27e6e5089d1de042", + "last_commit": "a9d120b", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "c1ffd88afd48fbef6e14bcec2a9c1318e372ab31c01481f3fb60a8ed0b04a6e1", - "last_commit": "adb4dd0", + "digest": "08b0938d801a9dfeea54c16f1f585ff01e806ef264047b625e3d0555389087ac", + "last_commit": "a9d120b", "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:5cc12ebd8eba25b26b0dc719d258803dd234004d199c22427d41e9c53fa63725", - "compiled_at": "2026-10-12T04:51:22Z", + "children_root": "sha256:66e1de38e86954327f2760f0afb7d72a7fb669ba28f06136d111303185cb9499", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:0da8cdbe6a21cf428fdf4f163e4bc2e80bcae3e2917a649b8b1c0d8280b425a2", + "digest": "sha256:43d5aaa5344f8b3514ebe464c12e3ba1b74b2d3ded7e7dbed891b5d884567de2", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 296, - "generated_at_commit": "e01be85155f959705f2534cf07a7b66a03567404", - "source_tree": "sha256:65549349df470cfc371e44ef52ec3338a8c22ad153aed5738cdd62cca093e622" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:a690fc0ca1fea1283afc1c77c805e69e95d416aba7205c058a1196b0c74a63e9" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 3a7e1982..9d95b8fc 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -812,13 +812,16 @@ The product boundary. A `CK_SERVICE=1` image (`full-service/`, allowed with `CK_ | # | Check | CK gate | Status | |---|---|---|---| -| 160 | production service image: the lifecycle starts after `mm_usage`, `state=locked store=locked`; the Store stage refused with the production key source (`rc=-2002` BlockedOperator, disk not read) and released the NVMe before the lifecycle; no TEST key banner | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | -| 161 | locked: `task` and `unlock` REFUSED (state stays locked; `unlock` asks the production key source again and it refuses); `status`, `help`, `evidence` answered | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | -| 162 | locked shutdown: `stop order store=was-locked nvme=released`, then `stage lifecycle: ok`, then the final report; no panic or fault; QEMU exit 0; M1 checks | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 160 | production service image: the lifecycle starts after `mm_usage`, `state=locked store=locked`; the Store stage refused with the production key source (`rc=-2002` BlockedOperator, disk not read) and kept the NVMe bound for a later `unlock` (no bus master revoke before `lifecycle: ready state=locked`); no TEST key banner | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 161 | locked: `task` and a bare `unlock` REFUSED (state stays locked; bare `unlock` asks the TPM Slot 0 key source again and it refuses); `status`, `help`, `evidence` answered | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 162 | locked shutdown: `lifecycle: ready state=locked` < `dma_gate: nvme bus master revoked` < `stop order store=was-locked nvme=released`, then `stage lifecycle: ok`, then the final report; no panic or fault; QEMU exit 0; M1 checks | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | | 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 65-byte line 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 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) | | 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 is not yet read back after a cold boot (P05/P06); the production image cannot leave LOCKED until P04 lands the real K_vol path; the lifecycle loop polls with interrupts as the boot stages left them (no scheduler integration yet) | LIFECYCLE | LIMIT (declared) | +| 167b | limits: operator input is serial only (USB keyboard binding stays with the xHCI session code, later cut); a committed note is not yet read back after a cold boot (P05/P06); the lifecycle loop polls with interrupts as the boot stages left them (no scheduler integration yet) | LIFECYCLE | LIMIT (declared) | +| 168 | P04 cut 1, recovery keyslot GENESIS (production service image, blank disk): `unlock deadbeef` refused (`rc=-2004`, 64 hex digits expected); `unlock <64-hex secret>` draws K_vol/salt/nonce from the entropy service, wraps K_vol into the ADR 0017 Slot 1 Recovery_RawSecret descriptor (HKDF-SHA256 KEK) and commits the KeySlotManifest (kind 21, plaintext, bound by the sealed transaction record) in the same first transaction as the boot record: `formatted production store`, `key source=recovery-keyslot genesis=1`, `committed generation=2 boot_count=1`, `unlock ok ... genesis=1`, lifecycle READY; a note is approved and committed at generation 3; stop order closes the Store; QEMU exit 0; M1 checks | LIFECYCLE / TRUST-1 | QEMU PASS (hardware NOT_RUN) | +| 169 | P04 cut 1, recovery keyslot RESUME (same disk, cold boot): the wrong secret is refused by the keyslot AES-GCM-SIV tag (`RecoverySecretRejected rc=-2004`, disk not opened, not reformatted, lifecycle stays locked, `refused=1`); the right secret unwraps K_vol, derives PRODUCTION-class keys (owner hierarchy generation = active_key_epoch 1), the keyed open re-binds the manifest through the transaction record: `opened generation=3 boot_count=1 ... anchor=valid-resume`, `committed generation=4 boot_count=2`, `unlock ok ... genesis=0`; stop order closes the Store; QEMU exit 0; M1 checks | LIFECYCLE / TRUST-1 | QEMU PASS (hardware NOT_RUN) | +| 170 | secret hygiene and limits: the typed secret is echoed as 64 `*` and neither secret's hex appears in any serial log; no TEST key banner; no key material is compiled into the image (`ck_store_production_keys` still refuses `rc=-2002`: TPM Slot 0 BLOCKED_HARDWARE); no key rotation (`active_key_epoch` 1); the manifest is not yet bound into the secman `keyslot_manifest_id`; host rig (`stage_test` `test_unlock_recovery`): entropy refusal leaves a blank disk untouched, wrong secret and wrong provisioned uuid leave the image byte-identical, a TEST-key Store has no manifest (`NoKeyslotManifest rc=-2003`) | LIFECYCLE / TRUST-1 | QEMU PASS + LIMIT (declared) | diff --git a/native/kernel/README.md b/native/kernel/README.md index 9f82c6c7..89bf80b3 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -61,17 +61,28 @@ of them: uuid the boot Store uuid, and the boot log prints a SHA-256 fingerprint of the owner root key. Nothing in the kernel verifies with the owner root key yet. -- **Production Store keys are BLOCKED_OPERATOR.** Store keys derive from - K_vol (ADR 0017), which is secret and cannot come from public material. - `ck_store_production_keys` always refuses (`CK_SB_E_BLOCKED_OPERATOR`), so - the hardware staging Store stage prints `store: REFUSED proof=keyed - step="production Store key source"` and neither reads nor formats the disk. - To unblock it the operator must provide, through the TRUST-1 key ceremony: - the Gate 3 Owner Root (offline), and a K_vol source the kernel can unwrap - at boot: the Gate 5/6 TPM PolicyAuthorize key with the sealed Slot 0 KEK, - or the Slot 1 recovery KEK. The in-kernel keyslot unwrap is also not - written yet. `store_boot_run` refuses TEST-identity keys in a hardware - staging build before any disk access (host test `stage_test`). +- **Production Store keys: TPM Slot 0 is BLOCKED_HARDWARE, Slot 1 recovery + unlock is implemented (P04 cut 1).** Store keys derive from K_vol (ADR + 0017), which is secret and cannot come from public material. + `ck_store_production_keys` (the TPM Slot 0 path) still always refuses + (`CK_SB_E_BLOCKED_OPERATOR`), so a diagnostic hardware staging boot prints + `store: REFUSED proof=keyed step="production Store key source"` and neither + reads nor formats the disk. A `CK_SERVICE` image instead keeps the NVMe + bound and comes up LOCKED; the operator types `unlock <64-hex recovery + secret>` on the serial console and `ck_store_unlock_boot` + (`svc/store_boot.c`) runs the ADR 0017 section 2.4 Slot 1 path: on a blank + disk it draws K_vol, salt and nonce from the entropy service (refused when + RNDR is), wraps K_vol into a Recovery_RawSecret descriptor (HKDF-SHA256 + KEK) and commits the KeySlotManifest (kind 21) with the first boot record; + on a non-blank disk it reads the manifest before any key exists, unwraps + K_vol and opens the Store with PRODUCTION-class keys. A wrong secret fails + the AES-GCM-SIV tag before the disk is opened; the secret is echoed as `*` + and never printed. Under `CK_HARDWARE_STAGING` the manifest uuid must be + the provisioned store uuid. Limits: no TPM path, no key rotation + (`active_key_epoch` 1), manifest not yet bound into the secman + `keyslot_manifest_id`. `store_boot_run` refuses TEST-identity keys in a + hardware staging build before any disk access (host test `stage_test`, + including `test_unlock_recovery`; QEMU rows 168-170). - Proof: `scripts/ck_owner_keys_check.sh` (build only, takes the quiet flag) checks the refusals, the generator refusals, and builds the hardware staging image from the labelled TEST-fixture files in `tests/fixtures/owner/` diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 73c4476c..7b703bae 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-12T04:00:08Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:6c183da30a2362e1e5c86b485cc6f6be46cfe9a9e30960f8f395e47659b18d42", + "digest": "sha256:2c67028de54aa45d784364bf04b531a9160802bdea23fc1ee521b9e53173a00c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 42, - "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", - "source_tree": "sha256:ba44efeb611bb205e9e1954fecd0ee7c58cbe4cbc568b80337883289d3f8ca1a" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:08e6e7db7e8da50a46500f437f10687280037ed6491247260ac1d4e9d2983594" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c index ec1b466b..928f8229 100644 --- a/native/kernel/core/lifecycle.c +++ b/native/kernel/core/lifecycle.c @@ -48,7 +48,7 @@ void ck_dev_nvme_release(void); int ck_store_service_ready(void); int ck_store_service_close(void); -int ck_store_service_unlock(void); +int ck_store_service_unlock(const char *arg, size_t arg_len); static ck_lc g_lc; static uint8_t g_last_id[32]; @@ -57,7 +57,7 @@ static unsigned g_overlong, g_dropped; static void help(void) { - ck_puts("lifecycle: verbs: status unlock task <text> approve deny cancel evidence help shutdown\n"); + ck_puts("lifecycle: verbs: status unlock [<64-hex recovery secret>] task <text> approve deny cancel evidence help shutdown\n"); } static void status(void) @@ -110,7 +110,7 @@ static void handle_line(const char *line, size_t len) ck_lc_state_name(g_lc.state), g_lc.reason); return; case CK_LC_R_UNLOCK: { - int opened = ck_store_service_unlock(); + int opened = ck_store_service_unlock(arg, arg_len); ck_lc_unlock_done(&g_lc, opened); if (opened) ck_printf("lifecycle: unlock ok state=%s\n", ck_lc_state_name(g_lc.state)); else ck_printf("lifecycle: REFUSED verb=unlock state=%s reason=\"%s\"\n", ck_lc_state_name(g_lc.state), g_lc.reason); @@ -147,6 +147,19 @@ 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) +{ + static const char verb[] = "unlock"; + size_t i = 0, j; + while (i < len && buf[i] == ' ') i++; + for (j = 0; j < sizeof verb - 1; j++) + if (i + j >= len || buf[i + j] != verb[j]) return 0; + return i + j < len && buf[i + j] == ' '; +} + /* 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 * rest of it is dropped until the next Enter). Returns 1 when the line is complete. */ @@ -176,8 +189,7 @@ static int rx_line(char *buf, size_t *len, int *overflow) if (c < 0x20 || c > 0x7e) { g_dropped++; continue; } if (*overflow) continue; if (*len >= CK_LC_LINE_MAX) { *overflow = 1; continue; } - buf[(*len)++] = c; - { char e[2] = { c, 0 }; ck_puts(e); } + { char e[2] = { in_secret(buf, *len) ? '*' : c, 0 }; buf[(*len)++] = c; ck_puts(e); } } } diff --git a/native/kernel/core/lifecycle_fsm.c b/native/kernel/core/lifecycle_fsm.c index 9d0a9394..c8b1aaff 100644 --- a/native/kernel/core/lifecycle_fsm.c +++ b/native/kernel/core/lifecycle_fsm.c @@ -39,10 +39,12 @@ enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, siz if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; return CK_LC_V_TASK; } - if (*arg_len != 0) return CK_LC_V_INVALID; /* every other verb takes no argument */ + /* 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; + 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; - if (word_is(w, n, "unlock")) return CK_LC_V_UNLOCK; if (word_is(w, n, "approve")) return CK_LC_V_APPROVE; if (word_is(w, n, "deny")) return CK_LC_V_DENY; if (word_is(w, n, "cancel")) return CK_LC_V_CANCEL; diff --git a/native/kernel/core/lifecycle_fsm.h b/native/kernel/core/lifecycle_fsm.h index eb090fc2..a25870e7 100644 --- a/native/kernel/core/lifecycle_fsm.h +++ b/native/kernel/core/lifecycle_fsm.h @@ -38,7 +38,7 @@ enum ck_lc_verb { }; /* Bounds: one request line, and the task text that a line may carry. */ -#define CK_LC_LINE_MAX 64 +#define CK_LC_LINE_MAX 96 /* "unlock " + 64 hex = 71 bytes fits */ #define CK_LC_TASK_MAX 48 /* Outcome of one request against the state machine. */ diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index 99637e42..17e59568 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": "b44c6286f48a561179cc97442ad6fa4c368b53f802bad1b35a493b6a7ed037cf", - "last_commit": "98fbc3c", + "digest": "9c684a5898006e4b5ad249a723740c94ef783b84475ad949f69b30c255ace5ba", + "last_commit": "a9d120b", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:663de76750aa7914e1994534b4284995ddc98a54574ed3d29e0368506e9a0041", - "compiled_at": "2026-10-12T03:57:43Z", + "children_root": "sha256:2daefe867d6e169fa9fbbb2580e8cb53b4f1de2e5d097047195e38d406c53a71", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:97067439083188400b87693964d28f9a06fb8b6cf3ff1271c4016ecbe80e5133", + "digest": "sha256:3bca3564005f4581d6646e0b3ef797ec8157b80af1f1389e27e6e5089d1de042", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 35, - "generated_at_commit": "51c05daff49b0836692eb1c777ec2758cf0746ae", - "source_tree": "sha256:018b5ce1814892e07f893a5834f48f5967b793f81619f536cfbcad95c11cc074" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:6a5a010ba7dc7862fa7f94bf159f04831fe87d38c8859d3b6826360301a5166c" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index 08e151ec..95176967 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -154,6 +154,7 @@ static ss_store *g_ss; #ifdef CK_SERVICE static int g_service_open; /* Store open for the service lifetime (core/lifecycle.c) */ static int g_service_locked_rc; /* last production key source refusal */ +static const disk_dev *g_disk; /* boot disk kept bound while LOCKED, for a later unlock */ #endif int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, @@ -162,18 +163,16 @@ int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16 return store_boot_run_policy(d, keys, uuid, commit, CK_SB_HW, r); } -int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, - int hardware_staging, ck_store_report *r) +/* static: the open Store (g_ss) keeps pointers to these devices and is + * used after the boot returns (store_boot_store, the artifact read). */ +static st_disk g_sd; +static st_dev g_sdev; +static ts_device g_tdev; + +/* Geometry + workspace + device binding shared by the boot and the unlock. + * Fills the layout fields of r; no disk access. */ +static int bind_disk(const disk_dev *d, ck_store_report *r, uint32_t *bpu_out) { - bz(r, sizeof *r); - bcp(r->prev_commit, "none", 5); - if (!d || !keys || !uuid || !commit) return fail(r, "geometry", "arguments", -1); - r->identity_class = keys->identity_class; - /* Before any disk access: a hardware staging build never opens, and - * never formats a blank disk, under TEST-identity keys. */ - if (!ck_store_keys_admissible(keys, hardware_staging)) - return fail(r, "keyed", "identity class not admissible in this build (TEST keys refused on hardware)", - CK_SB_E_TEST_KEYS); if (d->block_size != 512 && d->block_size != 4096) return fail(r, "geometry", "block size", -1); uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; uint64_t units = d->block_count / bpu; @@ -188,30 +187,47 @@ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t if (!g_ws || !g_ss) return fail(r, "io", "allocate workspace", -1); bz(g_ss, sizeof *g_ss); - /* static: the open Store (g_ss) keeps pointers to these devices and is - * used after this returns (store_boot_store, the artifact read). */ - static st_disk sd; - static st_dev sdev; - static ts_device tdev; - int rc = st_disk_bind(&sd, d, r->store_base_lba, r->store_units, &sdev); + int rc = st_disk_bind(&g_sd, d, r->store_base_lba, r->store_units, &g_sdev); if (rc) return fail(r, "geometry", "st_disk_bind", rc); - ss_ts_device(d, &tdev); + ss_ts_device(d, &g_tdev); + *bpu_out = bpu; + return 0; +} + +/* The boot proper. genesis_obj (optional) is committed beside the boot record + * in the first transaction of a disk this run formatted (the KeySlotManifest + * of ck_store_unlock_boot); on a non-blank disk it is ignored. */ +static int run_policy_ex(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, + int hardware_staging, const ss_object *genesis_obj, ck_store_report *r) +{ + bz(r, sizeof *r); + bcp(r->prev_commit, "none", 5); + if (!d || !keys || !uuid || !commit) return fail(r, "geometry", "arguments", -1); + r->identity_class = keys->identity_class; + /* Before any disk access: a hardware staging build never opens, and + * never formats a blank disk, under TEST-identity keys. */ + if (!ck_store_keys_admissible(keys, hardware_staging)) + return fail(r, "keyed", "identity class not admissible in this build (TEST keys refused on hardware)", + CK_SB_E_TEST_KEYS); + uint32_t bpu = 0; + int rc = bind_disk(d, r, &bpu); + if (rc) return rc; int blank = 0; rc = is_blank(d, bpu, g_ws->rd, &blank); if (rc) return fail(r, "io", "blank check read", rc); if (blank) { - rc = ss_format(&sdev, &tdev, r->anchor_lba, uuid, keys, g_ws); + rc = ss_format(&g_sdev, &g_tdev, r->anchor_lba, uuid, keys, g_ws); if (rc) return fail(r, "commit", "format blank disk", rc); r->formatted = 1; } /* Proof 1: structural, unkeyed. */ - rc = st_open(&g_ss->st, &sdev, &g_ws->st); + rc = st_open(&g_ss->st, &g_sdev, &g_ws->st); if (rc) return fail(r, rc == ST_M_IO ? "io" : "structural", "st_open (unkeyed)", rc); /* Proofs 2 and 3: keyed M5 + anti-rollback anchor. */ bz(g_ss, sizeof *g_ss); - rc = ss_open(g_ss, &sdev, &tdev, r->anchor_lba, keys, g_ws); + rc = ss_open(g_ss, &g_sdev, &g_tdev, r->anchor_lba, keys, g_ws); if (rc) return fail(r, classify(rc), "ss_open (keyed + anchor)", rc); r->rb = g_ss->rb; r->gen_open = ss_generation(g_ss); @@ -248,11 +264,13 @@ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t put32(rec + 8, CK_BOOT_VERSION); put64(rec + 16, r->boot_count_new); for (size_t i = 0; i < CK_BOOT_COMMIT_MAX && commit[i]; i++) rec[24 + i] = (uint8_t)commit[i]; - ss_object obj = {CK_BOOT_KIND, CK_BOOT_VERSION, rec, sizeof rec}; + ss_object objs[2] = {{CK_BOOT_KIND, CK_BOOT_VERSION, rec, sizeof rec}, {0, 0, 0, 0}}; + size_t nobjs = 1; + if (genesis_obj && r->formatted) objs[nobjs++] = *genesis_obj; #ifdef CK_TEST_STORE_CRASH ck_store_crash_opened(r->gen_open); #endif - rc = ss_transact(g_ss, &obj, 1, CK_SB_HOOK, 0, 0); + rc = ss_transact(g_ss, objs, nobjs, CK_SB_HOOK, 0, 0); if (rc) return fail(r, "commit", "ss_transact", rc); rc = disk_flush(d); if (rc) return fail(r, "io", "flush", rc); @@ -262,6 +280,157 @@ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t return 0; } +int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, + int hardware_staging, ck_store_report *r) +{ + return run_policy_ex(d, keys, uuid, commit, hardware_staging, 0, r); +} + +/* ---- recovery keyslot unlock (ADR 0017 section 2.4, Slot 1) ---- */ + +/* PRODUCTION-class Store keys from K_vol: owner hierarchy generation is the + * manifest's active_key_epoch (1 in cut 1, no rotation yet). */ +static int production_keys_from_kvol(const uint8_t k_vol[32], uint64_t epoch, ss_keys *k) +{ + m5_subkeys sk; + bz(k, sizeof *k); + int rc = m5_derive_subkeys(k_vol, M5_ID_PRODUCTION, epoch, &sk); + if (rc) { + m5_subkeys_wipe(&sk); + return rc; + } + k->identity_class = M5_ID_PRODUCTION; + k->key_generation = epoch; + bcp(k->k_root_auth, sk.k_root_auth, 32); + bcp(k->k_domain, sk.k_artifact, 32); + m5_subkeys_wipe(&sk); + return 0; +} + +#define CK_RECOVERY_SLOT_ID 1u +#define CK_RECOVERY_EPOCH 1u + +static uint8_t *g_ksm_buf; /* manifest bytes (bootstrap read / genesis encode) */ + +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) +{ + ss_keys keys; + uint8_t k_vol[32], kek[32], uuid[16]; + m5_ksm m; + int rc, blank = 0; + uint32_t bpu = 0; + size_t mlen = 0; + + bz(r, sizeof *r); + bcp(r->prev_commit, "none", 5); + r->identity_class = M5_ID_PRODUCTION; + r->key_source = "recovery-keyslot"; + if (!d || !secret || !commit) return fail(r, "geometry", "arguments", -1); + if (!g_ksm_buf) g_ksm_buf = ck_alloc(SS_KEYSLOT_MAX); + if (!g_ksm_buf) return fail(r, "io", "allocate keyslot buffer", -1); + rc = bind_disk(d, r, &bpu); + if (rc) return rc; + rc = is_blank(d, bpu, g_ws->rd, &blank); + if (rc) return fail(r, "io", "blank check read", rc); + + bz(&m, sizeof m); + bz(&keys, sizeof keys); + if (blank) { + /* Genesis: fresh K_vol, salt and nonce from the kernel entropy service; + * refuse (no fallback) when RNDR refuses. The manifest goes into the + * same first transaction as the boot record. */ + uint8_t fresh[32 + 16 + 12 + 16]; + if (ck_entropy_fill(fresh, sizeof fresh) != 0) { + bz(fresh, sizeof fresh); + return fail(r, "keyed", "genesis entropy (K_vol, salt, nonce) refused by the entropy service", + CK_SB_E_ENTROPY); + } + bcp(k_vol, fresh, 32); + if (expect_uuid) bcp(uuid, expect_uuid, 16); + else bcp(uuid, fresh + 60, 16); + bcp(m.store_uuid, uuid, 16); + m.manifest_sequence = 1; + m.active_key_epoch = CK_RECOVERY_EPOCH; + m.slot_count = 2; /* index 0 reserved for the TPM slot (Empty), index 1 recovery */ + m5_keyslot *sl = &m.slots[1]; + sl->slot_type = M5_SLOT_RECOVERY_RAW_SECRET; + sl->wrap_suite = M5_WRAP_SUITE_GCMSIV; + sl->kdf_suite = 0x02; /* HKDF-SHA256 */ + sl->slot_id = CK_RECOVERY_SLOT_ID; + sl->key_epoch = CK_RECOVERY_EPOCH; + bcp(sl->salt, fresh + 32, 16); + rc = m5_recovery_kek(sl->salt, secret, CK_SB_SECRET_LEN, kek); + if (rc == 0) rc = m5_keyslot_wrap(sl, uuid, kek, k_vol, fresh + 48); + bz(fresh, sizeof fresh); + bz(kek, sizeof kek); + if (rc) { + bz(k_vol, sizeof k_vol); + return fail(r, "keyed", "wrap K_vol into the recovery keyslot", rc); + } + m5_ksm_encode(&m, g_ksm_buf); + mlen = M5_KSM_FIXED_LEN; + r->unlocked_genesis = 1; + } else { + /* Bootstrap: the manifest is read before any key exists, then the + * keyed open re-binds the same bytes through the transaction record. */ + rc = ss_keyslot_bootstrap(&g_ss->st, &g_sdev, g_ws, g_ksm_buf, SS_KEYSLOT_MAX, &mlen); + if (rc) return fail(r, rc == ST_M_IO ? "io" : "keyed", "no KeySlotManifest on a non-blank disk", rc == SS_E_KEYSLOT ? CK_SB_E_NO_KEYSLOT : rc); + rc = m5_ksm_decode(g_ksm_buf, mlen, &m); + if (rc) return fail(r, "keyed", "KeySlotManifest decode", CK_SB_E_KEYSLOT_BAD); + if (expect_uuid) { + int diff = 0; + for (size_t i = 0; i < 16; i++) diff |= m.store_uuid[i] ^ expect_uuid[i]; + if (diff) return fail(r, "keyed", "KeySlotManifest store_uuid is not this machine's", CK_SB_E_KEYSLOT_BAD); + } + bcp(uuid, m.store_uuid, 16); + const m5_keyslot *sl = 0; + 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) { + sl = &m.slots[i]; + break; + } + if (!sl) return fail(r, "keyed", "no Recovery_RawSecret (HKDF) descriptor in the manifest", CK_SB_E_KEYSLOT_BAD); + rc = m5_recovery_kek(sl->salt, secret, CK_SB_SECRET_LEN, kek); + if (rc == 0) rc = m5_keyslot_unwrap(sl, uuid, kek, k_vol); + bz(kek, sizeof kek); + if (rc) { + bz(k_vol, sizeof k_vol); + return fail(r, "keyed", "recovery secret rejected (K_vol unwrap tag failed); disk not opened", + CK_SB_E_SECRET_REJECTED); + } + /* The epoch the descriptor wrapped under is the active one in cut 1. */ + if (sl->key_epoch != m.active_key_epoch) { + bz(k_vol, sizeof k_vol); + return fail(r, "keyed", "descriptor key_epoch differs from active_key_epoch (rotation not supported)", + CK_SB_E_KEYSLOT_BAD); + } + } + rc = production_keys_from_kvol(k_vol, m.active_key_epoch, &keys); + bz(k_vol, sizeof k_vol); + if (rc) { + bz(&keys, sizeof keys); + return fail(r, "keyed", "derive PRODUCTION subkeys", rc); + } + { + ss_object ks = {M5_KSM_KIND, 1, g_ksm_buf, mlen}; + int genesis = r->unlocked_genesis; + rc = run_policy_ex(d, &keys, uuid, commit, hardware_staging, blank ? &ks : 0, r); + r->key_source = "recovery-keyslot"; + r->unlocked_genesis = genesis; + 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. */ + bz(&g_ss->keys, sizeof g_ss->keys); + rc = fail(r, "keyed", "KeySlotManifest not bound by the sealed transaction records", CK_SB_E_KEYSLOT_BAD); + } + } + bz(&keys, sizeof keys); + bz(g_ksm_buf, SS_KEYSLOT_MAX); + return rc; +} + static const char *rb_name(int rb) { return rb == SS_RB_VALID_RESUME ? "valid-resume" : rb == SS_RB_PREPARED_ADVANCE ? "prepared-advance" : @@ -274,6 +443,10 @@ static const char *err_name(int rc) if (rc <= -101 && rc >= -212) return st_strerror(rc); if (rc == CK_SB_E_TEST_KEYS) return "TestKeysRefused"; if (rc == CK_SB_E_BLOCKED_OPERATOR) return "BlockedOperator"; + if (rc == CK_SB_E_NO_KEYSLOT) return "NoKeyslotManifest"; + if (rc == CK_SB_E_SECRET_REJECTED) return "RecoverySecretRejected"; + if (rc == CK_SB_E_ENTROPY) return "EntropyRefused"; + if (rc == CK_SB_E_KEYSLOT_BAD) return "KeyslotManifestUnusable"; return "error"; } @@ -297,6 +470,9 @@ void store_boot_print(const ck_store_report *r) fmt_line = "store: blank disk (all-zero anchor + Store head): formatted production store\n"; } if (r->formatted) ck_puts(fmt_line); + if (r->key_source) + ck_printf("store: key source=%s genesis=%d (K_vol source: the Slot 1 recovery descriptor; " + "TPM Slot 0 BLOCKED_HARDWARE; no rotation)\n", r->key_source, r->unlocked_genesis); if (r->verdict != CK_SB_COMMITTED) { ck_printf("store: REFUSED proof=%s step=\"%s\" rc=%d (%s); disk left as found, not reformatted\n", r->proof, r->step, r->rc, err_name(r->rc)); @@ -344,7 +520,14 @@ int ck_stage_store(void) "disk not read, not formatted\n", rc); ck_art_stage_load(0, "production Store key source BLOCKED_OPERATOR"); +#ifdef CK_SERVICE + g_service_locked_rc = rc; + g_disk = d; + ck_puts("store: service lifetime: Store LOCKED, NVMe stays bound for `unlock <64-hex recovery secret>` " + "or the lifecycle stop order\n"); +#else ck_dev_nvme_release(); +#endif return rc; } rc = store_boot_run(d, &keys, ck_owner_store_uuid, commit ? commit : "unknown", &r); @@ -375,8 +558,10 @@ int ck_stage_store(void) "K_vol needs the TRUST-1 key ceremony, Gate 6 TPM keyslot or the operator recovery keyslot); " "disk not read, not formatted\n", rc); ck_art_stage_load(0, "production Store key source BLOCKED_OPERATOR"); - ck_dev_nvme_release(); g_service_locked_rc = rc; + g_disk = d; + ck_puts("store: service lifetime: Store LOCKED, NVMe stays bound for `unlock <64-hex recovery secret>` " + "or the lifecycle stop order\n"); (void)commit; return rc; #else @@ -419,18 +604,74 @@ int ck_store_service_close(void) return 1; } -/* "unlock" while LOCKED: ask the production key source again. Today it - * refuses (CK_SB_E_BLOCKED_OPERATOR); the Store is never opened with any - * other key here. Returns 1 only when the Store is open. */ -int ck_store_service_unlock(void) +static int hex_nibble(char c) +{ + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +/* "unlock" while LOCKED. With no argument the production key source is asked + * again and refuses (CK_SB_E_BLOCKED_OPERATOR: no TPM Slot 0 path). With the + * operator's 64-hex recovery secret the Slot 1 keyslot path runs on the disk + * kept bound at boot (ck_store_unlock_boot). The secret bytes are wiped + * before return and never printed. Returns 1 only when the Store is open. */ +int ck_store_service_unlock(const char *arg, size_t arg_len) { if (g_service_open) return 1; - ss_keys keys; - int rc = ck_store_production_keys(&keys); - bz(&keys, sizeof keys); - ck_printf("store: unlock REFUSED proof=keyed step=\"production Store key source\" rc=%d (%s)\n", rc ? rc : g_service_locked_rc, - "BlockedOperator: no in-kernel K_vol unwrap yet"); - return 0; + if (arg_len == 0) { + ss_keys keys; + int rc = ck_store_production_keys(&keys); + bz(&keys, sizeof keys); + ck_printf("store: unlock REFUSED proof=keyed step=\"production Store key source\" rc=%d (%s)\n", + rc ? rc : g_service_locked_rc, "BlockedOperator: no TPM Slot 0 unwrap; give `unlock <64-hex recovery secret>`"); + return 0; + } + uint8_t secret[CK_SB_SECRET_LEN]; + if (arg_len != 2 * CK_SB_SECRET_LEN) { + ck_printf("store: unlock REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (64 hex digits expected, got %u)\n", + CK_SB_E_SECRET_REJECTED, (unsigned)arg_len); + return 0; + } + for (size_t i = 0; i < CK_SB_SECRET_LEN; i++) { + int hi = hex_nibble(arg[2 * i]), lo = hex_nibble(arg[2 * i + 1]); + if (hi < 0 || lo < 0) { + bz(secret, sizeof secret); + ck_printf("store: unlock REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (not hex)\n", + CK_SB_E_SECRET_REJECTED); + return 0; + } + secret[i] = (uint8_t)((hi << 4) | lo); + } + if (!g_disk) { + bz(secret, sizeof secret); + ck_printf("store: unlock REFUSED proof=io step=\"no boot disk bound\" rc=%d\n", g_service_locked_rc); + return 0; + } + ck_store_report r; + const char *commit = ck_commit(); +#ifdef CK_HARDWARE_STAGING + const uint8_t *expect = ck_owner_store_uuid; +#else + const uint8_t *expect = 0; +#endif + int rc = ck_store_unlock_boot(g_disk, secret, expect, commit ? commit : "unknown", CK_SB_HW, &r); + bz(secret, sizeof secret); + store_boot_print(&r); + if (rc || r.verdict != CK_SB_COMMITTED) { + ck_printf("store: unlock REFUSED proof=%s step=\"%s\" rc=%d (%s); still LOCKED, disk not reformatted\n", + r.proof, r.step, r.rc, err_name(r.rc)); + g_service_locked_rc = r.rc; + return 0; + } + g_service_open = 1; + ck_printf("store: unlock ok source=recovery-keyslot slot=%u genesis=%d generation=%llu boot_count=%llu\n", + (unsigned)CK_RECOVERY_SLOT_ID, r.unlocked_genesis, (unsigned long long)r.gen_commit, + (unsigned long long)r.boot_count_new); + /* Signed artifact candidates live in this Store; read them now that it is open. */ + ck_art_stage_load(g_ss, r.step); + return 1; } #endif diff --git a/native/kernel/svc/store_boot.h b/native/kernel/svc/store_boot.h index 39ba8b22..162f7240 100644 --- a/native/kernel/svc/store_boot.h +++ b/native/kernel/svc/store_boot.h @@ -20,7 +20,23 @@ * BLOCKED_OPERATOR seam (K_vol is secret and needs the TRUST-1 key * ceremony), so the stage refuses before reading the disk. store_boot_run * refuses TEST-identity keys in that build before any disk access, so it - * never formats a blank disk with them. */ + * never formats a blank disk with them. + * + * RECOVERY KEYSLOT UNLOCK (P04 cut 1, ADR 0017 section 2.4 Slot 1). + * ck_store_unlock_boot takes the operator's 32-byte raw recovery secret and + * never any key material from the image: on a blank disk it draws K_vol, + * salt and nonce from the kernel entropy service (RNDR; refused when RNDR + * is), wraps K_vol into a Recovery_RawSecret descriptor (slot_id 1, HKDF + * KEK) and commits the KeySlotManifest (kind 21, plaintext, bound by the + * sealed transaction record) in the same first transaction as the boot + * record. On a non-blank disk it reads the manifest before any key exists + * (ss_keyslot_bootstrap), derives the KEK, unwraps K_vol and opens the Store + * with PRODUCTION-class keys (owner hierarchy generation = active_key_epoch). + * A wrong secret is refused by the AES-GCM-SIV tag before the Store is + * touched. K_vol lives in a stack buffer for the unwrap only and is wiped. + * Limits (cut 1): TPM Slot 0 stays BLOCKED_HARDWARE, no key rotation + * (active_key_epoch 1), the manifest is not yet bound into the secman + * keyslot_manifest_id field. */ #ifndef AIENOS_CK_STORE_BOOT_H #define AIENOS_CK_STORE_BOOT_H #include <stdint.h> @@ -46,10 +62,17 @@ typedef struct { char prev_commit[CK_BOOT_COMMIT_MAX + 1]; uint64_t store_base_lba, store_units, anchor_lba; uint8_t identity_class; /* identity class of the keys given (M5_ID_*) */ + int unlocked_genesis; /* ck_store_unlock_boot: this run created the keyslot manifest */ + const char *key_source; /* ck_store_unlock_boot: "recovery-keyslot" */ } ck_store_report; #define CK_SB_E_TEST_KEYS (-2001) /* TEST identity keys refused (hardware staging) */ #define CK_SB_E_BLOCKED_OPERATOR (-2002) /* production key source needs the operator */ +#define CK_SB_E_NO_KEYSLOT (-2003) /* non-blank disk carries no KeySlotManifest (not a production Store) */ +#define CK_SB_E_SECRET_REJECTED (-2004) /* recovery secret did not unwrap K_vol (tag failed) */ +#define CK_SB_E_ENTROPY (-2005) /* genesis refused: kernel entropy service refused */ +#define CK_SB_E_KEYSLOT_BAD (-2006) /* manifest decoded but has no usable recovery descriptor / wrong uuid */ +#define CK_SB_SECRET_LEN 32u /* TEST keys and store uuid. Public, not secret, never production. Not * compiled in a CK_HARDWARE_STAGING build. */ @@ -75,6 +98,13 @@ int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16 * CK_HARDWARE_STAGING build, else 0); the host test drives both. */ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, int hardware_staging, ck_store_report *r); +/* Recovery keyslot unlock + boot (see the header comment). secret is the + * operator's raw 32-byte recovery secret. expect_uuid (optional) must equal + * the manifest's store_uuid and is the uuid of a formatted blank disk; when + * NULL a blank disk gets a random uuid and any manifest uuid is accepted. + * 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); /* 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 901b913f..2c64060d 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-09T02:37:36Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:b44c6286f48a561179cc97442ad6fa4c368b53f802bad1b35a493b6a7ed037cf", + "digest": "sha256:9c684a5898006e4b5ad249a723740c94ef783b84475ad949f69b30c255ace5ba", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", - "source_tree": "sha256:ed6229a0470e3f5fea11ec069737aac7d38d08cd374479243af9bb19b6aa4389" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:8b71d730fb8592983dab48f46313e6ebabe62dbdde1b9736e6f8375a8339d46a" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/ck_host.c b/native/kernel/svc/tests/ck_host.c index 52541719..801e237f 100644 --- a/native/kernel/svc/tests/ck_host.c +++ b/native/kernel/svc/tests/ck_host.c @@ -210,6 +210,20 @@ int ck_compat_entropy_source(void) { return CK_ENTROPY_HOST; } /* Host: the kernel RNDR service is not in play (libc /dev/urandom above). */ int ck_entropy_status(void) { return 0; } const char *ck_entropy_reason(void) { return "host-urandom"; } +/* Host: deterministic fill (a counter), so key-slot genesis tests are reproducible. + * ck_host_entropy_refuse = 1 models an RNDR refusal (fill returns -1, buffer zeroed). */ +int ck_host_entropy_refuse; +int ck_entropy_fill(void *buf, size_t len) +{ + static uint32_t ctr = 0x1000; + uint8_t *b = buf; + if (ck_host_entropy_refuse) { + for (size_t i = 0; i < len; i++) b[i] = 0; + return -1; + } + for (size_t i = 0; i < len; i++) b[i] = (uint8_t)(0xA5u ^ (ctr++ * 2654435761u >> 24)); + return 0; +} /* Host: no IORT by default, so no SMMU (the NVMe gate stays fail-closed). A test may set * ck_host_confine_rc = 0 to model an SMMU that confines; every call is recorded. */ diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index 638ab5b3..25ce962e 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -1066,6 +1066,130 @@ static void test_store(void) /* ---------------- security ---------------- */ +/* P04 cut 1: recovery keyslot unlock (ADR 0017 section 2.4 Slot 1) over the + * real sealed Store in a file. No key ever comes from the image: K_vol is + * drawn at genesis (host: deterministic counter fill) and wrapped under the + * HKDF KEK of the operator's raw secret. */ +extern int ck_host_entropy_refuse; +static int unlock(const uint8_t *S, const uint8_t *uuid, const char *commit, int hw, ck_store_report *r) +{ + if (dopen(0)) return -999; + int rc = ck_store_unlock_boot(&g_d, S, uuid, commit, hw, r); + store_boot_print(r); + dclose(); + return rc; +} +static size_t nonzero(const uint8_t *b, size_t n) +{ + size_t nz = 0; + for (size_t i = 0; i < n; i++) nz += b[i] != 0; + return nz; +} +static void test_unlock_recovery(void) +{ + ck_store_report r; + uint8_t S[32], W[32], U[16], V[16]; + for (int i = 0; i < 32; i++) S[i] = (uint8_t)(i * 7 + 1); + memcpy(W, S, 32); + W[0] ^= 1; + for (int i = 0; i < 16; i++) { U[i] = (uint8_t)(0x40 + i); V[i] = (uint8_t)(0x80 + i); } + uint8_t *now = malloc(T_BYTES), *after = malloc(T_BYTES); + CHECK(now && after); + if (!now || !after) return; + + printf(" [unlock: production key source still BLOCKED_OPERATOR]\n"); + { + ss_keys pk; + memset(&pk, 0xEE, sizeof pk); + CHECK(ck_store_production_keys(&pk) == CK_SB_E_BLOCKED_OPERATOR && nonzero((const uint8_t *)&pk, sizeof pk) == 0); + } + + printf(" [unlock: blank disk, entropy refused -> no genesis, disk untouched]\n"); + CHECK(dopen(1) == 0); + dclose(); + ck_host_entropy_refuse = 1; + int rc = unlock(S, 0, "unlock-0", 0, &r); + ck_host_entropy_refuse = 0; + CHECK(rc == CK_SB_E_ENTROPY && r.verdict == CK_SB_REFUSED && r.formatted == 0 && r.unlocked_genesis == 0); + CHECK(r.proof && strcmp(r.proof, "keyed") == 0); + CHECK(load(now) == 0 && nonzero(now, T_BYTES) == 0); + + printf(" [unlock: blank disk, genesis: keyslot manifest + boot record in one transaction]\n"); + rc = unlock(S, 0, "unlock-1", 0, &r); + CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && r.unlocked_genesis == 1); + CHECK(r.identity_class == M5_ID_PRODUCTION && r.boot_count_prev == 0 && r.boot_count_new == 1); + CHECK(r.gen_open == 1 && r.gen_commit == 2 && r.rb == SS_RB_VALID_RESUME); /* ss_format commits the gen-1 anchor */ + CHECK(r.key_source && strcmp(r.key_source, "recovery-keyslot") == 0); + CHECK(store_boot_store()->has_keyslot == 1 && store_boot_store()->keys.identity_class == M5_ID_PRODUCTION); + + printf(" [unlock: reboot, same secret, hardware policy: K_vol unwrapped, no genesis]\n"); + rc = unlock(S, 0, "unlock-2", 1, &r); + CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0 && r.unlocked_genesis == 0); + CHECK(r.boot_count_prev == 1 && r.boot_count_new == 2 && strcmp(r.prev_commit, "unlock-1") == 0); + CHECK(r.gen_open == 2 && r.gen_commit == 3 && r.rb == SS_RB_VALID_RESUME); + CHECK(store_boot_store()->has_keyslot == 1); + { + /* the open Store serves the lifecycle: one sealed note, then it is back after a reboot */ + static const uint8_t note[] = "aienos.note.v1\nkeyslot test"; + ss_object o = {0x4e31, 1, note, sizeof note - 1}; + uint8_t ids[1][32]; + CHECK(dopen(0) == 0); + CHECK(ss_transact(store_boot_store(), &o, 1, 0, 0, ids) == 0); + dclose(); + rc = unlock(S, 0, "unlock-3", 0, &r); + CHECK(rc == 0 && r.gen_open == 4 && r.boot_count_prev == 2); + uint8_t rd[64]; + size_t len = 0; + uint16_t kind = 0; + CHECK(dopen(0) == 0); + CHECK(ss_read(store_boot_store(), ids[0], rd, sizeof rd, &len, &kind) == 0 && kind == 0x4e31 && + len == sizeof note - 1 && memcmp(rd, note, len) == 0); + dclose(); + } + + printf(" [unlock: wrong secret refused by the keyslot tag, disk unchanged]\n"); + CHECK(load(now) == 0); + rc = unlock(W, 0, "unlock-bad", 0, &r); + CHECK(rc == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED && r.formatted == 0); + CHECK(r.proof && strcmp(r.proof, "keyed") == 0 && r.rc == CK_SB_E_SECRET_REJECTED); + CHECK(load(after) == 0 && memcmp(now, after, T_BYTES) == 0); + + printf(" [unlock: manifest uuid is not the provisioned one, disk unchanged]\n"); + rc = unlock(S, V, "unlock-uuid", 1, &r); + CHECK(rc == CK_SB_E_KEYSLOT_BAD && r.verdict == CK_SB_REFUSED); + CHECK(load(after) == 0 && memcmp(now, after, T_BYTES) == 0); + + printf(" [unlock: a TEST-key Store has no keyslot manifest: NoKeyslotManifest, disk unchanged]\n"); + { + ss_keys k; + ck_store_test_keys(&k); + CHECK(dopen(1) == 0); + dclose(); + CHECK(boot(&k, "test-one", &r) == 0 && r.formatted == 1); + CHECK(load(now) == 0); + rc = unlock(S, 0, "unlock-test", 0, &r); + CHECK(rc == CK_SB_E_NO_KEYSLOT && r.verdict == CK_SB_REFUSED && r.rc == CK_SB_E_NO_KEYSLOT); + CHECK(load(after) == 0 && memcmp(now, after, T_BYTES) == 0); + } + + printf(" [unlock: provisioned uuid at genesis, then checked on every unlock]\n"); + CHECK(dopen(1) == 0); + dclose(); + rc = unlock(S, U, "unlock-prov", 1, &r); + CHECK(rc == 0 && r.formatted == 1 && r.unlocked_genesis == 1); + CHECK(memcmp(store_boot_store()->uuid, U, 16) == 0); + rc = unlock(S, V, "unlock-prov-2", 1, &r); + CHECK(rc == CK_SB_E_KEYSLOT_BAD); + rc = unlock(S, U, "unlock-prov-3", 1, &r); + CHECK(rc == 0 && r.formatted == 0 && r.boot_count_prev == 1 && strcmp(r.prev_commit, "unlock-prov") == 0); + rc = unlock(S, 0, "unlock-prov-4", 0, &r); + CHECK(rc == 0 && r.boot_count_prev == 2); /* no expectation: any manifest uuid */ + + unlink(img); + free(now); + free(after); +} + static void test_security(void) { ck_sec_report r; @@ -2133,6 +2257,7 @@ int main(void) test_mmio_unmap(); test_mmio_stage_report(); test_store(); + test_unlock_recovery(); test_artifact_store(); test_security(); test_nvme_shutdown(); diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index a54efeda..cbe75f01 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-12T04:00:08Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:c1ffd88afd48fbef6e14bcec2a9c1318e372ab31c01481f3fb60a8ed0b04a6e1", + "digest": "sha256:08b0938d801a9dfeea54c16f1f585ff01e806ef264047b625e3d0555389087ac", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 150, - "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", - "source_tree": "sha256:f6cbc8239856f043ddb0f3e67cc681c10c453efe9ebf9df3f4ddc7f8ee9703e7" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:19e2d9e99bdd9fe265da5c4d84140d7f0b9cc52ae60e19838e2cfcb8cf64f8d5" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_lifecycle.c b/native/kernel/tests/test_lifecycle.c index 710c761d..5dcdeef5 100644 --- a/native/kernel/tests/test_lifecycle.c +++ b/native/kernel/tests/test_lifecycle.c @@ -24,16 +24,19 @@ int main(void) CHECK(dec("task padded ", &a, &al) == CK_LC_V_TASK && al == 6 && memcmp(a, "padded", 6) == 0); CHECK(dec("task", &a, &al) == CK_LC_V_INVALID); /* task needs text */ CHECK(dec("status now", &a, &al) == CK_LC_V_INVALID); /* no argument allowed */ + 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(dec("frobnicate", &a, &al) == CK_LC_V_UNKNOWN); CHECK(dec("sta\x01tus", &a, &al) == CK_LC_V_INVALID); /* unprintable */ { char big[CK_LC_LINE_MAX + 2]; memset(big, 'x', sizeof big - 1); big[sizeof big - 1] = 0; - CHECK(dec(big, &a, &al) == CK_LC_V_INVALID); /* 65 bytes refused whole */ + CHECK(dec(big, &a, &al) == CK_LC_V_INVALID); /* CK_LC_LINE_MAX + 1 bytes refused whole */ char t[CK_LC_LINE_MAX + 1]; memset(t, 'y', CK_LC_LINE_MAX); t[CK_LC_LINE_MAX] = 0; memcpy(t, "task ", 5); - CHECK(dec(t, &a, &al) == CK_LC_V_INVALID && al == CK_LC_LINE_MAX - 5); /* text 59 > 48 */ + CHECK(dec(t, &a, &al) == CK_LC_V_INVALID && al == CK_LC_LINE_MAX - 5); /* text 91 > 48 */ } CHECK(strcmp(ck_lc_verb_name(CK_LC_V_SHUTDOWN), "shutdown") == 0 && strcmp(ck_lc_state_name(CK_LC_SERVE), "serve") == 0); diff --git a/native/m5/.crumb b/native/m5/.crumb index 5ca9d494..7c787da8 100644 --- a/native/m5/.crumb +++ b/native/m5/.crumb @@ -17,8 +17,8 @@ "generated": { "children": [ { - "digest": "90460ee7f9dd6e3e0a930d07ca84ab3e97ee714e368f24c3c87ab78c1a84f65d", - "last_commit": "ec6ebd6", + "digest": "14c3d97cc49d279cf57a819bf7aa2c75c8bcd3d0071b302f352ae6ddda74f2b6", + "last_commit": "a9d120b", "name": "tests", "purpose": "Directory native/m5/tests. Purpose not yet described by a human." }, @@ -29,15 +29,15 @@ "purpose": "Directory native/m5/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:ccde2eab22cf4aead63c14ac3127e3dfca7ba0984ca8443cce3cc8b915f19b23", - "compiled_at": "2026-10-04T13:47:18Z", + "children_root": "sha256:b461ac42b9135cc73e941d0acb46347321f14ed28765e041f00445bdbff7ab54", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:2a90bf4a27e33298c83bd86fe20ea532f113e4162f10dce8f37130afef0eeefd", + "digest": "sha256:3883ad91cdafdb9158b14c4d8b11d4dcbfe1205ad8071c988b8f7c4b959a4004", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 13, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:09944d5ca7d06b84e7fed9769ca9da221bb1f052daa9cca0fced521e184396d7" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:d76bad451b8d4bd994e32e8d58cc2584527549751edb1a8f570d9bc87c194cf9" }, "owner_lane": "", "provenance": { diff --git a/native/m5/Makefile b/native/m5/Makefile index 2f5851c5..3d2a07c2 100644 --- a/native/m5/Makefile +++ b/native/m5/Makefile @@ -90,6 +90,7 @@ endif # tested. A mutant that does not compile counts as a harness failure. Each # mutant runs both test programs; it is killed if either fails. GUARDS = class-in-kdf slot-empty slot-check slot-suite slot-kdf slot-flags secman-mac \ + ksm-version ksm-reserved ksm-slot-count ksm-payload-range ksm-unused-empty \ env-len env-mode env-envid env-keygen wipe-all \ commit-mac commit-class commit-objid commit-envid commit-keygen \ anchor-mac anchor-class anchor-missing anchor-store \ diff --git a/native/m5/m5.h b/native/m5/m5.h index c43d27d4..f4939eb1 100644 --- a/native/m5/m5.h +++ b/native/m5/m5.h @@ -102,6 +102,34 @@ int m5_keyslot_wrap(m5_keyslot *s, const uint8_t store_uuid[16], const uint8_t k int m5_keyslot_unwrap(const m5_keyslot *s, const uint8_t store_uuid[16], const uint8_t kek[32], uint8_t k_vol_out[32]); +/* ---- KeySlotManifest (kind 21, ADR 0017 section 2.4): the plaintext Store + * object that bootstraps K_vol. Fixed part: 128-byte header + 4 descriptors + * (640 bytes), then a payload arena the raw-secret profile leaves empty. + * Layout is byte-compatible with the Rust KeySlotManifest. ---- */ +#define M5_KSM_KIND 21 +#define M5_KSM_SLOTS 4 +#define M5_KSM_FIXED_LEN 640 +#define M5_KSM_ARENA_MAX 32768 +typedef struct { + uint8_t store_uuid[16]; + uint64_t manifest_sequence; + uint8_t previous_keyslot_manifest_id[32]; /* zero at genesis */ + uint64_t active_key_epoch; + uint32_t slot_count; /* descriptors in use, at most M5_KSM_SLOTS */ + m5_keyslot slots[M5_KSM_SLOTS]; + uint32_t arena_len; /* payload bytes after the fixed part (decode only) */ +} m5_ksm; +/* Fixed part only; the caller appends the arena bytes. */ +void m5_ksm_encode(const m5_ksm *m, uint8_t out[M5_KSM_FIXED_LEN]); +/* Bounded decode of a whole manifest (fixed part + arena). Every descriptor + * passes m5_keyslot_decode; payload ranges must lie inside the arena. */ +int m5_ksm_decode(const uint8_t *b, size_t len, m5_ksm *m); +/* Recovery KEK, high-entropy profile (ADR 0017 section 2.4): + * HKDF-SHA256(salt = slot.salt, ikm = raw secret, info = + * "AIENOS/M5/RECOVERY-KEK-V1", 32 bytes). On error out is zero. */ +#define M5_RECOVERY_KEK_INFO "AIENOS/M5/RECOVERY-KEK-V1" +int m5_recovery_kek(const uint8_t salt[16], const uint8_t *secret, size_t secret_len, uint8_t out[32]); + /* ---- SecurityManifest (kind 22): byte-compatible with Rust v1 ---- * 256 bytes, magic "AIENSEC1", format_version 1, flags 0, reserved 0, * root_mac = HMAC(k_root_auth, "AIENOS-M5-ROOT-AUTH-V1\0" || bytes[0..224]). diff --git a/native/m5/m5_keys.c b/native/m5/m5_keys.c index 711cfa3e..388e1059 100644 --- a/native/m5/m5_keys.c +++ b/native/m5/m5_keys.c @@ -250,3 +250,63 @@ int m5_secman_open(const uint8_t *b, size_t len, const uint8_t k_root_auth[32], memcpy(m->root_mac, b + SECMAN_MAC_OFF, 32); return M5_OK; } + +/* ---- KeySlotManifest (kind 21) ---- */ + +void m5_ksm_encode(const m5_ksm *m, uint8_t out[M5_KSM_FIXED_LEN]) +{ + memset(out, 0, M5_KSM_FIXED_LEN); + memcpy(out, "AIENKSL1", 8); + m5_put16(out + 8, 1); + m5_put16(out + 10, 0); + memcpy(out + 12, m->store_uuid, 16); + m5_put64(out + 28, m->manifest_sequence); + memcpy(out + 36, m->previous_keyslot_manifest_id, 32); + m5_put64(out + 68, m->active_key_epoch); + m5_put32(out + 76, m->slot_count); + for (int i = 0; i < M5_KSM_SLOTS; i++) m5_keyslot_encode(&m->slots[i], out + 128 + i * M5_KEYSLOT_LEN); +} + +int m5_ksm_decode(const uint8_t *b, size_t len, m5_ksm *m) +{ + if (!b || !m) return M5_ERR_ARG; + memset(m, 0, sizeof *m); + if (len < M5_KSM_FIXED_LEN || len > M5_KSM_FIXED_LEN + M5_KSM_ARENA_MAX) return M5_ERR_BOUNDS; + if (memcmp(b, "AIENKSL1", 8) != 0) return M5_ERR_FORMAT; + if (m5_get16(b + 8) != 1) return M5_ERR_FORMAT; /* GUARD:ksm-version */ + if (m5_get16(b + 10) != 0 || !m5_all_zero(b + 80, 48)) return M5_ERR_FORMAT; /* GUARD:ksm-reserved */ + memcpy(m->store_uuid, b + 12, 16); + m->manifest_sequence = m5_get64(b + 28); + memcpy(m->previous_keyslot_manifest_id, b + 36, 32); + m->active_key_epoch = m5_get64(b + 68); + m->slot_count = m5_get32(b + 76); + if (m->slot_count > M5_KSM_SLOTS) return M5_ERR_BOUNDS; /* GUARD:ksm-slot-count */ + m->arena_len = (uint32_t)(len - M5_KSM_FIXED_LEN); + for (int i = 0; i < M5_KSM_SLOTS; i++) { + int rc = m5_keyslot_decode(b + 128 + i * M5_KEYSLOT_LEN, M5_KEYSLOT_LEN, &m->slots[i]); + if (rc != M5_OK) return rc; + const m5_keyslot *s = &m->slots[i]; + if ((uint64_t)s->payload_offset + s->payload_length > m->arena_len) return M5_ERR_BOUNDS; /* GUARD:ksm-payload-range */ + /* an unused descriptor past slot_count must be Empty */ + if ((uint32_t)i >= m->slot_count && s->slot_type != M5_SLOT_EMPTY) return M5_ERR_FORMAT; /* GUARD:ksm-unused-empty */ + } + return M5_OK; +} + +int m5_recovery_kek(const uint8_t salt[16], const uint8_t *secret, size_t secret_len, uint8_t out[32]) +{ + if (out) memset(out, 0, 32); + if (!salt || !secret || !out || secret_len == 0) return M5_ERR_ARG; + /* HKDF-Extract: HMAC keyed by the 16-byte salt. HMAC zero-pads a short + * key, so a 32-byte key of salt || 0^16 is the same PRK as RFC 5869 with + * the bare 16-byte salt. */ + uint8_t k[32], prk[32]; + memset(k, 0, 32); + memcpy(k, salt, 16); + aienos_hmac_sha256(k, secret, secret_len, prk); + int rc = m5_hkdf_expand(prk, (const uint8_t *)M5_RECOVERY_KEK_INFO, sizeof(M5_RECOVERY_KEK_INFO) - 1, out, 32); + aienos_wipe(k, sizeof k); + aienos_wipe(prk, sizeof prk); + if (rc != M5_OK) memset(out, 0, 32); + return rc; +} diff --git a/native/m5/tests/.crumb b/native/m5/tests/.crumb index cbb33fa2..04c68ca8 100644 --- a/native/m5/tests/.crumb +++ b/native/m5/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:90460ee7f9dd6e3e0a930d07ca84ab3e97ee714e368f24c3c87ab78c1a84f65d", + "digest": "sha256:14c3d97cc49d279cf57a819bf7aa2c75c8bcd3d0071b302f352ae6ddda74f2b6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:25babc8424884b63552b9335e45763a2da74f17f5acf9288e0b9d57930e87bba" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:ca5032aab793b251b45c2292dff134fe9ef7e98f55bbd01480984375ef9f9559" }, "owner_lane": "", "provenance": { diff --git a/native/m5/tests/m5_test.c b/native/m5/tests/m5_test.c index 1546636e..e7604151 100644 --- a/native/m5/tests/m5_test.c +++ b/native/m5/tests/m5_test.c @@ -973,10 +973,95 @@ static void t_recovery_store_binding(void) } struct test { const char *name; void (*fn)(void); }; + +/* ---- KeySlotManifest (kind 21) and the recovery KEK (ADR 0017 section 2.4) ---- */ + +static m5_ksm ksm_fixture(void) +{ + m5_ksm m; + memset(&m, 0, sizeof m); + memset(m.store_uuid, 0xA0, 16); + m.manifest_sequence = 1; + m.active_key_epoch = 3; + m.slot_count = 2; /* slot 0 reserved Empty (TPM later), slot 1 recovery */ + m.slots[1] = slot_fixture(); + m.slots[1].kdf_suite = 0x02; + m.slots[1].wrap_suite = M5_WRAP_SUITE_GCMSIV; + memset(m.slots[1].salt, 0x5a, 16); + return m; +} + +static void t_ksm_roundtrip(void) +{ + uint8_t kek[32], nonce[12], kv[32], enc[M5_KSM_FIXED_LEN + 8]; + memset(kek, 0x42, 32); memset(nonce, 0x24, 12); + m5_ksm m = ksm_fixture(), d; + CHECK(m5_keyslot_wrap(&m.slots[1], m.store_uuid, kek, K_VOL, nonce) == M5_OK); + m5_ksm_encode(&m, enc); + CHECK(memcmp(enc, "AIENKSL1", 8) == 0 && enc[8] == 1 && enc[9] == 0 && enc[76] == 2); + CHECK(memcmp(enc + 12, m.store_uuid, 16) == 0 && enc[68] == 3); + CHECK(all_zero(enc + 80, 48) && all_zero(enc + 128, M5_KEYSLOT_LEN)); /* reserved; slot 0 Empty */ + CHECK(m5_ksm_decode(enc, M5_KSM_FIXED_LEN, &d) == M5_OK); + CHECK(d.slot_count == 2 && d.active_key_epoch == 3 && d.manifest_sequence == 1 && d.arena_len == 0); + CHECK(d.slots[1].slot_type == M5_SLOT_RECOVERY_RAW_SECRET && d.slots[1].kdf_suite == 0x02); + CHECK(m5_keyslot_unwrap(&d.slots[1], d.store_uuid, kek, kv) == M5_OK && memcmp(kv, K_VOL, 32) == 0); + /* an arena is carried as length only */ + memset(enc + M5_KSM_FIXED_LEN, 7, 8); + CHECK(m5_ksm_decode(enc, sizeof enc, &d) == M5_OK && d.arena_len == 8); +} + +static void t_ksm_refusals(void) +{ + uint8_t enc[M5_KSM_FIXED_LEN + 8], e2[M5_KSM_FIXED_LEN + 8]; + m5_ksm m = ksm_fixture(), d; + m5_ksm_encode(&m, enc); + memset(enc + M5_KSM_FIXED_LEN, 0, 8); + CHECK(m5_ksm_decode(enc, M5_KSM_FIXED_LEN - 1, &d) == M5_ERR_BOUNDS); + CHECK(m5_ksm_decode(NULL, M5_KSM_FIXED_LEN, &d) == M5_ERR_ARG); + memcpy(e2, enc, sizeof enc); e2[0] = 'X'; + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); + memcpy(e2, enc, sizeof enc); e2[8] = 2; /* format_version */ + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); + memcpy(e2, enc, sizeof enc); e2[10] = 1; /* flags */ + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); + memcpy(e2, enc, sizeof enc); e2[100] = 1; /* reserved */ + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); + memcpy(e2, enc, sizeof enc); e2[76] = 5; /* slot_count > 4 */ + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_BOUNDS); + memcpy(e2, enc, sizeof enc); e2[128 + 3] = 1; /* slot 0 flags: descriptor refused */ + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); + /* payload range: slot 1 names 8 bytes of arena that are not there */ + memcpy(e2, enc, sizeof enc); e2[128 + M5_KEYSLOT_LEN + 108] = 8; + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_BOUNDS); + CHECK(m5_ksm_decode(e2, sizeof e2, &d) == M5_OK); /* present: fine */ + /* a descriptor past slot_count must be Empty */ + memcpy(e2, enc, sizeof enc); e2[76] = 1; + CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); +} + +static void t_recovery_kek_vector(void) +{ + /* openssl kdf -keylen 32 -kdfopt digest:SHA256 -kdfopt hexkey:<secret> + * -kdfopt hexsalt:<salt> -kdfopt info:AIENOS/M5/RECOVERY-KEK-V1 HKDF */ + uint8_t secret[32], salt[16], out[32], out2[32]; + for (int i = 0; i < 32; i++) secret[i] = (uint8_t)i; + for (int i = 0; i < 16; i++) salt[i] = (uint8_t)(0xa0 + i); + static const uint8_t want[32] = {0x73, 0xa4, 0xe8, 0x99, 0x5a, 0x85, 0x9f, 0xca, 0xb9, 0xbd, 0x19, 0x01, 0x91, + 0xb4, 0x33, 0x4c, 0x5e, 0x75, 0x2c, 0xdc, 0x59, 0x21, 0x4c, 0x00, 0xe0, 0x72, + 0x21, 0xa2, 0x5e, 0xb6, 0x26, 0x38}; + CHECK(m5_recovery_kek(salt, secret, 32, out) == M5_OK); + CHECK(memcmp(out, want, 32) == 0); + salt[0] ^= 1; + CHECK(m5_recovery_kek(salt, secret, 32, out2) == M5_OK && memcmp(out, out2, 32) != 0); + CHECK(m5_recovery_kek(salt, secret, 0, out2) == M5_ERR_ARG && all_zero(out2, 32)); + CHECK(m5_recovery_kek(NULL, secret, 32, out2) == M5_ERR_ARG && all_zero(out2, 32)); +} + static const struct test TESTS[] = { #define T(x) {#x, x} T(t_hkdf_rfc5869_case1), T(t_subkeys_deterministic_and_separated), T(t_object_key_binds_every_field), - T(t_keyslot_wrap_unwrap), T(t_keyslot_refusals), T(t_secman_roundtrip_rust_v1_layout), + T(t_keyslot_wrap_unwrap), T(t_keyslot_refusals), T(t_ksm_roundtrip), T(t_ksm_refusals), T(t_recovery_kek_vector), + T(t_secman_roundtrip_rust_v1_layout), T(t_secman_wrong_root_auth_key_refused), T(t_secman_every_byte_tamper_refused), T(t_env_roundtrip_sizes), T(t_env_header_bounds_each_field), T(t_env_rust_v1_header_and_standard_aad), T(t_env_hostile_header_fuzz), T(t_all_or_nothing_late_chunk_failure_zeroes_output), diff --git a/native/store/.crumb b/native/store/.crumb index be11194d..399f4ffc 100644 --- a/native/store/.crumb +++ b/native/store/.crumb @@ -13,21 +13,21 @@ "generated": { "children": [ { - "digest": "b0cb49a83a84e9295e3ff4ec0f77f83fa3697788454e27a9fd2b8d8c2e408f09", - "last_commit": "470c160", + "digest": "68639cafb167da617ac29e4caf4a1f7da653f9629d764c096e1992979cb557ce", + "last_commit": "a9d120b", "name": "tests", "purpose": "Directory native/store/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:01a52bf100eacedaefd89db0b755e10c4d02c2dc9e328fcb7cecf7b7b142bd7e", - "compiled_at": "2026-10-04T13:47:18Z", + "children_root": "sha256:e7b4e9b800c80b1534fb10f83005e1a39d7515212c0e0aa423c00ec22aa92c45", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:8c0d84e2ac3642d2bf59a7faf91c6ddf7c27b4038ee020f243fade5385044efb", + "digest": "sha256:9146f4be830fe28ed14ce6bfac89cd97bd2043c825a60a00451152309e1f87ea", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 22, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:780c24b1b76326e380b97f9243d0667ea19ef052222a0a4178cf911cb9e37e5d" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:2e3ff42747a95c42fc2d164f6a46834c835e51f41bd85820dc1f174740fcb847" }, "owner_lane": "", "provenance": { diff --git a/native/store/store_sealed.c b/native/store/store_sealed.c index 541cfcea..a214b668 100644 --- a/native/store/store_sealed.c +++ b/native/store/store_sealed.c @@ -66,6 +66,7 @@ const char *ss_strerror(int e) case SS_E_CRYPTO: return "SealedCryptoFailure"; case SS_E_NEEDS_REOPEN: return "SealedNeedsReopen"; case SS_E_FORMAT_VERSION: return "SealedFormatVersion"; + case SS_E_KEYSLOT: return "SealedKeyslot"; default: return st_strerror(e); } } @@ -212,18 +213,61 @@ static int find_entry(const sv1_entry *e, uint32_t n, const uint8_t id[32]) return -1; } +/* KeySlotManifest shape (ADR 0017 section 2.4) as far as the sealed layer + * needs it: length, magic, format version, and the store it names. uuid may + * be NULL (bootstrap: the store identity is not known yet). */ +static int keyslot_shape(const uint8_t *b, size_t len, const uint8_t uuid[16]) +{ + if (len < SS_KEYSLOT_MIN || len > SS_KEYSLOT_MAX) return SS_E_KEYSLOT; + if (memcmp(b, "AIENKSL1", 8) != 0 || get16(b + 8) != 1) return SS_E_KEYSLOT; + if (uuid && memcmp(b + 12, uuid, 16) != 0) return SS_E_KEYSLOT; + return 0; +} + +int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *ws, uint8_t *out, size_t cap, + size_t *len) +{ + if (len) *len = 0; + if (!scratch || !store || !ws || !out || !len) return SS_E_ARG; + memset(scratch, 0, sizeof *scratch); + int rc = st_open(scratch, store, &ws->st); + if (rc != 0) return rc; + uint32_t n; + const sv1_entry *e = st_catalog(scratch, &n); + int at = -1; + 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 (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; + 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; +} + /* "m5 open": every record and envelope in the Store catalog. */ static int verify_records(ss_store *s, uint64_t G) { ss_workspace *ws = s->ws; uint32_t n; const sv1_entry *e = st_catalog(&s->st, &n); - uint32_t ntx = 0, nc = 0, nenv = 0; + uint32_t ntx = 0, nc = 0, nenv = 0, nks = 0; + s->has_keyslot = 0; for (uint32_t i = 0; i < n; i++) { if (e[i].kind == SS_KIND_ENVELOPE && e[i].version == 1) { nenv++; continue; } + if (e[i].kind == SS_KIND_KEYSLOT && e[i].version == 1) { + if (nks) return SS_E_KEYSLOT; /* GUARD:keyslot-single */ + nks++; + continue; + } if (e[i].kind != SS_KIND_TXREC || e[i].version != 1) return SS_E_FOREIGN_KIND; /* GUARD:foreign-kind */ if (e[i].byte_length > SS_TX_MAX) return SS_E_TXREC; size_t len; @@ -255,16 +299,26 @@ static int verify_records(ss_store *s, uint64_t G) /* envelopes: claims and envelope entries are in one-to-one correspondence */ memset(ws->used, 0, n); 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); - if (at < 0 || e[at].kind != SS_KIND_ENVELOPE) return SS_E_ENVELOPE; /* GUARD:claim-present */ + if (at < 0 || e[at].kind != (ks ? SS_KIND_KEYSLOT : SS_KIND_ENVELOPE)) return SS_E_ENVELOPE; /* GUARD:claim-present */ if (ws->used[at]) return SS_E_DUPLICATE; /* GUARD:claim-once */ ws->used[at] = 1; size_t len; int rc = st_read_object(&s->st, ws->claims[k].sid, ws->rd, sizeof ws->rd, &len); if (rc != 0) return rc; + if (ks) { + /* plaintext manifest: the keyed commit record binds its digest */ + 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); + 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) return SS_E_ENVELOPE; /* GUARD:unclaimed-envelope */ + if (nc != nenv + nks) return SS_E_ENVELOPE; /* GUARD:unclaimed-envelope */ s->nclaims = nc; return 0; } @@ -383,9 +437,36 @@ int ss_prepare(ss_store *s, const ss_object *o, size_t n) uint64_t ng = G + 1; uint8_t *tx = ws->tx; memset(tx, 0, SS_TX_MAX); + int nks = 0; for (size_t i = 0; i < n; i++) { if (o[i].len > SS_MAX_PLAINTEXT || (!o[i].bytes && o[i].len)) return SS_E_ARG; /* GUARD:sealed-size */ 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 */ + 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 */ + 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; + memset(&mc, 0, sizeof mc); + memcpy(mc.obj.store_uuid, s->uuid, 16); + mc.obj.object_kind = SS_KIND_KEYSLOT; + mc.obj.object_version = 1; + mc.obj.key_generation = s->keys.key_generation; + mc.obj.identity_class = s->keys.identity_class; + mc.store_generation = ng; + mc.counter = ng; + mc.object_sequence = i; + sha256_hash(o[i].bytes, o[i].len, mc.object_id); + if (m5_commit_seal(&mc, s->keys.k_root_auth, e + 32) != M5_OK) return SS_E_CRYPTO; + ws->objs[i].kind = SS_KIND_KEYSLOT; + ws->objs[i].version = 1; + ws->objs[i].bytes = o[i].bytes; + ws->objs[i].len = o[i].len; + continue; + } uint8_t h[32]; if (envid_derive(s, ng, (uint32_t)i, o[i].kind, o[i].version, o[i].bytes, o[i].len, h) != 0) return SS_E_CRYPTO; @@ -460,6 +541,10 @@ int ss_commit_prepared(ss_store *s, st_hook hook, void *arg) memcpy(c->sid, e, 32); if (m5_commit_open(e + 32, M5_COMMIT_LEN, s->keys.k_root_auth, s->keys.identity_class, &c->c) != M5_OK) return SS_E_CRYPTO; + if (c->c.obj.object_kind == SS_KIND_KEYSLOT) { + s->has_keyslot = 1; + memcpy(s->keyslot_sid, c->sid, 32); + } } if (hook) hook(arg, SS_CP_BEFORE_ANCHOR); rc = anchor_write(s, G + 1, s->last_tx_digest); @@ -490,6 +575,17 @@ int ss_read(ss_store *s, const uint8_t sid[32], uint8_t *out, size_t cap, size_t size_t elen; int rc = st_read_object(&s->st, sid, s->ws->rd, sizeof s->ws->rd, &elen); if (rc != 0) return rc; + if (c->c.obj.object_kind == SS_KIND_KEYSLOT) { + /* plaintext manifest: digest-bound by its commit record, returned as is */ + uint8_t dg[32]; + sha256_hash(s->ws->rd, elen, dg); + if (!aienos_ct_equal(dg, c->c.object_id, 32)) return SS_E_KEYSLOT; /* GUARD:read-keyslot-digest */ + if (elen > cap) return SS_E_ARG; + memcpy(out, s->ws->rd, elen); + *len = elen; + if (kind) *kind = SS_KIND_KEYSLOT; + return 0; + } if (m5_commit_check_object(&c->c, s->ws->rd, elen) != M5_OK) return SS_E_ENVELOPE; /* GUARD:read-digest */ rc = m5_envelope_open(s->keys.k_domain, s->keys.identity_class, &c->c.obj, s->ws->rd, elen, out, cap, len); diff --git a/native/store/store_sealed.h b/native/store/store_sealed.h index c280c5e7..4d083e05 100644 --- a/native/store/store_sealed.h +++ b/native/store/store_sealed.h @@ -42,6 +42,13 @@ #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. */ +#define SS_KIND_KEYSLOT 21u +#define SS_KEYSLOT_MIN 640u /* header + 4 descriptors */ +#define SS_KEYSLOT_MAX SS_MAX_PLAINTEXT #define SS_MAX_OBJECTS 8u /* application objects per transaction */ #define SS_MAX_PLAINTEXT 16384u /* bytes per application object */ #define SS_CHUNK 4096u /* envelope chunk size */ @@ -66,6 +73,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 */ }; const char *ss_strerror(int e); @@ -128,8 +136,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 */ } 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). */ +int ss_keyslot_bootstrap(st_store *scratch, const st_dev *store, ss_workspace *ws, uint8_t *out, size_t cap, + size_t *len); + /* torn_slot device over a disk_dev (adds the disk layer's I/O splitting). */ void ss_ts_device(const disk_dev *disk, ts_device *out); diff --git a/native/store/tests/.crumb b/native/store/tests/.crumb index cc7bf1f8..0b6ea044 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-04T13:47:18Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:b0cb49a83a84e9295e3ff4ec0f77f83fa3697788454e27a9fd2b8d8c2e408f09", + "digest": "sha256:68639cafb167da617ac29e4caf4a1f7da653f9629d764c096e1992979cb557ce", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:a7689fd08f846e9208c4ad40ff5550e400e4ffe1d8207286d173b670923f3350" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:416d15ec9b91a7c7688dcbf9a9d512f6c5061fbba59fbb70a82c0892e1ff0a67" }, "owner_lane": "", "provenance": { diff --git a/native/store/tests/store_sealed_test.c b/native/store/tests/store_sealed_test.c index da9335da..3bfbd796 100644 --- a/native/store/tests/store_sealed_test.c +++ b/native/store/tests/store_sealed_test.c @@ -303,14 +303,13 @@ static void test_identity(void) /* Forge helper: prepare one object, let edit() change the workspace, commit * with the real Store engine, then return the next mount's result. */ typedef void (*forge_fn)(ss_store *s, void *arg); -static int forge(forge_fn edit, void *arg) +static int forge_objs(forge_fn edit, void *arg, const ss_object *o, size_t n) { rig r; ss_store s; int rc = sopen(&r, &s, &K); if (rc != 0) { rig_close(&r); return -8888; } - ss_object o[2] = {{44, 1, P1, 300}, {45, 1, P2, 5000}}; - rc = ss_prepare(&s, o, 2); + rc = ss_prepare(&s, o, n); if (rc == 0) { edit(&s, arg); rc = ss_commit_prepared(&s, NULL, NULL); @@ -319,6 +318,11 @@ static int forge(forge_fn edit, void *arg) if (rc != 0) return -7777; return open_rc(&K); } +static int forge(forge_fn edit, void *arg) +{ + ss_object o[2] = {{44, 1, P1, 300}, {45, 1, P2, 5000}}; + return forge_objs(edit, arg, o, 2); +} static void put64le(uint8_t *p, uint64_t v) { @@ -593,6 +597,133 @@ static void cut_before_anchor(void *arg, int cp) if (cp == SS_CP_BEFORE_ANCHOR) disk_file_arm_cut(&g_cut_rig->f, 0); } + +/* ---- 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 void ksm_fill(uint8_t *m, uint64_t seq, const uint8_t uuid[16]) +{ + memset(m, 0, SS_KEYSLOT_MIN); + memcpy(m, "AIENKSL1", 8); + m[8] = 1; + memcpy(m + 12, uuid, 16); + put64le(m + 28, seq); + m[76] = 2; /* slot_count; descriptors stay Empty: the sealed layer checks shape only */ +} +static st_store ks_scratch; +static int bootstrap_rc(uint8_t *out, size_t cap, size_t *len) +{ + rig r; + if (rig_open(&r, g_path, g_bs, 0) != 0) return -9999; + int rc = ss_keyslot_bootstrap(&ks_scratch, &r.sdev, &g_ws, out, cap, len); + rig_close(&r); + return rc; +} +static int ks_prepare_rc(const ss_object *o, size_t n) +{ + rig r; + ss_store s; + int rc = sopen(&r, &s, &K); + if (rc == 0) rc = ss_prepare(&s, o, n); + rig_close(&r); + return rc; +} +static void ed_ks_digest(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_digest); } +static void ed_ks_second(ss_store *s, void *a) +{ + (void)a; + s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}; +} +static void ed_ks_unclaimed(ss_store *s, void *a) +{ + (void)a; + s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}; +} +static void ed_ks_bad_unclaimed(ss_store *s, void *a) +{ + (void)a; + s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM_BAD, sizeof KSM_BAD}; +} + +static void test_keyslot(void) +{ + uint8_t ids[2][32], out[SS_MAX_PLAINTEXT]; + size_t len = 0; + ksm_fill(KSM, 1, RIG_UUID); + ksm_fill(KSM2, 2, RIG_UUID); + ksm_fill(KSM_BAD, 3, RIG_UUID); + KSM_BAD[0] = 'X'; + make_gen2(ids); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "no manifest to bootstrap from"); + CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "snapshot gen 2 (no keyslot)"); + + /* prepare-time refusals: shape, store identity, one per store */ + { + uint8_t m[SS_KEYSLOT_MIN]; + ss_object o[2] = {{SS_KIND_KEYSLOT, 1, m, sizeof m}, {SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}}; + memcpy(m, KSM, sizeof m); m[0] = 'X'; + SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "bad magic refused at prepare"); + memcpy(m, KSM, sizeof m); m[8] = 2; + SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "format version 2 refused at prepare"); + memcpy(m, KSM, sizeof m); m[12] ^= 1; + SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "another store's manifest refused at prepare"); + memcpy(m, KSM, sizeof m); o[0].len = SS_KEYSLOT_MIN - 1; + SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "short manifest refused at prepare"); + o[0].len = sizeof m; o[0].version = 2; + SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "manifest version 2 refused at prepare"); + o[0].version = 1; + SCHECK_EQ(ks_prepare_rc(o, 2), SS_E_KEYSLOT, "two manifests in one transaction refused"); + SCHECK_EQ(ks_prepare_rc(o, 1), 0, "one well-formed manifest prepares"); + } + + /* commit {manifest, object}: bootstrap reads it unkeyed, the keyed open binds it */ + { + rig r; + ss_store s; + SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 2"); + ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {42, 1, P4, sizeof P4}}; + uint8_t kid[2][32]; + SCHECK_EQ(ss_transact(&s, o, 2, NULL, NULL, kid), 0, "gen 3 commits the manifest"); + CHECK(s.has_keyslot && memcmp(s.keyslot_sid, kid[0], 32) == 0, "manifest bound after commit"); + rig_close(&r); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), 0, "bootstrap reads the manifest"); + CHECK(len == sizeof KSM && memcmp(out, KSM, len) == 0, "bootstrap bytes are the manifest"); + SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 3"); + CHECK(s.has_keyslot && memcmp(s.keyslot_sid, kid[0], 32) == 0, "manifest bound at open"); + uint16_t kind = 0; + 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"); + 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"); + /* 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; + SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, NULL), SS_E_KEYSLOT, "manifest read with a wrong digest"); + rig_close(&r); + } + + /* forgeries from the no-keyslot snapshot */ + 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_digest, NULL, o, 2), SS_E_KEYSLOT, "commit digest not the manifest's"); + } + 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"); + } + CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); + SCHECK_EQ(forge(ed_ks_unclaimed, NULL), SS_E_ENVELOPE, "unclaimed manifest"); + CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); + SCHECK_EQ(forge(ed_ks_bad_unclaimed, NULL), SS_E_ENVELOPE, "unclaimed malformed manifest refused keyed"); + SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses a malformed manifest"); + CHECK(len == 0, "refused bootstrap reports zero length"); + CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); +} + static void test_anchor_crash(void) { uint8_t ids[2][32]; @@ -795,6 +926,7 @@ int main(int argc, char **argv) test_rollback(); test_identity(); test_forgery(); + test_keyslot(); test_anchor_crash(); test_nonce(); printf("sealed store, %u-byte blocks: %s\n", g_bs, g_failed == before ? "ok" : "FAILED"); diff --git a/scripts/.crumb b/scripts/.crumb index eddc6136..94370ada 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T04:51:22Z", + "compiled_at": "2026-10-12T05:32:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:220b980dcb68f58f243c7334f554634ff22438a535a770bee491dd57f0f8dc69", + "digest": "sha256:9f409f70dc736ec25cc1fb97a221db84f7ac631b613bf50563740171fabc6983", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 82, - "generated_at_commit": "e01be85155f959705f2534cf07a7b66a03567404", - "source_tree": "sha256:a3de6543e6481e3f074dc558bbc871c7cec34731fdd5381beb4efd02b9ff3811" + "generated_at_commit": "a9d120b2e91893bb80a378dc746cf0a274e42878", + "source_tree": "sha256:47a79ad1ca48ece61a18aa7a3fc3d3ad99643f37b67e6e0bd7ec5215fa8c195d" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_lifecycle_test.sh b/scripts/qemu_ck_lifecycle_test.sh index 259d5fef..20af04d2 100755 --- a/scripts/qemu_ck_lifecycle_test.sh +++ b/scripts/qemu_ck_lifecycle_test.sh @@ -1,14 +1,21 @@ #!/usr/bin/env bash -# LIFECYCLE gate for the AIENOS C kernel (native v1 plan P03): rows 160-167 of -# native/kernel/GATES.md "LIFECYCLE resident production lifecycle". C-only. +# LIFECYCLE gate for the AIENOS C kernel (native v1 plan P03 + P04 cut 1): rows +# 160-170 of native/kernel/GATES.md "LIFECYCLE resident production lifecycle". C-only. # QEMU is not hardware: a PASS here qualifies nothing physical. # # Boots two CK_SERVICE images in QEMU AArch64 (AAVMF, NVMe disk, iommu=smmuv3), # operator input over the PL011 serial from OUTSIDE the guest (pipe chardev): # service make full CK_SERVICE=1: the production entry. The production key -# source refuses (no in-kernel K_vol unwrap, P04), so the lifecycle -# must come up LOCKED, refuse every task and unlock, still answer -# status/help/evidence, and shut down in order. +# source (TPM Slot 0) refuses, so the lifecycle comes up LOCKED with +# the NVMe kept bound, refuses every task and a bare unlock, still +# answers status/help/evidence, and shuts down in order. Booted a +# second and third time (P04 cut 1): `unlock <64-hex recovery secret>` +# runs the ADR 0017 Slot 1 keyslot path: genesis on the blank disk +# (K_vol from the entropy service, manifest committed with the boot +# record), a note approved and committed, shutdown; then on the SAME +# disk a wrong secret is refused by the keyslot tag and the right one +# unwraps K_vol and reopens the Store at the next generation. The +# secret is echoed as '*' and never appears in the serial log. # testkeys make full CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 (TEST-ONLY): the # Store opens with TEST keys so READY/SERVE run: a task is proposed, # approve commits it to the Store, deny and cancel drop tasks, a @@ -18,22 +25,32 @@ # # row 160 service image: lifecycle starts after mm_usage, state=locked, # store=locked; the Store stage refused with BlockedOperator and -# released the NVMe; no TEST key banner -# row 161 locked: task and unlock REFUSED (state stays locked), status/help/ -# evidence answered -# row 162 locked shutdown: stop order store=was-locked, then stage result, -# then final report; no panic/fault; QEMU exit 0; M1 checks +# kept the NVMe bound for a later unlock; no TEST key banner +# row 161 locked: task and a bare unlock REFUSED (state stays locked), +# status/help/evidence answered +# row 162 locked shutdown: stop order store=was-locked after the NVMe +# revoke, then stage result, then final report; no panic/fault; +# QEMU exit 0; M1 checks # row 163 testkeys image: announces TEST-ONLY banner, Store open, NVMe kept # bound for the service lifetime (no dma_gate revoke before ready) # row 164 serve: task -> pending with the proposed note; approve -> committed # 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 65-byte line is refused whole and never run; evidence +# row 166 bounds: a 97-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 # final report; QEMU exit 0; M1 checks +# row 168 unlock genesis (service image, blank disk): `unlock deadbeef` refused +# (64 hex digits expected); `unlock <secret>` formats a production +# Store with the keyslot manifest (genesis=1, generation 2, boot 1), +# lifecycle ready, a note approved and committed, stop order closes +# row 169 unlock resume (same disk): wrong secret refused (RecoverySecretRejected, +# disk not reformatted, still locked); right secret unwraps K_vol, +# Store opened at generation 3 boot_count 1, committed generation 4 +# row 170 secret hygiene: the typed secret is echoed as 64 '*' and the hex +# never appears in either serial log; no TEST key banner # 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 @@ -94,8 +111,12 @@ wait_for() { local deadline=$(( $(date +%s) + $1 )); shift kill -0 "${qemu_pid}" 2>/dev/null || break; sleep 1; done for p in "$@"; do serial_has "${p}" && return 0; done; return 1; } ser_line() { printf '%s\r' "$1" >&4; sleep 1.2; } +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" -boot() { # name efi +boot() { # name efi driver [nvme.img to reuse] work="${top}/$1" mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" @@ -103,7 +124,7 @@ boot() { # name efi cp "${vars_fd}" "${work}/vars.fd" # GPT boot disk (tools/ck_gpt_image.c, as the Store gate): protective MBR, # primary + backup GPT, sentinels, AIENOS partition all zero (Store at genesis). - "${gpt_tool}" create "${work}/nvme.img" 512 64 aienos-middle >/dev/null + if [[ -n "${4:-}" ]]; then cp "$4" "${work}/nvme.img"; else "${gpt_tool}" create "${work}/nvme.img" 512 64 aienos-middle >/dev/null; fi mkfifo "${work}/ser.in" "${work}/ser.out" : >"${work}/serial.log" timeout "${boot_timeout}" qemu-system-aarch64 \ @@ -146,7 +167,24 @@ 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 65))" + ser_line "$(printf 'x%.0s' $(seq 97))" + ser_line evidence + ser_line shutdown +} + +drive_unlock() { + ser_line "unlock deadbeef" + ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' + ser_line status + ser_line "task write a note"; ser_line approve; ser_wait '^lifecycle: (approve committed|REFUSED verb=approve)' + 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 + ser_line "unlock ${secret_ok}"; ser_wait '^(lifecycle: unlock ok|lifecycle: REFUSED verb=unlock)' + ser_line status ser_line evidence ser_line shutdown } @@ -171,15 +209,18 @@ if [[ "${st}" != 0 ]] && grep -qF "CK_SERVICE_TEST_KEYS (TEST keys) cannot be co boot service "${service_efi}" drive_locked before 160 "lifecycle after mm_usage" '^mm_usage: ' '^lifecycle: ready state=locked store=locked' rcheck 160 "Store stage refused with the production key source (BlockedOperator), disk not read" '^store: REFUSED proof=keyed step="production Store key source" rc=-2002 \(service image stays LOCKED' -before 160 "NVMe released by the refused Store stage before the lifecycle" '^dma_gate: nvme bus master revoked' '^lifecycle: ready state=locked' +rcheck 160 "Store LOCKED keeps the NVMe bound for a later unlock" '^store: service lifetime: Store LOCKED, NVMe stays bound for `unlock <64-hex recovery secret>` or the lifecycle stop order$' +if [[ -n "$(line_of '^dma_gate: nvme bus master revoked')" && "$(line_of '^dma_gate: nvme bus master revoked')" -lt "$(line_of '^lifecycle: ready state=locked')" ]]; then bad 160 "NVMe revoked before the locked lifecycle (unlock would have no disk)"; else ok 160 "no NVMe revoke before the locked lifecycle"; fi rabsent 160 "no TEST key banner" 'TEST-ONLY service image' rcheck 161 "task refused while locked" '^lifecycle: REFUSED verb=task state=locked reason="locked: no production Store"' 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 task <text> approve deny cancel evidence help shutdown$' +rcheck 161 "help answered" '^lifecycle: verbs: status unlock \[<64-hex recovery secret>\] task <text> approve deny cancel 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' +before 162 "NVMe revoke < stop order line" '^dma_gate: nvme bus master revoked' '^lifecycle: stop order store=was-locked' before 162 "stop order < stage result" '^lifecycle: stop order' '^stage lifecycle: ok rc=0$' before 162 "stage result < final report" '^stage lifecycle: ok rc=0$' '^report_kind: final' rabsent 162 "no panic or fault" '^report_kind: (panic|fault)' @@ -198,7 +239,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 64 bytes"$' +rcheck 166 "overlong line refused whole" '^lifecycle: REFUSED verb=invalid reason="line longer than 96 bytes"$' rcheck 166 "evidence counts" '^lifecycle: evidence requests=10 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}$' rcheck 167 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=11$' before 167 "lifecycle ready < NVMe revoke (revoke only at the stop order)" '^lifecycle: ready state=ready' '^dma_gate: nvme bus master revoked' @@ -209,8 +250,50 @@ rabsent 167 "no panic or fault" '^report_kind: (panic|fault)' if [[ "${qemu_status}" == 0 ]]; then ok 167 "QEMU exit 0"; else bad 167 "QEMU exit ${qemu_status}"; fi m1 167 +# --- boot 3: production service image, blank disk, recovery keyslot GENESIS --- +boot unlock "${service_efi}" drive_unlock +rcheck 168 "lifecycle came up locked" '^lifecycle: ready state=locked store=locked' +rcheck 168 "short secret refused (64 hex digits expected)" '^store: unlock REFUSED proof=keyed step="recovery secret syntax" rc=-2004 \(64 hex digits expected, got 8\)$' +rcheck 168 "blank disk formatted as a production store" '^store: blank disk \(all-zero anchor \+ Store head\): formatted production store$' +rcheck 168 "key source line: recovery keyslot genesis" '^store: key source=recovery-keyslot genesis=1 \(K_vol source: the Slot 1 recovery descriptor; TPM Slot 0 BLOCKED_HARDWARE; no rotation\)$' +rcheck 168 "store committed generation 2 boot 1" '^store: committed generation=2 boot_count=1$' +rcheck 168 "unlock ok" '^store: unlock ok source=recovery-keyslot slot=1 genesis=1 generation=2 boot_count=1$' +rcheck 168 "lifecycle unlocked to ready" '^lifecycle: unlock ok state=ready$' +rcheck 168 "status open" '^lifecycle: state=ready store=open pending=- requests=' +rcheck 168 "task proposed" '^lifecycle: task pending state=serve proposed="note: write a note" \(approve \| deny \| cancel\)$' +rcheck 168 "approve committed generation 3" '^lifecycle: approve committed gen=3 id=[0-9a-f]{8} state=ready$' +rcheck 168 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=7$' +before 168 "stop order < stage result" '^lifecycle: stop order store=closed' '^stage lifecycle: ok rc=0$' +rabsent 168 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 168 "QEMU exit 0"; else bad 168 "QEMU exit ${qemu_status}"; fi +m1 168 +rabsent 170 "secret hex never in the serial log (genesis boot)" "${secret_ok}" +rcheck 170 "typed secret echoed as 64 '*'" '^> unlock \*{64}$' +rabsent 170 "no TEST key banner" 'TEST-ONLY service image' +unlock_img="${top}/unlock/nvme.img" + +# --- boot 4: same disk, wrong secret refused, right secret resumes --- +boot relock "${service_efi}" drive_relock "${unlock_img}" +rcheck 169 "lifecycle came up locked" '^lifecycle: ready state=locked store=locked' +rcheck 169 "wrong secret refused by the keyslot tag" '^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 169 "lifecycle still locked after the wrong secret" '^lifecycle: REFUSED verb=unlock state=locked reason="production key source refused: still locked"$' +rcheck 169 "status still locked" '^lifecycle: state=locked store=locked pending=- requests=2$' +rabsent 169 "not reformatted" '^store: blank disk' +rcheck 169 "key source line: recovery keyslot resume" '^store: key source=recovery-keyslot genesis=0 ' +rcheck 169 "store opened at generation 3 (keyslot + boot record + note), boot_count 1" '^store: opened generation=3 boot_count=1 prev_commit=[0-9a-f]+ anchor=valid-resume$' +rcheck 169 "store committed generation 4 boot 2" '^store: committed generation=4 boot_count=2$' +rcheck 169 "unlock ok, no genesis" '^store: unlock ok source=recovery-keyslot slot=1 genesis=0 generation=4 boot_count=2$' +rcheck 169 "lifecycle unlocked to ready" '^lifecycle: unlock ok state=ready$' +rcheck 169 "evidence: one refused unlock" '^lifecycle: evidence requests=5 refused=1 invalid=0 tasks=0 approved=0' +rcheck 169 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=6$' +rabsent 169 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 169 "QEMU exit 0"; else bad 169 "QEMU exit ${qemu_status}"; fi +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}" + if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then - for b in service testkeys; do echo "---- ${b} serial (lifecycle lines) ----"; grep -E '^(lifecycle|store|stage lifecycle|dma_gate|smmu: nvme|report_kind|mm_usage)' "${top}/${b}/serial.txt" | head -60 || true; done + for b in service testkeys unlock relock; do echo "---- ${b} serial (lifecycle lines) ----"; grep -E '^(lifecycle|store|stage lifecycle|dma_gate|smmu: nvme|report_kind|mm_usage)' "${top}/${b}/serial.txt" | head -60 || 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