sm2: fix SM2PKE biased k generation [SECURITY]
#1600
Merged
+5
−14
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.
This fixes a critical security vulnerability originally reported as GHSA-w3g8-fp6j-wvqw by @XlabAITeam.
Previously the module defined its own
next_kfunction which invoked theRandomBits::try_random_bitsAPI to generate akvalue via rejection sampling, however it was passing previous calculation of the number of bytes to generate to an API expecting a number of bits. This bits/bytes confusion lead to highly biased values forkconsisting of mostly zeros.Such a bias can be exploited for full plaintext recovery, and potentially key recovery. All ciphertexts ever encrypted using this API are vulnerable.
This commit replaces the
next_kfunction with the recently addedGeneratetrait (#1586), usingNonZeroScalar::try_generate_from_rngto generatekwhich is backed by the rejection sampling implementation fromcrypto-bigint.