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.io.ByteArrayInputStream; 25 import java.io.ByteArrayOutputStream; 26 import java.io.IOException; 27 import java.security.InvalidAlgorithmParameterException; 28 import java.security.InvalidKeyException; 29 import java.security.NoSuchAlgorithmException; 30 import java.security.NoSuchProviderException; 31 import java.security.spec.AlgorithmParameterSpec; 32 import java.util.Random; 33 import javax.crypto.Cipher; 34 import javax.crypto.CipherInputStream; 35 import javax.crypto.CipherOutputStream; 36 import javax.crypto.KeyGenerator; 37 import javax.crypto.NoSuchPaddingException; 38 import javax.crypto.SecretKey; 39 import javax.crypto.spec.IvParameterSpec; 40 41 /** 42 * @test 43 * @bug 8043836 44 * @summary Test AES ciphers with different modes and padding schemes (ECB mode 45 * doesn't use IV). The test tries 3 different read methods of 46 * CipherInputStream. 47 * @key randomness 48 */ 49 public class CICO { 50 private static final String ALGORITHM = "aEs"; 51 private static final String[] MODES = { "PCBC", "ECb", "cbC", "cFB", 52 "cFB24", "cFB32", "Cfb40", "CFB72", "OfB", "OfB20", "OfB48", 53 "OfB56", "OFB64", "OFB112", "CFB112", "pCbC" }; 54 private static final String[] PADDING = { "noPadding", "pkcs5padding" }; 55 private static final String PROVIDER = "SunJCE"; 56 private static final int NREADS = 3; 57 private static final int KEY_LENGTH = 128; 58 59 private final byte[] plainText = new byte[1600000]; 60 61 62 public static void main(String argv[]) throws Exception { 63 CICO test = new CICO(); 64 for (String mode : MODES) { 65 for (String pad : PADDING) { 66 for (int m = 0; m < NREADS; m++) { 67 test.runTest(ALGORITHM, mode, pad, m); 68 } 69 } 70 } 71 } 72 73 public void runTest(String algo, String mo, String pad, int whichRead) throws Exception { 74 Cipher ci1 = null; 75 Cipher ci2 = null; 76 byte[] iv = null; 77 AlgorithmParameterSpec aps = null; 78 SecretKey key = null; 79 80 try { 81 // Do initialization 82 Random rdm = new Random(); 83 rdm.nextBytes(plainText); 84 KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER); 85 if (!kg.getAlgorithm().equals(algo)) { 86 throw new RuntimeException("Unexpected algorithm <" 87 + kg.getAlgorithm() + ">, expected value is <" + algo 88 + ">"); 89 } 90 91 kg.init(KEY_LENGTH); 92 key = kg.generateKey(); 93 94 ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER); 95 96 if (mo.equalsIgnoreCase("ECB")) { 97 ci1.init(Cipher.ENCRYPT_MODE, key); 98 } else { 99 ci1.init(Cipher.ENCRYPT_MODE, key, aps); 100 } 101 102 if (!mo.equalsIgnoreCase("ECB")) { 103 iv = ci1.getIV(); 104 aps = new IvParameterSpec(iv); 105 } else { 106 aps = null; 107 } 108 109 ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER); 110 if (mo.equalsIgnoreCase("ECB")) { 111 ci2.init(Cipher.DECRYPT_MODE, key); 112 } else { 113 ci2.init(Cipher.DECRYPT_MODE, key, aps); 114 } 115 116 ByteArrayInputStream baInput = new ByteArrayInputStream(plainText); 117 ByteArrayOutputStream baOutput = new ByteArrayOutputStream(); 118 try (CipherInputStream ciInput = new CipherInputStream(baInput, ci1); 119 CipherOutputStream ciOutput = new CipherOutputStream( 120 baOutput, ci2)) { 121 // According to specification, CipherInputStream does not support the 122 // mark and reset methods 123 if (ciInput.markSupported()) { 124 throw new RuntimeException( 125 "CipherInputStream unexpectedly supports the mark and reset methods"); 126 } 127 128 // Read from the input and write to the output using 2 types 129 // of buffering : byte[] and int 130 switch (whichRead) { 131 case 0: 132 int buffer0 = ciInput.read(); 133 while (buffer0 != -1) { 134 ciOutput.write(buffer0); 135 buffer0 = ciInput.read(); 136 } 137 break; 138 case 1: 139 byte[] buffer1 = new byte[20]; 140 int len1 = ciInput.read(buffer1); 141 while (len1 != -1) { 142 ciOutput.write(buffer1, 0, len1); 143 len1 = ciInput.read(buffer1); 144 } 145 break; 146 case NREADS - 1: 147 byte[] buffer2 = new byte[ci1 148 .getOutputSize(plainText.length)]; 149 int offset2 = 0; 150 int len2 = 0; 151 while (len2 != -1) { 152 len2 = ciInput.read(buffer2, offset2, buffer2.length 153 - offset2); 154 offset2 += len2; 155 } 156 ciOutput.write(buffer2, 0, buffer2.length); 157 break; 158 } 159 } 160 161 // Get the output 162 byte[] recoveredText = new byte[baOutput.size()]; 163 recoveredText = baOutput.toByteArray(); 164 if (!java.util.Arrays.equals(plainText, recoveredText)) { 165 throw new RuntimeException( 166 "Original text is not equal with recovered text, with " 167 + algo + "/" + mo + "/" + pad + "/" + whichRead); 168 } 169 170 // Compare input and output 171 172 } catch (NoSuchAlgorithmException e) { 173 //OFB20 is for negative testing 174 if (!mo.equalsIgnoreCase("OFB20")) { 175 System.out.println("Unexpected NoSuchAlgorithmException with " 176 + algo + "/" + mo + "/" + pad + "/" + whichRead); 177 throw new RuntimeException("Test failed!"); 178 } 179 } catch (IOException | NoSuchProviderException | NoSuchPaddingException 180 | InvalidKeyException | InvalidAlgorithmParameterException e) { 181 System.out.println("Unexpected Exception with " 182 + algo + "/" + mo + "/" + pad + "/" + whichRead); 183 System.out.println("Test failed!"); 184 throw e; 185 } 186 } 187 }