{"record":{"id":"7e68b8b9599b38ab","repo":"gchq/CyberChef","slug":"invalid-nonce-length-nonce-length-bytes-chac","errorCode":null,"errorMessage":"Invalid nonce length: ${nonce.length} bytes.\n\nChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).","messagePattern":"Invalid nonce length: (.+?) bytes\\.\n\nChaCha uses a nonce of 8 or 12 bytes \\(64 or 96 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ChaCha.mjs","lineNumber":170,"sourceCode":"            nonceType = args[1].option,\n            rounds = parseInt(args[3], 10),\n            inputType = args[4],\n            outputType = args[5];\n\n        if (key.length !== 16 && key.length !== 32) {\n            throw new OperationError(`Invalid key length: ${key.length} bytes.\n\nChaCha uses a key of 16 or 32 bytes (128 or 256 bits).`);\n        }\n\n        let counter, nonce, counterLength;\n        if (nonceType === \"Integer\") {\n            nonce = Utils.intToByteArray(parseInt(args[1].string, 10), 12, \"little\");\n            counterLength = 4;\n        } else {\n            nonce = Utils.convertToByteArray(args[1].string, args[1].option);\n            if (!(nonce.length === 12 || nonce.length === 8)) {\n                throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.\n\nChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).`);\n            }\n            counterLength = 16 - nonce.length;\n        }\n        counter = Utils.intToByteArray(args[2], counterLength, \"little\");\n\n        const output = [];\n        input = Utils.convertToByteArray(input, inputType);\n\n        let counterAsInt = Utils.byteArrayToInt(counter, \"little\");\n        for (let i = 0; i < input.length; i += 64) {\n            counter = Utils.intToByteArray(counterAsInt, counterLength, \"little\");\n            const stream = chacha(key, nonce, counter, rounds);\n            for (let j = 0; j < 64 && i + j < input.length; j++) {\n                output.push(input[i + j] ^ stream[j]);\n            }\n            counterAsInt++;","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ChaCha.mjs#L152-L188","documentation":"Thrown by the ChaCha operation when a user-supplied nonce (non-Integer nonce type) does not decode to exactly 8 or 12 bytes. ChaCha accepts a 64-bit (8-byte) or 96-bit (12-byte) nonce; the counter occupies the remaining bytes of the 16-byte counter+nonce block.","triggerScenarios":"ChaCha.run with nonceType !== 'Integer' calls Utils.convertToByteArray on args[1].string; if the result is not length 8 or 12, it throws. Wrong-length hex (not 16/24 chars), wrong Base64 length, or UTF-8 nonce text triggers it. Note: when nonceType === 'Integer' this check is skipped (the integer is forced to 12 bytes).","commonSituations":"User mixes up nonce formats — supplies a 16-byte (32-hex) IV from AES-GCM expecting it to work, uses a 6-byte nonce, or pastes nonce text as UTF-8 instead of hex.","solutions":["Supply exactly 8 bytes (16 hex chars) or 12 bytes (24 hex chars) for the nonce.","Match the nonce input option to its encoding (Hex/Base64).","If you only have an integer nonce, set nonceType to 'Integer' to let the operation size it to 12 bytes.","Ensure counter length = 16 − nonce length fits the algorithm variant you intend."],"exampleFix":"// before — nonce hex \"010203040506070809\" = 9 bytes, throws\n// after — 12 bytes\n// nonce (hex): \"0102030405060708090a0b0c\"","handlingStrategy":"validation","validationCode":"if (nonceType !== \"Integer\") {\n  const nonce = Utils.convertToByteArray(args[1].string, args[1].option);\n  if (!(nonce.length === 12 || nonce.length === 8)) { /* reject before run */ }\n}","typeGuard":"function isValidChaChaNonce(nonce) { return nonce.length === 8 || nonce.length === 12; }","tryCatchPattern":"null","preventionTips":["For hex nonces use 16 chars (8 bytes) or 24 chars (12 bytes).","If you only have an integer nonce, set nonceType to 'Integer'.","Match the nonce input option to its encoding.","Avoid reusing AES-GCM 12-byte IVs verbatim without checking length semantics."],"tags":["chacha","cryptography","nonce","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}