{"record":{"id":"c2f9c10ba872c503","repo":"peass-ng/PEASS-ng","slug":"must-be-in-the-range-0-7","errorCode":null,"errorMessage":"must be in the range [0,7]","messagePattern":"must be in the range \\[0,7\\]","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/crypto/digests/SHA3Digest.cs","lineNumber":63,"sourceCode":"        public override string AlgorithmName\n        {\n            get { return \"SHA3-\" + fixedOutputLength; }\n        }\n\n        public override int DoFinal(byte[] output, int outOff)\n        {\n            AbsorbBits(0x02, 2);\n\n            return base.DoFinal(output,  outOff);\n        }\n\n        /*\n         * TODO Possible API change to support partial-byte suffixes.\n         */\n        protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)\n        {\n            if (partialBits < 0 || partialBits > 7)\n                throw new ArgumentException(\"must be in the range [0,7]\", \"partialBits\");\n\n            int finalInput = (partialByte & ((1 << partialBits) - 1)) | (0x02 << partialBits);\n            Debug.Assert(finalInput >= 0);\n            int finalBits = partialBits + 2;\n\n            if (finalBits >= 8)\n            {\n                Absorb((byte)finalInput);\n                finalBits -= 8;\n                finalInput >>= 8;\n            }\n\n            return base.DoFinal(output, outOff, (byte)finalInput, finalBits);\n        }\n\n        public override IMemoable Copy()\n\t\t{\n\t\t\treturn new Sha3Digest(this);","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/crypto/digests/SHA3Digest.cs#L45-L81","documentation":"Sha3Digest.DoFinal's partial-byte overload throws ArgumentException when `partialBits` is negative or greater than 7. It represents how many low bits of `partialByte` are valid input; SHA-3 additionally appends a 2-bit domain suffix, so any out-of-range count would corrupt the padding. Unlike KeccakDigest.AbsorbBits, 0 partial bits is allowed here.","triggerScenarios":"Passing partialBits < 0 or > 7 to DoFinal(output, outOff, partialByte, partialBits), typically from an upstream bit-count computation bug.","commonSituations":"Implementing KMAC/TupleHash-style suffixes where the partial-bit counter overflows or is negative due to unsigned/signed confusion; ported code passing byte counts instead of bit counts.","solutions":["Validate that partialBits is in [0,7] before calling DoFinal; mask with & 7 only after validating.","If more than 7 bits remain, absorb them via BlockUpdate as whole bytes first.","Check signedness of the bit-count arithmetic in the caller (negative underflow)."],"exampleFix":"// before\nint partialBits = remainingBits; // could be > 7 or negative on underflow\ndigest.DoFinal(output, outOff, partialByte, partialBits);\n// after\nif (partialBits < 0 || partialBits > 7)\n    throw new ArgumentOutOfRangeException(nameof(partialBits));\ndigest.BlockUpdate(bytes, 0, bytes.Length); // whole bytes first\ndigest.DoFinal(output, outOff, partialByte, partialBits);","handlingStrategy":"validation","validationCode":"if (partialBits < 0 || partialBits > 7)\n    throw new ArgumentOutOfRangeException(nameof(partialBits), \"must be in the range [0,7]\");\ndigest.DoFinal(output, outOff, partialByte, partialBits);","typeGuard":"bool IsValidSha3PartialBits(int bits) => (uint)bits <= 7u; // also rejects negatives","tryCatchPattern":"try { digest.DoFinal(outBuf, 0, partialByte, partialBits); } catch (ArgumentException ex) when (ex.ParamName == \"partialBits\") { partialBits &= 7; digest.Reset(); /* replay with corrected value */ }","preventionTips":["Clamp bit-count arithmetic with checks for negative underflow before calling DoFinal.","Absorb whole bytes via BlockUpdate first; only pass the final 0-7 residual bits.","Test the partial-bits code path with boundary values 0 and 7."],"tags":["csharp","bouncycastle","sha3","argument-validation"],"backgroundTag":"invalid-argument-range","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}