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