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