Skip to content

Commit e60e991

Browse files
authored
sm2: fix SM2PKE ciphertext parsing DoS [SECURITY] (#1603)
This fixes a potential denial-of-service attack in the SM2PKE decryption implementation originally reported as GHSA-j9xq-69pf-pcm8 by @XlabAITeam Previously the ciphertext was parsed using `split_at` which panics in the event the input is not of sufficient length, leading to a potential DoS if the ciphertext is malformed. The issue was corrected by replacing `split_at` with `split_at_checked` which returns `Option::None` in the event the input is of insufficient length, then propagating an `Error` in such a case.
1 parent 085b7be commit e60e991

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

sm2/src/pke/decrypting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ fn decrypt(
157157
secret_scalar: &Scalar,
158158
mode: Mode,
159159
hasher: &mut dyn DynDigest,
160-
cipher: &[u8],
160+
ciphertext: &[u8],
161161
) -> Result<Vec<u8>> {
162162
let q = U256::from_be_hex(FieldElement::MODULUS);
163163
let c1_len = q.bits().div_ceil(8) * 2 + 1;
164164

165165
// B1: get 𝐶1 from 𝐶
166-
let (c1, c) = cipher.split_at(c1_len as usize);
166+
let (c1, c) = ciphertext.split_at_checked(c1_len as usize).ok_or(Error)?;
167167
let encoded_c1 = EncodedPoint::from_bytes(c1).map_err(Error::from)?;
168168

169169
// verify that point c1 satisfies the elliptic curve
@@ -182,10 +182,10 @@ fn decrypt(
182182
let digest_size = hasher.output_size();
183183
let (c2, c3) = match mode {
184184
Mode::C1C3C2 => {
185-
let (c3, c2) = c.split_at(digest_size);
185+
let (c3, c2) = c.split_at_checked(digest_size).ok_or(Error)?;
186186
(c2, c3)
187187
}
188-
Mode::C1C2C3 => c.split_at(c.len() - digest_size),
188+
Mode::C1C2C3 => c.split_at_checked(c.len() - digest_size).ok_or(Error)?,
189189
};
190190

191191
// B4: compute 𝑡 = 𝐾𝐷𝐹(𝑥2 ∥ 𝑦2, 𝑘𝑙𝑒𝑛)

0 commit comments

Comments
 (0)