Skip to content

feat(tbor): implement ECC keygen, sign, and ECDH derive#585

Open
vsonims wants to merge 1 commit into
tbor/unwrap-keyfrom
tbor/ecc-crypto
Open

feat(tbor): implement ECC keygen, sign, and ECDH derive#585
vsonims wants to merge 1 commit into
tbor/unwrap-keyfrom
tbor/ecc-crypto

Conversation

@vsonims

@vsonims vsonims commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds three stateless masked-key TBOR crypto DDI commands, mirroring the existing MBOR vault-key-handle equivalents. The private key is AEAD-GCM-256 masked and returned to the host (nothing persisted on-device); the caller passes the masked blob back on use.

Opcode Command Handler
0x17 EccGenerateKey generate keypair → masked private key + wire public key
0x18 EccSign unmask masked key in place → ECDSA r‖s over a host digest
0x19 EcdhDerive unmask local key in place → ECDH vs. peer pub → re-mask secret

ECC import is already provided by the existing UnwrapKey (Ecc class) from the base PR; a cross-command test covers UnwrapKey-Ecc → EccSign.

Design (mirrors the sibling TBOR handlers)

  • Zero-copy: keygen masks the private key straight into a reserved response slot; sign/ecdh use decode_mut to unmask the masked key in place (no scratch copy) and reserve+fill outputs.
  • Secret hygiene: recovered plaintext keys/secrets are scrubbed on every return path (scope exit only resets the bump watermark — it does not zero freed DMA memory).
  • Endianness: device emits PKA-native wire-LE; host-side test verification reverses per-coordinate (confirmed by passing OpenSSL ECDSA verification for all curves).
  • Strict key-class handling: EccSign/EcdhDerive recover the curve via a strict from_pal::ecc_private_curve that rejects non-ECC-private kinds (incl. SdSealing); usage attrs (sign/derive) enforced post-unmask.

Changes

  • Handlers: fw/core/lib/src/ddi/tbor/{ecc_generate_key,ecc_sign,ecdh_derive}.rs.
  • Shared PAL mappings added to fw/core/lib/src/ddi/tbor/from_pal.rs (ecc_private, ecdh_secret, ecc_private_curve).
  • Wiring: mod.rs (opcodes, dispatch, 3 classifiers), op.rs (from_tbor_opcode).
  • Wire schemas (fw + host mirror) + lib.rs registration.
  • Emu integration tests (13): keygen all curves/scopes/reject; sign roundtrip host-verified for all curves + UnwrapKey-Ecc→sign cross-test + rejects; ecdh all curves/scopes + reject.
  • Docs: 3 command .md + README rows.

Testing

  • cargo check fw core (host + Uno no_std) ✅
  • clippy (fw + host) ✅ · nightly fmt ✅ · copyright ✅
  • Full TBOR emu suite: 127 passed (114 baseline + 13 ECC).

Notes

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Add three stateless masked-key TBOR crypto commands mirroring the MBOR
vault-key-handle equivalents:

- EccGenerateKey (0x17): generate an ECC keypair (P-256/384/521) and
  return the private key AEAD-GCM-256 masked under the requested scope
  plus the wire public key. Nothing is persisted on-device.
- EccSign (0x18): unmask a caller-held masked ECC private key in place,
  ECDSA-sign a host-supplied pre-hashed digest, return raw r||s.
- EcdhDerive (0x19): unmask a masked local ECC private key in place,
  derive an ECDH shared secret against a host peer public key, and
  re-mask the secret under a target scope.

Uses the zero-copy reserve+fill encoder and in-place unmask (decode_mut)
patterns. Recovered plaintext keys and derived secrets are scrubbed on
every return path (scope exit only resets the bump watermark). ECC import
is provided by the existing UnwrapKey (Ecc class); a cross-command test
covers UnwrapKey-Ecc -> EccSign.

Shared curve/kind mappings added to tbor from_pal (ecc_private,
ecdh_secret, ecc_private_curve); dispatch/classifier wiring in mod.rs and
op.rs. Wire schemas (fw + host mirror), emu integration tests (host-
verified ECDSA roundtrip for all curves), and per-command docs included.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0b09e50a-a9be-4bae-b347-d42dc775a258
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.

1 participant