{"record":{"id":"cef41167f827b71b","repo":"gchq/CyberChef","slug":"kek-must-be-either-16-24-or-32-bytes-currently","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/AESKeyUnwrap.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 < 24) {\n            throw new OperationError(\"input must be 8n (n>=3) bytes (currently \" + inputData.length + \" bytes)\");\n        }\n\n        const cipher = forge.cipher.createCipher(\"AES-ECB\", kek);\n        cipher.start();\n        cipher.update(forge.util.createBuffer(\"\"));\n        cipher.finish();\n        const paddingBlock = cipher.output.getBytes();\n\n        const decipher = forge.cipher.createDecipher(\"AES-ECB\", kek);\n\n        let A = inputData.substring(0, 8);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AESKeyUnwrap.mjs#L50-L86","documentation":"AESKeyUnwrap (RFC 3394) uses an AES Key-Encryption-Key (KEK) whose size must itself be a valid AES key size: 16, 24, or 32 bytes. The KEK length is validated before any unwrap work. This mirrors the standard AES key-size constraint because the KEK drives an AES-ECB cipher.","triggerScenarios":"args[0] converted to a byte string yields a length other than 16, 24, or 32. Usually a Key Format mismatch (Hex/UTF8/Base64) on the KEK field, or a passphrase used directly.","commonSituations":"KEK supplied as hex but the format option left on UTF8 (doubling/halving the byte count); KEK from a different system that was truncated; using a derived key whose output length was not pinned to 16/24/32.","solutions":["Set the KEK format option to match its representation (Hex/Base64/UTF8).","Confirm the KEK byte length is exactly 16, 24, or 32.","Derive the KEK via a KDF that outputs a fixed 16/24/32-byte length."],"exampleFix":"// before: KEK \"a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6a6\" (hex) with format UTF8 → 32 bytes\n// after: set KEK format to \"Hex\" → 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 { aesKeyUnwrap(...); } catch (e) { if (/KEK must be/.test(e.message)) {/* fix KEK format/length */} else throw e; }","preventionTips":["Match the KEK format option to its representation.","Pin KDF output to 16/24/32 bytes.","Assert KEK length before unwrapping."],"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"}