[crypto] Harden RSA key import CRT checks with random mask #226
Draft
mkannwischer wants to merge 2 commits intomasterfrom
Draft
[crypto] Harden RSA key import CRT checks with random mask #226mkannwischer wants to merge 2 commits intomasterfrom
mkannwischer wants to merge 2 commits intomasterfrom
Conversation
Multiply a nonzero random mask into the d_p, d_q, and i_q validity checks. Instead of computing e * d_p mod (p-1) and comparing to 1, we now computes r * e * d_p mod (p-1) and the C side compares against r. This avoids the multi-limb value 1 as an intermediate or comparison target, hardening the check against fault injection. Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
The existing tests only checked rejection of an invalid d_p. Add test cases for invalid d_q and i_q as well, each constructed by flipping an arbitrary single bit in the valid test vector. Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
mkannwischer
commented
Mar 25, 2026
Comment on lines
+225
to
+227
| bn.wsrr w20, URND | ||
| bn.rshi w20, w31, w20 >> 1 | ||
| bn.addi w20, w20, 1 |
Contributor
Author
There was a problem hiding this comment.
Is a 256-bit (or rather-255 bit) random mask sufficient here, or do we need a full-width masks?
If we need a full width masks, then we would need different masks mod p, mod q, and mod p-1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As proposed in #170, this PR
multiplies a nonzero random mask into the d_p, d_q, and i_q
validity checks. Instead of computing e * d_p mod (p-1) and
comparing to 1, we now computes r * e * d_p mod (p-1) and
the C side compares against r.
This avoids the multi-limb value 1 as an intermediate or
comparison target, hardening the check against fault injection.
I have also added additional negative tests that test these failures cases for invalid d_q and i_q.