Skip to content

Wipe intermediate key material#25

Open
mtmk wants to merge 1 commit intomainfrom
wipe-intermediate-key-material
Open

Wipe intermediate key material#25
mtmk wants to merge 1 commit intomainfrom
wipe-intermediate-key-material

Conversation

@mtmk
Copy link
Copy Markdown
Member

@mtmk mtmk commented Mar 31, 2026

Several crypto operations left sensitive intermediate byte arrays on the managed heap after use. This adds try/finally blocks with Array.Clear to wipe them. Best-effort given GC can relocate arrays, but reduces the number of live copies containing key material.

Locations:

  • Ed25519 sign: expanded key (az), nonce (r), hram
  • X25519 ScalarMult: clamped private key copy (e)
  • TweetNaCl CryptoBox/CryptoBoxOpen: shared key (k)
  • TweetNaCl CryptoBoxBeforenm: DH intermediate (s)

Also adds using/dispose for the Sha512 hasher in sign.

  • Create_key_pair (sign/verify roundtrip)
  • XKey_seal_open_compare_to_Go_nkeys_library (seal/open cross-validated with Go)
  • XKey_seal_open (seal/open roundtrip)
  • DecodePubCurveKey_blackbox_seal_open_with_decoded_key (seal/open multiple payload sizes)
  • All 43 NKeys + 56 NaCl tests pass across 4 target frameworks

Ed25519 sign left expanded key (az), nonce (r), and hram
arrays on the heap after signing. X25519 ScalarMult left
the clamped private key copy. TweetNaCl CryptoBox and
CryptoBoxOpen left the shared key and DH intermediate.

Add try/finally blocks to wipe these with Array.Clear.
Best-effort in managed .NET since the GC may have already
copied the arrays, but reduces the number of live copies.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 reduces exposure of sensitive intermediate key material on the managed heap by ensuring temporary byte arrays used during cryptographic operations are wiped after use (best-effort), and it also disposes the SHA-512 hasher used during Ed25519 signing.

Changes:

  • Add try/finally blocks with Array.Clear to wipe intermediate scalar/key material in X25519 and TweetNaCl crypto_box flows.
  • Wipe Ed25519 signing intermediates (az, r, hram) in a finally block and dispose the Sha512 hasher via using.
  • Keep functional behavior unchanged while improving post-operation memory hygiene.

Reviewed changes

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

File Description
NATS.NKeys/X25519/X25519.cs Wipes the clamped scalar copy (e) in Curve25519 scalar multiplication via try/finally.
NATS.NKeys/NaCl/TweetNaCl.cs Wipes shared-key intermediates (s, k) for CryptoBox/CryptoBoxOpen and Beforenm via try/finally.
NATS.NKeys/NaCl/Internal/Ed25519Ref10/sign.cs Disposes the SHA-512 hasher and wipes Ed25519 signing intermediates (az, r, hram) in finally.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mtmk mtmk requested a review from aricart April 2, 2026 08:16
Copy link
Copy Markdown
Member

@aricart aricart left a comment

Choose a reason for hiding this comment

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

LGTM

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.

3 participants