{"record":{"id":"b8bc349090151874","repo":"gchq/CyberChef","slug":"unknown-padding-type-padding-b8bc34","errorCode":null,"errorMessage":"Unknown padding type: ${padding}","messagePattern":"Unknown padding type: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Twofish.mjs","lineNumber":381,"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":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Twofish.mjs#L363-L399","documentation":"Thrown by applyPadding() in Twofish.mjs (the `default` switch branch) when the `padding` argument is not one of \"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\". The function refuses to apply an unknown padding scheme rather than silently leaving data unaligned, which would produce ciphertext that cannot be decrypted correctly.","triggerScenarios":"Calling encryptTwofish() with a padding string outside the supported set: typos (\"PKCS7\", \"pkcs5\", \"None\", \"ZEROS\"), constants borrowed from another library (\"Iso10126\", \"AnsiX923\", \"Space\"), or undefined/null coerced to string.","commonSituations":"Recipe/config deserialisation after a rename; UI dropdown emitting a value not on the whitelist; cross-library code reuse where the padding vocabulary differs.","solutions":["Pass one of the exactly-cased supported padding values: \"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\".","Note PKCS#7 is functionally PKCS#5 for 16-byte blocks — use \"PKCS5\" here.","Normalise and whitelist the value before calling encryptTwofish so an invalid choice fails early with a clearer message."],"exampleFix":"// before\nencryptTwofish(msg, key, iv, \"CBC\", \"PKCS7\");\n// after\nencryptTwofish(msg, key, iv, \"CBC\", \"PKCS5\");","handlingStrategy":"validation","validationCode":"const PADDING = [\"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\"];\nconst safePadding = String(padding ?? \"\").trim().toUpperCase();\nif (!PADDING.includes(safePadding)) {\n    throw new Error(`Unsupported Twofish padding: '${padding}'. Use one of ${PADDING.join(\", \")}`);\n}\nencryptTwofish(msg, key, iv, mode, safePadding);","typeGuard":"function isTwofishPadding(p) {\n    return typeof p === \"string\" &&\n        [\"NO\", \"PKCS5\", \"ZERO\", \"RANDOM\", \"BIT\"].includes(p);\n}","tryCatchPattern":"try {\n    ct = encryptTwofish(msg, key, iv, mode, padding);\n} catch (e) {\n    if (e instanceof OperationError && /Unknown padding type/.test(e.message)) {\n        return { error: `Padding '${padding}' is not supported. Note PKCS#7 == PKCS#5 here.` };\n    }\n    throw e;\n}","preventionTips":["Map external padding names to this library's vocabulary (PKCS7 -> PKCS5) at your boundary.","Centralise padding constants in one module.","Whitelist user/config padding values before they reach the cipher."],"tags":["crypto","twofish","padding","argument-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}