1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.crypto.tests.javax.crypto; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.InputStream; 23 import java.math.BigInteger; 24 import java.net.URL; 25 import java.nio.ByteBuffer; 26 import java.nio.ReadOnlyBufferException; 27 import java.security.AlgorithmParameters; 28 import java.security.InvalidAlgorithmParameterException; 29 import java.security.InvalidKeyException; 30 import java.security.Key; 31 import java.security.NoSuchAlgorithmException; 32 import java.security.NoSuchProviderException; 33 import java.security.Provider; 34 import java.security.SecureRandom; 35 import java.security.Security; 36 import java.security.cert.Certificate; 37 import java.security.cert.CertificateFactory; 38 import java.security.spec.AlgorithmParameterSpec; 39 import java.security.spec.RSAKeyGenParameterSpec; 40 import java.util.Arrays; 41 import javax.crypto.BadPaddingException; 42 import javax.crypto.Cipher; 43 import javax.crypto.CipherSpi; 44 import javax.crypto.IllegalBlockSizeException; 45 import javax.crypto.SecretKeyFactory; 46 import javax.crypto.ShortBufferException; 47 import javax.crypto.spec.DESedeKeySpec; 48 import javax.crypto.spec.IvParameterSpec; 49 import javax.crypto.spec.SecretKeySpec; 50 import org.apache.harmony.crypto.tests.support.MyCipher; 51 import tests.support.resource.Support_Resources; 52 53 public class CipherTest extends junit.framework.TestCase { 54 55 private static final Key CIPHER_KEY_3DES; 56 private static final String ALGORITHM_3DES = "DESede"; 57 58 private static final Key CIPHER_KEY_DES; 59 private static final String ALGORITHM_DES = "DES"; 60 private static final byte[] IV = new byte[] { 61 (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6, (byte) 7, 62 }; 63 64 static { 65 try { 66 byte[] encoded_3des = { 67 (byte) -57, (byte) -108, (byte) -42, (byte) 47, 68 (byte) 42, (byte) -12, (byte) -53, (byte) -83, 69 (byte) -123, (byte) 91, (byte) -99, (byte) -101, 70 (byte) 93, (byte) -62, (byte) -42, (byte) -128, 71 (byte) -2, (byte) 47, (byte) -8, (byte) 69, 72 (byte) -68, (byte) 69, (byte) 81, (byte) 74, 73 }; 74 CIPHER_KEY_3DES = new SecretKeySpec(encoded_3des, ALGORITHM_3DES); 75 76 byte[] encoded_des = { 77 (byte) 2, (byte) 41, (byte) -77, (byte) 107, 78 (byte) -99, (byte) -63, (byte) -42, (byte) -89, 79 }; 80 CIPHER_KEY_DES = new SecretKeySpec(encoded_des, ALGORITHM_DES); 81 82 } catch (Exception e) { 83 throw new AssertionError(e); 84 } 85 } 86 87 /** 88 * javax.crypto.Cipher#getInstance(java.lang.String) 89 */ 90 public void test_getInstanceLjava_lang_String() throws Exception { 91 Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 92 assertNotNull("Received a null Cipher instance", cipher); 93 94 try { 95 Cipher.getInstance("WrongAlgorithmName"); 96 fail(); 97 } catch (NoSuchAlgorithmException expected) { 98 } 99 } 100 101 /** 102 * javax.crypto.Cipher#getInstance(java.lang.String, 103 * java.lang.String) 104 */ 105 public void test_getInstanceLjava_lang_StringLjava_lang_String() 106 throws Exception { 107 108 Provider[] providers = Security.getProviders("Cipher.DES"); 109 110 assertNotNull("No installed providers support Cipher.DES", providers); 111 112 for (int i = 0; i < providers.length; i++) { 113 Cipher cipher = Cipher.getInstance("DES", providers[i].getName()); 114 assertNotNull("Cipher.getInstance() returned a null value", cipher); 115 116 try { 117 cipher = Cipher.getInstance("DoBeDoBeDo", providers[i]); 118 fail(); 119 } catch (NoSuchAlgorithmException expected) { 120 } 121 } 122 123 try { 124 Cipher.getInstance("DES", (String) null); 125 fail(); 126 } catch (IllegalArgumentException expected) { 127 } 128 129 try { 130 Cipher.getInstance("DES", "IHaveNotBeenConfigured"); 131 fail(); 132 } catch (NoSuchProviderException expected) { 133 } 134 } 135 136 /** 137 * javax.crypto.Cipher#getInstance(java.lang.String, 138 * java.security.Provider) 139 */ 140 public void test_getInstanceLjava_lang_StringLjava_security_Provider() throws Exception { 141 142 Provider[] providers = Security.getProviders("Cipher.DES"); 143 144 assertNotNull("No installed providers support Cipher.DES", providers); 145 146 for (int i = 0; i < providers.length; i++) { 147 Cipher cipher = Cipher.getInstance("DES", providers[i]); 148 assertNotNull("Cipher.getInstance() returned a null value", cipher); 149 } 150 151 try { 152 Cipher.getInstance("DES", (Provider) null); 153 fail(); 154 } catch (IllegalArgumentException expected) { 155 } 156 157 try { 158 Cipher.getInstance("WrongAlg", providers[0]); 159 fail(); 160 } catch (NoSuchAlgorithmException expected) { 161 } 162 } 163 164 /** 165 * javax.crypto.Cipher#getProvider() 166 */ 167 public void test_getProvider() throws Exception { 168 169 Provider[] providers = Security.getProviders("Cipher.AES"); 170 171 assertNotNull("No providers support Cipher.AES", providers); 172 173 for (int i = 0; i < providers.length; i++) { 174 Provider provider = providers[i]; 175 Cipher cipher = Cipher.getInstance("AES", provider.getName()); 176 Provider cipherProvider = cipher.getProvider(); 177 assertTrue("Cipher provider is not the same as that " 178 + "provided as parameter to getInstance()", cipherProvider 179 .equals(provider)); 180 } 181 } 182 183 /** 184 * javax.crypto.Cipher#getAlgorithm() 185 */ 186 public void test_getAlgorithm() throws Exception { 187 final String algorithm = "DESede/CBC/PKCS5Padding"; 188 189 Cipher cipher = Cipher.getInstance(algorithm); 190 assertTrue("Cipher algorithm does not match", cipher.getAlgorithm() 191 .equals(algorithm)); 192 } 193 194 /** 195 * javax.crypto.Cipher#getBlockSize() 196 */ 197 public void test_getBlockSize() throws Exception { 198 final String algorithm = "DESede/CBC/PKCS5Padding"; 199 200 Cipher cipher = Cipher.getInstance(algorithm); 201 assertEquals("Block size does not match", 8, cipher.getBlockSize()); 202 } 203 204 /** 205 * javax.crypto.Cipher#getOutputSize(int) 206 */ 207 public void test_getOutputSizeI() throws Exception { 208 209 Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding"); 210 211 try { 212 cipher.getOutputSize(25); 213 fail(); 214 } catch (IllegalStateException expected) { 215 } 216 217 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, new SecureRandom()); 218 219 // A 25-byte input could result in at least 4 8-byte blocks 220 int result = cipher.getOutputSize(25); 221 assertTrue("Output size too small", result > 31); 222 223 // A 8-byte input should result in 2 8-byte blocks 224 result = cipher.getOutputSize(8); 225 assertTrue("Output size too small", result > 15); 226 } 227 228 /** 229 * javax.crypto.Cipher#init(int, java.security.Key) 230 */ 231 public void test_initWithKey() throws Exception { 232 Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding"); 233 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 234 235 236 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 237 try { 238 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 239 fail(); 240 } catch (InvalidKeyException expected) { 241 } 242 } 243 244 /** 245 * javax.crypto.Cipher#init(int, java.security.Key, 246 * java.security.SecureRandom) 247 */ 248 public void test_initWithSecureRandom() throws Exception { 249 Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding"); 250 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, new SecureRandom()); 251 252 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 253 try { 254 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, new SecureRandom()); 255 fail(); 256 } catch (InvalidKeyException expected) { 257 } 258 } 259 260 /** 261 * javax.crypto.Cipher#init(int, java.security.Key, 262 * java.security.spec.AlgorithmParameterSpec) 263 */ 264 public void test_initWithAlgorithmParameterSpec() throws Exception { 265 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 266 267 Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding"); 268 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap); 269 byte[] cipherIV = cipher.getIV(); 270 assertTrue("IVs differ", Arrays.equals(cipherIV, IV)); 271 272 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 273 try { 274 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap); 275 fail(); 276 } catch (InvalidKeyException expected) { 277 } 278 279 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 280 ap = new RSAKeyGenParameterSpec(10, new BigInteger("10")); 281 try { 282 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 283 fail(); 284 } catch (InvalidAlgorithmParameterException expected) { 285 } 286 } 287 288 /** 289 * javax.crypto.Cipher#init(int, java.security.Key, 290 * java.security.spec.AlgorithmParameterSpec, 291 * java.security.SecureRandom) 292 */ 293 public void test_initWithKeyAlgorithmParameterSpecSecureRandom() throws Exception { 294 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 295 296 Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding"); 297 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom()); 298 byte[] cipherIV = cipher.getIV(); 299 assertTrue("IVs differ", Arrays.equals(cipherIV, IV)); 300 301 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 302 try { 303 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom()); 304 fail(); 305 } catch (InvalidKeyException expected) { 306 } 307 308 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 309 ap = new RSAKeyGenParameterSpec(10, new BigInteger("10")); 310 311 try { 312 cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 313 fail(); 314 } catch (InvalidAlgorithmParameterException expected) { 315 } 316 } 317 318 /** 319 * javax.crypto.Cipher#update(byte[], int, int) 320 */ 321 public void test_update$BII() throws Exception { 322 for (int index = 1; index < 4; index++) { 323 Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); 324 325 byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" 326 + index + ".key"); 327 DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); 328 SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); 329 Key k = skf.generateSecret(keySpec); 330 331 byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index 332 + ".iv"); 333 IvParameterSpec iv = new IvParameterSpec(ivMaterial); 334 335 c.init(Cipher.DECRYPT_MODE, k, iv); 336 337 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 338 byte[] input = new byte[256]; 339 String resPath = "hyts_" + "des-ede3-cbc.test" + index 340 + ".ciphertext"; 341 File resources = Support_Resources.createTempFolder(); 342 Support_Resources.copyFile(resources, null, resPath); 343 InputStream is = Support_Resources.getStream(resPath); 344 345 int bytesRead = is.read(input, 0, 256); 346 while (bytesRead > 0) { 347 byte[] output = c.update(input, 0, bytesRead); 348 if (output != null) { 349 baos.write(output); 350 } 351 bytesRead = is.read(input, 0, 256); 352 } 353 354 byte[] output = c.doFinal(); 355 if (output != null) { 356 baos.write(output); 357 } 358 359 byte[] decipheredCipherText = baos.toByteArray(); 360 is.close(); 361 362 byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" 363 + index + ".plaintext"); 364 assertTrue("Operation produced incorrect results", Arrays.equals( 365 plaintextBytes, decipheredCipherText)); 366 } 367 368 Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); 369 try { 370 cipher.update(new byte[64], 0, 32); 371 fail(); 372 } catch (IllegalStateException expected) { 373 } 374 } 375 376 /** 377 * javax.crypto.Cipher#doFinal() 378 */ 379 public void test_doFinal() throws Exception { 380 for (int index = 1; index < 4; index++) { 381 Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); 382 383 byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" 384 + index + ".key"); 385 DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); 386 SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); 387 Key k = skf.generateSecret(keySpec); 388 389 byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index 390 + ".iv"); 391 IvParameterSpec iv = new IvParameterSpec(ivMaterial); 392 393 c.init(Cipher.ENCRYPT_MODE, k, iv); 394 395 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 396 byte[] input = new byte[256]; 397 String resPath = "hyts_" + "des-ede3-cbc.test" + index 398 + ".plaintext"; 399 File resources = Support_Resources.createTempFolder(); 400 Support_Resources.copyFile(resources, null, resPath); 401 InputStream is = Support_Resources.getStream(resPath); 402 403 int bytesRead = is.read(input, 0, 256); 404 while (bytesRead > 0) { 405 byte[] output = c.update(input, 0, bytesRead); 406 if (output != null) { 407 baos.write(output); 408 } 409 bytesRead = is.read(input, 0, 256); 410 } 411 byte[] output = c.doFinal(); 412 if (output != null) { 413 baos.write(output); 414 } 415 byte[] encryptedPlaintext = baos.toByteArray(); 416 is.close(); 417 418 byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index 419 + ".ciphertext"); 420 assertTrue("Operation produced incorrect results", Arrays.equals( 421 encryptedPlaintext, cipherText)); 422 } 423 424 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 425 byte[] b1 = new byte[30]; 426 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 427 428 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 429 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 430 c.update(b, 0, 10, b1, 5); 431 try { 432 c.doFinal(); 433 fail(); 434 } catch (IllegalBlockSizeException expected) { 435 } 436 437 c = Cipher.getInstance("DES/CBC/NoPadding"); 438 try { 439 c.doFinal(); 440 fail(); 441 } catch (IllegalStateException expected) { 442 } 443 444 c = Cipher.getInstance("DES/CBC/NoPadding"); 445 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 446 int len = c.doFinal(b, 0, 16, b1, 0); 447 assertEquals(16, len); 448 449 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 450 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 451 assertTrue(Arrays.equals(c.getIV(), IV)); 452 453 c.update(b1, 0, 24, b, 0); 454 try { 455 c.doFinal(); 456 fail(); 457 } catch (BadPaddingException expected) { 458 } 459 } 460 461 private byte[] loadBytes(String resPath) throws Exception { 462 File resources = Support_Resources.createTempFolder(); 463 Support_Resources.copyFile(resources, null, resPath); 464 InputStream is = Support_Resources.getStream(resPath); 465 466 ByteArrayOutputStream out = new ByteArrayOutputStream(); 467 byte[] buff = new byte[1024]; 468 int readlen; 469 while ((readlen = is.read(buff)) > 0) { 470 out.write(buff, 0, readlen); 471 } 472 is.close(); 473 return out.toByteArray(); 474 } 475 476 public void testGetParameters() throws Exception { 477 Cipher c = Cipher.getInstance("DES"); 478 assertNull(c.getParameters()); 479 } 480 481 /* 482 * Class under test for int update(byte[], int, int, byte[], int) 483 */ 484 public void testUpdatebyteArrayintintbyteArrayint() throws Exception { 485 byte[] b = {1,2,3,4,5,6,7,8,9,10}; 486 byte[] b1 = new byte[6]; 487 Cipher c = Cipher.getInstance("DESede"); 488 489 try { 490 c.update(b, 0, 10, b1, 5); 491 fail(); 492 } catch (IllegalStateException expected) { 493 } 494 495 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 496 try { 497 c.update(b, 0, 10, b1, 5); 498 fail(); 499 } catch (ShortBufferException expected) { 500 } 501 502 b1 = new byte[30]; 503 c.update(b, 0, 10, b1, 5); 504 } 505 506 /* 507 * Class under test for int doFinal(byte[], int, int, byte[], int) 508 */ 509 public void testDoFinalbyteArrayintintbyteArrayint() throws Exception { 510 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 511 byte[] b1 = new byte[30]; 512 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 513 514 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 515 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 516 try { 517 c.doFinal(b, 0, 10, b1, 5); 518 fail(); 519 } catch (IllegalBlockSizeException expected) { 520 } 521 522 c = Cipher.getInstance("DES/CBC/NoPadding"); 523 try { 524 c.doFinal(b, 0, 10, b1, 5); 525 fail(); 526 } catch (IllegalStateException expected) { 527 } 528 529 c = Cipher.getInstance("DES/CBC/NoPadding"); 530 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 531 int len = c.doFinal(b, 0, 16, b1, 0); 532 assertEquals(16, len); 533 534 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 535 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 536 try { 537 c.doFinal(b1, 0, 24, new byte[42], 0); 538 fail(); 539 } catch (BadPaddingException expected) { 540 } 541 542 b1 = new byte[6]; 543 c = Cipher.getInstance("DESede"); 544 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 545 try { 546 c.doFinal(b, 3, 6, b1, 5); 547 fail(); 548 } catch (ShortBufferException expected) { 549 } 550 } 551 552 public void testGetMaxAllowedKeyLength() throws Exception { 553 try { 554 Cipher.getMaxAllowedKeyLength(null); 555 fail(); 556 } catch (NullPointerException expected) { 557 } 558 try { 559 Cipher.getMaxAllowedKeyLength(""); 560 } catch (NoSuchAlgorithmException expected) { 561 } 562 try { 563 Cipher.getMaxAllowedKeyLength("//CBC/PKCS5Paddin"); 564 fail(); 565 } catch (NoSuchAlgorithmException expected) { 566 } 567 try { 568 Cipher.getMaxAllowedKeyLength("/DES/CBC/PKCS5Paddin/1"); 569 fail(); 570 } catch (NoSuchAlgorithmException expected) { 571 } 572 assertTrue(Cipher.getMaxAllowedKeyLength("/DES/CBC/PKCS5Paddin") > 0); 573 } 574 575 public void testGetMaxAllowedParameterSpec() throws Exception { 576 try { 577 Cipher.getMaxAllowedParameterSpec(null); 578 fail(); 579 } catch (NullPointerException expected) { 580 } 581 try { 582 Cipher.getMaxAllowedParameterSpec("/DES//PKCS5Paddin"); 583 fail(); 584 } catch (NoSuchAlgorithmException expected) { 585 } 586 try { 587 Cipher.getMaxAllowedParameterSpec("/DES/CBC/ /1"); 588 fail(); 589 } catch (NoSuchAlgorithmException expected) { 590 } 591 Cipher.getMaxAllowedParameterSpec("DES/CBC/PKCS5Paddin"); 592 Cipher.getMaxAllowedParameterSpec("RSA"); 593 } 594 595 /** 596 * javax.crypto.Cipher#Cipher(CipherSpi cipherSpi, Provider provider, 597 * String transformation) 598 */ 599 public void test_Ctor() throws Exception { 600 // Regression for Harmony-1184 601 try { 602 new testCipher(null, null, "s"); 603 fail(); 604 } catch (NullPointerException expected) { 605 } 606 607 try { 608 new testCipher(new MyCipher(), null, "s"); 609 fail("NullPointerException expected for 'null' provider"); 610 } catch (NullPointerException expected) { 611 } 612 613 try { 614 new testCipher(null, new Provider("qwerty", 1.0, "qwerty") {}, "s"); 615 fail("NullPointerException expected for 'null' cipherSpi"); 616 } catch (NullPointerException expected) { 617 } 618 } 619 620 public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception { 621 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 622 ByteBuffer bInput = ByteBuffer.allocate(64); 623 ByteBuffer bOutput = ByteBuffer.allocate(64); 624 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 625 626 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 627 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 628 bInput.put(b, 0, 10); 629 try { 630 c.doFinal(bInput, bOutput); 631 fail(); 632 } catch (IllegalBlockSizeException expected) { 633 } 634 635 c = Cipher.getInstance("DES/CBC/NoPadding"); 636 try { 637 c.doFinal(bInput, bOutput); 638 fail(); 639 } catch (IllegalStateException expected) { 640 } 641 642 c = Cipher.getInstance("DES/CBC/NoPadding"); 643 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 644 bInput = ByteBuffer.allocate(16); 645 bInput.put(b, 0, 16); 646 int len = c.doFinal(bInput, bOutput); 647 assertEquals(0, len); 648 649 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 650 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 651 bInput = ByteBuffer.allocate(64); 652 try { 653 c.doFinal(bOutput, bInput); 654 fail(); 655 } catch (BadPaddingException expected) { 656 } 657 658 c = Cipher.getInstance("DES/CBC/NoPadding"); 659 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 660 bInput.put(b, 0, 16); 661 try { 662 c.doFinal(bInput, bInput); 663 fail(); 664 } catch (IllegalArgumentException expected) { 665 } 666 667 c = Cipher.getInstance("DES/CBC/NoPadding"); 668 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 669 bInput.put(b, 0, 16); 670 try { 671 c.doFinal(bInput, bOutput.asReadOnlyBuffer()); 672 fail(); 673 } catch (ReadOnlyBufferException expected) { 674 } 675 676 bInput.rewind(); 677 bInput.put(b, 0, 16); 678 bOutput = ByteBuffer.allocate(8); 679 c = Cipher.getInstance("DESede"); 680 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 681 try { 682 c.doFinal(bInput, bOutput); 683 fail(); 684 } catch (ShortBufferException expected) { 685 } 686 } 687 688 public void test_initWithKeyAlgorithmParameters() throws Exception { 689 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 690 691 Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 692 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 693 assertNotNull(c.getParameters()); 694 695 try { 696 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_3DES, ap); 697 fail(); 698 } catch (InvalidKeyException expected) { 699 } 700 701 try { 702 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, (AlgorithmParameters)null); 703 fail(); 704 } catch (InvalidAlgorithmParameterException expected) { 705 } 706 } 707 708 public void test_initWithKeyAlgorithmParametersSecureRandom() throws Exception { 709 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 710 711 Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 712 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 713 assertNotNull(c.getParameters()); 714 715 try { 716 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom()); 717 fail(); 718 } catch (InvalidKeyException expected) { 719 } 720 721 try { 722 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, (AlgorithmParameters)null, 723 new SecureRandom()); 724 fail(); 725 } catch (InvalidAlgorithmParameterException expected) { 726 } 727 728 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, (SecureRandom)null); 729 assertNotNull(c.getParameters()); 730 } 731 732 public void test_initWithCertificate() throws Exception { 733 734 /* Certificate creation notes: certificate should be valid 37273 starting 735 * from 13 Nov 2008 736 * If it brcomes invalidated regenerate it using following commands: 737 * 1. openssl genrsa -des3 -out test.key 1024 738 * 2. openssl req -new -key test.key -out test.csr 739 * 3. cp test.key test.key.org 740 * 4. openssl rsa -in test.key.org -out test.key 741 * 5. openssl x509 -req -days 37273 -in test.csr -signkey test.key -out test.cert 742 * */ 743 744 String certName = Support_Resources.getURL("test.cert"); 745 InputStream is = new URL(certName).openConnection().getInputStream(); 746 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 747 748 Certificate cert = cf.generateCertificate(is); 749 is.close(); 750 751 Cipher c = Cipher.getInstance("RSA"); 752 753 c.init(Cipher.ENCRYPT_MODE, cert); 754 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 755 try { 756 c.init(Cipher.ENCRYPT_MODE, cert); 757 fail(); 758 } catch (InvalidKeyException expected) { 759 } 760 } 761 762 public void test_initWithCertificateSecureRandom() throws Exception { 763 764 /* Certificate creation notes: certificate should be valid 37273 starting 765 * from 13 Nov 2008 766 * If it brcomes invalidated regenerate it using following commands: 767 * 1. openssl genrsa -des3 -out test.key 1024 768 * 2. openssl req -new -key test.key -out test.csr 769 * 3. cp test.key test.key.org 770 * 4. openssl rsa -in test.key.org -out test.key 771 * 5. openssl x509 -req -days 37273 -in test.csr -signkey test.key -out test.cert 772 * */ 773 774 String certName = Support_Resources.getURL("test.cert"); 775 InputStream is = new URL(certName).openConnection().getInputStream(); 776 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 777 778 Certificate cert = cf.generateCertificate(is); 779 is.close(); 780 781 Cipher c = Cipher.getInstance("RSA"); 782 783 c.init(Cipher.ENCRYPT_MODE, cert, new SecureRandom()); 784 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 785 try { 786 c.init(Cipher.ENCRYPT_MODE, cert, new SecureRandom()); 787 fail(); 788 } catch (InvalidKeyException expected) { 789 } 790 } 791 792 public void test_unwrap$BLjava_lang_StringI() throws Exception { 793 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 794 795 Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 796 797 c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 798 byte[] arDES = c.wrap(CIPHER_KEY_DES); 799 byte[] ar = c.wrap(CIPHER_KEY_3DES); 800 801 try { 802 c.unwrap(arDES, "DES", Cipher.SECRET_KEY); 803 fail(); 804 } catch (IllegalStateException expected) { 805 } 806 807 c.init(Cipher.UNWRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 808 assertTrue(CIPHER_KEY_DES.equals(c.unwrap(arDES, "DES", Cipher.SECRET_KEY))); 809 assertFalse(CIPHER_KEY_DES.equals(c.unwrap(ar, "DES", Cipher.SECRET_KEY))); 810 811 try { 812 c.unwrap(arDES, "RSA38", Cipher.PUBLIC_KEY); 813 fail(); 814 } catch (NoSuchAlgorithmException expected) { 815 } 816 817 c = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 818 c.init(Cipher.UNWRAP_MODE, CIPHER_KEY_3DES, ap, new SecureRandom()); 819 try { 820 c.unwrap(arDES, "DESede", Cipher.SECRET_KEY); 821 fail(); 822 } catch (InvalidKeyException expected) { 823 } 824 } 825 826 public void test_updateLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception { 827 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 828 ByteBuffer bInput = ByteBuffer.allocate(256); 829 ByteBuffer bOutput = ByteBuffer.allocate(256); 830 831 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 832 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 833 bInput.put(b, 0, 10); 834 bInput.rewind(); 835 bOutput.rewind(); 836 c.update(bInput, bOutput); 837 838 c = Cipher.getInstance("DES/CBC/NoPadding"); 839 try { 840 c.update(bInput, bOutput); 841 fail(); 842 } catch (IllegalStateException expected) { 843 } 844 845 c = Cipher.getInstance("DES/CBC/NoPadding"); 846 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 847 bInput = ByteBuffer.allocate(16); 848 bInput.put(b, 0, 16); 849 bInput.rewind(); 850 bOutput.rewind(); 851 c.update(bInput, bOutput); 852 853 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 854 855 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 856 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 857 bInput = ByteBuffer.allocate(64); 858 859 c = Cipher.getInstance("DES/CBC/NoPadding"); 860 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 861 bInput.put(b, 0, 16); 862 bInput.rewind(); 863 try { 864 c.update(bInput, bInput); 865 fail(); 866 } catch (IllegalArgumentException expected) { 867 } 868 869 c = Cipher.getInstance("DES/CBC/NoPadding"); 870 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 871 bInput.put(b, 0, 16); 872 bInput.rewind(); 873 bOutput.rewind(); 874 try { 875 c.update(bInput, bOutput.asReadOnlyBuffer()); 876 fail(); 877 } catch (ReadOnlyBufferException expected) { 878 } 879 880 bInput.rewind(); 881 bInput.put(b, 0, 16); 882 bInput.rewind(); 883 bOutput = ByteBuffer.allocate(8); 884 c = Cipher.getInstance("DESede"); 885 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 886 try { 887 c.update(bInput, bOutput); 888 fail(); 889 } catch (ShortBufferException expected) { 890 } 891 } 892 893 class Mock_Key implements Key { 894 public String getAlgorithm() { 895 return null; 896 } 897 898 public byte[] getEncoded() { 899 return null; 900 } 901 902 public String getFormat() { 903 return null; 904 } 905 906 } 907 908 public void test_wrap_java_security_Key() throws Exception { 909 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 910 911 Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 912 913 c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 914 assertNotNull(c.wrap(CIPHER_KEY_DES)); 915 assertNotNull(c.wrap(CIPHER_KEY_3DES)); 916 String certName = Support_Resources.getURL("test.cert"); 917 InputStream is = new URL(certName).openConnection().getInputStream(); 918 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 919 920 Certificate cert = cf.generateCertificate(is); 921 assertNotNull(c.wrap(cert.getPublicKey())); 922 923 c = Cipher.getInstance("DES/CBC/NoPadding"); 924 c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 925 try { 926 assertNotNull(c.wrap(cert.getPublicKey())); 927 fail(); 928 } catch (IllegalBlockSizeException expected) { 929 } 930 931 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 932 933 try { 934 c.wrap(CIPHER_KEY_DES); 935 fail(); 936 } catch (IllegalStateException expected) { 937 } 938 939 c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom()); 940 try { 941 c.wrap(new Mock_Key()); 942 fail(); 943 } catch (InvalidKeyException expected) { 944 } 945 } 946 947 public void test_doFinal$BI() throws Exception { 948 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 949 byte[] b1 = new byte[30]; 950 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 951 952 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 953 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 954 c.update(b, 0, 10); 955 try { 956 c.doFinal(b1, 5); 957 fail(); 958 } catch (IllegalBlockSizeException expected) { 959 } 960 961 c = Cipher.getInstance("DES/CBC/NoPadding"); 962 try { 963 c.doFinal(b1, 5); 964 fail(); 965 } catch (IllegalStateException expected) { 966 } 967 968 c = Cipher.getInstance("DES/CBC/NoPadding"); 969 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 970 c.update(b, 3, 8); 971 int len = c.doFinal(b1, 0); 972 assertEquals(0, len); 973 974 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 975 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 976 c.update(b1, 0, 24); 977 try { 978 c.doFinal(b, 0); 979 fail(); 980 } catch (BadPaddingException expected) { 981 } 982 983 b1 = new byte[6]; 984 c = Cipher.getInstance("DESede"); 985 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 986 c.update(b, 3, 6); 987 try { 988 c.doFinal(b1, 5); 989 fail(); 990 } catch (ShortBufferException expected) { 991 } 992 } 993 994 public void test_doFinal$B() throws Exception { 995 byte[] b1 = new byte[32]; 996 byte[] bI1 = {1,2,3,4,5,6,7,8,9,10}; 997 byte[] bI2 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; 998 byte[] bI3 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 999 byte[] bI4 = {1,2,3}; 1000 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 1001 1002 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 1003 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1004 try { 1005 c.doFinal(bI1); 1006 fail(); 1007 } catch (IllegalBlockSizeException expected) { 1008 } 1009 1010 c = Cipher.getInstance("DES/CBC/NoPadding"); 1011 try { 1012 c.doFinal(bI1); 1013 fail(); 1014 } catch (IllegalStateException expected) { 1015 } 1016 1017 c = Cipher.getInstance("DES/CBC/NoPadding"); 1018 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1019 int len1 = c.doFinal(bI2).length; 1020 assertEquals(16, len1); 1021 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1022 int len2 = c.doFinal(bI3, 0, 16, b1, 0); 1023 assertEquals(16, len2); 1024 1025 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 1026 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 1027 try { 1028 c.doFinal(b1); 1029 fail(); 1030 } catch (BadPaddingException expected) { 1031 } 1032 } 1033 1034 public void test_doFinal$BII() throws Exception { 1035 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 1036 byte[] b1 = new byte[30]; 1037 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 1038 1039 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 1040 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1041 try { 1042 c.doFinal(b, 0, 10); 1043 fail(); 1044 } catch (IllegalBlockSizeException expected) { 1045 } 1046 1047 c = Cipher.getInstance("DES/CBC/NoPadding"); 1048 try { 1049 c.doFinal(b, 0, 10); 1050 fail(); 1051 } catch (IllegalStateException expected) { 1052 } 1053 1054 c = Cipher.getInstance("DES/CBC/NoPadding"); 1055 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1056 int len1 = c.doFinal(b, 0, 16).length; 1057 assertEquals(16, len1); 1058 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1059 int len2 = c.doFinal(b, 0, 16, b1, 0); 1060 assertEquals(16, len2); 1061 1062 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 1063 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 1064 try { 1065 c.doFinal(b1, 0, 24); 1066 fail(); 1067 } catch (BadPaddingException expected) { 1068 } 1069 } 1070 1071 public void test_doFinal$BII$B() throws Exception { 1072 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 1073 byte[] b1 = new byte[30]; 1074 AlgorithmParameterSpec ap = new IvParameterSpec(IV); 1075 1076 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 1077 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1078 try { 1079 c.doFinal(b, 0, 10, b1); 1080 fail(); 1081 } catch (IllegalBlockSizeException expected) { 1082 } 1083 1084 c = Cipher.getInstance("DES/CBC/NoPadding"); 1085 try { 1086 c.doFinal(b, 0, 10, b1); 1087 fail(); 1088 } catch (IllegalStateException expected) { 1089 } 1090 1091 c = Cipher.getInstance("DES/CBC/NoPadding"); 1092 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap); 1093 int len = c.doFinal(b, 0, 16, b1); 1094 assertEquals(16, len); 1095 1096 c = Cipher.getInstance("DES/CBC/PKCS5Padding"); 1097 c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap); 1098 try { 1099 c.doFinal(b1, 0, 24, new byte[42]); 1100 fail(); 1101 } catch (BadPaddingException expected) { 1102 } 1103 1104 b1 = new byte[6]; 1105 c = Cipher.getInstance("DESede"); 1106 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES); 1107 try { 1108 c.doFinal(b, 3, 6, b1); 1109 fail(); 1110 } catch (ShortBufferException expected) { 1111 } 1112 } 1113 1114 public void test_update$B() throws Exception { 1115 Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); 1116 try { 1117 cipher.update(new byte[64]); 1118 fail(); 1119 } catch (IllegalStateException expected) { 1120 } 1121 } 1122 1123 public void test_() throws Exception { 1124 byte[] b = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 1125 byte[] b1 = new byte[30]; 1126 1127 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 1128 1129 try { 1130 c.update(b, 0, 10, b1); 1131 fail(); 1132 } catch (IllegalStateException expected) { 1133 } 1134 1135 c = Cipher.getInstance("DES/CBC/NoPadding"); 1136 c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES); 1137 c.update(b, 0, 16, b1); 1138 1139 b1 = new byte[3]; 1140 1141 try { 1142 c.update(b, 3, 15, b1); 1143 fail(); 1144 } catch (ShortBufferException expected) { 1145 } 1146 } 1147 1148 class testCipher extends Cipher { 1149 testCipher(CipherSpi c, Provider p, String s) { 1150 super(c, p, s); 1151 } 1152 } 1153 } 1154