← Back to Pull requests

feat(crypto): seal tenant envelopes with the X-Wing hybrid suite (HPK2) #228

Closed
unknown closed this pull request without merging on Nov 19, 2026 22:28 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 08:14 UTC
Author

What and why

Part of #187 (P35). Tenant envelopes move to the hybrid post-quantum suite ADR 0007 decides: HPKE with X-Wing (X25519 + ML-KEM-768), HKDF-SHA256 and AES-256-GCM, frame HPK2 || enc (1120) || ciphertext.

1. build(crypto): upgrade hpke to 0.14.1. Still HPK1; the committed hpk1.json vectors are byte-identical. hpke 0.14 turns on x25519-dalek's zeroize itself, so #244's direct x25519-dalek 2 dependency is gone; TenantKeypair keeps ZeroizeOnDrop, and the zeroize test covers both suites' private keys. envelope-wasm uses getrandom 0.4 with wasm_js.
2. feat(crypto): seal tenant envelopes with the X-Wing hybrid suite (HPK2):
- Keys know their suite, and a key opens only frames of its own suite. Public keys are parsed by length (32 HPK1, 1216 HPK2); private keys are exported as magic || key. info strings are -v2 for HPK2.
- Key loaders take a SuitePolicy. From APP_ENV: production accepts only HPK2, development also HPK1, so the browser keeps working until #186; in production, browser enrollment and uploads are refused until then (fail closed, not a regression). The raw hpk1_* loaders are the browser's path and take no policy until P36. Refusals emit crypto.suite_refused.
- Key release and the stand-in accept only HPK2 tenant keys (SuitePolicy::only). The enclave's own key-release key stays X25519 until #176, so dev key release wraps HPK2 tenant keys classically (recorded in the threat model).
- envelope-wasm stays HPK1-only until #186.
- ADR 0007 is accepted: draft-06 pinned, hpke-js as the second implementation in both directions.
3. Uploads: the route refuses a suite the deployment doesn't accept, a suite other than the tenant's enrolled key's, or, with no enrolled key, a suite other than the current snapshot's (422, audited), so a readable snapshot is never replaced by one no key can open. An unreadable enrollment record fails closed.
4. One seal path: hpke_seal_to_tenant calls the seeded-RNG function with the OS RNG, so the vectors pin what production seals. The server keeps curve25519-dalek's basepoint table (precomputed-tables under server); the wasm build doesn't.
5. Every refused enrollment key is audited as crypto.key_enrollment denied.

docs/BENCHMARKS.md records both suites' numbers from one bench run of this head. #187 closes after the hybrid target and regression run (#185's second PR).

How it was tested

  • X-Wing known-answer tests against @noble/post-quantum; HPK2 frames sealed by hpke-js open in Rust, and Rust-sealed frames (hpk2-rust-sealed.json) open in hpke-js. A seal-side KDF change fails the hpke-js opens.

  • Property tests over both suites, including complete frames that reach decapsulation and the AEAD.

  • Negative tests with audit assertions:

  • - a non-canonical 1216-byte key;
    - production refusing an X25519 enrollment;
    - bad base64 at enrollment;
    - mixed-suite upload bodies;
    - an upload in another suite than the enrolled key or the current snapshot;
    - an unreadable enrollment record;
    - the stand-in refusing HPK1;
    - a released HPK1 key;
    - an HPK1 key on the dev release stack;
    - the production demo-key policy.
  • End-to-end queries of an HPK2 upload, and of an HPK1 upload under the development config.

  • scripts/test-simulated-tee.sh: all stages pass; every stored object is an HPK2 envelope.

  • Fuzz target hpke_envelope covers both suites.

  • cargo tree: precomputed-tables on for platform-api, off for envelope-wasm; getrandom 0.4 with wasm_js in the wasm tree.
  • Checklist

  • scripts/check.sh passes locally

  • Every new refusal has a negative test

  • Every new security decision emits a SECURITY_AUDIT_EVENT covered by audit_coverage.rs

  • Fails closed on missing configuration, key material, or attestation (unset APP_ENV is production, so HPK1 is refused)

  • No new cryptographic primitives; suites stay behind their type aliases

  • Synthetic or public data only (docs/DATA_POLICY.md); vector seeds are public and test-only

  • Docs and CHANGELOG.md updated for user-visible changes

  • Architectural decision recorded in docs/adr/ if one was made (ADR 0007 accepted)
  • Diff: 525 lines (+393 / -132) across 6 files Reviewer: abendrothj Review duration: 9493m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.71) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 525 lines diff across 6 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.375
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #228"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 71.0% (HIGH) · Higher = larger blast radius
    71.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 28% · Safe to merge
    28%