{"record":{"id":"82b5bf8d3e762e25","repo":"gchq/CyberChef","slug":"invalid-block-cipher-mode-mode","errorCode":null,"errorMessage":"Invalid block cipher mode: ${mode}","messagePattern":"Invalid block cipher mode: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Present.mjs","lineNumber":365,"sourceCode":"                const encrypted = encryptBlock(block, roundKeys);\n                cipherText.push(...bigIntToBytes(encrypted, BLOCKSIZE));\n            }\n            break;\n\n        case \"CBC\": {\n            let ivBlock = bytesToBigInt(iv);\n            for (let i = 0; i < paddedMessage.length; i += BLOCKSIZE) {\n                let block = bytesToBigInt(paddedMessage.slice(i, i + BLOCKSIZE));\n                block ^= ivBlock;\n                const encrypted = encryptBlock(block, roundKeys);\n                cipherText.push(...bigIntToBytes(encrypted, BLOCKSIZE));\n                ivBlock = encrypted;\n            }\n            break;\n        }\n\n        default:\n            throw new OperationError(`Invalid block cipher mode: ${mode}`);\n    }\n\n    return cipherText;\n}\n\n/**\n * Decrypt using PRESENT cipher with specified block mode\n *\n * @param {number[]} cipherText - Ciphertext as byte array\n * @param {number[]} key - Key (10 bytes for 80-bit or 16 bytes for 128-bit)\n * @param {number[]} iv - IV (8 bytes, not used for ECB)\n * @param {string} mode - Block cipher mode (\"ECB\" or \"CBC\")\n * @param {string} padding - Padding type (\"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\")\n * @returns {number[]} - Plaintext as byte array\n */\nexport function decryptPRESENT(cipherText, key, iv, mode = \"ECB\", padding = \"PKCS5\") {\n    if (cipherText.length === 0) return [];\n","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Present.mjs#L347-L383","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Use exactly 'ECB' or 'CBC' (case-sensitive).","For CBC, supply an 8-byte IV; for ECB the IV is ignored.","Normalize the value before calling: mode = (mode||'ECB').toUpperCase().","Restrict the UI mode selector to only ECB/CBC for PRESENT."],"exampleFix":"// before\nencryptPRESENT(msg, key, iv, 'CFB', 'PKCS5'); // unsupported -> error\nencryptPRESENT(msg, key, iv, 'cbc', 'PKCS5');   // wrong case -> error\n\n// after\nconst mode = ['ECB','CBC'].includes((rawMode||'').toUpperCase()) ? rawMode.toUpperCase() : 'ECB';\nencryptPRESENT(msg, key, iv, mode, 'PKCS5');","handlingStrategy":"validation","validationCode":"const PRESENT_MODES = new Set(['ECB','CBC']);\nfunction normalizeMode(m) {\n  const up = String(m || 'ECB').toUpperCase();\n  return PRESENT_MODES.has(up) ? up : 'ECB';\n}\n\nencryptPRESENT(msg, key, iv, normalizeMode(mode), padding);","typeGuard":"function isPresentMode(x): x is 'ECB'|'CBC' {\n  return x === 'ECB' || x === 'CBC';\n}","tryCatchPattern":null,"preventionTips":["Use exactly 'ECB' or 'CBC' (case-sensitive).","Restrict the UI mode selector for PRESENT to ECB/CBC only.","Uppercase and validate before calling; default to ECB when uncertain.","Remember PRESENT does not support CTR/CFB/OFB/GCM - pick another cipher if you need them."],"tags":["cryptography","present","block-mode","api-misuse","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}