{"record":{"id":"cd60b6e08201d419","repo":"gchq/CyberChef","slug":"iv-must-be-8-bytes-currently-iv-length-by-cd60b6","errorCode":null,"errorMessage":"IV must be 8 bytes (currently \" + iv.length + \" bytes)","messagePattern":"IV must be 8 bytes \\(currently \" \\+ iv\\.length \\+ \" bytes\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AESKeyWrap.mjs","lineNumber":71,"sourceCode":"        ];\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++) {\n            for (let i = 0; i < R.length; i++) {\n                cipher.start();\n                cipher.update(forge.util.createBuffer(A + R[i]));","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AESKeyWrap.mjs#L53-L89","documentation":"RFC 3394 wrap uses a fixed 8-byte IV/integrity value (conventionally A6A6A6A6A6A6A6A6). AESKeyWrap requires args[1] to convert to exactly 8 bytes and throws otherwise.","triggerScenarios":"The IV argument converts to a byte string whose length is not 8 — e.g. the default A6A6A6A6A6A6A6A6 entered as hex while the format is UTF8 (16 bytes), or any other length supplied.","commonSituations":"IV format option (Hex/UTF8) does not match the pasted string; user entered a 16-byte AES-CBC-style IV by mistake; IV field left blank.","solutions":["Use the standard 8-byte IV A6A6A6A6A6A6A6A6 with IV format Hex (→ 8 bytes).","Supply the exact 8-byte custom IV in the matching format if you need a non-default value.","Re-check the IV format option against the string you entered."],"exampleFix":"// before: IV \"A6A6A6A6A6A6A6A6\" format UTF8 → 16 bytes → throws\n// after: set IV format to \"Hex\" → 8 bytes","handlingStrategy":"validation","validationCode":"function validateWrapIv(ivBytes) {\n  if (ivBytes.length !== 8) {\n    throw new Error(`AES-KW IV must be 8 bytes, got ${ivBytes.length}`);\n  }\n}","typeGuard":"function isWrapIv(bytes) { return bytes instanceof Uint8Array && bytes.length === 8; }","tryCatchPattern":"try { aesKeyWrap(...); } catch (e) { if (/IV must be 8 bytes/.test(e.message)) {/* fix IV format */} else throw e; }","preventionTips":["Use the standard A6A6A6A6A6A6A6A6 IV in Hex format.","Verify the IV format option matches the pasted value.","Keep the 8-byte wrap IV distinct from a 16-byte AES-CBC IV."],"tags":["aes","key-wrap","iv","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}