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 /* 25 * @test 26 * @bug 8014374 27 * @summary Test CipherInputStream/OutputStream func w/ GCM mode and AAD. 28 * @author Valerie Peng 29 */ 30 31 import java.io.*; 32 import java.security.*; 33 import java.util.*; 34 import javax.crypto.*; 35 import javax.crypto.spec.*; 36 37 public class TestCICOWithGCMAndAAD extends UcryptoTest { 38 public static void main(String[] args) throws Exception { 39 main(new TestCICOWithGCMAndAAD(), null); 40 } 41 42 public void doTest(Provider p) throws Exception { 43 // check if GCM support exists 44 try { 45 Cipher.getInstance("AES/GCM/NoPadding", p); 46 } catch (NoSuchAlgorithmException nsae) { 47 System.out.println("Skipping Test due to no GCM support"); 48 return; 49 } 50 51 Random rdm = new Random(); 52 53 //init Secret Key 54 byte[] keyValue = new byte[16]; 55 rdm.nextBytes(keyValue); 56 SecretKey key = new SecretKeySpec(keyValue, "AES"); 57 58 //Do initialization of the plainText 59 byte[] plainText = new byte[400]; 60 rdm.nextBytes(plainText); 61 62 byte[] aad = new byte[128]; 63 rdm.nextBytes(aad); 64 byte[] aad2 = aad.clone(); 65 aad2[50]++; 66 67 GCMParameterSpec spec = new GCMParameterSpec(128, new byte[16]); 68 Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", p); 69 encCipher.init(Cipher.ENCRYPT_MODE, key, spec); 70 encCipher.updateAAD(aad); 71 Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", p); 72 decCipher.init(Cipher.DECRYPT_MODE, key, spec); //encCipher.getParameters()); 73 decCipher.updateAAD(aad); 74 75 byte[] recovered = test(encCipher, decCipher, plainText); 76 if (!Arrays.equals(plainText, recovered)) { 77 throw new Exception("sameAAD: diff check failed!"); 78 } else System.out.println("sameAAD: passed"); 79 80 encCipher.init(Cipher.ENCRYPT_MODE, key); 81 encCipher.updateAAD(aad2); 82 recovered = test(encCipher, decCipher, plainText); 83 if (recovered != null && recovered.length != 0) { 84 throw new Exception("diffAAD: no data should be returned!"); 85 } else System.out.println("diffAAD: passed"); 86 } 87 88 private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText) 89 throws Exception { 90 //init cipher streams 91 ByteArrayInputStream baInput = new ByteArrayInputStream(plainText); 92 CipherInputStream ciInput = new CipherInputStream(baInput, encCipher); 93 ByteArrayOutputStream baOutput = new ByteArrayOutputStream(); 94 CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher); 95 96 //do test 97 byte[] buffer = new byte[200]; 98 int len = ciInput.read(buffer); 99 System.out.println("read " + len + " bytes from input buffer"); 100 101 while (len != -1) { 102 ciOutput.write(buffer, 0, len); 103 System.out.println("wite " + len + " bytes to output buffer"); 104 len = ciInput.read(buffer); 105 if (len != -1) { 106 System.out.println("read " + len + " bytes from input buffer"); 107 } else { 108 System.out.println("finished reading"); 109 } 110 } 111 112 ciOutput.flush(); 113 ciInput.close(); 114 ciOutput.close(); 115 116 return baOutput.toByteArray(); 117 } 118 }