Skip to content

feat(fw): add InitBk3 firmware handler with masked key codec#372

Merged
jaygmsft merged 6 commits into
fwfrom
user/jayg/init_bk3
May 27, 2026
Merged

feat(fw): add InitBk3 firmware handler with masked key codec#372
jaygmsft merged 6 commits into
fwfrom
user/jayg/init_bk3

Conversation

@jaygmsft
Copy link
Copy Markdown
Contributor

@jaygmsft jaygmsft commented May 6, 2026

Add the InitBk3 DDI command handler and supporting infrastructure for BK3 initialization and masked key operations.

New modules:

  • fw/core/lib/src/masked_key.rs: AES-CBC-256 + HMAC-SHA-384 masked key envelope codec (mask_cbc/unmask_cbc) using zerocopy for wire-format header serialization
  • fw/core/lib/src/lm_key_derive.rs: live migration key derivation primitives (BK_BOOT key gen, BK_BOOT masking key, session BK, etc.)
  • fw/core/lib/src/ddi/init_bk3.rs: InitBk3 handler — generates BK_BOOT, masks BK3, masks BK_BOOT, stores masked BK_BOOT, returns masked BK3
  • fw/core/ddi/mbor/types/src/masked_key.rs: masked key metadata DDI types

PAL changes:

  • Add derive_masking_key to HsmPartitionManager — KBKDF with BKS1/BKS2 context, keeping device secrets (BKS1, BKS2, fw_secret) inside PAL
  • Add part_masked_bk_boot/part_set_masked_bk_boot for masked BK_BOOT storage on partitions (cleared on free, persists across disable/enable)

DDI integration test results with --features emu (566 total):
32 passed, 534 failed, 0 skipped

DDI smoke test results with --features emu (9 total):
9 passed, 0 failed, 0 skipped

Comment thread fw/core/crypto/key-derive/src/lib.rs Outdated
Comment thread fw/core/crypto/key-mask/src/lib.rs Outdated
Comment thread fw/core/lib/src/masked_key.rs Outdated
Comment thread fw/core/crypto/key-derive/src/lib.rs Outdated
@jaygmsft jaygmsft force-pushed the user/jayg/init_bk3 branch 3 times, most recently from 431438e to 8882e7c Compare May 13, 2026 00:31
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jaygmsft jaygmsft force-pushed the user/jayg/init_bk3 branch 3 times, most recently from 3b25628 to 822a07d Compare May 19, 2026 03:37
Adds the firmware-side InitBk3 DDI handler that masks a 48-byte BK3
against the partition's 80-byte BK_BOOT using AES-CBC-256 +
HMAC-SHA-384 (encrypt-then-MAC), producing an mcr-hsm-compatible
244-byte MaskedKey wire-format blob.

New crate fw/core/crypto/masked-key/ hosts the wire format and
encode logic.  Public surface:

  - mask_cbc(pal, io, masking_key, plaintext, metadata, out)
  - MASKING_KEY_AES_CBC_256_HMAC_384_LEN

The encoder uses zerocopy-derived #[repr(C)] header structs as the
single layout-of-truth and writes the blob directly into a caller-
supplied DMA buffer (zero-init precondition checked via
plaintext staging.

Also includes:

  - fw/core/ddi/mbor/types/src/masked_key.rs: DdiMaskedKeyMetadata
  - Integration smoke tests in ddi/lib/tests/integration/init_bk3_smoke.rs
  - Copyright/format fixes in unrelated touched crypto files

Validated:
  - 4/4 init_bk3 smoke tests pass against emu
  - 10/10 emu smoke tests pass
  - fmt + copyright clean

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jaygmsft jaygmsft force-pushed the user/jayg/init_bk3 branch from 822a07d to a863166 Compare May 19, 2026 21:23
jaygmsft and others added 2 commits May 21, 2026 22:45
…intext

- Drop the redundant `[..masked_bk_boot_len]` slice on the
  `part_set_masked_bk_boot` call since `&mut DmaBuf` already
  deref-coerces to `&[u8]`.

- Bind `bk_boot_plain = &bk_boot_dma[..BK_BOOT_LEN]` once and reuse
  it for both `mask_cbc` calls (size-query and fill).

- Trim the doc comments on `BK3_KEY_ATTRIBUTES`,
  `BK_BOOT_MK_LABEL`, and `BK_BOOT_KEY_ATTRIBUTES` to keep only the
  PKCS#11 rationale that is not obvious from the const definition
  itself.  `BK_BOOT_KEY_ATTRIBUTES` now points at
  `BK3_KEY_ATTRIBUTES` and only calls out the `local` flag
  difference (on-device generation vs. host import).

- Replace the per-step numbered fn-level doc on `handle_init_bk3`
  with a tight 5-phase pipeline overview; the body's section markers
  already cover the per-step detail.

No behavior change.  Build clean, `init_bk3_smoke` 4/4 pass on emu,
fmt and copyright clean.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jaygmsft and others added 2 commits May 26, 2026 16:36
* ddi/sim: `set_sealed_bk3_data` now returns `SealedBk3AlreadySet`
  instead of the generic `KeyAlreadyExists` so the simulator agrees
  with the firmware (old and new) and the public `DdiStatus` enum on
  this specific error.

* ddi/lib/tests: update `sealed_bk3_smoke::test_set_sealed_bk3_twice_fails`
  and `live_migration_sim::test_live_migration_sealed_bk3` to assert
  the specific `SealedBk3AlreadySet` status now produced by both
  backends.

* ddi/lib/tests: relax `init_bk3_smoke::test_init_bk3_smoke` length
  assertion from exact 244 bytes to a sanity range. The firmware and
  simulator legitimately populate different optional metadata fields,
  so the encoded envelope size differs while still being a valid
  `MaskedKey`. The smoke test should not lock in either backend's
  exact metadata set.

* fw/core/crypto/masked-key, fw/pal/traits: address clippy lints
  (`is_multiple_of` and `too_many_arguments`).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jaygmsft jaygmsft enabled auto-merge (squash) May 26, 2026 23:09
@jaygmsft jaygmsft disabled auto-merge May 26, 2026 23:09
@jaygmsft jaygmsft enabled auto-merge (squash) May 26, 2026 23:10
@jaygmsft jaygmsft merged commit b7cd269 into fw May 27, 2026
7 of 8 checks passed
@jaygmsft jaygmsft deleted the user/jayg/init_bk3 branch May 27, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants