{"record":{"id":"5074295a7c8205ca","repo":"nilaoda/N_m3u8DL-RE","slug":"nonce-length-must-be-allowednoncelength-actual-nonce-length","errorCode":null,"errorMessage":"Nonce length must be {allowedNonceLength}. Actual: {nonce.Length}","messagePattern":"Nonce length must be (.+?)\\. Actual: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/Crypto/CSChaCha20.cs","lineNumber":173,"sourceCode":"        /// <param name=\"nonce\">\n        /// A 12-byte (96-bit) nonce, treated as a concatenation of three 32-bit little-endian integers\n        /// </param>\n        /// <param name=\"counter\">\n        /// A 4-byte (32-bit) block counter, treated as a 32-bit little-endian integer\n        /// </param>\n        private void IvSetup(byte[] nonce, uint counter)\n        {\n            if (nonce == null)\n            {\n                // There has already been some state set up. Clear it before exiting.\n                Dispose();\n                throw new ArgumentNullException(\"Nonce is null\");\n            }\n\n            if (nonce.Length != allowedNonceLength)\n            {\n                // There has already been some state set up. Clear it before exiting.\n                Dispose();\n                throw new ArgumentException($\"Nonce length must be {allowedNonceLength}. Actual: {nonce.Length}\");\n            }\n\n            state[12] = counter;\n            state[13] = Util.U8To32Little(nonce, 0);\n            state[14] = Util.U8To32Little(nonce, 4);\n            state[15] = Util.U8To32Little(nonce, 8);\n        }\n\n\n        #region Encryption methods\n\n        /// <summary>\n        /// Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.\n        /// </summary>\n        /// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>\n        /// <param name=\"output\">Output byte array, must have enough bytes</param>\n        /// <param name=\"input\">Input byte array</param>","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/Crypto/CSChaCha20.cs#L155-L191","documentation":"ChaCha20 in this implementation requires a nonce of exactly allowedNonceLength bytes (12). IvSetup checks nonce.Length and, on mismatch, disposes the partially-initialized state and throws ArgumentException(\"Nonce length must be 12. Actual: N\").","triggerScenarios":"Passing an 8-byte nonce directly to CSChaCha20 without padding to 12 bytes, a 16-byte IETF 96-bit variant mismatch, or a decoded IV with padding bytes included.","commonSituations":"Older 8-byte-nonce ChaCha20 streams fed into this 12-byte-nonce implementation; IV attributes parsed with an extra 4 bytes; hex-decoded IV of odd length padded incorrectly.","solutions":["Left-pad 8-byte nonces with four zero bytes to make 12 bytes (as ChaCha20Util.DecryptPer1024Bytes does).","Trim decoded IV buffers to exactly 12 bytes.","Use ChaCha20Util.DecryptPer1024Bytes, which handles the 8-byte padding automatically.","Validate nonce length at the point where IVs are extracted from the manifest."],"exampleFix":"// before\nvar cipher = new CSChaCha20(key, nonce8, 0); // 8-byte nonce\n// after\nvar nonce12 = new byte[4].Concat(nonce8).ToArray();\nvar cipher = new CSChaCha20(key, nonce12, 0);","handlingStrategy":"validation","validationCode":"if (nonceBytes.Length == 8) nonceBytes = new byte[4].Concat(nonceBytes).ToArray();\nif (nonceBytes.Length != 12) throw new InvalidOperationException($\"nonce must be 12 bytes, got {nonceBytes.Length}\");","typeGuard":"bool IsChaCha20Nonce(byte[] n) => n is { Length: 12 };","tryCatchPattern":"try { cipher = new CSChaCha20(key, nonce, 0); }\ncatch (ArgumentException ex) { Console.Error.WriteLine($\"Bad nonce: {ex.Message}\"); }","preventionTips":["Pad 8-byte nonces to 12 bytes with four leading zero bytes.","Trim decoded IVs to exactly 12 bytes.","Prefer ChaCha20Util.DecryptPer1024Bytes which handles 8-byte nonces automatically."],"tags":["crypto","chacha20","nonce-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"e113dee70c924ee08dae5460624909b04d84cb76","analyzedAt":"2026-09-13T02:43:21.078Z","contentChangedAt":"2026-09-13T02:43:21.078Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}