Skip to content

Hash the pad byte of K without a branch - #1283

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

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

Conversation

@ejohnstown

Copy link
Copy Markdown
Contributor

The MSB of the shared secret K decides whether its mpint encoding takes a 0 pad byte. The pad is now derived with masks and hashed with the length in a single update, with no branch on it in wolfSSH (F-8808).

  • HashMpintHeader() replaces the conditional pad update in DoKexDhReply(), SendKexDhReply() and GenerateKey(); it never makes a zero-length hash update, which the SE050 and TI hash ports reject.
  • tests/unit.c covers CreateMpint's pad, leading-zero, empty and NULL-argument cases.

The MSB of the shared secret K decides whether its mpint encoding
takes a 0 pad byte. CreateMpint now derives the pad with masks, and
HashMpintHeader hashes the length and the optional pad as one update of
LENGTH_SZ + kPad bytes: no branch on the pad in wolfSSH, and no
zero-length update, which the SE050 and TI hash ports reject.

- apply to DoKexDhReply, SendKexDhReply, and the in-tree GenerateKey
- add wolfSSH_TestCreateMpint and a unit test of the pad, leading-zero,
  empty-input and NULL-argument cases

Issue: F-8808
Copilot AI balanced review requested due to automatic review settings September 29, 2026 23:07

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

The security-sensitive padded hashing path lacks known-answer test coverage.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Refactors shared-secret mpint hashing to avoid branching on the pad byte.

Changes:

  • Adds branchless mpint header hashing.
  • Updates key exchange and key derivation paths.
  • Adds CreateMpint() edge-case tests.
File Description
src/​internal.c Implements branchless pad handling and hashing.
wolfssh/​internal.h Exposes the internal test wrapper.
tests/​unit.c Tests mpint normalization and validation.

💡 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
pad &= 1;
c32toa(sz + pad, hdr);
hdr[LENGTH_SZ] = 0;
return HashUpdate(hash, type, hdr, LENGTH_SZ + pad);
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.

2 participants