{"record":{"id":"a1c7dbf9817c4239","repo":"nilaoda/N_m3u8DL-RE","slug":"key-is-null","errorCode":null,"errorMessage":"Key is null","messagePattern":"Key is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/Crypto/CSChaCha20.cs","lineNumber":125,"sourceCode":"        }\n\n\n        // These are the same constants defined in the reference implementation.\n        // http://cr.yp.to/streamciphers/timings/estreambench/submissions/salsa20/chacha8/ref/chacha.c\n        private static readonly byte[] sigma = Encoding.ASCII.GetBytes(\"expand 32-byte k\");\n        private static readonly byte[] tau = Encoding.ASCII.GetBytes(\"expand 16-byte k\");\n\n        /// <summary>\n        /// Set up the ChaCha state with the given key. A 32-byte key is required and enforced.\n        /// </summary>\n        /// <param name=\"key\">\n        /// A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit little-endian integers\n        /// </param>\n        private void KeySetup(byte[] key)\n        {\n            if (key == null)\n            {\n                throw new ArgumentNullException(\"Key is null\");\n            }\n\n            if (key.Length != allowedKeyLength)\n            {\n                throw new ArgumentException($\"Key length must be {allowedKeyLength}. Actual: {key.Length}\");\n            }\n\n            state[4] = Util.U8To32Little(key, 0);\n            state[5] = Util.U8To32Little(key, 4);\n            state[6] = Util.U8To32Little(key, 8);\n            state[7] = Util.U8To32Little(key, 12);\n\n            byte[] constants = (key.Length == allowedKeyLength) ? sigma : tau;\n            int keyIndex = key.Length - 16;\n\n            state[8] = Util.U8To32Little(key, keyIndex + 0);\n            state[9] = Util.U8To32Little(key, keyIndex + 4);\n            state[10] = Util.U8To32Little(key, keyIndex + 8);","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/Crypto/CSChaCha20.cs#L107-L143","documentation":"CSChaCha20.KeySetup validates the ChaCha20 key before initializing the cipher state. A null key is rejected with ArgumentNullException(\"Key is null\"). This runs during ChaCha20 construction for every decryption using this class.","triggerScenarios":"Constructing CSChaCha20 (via the ChaCha20 entry) with key == null, typically when a key extraction step returned null (failed base64/hex decode, missing DRM key) and the null was passed through unchecked.","commonSituations":"A earlier parse step failed silently and returned null key bytes; config where the key option was omitted but decryption was still requested; tests passing null deliberately.","solutions":["Check key != null before constructing the ChaCha20 cipher.","Ensure the key-extraction/parse step succeeded and produced a byte[].","Fail fast with a clear message upstream if the key is unavailable instead of passing null.","In tests, assert keys are non-null before invoking decryption paths."],"exampleFix":"// before\nvar cipher = new CSChaCha20(keyBytes, nonce, counter);\n// after\nif (keyBytes == null) throw new InvalidOperationException(\"decryption key missing\");\nvar cipher = new CSChaCha20(keyBytes, nonce, counter);","handlingStrategy":"type-guard","validationCode":"if (keyBytes == null || keyBytes.Length == 0) throw new InvalidOperationException(\"no decryption key available\");","typeGuard":"bool HasKey(byte[] k) => k is { Length: 32 };","tryCatchPattern":"try { new CSChaCha20(key, nonce, 0); }\ncatch (ArgumentNullException) { /* key missing — abort decryption */ }","preventionTips":["Check that key extraction succeeded before constructing the cipher.","Never pass through a nullable key without a null check.","Fail early with a clear message when the key option is absent."],"tags":["crypto","chacha20","null-check"],"backgroundTag":"null-argument","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"}