1 21 22 package org.apache.derbyTesting.unitTests.crypto; 23 24 import org.apache.derbyTesting.unitTests.harness.T_Generic; 25 import org.apache.derbyTesting.unitTests.harness.T_Fail; 26 27 import org.apache.derby.iapi.services.crypto.*; 28 29 import org.apache.derby.iapi.services.monitor.Monitor; 30 31 import org.apache.derby.iapi.db.PropertyInfo; 32 33 import org.apache.derby.iapi.error.StandardException; 34 35 import java.security.AccessController ; 36 import java.security.Key ; 37 import java.security.PrivilegedAction ; 38 import java.security.PrivilegedExceptionAction ; 39 40 import java.io.File ; 41 import java.io.FileNotFoundException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.io.FileInputStream ; 45 import java.io.FileOutputStream ; 46 import java.io.RandomAccessFile ; 47 import java.io.IOException ; 48 49 import java.util.Properties ; 50 51 52 64 65 66 73 public class T_Cipher extends T_Generic 74 { 75 private static final String testService = "CipherText"; 76 77 CipherProvider enEngine; 78 CipherProvider deEngine; 79 Key secretKey; 80 byte[] IV; 81 82 CipherFactory factory; 83 84 public T_Cipher() 85 { 86 super(); 87 } 88 89 92 93 public String getModuleToTestProtocolName() { 94 return org.apache.derby.iapi.reference.Module.CipherFactoryBuilder; 95 } 96 97 protected String getAlgorithm() 98 { 99 return "DES/CBC/NoPadding"; 100 } 101 102 protected String getProvider() 103 { 104 String testProvider = 106 107 (String ) AccessController.doPrivileged(new PrivilegedAction () { 108 public Object run() { 109 return System.getProperty("testEncryptionProvider"); 110 } 111 }); 112 113 if (testProvider != null) 114 return testProvider; 115 else 116 return null; 117 118 } 119 120 public void runTests() throws T_Fail { 121 122 File testFile = new File ("extinout/T_Cipher.data"); 123 deleteFile(testFile); 124 125 String bootPassword = "a secret, don't tell anyone"; 126 127 try 128 { 129 RandomAccessFile file = new RandomAccessFile (testFile, "rw"); 130 131 setupCiphers(bootPassword); 132 133 int patternLength = 8192; 135 byte[] pattern = new byte[patternLength]; 136 for (int i = 0; i < patternLength; i++) 137 pattern[i] = (byte)(i & 0xFF); 138 139 test(pattern, 0, 8, file); test(pattern, 8, 8, file); 141 test(pattern, 1, 16, file); 142 143 test(pattern, 0, patternLength, file); test(pattern, 0, patternLength/2, file); 145 test(pattern, 1, patternLength/2, file); 146 test(pattern, 2, patternLength/2, file); 147 test(pattern, 3, patternLength/2, file); 148 149 file.seek(0); 150 check(pattern, 0, 8, file); check(pattern, 8, 8, file); check(pattern, 1, 16, file); check(pattern, 0, patternLength, file); check(pattern, 0, patternLength/2, file); check(pattern, 1, patternLength/2, file); check(pattern, 2, patternLength/2, file); check(pattern, 3, patternLength/2, file); 159 REPORT("starting random test"); 160 161 file.seek(32+patternLength); 163 check(pattern, 0, patternLength/2, file); 164 165 file.seek(32); 166 check(pattern, 0, patternLength, file); 167 168 file.seek(32+(2*patternLength)); 169 check(pattern, 2, patternLength/2, file); 170 171 file.seek(0); 172 check(pattern, 0, 8, file); 173 174 file.seek(16); 175 check(pattern, 1, 16, file); 176 177 file.seek(32+(2*patternLength)+(patternLength/2)); 178 check(pattern, 3, patternLength/2, file); 179 180 file.seek(8); 181 check(pattern, 8, 8, file); 182 183 file.seek(32+patternLength+(patternLength/2)); 184 check(pattern, 1, patternLength/2, file); 185 186 file.close(); 187 } 188 catch (StandardException se) 189 { 190 se.printStackTrace(System.out); 191 throw T_Fail.exceptionFail(se); 192 } 193 catch (IOException ioe) 194 { 195 throw T_Fail.exceptionFail(ioe); 196 } 197 198 199 PASS("T_Cipher"); 200 } 201 202 203 protected void setupCiphers(String bootPassword) throws T_Fail, StandardException 204 { 205 Properties props = new Properties (); 207 props.put("encryptionAlgorithm",getAlgorithm()); 208 String provider = getProvider(); 209 if (provider != null) 210 props.put("encryptionProvider",getProvider()); 211 props.put("bootPassword", bootPassword); 212 213 REPORT("encryption algorithm used : " + getAlgorithm()); 214 REPORT("encryption provider used : " + provider); 215 216 CipherFactoryBuilder cb = (CipherFactoryBuilder) 217 Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.CipherFactoryBuilder); 218 219 factory = cb.createCipherFactory(true, props, false); 220 221 if (factory == null) 222 throw T_Fail.testFailMsg("cannot find Cipher factory "); 223 224 enEngine = factory.createNewCipher(CipherFactory.ENCRYPT); 225 deEngine = factory.createNewCipher(CipherFactory.DECRYPT); 226 227 if (enEngine == null) 228 throw T_Fail.testFailMsg("cannot create encryption engine"); 229 if (deEngine == null) 230 throw T_Fail.testFailMsg("cannot create decryption engine"); 231 } 232 233 protected void test(byte[] cleartext, int offset, int length, 234 RandomAccessFile outfile) 235 throws T_Fail, StandardException, IOException 236 { 237 byte[] ciphertext = new byte[length]; 238 System.arraycopy(cleartext, offset, ciphertext, 0, length); 239 240 if (enEngine.encrypt(ciphertext, 0, length, ciphertext, 0) != length) 241 throw T_Fail.testFailMsg("encrypted text length != length"); 242 243 if (byteArrayIdentical(ciphertext, cleartext, offset, length)) 244 throw T_Fail.testFailMsg("encryption just made a copy of the clear text"); 245 246 outfile.write(ciphertext); 247 248 deEngine.decrypt(ciphertext, 0, length, ciphertext, 0); 250 if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false) 251 throw T_Fail.testFailMsg("decryption did not yield the same clear text"); 252 } 253 254 protected void check(byte[] cleartext, int offset, int length, 255 RandomAccessFile infile) 256 throws IOException , T_Fail, StandardException 257 { 258 byte[] ciphertext = new byte[length]; 259 infile.read(ciphertext); 260 261 if (deEngine.decrypt(ciphertext, 0, length, ciphertext, 0) != length) 262 throw T_Fail.testFailMsg("decrypted text length != length"); 263 264 if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false) 265 throw T_Fail.testFailMsg("decryption did not yield the same clear text"); 266 267 } 268 269 protected boolean byteArrayIdentical(byte[] compare, byte[] original, 271 int offset, int length) 272 { 273 for (int i = 0; i < length; i++) 274 { 275 if (compare[i] != original[offset+i]) 276 return false; 277 } 278 return true; 279 } 280 281 282 614 615 619 private void deleteFile(final File f) 620 { 621 AccessController.doPrivileged(new PrivilegedAction () { 622 public Object run() { 623 if (f.exists()) 624 f.delete(); 625 return null; 626 } 627 }); 628 } 629 } 630 | Popular Tags |