gchq/CyberChef · error · OperationError

The key for AES must be either 16, 24, or 32 bytes (currentl

Error message

The key for AES must be either 16, 24, or 32 bytes (currently ${key.length} bytes)

What it means

CMAC over AES uses an AES block cipher whose key must be 128, 192, or 256 bits (16, 24, or 32 bytes). The operation throws when the key length is none of these, because AES-ECB used to derive the CMAC subkeys would reject it.

Source

Thrown at src/core/operations/CMAC.mjs:58

                "value": ["AES", "Triple DES"]
            }
        ];
    }

    /**
     * @param {ArrayBuffer} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const key = Utils.convertToByteString(args[0].string, args[0].option);
        const algo = args[1];

        const info = (function() {
            switch (algo) {
                case "AES":
                    if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
                        throw new OperationError("The key for AES must be either 16, 24, or 32 bytes (currently " + key.length + " bytes)");
                    }
                    return {
                        "algorithm": "AES-ECB",
                        "key": key,
                        "blockSize": 16,
                        "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87]),
                    };
                case "Triple DES":
                    if (key.length !== 16 && key.length !== 24) {
                        throw new OperationError("The key for Triple DES must be 16 or 24 bytes (currently " + key.length + " bytes)");
                    }
                    return {
                        "algorithm": "3DES-ECB",
                        "key": key.length === 16 ? key + key.substring(0, 8) : key,
                        "blockSize": 8,
                        "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0x1b]),
                    };
                default:

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Provide a 16, 24, or 32 byte AES key.
  2. Confirm args[0].option matches the key encoding.
  3. Derive the key with a KDF (e.g. HKDF) to the required length.

Example fix

// before
key option 'UTF8' with 'short'
// after
key option 'Hex' with '000102030405060708090a0b0c0d0e0f'
Defensive patterns

Strategy: validation

Validate before calling

const keyBytes = Utils.convertToByteString(args[0].string, args[0].option);
if (![16, 24, 32].includes(keyBytes.length)) throw new Error('AES-CMAC key must be 16/24/32 bytes');

Type guard

function isAesKeyLen(len) { return [16, 24, 32].includes(len); }

Prevention

When it happens

Trigger: Calling CMAC.run with algo='AES' and a key whose byte length is not 16, 24, or 32.

Common situations: Using a passphrase directly instead of a sized key; wrong key encoding option (hex text read as UTF-8 doubles length); trailing newline in key.

Related errors


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