Skip to content

Strip leading zeros from prime in wc_DhCheckPubValue - #12173

Open
embhorn wants to merge 1 commit into
wolfSSL:masterfrom
embhorn:zd22857
Open

embhorn wants to merge 1 commit into
wolfSSL:masterfrom
embhorn:zd22857

Conversation

@embhorn

@embhorn embhorn commented Nov 18, 2026

Copy link
Copy Markdown
Member

Description

wc_DhCheckPubValue() strips leading zeros from pub but not from prime. When the prime carries DER-style padding (for example a 2048-bit modulus passed as 257 bytes), a 256-byte y takes the pubSz < primeSz path. The function then returns 0 without comparing magnitudes, so p-1, p and y > p are accepted.

In the TLS path, wc_DhAgree_Sync() still rejects these values through its numeric wc_DhCheckPubKey_ex() check. Direct callers of the public API have no such backstop. Neither do builds using a DhAgreeCb PK callback.

Fixes zd22857

Testing

  • dh_test_check_pubvalue() now runs every existing vector against the prime with zero, one and two leading zero bytes.
  • The new test vectors fail without the fix and pass with it.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@embhorn embhorn self-assigned this Nov 18, 2026
Copilot AI balanced review requested due to automatic review settings November 18, 2026 14:54

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.

🟢 Approval recommended

No active review issues are reported for the Diffie–Hellman public-value validation fix.

No open issues 🎉

What changed in this PR

Fixes wolfCrypt’s Diffie–Hellman public-value validation so leading-zero padding on a prime cannot let invalid public values bypass range checks.

Changes:

  • Strip leading zeros from the prime before comparing lengths in wc_DhCheckPubValue().
  • Run existing valid and invalid test vectors with zero, one, and two leading-zero bytes on the prime.
  • Document rejected values and leading-zero handling.
File Description
wolfcrypt/​test/​test.c Extends public-value regression tests to padded primes.
wolfcrypt/​src/​dh.c Normalizes the prime before validation and documents padding behavior.
doc/​dox_comments/​header_files/​dh.h Clarifies validation bounds and ignored leading zeros.

Review effort: Balanced

@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 #12173

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/dh.h
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: Max

@github-actions

Copy link
Copy Markdown

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