1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.security.InvalidAlgorithmParameterException; 25 import java.security.InvalidKeyException; 26 import java.security.NoSuchAlgorithmException; 27 import java.security.NoSuchProviderException; 28 import java.security.spec.AlgorithmParameterSpec; 29 import java.util.Random; 30 import javax.crypto.BadPaddingException; 31 import javax.crypto.Cipher; 32 import javax.crypto.IllegalBlockSizeException; 33 import javax.crypto.KeyGenerator; 34 import javax.crypto.NoSuchPaddingException; 35 import javax.crypto.SecretKey; 36 import javax.crypto.ShortBufferException; 37 import javax.crypto.spec.IvParameterSpec; 38 39 /** 40 * @test 41 * @bug 8043836 42 * @summary Test AES ciphers with different modes and padding schemes (ECB mode 43 * doesn't use IV). The test tries 3 different read methods of 44 * CipherInputStream. 45 */ 46 public class TestSameBuffer { 47 48 private static final String ALGORITHM = "Rijndael"; 49 private static final String PROVIDER = "SunJCE"; 50 private static final String[] MODES = { "ECb", "CbC", "OFB", "CFB150", 51 "cFB", "CFB7", " cFB8", "cFB16", "cFB24", "cFB32", "Cfb40", 52 "cfB48", " cfB56", "cfB64", "cfB72", "cfB80", "cfB88", "cfB96", 53 "cfb104", "cfB112", "cfB120" }; 54 private static final String PADDING = "NoPadding"; 55 private static final int KEY_LENGTH = 128; 56 57 public static void main(String argv[]) throws Exception { 58 TestSameBuffer test = new TestSameBuffer(); 59 for (String mode : MODES) { 60 test.runTest(ALGORITHM, mode, PADDING); 61 } 62 } 63 64 public void runTest(String algo, String mo, String pad) throws Exception { 65 Cipher ci = null; 66 byte[] iv = null; 67 AlgorithmParameterSpec aps = null; 68 SecretKey key = null; 69 try { 70 // Initialization 71 Random rdm = new Random(); 72 byte[] plainText = new byte[128]; 73 rdm.nextBytes(plainText); 74 75 // keep the plain text 76 byte[] tmpText = new byte[plainText.length]; 77 for (int i = 0; i < plainText.length; i++) { 78 tmpText[i] = plainText[i]; 79 } 80 81 ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER); 82 83 KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER); 84 kg.init(KEY_LENGTH); 85 key = kg.generateKey(); 86 87 // encrypt 88 ci.init(Cipher.ENCRYPT_MODE, key); 89 int offset = ci 90 .update(plainText, 0, plainText.length, plainText, 0); 91 ci.doFinal(plainText, offset); 92 93 if (!mo.equalsIgnoreCase("ECB")) { 94 iv = ci.getIV(); 95 aps = new IvParameterSpec(iv); 96 } else { 97 aps = null; 98 } 99 100 ci.init(Cipher.DECRYPT_MODE, key, aps); 101 byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)]; 102 ci.doFinal(plainText, 0, plainText.length, recoveredText); 103 104 // Comparison 105 if (!java.util.Arrays.equals(tmpText, recoveredText)) { 106 System.out.println("Original: "); 107 dumpBytes(plainText); 108 System.out.println("Recovered: "); 109 dumpBytes(recoveredText); 110 throw new RuntimeException( 111 "Original text is not equal with recovered text, with mode:" 112 + mo); 113 } 114 115 } catch (NoSuchAlgorithmException e) { 116 //CFB7 and CFB150 are for negative testing 117 if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("CFB150")) { 118 System.out.println("Unexpected NoSuchAlgorithmException with mode: " 119 + mo); 120 throw new RuntimeException("Test failed!"); 121 } 122 } catch (NoSuchProviderException | NoSuchPaddingException 123 | InvalidKeyException | InvalidAlgorithmParameterException 124 | ShortBufferException | IllegalBlockSizeException 125 | BadPaddingException e) { 126 System.out.println("Test failed!"); 127 throw e; 128 } 129 } 130 131 private void dumpBytes(byte[] bytes) { 132 for (byte b : bytes) { 133 System.out.print(Integer.toHexString(b)); 134 } 135 136 System.out.println(); 137 } 138 }