{"record":{"id":"96484260e181327c","repo":"gchq/CyberChef","slug":"no-padding-requested-but-input-length-message-l","errorCode":null,"errorMessage":"No padding requested but input length (${message.length} bytes) is not a multiple of ${BLOCK_SIZE} bytes.","messagePattern":"No padding requested but input length \\((.+?) bytes\\) is not a multiple of (.+?) bytes\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/TEA.mjs","lineNumber":196,"sourceCode":" * @param {number[]} message\n * @param {string} padding - \"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\"\n * @returns {number[]}\n */\nfunction applyPadding(message, padding) {\n    const remainder = message.length % BLOCK_SIZE;\n    if (remainder === 0 && padding !== \"PKCS5\") return [...message];\n\n    const nPadding = (remainder === 0 && padding === \"PKCS5\") ?\n        BLOCK_SIZE :\n        BLOCK_SIZE - remainder;\n\n    if (nPadding === 0) return [...message];\n\n    const padded = [...message];\n\n    switch (padding) {\n        case \"NO\":\n            throw new OperationError(\n                `No padding requested but input length (${message.length} bytes) is not a multiple of ${BLOCK_SIZE} bytes.`\n            );\n        case \"PKCS5\":\n            for (let i = 0; i < nPadding; i++) padded.push(nPadding);\n            break;\n        case \"ZERO\":\n            for (let i = 0; i < nPadding; i++) padded.push(0);\n            break;\n        case \"RANDOM\":\n            for (let i = 0; i < nPadding; i++) padded.push(Math.floor(Math.random() * 256));\n            break;\n        case \"BIT\":\n            padded.push(0x80);\n            for (let i = 1; i < nPadding; i++) padded.push(0);\n            break;\n        default:\n            throw new OperationError(`Unknown padding type: ${padding}`);\n    }","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/TEA.mjs#L178-L214","documentation":"TEA's applyPadding at TEA.mjs:196 refuses to encrypt unaligned input when padding='NO'. TEA uses an 8-byte block (BLOCK_SIZE=8); NO padding is only valid when the message is already a block multiple, otherwise the cipher would silently truncate or misalign.","triggerScenarios":"applyPadding(message, 'NO', ...) called with message.length % 8 != 0. This is invoked from the encrypt path for ECB/CBC modes when the user requests no padding but supplies a non-aligned message.","commonSituations":"Protocol mandates no padding but the framing layer did not pre-pad to 8 bytes; toggling padding to 'NO' without aligning the payload; variable-length records fed directly into ECB.","solutions":["Pre-pad the message to an 8-byte multiple yourself before encrypting with padding='NO'.","Or switch padding to 'PKCS5' (or ZERO/RANDOM/BIT) to let the library handle alignment.","Validate message.length % 8 === 0 at the caller boundary so the error surfaces early."],"exampleFix":"// before\nencryptWithBlockMode(msg, key, iv, \"ECB\", \"NO\"); // msg is 13 bytes\n// after\nconst aligned = msg.concat(new Array((8 - msg.length % 8) % 8).fill(0));\nencryptWithBlockMode(aligned, key, iv, \"ECB\", \"NO\");","handlingStrategy":"validation","validationCode":"const TEA_BLOCK_SIZE = 8;\nfunction assertTeaNoPaddingAligned(message, padding) {\n  if (padding === \"NO\" && message.length % TEA_BLOCK_SIZE !== 0)\n    throw new TypeError(\n      `padding='NO' requires an 8-byte multiple; got ${message.length} bytes`);\n}","typeGuard":"function isTeaBlockAligned(bytes) {\n  return Number.isInteger(bytes.length) && bytes.length % 8 === 0;\n}","tryCatchPattern":"import OperationError from \"../errors/OperationError.mjs\";\ntry {\n  assertTeaNoPaddingAligned(msg, padding);\n  encryptWithBlockMode(msg, key, iv, mode, padding);\n} catch (e) {\n  if (e instanceof OperationError && /No padding requested/.test(e.message)) {\n    // pre-pad to 8 bytes or switch padding to PKCS5/ZERO/RANDOM/BIT\n  } else throw e;\n}","preventionTips":["If you need padding='NO', pre-align the message to an 8-byte multiple.","Otherwise let the library pad by using 'PKCS5'/'ZERO'/'RANDOM'/'BIT'.","TEA's block is 8 bytes (not 16) — size checks must use 8."],"tags":["tea","xtea","cipher","padding","block-mode","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}