Reinitialize server MAC HashAlgorithm between packets to prevent spurious MAC errors - #1834
Draft
WojciechNagorski with Copilot wants to merge 2 commits into
Draft
Reinitialize server MAC HashAlgorithm between packets to prevent spurious MAC errors#1834WojciechNagorski with Copilot wants to merge 2 commits into
WojciechNagorski with Copilot wants to merge 2 commits into
Conversation
…tion Co-authored-by: WojciechNagorski <17333903+WojciechNagorski@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix MAC error and CryptographicException in .NET Framework
Reinitialize server MAC HashAlgorithm between packets to prevent spurious MAC errors
Sep 1, 2026
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.
Since 2026.0.0,
Session.ReceiveMessageverifies the server MAC by reusing a single_serverMacHashAlgorithminstance across the whole session viaTransformBlock/TransformFinalBlock, without ever callingInitialize()between packets. This violatesHashAlgorithm's reuse contract. On certain older .NET Frameworkmscorlib.dllbuilds (pre-HMAC-rework, CAPI-backed implementations), the inner hash state isn't reset afterTransformFinalBlock, so the second and subsequent MAC-protected packets fail — silently producing a wrong hash forhmac-sha2-*(SshConnectionException: MAC error) or throwingCryptographicExceptionforhmac-sha1.Fix
Session.ReceiveMessage, call_serverMac.Initialize()right after computing/comparing the MAC and before evaluating whether to throw, so the instance is always ready for the next packet..NET Framework, since this addresses an actualHashAlgorithmreuse contract violation rather than a platform-specific quirk.The client-side send path (
_clientMac.TryComputeHash) already performs a one-shot hash computation per packet and is unaffected by this issue.