Repository navigation
kex: zero-pad x25519 shared secret in mlkem768x25519 - #2633
Closed
abdul-khaliq-khalid wants to merge 2 commits into
Closed
abdul-khaliq-khalid wants to merge 2 commits into
abdul-khaliq-khalid wants to merge 2 commits into
Conversation
vszakats
reviewed
Oct 1, 2026
| if(ssh2_bn_to_bin(exchange_state->k, | ||
| shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN)) { | ||
| shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN + | ||
| (SSH2_ED25519_KEY_LEN - x25519_len))) { |
Member
There was a problem hiding this comment.
Suggested change
| (SSH2_ED25519_KEY_LEN - x25519_len))) { | |
| SSH2_ED25519_KEY_LEN - x25519_len)) { |
tiny nit: drop redundant parentheses.
Contributor
Author
There was a problem hiding this comment.
Good catch, dropped the parentheses and pushed.
Member
|
Nice catch! |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused change correctly prevents uninitialized reads and restores fixed-width secret serialization.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes intermittent ML-KEM/X25519 key exchange failures caused by truncated leading-zero bytes.
Changes:
- Validates the X25519 secret length.
- Zero-pads and right-aligns the secret before hashing.
| File | Description |
|---|---|
src/kex.c |
Correctly serializes the fixed-width X25519 shared secret. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
vszakats
approved these changes
Oct 2, 2026
Member
|
Thank you! |
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
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.
In
kex_mlkem768x25519_sha256the x25519 half of the hybrid shared secret is written into a fixed 64-byte buffer withssh2_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 siblingkex_mlkem_nistpsizes its heap buffer to the bignum length, so it has no uninitialized read.Possibly related bug: #1644 (comment)
Follow-up to 3ba252e #1644