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 * @key randomness 46 */ 47 public class TestSameBuffer { 48 49 private static final String ALGORITHM = "Rijndael"; 50 private static final String PROVIDER = "SunJCE"; 51 private static final String[] MODES = { "ECb", "CbC", "OFB", "CFB150", 52 "cFB", "CFB7", " cFB8", "cFB16", "cFB24", "cFB32", "Cfb40", 53 "cfB48", " cfB56", "cfB64", "cfB72", "cfB80", "cfB88", "cfB96", 54 "cfb104", "cfB112", "cfB120" }; 55 private static final String PADDING = "NoPadding"; 56 private static final int KEY_LENGTH = 128; 57 58 public static void main(String argv[]) throws Exception { 59 TestSameBuffer test = new TestSameBuffer(); 60 for (String mode : MODES) { 61 test.runTest(ALGORITHM, mode, PADDING); 62 } 63 } 64 65 public void runTest(String algo, String mo, String pad) throws Exception { 66 Cipher ci = null; 67 byte[] iv = null; 68 AlgorithmParameterSpec aps = null; 69 SecretKey key = null; 70 try { 71 // Initialization 72 Random rdm = new Random(); 73 byte[] plainText = new byte[128]; 74 rdm.nextBytes(plainText); 75 76 // keep the plain text 77 byte[] tmpText = new byte[plainText.length]; 78 for (int i = 0; i < plainText.length; i++) { 79 tmpText[i] = plainText[i]; 80 } 81 82 ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER); 83 84 KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER); 85 kg.init(KEY_LENGTH); 86 key = kg.generateKey(); 87 88 // encrypt 89 ci.init(Cipher.ENCRYPT_MODE, key); 90 int offset = ci 91 .update(plainText, 0, plainText.length, plainText, 0); 92 ci.doFinal(plainText, offset); 93 94 if (!mo.equalsIgnoreCase("ECB")) { 95 iv = ci.getIV(); 96 aps = new IvParameterSpec(iv); 97 } else { 98 aps = null; 99 } 100 101 ci.init(Cipher.DECRYPT_MODE, key, aps); 102 byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)]; 103 ci.doFinal(plainText, 0, plainText.length, recoveredText); 104 105 // Comparison 106 if (!java.util.Arrays.equals(tmpText, recoveredText)) { 107 System.out.println("Original: "); 108 dumpBytes(plainText); 109 System.out.println("Recovered: "); 110 dumpBytes(recoveredText); 111 throw new RuntimeException( 112 "Original text is not equal with recovered text, with mode:" 113 + mo); 114 } 115 116 } catch (NoSuchAlgorithmException e) { 117 //CFB7 and CFB150 are for negative testing 118 if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("CFB150")) { 119 System.out.println("Unexpected NoSuchAlgorithmException with mode: " 120 + mo); 121 throw new RuntimeException("Test failed!"); 122 } 123 } catch (NoSuchProviderException | NoSuchPaddingException 124 | InvalidKeyException | InvalidAlgorithmParameterException 125 | ShortBufferException | IllegalBlockSizeException 126 | BadPaddingException e) { 127 System.out.println("Test failed!"); 128 throw e; 129 } 130 } 131 132 private void dumpBytes(byte[] bytes) { 133 for (byte b : bytes) { 134 System.out.print(Integer.toHexString(b)); 135 } 136 137 System.out.println(); 138 } 139 }