Repository navigation
Hybrid ML-KEM KEX algorithms support with OpenSSL 3.5 - #1644
Merged
Merged
Conversation
yaroslavros
force-pushed
the
master
branch
4 times, most recently
from
August 28, 2025 13:10
4b3ad4c to
dd1377a
Compare
Contributor
|
@yaroslavros Thanks for submitting this. Let me know when you're finished making commits and I'll start reviewing the code. |
Contributor
Author
|
All CI checks seem to be happy now, should be ready for review. |
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
vszakats
reviewed
Aug 28, 2025
This was referenced Jul 3, 2026
There was a problem hiding this comment.
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, andmlkem1024nistp384-sha384. - Refactor common KEX steps into
process_host_key()andfinish_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.
This was referenced Jul 5, 2026
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
This was referenced Jul 6, 2026
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
This was referenced Jul 23, 2026
vszakats
added a commit
that referenced
this pull request
Jul 28, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.cby moving code blocks that are common for all KEXes intoprocess_host_keyandfinish_kex.