From 679f733103ffbd4bb975b077f6cb0899231dd9e3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Sep 2026 11:34:31 +0000 Subject: [PATCH 1/2] Initial plan From 7366401af66b7a6f495125b074f5742d694ec364 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Sep 2026 11:45:46 +0000 Subject: [PATCH 2/2] Fix: reinitialize server MAC HashAlgorithm after each packet verification Co-authored-by: WojciechNagorski <17333903+WojciechNagorski@users.noreply.github.com> --- src/Renci.SshNet/Session.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Renci.SshNet/Session.cs b/src/Renci.SshNet/Session.cs index a17461a1d..eaacdaeeb 100644 --- a/src/Renci.SshNet/Session.cs +++ b/src/Renci.SshNet/Session.cs @@ -1323,7 +1323,14 @@ private Message ReceiveMessage(Socket socket) _ = _serverMac.TransformFinalBlock(Array.Empty(), 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 @@ private Message ReceiveMessage(Socket socket) _ = _serverMac.TransformFinalBlock(Array.Empty(), 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); }