Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/AES/TestCICOWithGCM.java
38889 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 801263726* @library ../UTIL27* @build TestUtil28* @run main TestCICOWithGCM29* @summary Test CipherInputStream/OutputStream with AES GCM mode.30* @author Valerie Peng31* @key randomness32*/3334import java.security.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import java.math.*;38import java.io.*;39import com.sun.crypto.provider.*;4041import java.util.*;4243public class TestCICOWithGCM {44public static void main(String[] args) throws Exception {45//init Secret Key46KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");47kg.init(128);48SecretKey key = kg.generateKey();4950//do initialization of the plainText51byte[] plainText = new byte[800];52Random rdm = new Random();53rdm.nextBytes(plainText);5455//init ciphers56Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");57encCipher.init(Cipher.ENCRYPT_MODE, key);58Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");59decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());6061//init cipher streams62ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);63CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);64ByteArrayOutputStream baOutput = new ByteArrayOutputStream();65CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);6667//do test68byte[] buffer = new byte[800];69int len = ciInput.read(buffer);70System.out.println("read " + len + " bytes from input buffer");7172while (len != -1) {73ciOutput.write(buffer, 0, len);74System.out.println("wite " + len + " bytes to output buffer");75len = ciInput.read(buffer);76if (len != -1) {77System.out.println("read " + len + " bytes from input buffer");78} else {79System.out.println("finished reading");80}81}8283ciOutput.flush();84ciInput.close();85ciOutput.close();86byte[] recovered = baOutput.toByteArray();87System.out.println("recovered " + recovered.length + " bytes");88if (!Arrays.equals(plainText, recovered)) {89throw new RuntimeException("diff check failed!");90} else {91System.out.println("diff check passed");92}93}94}959697