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 GCMParameterSpec spec = new GCMParameterSpec(128, new byte[16]); 69 Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", p); 70 encCipher.init(Cipher.ENCRYPT_MODE, key, spec); 71 encCipher.updateAAD(aad); 72 Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", p); 73 decCipher.init(Cipher.DECRYPT_MODE, key, spec); //encCipher.getParameters()); 74 decCipher.updateAAD(aad); 75 76 byte[] recovered = test(encCipher, decCipher, plainText); 77 if (!Arrays.equals(plainText, recovered)) { 78 throw new Exception("sameAAD: diff check failed!"); 79 } else System.out.println("sameAAD: passed"); 80 81 encCipher.init(Cipher.ENCRYPT_MODE, key); 82 encCipher.updateAAD(aad2); 83 recovered = test(encCipher, decCipher, plainText); 84 if (recovered != null && recovered.length != 0) { 85 throw new Exception("diffAAD: no data should be returned!"); 86 } else System.out.println("diffAAD: passed"); 87 } 88 89 private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText) 90 throws Exception { 91 //init cipher streams 92 ByteArrayInputStream baInput = new ByteArrayInputStream(plainText); 93 CipherInputStream ciInput = new CipherInputStream(baInput, encCipher); 94 ByteArrayOutputStream baOutput = new ByteArrayOutputStream(); 95 CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher); 96 97 //do test 98 byte[] buffer = new byte[200]; 99 int len = ciInput.read(buffer); 100 System.out.println("read " + len + " bytes from input buffer"); 101 102 while (len != -1) { 103 ciOutput.write(buffer, 0, len); 104 System.out.println("wite " + len + " bytes to output buffer"); 105 len = ciInput.read(buffer); 106 if (len != -1) { 107 System.out.println("read " + len + " bytes from input buffer"); 108 } else { 109 System.out.println("finished reading"); 110 } 111 } 112 113 ciOutput.flush(); 114 ciInput.close(); 115 ciOutput.close(); 116 117 return baOutput.toByteArray(); 118 } 119 }