{"record":{"id":"c218d2e1fe5d5e9e","repo":"gchq/CyberChef","slug":"invalid-nonce-length-nonce-length-bytes-sals","errorCode":null,"errorMessage":"Invalid nonce length: ${nonce.length} bytes.\n\nSalsa20 uses a nonce of 8 bytes (64 bits).","messagePattern":"Invalid nonce length: (.+?) bytes\\.\n\nSalsa20 uses a nonce of 8 bytes \\(64 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Salsa20.mjs","lineNumber":91,"sourceCode":"        const key = Utils.convertToByteArray(args[0].string, args[0].option),\n            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\nSalsa20 uses a key of 16 or 32 bytes (128 or 256 bits).`);\n        }\n\n        let counter, nonce;\n        if (nonceType === \"Integer\") {\n            nonce = Utils.intToByteArray(parseInt(args[1].string, 10), 8, \"little\");\n        } else {\n            nonce = Utils.convertToByteArray(args[1].string, args[1].option);\n            if (!(nonce.length === 8)) {\n                throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.\n\nSalsa20 uses a nonce of 8 bytes (64 bits).`);\n            }\n        }\n        counter = Utils.intToByteArray(args[2], 8, \"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, 8, \"little\");\n            const stream = salsa20Block(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++;\n        }","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Salsa20.mjs#L73-L109","documentation":"Thrown by Salsa20 when the nonce (decoded from its toggle) is not exactly 8 bytes, but only when the nonce type is not 'Integer' (the Integer branch builds an 8-byte little-endian nonce from the parsed number). Salsa20 uses a 64-bit nonce.","triggerScenarios":"Nonce toggle set to Hex/UTF8/Latin1/Base64 with a decoded length other than 8 bytes. E.g. a 16-char hex nonce with 'Hex' toggle = 8 bytes (valid), but the same with UTF8 = 16 bytes (fails); a 12-byte nonce (ChaCha-style) always fails.","commonSituations":"Using a ChaCha20 12-byte/24-byte nonce by mistake; toggle mismatch; blank nonce (0 bytes) with a non-Integer toggle; hex nonce with UTF8 toggle.","solutions":["Provide an 8-byte nonce, or switch the nonce toggle to 'Integer' and supply a numeric value (auto-converted to 8 bytes little-endian).","For hex nonces use the 'Hex' toggle so 16 chars decode to 8 bytes.","Do not reuse ChaCha20 nonces; Salsa20 requires exactly 64 bits."],"exampleFix":"// before: 16 hex chars as UTF8 -> 16 bytes\nsalsa20.run(pt, [keyArg, {string:\"0011223344556677\", option:\"UTF8\"}, ...])\n// after\nsalsa20.run(pt, [keyArg, {string:\"0011223344556677\", option:\"Hex\"}, ...])","handlingStrategy":"validation","validationCode":"let nonce;\nif (nonceType === \"Integer\") {\n  nonce = Utils.intToByteArray(parseInt(nonceArg.string, 10), 8, \"little\");\n} else {\n  nonce = Utils.convertToByteArray(nonceArg.string, nonceArg.option);\n  if (nonce.length !== 8) {\n    throw new Error(`Salsa20 nonce must be 8 bytes, got ${nonce.length}`);\n  }\n}","typeGuard":"function isSalsa20NonceValid(nonceArg) {\n  if (nonceArg.option === \"Integer\") return true;\n  return Utils.convertToByteArray(nonceArg.string, nonceArg.option).length === 8;\n}","tryCatchPattern":null,"preventionTips":["Use the 'Integer' nonce type for numeric nonces (auto-sized to 8 bytes).","For byte nonces, decode to exactly 8 bytes; do not reuse ChaCha20's 12-byte nonce."],"tags":["crypto","salsa20","cipher","nonce","operation","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}