Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java
41152 views
/*1* Copyright (c) 2018, 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 808046226* @library /test/lib ..27* @modules jdk.crypto.cryptoki28* @run main TestCICOWithGCM29* @summary Test CipherInputStream/OutputStream with AES GCM mode.30* @key randomness31*/3233import java.security.*;34import javax.crypto.*;35import javax.crypto.spec.*;36import java.math.*;37import java.io.*;3839import java.util.*;4041public class TestCICOWithGCM extends PKCS11Test {42public static void main(String[] args) throws Exception {43main(new TestCICOWithGCM(), args);44}4546@Override47public void main(Provider p) throws Exception {48test("GCM", p);49}5051public void test(String mode, Provider p) throws Exception {52Cipher c;53try {54String transformation = "AES/" + mode + "/NoPadding";55c = Cipher.getInstance(transformation, p);56} catch (GeneralSecurityException e) {57System.out.println("Skip testing " + p.getName() +58", no support for " + mode);59return;60}6162SecretKey key = new SecretKeySpec(new byte[16], "AES");6364//do initialization of the plainText65byte[] plainText = new byte[800];66Random rdm = new Random();67rdm.nextBytes(plainText);6869//init ciphers70Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", p);71encCipher.init(Cipher.ENCRYPT_MODE, key);72Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", p);73decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());7475//init cipher streams76ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);77CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);78ByteArrayOutputStream baOutput = new ByteArrayOutputStream();79CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);8081//do test82byte[] buffer = new byte[800];83int len = ciInput.read(buffer);84System.out.println("read " + len + " bytes from input buffer");8586while (len != -1) {87ciOutput.write(buffer, 0, len);88System.out.println("wite " + len + " bytes to output buffer");89len = ciInput.read(buffer);90if (len != -1) {91System.out.println("read " + len + " bytes from input buffer");92} else {93System.out.println("finished reading");94}95}9697ciOutput.flush();98ciInput.close();99ciOutput.close();100byte[] recovered = baOutput.toByteArray();101System.out.println("recovered " + recovered.length + " bytes");102if (!Arrays.equals(plainText, recovered)) {103throw new RuntimeException("diff check failed!");104} else {105System.out.println("diff check passed");106}107}108}109110111