Skip to content

kex: zero-pad x25519 shared secret in mlkem768x25519 - #2633

Closed
abdul-khaliq-khalid wants to merge 2 commits into
libssh2:masterfrom
abdul-khaliq-khalid:mlkem768x25519-shared-secret-pad
Closed

abdul-khaliq-khalid wants to merge 2 commits into
libssh2:masterfrom
abdul-khaliq-khalid:mlkem768x25519-shared-secret-pad

Conversation

@abdul-khaliq-khalid

@abdul-khaliq-khalid abdul-khaliq-khalid commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

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.

Possibly related bug: #1644 (comment)
Follow-up to 3ba252e #1644

Comment thread src/kex.c Outdated
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))) {

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.

Suggested change
(SSH2_ED25519_KEY_LEN - x25519_len))) {
SSH2_ED25519_KEY_LEN - x25519_len)) {

tiny nit: drop redundant parentheses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, dropped the parentheses and pushed.

@vszakats

vszakats commented Oct 1, 2026

Copy link
Copy Markdown
Member

Nice catch!

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.

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 vszakats closed this in 4f2692f Oct 2, 2026
@vszakats

vszakats commented Oct 2, 2026

Copy link
Copy Markdown
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
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.

3 participants