001: package org.bouncycastle.jce.provider.test;
002:
003: import java.security.Key;
004: import java.security.Security;
005:
006: import javax.crypto.Cipher;
007: import javax.crypto.KeyGenerator;
008:
009: import org.bouncycastle.jce.provider.BouncyCastleProvider;
010: import org.bouncycastle.util.test.SimpleTestResult;
011: import org.bouncycastle.util.test.Test;
012: import org.bouncycastle.util.test.TestResult;
013:
014: /**
015: * check that doFinal is properly reseting the cipher.
016: */
017: public class DoFinalTest implements Test {
018: public DoFinalTest() {
019: }
020:
021: private boolean equalArray(byte[] a, int aOff, byte[] b, int length) {
022: if (aOff + a.length < length) {
023: return false;
024: }
025:
026: if (b.length < length) {
027: return false;
028: }
029:
030: for (int i = 0; i != length; i++) {
031: if (a[aOff + i] != b[i]) {
032: return false;
033: }
034: }
035:
036: return true;
037: }
038:
039: public TestResult checkCipher(String cipherName) {
040: String lCode = "ABCDEFGHIJKLMNOPQRSTUVWXY0123456789";
041: String baseAlgorithm;
042: int index = cipherName.indexOf('/');
043:
044: if (index > 0) {
045: baseAlgorithm = cipherName.substring(0, index);
046: } else {
047: baseAlgorithm = cipherName;
048: }
049:
050: try {
051: KeyGenerator kGen = KeyGenerator.getInstance(baseAlgorithm,
052: "BC");
053: Cipher cipher = Cipher.getInstance(cipherName, "BC");
054: Key key = kGen.generateKey();
055:
056: cipher.init(Cipher.ENCRYPT_MODE, key);
057:
058: byte[] encrypted = cipher.doFinal(lCode.getBytes());
059:
060: // 2nd try
061: byte[] encrypted2 = cipher.doFinal(lCode.getBytes());
062:
063: if (encrypted.length != encrypted2.length) {
064: return new SimpleTestResult(false, getName()
065: + ": Failed " + cipherName
066: + " - expected length " + encrypted.length
067: + " got " + encrypted2.length);
068: }
069:
070: if (!equalArray(encrypted, 0, encrypted2, encrypted.length)) {
071: return new SimpleTestResult(false, getName()
072: + ": Failed " + cipherName
073: + " - first two arrays not equal");
074: }
075:
076: // 3rd try
077: byte[] enc1 = cipher.update(lCode.getBytes());
078: byte[] enc2 = cipher.doFinal();
079:
080: if ((enc1.length + enc2.length) != encrypted.length) {
081: return new SimpleTestResult(false, getName()
082: + ": Failed " + cipherName
083: + " - expected length " + encrypted.length
084: + " got " + (enc1.length + enc2.length));
085: }
086:
087: if (!equalArray(encrypted, 0, enc1, enc1.length)) {
088: return new SimpleTestResult(false, getName()
089: + ": Failed " + cipherName
090: + " - enc1 array not equal");
091: }
092:
093: if (!equalArray(encrypted, enc1.length, enc2, enc2.length)) {
094: return new SimpleTestResult(false, getName()
095: + ": Failed " + cipherName
096: + " - enc1 array not equal");
097: }
098:
099: enc1 = cipher.update(lCode.getBytes());
100:
101: if (!equalArray(encrypted, 0, enc1, enc1.length)) {
102: return new SimpleTestResult(false, getName()
103: + ": Failed " + cipherName
104: + " - 2nd enc1 array not equal");
105: }
106:
107: int len = cipher.doFinal(enc1, 0);
108: if ((enc1.length + len) != encrypted.length) {
109: return new SimpleTestResult(false, getName()
110: + ": Failed " + cipherName
111: + " - expected length " + encrypted.length
112: + " got " + (enc1.length + len));
113: }
114: } catch (Exception e) {
115: return new SimpleTestResult(false, getName() + ": Failed "
116: + cipherName + " - exception " + e.toString());
117: }
118:
119: return new SimpleTestResult(true, getName() + ": Okay");
120: }
121:
122: public TestResult perform() {
123: TestResult result = checkCipher("RC4");
124:
125: if (!result.isSuccessful()) {
126: return result;
127: }
128:
129: result = checkCipher("DES/CBC/PKCS5Padding");
130:
131: if (!result.isSuccessful()) {
132: return result;
133: }
134:
135: return checkCipher("Rijndael");
136: }
137:
138: public String getName() {
139: return "DoFinalTest";
140: }
141:
142: public static void main(String[] args) {
143: Security.addProvider(new BouncyCastleProvider());
144:
145: Test test = new DoFinalTest();
146: TestResult result = test.perform();
147:
148: System.out.println(result.toString());
149: }
150: }
|