Fix: reinitialize server MAC HashAlgorithm after each packet verification

Co-authored-by: WojciechNagorski <17333903+WojciechNagorski@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-09-01 11:45:46 +00:00
committed by GitHub
parent 679f733103
commit 7366401af6
+16 -2
View File
@@ -1323,7 +1323,14 @@ namespace Renci.SshNet
_ = _serverMac.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
if (!CryptoAbstraction.FixedTimeEquals(_serverMac.Hash, _receiveBuffer.ActiveSpan.Slice(totalPacketLength - serverMacLength, serverMacLength)))
var macIsValid = CryptoAbstraction.FixedTimeEquals(_serverMac.Hash, _receiveBuffer.ActiveSpan.Slice(totalPacketLength - serverMacLength, serverMacLength));
// Not all HashAlgorithm implementations reset their internal state after
// TransformFinalBlock(), so we need to explicitly reinitialize the algorithm
// before it can be reused to compute the MAC of the next packet.
_serverMac.Initialize();
if (!macIsValid)
{
throw new SshConnectionException("MAC error", DisconnectReason.MacError);
}
@@ -1370,7 +1377,14 @@ namespace Renci.SshNet
_ = _serverMac.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
if (!CryptoAbstraction.FixedTimeEquals(_serverMac.Hash, _receiveBuffer.ActiveSpan.Slice(totalPacketLength - serverMacLength, serverMacLength)))
var macIsValid = CryptoAbstraction.FixedTimeEquals(_serverMac.Hash, _receiveBuffer.ActiveSpan.Slice(totalPacketLength - serverMacLength, serverMacLength));
// Not all HashAlgorithm implementations reset their internal state after
// TransformFinalBlock(), so we need to explicitly reinitialize the algorithm
// before it can be reused to compute the MAC of the next packet.
_serverMac.Initialize();
if (!macIsValid)
{
throw new SshConnectionException("MAC error", DisconnectReason.MacError);
}