Skip to content

M1: Zero private key byte arrays in finally blocks#916

Merged
dangershony merged 1 commit into
mainfrom
security/m1-zero-key-bytes
Jul 4, 2026
Merged

M1: Zero private key byte arrays in finally blocks#916
dangershony merged 1 commit into
mainfrom
security/m1-zero-key-bytes

Conversation

@DavidGershony

Copy link
Copy Markdown
Collaborator

Summary

Zero private key byte arrays immediately after use via CryptographicOperations.ZeroMemory in finally blocks.

Problem

When converting AngorKey to NBitcoin Key via new Key(privateKey.ToBytes()), the intermediate byte array containing raw private key material was never zeroed. It remained in managed memory until garbage collected, exposing it to memory-scraping attacks and crash dumps.

Solution

Wrap the new Key(bytes) call in a try/finally that zeros the byte array:

csharp var keyBytes = privateKey.ToBytes(); Key key; try { key = new Key(keyBytes); } finally { CryptographicOperations.ZeroMemory(keyBytes); }

Files Changed

  • InvestorTransactionActions.csBuildAndSignRecoverReleaseFundsTransaction
  • SeederTransactionActions.csSignInvestorRecoveryTransactions
  • SpendingTransactionBuilder.cs — founder key conversion

Testing

All 154 shared tests pass.

Ensure private key bytes extracted via .ToBytes() are zeroed immediately
after constructing the Key object, using CryptographicOperations.ZeroMemory
in a finally block to guarantee cleanup even on exceptions.

Files:
- InvestorTransactionActions: zero investorPrivateKey bytes
- SeederTransactionActions: zero privateKey bytes
- SpendingTransactionBuilder: zero founderPrivateKey bytes

This reduces the window during which raw key material exists in memory,
mitigating memory-scraping attacks and crash dump exposure.
@dangershony dangershony merged commit 3a0f1c1 into main Jul 4, 2026
3 checks passed
@dangershony dangershony deleted the security/m1-zero-key-bytes branch July 4, 2026 21:26
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.

2 participants