diff options
author | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-15 01:38:01 +0400 |
---|---|---|
committer | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-26 11:23:17 +0400 |
commit | 7cb752aaf746dc0b473afeb9e892b7fbc12666c5 (patch) | |
tree | cc4f91ddc18332b5adbe82e3fcb040d976c90105 /core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java | |
parent | 551830f8ea5177042af2c7dd1fc90888bc67387d (diff) |
Execute become-spongy.sh
https://github.com/rtyley/spongycastle/blob/3040af/become-spongy.sh
Diffstat (limited to 'core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java')
-rw-r--r-- | core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java b/core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java new file mode 100644 index 00000000..7ac877c1 --- /dev/null +++ b/core/src/test/java/org/spongycastle/crypto/test/Grain128Test.java @@ -0,0 +1,117 @@ +package org.spongycastle.crypto.test; + +import org.spongycastle.crypto.CipherParameters; +import org.spongycastle.crypto.StreamCipher; +import org.spongycastle.crypto.engines.Grain128Engine; +import org.spongycastle.crypto.params.KeyParameter; +import org.spongycastle.crypto.params.ParametersWithIV; +import org.spongycastle.util.encoders.Hex; +import org.spongycastle.util.test.SimpleTest; + +/** + * Grain-128 Test + */ +public class Grain128Test + extends SimpleTest +{ + + String keyStream1 = "f09b7bf7d7f6b5c2de2ffc73ac21397f"; + String keyStream2 = "afb5babfa8de896b4b9c6acaf7c4fbfd"; + + public String getName() + { + return "Grain-128"; + } + + public void performTest() + { + Grain128Test1(new ParametersWithIV(new KeyParameter(Hex + .decode("00000000000000000000000000000000")), Hex + .decode("000000000000000000000000"))); + Grain128Test2(new ParametersWithIV(new KeyParameter(Hex + .decode("0123456789abcdef123456789abcdef0")), Hex + .decode("0123456789abcdef12345678"))); + Grain128Test3(new ParametersWithIV(new KeyParameter(Hex + .decode("0123456789abcdef123456789abcdef0")), Hex + .decode("0123456789abcdef12345678"))); + } + + private void Grain128Test1(CipherParameters params) + { + StreamCipher grain = new Grain128Engine(); + byte[] in = new byte[16]; + byte[] out = new byte[16]; + + grain.init(true, params); + + grain.processBytes(in, 0, in.length, out, 0); + + if (!areEqual(out, Hex.decode(keyStream1))) + { + mismatch("Keystream 1", keyStream1, out); + } + + grain.reset(); + + grain.processBytes(in, 0, in.length, out, 0); + + if (!areEqual(out, Hex.decode(keyStream1))) + { + mismatch("Keystream 1", keyStream1, out); + } + } + + private void Grain128Test2(CipherParameters params) + { + StreamCipher grain = new Grain128Engine(); + byte[] in = new byte[16]; + byte[] out = new byte[16]; + + grain.init(true, params); + + grain.processBytes(in, 0, in.length, out, 0); + + if (!areEqual(out, Hex.decode(keyStream2))) + { + mismatch("Keystream 2", keyStream2, out); + } + + grain.reset(); + + grain.processBytes(in, 0, in.length, out, 0); + + if (!areEqual(out, Hex.decode(keyStream2))) + { + mismatch("Keystream 2", keyStream2, out); + } + } + + private void Grain128Test3(CipherParameters params) + { + StreamCipher grain = new Grain128Engine(); + byte[] in = "Encrypt me!".getBytes(); + byte[] cipher = new byte[in.length]; + byte[] clear = new byte[in.length]; + + grain.init(true, params); + + grain.processBytes(in, 0, in.length, cipher, 0); + grain.reset(); + grain.processBytes(cipher, 0, cipher.length, clear, 0); + + if (!areEqual(in, clear)) + { + mismatch("Test 3", new String(Hex.encode(in)), clear); + } + } + + private void mismatch(String name, String expected, byte[] found) + { + fail("mismatch on " + name, expected, new String(Hex.encode(found))); + } + + public static void main(String[] args) + { + runTest(new Grain128Test()); + } +} |