Skip to content

Hybrid ML-KEM KEX algorithms support with OpenSSL 3.5 - #1644

Merged
willco007 merged 17 commits into
libssh2:masterfrom
yaroslavros:master
Apr 3, 2026
Merged

willco007 merged 17 commits into
libssh2:masterfrom
yaroslavros:master

Conversation

@yaroslavros

Copy link
Copy Markdown
Contributor

This PR adds mlkem768x25519-sha256, mlkem768nistp256-sha256 and mlkem1024nistp384-sha384 according to draft-ietf-sshm-mlkem-hybrid-kex if libssh2 built against OpenSSL 3.5 or newer.

Tested against OpenSSH 10 (mlkem768x25519-sha256) and against AsyncSSH (all three hybrids).

I also reduced code duplication in kex.c by moving code blocks that are common for all KEXes into process_host_key and finish_kex.

@yaroslavros
yaroslavros force-pushed the master branch 4 times, most recently from 4b3ad4c to dd1377a Compare August 28, 2025 13:10
@Michael-Panic

Copy link
Copy Markdown
Contributor

@yaroslavros Thanks for submitting this. Let me know when you're finished making commits and I'll start reviewing the code.

@yaroslavros

Copy link
Copy Markdown
Contributor Author

All CI checks seem to be happy now, should be ready for review.
Thank you, @Michael-Panic !

Comment thread src/crypto.h Resolved
Comment thread src/crypto.h Resolved
Comment thread src/misc.h Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
vszakats added a commit that referenced this pull request Jul 3, 2026
When using the 'mlkem1024nistp384' KEX method, where the zeroing was
done with mlkem768 key lengths.

Also generalize to store key size on generation and zero that size on
cleanup.

Reported by GitHub Code Quality

Follow-up to 3ba252e #1644

Closes #2192
vszakats added a commit that referenced this pull request Jul 4, 2026
Replace `SSH2_FREE()` with `ssh2_ecdsa_free()` in
`kex_method_mlkem_nistp_cleanup()`. This buffer is allocated with the
crypto backend's allocator, and may also require backend-specific
cleanup beforehand.

Follow-up to 3ba252e #1644
Cherry-picked from #2196

Closes #2200
@vszakats
vszakats requested a review from Copilot July 5, 2026 02:27

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

This PR adds support for the draft hybrid ML-KEM SSH key exchange algorithms when building libssh2 against OpenSSL ≥ 3.5, and refactors the KEX implementation to reduce duplicated hostkey parsing / key-derivation logic.

Changes:

  • Add OpenSSL 3.5+ ML-KEM key generation and decapsulation helpers and gate them behind LIBSSH2_MLKEM.
  • Introduce hybrid KEX implementations: mlkem768x25519-sha256, mlkem768nistp256-sha256, and mlkem1024nistp384-sha384.
  • Refactor common KEX steps into process_host_key() and finish_kex() and add a helper to serialize concatenated hybrid key strings.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/wincng.h Define LIBSSH2_MLKEM as disabled for WinCNG backend.
src/os400qc3.h Define LIBSSH2_MLKEM as disabled for OS/400 backend.
src/mbedtls.h Define LIBSSH2_MLKEM as disabled for mbedTLS backend.
src/libgcrypt.h Define LIBSSH2_MLKEM as disabled for libgcrypt backend.
src/openssl.h Enable LIBSSH2_MLKEM only for OpenSSL ≥ 3.5 (excluding LibreSSL).
src/openssl.c Implement ML-KEM keygen and decapsulation via OpenSSL EVP APIs.
src/crypto.h Add ML-KEM and EC public key size constants and ML-KEM function prototypes.
src/crypto_config.h Add LIBSSH2_NO_MLKEM override to force-disable ML-KEM.
src/misc.h Declare _libssh2_store_hybrid_str() for concatenated “hybrid” strings.
src/misc.c Implement _libssh2_store_hybrid_str() for combined key serialization.
src/libssh2_priv.h Resize KEX request buffer and add ML-KEM key pointers to KEX state.
src/kex.c Add hybrid ML-KEM KEX methods and refactor shared KEX logic into helpers.

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

Comment thread src/openssl.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/misc.c Resolved
@vszakats
vszakats requested a review from Copilot July 5, 2026 10:37

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

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Comment thread src/openssl.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/kex.c Resolved
Comment thread src/misc.c Resolved
vszakats added a commit that referenced this pull request Jul 5, 2026
…ings

Suggested by GitHub Code Quality

Follow-up to 3ba252e #1644

Closes #2215
vszakats added a commit that referenced this pull request Jul 5, 2026
"`shared_secret` is a local variable allocated at line 2113 within the
`ssh2_NB_state_created` block. When `ssh2_transport_send` returns
`LIBSSH2_ERROR_EAGAIN` here, the function returns immediately without
freeing `shared_secret`. On re-entry the variable is re-initialized to
`NULL` at line 2016, so the previous allocation is permanently leaked.
The `shared_secret` should be freed before returning EAGAIN, or its
lifetime should be extended beyond the local scope (e.g., stored in
`exchange_state`) so it can be cleaned up on re-entry or at
`clean_exit`."

Reported by GitHub Code Quality

Follow-up to 3ba252e #1644

Closes #2218
vszakats added a commit that referenced this pull request Jul 8, 2026
It may cause a confusing compiler warning in some cases, and also not
needed for wolfSSL builds.

Follow-up to 3ba252e #1644

Closes #2255
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 8, 2026
vszakats added a commit that referenced this pull request Jul 9, 2026
Also enable a success-path debug message in the OpenSSL no-deprecated
job.

Bug: #1644 (comment)
Follow-up tp bc77633
Follow-up to a69fb2a #2092
Follow-up to 8e4c14b #2083
Follow-up to 3ba252e #1644

Closes #2259
vszakats added a commit that referenced this pull request Jul 11, 2026
vszakats added a commit that referenced this pull request Jul 28, 2026
- `ssh2_mlkem_new()` with `ssh2_curve25519_new()`.
- also inline two comments.

Follow-up to 1164743 #2308
Follow-up to 41389f8 #2029
Follow-up to 3ba252e #1644

Closes #2442
vszakats pushed a commit that referenced this pull request Oct 2, 2026
In `kex_mlkem768x25519_sha256` the x25519 half of the hybrid shared
secret is written into a fixed 64-byte buffer with `ssh2_bn_to_bin`,
which emits only the significant bytes and drops any leading zeros. When
the x25519 secret has a leading zero byte (about 1 in 256 handshakes,
and it comes from the server's ephemeral key) the write is short, so the
tail of the buffer is hashed while still uninitialized and the value
sits left-shifted instead of filling the fixed 32-byte field. That means
an uninitialized stack read into the exchange hash and a wrong K, which
fails the signature check for mlkem768x25519-sha256, the first KEX we
offer. This zero-fills the 32-byte field and writes the value
right-aligned so it is always the full fixed-width secret. The sibling
`kex_mlkem_nistp` sizes its heap buffer to the bignum length, so it has
no uninitialized read.

Bug possibly related: #1644 (comment)
Follow-up to 3ba252e #1644

Closes #2633
vszakats added a commit that referenced this pull request Oct 4, 2026
With the issue fixed, the debug/trace code is no longer necessary.

Also:
- GHA: drop tracing targeting FreeBSD, those flaky failures were not
  seen since then.
  Follow-up to 13dcd17 #2227

Ref: #1644 (comment)
Follow-up to 4f2692f #2633
Follow-up to ca0e2dc #2259
Follow-up to 3ba252e #1644

Closes #2651
vszakats added a commit that referenced this pull request Oct 6, 2026
- kex: clear `k_value` buffer in `kex_*_state_cleanup()` functions.
  Reported-by: Ravikanth
  Fixes #2653

- kex: clear shared secret buffer after use in
  `kex_mlkem768x25519_sha256()`.
  Reported-by: Ravikanth
  Fixes #2654
  Follow-up to 3ba252e #1644

- kex: clear shared secret buffer after use in `kex_mlkem_nistp()`.
  Follow-up to 3ba252e #1644

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants