Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestCICOWithGCMAndAAD.java
41161 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 801290026* @library ../UTIL27* @build TestUtil28* @run main TestCICOWithGCMAndAAD29* @summary Test CipherInputStream/OutputStream with AES GCM mode with AAD.30* @author Valerie Peng31* @key randomness32*/33import java.io.*;34import java.security.*;35import java.util.*;36import javax.crypto.*;3738public class TestCICOWithGCMAndAAD {39public static void main(String[] args) throws Exception {40//init Secret Key41KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");42kg.init(128);43SecretKey key = kg.generateKey();4445//Do initialization of the plainText46byte[] plainText = new byte[700];47Random rdm = new Random();48rdm.nextBytes(plainText);4950byte[] aad = new byte[128];51rdm.nextBytes(aad);52byte[] aad2 = aad.clone();53aad2[50]++;5455Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");56encCipher.init(Cipher.ENCRYPT_MODE, key);57encCipher.updateAAD(aad);58Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");59decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());60decCipher.updateAAD(aad);6162byte[] recovered = test(encCipher, decCipher, plainText);63if (!Arrays.equals(plainText, recovered)) {64throw new Exception("sameAAD: diff check failed!");65} else System.out.println("sameAAD: passed");6667encCipher.init(Cipher.ENCRYPT_MODE, key);68encCipher.updateAAD(aad2);69recovered = test(encCipher, decCipher, plainText);70if (recovered != null && recovered.length != 0) {71throw new Exception("diffAAD: no data should be returned!");72} else System.out.println("diffAAD: passed");73}7475private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText)76throws Exception {77//init cipher streams78ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);79CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);80ByteArrayOutputStream baOutput = new ByteArrayOutputStream();81CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);8283//do test84byte[] buffer = new byte[200];85int len = ciInput.read(buffer);86System.out.println("read " + len + " bytes from input buffer");8788while (len != -1) {89ciOutput.write(buffer, 0, len);90System.out.println("wite " + len + " bytes to output buffer");91len = ciInput.read(buffer);92if (len != -1) {93System.out.println("read " + len + " bytes from input buffer");94} else {95System.out.println("finished reading");96}97}9899ciOutput.flush();100ciInput.close();101ciOutput.close();102103return baOutput.toByteArray();104}105}106107108