SM2 ciphertext authentication uses a prefix-leaking comparison
Closed, ResolvedPublic

Assigned To
Authored By
werner
Tue, Aug 4, 5:09 PM
Subscribers

Description

SM2 ciphertext authentication uses a prefix-leaking comparison

SM2 decryption recomputes the C3 digest and checks it with the ordinary memcmp routine. memcmp may stop at the first differing byte, making rejection time depend on the length of the correct digest prefix. This introduces a timing side channel in ciphertext authentication.

Vulnerable code

In cipher/ecc-sm2.c, function _gcry_ecc_sm2_decrypt:

c3 = mpi_get_opaque (data_c3, &c3_len);
c3_len = (c3_len + 7) / 8;
if (c3_len != mdlen || memcmp (dgst, c3, c3_len) != 0)
  {
    memset (plain, 0, inlen);
    rc = GPG_ERR_INV_DATA;
    goto leave_main;
  }

Why it matters

An attacker must submit many chosen ciphertexts to a stable, low-noise decryption oracle and distinguish small timing differences. Network jitter, allocation, elliptic-curve work, and hashing may swamp the signal, so practical digest recovery was not demonstrated. Nonetheless, authentication comparisons should not disclose correct-prefix length, especially in a decryption path handling adversarial data.

Proposed fix

After safely handling the public length mismatch, compare the full mdlen bytes with Libgcrypt's constant-time equality helper and map every mismatch to the same error. Ensure plaintext clearing remains unconditional on failure. Add correctness tests for mismatches at the first, middle, and last digest bytes, plus a statistical timing regression or instrumentation test confirming the constant-time helper is used.

Related Objects

Event Timeline

werner triaged this task as Normal priority.Tue, Aug 4, 5:09 PM
werner created this task.
werner created this object with visibility "Public (No Login Required)".
werner created this object with edit policy "Contributor (Project)".
gniibe changed the task status from Open to Testing.Wed, Aug 12, 4:54 AM
gniibe mentioned this in Unknown Object (Maniphest Task).Mon, Aug 24, 8:26 AM
werner shifted this object from the Restricted Space space to the S1 Public space.