Skip to content

feat(tbor): implement SdCreate/RestorePeerBackup (peer SD cloning) - #570

Merged
vsonims merged 1 commit into
mainfrom
sd/peer-backup
Jul 20, 2026
Merged

feat(tbor): implement SdCreate/RestorePeerBackup (peer SD cloning)#570
vsonims merged 1 commit into
mainfrom
sd/peer-backup

Conversation

@vsonims

@vsonims vsonims commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

What

Implements the security-domain peer-cloning command pair — SdCreatePeerBackup (opcode 0x0E, §3.3.10) and SdRestorePeerBackup (0x0F, §3.3.11) — gated by the SD policy's allow_peer_cloning flag.

Stacked on #567 (sd/restore-remote-backup). Review/merge that first; this PR's diff is only the peer-backup delta.

Both commands use masked SD-sealing keys (unmasked on-device via SdSealingKeyGen), not vault handles — matching the rest of the SD backup family. The stale schema-only stubs (key_id + a 180-byte pok_peer_backup) are reworked to masked_sealing_key and a 161-byte HPKE-Auth seal.

SdCreatePeerBackup (0x0E) — stateless

Recover BKS3 from the caller's pok_local_backup (unmask under PartLocalMK) and HPKE-Auth-seal it to a destination peer named by dst_evidence, authenticated by the sender's masked sealing key → pok_peer_backup (161 B). It is SdCreateRemoteBackup's seal front-end over a recovered (not minted) BKS3. Not one-shot and does not touch SDMK, so a rebooted partition can clone to a peer after PartFinal without first restoring the SD locally.

SdRestorePeerBackup (0x0F) — stateful, one-shot

= SdRestoreRemoteBackup + an allow_peer_cloning gate. HPKE-Auth-open pok_peer_backup with the masked receiver key (authenticated by the sender peer's attested key), then reprovision_sd_from_bks3(prev_sd_mk_backup) → device-local backups + vault SDMK + mark SD-initialized (undo-guarded).

Shared refactor

  • Extracted recover_bks3_from_pok_local into sd_backup.rs (BKS3 unmask under PartLocalMK + SdPartitionOwnerSeed kind + anti-rollback); SdRestoreLocalBackup refactored to reuse it, and SdCreatePeerBackup shares it.
  • New error HsmError::SdPeerCloningNotAllowed = 0x0870010A + TborStatus mirror.
  • Opcodes 0x0E/0x0F wired through dispatch, op.rs from_tbor_opcode, and the mod.rs classifiers.

Tests

New emu integration tests (self-peer, sender == receiver):

  • create-peer: self-seal round-trip (161-byte pok_peer_backup), reject-no-peer-cloning, reject-before-finalize.
  • restore-peer: create-peer → reboot → PartFinal(prev_local_mk_backup) restores PartLocalMKHPKE-open restore returns non-zero refreshed backups; one-shot (SdAlreadyInitialized); no-peer-cloning (SdPeerCloningNotAllowed); reject-before-finalize.

Validation

  • cargo xtask nextest --features emu -p azihsm_ddi_tbor_types121/121 (114 prior + 7 new, no regression).
  • cargo xtask build -p azihsm_fw_hsm_std ✓ · uno cargo xtask clippy ✓ · main cargo xtask clippy ✓ · fw types unit tests 35/35 ✓.
  • cargo +nightly fmt + cargo xtask copyright clean.

Docs

Rewrites both command docs and the README table rows (drops "schema-only").

Note: this PR's base (#567) was amended to fix a latent sd_restore_remote_backup fw unit-test buffer overflow (1024→2048) that the non-main-targeting CI didn't surface.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Copilot-Session: 0b09e50a-a9be-4bae-b347-d42dc775a258

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements TBOR security-domain peer cloning by adding the SdCreatePeerBackup (0x0E) and SdRestorePeerBackup (0x0F) command pair, gated by the SD policy’s allow_peer_cloning flag, and aligns the wire schema/docs/tests accordingly.

Changes:

  • Add firmware handlers for SdCreatePeerBackup (stateless HPKE-Auth seal of recovered BKS3) and SdRestorePeerBackup (stateful HPKE-Auth open + reprovision flow).
  • Refactor shared SD-backup logic by extracting recover_bks3_from_pok_local into sd_backup.rs and reusing it from SdRestoreLocalBackup and SdCreatePeerBackup.
  • Extend error/status surface and wiring: new SdPeerCloningNotAllowed error, opcode classification/dispatch, updated host wrappers, docs, and new emu integration tests.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
fw/pal/traits/src/error.rs Adds HsmError::SdPeerCloningNotAllowed for policy-gated peer cloning.
fw/core/lib/src/op.rs Marks 0x0E/0x0F as InSession via SessionCtrl::from_tbor_opcode.
fw/core/lib/src/ddi/tbor/sd_create_peer_backup.rs New handler: recover BKS3 from local backup and HPKE-Auth-seal to a peer (policy-gated).
fw/core/lib/src/ddi/tbor/sd_restore_peer_backup.rs New handler: HPKE-Auth-open peer backup and reprovision SD (policy-gated, one-shot).
fw/core/lib/src/ddi/tbor/sd_restore_local_backup.rs Refactors to reuse shared BKS3 recovery helper.
fw/core/lib/src/ddi/tbor/sd_backup.rs Extracts shared recover_bks3_from_pok_local helper used by restore/local + create/peer.
fw/core/lib/src/ddi/tbor/mod.rs Wires opcodes 0x0E/0x0F through module exports, dispatch, and opcode classifiers.
fw/core/ddi/tbor/types/src/sd_create_peer_backup.rs Updates firmware-side wire schema for masked sealing key + HPKE peer backup (161B).
fw/core/ddi/tbor/types/src/sd_restore_peer_backup.rs Updates firmware-side wire schema for masked sealing key + HPKE peer backup + prev_sd_mk_backup.
ddi/tbor/types/src/status.rs Adds TborStatus::SdPeerCloningNotAllowed mirror status code.
ddi/tbor/types/src/sd_create_peer_backup.rs Updates host-side wrapper types to match new schema (masked key + fixed-size peer backup).
ddi/tbor/types/src/sd_restore_peer_backup.rs Updates host-side wrapper types to match new schema (masked key + fixed-size peer backup + prev backup).
ddi/tbor/types/tests/commands/mod.rs Registers new integration test modules.
ddi/tbor/types/tests/commands/sd_create_peer_backup.rs New emu integration tests for create-peer (roundtrip + policy/lifecycle rejects).
ddi/tbor/types/tests/commands/sd_restore_peer_backup.rs New emu integration tests for restore-peer (roundtrip + one-shot + policy/lifecycle rejects).
docs/tbor-ddi/README.md Updates command table rows to remove “schema-only” for 0x0E/0x0F.
docs/tbor-ddi/commands/sd_create_peer_backup.md Rewrites command documentation to reflect the implemented handler and updated schema/flow.
docs/tbor-ddi/commands/sd_restore_peer_backup.md Rewrites command documentation to reflect the implemented handler and updated schema/flow.

Comment thread ddi/tbor/types/tests/commands/sd_restore_peer_backup.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.

jaygmsft
jaygmsft previously approved these changes Jul 17, 2026
@vsonims
vsonims force-pushed the sd/restore-remote-backup branch from aeb070b to 717daad Compare July 18, 2026 11:56
@vsonims
vsonims force-pushed the sd/restore-remote-backup branch from 717daad to 401df3b Compare July 18, 2026 12:08
@vsonims
vsonims force-pushed the sd/restore-remote-backup branch from 401df3b to ce8c882 Compare July 19, 2026 03:17
@vsonims
vsonims force-pushed the sd/restore-remote-backup branch from ce8c882 to 2b03645 Compare July 20, 2026 18:35
@vsonims
vsonims changed the base branch from sd/restore-remote-backup to main July 20, 2026 19:43
@vsonims
vsonims dismissed jaygmsft’s stale review July 20, 2026 19:43

The base branch was changed.

@vsonims
vsonims enabled auto-merge July 20, 2026 19:44
Implements the security-domain peer-cloning command pair gated by the
SD policy's `allow_peer_cloning` flag.

Both use masked SD-sealing keys (unmasked on-device), not vault handles,
matching the rest of the SD backup family; the stale schema-only stubs
(key_id + 180-byte pok_peer_backup) are reworked to masked_sealing_key
and a 161-byte HPKE-Auth seal.

SdCreatePeerBackup (0x0E, stateless): recover BKS3 from the caller's
pok_local_backup (under PartLocalMK) and HPKE-Auth-seal it to a
destination peer named by dst_evidence, authenticated by the sender's
masked sealing key. It is CreateSD's seal front-end over a recovered (not
minted) BKS3; not one-shot, so a rebooted partition can clone to a peer
after PartFinal without first restoring the SD locally.

SdRestorePeerBackup (0x0F, stateful/one-shot): = SdRestoreRemoteBackup +
an allow_peer_cloning gate. HPKE-Auth-open pok_peer_backup with the masked
receiver key (authenticated by the sender peer's attested key), then
reprovision_sd_from_bks3(prev_sd_mk_backup).

Shared refactor: extract recover_bks3_from_pok_local into sd_backup.rs
(used by local-restore and create-peer). New error
HsmError::SdPeerCloningNotAllowed (0x0870010A) + TborStatus mirror. Wires
opcodes 0x0E/0x0F through dispatch, op.rs (from_tbor_opcode), and the
mod.rs classifiers.

Adds emu integration tests: create-peer self-seal round-trip + reject
paths; restore-peer create-peer→reboot→PartFinal(restore PartLocalMK)→
restore HPKE round-trip, one-shot, no-peer-cloning, and reject-before-
finalize. Full emu suite: 121/121. Rewrites both command docs and the
README table rows.
@vsonims
vsonims added this pull request to the merge queue Jul 20, 2026
Merged via the queue into main with commit 4c8d5ca Jul 20, 2026
29 checks passed
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.

4 participants