Skip to content

Harden the non-blocking RSA and DH paths - #12398

Open
dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:nb_rsa_dh
Open

dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:nb_rsa_dh

Conversation

@dgarske

@dgarske dgarske commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Description

  • fp_exptmod_nb() now wipes its ladder and digit state when an exponentiation ends or fails. Before, it left the RSA private result in the caller's non-blocking context. (F-15332)
  • wc_RsaFunctionNonBlock() writes the result with the constant-time fixed-length encoder that the blocking path uses. (F-19389)
  • Fastmath non-blocking RSA private operations are now blinded under WC_RSA_BLINDING, as in the blocking path, with blinding run as resumable steps within the time budget. Fastmath and SP non-blocking private operations both return MISSING_RNG_E when there is no RNG. RsaNb gains two mp_ints and a state field in fastmath builds with blinding. (F-19390)
  • The SP non-blocking DH agreement now rejects an empty agreement or one equal to 1, as the blocking path does. (F-15169)
  • _fp_exptmod_ct(), _fp_exptmod_nct() and fp_to_unsigned_bin() wipe their temporaries before freeing them. The fp_to_unsigned_bin_len() fallback no longer reads its copy after freeing it. (F-17661, F-19061)

Resolves F-15332, F-19389, F-19390, F-15169, F-17661, F-19061.

Testing

New and extended tests are in tests/api (test_rsa.c, test_dh.c, test_wolfmath.c). Where the effect can be observed, each test fails without its fix. There is also a test comparing constant-time and regular Montgomery reduction, since that reduction does the new unblinding.

  • Fastmath non-blocking RSA (with and without WC_RSA_NONBLOCK_TIME), SP non-blocking RSA and DH, and fastmath --disable-sp: ASan/UBSan.
  • --enable-all, --enable-wpas, and --enable-cryptonly without RSA or DH, with -Werror.
  • No measurable slowdown in fastmath RSA-2048 private operations.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 02:01
@dgarske dgarske self-assigned this Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 214,232 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m7

  • FLASH: .text +64 B (+0.0%, 214,232 B / 262,144 B, total: 82% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,904 B (+5.0%, 296,484 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

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

Critical issues remain: asynchronous DH can report success with a truncated shared secret, and modular-exponentiation error paths can free secret-bearing memory without wiping it.

4 open issues
What changed in this PR

Hardens wolfCrypt’s non-blocking RSA and DH operations and improves cleanup of sensitive arithmetic state.

Changes:

  • Adds RSA blinding, RNG checks, and constant-time result encoding.
  • Adds DH output-capacity and invalid shared-secret checks.
  • Expands zeroization and regression coverage.
File Description
wolfssl/​wolfcrypt/​rsa.h Stores the non-blocking blinding inverse.
wolfcrypt/​src/​tfm.c Wipes arithmetic state and fixes encoder cleanup.
wolfcrypt/​src/​rsa.c Hardens non-blocking private operations and encoding.
wolfcrypt/​src/​dh.c Adds capacity and shared-secret validation.
tests/​unit-mcdc/​test_rsa_whitebox.c Updates calls for the RNG parameter.
tests/​api/​test_wolfmath.h Registers new arithmetic tests.
tests/​api/​test_wolfmath.c Tests zeroization and Montgomery reduction.
tests/​api/​test_rsa.h Registers the non-blocking RSA test.
tests/​api/​test_rsa.c Tests RNG requirements, results, and cleanup.
tests/​api/​test_dh.h Registers the short-buffer test.
tests/​api/​test_dh.c Tests short buffers and invalid agreements.

Review effort: Balanced

Comment thread wolfcrypt/src/dh.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread tests/api/test_rsa.c Resolved
Comment thread wolfcrypt/src/rsa.c Resolved
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