gchq/CyberChef · error · OperationError

Unknown padding type: ${padding}

Error message

Unknown padding type: ${padding}

What it means

Thrown by applyPadding() in Twofish.mjs (the `default` switch branch) when the `padding` argument is not one of "NO", "PKCS5", "ZERO", "RANDOM", "BIT". The function refuses to apply an unknown padding scheme rather than silently leaving data unaligned, which would produce ciphertext that cannot be decrypted correctly.

Source

Thrown at src/core/lib/Twofish.mjs:381

                paddedMessage.push(0);
            }
            break;

        case "RANDOM":
            for (let i = 0; i < nPadding; i++) {
                paddedMessage.push(Math.floor(Math.random() * 256));
            }
            break;

        case "BIT":
            paddedMessage.push(0x80);
            for (let i = 1; i < nPadding; i++) {
                paddedMessage.push(0);
            }
            break;

        default:
            throw new OperationError(`Unknown padding type: ${padding}`);
    }

    return paddedMessage;
}

/**
 * Remove padding from message
 * @param {number[]} message - Padded message
 * @param {string} padding - Padding type ("NO", "PKCS5", "ZERO", "RANDOM", "BIT")
 * @param {number} blockSize - Block size in bytes
 * @returns {number[]} - Unpadded message
 */
function removePadding(message, padding, blockSize) {
    if (message.length === 0) return message;

    switch (padding) {
        case "NO":
        case "ZERO":

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Pass one of the exactly-cased supported padding values: "NO", "PKCS5", "ZERO", "RANDOM", "BIT".
  2. Note PKCS#7 is functionally PKCS#5 for 16-byte blocks — use "PKCS5" here.
  3. Normalise and whitelist the value before calling encryptTwofish so an invalid choice fails early with a clearer message.

Example fix

// before
encryptTwofish(msg, key, iv, "CBC", "PKCS7");
// after
encryptTwofish(msg, key, iv, "CBC", "PKCS5");
Defensive patterns

Strategy: validation

Validate before calling

const PADDING = ["NO", "PKCS5", "ZERO", "RANDOM", "BIT"];
const safePadding = String(padding ?? "").trim().toUpperCase();
if (!PADDING.includes(safePadding)) {
    throw new Error(`Unsupported Twofish padding: '${padding}'. Use one of ${PADDING.join(", ")}`);
}
encryptTwofish(msg, key, iv, mode, safePadding);

Type guard

function isTwofishPadding(p) {
    return typeof p === "string" &&
        ["NO", "PKCS5", "ZERO", "RANDOM", "BIT"].includes(p);
}

Try / catch

try {
    ct = encryptTwofish(msg, key, iv, mode, padding);
} catch (e) {
    if (e instanceof OperationError && /Unknown padding type/.test(e.message)) {
        return { error: `Padding '${padding}' is not supported. Note PKCS#7 == PKCS#5 here.` };
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling encryptTwofish() with a padding string outside the supported set: typos ("PKCS7", "pkcs5", "None", "ZEROS"), constants borrowed from another library ("Iso10126", "AnsiX923", "Space"), or undefined/null coerced to string.

Common situations: Recipe/config deserialisation after a rename; UI dropdown emitting a value not on the whitelist; cross-library code reuse where the padding vocabulary differs.

Related errors


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