Skip to content

Fix RSA state machine for the pending crypto operations - #12289

Open
yosuke-wolfssl wants to merge 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-rsa-state
Open

yosuke-wolfssl wants to merge 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-rsa-state

Conversation

@yosuke-wolfssl

@yosuke-wolfssl yosuke-wolfssl commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Two bugs in how an RsaKey resumes an operation after it returns WC_PENDING_E:

  • The input range checks in wc_RsaFunction_ex() returned before the shared error handler that resets key->state and frees key->data.
  • When a pending crypto callback is finished by calling the API again, RsaPublicEncryptEx(), RsaPrivateDecryptEx() and wc_RsaDirect() still moved past the exponentiation on WC_PENDING_E. The next call never reached the callback and un-padded its own input.

Fix (wolfcrypt/src/rsa.c, wolfcrypt/src/asn.c)

  • wc_RsaFunction_ex(): the range checks set ret and fall through to the shared error handler.
  • The three RSA state machines move past the exponentiation on WC_PENDING_E only with WOLFSSL_ASYNC_CRYPT_SW, HAVE_INTEL_QA or HAVE_CAVIUM, the same split as WOLFSSL_ASYNC_REINVOKE.
  • ConfirmSignature() sets up the event at the start of SIG_STATE_DO on every entry in those builds. The block in SIG_STATE_KEY is unchanged, so builds where the poll finishes the job compile as before (Nitrox reads event.ret on resume).
  • FreeSignatureCtx: Clears sigCtx->asyncDev along with the freed key for WOLFSSL_ASYNC_CRYPT.

Behaviour change: a callback that computes the result and then returns WC_PENDING_E is now called again for RSA.

Closes f-19064 and f-19075.

Tests

Test Checks
test_wc_RsaOaep_AsyncRangeFailReset after an OAEP ciphertext of 1 is refused on an async-SW key, the next decrypt returns its own plaintext
test_wc_RsaPending_CryptoCbReinvoke with a callback that pends once, then twice: a bare block fails verification; a real signature verifies; device ciphertext decrypts in software
test_tls13_cryptocb_async new pend depth and RSA target; RSA at depths 1 and 2, ECDSA verify at depth 2; asserts no device job is left behind

rsa_pending_test_cb now pends first and computes when called again.

Verification

  • Before the fix, all three failed and passed with the fix.
  • --enable-all --enable-asynccrypt, GCC -Terror: unit.test 3313 tests, 0 failed.
  • --enable-all --enable-asynccrypt-sw: the async-SW test passes; the only failures are 2 AES-GCM nonce tests that also fail on master.
  • --enable-asynccrypt --enable-cryptocb, with and without WOLF_CRYPTO_CB_ASYNC_POLL: rsa, tls13 and async groups 0 failed. Clean under ASan + UBSan, and LeakSanitizer on Linux.
  • --enable-cryptocb, --enable-all and the default build: rsa group and testwolfcrypt pass; default make check 0 failed.

Not in this PR

  • Builds with both a backend whose poll finishes the job and a crypto callback that pends still move past the exponentiation on the callback's pend.
  • A length bound in the public wc_RsaUnPad_ex().

- The input range checks in wc_RsaFunction_ex() set ret and fall
  through to the shared error handler.
- Add test_wc_RsaOaep_AsyncRangeFailReset.

Issue: F-19064
@yosuke-wolfssl yosuke-wolfssl self-assigned this Nov 26, 2026
Copilot AI balanced review requested due to automatic review settings November 26, 2026 23:58
@yosuke-wolfssl yosuke-wolfssl changed the title Fix/f rsa state Fix RSA state machine for the pending crypto operations Nov 26, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text -64 B (-0.0%, 211,565 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text -64 B (-0.0%, 804,168 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text -64 B (-0.0%, 318,320 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text -64 B (-0.0%, 348,776 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text -64 B (-0.0%, 254,663 B / 262,144 B, total: 97% used)

stm32-sim-stm32h753

  • FLASH: .text -128 B (-0.1%, 209,964 B / 2,097,152 B, total: 10% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,816 B (+4.9%, 296,396 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

The new guard enables RSA handshake tests in public-only and verify-only builds where RSA signing is unavailable, causing failures in supported configurations unless both restricted modes are excluded.

1 open issue
What changed in this PR

This PR fixes RSA operation recovery after asynchronous validation failures and pending crypto callbacks, including signature verification during TLS handshakes.

Changes:

  • Routes RSA range-check failures through shared state and buffer cleanup.
  • Preserves callback retry states and reinitializes signature events where reinvocation is required.
  • Adds regression tests for key reuse after failure and repeated pending callbacks.
File Description
wolfcrypt/​src/​rsa.c Fixes error cleanup and pending-operation state transitions.
wolfcrypt/​src/​asn.c Reinitializes async events when retrying signature verification.
tests/​api/​test_tls13.c Adds RSA callback tests and repeated-pending handshake coverage.
tests/​api/​test_rsa.h Declares and registers the new RSA regression tests.
tests/​api/​test_rsa.c Tests range-failure recovery and callback reinvocation.

Review effort: Balanced

Comment thread tests/api/test_tls13.c Resolved
@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread wolfcrypt/src/asn.c Resolved
- wc_RsaDirect(), RsaPublicEncryptEx() and RsaPrivateDecryptEx()
  move past the exponentiation on WC_PENDING_E only with
  WOLFSSL_ASYNC_CRYPT_SW, HAVE_INTEL_QA or HAVE_CAVIUM.
- ConfirmSignature() sets up the async event at the start of
  SIG_STATE_DO when none of those backends is built in.
- tests: add test_wc_RsaPending_CryptoCbReinvoke, extend
  rsa_pending_test_cb, and stop test_rsa_oaep_async after
  TEST_RSA_PENDING_MAX_CALLS.

Issue: F-19075
- FreeSignatureCtx() sets sigCtx->asyncDev to NULL along with
  freeing the key it points into.

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_rsa.h
Coverage: 4 of 4 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.

4 participants