Skip to content

Wipe the KEX shared secret once keys are derived - #1275

Open
ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:sf28
Open

ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:sf28

Conversation

@ejohnstown

@ejohnstown ejohnstown commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

The KEX shared secret K stayed in ssh->k until the session was freed. It is now wiped as soon as the session keys are derived, whether the exchange succeeds or fails.

  • DoKexDhReply() and SendKexDhReply() zero all of ssh->k and reset kSz on exit. Wiping the whole buffer also clears the bytes left behind by the CreateMpint shift, a shorter rekey secret and the ML-KEM hybrid hash copy (F-8845, F-11680, F-11681, F-8843, F-11682).
  • GenerateKeys() zeroes both handshake key sets when a derivation fails (F-14001).
  • tests/regress.c checks K after a handshake on each KEX, including the three ML-KEM hybrids, after a rejected host-key signature, and checks the key sets after a failed derivation.

DoKexDhReply() and SendKexDhReply() zero all of ssh->k and reset kSz
on every exit, so K does not outlive the key exchange. GenerateKeys()
zeroes both handshake key sets when a derivation fails.

- the full-buffer wipe clears CreateMpint and ML-KEM hybrid leftovers
- tests/regress.c checks K after each KEX, the hybrids included
- tests/regress.c checks a failed derivation leaves no keys

Issue: F-8845, F-11680, F-11681, F-8843, F-11682, F-14001

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Cleanup misses some exit paths and occurs too late before packet sending.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Wipes KEX shared secrets and failed handshake key material earlier, with regression coverage.

Changes:

  • Clears ssh->k and resets kSz.
  • Clears handshake key sets when derivation fails.
  • Adds KEX and failure-path tests.
File Summary
tests/​regress.c Verifies wiping behavior across handshakes and failures.
src/​internal.c Implements secret and key-material cleanup.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/internal.c
Comment on lines +8923 to +8925
/* K is only needed to derive the keys. Wipe all of it, success or not. */
WS_FORCEZERO(ssh->k, sizeof(ssh->k));
ssh->kSz = 0;
Comment thread tests/regress.c
Comment on lines +1960 to +1963
#ifndef WOLFSSH_NO_CURVE25519_MLKEM768_SHA256
"mlkem768x25519-sha256",
#endif
NULL
@LinuxJedi

Copy link
Copy Markdown
Member

@wolfSSL-Fenrir-bot review

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot 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.

Fenrir Automated Review — PR #1275

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

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.

4 participants