← Back to Pull requests

feat(crypto): open tenant envelopes in the browser #200

Open
unknown opened this pull request with 541 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 05, 2026 05:02 UTC
Author

What and why

P34 (#171), checkpoint B: open in the browser, with private-key custody as ADR 0007 ("Browser private-key storage") decides. The scheme is applied to the current HPK1 X25519 key now, so the hybrid suite (#186) changes only the KEM behind it.

  • platform-crypto: hpke_open_with_private_key (suite from the frame magic) and tenant_public_key_from_private. Both take a raw 32-byte key, refuse any other length, and scrub the parsed key before returning. TenantKeypair stays as it was and now shares the open and scrub code.

  • envelope-wasm: exports openEnvelope and publicKeyFromPrivateKey. Each zeroizes its copy of the private key whether or not the call succeeds.

  • Frontend:

  • - lib/crypto/devicekey.ts: the device HPKE key is 32 random bytes, with its public key derived by the wasm module. It is stored only AES-256-GCM-encrypted under a separate non-extractable seed key (encrypt/decrypt only), with AAD x25519-key-v1|{tenant}|{SHA-256 of the public key}, the HPK1 counterpart of ADR 0007's xwing-seed-v1. openWithDeviceKey decrypts it just before an open; envelope.ts zeroes the JavaScript copy when the open ends.
    - The Web Crypto X25519 key, which could never open an envelope, is removed. keystore.ts loads an old-shape record with no HPKE key, and the worker generates one in the new form and re-enrolls its public key on the next init.
    - Key setup in the worker runs one at a time, so concurrent requests cannot each generate and save a different key.
    - InitResult.x25519Supported is now hpkeKeyAvailable (false while the envelope module cannot load); the key-setup warning is reworded.
  • Vectors: fixtures/envelope/hpk1.json carries each recipient's test-only private key, derived from the seed published in the file (docs/DATA_POLICY.md rule 3).

  • Docs: ARCHITECTURE.md (KEK note exception, crypto-agility seam), THREAT_MODEL.md (steward and researcher private-key rows, residual risk for the key during an open), TESTING.md, CHANGELOG.md.
  • Not in this PR:

  • The checkpoint A benchmark (docs/BENCHMARKS.md, a Chromium run, a target to agree with the maintainer).

  • No worker operation exposes open to the UI; P56 adds it.

  • ADR 0004 still says the researcher's result key is generated "non-extractable". ADR 0007 says it is amended to "held as ADR 0007 describes"; as an accepted ADR, that edit is left to the maintainer.
  • How it was tested

  • platform-crypto: raw_private_key_opens_what_its_public_key_sealed (wrong info, wrong recipient and wrong magic refused), raw_private_keys_of_the_wrong_length_are_refused.

  • envelope-wasm: open_round_trips_with_the_raw_private_key, open_refuses_a_malformed_private_key. tests/vectors.rs checks each recipient's private key against its seed and public key, and that the raw-key open agrees with TenantKeypair on every vector.

  • envelope.test.ts: the wasm build opens every hpk1.json envelope (opens; tampered ciphertext, wrong info, wrong recipient, wrong frame magic and truncated frame fail with the recorded error) — the Rust-to-browser direction. Also: the caller's key is zeroed on success and failure, wrong-length keys are refused, public keys derive as recorded, and the device key seals and opens, is stored only encrypted, and is refused when moved to another tenant, paired with another public key or decrypted under another seed key.

  • core.test.ts: the seed key is non-extractable with encrypt/decrypt only; the AAD binds tenant and full public-key digest.

  • keystore.test.ts: round trip, and an old-shape record loads with no HPKE key.
  • scripts/check.sh passes locally.

    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 (no server-side decision added; browser-only)

  • Fails closed on missing configuration, key material, or attestation (no HPKE key until the envelope module loads; a tampered or moved key record does not decrypt)

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

  • Synthetic or public data only (docs/DATA_POLICY.md)

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

  • Architectural decision recorded in docs/adr/ if one was made (ADR 0007)
  • Diff: 541 lines (+405 / -136) across 6 files Reviewer: abendrothj Review duration: 4992m
    All Vouch review confidence checks passed
    Residual risk 0.41 = Change Risk (0.71) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 541 lines diff across 6 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    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.377
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #200"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 43.0% 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.2% (HIGH) · Higher = larger blast radius
    71.2%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 40.6% · Moderate risk
    40.6%