Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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/finallyblocks withArray.Clearto wipe intermediate scalar/key material in X25519 and TweetNaCl crypto_box flows. - Wipe Ed25519 signing intermediates (
az,r,hram) in afinallyblock and dispose theSha512hasher viausing. - 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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)