diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-03-12 09:49:43 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-03-12 09:49:43 +0700 |
commit | 8648332a9bc98d0c11f089901b83936124a0352c (patch) | |
tree | 41a923f102c51ffaa8d23e520653f2b56c88164d /crypto/test | |
parent | Optimization (faster at all input lengths, but especially for long block-alig... (diff) | |
download | BouncyCastle.NET-ed25519-8648332a9bc98d0c11f089901b83936124a0352c.tar.xz |
Add extra random tests for SipHash to check consistency across different update methods
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/crypto/test/SipHashTest.cs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/crypto/test/src/crypto/test/SipHashTest.cs b/crypto/test/src/crypto/test/SipHashTest.cs index 886ffc4d6..82dfce82c 100644 --- a/crypto/test/src/crypto/test/SipHashTest.cs +++ b/crypto/test/src/crypto/test/SipHashTest.cs @@ -7,6 +7,7 @@ using NUnit.Framework; using Org.BouncyCastle.Crypto.Macs; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; +using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; @@ -37,6 +38,12 @@ namespace Org.BouncyCastle.Crypto.Tests RunMac(key, input, UPDATE_BYTES); RunMac(key, input, UPDATE_FULL); RunMac(key, input, UPDATE_MIX); + + SecureRandom random = new SecureRandom(); + for (int i = 0; i < 100; ++i) + { + RandomTest(random); + } } private void RunMac(byte[] key, byte[] input, int updateType) @@ -71,6 +78,33 @@ namespace Org.BouncyCastle.Crypto.Tests } } + private void RandomTest(SecureRandom random) + { + byte[] key = new byte[16]; + random.NextBytes(key); + + int length = 1 + random.Next(1024); + byte[] input = new byte[length]; + random.NextBytes(input); + + SipHash mac = new SipHash(); + mac.Init(new KeyParameter(key)); + + UpdateMac(mac, input, UPDATE_BYTES); + long result1 = mac.DoFinal(); + + UpdateMac(mac, input, UPDATE_FULL); + long result2 = mac.DoFinal(); + + UpdateMac(mac, input, UPDATE_MIX); + long result3 = mac.DoFinal(); + + if (result1 != result2 || result1 != result3) + { + Fail("Inconsistent results in random test"); + } + } + private void UpdateMac(SipHash mac, byte[] input, int updateType) { switch (updateType) |