{"record":{"id":"0368af4b523978d3","repo":"gchq/CyberChef","slug":"unknown-padding-type-padding","errorCode":null,"errorMessage":"Unknown padding type: ${padding}","messagePattern":"Unknown padding type: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Present.mjs","lineNumber":266,"sourceCode":"                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;\n\n        case \"BIT\":\n            paddedMessage.push(0x80);\n            for (let i = 1; i < nPadding; i++) {\n                paddedMessage.push(0);\n            }\n            break;\n\n        default:\n            throw new OperationError(`Unknown padding type: ${padding}`);\n    }\n\n    return paddedMessage;\n}\n\n/**\n * Remove padding from message\n * @param {number[]} message - Padded message\n * @param {string} padding - Padding type (\"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\")\n * @param {number} blockSize - Block size in bytes\n * @returns {number[]} - Unpadded message\n */\nfunction removePadding(message, padding, blockSize) {\n    if (message.length === 0) return message;\n\n    switch (padding) {\n        case \"NO\":\n        case \"ZERO\":","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Present.mjs#L248-L284","documentation":"applyPadding's switch has cases for NO, PKCS5, ZERO, RANDOM, BIT. The default case rejects any other padding string so the cipher never silently picks an unintended scheme. This is a programmer/API error rather than a data error.","triggerScenarios":"Calling applyPadding (via encryptPRESENT) with a padding argument outside the supported set: typos like 'PKCS7', 'None', lowercase 'pkcs5', 'CTS', 'ANSI', or passing undefined/null.","commonSituations":"Recipe/JSON config with a typo or wrong casing; copying a padding name from an AES recipe ('PKCS7') into a PRESENT recipe; version mismatch where an older caller sends a value the newer library removed; passing the empty default from an unconfigured dropdown.","solutions":["Use one of the documented values exactly: 'NO', 'PKCS5', 'ZERO', 'RANDOM', 'BIT' (case-sensitive).","Constrain the UI dropdown to FROM/TO padding options so users cannot type arbitrary values.","Normalize input before calling: padding = padding.toUpperCase(); validate against a Set of allowed values.","Default to 'PKCS5' when the incoming value is missing or unrecognized."],"exampleFix":"// before\nencryptPRESENT(msg, key, iv, 'ECB', 'PKCS7'); // typo -> Unknown padding type\n\n// after\nconst ALLOWED = new Set(['NO','PKCS5','ZERO','RANDOM','BIT']);\nconst safePad = ALLOWED.has(padding) ? padding : 'PKCS5';\nencryptPRESENT(msg, key, iv, 'ECB', safePad);","handlingStrategy":"validation","validationCode":"const PRESENT_PADS = new Set(['NO','PKCS5','ZERO','RANDOM','BIT']);\nfunction normalizePadding(p) {\n  const up = String(p || '').toUpperCase();\n  return PRESENT_PADS.has(up) ? up : 'PKCS5';\n}\n\nencryptPRESENT(msg, key, iv, mode, normalizePadding(padding));","typeGuard":"function isPresentPadding(x): x is 'NO'|'PKCS5'|'ZERO'|'RANDOM'|'BIT' {\n  return ['NO','PKCS5','ZERO','RANDOM','BIT'].includes(x);\n}","tryCatchPattern":null,"preventionTips":["Restrict UI selectors to the five supported values.","Uppercase and validate against a Set before calling.","Default to 'PKCS5' for unrecognized values rather than failing.","Do not copy padding names from AES recipes (e.g. 'PKCS7') into PRESENT recipes."],"tags":["cryptography","present","padding","api-misuse","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}