Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-03-12 06:49:31 +0400
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-03-12 06:49:31 +0400
commit531316148bf65917e948f8495565a1b9c3b86f33 (patch)
tree54a72d2b6bebf97e64d7b1a9416fa59c36b40955 /core
parent58485230e92a08bee744252fe9c8a33f90004fff (diff)
Add extra random tests for SipHash to check consistency across different
update methods
Diffstat (limited to 'core')
-rw-r--r--core/src/test/java/org/bouncycastle/crypto/test/SipHashTest.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/core/src/test/java/org/bouncycastle/crypto/test/SipHashTest.java b/core/src/test/java/org/bouncycastle/crypto/test/SipHashTest.java
index 4086893e..49501b8b 100644
--- a/core/src/test/java/org/bouncycastle/crypto/test/SipHashTest.java
+++ b/core/src/test/java/org/bouncycastle/crypto/test/SipHashTest.java
@@ -1,5 +1,7 @@
package org.bouncycastle.crypto.test;
+import java.security.SecureRandom;
+
import org.bouncycastle.crypto.macs.SipHash;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.util.Pack;
@@ -31,6 +33,12 @@ public class SipHashTest
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)
@@ -66,6 +74,33 @@ public class SipHashTest
}
}
+ private void randomTest(SecureRandom random)
+ {
+ byte[] key = new byte[16];
+ random.nextBytes(key);
+
+ int length = 1 + random.nextInt(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)