gchq/CyberChef · error · OperationError

Invalid block cipher mode: ${mode}

Error message

Invalid block cipher mode: ${mode}

What it means

encryptPRESENT's mode switch only supports 'ECB' and 'CBC'. The default case rejects any other mode string so the cipher does not silently fall back to a default. This is a programmer/API error, not a data error.

Source

Thrown at src/core/lib/Present.mjs:365

                const encrypted = encryptBlock(block, roundKeys);
                cipherText.push(...bigIntToBytes(encrypted, BLOCKSIZE));
            }
            break;

        case "CBC": {
            let ivBlock = bytesToBigInt(iv);
            for (let i = 0; i < paddedMessage.length; i += BLOCKSIZE) {
                let block = bytesToBigInt(paddedMessage.slice(i, i + BLOCKSIZE));
                block ^= ivBlock;
                const encrypted = encryptBlock(block, roundKeys);
                cipherText.push(...bigIntToBytes(encrypted, BLOCKSIZE));
                ivBlock = encrypted;
            }
            break;
        }

        default:
            throw new OperationError(`Invalid block cipher mode: ${mode}`);
    }

    return cipherText;
}

/**
 * Decrypt using PRESENT cipher with specified block mode
 *
 * @param {number[]} cipherText - Ciphertext as byte array
 * @param {number[]} key - Key (10 bytes for 80-bit or 16 bytes for 128-bit)
 * @param {number[]} iv - IV (8 bytes, not used for ECB)
 * @param {string} mode - Block cipher mode ("ECB" or "CBC")
 * @param {string} padding - Padding type ("NO", "PKCS5", "ZERO", "RANDOM", "BIT")
 * @returns {number[]} - Plaintext as byte array
 */
export function decryptPRESENT(cipherText, key, iv, mode = "ECB", padding = "PKCS5") {
    if (cipherText.length === 0) return [];

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Use exactly 'ECB' or 'CBC' (case-sensitive).
  2. For CBC, supply an 8-byte IV; for ECB the IV is ignored.
  3. Normalize the value before calling: mode = (mode||'ECB').toUpperCase().
  4. Restrict the UI mode selector to only ECB/CBC for PRESENT.

Example fix

// before
encryptPRESENT(msg, key, iv, 'CFB', 'PKCS5'); // unsupported -> error
encryptPRESENT(msg, key, iv, 'cbc', 'PKCS5');   // wrong case -> error

// after
const mode = ['ECB','CBC'].includes((rawMode||'').toUpperCase()) ? rawMode.toUpperCase() : 'ECB';
encryptPRESENT(msg, key, iv, mode, 'PKCS5');
Defensive patterns

Strategy: validation

Validate before calling

const PRESENT_MODES = new Set(['ECB','CBC']);
function normalizeMode(m) {
  const up = String(m || 'ECB').toUpperCase();
  return PRESENT_MODES.has(up) ? up : 'ECB';
}

encryptPRESENT(msg, key, iv, normalizeMode(mode), padding);

Type guard

function isPresentMode(x): x is 'ECB'|'CBC' {
  return x === 'ECB' || x === 'CBC';
}

Prevention

When it happens

Trigger: Calling encryptPRESENT(message, key, iv, mode, padding) with mode set to anything other than 'ECB' or 'CBC' - e.g. 'CFB','OFB','CTR','GCM','ecb' (lowercase), or undefined/null.

Common situations: Recipe/JSON config with wrong casing or a typo; copying mode from an AES recipe (which supports CTR/CFB etc.) into PRESENT; unconfigured dropdown; passing the IV as the mode argument (wrong positional order).

Related errors


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