Commit e4f7778
authored
sm2: fix SM2PKE biased
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`.k generation [SECURITY] (#1600)1 parent f9b5009 commit e4f7778
1 file changed
+5
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
| 12 | + | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
| 174 | + | |
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
0 commit comments