Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Jan 9, 2026

This fixes a critical security vulnerability originally reported as GHSA-w3g8-fp6j-wvqw by @XlabAITeam.

Previously the module defined its own next_k function which invoked the RandomBits::try_random_bits API to generate a k value 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 for k consisting 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_k function with the recently added Generate trait (#1586), using NonZeroScalar::try_generate_from_rng to generate k which is backed by the rejection sampling implementation from crypto-bigint.

@tarcieri tarcieri added the security Security Vulnerabilities label Jan 9, 2026
This fixes a critical security vulnerability originally reported as
GHSA-w3g8-fp6j-wvqw by @XlabAITeam.

Previously the module defined its own `next_k` function which invoked
the `RandomBits::try_random_bits` API to generate a `k` value 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 for `k`
consisting 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_k` function with the recently added
`Generate` trait (#1586), using `NonZeroScalar::try_generate_from_rng`
which is backed by the rejection sampling implemenatation from
`crypto-bigint`.
@tarcieri tarcieri force-pushed the sm2/fix-pke-biased-k branch from 378d987 to c3296aa Compare January 9, 2026 18:00
@tarcieri tarcieri merged commit e4f7778 into master Jan 9, 2026
13 checks passed
@tarcieri tarcieri deleted the sm2/fix-pke-biased-k branch January 9, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Security Vulnerabilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants