{"record":{"id":"36180eb979fe2e97","repo":"gchq/CyberChef","slug":"no-padding-requested-but-input-is-not-a-blocksiz-36180e","errorCode":null,"errorMessage":"No padding requested but input is not a ${blockSize}-byte multiple.","messagePattern":"No padding requested but input is not a (.+?)-byte multiple\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Twofish.mjs","lineNumber":353,"sourceCode":" * @param {number} blockSize - Block size in bytes\n * @returns {number[]} - Padded message\n */\nfunction applyPadding(message, padding, blockSize) {\n    const remainder = message.length % blockSize;\n    let nPadding = remainder === 0 ? 0 : blockSize - remainder;\n\n    // For PKCS5, always add at least one byte (full block if already aligned)\n    if (padding === \"PKCS5\" && remainder === 0) {\n        nPadding = blockSize;\n    }\n\n    if (nPadding === 0) return [...message];\n\n    const paddedMessage = [...message];\n\n    switch (padding) {\n        case \"NO\":\n            throw new OperationError(`No padding requested but input is not a ${blockSize}-byte multiple.`);\n\n        case \"PKCS5\":\n            for (let i = 0; i < nPadding; i++) {\n                paddedMessage.push(nPadding);\n            }\n            break;\n\n        case \"ZERO\":\n            for (let i = 0; i < nPadding; i++) {\n                paddedMessage.push(0);\n            }\n            break;\n\n        case \"RANDOM\":\n            for (let i = 0; i < nPadding; i++) {\n                paddedMessage.push(Math.floor(Math.random() * 256));\n            }\n            break;","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Twofish.mjs#L335-L371","documentation":"Thrown by applyPadding() in Twofish.mjs when padding is \"NO\" but the message length is not a multiple of the 16-byte Twofish block size. ECB and CBC modes require block-aligned input; selecting \"NO\" padding is a promise that the caller has already aligned the data, and the function refuses to silently misalign it.","triggerScenarios":"Calling encryptTwofish() with mode \"ECB\" or \"CBC\" and padding \"NO\" on a message whose length % 16 !== 0. Hit when a caller wants to avoid padding overhead but supplies plaintext that does not fit whole blocks, or when \"NO\" was selected by mistake instead of \"PKCS5\".","commonSituations":"Interoperating with a system that uses no padding but expects block-aligned input; defaulting to \"NO\" in a UI without ensuring alignment; encrypting variable-length fields with ECB.","solutions":["Use \"PKCS5\" padding (the default) for variable-length messages so the library can align them for you.","If you must use \"NO\", pre-pad or pre-truncate the message so message.length % 16 === 0 before calling encryptTwofish.","Switch to a stream mode (CFB/OFB/CTR) which does not require block alignment and ignores the padding argument."],"exampleFix":"// before\nencryptTwofish(msg, key, iv, \"ECB\", \"NO\"); // msg is 20 bytes\n// after\nencryptTwofish(msg, key, iv, \"ECB\", \"PKCS5\");","handlingStrategy":"validation","validationCode":"const BLOCK = 16; // Twofish\nif ((mode === \"ECB\" || mode === \"CBC\") && padding === \"NO\" && msg.length % BLOCK !== 0) {\n    throw new Error(\n        `Message is ${msg.length} bytes; 'NO' padding requires a multiple of ${BLOCK}. ` +\n        `Use 'PKCS5' or pre-align the data.`\n    );\n}\nencryptTwofish(msg, key, iv, mode, padding);","typeGuard":"function canUseNoPadding(msg, mode, blockSize = 16) {\n    return mode !== \"ECB\" && mode !== \"CBC\" || msg.length % blockSize === 0;\n}","tryCatchPattern":"try {\n    ct = encryptTwofish(msg, key, iv, mode, padding);\n} catch (e) {\n    if (e instanceof OperationError && /No padding requested/.test(e.message)) {\n        // auto-upgrade to PKCS5 or pre-pad the message\n        ct = encryptTwofish(msg, key, iv, mode, \"PKCS5\");\n    } else { throw e; }\n}","preventionTips":["Default to PKCS5 for variable-length messages; only use 'NO' when you control alignment.","If 'NO' is required for interop, pre-pad the plaintext to a block multiple yourself.","Prefer stream modes (CFB/OFB/CTR) when you cannot guarantee block alignment."],"tags":["crypto","twofish","padding","block-alignment","argument-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}