Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,14 @@ private Message ReceiveMessage(Socket socket)

_ = _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);
}
Expand Down Expand Up @@ -1370,7 +1377,14 @@ private Message ReceiveMessage(Socket socket)

_ = _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);
}
Expand Down