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