diff options
author | Sergio Giro <sgiro@google.com> | 2016-02-01 18:52:42 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-02-01 18:52:42 +0000 |
commit | 9218edabd1ef9852bc2f13115dcadc81b442dd6c (patch) | |
tree | 8229ff72c8cbb06f49dce3a8382930919fa6fc2b /bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java | |
parent | 9b30eb05e5be69d51881a0d1b31e503e97acd784 (diff) | |
parent | 397d32894b89b506dc318e0f83446187c9b76ebe (diff) | |
download | android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.gz android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.bz2 android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.zip |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge-152-from-upstream"
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java b/bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java new file mode 100644 index 0000000..5986141 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java @@ -0,0 +1,129 @@ +package org.bouncycastle.jce.provider.test; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Security; + +import javax.crypto.KeyGenerator; +import javax.crypto.Mac; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.util.Arrays; +import org.bouncycastle.util.encoders.Hex; +import org.bouncycastle.util.test.SimpleTest; + +public class SipHashTest + extends SimpleTest +{ + public void performTest() + throws Exception + { + testMac(); + testKeyGenerator(); + } + + private void testKeyGenerator() + throws NoSuchAlgorithmException, + NoSuchProviderException + { + testKeyGen("SipHash"); + testKeyGen("SipHash-2-4"); + testKeyGen("SipHash-4-8"); + } + + private void testKeyGen(String algorithm) + throws NoSuchAlgorithmException, + NoSuchProviderException + { + KeyGenerator kg = KeyGenerator.getInstance(algorithm, "BC"); + + SecretKey key = kg.generateKey(); + + if (!key.getAlgorithm().equalsIgnoreCase("SipHash")) + { + fail("Unexpected algorithm name in key", "SipHash", key.getAlgorithm()); + } + if (key.getEncoded().length != 16) + { + fail("Expected 128 bit key"); + } + } + + private void testMac() + throws NoSuchAlgorithmException, + NoSuchProviderException, + InvalidKeyException + { + byte[] key = Hex.decode("000102030405060708090a0b0c0d0e0f"); + byte[] input = Hex.decode("000102030405060708090a0b0c0d0e"); + + byte[] expected = Hex.decode("e545be4961ca29a1"); + + Mac mac = Mac.getInstance("SipHash", "BC"); + + mac.init(new SecretKeySpec(key, "SipHash")); + + mac.update(input, 0, input.length); + + byte[] result = mac.doFinal(); + + if (!Arrays.areEqual(expected, result)) + { + fail("Result does not match expected value for doFinal()"); + } + + mac.init(new SecretKeySpec(key, "SipHash-2-4")); + + mac.update(input, 0, input.length); + + result = mac.doFinal(); + if (!Arrays.areEqual(expected, result)) + { + fail("Result does not match expected value for second doFinal()"); + } + + mac = Mac.getInstance("SipHash-2-4", "BC"); + + mac.init(new SecretKeySpec(key, "SipHash-2-4")); + + mac.update(input, 0, input.length); + + result = mac.doFinal(); + if (!Arrays.areEqual(expected, result)) + { + fail("Result does not match expected value for alias"); + } + + // SipHash 4-8 + expected = Hex.decode("e0a6a97dd589d383"); + + mac = Mac.getInstance("SipHash-4-8", "BC"); + + mac.init(new SecretKeySpec(key, "SipHash")); + + mac.update(input, 0, input.length); + + result = mac.doFinal(); + + if (!Arrays.areEqual(expected, result)) + { + fail("Result does not match expected value for SipHash 4-8"); + } + } + + public String getName() + { + return "SipHash"; + } + + public static void main( + String[] args) + { + Security.addProvider(new BouncyCastleProvider()); + + runTest(new SipHashTest()); + } +} |