{"record":{"id":"2e94bd825dd54f43","repo":"gchq/CyberChef","slug":"kek-must-be-either-16-24-or-32-bytes-currently-2e94bd","errorCode":null,"errorMessage":"KEK must be either 16, 24, or 32 bytes (currently \" + kek.length + \" bytes)","messagePattern":"KEK must be either 16, 24, or 32 bytes \\(currently \" \\+ kek\\.length \\+ \" bytes\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AESKeyWrap.mjs","lineNumber":68,"sourceCode":"                \"type\": \"option\",\n                \"value\": [\"Hex\", \"Raw\"]\n            },\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const kek = Utils.convertToByteString(args[0].string, args[0].option),\n            iv = Utils.convertToByteString(args[1].string, args[1].option),\n            inputType = args[2],\n            outputType = args[3];\n\n        if (kek.length !== 16 && kek.length !== 24 && kek.length !== 32) {\n            throw new OperationError(\"KEK must be either 16, 24, or 32 bytes (currently \" + kek.length + \" bytes)\");\n        }\n        if (iv.length !== 8) {\n            throw new OperationError(\"IV must be 8 bytes (currently \" + iv.length + \" bytes)\");\n        }\n        const inputData = Utils.convertToByteString(input, inputType);\n        if (inputData.length % 8 !== 0 || inputData.length < 16) {\n            throw new OperationError(\"input must be 8n (n>=2) bytes (currently \" + inputData.length + \" bytes)\");\n        }\n\n        const cipher = forge.cipher.createCipher(\"AES-ECB\", kek);\n\n        let A = iv;\n        const R = [];\n        for (let i = 0; i < inputData.length; i += 8) {\n            R.push(inputData.substring(i, i + 8));\n        }\n        let cntLower = 1, cntUpper = 0;\n        for (let j = 0; j < 6; j++) {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AESKeyWrap.mjs#L50-L86","documentation":"AESKeyWrap (RFC 3394) uses an AES Key-Encryption-Key whose size must be a valid AES key size: 16, 24, or 32 bytes. This is the same constraint enforced by the unwrap operation, validated before any wrap work.","triggerScenarios":"args[0] (KEK) converted to a byte string yields a length outside {16, 24, 32}. Typically a Key Format option mismatch on the KEK field.","commonSituations":"KEK supplied as hex with format UTF8 (or vice versa); short passphrase used directly as KEK; KEK from another system whose length does not match a valid AES key size.","solutions":["Set the KEK format option to match its representation (Hex/Base64/UTF8).","Confirm the KEK is exactly 16, 24, or 32 bytes.","Derive the KEK through a KDF that pins the output length."],"exampleFix":"// before: 16-char KEK with format Hex → 8 bytes → throws\n// after: set KEK format to \"UTF8\" (raw 16 bytes)","handlingStrategy":"validation","validationCode":"function validateKek(kekBytes) {\n  if (![16, 24, 32].includes(kekBytes.length)) {\n    throw new Error(`KEK must be 16/24/32 bytes, got ${kekBytes.length}`);\n  }\n}","typeGuard":"function isAesKek(bytes) { return bytes instanceof Uint8Array && [16, 24, 32].includes(bytes.length); }","tryCatchPattern":"try { aesKeyWrap(...); } catch (e) { if (/KEK must be/.test(e.message)) {/* fix KEK format */} else throw e; }","preventionTips":["Match the KEK format option to its representation.","Pin KDF output to a valid AES key length.","Assert KEK length before wrapping."],"tags":["aes","key-wrap","kek","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}