1 /* 2 * Copyright (c) 2013, 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 /* 25 * @test 26 * @bug 8012637 27 * @library ../UTIL 28 * @build TestUtil 29 * @run main TestCICOWithGCM 30 * @summary Test CipherInputStream/OutputStream with AES GCM mode. 31 * @author Valerie Peng 32 */ 33 34 import java.security.*; 35 import javax.crypto.*; 36 import javax.crypto.spec.*; 37 import java.math.*; 38 import java.io.*; 39 import com.sun.crypto.provider.*; 40 41 import java.util.*; 42 43 public class TestCICOWithGCM { 44 public static void main(String[] args) throws Exception { 45 //init Secret Key 46 KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE"); 47 kg.init(128); 48 SecretKey key = kg.generateKey(); 49 50 //do initialization of the plainText 51 byte[] plainText = new byte[800]; 52 Random rdm = new Random(); 53 rdm.nextBytes(plainText); 54 55 //init ciphers 56 Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE"); 57 encCipher.init(Cipher.ENCRYPT_MODE, key); 58 Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE"); 59 decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters()); 60 61 //init cipher streams 62 ByteArrayInputStream baInput = new ByteArrayInputStream(plainText); 63 CipherInputStream ciInput = new CipherInputStream(baInput, encCipher); 64 ByteArrayOutputStream baOutput = new ByteArrayOutputStream(); 65 CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher); 66 67 //do test 68 byte[] buffer = new byte[800]; 69 int len = ciInput.read(buffer); 70 System.out.println("read " + len + " bytes from input buffer"); 71 72 while (len != -1) { 73 ciOutput.write(buffer, 0, len); 74 System.out.println("wite " + len + " bytes to output buffer"); 75 len = ciInput.read(buffer); 76 if (len != -1) { 77 System.out.println("read " + len + " bytes from input buffer"); 78 } else { 79 System.out.println("finished reading"); 80 } 81 } 82 83 ciOutput.flush(); 84 ciInput.close(); 85 ciOutput.close(); 86 byte[] recovered = baOutput.toByteArray(); 87 System.out.println("recovered " + recovered.length + " bytes"); 88 if (!Arrays.equals(plainText, recovered)) { 89 throw new RuntimeException("diff check failed!"); 90 } else { 91 System.out.println("diff check passed"); 92 } 93 } 94 }