Skip to content

Reset server MAC after TransformFinalBlock on .NET FW - #1830

Open
scott-xu wants to merge 2 commits into
sshnet:developfrom
scott-xu:fix/initialize-mac-netframework
Open

Reset server MAC after TransformFinalBlock on .NET FW#1830
scott-xu wants to merge 2 commits into
sshnet:developfrom
scott-xu:fix/initialize-mac-netframework

Conversation

@scott-xu

Copy link
Copy Markdown
Collaborator

Call _serverMac.Initialize() after TransformFinalBlock when running on .NET Framework. This ensures the MAC is properly reset, addressing differences in cryptographic API behavior across different versions of mscorlib.dll.

Close #1829

Call _serverMac.Initialize() after TransformFinalBlock when running on .NET Framework. This ensures the MAC is properly reset, addressing differences in cryptographic API behavior across different versions of mscorlib.dll.

Close sshnet#1829

#if NETFRAMEWORK
// Ensure MAC is reset on .NET Framework
_serverMac.Initialize();

@Rob-Hague Rob-Hague Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm just guessing without having looked at the old mscorlib source, but perhaps it ought to be after the get on .Hash i.e. after the FixedTimeEquals check?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I reviewed the mscorlib source and found that .Hash is not cleared by .Initialize(). It gets cleared in Dispose().
I understand your concern and there's no harm to move .Initialize() after FixedTimeEquals check.

As a reference, the ComputeHash implementation calls .Initialize() at the very end, just before it returns.
The Initialize method name is kind of misleading.

public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
	if (buffer == null)
	{
		throw new ArgumentNullException("buffer");
	}
	if (offset < 0)
	{
		throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
	}
	if (count < 0 || count > buffer.Length)
	{
		throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
	}
	if (buffer.Length - count < offset)
	{
		throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
	}
	if (m_bDisposed)
	{
		throw new ObjectDisposedException(null);
	}
	HashCore(buffer, offset, count);
	HashValue = HashFinal();
	byte[] result = (byte[])HashValue.Clone();
	Initialize();
	return result;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

there's no harm to move .Initialize() after FixedTimeEquals check.

The only minor difference is that .Initialize() will no longer execute if the FixedTimeEquals check fails. This is not a significant concern because an SshConnectionException will be thrown in that scenario.

An alternative approach is to capture the hash into a local variable, invoke .Initialize(), and then perform the FixedTimeEquals check.

Please let me know which option you prefer:

  1. Move .Initialize() after the FixedTimeEquals check
  2. Capture the hash → call .Initialize() → run the FixedTimeEquals check
  3. Keep the existing behavior in this PR: call .Initialize() before the FixedTimeEquals check

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.

MAC error / CryptographicException on .NET Framework in 2026.0.0: _serverMac is reused across packets without Initialize()

2 participants