{"record":{"id":"3bf1f4b23d6f1f3f","repo":"gchq/CyberChef","slug":"no-padding-requested-but-input-is-not-a-blocksiz","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":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Present.mjs","lineNumber":238,"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":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Present.mjs#L220-L256","documentation":"applyPadding with padding='NO' requires the message length to already be a multiple of the cipher block size (8 bytes for PRESENT). When remainder != 0 the function refuses to invent padding and throws, telling the caller the input is misaligned. This protects against silently truncating or extending plaintext.","triggerScenarios":"Calling encryptPRESENT(message, key, iv, 'ECB'|'CBC', 'NO') where message.length % 8 !== 0. Typical with arbitrary-length text or partial buffers.","commonSituations":"User selects 'No padding' in a recipe but supplies non-block-aligned input; interop with another cipher implementation that expects zero-padding; forgetting that PRESENT block size is 8 bytes (not 16 like AES).","solutions":["Switch padding to 'PKCS5' (default and most interoperable) or 'BIT' (ISO 7816-4).","Pre-pad/align the message to a multiple of 8 bytes yourself before using 'NO'.","Use 'ZERO' padding if the receiver can tolerate trailing zero bytes.","Confirm PRESENT's 8-byte block size matches the peer implementation's expectation."],"exampleFix":"// before\nencryptPRESENT([1,2,3], key, iv, 'ECB', 'NO'); // 3 bytes -> error\n\n// after\nencryptPRESENT([1,2,3], key, iv, 'ECB', 'PKCS5'); // auto-padded to 8\n// or align manually:\nconst padded = [...msg];\nwhile (padded.length % 8) padded.push(0);\nencryptPRESENT(padded, key, iv, 'ECB', 'NO');","handlingStrategy":"validation","validationCode":"const BLOCKSIZE = 8;\nfunction encryptPresentSafe(msg, key, iv, mode, padding) {\n  if (padding === 'NO' && msg.length % BLOCKSIZE !== 0) {\n    throw new Error('Input is not a multiple of 8 bytes; use PKCS5 or align manually.');\n  }\n  return encryptPRESENT(msg, key, iv, mode, padding);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return encryptPRESENT(msg, key, iv, mode, padding);\n} catch (e) {\n  if (/No padding requested/.test(e.message)) {\n    // recover by switching to PKCS5\n    return encryptPRESENT(msg, key, iv, mode, 'PKCS5');\n  }\n  throw e;\n}","preventionTips":["Use PKCS5 (default) for arbitrary-length input.","If you must use 'NO', pre-align the message to a multiple of 8 bytes yourself.","Remember PRESENT's block size is 8 bytes, not 16 like AES.","Validate msg.length % 8 === 0 in your UI before allowing 'NO' padding."],"tags":["cryptography","present","padding","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}