Skip to content

Conversation

gilles-peskine-arm
Copy link
Contributor

Minor improvements to the protection against Lenstra's glitch attack in RSA:

  • This was done twice for PKCS#1v1.5 signatures. Keep only one instance.
  • Make it clear when looking at the code what is done and why. (We were temporarily worried that it wasn't done for PSS signatures, until we found the code that does it.)
  • Use the “unsafe” exponentiation function that leaks the public exponent, but is faster.
  • Don't do it when MBEDTLS_RSA_NO_CRT is enabled: it's pointless.

Note: I did 3.6 first out of habit, but do we actually want all of these changes in the LTS? Or just add a comment?

PR checklist

  • changelog not required because: just a maintainability and performance improvement
  • development PR not required because: crypto only
  • TF-PSA-Crypto PR TODO
  • framework PR not required
  • 3.6 PR here
  • tests provided

After doing an RSA private-key operation, we do the public-key operation.
This protects against a glitch attack (Lenstra 1996) against signatures when
using CRT coefficients to optimize the private-key operation. Glitch attacks
are normally outside our threat model, but this one is especially easy to
exploit so we defend against it.

For historical reasons we ended up having this protection twice: once inside
`mbedtls_rsa_private()` (before unblinding), and another one in
`mbedtls_rsa_rsassa_pkcs1_v15_sign()` (but not in PSS signature). Keep only
the one that's done systematically in `mbedtls_rsa_private()`.

Signed-off-by: Gilles Peskine <[email protected]>
It's protection against Lenstra's glitch attack on CRT exponentiation.

Signed-off-by: Gilles Peskine <[email protected]>
Slight performance improvement.

Signed-off-by: Gilles Peskine <[email protected]>
The glitch attack it protects against is specifically on the algorithm that
uses the CRT coefficients.

Slight performance improvement when `MBEDTLS_RSA_NO_CRT` is enabled.

Signed-off-by: Gilles Peskine <[email protected]>
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-reviewer This PR needs someone to pick it up for review priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most) labels Jul 28, 2025
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's there is good, but I think the PKCS#1 v1.5 sign function needs further cleanup.

}

MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but I think this function's cleanup is not complete:

  • the comment a few lines above needs updating
  • we no longer need verif
  • I think we don't even need sig_try now.

Signed-off-by: Gilles Peskine <[email protected]>
`mbedtls_rsa_private()` is ok with aliasing the output with the input.

Signed-off-by: Gilles Peskine <[email protected]>
@gilles-peskine-arm gilles-peskine-arm requested a review from mpg July 30, 2025 13:40
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Jul 30, 2025
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mpg mpg added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review needs-backports Backports are missing or are pending review and approval. and removed needs-work labels Jul 31, 2025
@mpg
Copy link
Contributor

mpg commented Jul 31, 2025

Note: I did 3.6 first out of habit, but do we actually want all of these changes in the LTS? Or just add a comment?

I think it's good to have in 3.6 as well.

As a side benefit, it should save a bit of code size. Since we need to increase code size from time to time for security fixes, it's good to also have some size savings from time to time to partially compensate.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Jul 31, 2025
@gilles-peskine-arm
Copy link
Contributor Author

The CI is unhappy. I think it's because the private-key operation now calls the unsafe exponentiation (the one that leaks the exponent). It's ok from a security perspective, because we're calling the safe one with the private exponent(s) and the unsafe one only with the public exponent. But the check in our test code can't tell that. Fixing this requires some thought and I'm treating this as low-priority so this pull request may be parked for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component-crypto Crypto primitives and low-level interfaces enhancement needs-backports Backports are missing or are pending review and approval. needs-work priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most)

Projects

Status: In Development

Development

Successfully merging this pull request may close these issues.

2 participants