private void VectorTest(byte[] Key, byte[] Input, byte[] Output) { byte[] outBytes = new byte[Input.Length]; using (THX tfx = new THX()) { tfx.Initialize(true, new KeyParams(Key)); tfx.EncryptBlock(Input, outBytes); } if (Evaluate.AreEqual(outBytes, Output) == false) { throw new Exception("Twofish Vector: Encrypted arrays are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes)); } using (THX tfx = new THX()) { tfx.Initialize(false, new KeyParams(Key)); tfx.Transform(Output, outBytes); } if (Evaluate.AreEqual(outBytes, Input) == false) { throw new Exception("Twofish Vector: Decrypted arrays are not equal! Expected: " + HexConverter.ToString(Input) + " Received: " + HexConverter.ToString(outBytes)); } }
private void MonteCarloTest(byte[] Key, byte[] Input, byte[] Output, bool Encrypt = true, int Count = 10000) { byte[] outBytes = new byte[Input.Length]; Array.Copy(Input, 0, outBytes, 0, outBytes.Length); using (THX engine = new THX()) { engine.Initialize(Encrypt, new KeyParams(Key)); for (int i = 0; i < Count; i++) { engine.Transform(outBytes, outBytes); } } if (Evaluate.AreEqual(outBytes, Output) == false) { throw new Exception("Twofish MonteCarlo: Arrays are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes)); } }
/// <summary> /// Tests the SecureRandom access methods and return ranges /// </summary> /// <returns>Status</returns> public string Run() { try { CbcModeTest(); OnProgress(new TestEventArgs("Passed CBC Mode tests..")); CfbModeTest(); OnProgress(new TestEventArgs("Passed CFB Mode tests..")); CtrModeTest(); OnProgress(new TestEventArgs("Passed CTR Mode tests..")); OfbModeTest(); OnProgress(new TestEventArgs("Passed OFB Mode tests..")); StreamTest(); OnProgress(new TestEventArgs("Passed Stream Cipher tests")); SerializeStructTest(); OnProgress(new TestEventArgs("Passed CipherDescription serialization test..")); OnProgress(new TestEventArgs("")); OnProgress(new TestEventArgs("***Testing Cipher Parameters***.. ")); ParametersTest(); OnProgress(new TestEventArgs("Passed Cipher Parameters test..")); RHX eng = new RHX(); OnProgress(new TestEventArgs("***Testing Padding Modes***..")); StreamModesTest(new CBC(eng, false), new X923()); OnProgress(new TestEventArgs("Passed CBC/X923 CipherStream test..")); StreamModesTest(new CBC(eng, false), new PKCS7()); OnProgress(new TestEventArgs("Passed CBC/PKCS7 CipherStream test..")); StreamModesTest(new CBC(eng, false), new TBC()); OnProgress(new TestEventArgs("Passed CBC/TBC CipherStream test..")); StreamModesTest(new CBC(eng, false), new ISO7816()); OnProgress(new TestEventArgs("Passed CBC/ISO7816 CipherStream test..")); OnProgress(new TestEventArgs("")); OnProgress(new TestEventArgs("***Testing Cipher Modes***..")); StreamModesTest(new CTR(eng, false), new ISO7816()); OnProgress(new TestEventArgs("Passed CTR CipherStream test..")); StreamModesTest(new CFB(eng, 16, false), new ISO7816()); OnProgress(new TestEventArgs("Passed CFB CipherStream test..")); StreamModesTest(new OFB(eng, 16, false), new ISO7816()); OnProgress(new TestEventArgs("Passed OFB CipherStream test..")); OnProgress(new TestEventArgs("")); eng.Dispose(); OnProgress(new TestEventArgs("***Testing Stream Ciphers***..")); StreamingTest(new ChaCha20()); OnProgress(new TestEventArgs("Passed ChaCha CipherStream test..")); StreamingTest(new Salsa20()); OnProgress(new TestEventArgs("Passed Salsa20 CipherStream test..")); OnProgress(new TestEventArgs("")); OnProgress(new TestEventArgs("***Testing Cipher Description Initialization***..")); CipherDescription cd = new CipherDescription( SymmetricEngines.RHX, // cipher engine 32, // key size in bytes IVSizes.V128, // cipher iv size CipherModes.CTR, // cipher mode PaddingModes.ISO7816, // cipher padding BlockSizes.B128, // cipher block size RoundCounts.R14, // number of transformation rounds Digests.None, // optional key schedule engine (HX ciphers) 0, // optional HMAC size Digests.None); // optional HMAC engine DescriptionTest(cd); OnProgress(new TestEventArgs("Passed CipherDescription stream test..")); OnProgress(new TestEventArgs("")); OnProgress(new TestEventArgs("***Testing Block Ciphers***.. ")); THX tfx = new THX(); StreamModesTest(new CBC(tfx, false), new ISO7816()); tfx.Dispose(); OnProgress(new TestEventArgs("Passed THX CipherStream test..")); SHX spx = new SHX(); StreamModesTest(new CBC(spx, false), new ISO7816()); spx.Dispose(); OnProgress(new TestEventArgs("Passed SHX CipherStream test..")); Array.Resize(ref _key, 192); for (int i = 0; i < 192; i++) { _key[i] = (byte)i; } // test extended ciphers RHX rhx = new RHX(); StreamModesTest(new CBC(rhx, false), new ISO7816()); rhx.Dispose(); OnProgress(new TestEventArgs("Passed RHX CipherStream test..")); SHX shx = new SHX(); StreamModesTest(new CBC(shx, false), new ISO7816()); shx.Dispose(); OnProgress(new TestEventArgs("Passed SHX CipherStream test..")); THX thx = new THX(); StreamModesTest(new CBC(thx, false), new ISO7816()); thx.Dispose(); OnProgress(new TestEventArgs("Passed THX CipherStream test..")); OnProgress(new TestEventArgs("")); return(SUCCESS); } catch (Exception Ex) { string message = Ex.Message == null ? "" : Ex.Message; throw new Exception(FAILURE + message); } }