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