Skip to content

Avoid variable-time inversion of secret primes when computing fwTPM RSA CRT values - #668

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

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

Conversation

@dgarske

@dgarske dgarske commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Description

FwRsaComputeCRT() no longer uses mp_invmod() on secret values. u now comes from mp_exptmod(q, p-2, p), and d from inverting e blinded by a fresh random factor. The resulting key is unchanged. FwRsaComputeCRT() and FwImportReconstructKey() gain an RNG parameter.

Behavior change: TPM2_LoadExternal and TPM2_Import now reject a key whose u*q mod p is not 1. u comes from an exponentiation that is only an inverse for a prime p, so this rejects most, but not all, keys with a composite p. Such keys could not perform valid RSA operations.

Testing

Tested with the fwTPM and against ibmswtpm2. Not tested on hardware.

Resolves F-17134. ZD 22745.

@dgarske dgarske self-assigned this Nov 5, 2026
Copilot AI balanced review requested due to automatic review settings November 5, 2026 18:32

Copilot AI 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.

🟡 Changes recommended

Exponentiation can leak secret prime information in permitted fast-math configurations, and the inverse check accepts some composite p values that produce invalid RSA keys.

2 open issues
What changed in this PR

This PR changes fwTPM RSA CRT computation to reduce timing exposure from inversions involving secret primes.

Changes:

  • Computes the CRT coefficient using modular exponentiation and blinds the private-exponent inversion.
  • Threads an RNG through key reconstruction and command callers.
  • Adds tests for stable CRT results, missing RNGs, and selected invalid factors.
File Description
wolftpm/​fwtpm/​fwtpm_crypto.h Adds RNG parameters to CRT and reconstruction interfaces.
tests/​fwtpm_unit_tests.c Adds CRT consistency and invalid-input tests.
src/​fwtpm/​fwtpm_crypto.c Implements blinded inversion, exponentiation, and inverse verification.
src/​fwtpm/​fwtpm_command.c Passes the context RNG from LoadExternal and Import.

Review effort: Balanced

Comment thread src/fwtpm/fwtpm_crypto.c
}
if (rc == 0) {
rc = mp_invmod(&rsaKey->q, &rsaKey->p, &rsaKey->u);
rc = mp_exptmod(&rsaKey->q, &qm1, &rsaKey->p, &rsaKey->u);
Comment thread src/fwtpm/fwtpm_crypto.c
Comment on lines +3936 to +3937
if (rc == 0 && !mp_isone(&qm1)) {
rc = MP_VAL;
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