1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
/*
Derby - Class org.apache.derbyTesting.unitTests.crypto.T_Cipher
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.unitTests.crypto;
import org.apache.derbyTesting.unitTests.harness.T_Generic;
import org.apache.derbyTesting.unitTests.harness.T_Fail;
import org.apache.derby.iapi.services.crypto.*;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.error.StandardException;
import java.security.AccessController;
import java.security.Key;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedAction;
import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.util.Properties;
/*
// PT
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import java.security.AlgorithmParameters;
// import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.lang.reflect.*;
*/
/*
To run, put the following line in derby.properties
derby.module.test.T_Cipher=org.apache.derbyTesting.unitTests.crypto.T_Cipher
and run java org.apache.derbyTesting.unitTests.harness.UnitTestMain
*/
public class T_Cipher extends T_Generic
{
private static final String testService = "CipherText";
CipherProvider enEngine;
CipherProvider deEngine;
Key secretKey;
byte[] IV;
CipherFactory factory;
public T_Cipher()
{
super();
}
/*
** Methods required by T_Generic
*/
public String getModuleToTestProtocolName() {
return org.apache.derby.iapi.reference.Module.CipherFactoryBuilder;
}
protected String getAlgorithm()
{
return "DES/CBC/NoPadding";
}
protected String getProvider()
{
// allow for alternate providers
String testProvider =
AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("testEncryptionProvider");
}
});
if (testProvider != null)
return testProvider;
else
return null;
}
public void runTests() throws T_Fail {
File testFile = new File("extinout/T_Cipher.data");
deleteFile(testFile);
String bootPassword = "a secret, don't tell anyone";
try
{
RandomAccessFile file = new RandomAccessFile(testFile, "rw");
setupCiphers(bootPassword);
// run thru some in patterns
int patternLength = 8192;
byte[] pattern = new byte[patternLength];
for (int i = 0; i < patternLength; i++)
pattern[i] = (byte)(i & 0xFF);
test(pattern, 0, 8, file); // test short patterns
test(pattern, 8, 8, file);
test(pattern, 1, 16, file);
test(pattern, 0, patternLength, file); // test long pattern
test(pattern, 0, patternLength/2, file);
test(pattern, 1, patternLength/2, file);
test(pattern, 2, patternLength/2, file);
test(pattern, 3, patternLength/2, file);
file.seek(0);
check(pattern, 0, 8, file); // file offset 0
check(pattern, 8, 8, file); // file offset 8
check(pattern, 1, 16, file); // file offset 16
check(pattern, 0, patternLength, file); // file offset 32
check(pattern, 0, patternLength/2, file);// file offset 32+patternLength
check(pattern, 1, patternLength/2, file);// file offset 32+patternLength+(patternLength/2)
check(pattern, 2, patternLength/2, file);// file offset 32+(2*patternLength)
check(pattern, 3, patternLength/2, file);// file offset 32+(2*patternLength)+(patternLength/2);
REPORT("starting random test");
// now do some random testing from file
file.seek(32+patternLength);
check(pattern, 0, patternLength/2, file);
file.seek(32);
check(pattern, 0, patternLength, file);
file.seek(32+(2*patternLength));
check(pattern, 2, patternLength/2, file);
file.seek(0);
check(pattern, 0, 8, file);
file.seek(16);
check(pattern, 1, 16, file);
file.seek(32+(2*patternLength)+(patternLength/2));
check(pattern, 3, patternLength/2, file);
file.seek(8);
check(pattern, 8, 8, file);
file.seek(32+patternLength+(patternLength/2));
check(pattern, 1, patternLength/2, file);
file.close();
}
catch (StandardException se)
{
se.printStackTrace(System.out);
throw T_Fail.exceptionFail(se);
}
catch (IOException ioe)
{
throw T_Fail.exceptionFail(ioe);
}
PASS("T_Cipher");
}
protected void setupCiphers(String bootPassword) throws T_Fail, StandardException
{
// set properties for testing
Properties props = new Properties();
props.put("encryptionAlgorithm",getAlgorithm());
String provider = getProvider();
if (provider != null)
props.put("encryptionProvider",getProvider());
props.put("bootPassword", bootPassword);
REPORT("encryption algorithm used : " + getAlgorithm());
REPORT("encryption provider used : " + provider);
CipherFactoryBuilder cb = (CipherFactoryBuilder)
startSystemModule(org.apache.derby.iapi.reference.Module.CipherFactoryBuilder);
factory = cb.createCipherFactory(true, props, false);
if (factory == null)
throw T_Fail.testFailMsg("cannot find Cipher factory ");
enEngine = factory.createNewCipher(CipherFactory.ENCRYPT);
deEngine = factory.createNewCipher(CipherFactory.DECRYPT);
if (enEngine == null)
throw T_Fail.testFailMsg("cannot create encryption engine");
if (deEngine == null)
throw T_Fail.testFailMsg("cannot create decryption engine");
}
protected void test(byte[] cleartext, int offset, int length,
RandomAccessFile outfile)
throws T_Fail, StandardException, IOException
{
byte[] ciphertext = new byte[length];
System.arraycopy(cleartext, offset, ciphertext, 0, length);
if (enEngine.encrypt(ciphertext, 0, length, ciphertext, 0) != length)
throw T_Fail.testFailMsg("encrypted text length != length");
if (byteArrayIdentical(ciphertext, cleartext, offset, length))
throw T_Fail.testFailMsg("encryption just made a copy of the clear text");
outfile.write(ciphertext);
// now decrypt it and check
deEngine.decrypt(ciphertext, 0, length, ciphertext, 0);
if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false)
throw T_Fail.testFailMsg("decryption did not yield the same clear text");
}
protected void check(byte[] cleartext, int offset, int length,
RandomAccessFile infile)
throws IOException, T_Fail, StandardException
{
byte[] ciphertext = new byte[length];
infile.read(ciphertext);
if (deEngine.decrypt(ciphertext, 0, length, ciphertext, 0) != length)
throw T_Fail.testFailMsg("decrypted text length != length");
if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false)
throw T_Fail.testFailMsg("decryption did not yield the same clear text");
}
// see if 2 byte arrays are identical
protected boolean byteArrayIdentical(byte[] compare, byte[] original,
int offset, int length)
{
for (int i = 0; i < length; i++)
{
if (compare[i] != original[offset+i])
return false;
}
return true;
}
/*
private void testBlowfish()
{
System.out.println("Running testBlowfish");
try
{
// set up the provider
java.security.Provider sunJce = new com.sun.crypto.provider.SunJCE();
java.security.Security.addProvider(sunJce);
// String key = "Paula bla la da trish123 sdkfs;ldkg;sa'jlskjgklad";
String key = "Paulabla123456789012345";
byte[] buf = key.getBytes();
System.out.println("key length is " + buf.length);
SecretKeySpec sKeySpec = new SecretKeySpec(buf,"Blowfish");
// SecretKeySpec sKeySpec = new SecretKeySpec(buf,"DESede");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
// Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
// Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,sKeySpec);
// only works with NoPadding if size is a multiple of 8 bytes
// with PKCS5Padding, works for all sizes
byte[] original = "This is what should get encrypte".getBytes();
System.out.println("original length is " + original.length);
byte[] encrypted = cipher.doFinal(original);
// works
// AlgorithmParameters algParam = cipher.getParameters();
byte[] iv = cipher.getIV();
System.out.println("length of iv is " + iv.length);
Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/NoPadding");
// Cipher cipher2 = Cipher.getInstance("DESede/CBC/NoPadding");
// Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
// works
// cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,algParam);
IvParameterSpec ivClass = new IvParameterSpec(iv);
cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,ivClass);
byte[] decrypted = cipher2.doFinal(encrypted);
if (byteArrayIdentical(original,decrypted,0,original.length))
System.out.println("PASSED");
else
System.out.println("FAILED");
System.out.println("original length is " + original.length);
System.out.println("encrypted length is " + encrypted.length);
System.out.println("decrypted length is " + decrypted.length);
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testBlowfish");
}
private void testCryptix()
{
System.out.println("Running testCryptix");
try
{
// set up the provider
Class jceClass = Class.forName("cryptix.jce.provider.Cryptix");
java.security.Provider cryptixProvider = (java.security.Provider) jceClass.newInstance();
java.security.Security.addProvider(cryptixProvider);
byte[] userkey = "a secret".getBytes();
System.out.println("userkey length is " + userkey.length);
Key secretKey = (Key) (new SecretKeySpec(userkey, "DES"));
byte[] IV = "anivspec".getBytes();
Cipher enCipher = Cipher.getInstance("DES/CBC/NoPadding","Cryptix");
Cipher deCipher = Cipher.getInstance("DES/CBC/NoPadding","Cryptix");
IvParameterSpec ivspec = new IvParameterSpec(IV);
enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);
deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);
int patternLength = 8;
byte[] pattern = new byte[patternLength];
for (int i = 0; i < patternLength; i++)
pattern[i] = (byte)(i & 0xFF);
byte[] cipherOutput1 = new byte[patternLength];
byte[] cipherOutput2 = new byte[patternLength];
int retval = 0;
retval = enCipher.doFinal(pattern, 0, 8, cipherOutput1, 0);
retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 1");
else
System.out.println("FAILED TEST 1");
retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 2");
else
System.out.println("FAILED TEST 2");
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testCryptix");
}
private void testMessageDigest()
{
// No provider needs to be installed for this to work.
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] data = "Paulas digest".getBytes();
byte[] digest = md.digest(data);
byte[] digest2 = md.digest(data);
if (byteArrayIdentical(digest,digest2,0,digest.length))
System.out.println("PASSED");
else
System.out.println("FAILED");
System.out.println("data length is " + data.length);
System.out.println("digest length is " + digest.length);
System.out.println("digest2 length is " + digest2.length);
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testBlowfish");
}
// PT
private void testPCBC()
{
System.out.println("Running testPCBC");
try
{
// set up the provider
Class jceClass = Class.forName("com.sun.crypto.provider.SunJCE");
java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();
java.security.Security.addProvider(myProvider);
// java.security.Provider sunJce = new com.sun.crypto.provider.SunJCE();
// java.security.Security.addProvider(sunJce);
// String key = "Paula bla la da trish123 sdkfs;ldkg;sa'jlskjgklad";
String key = "PaulablaPaulablaPaulabla";
byte[] buf = key.getBytes();
System.out.println("key length is " + buf.length);
SecretKeySpec sKeySpec = new SecretKeySpec(buf,"DESede");
Cipher cipher = Cipher.getInstance("DESede/PCBC/NoPadding");
// Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
// Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,sKeySpec);
// only works with NoPadding if size is a multiple of 8 bytes
// with PKCS5Padding, works for all sizes
byte[] original = "This is what should get encrypte".getBytes();
System.out.println("original length is " + original.length);
byte[] encrypted = cipher.doFinal(original);
// works
// AlgorithmParameters algParam = cipher.getParameters();
byte[] iv = cipher.getIV();
System.out.println("length of iv is " + iv.length);
Cipher cipher2 = Cipher.getInstance("DESede/PCBC/NoPadding");
// Cipher cipher2 = Cipher.getInstance("DESede/CBC/NoPadding");
// Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
// works
// cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,algParam);
IvParameterSpec ivClass = new IvParameterSpec(iv);
cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,ivClass);
byte[] decrypted = cipher2.doFinal(encrypted);
if (byteArrayIdentical(original,decrypted,0,original.length))
System.out.println("PASSED");
else
System.out.println("FAILED");
System.out.println("original length is " + original.length);
System.out.println("encrypted length is " + encrypted.length);
System.out.println("decrypted length is " + decrypted.length);
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testPCBC");
}
private void testPCBC2()
{
System.out.println("Running testPCBC2");
try
{
// set up the provider
Class jceClass = Class.forName("com.sun.crypto.provider.SunJCE");
java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();
java.security.Security.addProvider(myProvider);
byte[] userkey = "a secreta secreta secret".getBytes();
System.out.println("userkey length is " + userkey.length);
Key secretKey = (Key) (new SecretKeySpec(userkey, "DESede"));
byte[] IV = "anivspec".getBytes();
Cipher enCipher = Cipher.getInstance("DESede/PCBC/NoPadding","SunJCE");
Cipher deCipher = Cipher.getInstance("DESede/PCBC/NoPadding","SunJCE");
IvParameterSpec ivspec = new IvParameterSpec(IV);
enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);
deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);
int patternLength = 24;
byte[] pattern = new byte[patternLength];
for (int i = 0; i < patternLength; i++)
pattern[i] = (byte)(i & 0xFF);
byte[] cipherOutput1 = new byte[patternLength];
byte[] cipherOutput2 = new byte[patternLength];
int retval = 0;
retval = enCipher.doFinal(pattern, 0, 24, cipherOutput1, 0);
retval = deCipher.doFinal(cipherOutput1, 0, 24, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 1");
else
System.out.println("FAILED TEST 1");
retval = deCipher.doFinal(cipherOutput1, 0, 24, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 2");
else
System.out.println("FAILED TEST 2");
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testPCBC2");
}
private void testIAIK()
{
System.out.println("Running testIAIK");
try
{
// set up the provider
Class jceClass = Class.forName("iaik.security.provider.IAIK");
java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();
java.security.Security.addProvider(myProvider);
// iaik.security.provider.IAIK.addAsProvider(true);
// iaik.utils.Util.loadClass("iaik.security.provider.IAIK",true);
// IAIK p=new IAIK();
// iaik.security.provider.IAIK.getMd5();
byte[] userkey = "a secret".getBytes();
System.out.println("userkey length is " + userkey.length);
Key secretKey = (Key) (new SecretKeySpec(userkey, "DES"));
byte[] IV = "anivspec".getBytes();
Cipher enCipher = Cipher.getInstance("DES/CBC/NoPadding","IAIK");
Cipher deCipher = Cipher.getInstance("DES/CBC/NoPadding","IAIK");
IvParameterSpec ivspec = new IvParameterSpec(IV);
enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);
deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);
int patternLength = 8;
byte[] pattern = new byte[patternLength];
for (int i = 0; i < patternLength; i++)
pattern[i] = (byte)(i & 0xFF);
byte[] cipherOutput1 = new byte[patternLength];
byte[] cipherOutput2 = new byte[patternLength];
int retval = 0;
retval = enCipher.doFinal(pattern, 0, 8, cipherOutput1, 0);
retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 1");
else
System.out.println("FAILED TEST 1");
retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);
if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))
System.out.println("PASSED TEST 2");
else
System.out.println("FAILED TEST 2");
}
catch (Throwable t)
{
System.out.println("got an exception");
t.printStackTrace();
}
System.out.println("Finished testIAIK");
}
private void printByteArray(String name, byte[] array)
{
System.out.println("printing array " + name);
for (int i = 0; i < array.length; i++)
System.out.println("index " + i + " : " + array[i]);
}
*/
/**
* Delete a file in a Privileged block as these tests are
* run under the embedded engine code.
*/
private void deleteFile(final File f)
{
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
if (f.exists())
f.delete();
return null;
}
});
}
/**
* Privileged startup. Must be private so that user code
* can't call this entry point.
*/
private static Object startSystemModule( final String factoryInterface )
throws StandardException
{
try {
return AccessController.doPrivileged
(
new PrivilegedExceptionAction<Object>()
{
public Object run()
throws StandardException
{
return Monitor.startSystemModule( factoryInterface );
}
}
);
} catch (PrivilegedActionException pae)
{
throw StandardException.plainWrapException( pae );
}
}
}
|