gchq/CyberChef · error · OperationError

Invalid block cipher mode: ${mode}

Error message

Invalid block cipher mode: ${mode}

What it means

Default branch of the mode switch inside encryptSM4 at SM4.mjs:232. The message uses string concatenation ("..."+mode) rather than a template literal. Any mode value outside {ECB, CBC, CFB, OFB, CTR} reaches this throw.

Source

Thrown at src/core/lib/SM4.mjs:232

                block[0] ^= iv[0]; block[1] ^= iv[1];
                block[2] ^= iv[2]; block[3] ^= iv[3];
                Array.prototype.push.apply(cipherText, intsToBytes(block));
            }
            break;
        case "CTR":
            iv = bytesToInts(iv, 0);
            for (let i = 0; i < message.length; i += BLOCKSIZE) {
                let iv2 = [...iv]; /* containing the IV + counter */
                iv2[3] += (i >> 4);/* Using a 32 bit counter here. 64 Gb encrypts should be enough for everyone. */
                iv2 = encryptBlockSM4(iv2, roundKey);
                const block = bytesToInts(message, i);
                block[0] ^= iv2[0]; block[1] ^= iv2[1];
                block[2] ^= iv2[2]; block[3] ^= iv2[3];
                Array.prototype.push.apply(cipherText, intsToBytes(block));
            }
            break;
        default:
            throw new OperationError("Invalid block cipher mode: "+mode);
    }
    if (mode !== "ECB" && mode !== "CBC")
        return cipherText.slice(0, messageLength);
    return cipherText;
}

/**
 * Decrypt using SM4 using a given block cipher mode.
 *
 * @param {byteArray} cipherText - The ciphertext
 * @param {byteArray} key - The cipher key, 16 bytes.
 * @param {byteArray} iv - The IV or nonce, 16 bytes (not used with ECB mode)
 * @param {string} mode - The block cipher mode "CBC", "ECB", "CFB", "OFB", "CTR"
 * @param {boolean] ignorePadding - If true, ignore padding issues in ECB/CBC mode.
 * @returns {byteArray} - The cipher text.
 */
export function decryptSM4(cipherText, key, iv, mode="ECB", ignorePadding=false) {
    const originalLength = cipherText.length;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Pass one of: 'ECB', 'CBC', 'CFB', 'OFB', 'CTR'.
  2. Normalise the mode (trim + uppercase) and validate against an allowlist before calling encryptSM4.

Example fix

// before
const ct = encryptSM4(msg, key, iv, "cbc");
// after
const ct = encryptSM4(msg, key, iv, "CBC");
Defensive patterns

Strategy: validation

Validate before calling

const SM4_MODES = new Set(["ECB", "CBC", "CFB", "OFB", "CTR"]);
function normaliseSm4Mode(m) {
  const v = String(m).trim().toUpperCase();
  if (!SM4_MODES.has(v)) throw new TypeError(`Unsupported SM4 mode: ${JSON.stringify(m)}`);
  return v;
}

Type guard

function isSm4Mode(v) {
  return typeof v === "string" &&
    ["ECB","CBC","CFB","OFB","CTR"].includes(v.trim().toUpperCase());
}

Try / catch

try {
  encryptSM4(msg, key, iv, normaliseSm4Mode(mode));
} catch (e) {
  if (e instanceof TypeError && /Unsupported SM4 mode/.test(e.message)) {
    // report unsupported mode
  } else throw e;
}

Prevention

When it happens

Trigger: encryptSM4 called with mode not in the supported set. Examples: lowercase 'cbc', 'GCM', an empty string, undefined, or a compound label like 'CBC/PKCS7'.

Common situations: Mode string from external config or UI passed without normalisation; case mismatch; assumption that an unsupported AEAD mode is available.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/084c51aec96bbe2f. Report an issue: GitHub.