Skip to content

Fix mbedTLS AES-CBC backend: avoid BAD_INPUT_DATA during cipher_finish() in SSH handshake - #1758

Merged
willco007 merged 7 commits into
libssh2:masterfrom
AdrianMoranMontes:fix-mbedtls-cypher
Feb 17, 2026
Merged

willco007 merged 7 commits into
libssh2:masterfrom
AdrianMoranMontes:fix-mbedtls-cypher

Conversation

@AdrianMoranMontes

Copy link
Copy Markdown
Contributor

Description

This PR fixes a reproducible failure in the mbedTLS backend of libssh2 when negotiating AES-CBC ciphers with certain SSH servers.

When libssh2 is built with mbedTLS (tested with mbedTLS 3.6.2), the library fails to encrypt the very first SSH packet after NEWKEYS when the chosen cipher is AES-XXX-CBC. The failure manifests as:

Failure establishing SSH session: -44
libssh2 last error: Unable to ask for ssh-userauth service

Tracing the mbedTLS backend reveals the root cause:

mbedTLS cipher_finish failed: ret = -24832 (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)
mbedTLS cipher_crypt failed
crypt name = aes256-cbc

This happens during the encryption of SSH_MSG_SERVICE_REQUEST (seqno=0), right after the KEX exchange succeeds.
The server → client traffic is irrelevant at that point; this is a pure client-side mbedTLS cipher failure.

Reproduction steps

The issue can be reproduced with:

  • libssh2 master
  • mbedTLS 3.6.2
  • any SSH server that negotiates AES-128-CBC or AES-256-CBC
    (confirmed with OpenSSH and a proprietary embedded SSH server)

Steps:

  1. Build libssh2 with mbedTLS backend.
  2. Connect to a server offering CBC ciphers.
  3. Let the negotiation select AES-CBC.
  4. Observe handshake failure right after NEWKEYS.

Root cause analysis

  1. Wrong interpretation of the encrypt flag

The code used:

op = encrypt == 0 ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;

which inverted the intended direction.
The fix correctly maps the flag:

op = encrypt ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;
  1. Missing explicit padding configuration for mbedTLS

libssh2 handles SSH packet padding internally, so the cipher must operate with no padding.
Without explicitly setting this, mbedTLS rejects CBC operations during cipher_finish().

The fix adds:

mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);

This ensures that mbedTLS does not attempt to validate or add padding at the cipher layer.

Related issues

Probably, this PR fixes #793

vszakats
vszakats previously approved these changes Dec 26, 2025
Comment thread src/mbedtls.c
return -1;

op = encrypt == 0 ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;
op = encrypt ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;

@vszakats vszakats Dec 26, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes me wonder how could libssh2 + mbedTLS work correctly before if this condition
was reversed all along?

@vszakats
vszakats dismissed their stale review December 26, 2025 11:04

(clicked too quick)

@willco007
willco007 merged commit 26f57ef into libssh2:master Feb 17, 2026
@vszakats
vszakats requested a review from Copilot May 1, 2026 07:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes libssh2’s mbedTLS crypto backend so AES-CBC negotiation doesn’t fail immediately after NEWKEYS during the first encrypted client packet, aligning cipher direction and ensuring the cipher layer does not apply/validate padding that libssh2 already handles.

Changes:

  • Correct the mbedTLS cipher operation selection (encrypt vs decrypt) during cipher context initialization.
  • Configure mbedTLS CBC ciphers to use MBEDTLS_PADDING_NONE to avoid MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA during mbedtls_cipher_finish().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mbedtls.c
Comment on lines +136 to +140
if(algo == MBEDTLS_CIPHER_AES_128_CBC ||
algo == MBEDTLS_CIPHER_AES_192_CBC ||
algo == MBEDTLS_CIPHER_AES_256_CBC ||
algo == MBEDTLS_CIPHER_DES_EDE3_CBC) {
ret = mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);

Copilot AI May 1, 2026

Copy link

Choose a reason for hiding this comment

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

Padding is only disabled for a hard-coded subset of CBC cipher types. The mbedTLS backend can also advertise Blowfish-CBC (see src/mbedtls.h), and if it’s negotiated it will still run with mbedTLS’ default padding handling and can hit the same cipher_finish() BAD_INPUT_DATA failure. Consider either (a) including MBEDTLS_CIPHER_BLOWFISH_CBC under an #ifdef in this list, or (b) switching the condition to detect “CBC mode” via cipher_info/ctx instead of enumerating specific algorithms.

Copilot uses AI. Check for mistakes.
vszakats added a commit that referenced this pull request Jul 29, 2026
…ixed now

PR #1758 indeed fixed the issue reported in #793. Let's re-enable the
tests disabled earlier as a CI workaround.

Follow-up to 26f57ef #1758
Follow-up to 9ecb22d #969
Follow-up to 7487dcf
Ref: #793

Closes #2446
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.

Hitting error -44 "Unable to ask for ssh-userauth service" with mbedtls

4 participants