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 /prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java | |
parent | 551830f8ea5177042af2c7dd1fc90888bc67387d (diff) |
Execute become-spongy.sh
https://github.com/rtyley/spongycastle/blob/3040af/become-spongy.sh
Diffstat (limited to 'prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java')
-rw-r--r-- | prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java b/prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java new file mode 100644 index 00000000..fd0da3e5 --- /dev/null +++ b/prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java @@ -0,0 +1,164 @@ +package org.spongycastle.jce.provider.test; + +import java.security.Key; +import java.security.Security; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; + +import org.spongycastle.jce.provider.BouncyCastleProvider; +import org.spongycastle.util.test.SimpleTestResult; +import org.spongycastle.util.test.Test; +import org.spongycastle.util.test.TestResult; + +/** + * check that doFinal is properly reseting the cipher. + */ +public class DoFinalTest + implements Test +{ + public DoFinalTest() + { + } + + private boolean equalArray( + byte[] a, + int aOff, + byte[] b, + int length) + { + if (aOff + a.length < length) + { + return false; + } + + if (b.length < length) + { + return false; + } + + for (int i = 0; i != length; i++) + { + if (a[aOff + i] != b[i]) + { + return false; + } + } + + return true; + } + + public TestResult checkCipher( + String cipherName) + { + String lCode = "ABCDEFGHIJKLMNOPQRSTUVWXY0123456789"; + String baseAlgorithm; + int index = cipherName.indexOf('/'); + + if (index > 0) + { + baseAlgorithm = cipherName.substring(0, index); + } + else + { + baseAlgorithm = cipherName; + } + + try + { + KeyGenerator kGen = KeyGenerator.getInstance(baseAlgorithm, "SC"); + Cipher cipher = Cipher.getInstance(cipherName, "SC"); + Key key = kGen.generateKey(); + + cipher.init(Cipher.ENCRYPT_MODE, key); + + byte[] encrypted = cipher.doFinal(lCode.getBytes()); + + // 2nd try + byte[] encrypted2 = cipher.doFinal(lCode.getBytes()); + + if (encrypted.length != encrypted2.length) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + encrypted2.length); + } + + if (!equalArray(encrypted, 0, encrypted2, encrypted.length)) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - first two arrays not equal"); + } + + // 3rd try + byte[] enc1 = cipher.update(lCode.getBytes()); + byte[] enc2 = cipher.doFinal(); + + if ((enc1.length + enc2.length) != encrypted.length) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + (enc1.length + enc2.length)); + } + + if (!equalArray(encrypted, 0, enc1, enc1.length)) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - enc1 array not equal"); + } + + if (!equalArray(encrypted, enc1.length, enc2, enc2.length)) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - enc1 array not equal"); + } + + enc1 = cipher.update(lCode.getBytes()); + + if (!equalArray(encrypted, 0, enc1, enc1.length)) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - 2nd enc1 array not equal"); + } + + int len = cipher.doFinal(enc1, 0); + if ((enc1.length + len) != encrypted.length) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + (enc1.length + len)); + } + } + catch (Exception e) + { + return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - exception " + e.toString()); + } + + return new SimpleTestResult(true, getName() + ": Okay"); + } + + public TestResult perform() + { + TestResult result = checkCipher("RC4"); + + if (!result.isSuccessful()) + { + return result; + } + + result = checkCipher("DES/CBC/PKCS5Padding"); + + if (!result.isSuccessful()) + { + return result; + } + + return checkCipher("Rijndael"); + } + + public String getName() + { + return "DoFinalTest"; + } + + public static void main( + String[] args) + { + Security.addProvider(new BouncyCastleProvider()); + + Test test = new DoFinalTest(); + TestResult result = test.perform(); + + System.out.println(result.toString()); + } +} |