{"record":{"id":"898e9f6756054035","repo":"peass-ng/PEASS-ng","slug":"threefish-key-must-be-same-size-as-block","errorCode":null,"errorMessage":"Threefish key must be same size as block (","messagePattern":"Threefish key must be same size as block \\(","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/crypto/engines/ThreefishEngine.cs","lineNumber":185,"sourceCode":"\t\t\telse if (parameters is KeyParameter)\n\t\t\t{\n\t\t\t\tkeyBytes = ((KeyParameter)parameters).GetKey();\n\t\t\t\ttweakBytes = null;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tthrow new ArgumentException(\"Invalid parameter passed to Threefish init - \"\n\t\t\t\t\t+ Platform.GetTypeName(parameters));\n\t\t\t}\n\n\t\t\tulong[] keyWords = null;\n\t\t\tulong[] tweakWords = null;\n\n\t\t\tif (keyBytes != null)\n\t\t\t{\n\t\t\t\tif (keyBytes.Length != this.blocksizeBytes)\n\t\t\t\t{\n\t\t\t\t\tthrow new ArgumentException(\"Threefish key must be same size as block (\" + blocksizeBytes\n\t\t\t\t\t\t\t\t\t\t\t\t+ \" bytes)\");\n\t\t\t\t}\n\t\t\t\tkeyWords = new ulong[blocksizeWords];\n\t\t\t\tfor (int i = 0; i < keyWords.Length; i++)\n\t\t\t\t{\n\t\t\t\t\tkeyWords[i] = BytesToWord(keyBytes, i * 8);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (tweakBytes != null)\n\t\t\t{\n\t\t\t\tif (tweakBytes.Length != TWEAK_SIZE_BYTES)\n\t\t\t\t{\n\t\t\t\t\tthrow new ArgumentException(\"Threefish tweak must be \" + TWEAK_SIZE_BYTES + \" bytes\");\n\t\t\t\t}\n\t\t\t\ttweakWords = new ulong[] { BytesToWord(tweakBytes, 0), BytesToWord(tweakBytes, 8) };\n\t\t\t}\n\t\t\tInit(forEncryption, keyWords, tweakWords);\n\t\t}","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/crypto/engines/ThreefishEngine.cs#L167-L203","documentation":"During Init, the key byte array must match the engine's configured block size in bytes (32, 64, or 128). The engine converts the key into blocksizeWords 64-bit words and throws ArgumentException when the lengths differ.","triggerScenarios":"Creating ThreefishEngine(BLOCKSIZE_512) but calling Init with a 32-byte key (or vice versa); any key length not equal to blocksizeBytes.","commonSituations":"Mixing Threefish-256 key with a Threefish-1024 engine; deriving a key whose size was computed for a different variant; truncating a hex-decoded key.","solutions":["Ensure the key length equals the engine block size in bytes (32 for 256-bit, 64 for 512-bit, 128 for 1024-bit)","Construct the engine from the key: new ThreefishEngine(key.Length) then Init with that key","KDF-expand the key to the required size if the source key is shorter"],"exampleFix":"// before\nvar engine = new ThreefishEngine(ThreefishEngine.BLOCKSIZE_512);\nengine.Init(true, new KeyParameter(key32));\n// after\nvar engine = new ThreefishEngine(ThreefishEngine.BLOCKSIZE_256);\nengine.Init(true, new KeyParameter(key32));","handlingStrategy":"validation","validationCode":"if (key == null || key.Length != engine.GetBlockSize())\n    throw new ArgumentException(\"Threefish key length must equal block size (32/64/128 bytes)\");\nengine.Init(forEncryption, new KeyParameter(key));","typeGuard":"bool IsValidThreefishKey(ThreefishEngine e, byte[] key) => key != null && key.Length == e.GetBlockSize();","tryCatchPattern":"try { engine.Init(forEnc, new KeyParameter(key)); }\ncatch (ArgumentException ex) { /* reject key, re-derive at correct size */ }","preventionTips":["Generate keys sized to the chosen variant (32/64/128 bytes)","Store the variant (not just key bytes) with encrypted data","Assert key.Length == engine.GetBlockSize() in tests"],"tags":["csharp","cryptography","threefish","key-size"],"backgroundTag":"invalid-cipher-key-size","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}