← Back to Pull requests

Zero temporary buffers that hold key material and reduce more allocations #493

Closed
unknown closed this pull request without merging on Nov 02, 2026 08:31 UTC
View on GitHub
unknown unknown commented on Oct 23, 2026 15:31 UTC
Author

Motivation:

AES-KeyWrap unwrap, ML-KEM decapsulation, PBKDF2, and scrypt each write the key they return into a temporary buffer, copy it into a SymmetricKey, and free the buffer without zeroing it. The SymmetricKey stores its copy in SecureBytes, which zeroes the copy before freeing it. Nothing zeroes the original.

AES-KeyWrap and ML-KEM can write the key directly into the SymmetricKey, as X-Wing already does. That removes both the buffer and the copy. PBKDF2 and scrypt can't, because they are in CryptoExtras and SymmetricKey(unsafeUninitializedCapacity:initializingWith:) is internal to Crypto. They keep their buffer and zero it after the copy. Moving that buffer onto the stack made them twice as slow.

Modifications:

  • Add benchmarks for AES-KeyWrap, ML-KEM, PBKDF2, and scrypt

  • Unwrap AES-KeyWrap keys straight into the SymmetricKey

  • Decapsulate ML-KEM shared secrets straight into the SymmetricKey. Regenerating MLKEM_boring.swift from its .gyb also drops five @available attributes that were only ever in the generated file.

  • Zero the PBKDF2 and scrypt derived key buffers
  • Result:

  • After the call returns, no temporary buffer still contains the key.

  • AES.KeyWrap.unwrap makes 2 allocations instead of 3 and is about 6% faster.

  • MLKEM768.PrivateKey.decapsulate makes 2 allocations instead of 4.

  • KDF.Insecure.PBKDF2.deriveKey and KDF.Scrypt.deriveKey make the same number of allocations and take the same time as before.
  • Benchmarks:

    The tables come from swift package benchmark --metric mallocCountTotal --metric wallClock.

    aes-keywrap-unwrap (before)
    ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                   │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *         │         3 │         3 │         3 │         3 │         3 │         3 │         3 │         5 │
    ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (wall clock) (ns) * │       597 │       601 │       614 │       621 │       630 │       630 │       630 │         5 │
    ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    aes-keywrap-unwrap (after)
    ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                   │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *         │         2 │         2 │         2 │         2 │         2 │         2 │         2 │         5 │
    ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (wall clock) (ns) * │       563 │       564 │       578 │       578 │       581 │       581 │       581 │         5 │
    ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    mlkem768-decapsulate (before)
    ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                   │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *         │         4 │         4 │         4 │         4 │         4 │         4 │         4 │        10 │
    ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (wall clock) (μs) * │        48 │        49 │        50 │        51 │        51 │        51 │        51 │        10 │
    ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    mlkem768-decapsulate (after)
    ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                   │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *         │         2 │         2 │         2 │         2 │         2 │         2 │         2 │        10 │
    ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (wall clock) (μs) * │        46 │        48 │        49 │        51 │        51 │        51 │        51 │        10 │
    ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Lukasa Review duration: 13980m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #493"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%