Skip to content

SshConnectionException: MAC Error when using only hmac-sha2-512 with .NET 8 and SSH.NET 2024.1.0 #1660

Closed

Description

@giladsepio

When connecting to a Cisco switch that only allows the hmac-sha2-512 MAC algorithm, the SSH.NET client throws a SshConnectionException with the message MAC Error.

This happens only when hmac-sha2-512 is the sole allowed MAC on the server. If we include hmac-sha2-256 in the server config, the connection succeeds.

Environment:

SSH.NET version: 2024.1.0

.NET version: .NET 8

Platform: Linux and Windows (tested on both)

SSH server: Cisco switch with the following settings:

ip ssh server algorithm mac hmac-sha2-512
Server-Side SSH Debug Output (Cisco):

Jun 25 10:31:45.083: kex: client->server enc:aes128-ctr mac:hmac-sha2-512
Jun 25 10:31:45.083: kex: server->client enc:aes128-ctr mac:hmac-sha2-512

Jun 25 10:31:47.932: SSH2 1: SSH2_MSG_NEWKEYS received
Jun 25 10:31:48.760: SSH1: Session disconnected - error 0x00

Client Stack Trace:

Renci.SshNet.Common.SshConnectionException: MAC Error
   at Renci.SshNet.Session.ReceiveMessage(Socket socket)
   at Renci.SshNet.Session.MessageListener()
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.CreateAndConnectSession()
   at Renci.SshNet.BaseClient.Connect()

What Works:
When we change the server to:

ip ssh server algorithm mac hmac-sha2-512 hmac-sha2-256
Then the client connects successfully, negotiating hmac-sha2-256.

Expected Behavior:
SSH.NET should be able to successfully connect using hmac-sha2-512 as the MAC algorithm.

Notes:

This looks like a bug in the hmac-sha2-512 MAC handling implementation in SSH.NET.

Possibly related to incorrect handling of the MAC size (512 bits = 64 bytes) or key derivation.

Repro Steps (Pseudocode):

KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(_user);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(_user, _password);

//kauth.AuthenticationPrompt += new EventHandler<Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent);
kauth.AuthenticationPrompt += (a, b) => HandleKeyEvent(a, b, _password);
// Setup Credentials and Server Information
ConnectionInfo ConnNfo = new ConnectionInfo(_ipAddress, 22, _user,
	[kauth, pauth]
 );
ConnNfo.Timeout = TimeSpan.FromSeconds(30);
_sshClient = new SshClient(ConnNfo);
_sshClient.TrustKeyReceived();

_sshClient.Connect(); // throws SshConnectionException: MAC Error

Activity

  1. giladsepio commented on Jun 25, 2025

    @giladsepio
    Author

    Happens also in version 2025.0.0

  2. giladsepio commented on Jun 25, 2025

    @giladsepio
    Author

    in the switch ssh logs we see:

    Jun 25 08:46:04.650: SSH2_MSG_NEWKEYS sent  
    Jun 25 08:46:04.690: SSH2_MSG_NEWKEYS received  
    Jun 25 08:46:04.691: Next packet from client received  
    Jun 25 08:46:04.691: partial packet length(block size)16 bytes, needed 32 bytes, maclen 64  
    Jun 25 08:46:04.691: computed MAC for sequence no.#4 type 1  
    Jun 25 08:46:04.692: computed MAC for sequence no.#5 type 1  
    Jun 25 08:46:04.789: **Session disconnected - error 0x07 (MAC verification failed)**
    
  3. scott-xu commented on Jun 25, 2025

    @scott-xu
    Collaborator

    Can PuTTY or other SSH client connect to it with HmacSha512?

  4. giladsepio commented on Jul 7, 2025

    @giladsepio
    Author

    some of the tools worked and some not.

  5. YaelHerman123 commented on Jul 13, 2025

    @YaelHerman123

    I could connect using sshpass or Mobaxtream with HmacSha512
    and with powershell using:
    ssh user@host -o KexAlgorithms=diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1 -o MACs=hmac-sha2-512 -o HostKeyAlgorithms=+ssh-rsa

  6. YaelHerman123 commented on Jul 13, 2025

    @YaelHerman123

    This is the exception we get when trying to connect:

    This exception was originally thrown at this call stack: [External Code] NetPoller.session.SshClientWrapper.Connect() in ISshClient.cs NetPoller.session.SepioSshClient.Connect() in SepioSshClient.cs Sepio.Shared.Netpoller.session.SshSession.ReadAllAsync(int, Sepio.Prime.Core.Controller.Resources.Net.SwitchConnectionResource, System.Collections.Generic.IReadOnlyDictionary<string, string>, bool, Serilog.ILogger) in SshSession.cs Renci.SshNet.Common.SshConnectionException: 'MAC error'

  7. YaelHerman123 commented on Aug 6, 2025

    @YaelHerman123

    @Rob-Hague maybe you can help?

  8. Rob-Hague commented on Aug 7, 2025

    @Rob-Hague
    Collaborator

    hard to say, maybe something to do with padding. Are you also connecting to a cisco box? Does it happen every time?

    Anybody's device publicly accessible?

  9. YaelHerman123 commented on Aug 7, 2025

    @YaelHerman123

    hard to say, maybe something to do with padding. Are you also connecting to a cisco box? Does it happen every time?

    Anybody's device publicly accessible?

    It happens every time . Im connecting to this switch:
    Cisco IOS Software, C3560CX Software (C3560CX-UNIVERSALK9-M), Version 15.2(7)E9, RELEASE SOFTWARE (fc1)

  10. YaelHerman123 commented on Aug 10, 2025

    @YaelHerman123

    @Rob-Hague Let me know when you can take a look, and we’ll arrange access for you to the switch.
    can you share your email address?

  11. Rob-Hague commented on Aug 10, 2025

    @Rob-Hague
    Collaborator

    It's in the commit message here 03ae6bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions