Skip to content

Handle even moduli in sp_exptmod_nct for RSA public-only builds - #11849

Open
stenslae wants to merge 3 commits into
wolfSSL:masterfrom
stenslae:fix/sp-exptmod-nct-even-mod
Open

stenslae wants to merge 3 commits into
wolfSSL:masterfrom
stenslae:fix/sp-exptmod-nct-even-mod

Conversation

@stenslae

Copy link
Copy Markdown
Member

Description

In RSA public-only and verify-only builds (WOLFSSL_RSA_PUBLIC_ONLY/WOLFSSL_RSA_VERIFY_ONLY), constant-time exponentiation (_sp_exptmod_ex) is disabled to reduce code size. Montgomery modular exponentiation in sp_exptmod_nct only supports odd moduli, leaving no handler for even moduli in these configurations.

  • Implemented _sp_exptmod_nct_generic() in sp_int.c.
  • Routed mp_iseven(m) cases in sp_exptmod_nct to this fallback when _sp_exptmod_ex is omitted.

Testing

  • Extended mp_test_exptmod() in test.c to test even moduli against both mp_exptmod_ex() and mp_exptmod_nct().
  • Added cryptonly-rsapub-no-dh-public-mp test configuration to os-check-linux.json to ensure automated CI coverage for this configuration.

Checklist

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

@stenslae stenslae self-assigned this Oct 21, 2026
Copilot AI balanced review requested due to automatic review settings October 21, 2026 19:17

@claude claude 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@stenslae

Copy link
Copy Markdown
Member Author

@claude review

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.

🔵 Needs a closer look

Cryptographic arithmetic across feature-gated build variants needs final human validation despite no identified blocking findings.

No open issues 🎉

What changed in this PR

Adds even-modulus exponentiation support for RSA public-only and verify-only builds where the constant-time implementation is omitted.

Changes:

  • Adds a non-constant-time fallback for even moduli.
  • Extends regression tests with varied bases, exponents, and even moduli.
  • Adds CI coverage for RSA public-only builds with public multiprecision APIs enabled.
File Description
wolfcrypt/​test/​test.c Tests even-modulus exponentiation through both APIs.
wolfcrypt/​src/​sp_int.c Implements and routes calls to the even-modulus fallback.
.github/​configs/​os-check-linux.json Adds a targeted RSA public-only CI configuration.

Review effort: Balanced

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): .github/configs/os-check-linux.json
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

@claude claude 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.

Code review found no issues

No high-confidence issues detected in this change.

@stenslae stenslae assigned wolfSSL-Bot and unassigned stenslae Oct 21, 2026
@SparkiDev

Copy link
Copy Markdown
Contributor

Why are we handling even modulus values???
RSA/DH/ECC are not allowed to have even modulus values.

@stenslae

stenslae commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Why are we handling even modulus values??? RSA/DH/ECC are not allowed to have even modulus values.

@SparkiDev
This isn't for RSA, which already rejects an even n. It's for the generic mp_exptmod API, which already supports even moduli, and mp_test tests that on master. With --enable-rsapub --disable-dh, the even-modulus branch is compiled out, so an even m goes through Montgomery and returns a wrong result with MP_OKAY.

If you'd rather not support even moduli there, I can return MP_VAL instead and skip that test for this build. Which do you prefer?

@stenslae
stenslae force-pushed the fix/sp-exptmod-nct-even-mod branch from 9d3f862 to dcdf6d2 Compare November 6, 2026 20:37
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot November 6, 2026 20:37
@stenslae

stenslae commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

@claude review

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.

🟡 Changes recommended

The branch rejects even moduli with MP_VAL instead of providing the promised fallback, so either the fallback must be implemented or the PR description corrected.

1 open issue

Review effort: Balanced

Comment thread wolfcrypt/src/sp_int.c Resolved

@claude claude 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.

I reviewed the reworked version of this PR and didn't find any bugs. It changes the result of sp_exptmod_nct for even moduli in RSA public-only and verify-only builds, so a maintainer should still sign off on the new MP_VAL behaviour.

What was reviewed:

  • The new even-modulus guard in sp_exptmod_nct matches the guard around both _sp_exptmod_ex definitions (line 13155), so the call can't reach an undefined function.
  • The test's MP_VAL condition is the exact complement wherever sp_exptmod_nct is compiled (WOLFSSL_SP_MATH_ALL or WOLFSSL_HAVE_SP_DH). In non-SP_MATH_ALL builds DH is on, so the generic path is taken.
  • In RSA public-only builds with DH on, an even modulus now goes to the generic path instead of falling through to Montgomery. The two new CI configs cover the DH-off and DH-on cases.
Extended reasoning...

The change touches the non-constant-time modular exponentiation in wolfcrypt/src/sp_int.c, its test in wolfcrypt/test/test.c and two CI build configs. This is crypto code: any caller that passes an even modulus in RSA verify-only or public-only builds without DH now gets MP_VAL instead of a silently wrong result. The approach has changed since my earlier review (the PR description still mentions a new generic fallback that isn't in the current diff), and a maintainer commented before the latest commits. Because this is crypto behaviour with an API-visible error change, approval was not appropriate.

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): .github/configs/os-check-linux.json
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

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.

5 participants