Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCMAndAAD.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 TestCICOWithGCMAndAAD29* @summary Test CipherInputStream/OutputStream with AES GCM mode with AAD.30* @key randomness31*/32import java.io.*;33import java.security.*;34import java.util.*;35import javax.crypto.*;36import javax.crypto.spec.*;3738public class TestCICOWithGCMAndAAD extends PKCS11Test {39public static void main(String[] args) throws Exception {40main(new TestCICOWithGCMAndAAD(), args);41}4243@Override44public void main(Provider p) throws Exception {45test("GCM", p);46// test("CCM", p);47}4849public void test(String mode, Provider p) throws Exception {50Cipher c;51try {52String transformation = "AES/" + mode + "/NoPadding";53c = Cipher.getInstance(transformation, p);54} catch (GeneralSecurityException e) {55System.out.println("Skip testing " + p.getName() +56", no support for " + mode);57return;58}59SecretKey key = new SecretKeySpec(new byte[16], "AES");6061//Do initialization of the plainText62byte[] plainText = new byte[700];63Random rdm = new Random();64rdm.nextBytes(plainText);6566byte[] aad = new byte[128];67rdm.nextBytes(aad);68byte[] aad2 = aad.clone();69aad2[50]++;7071Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", p);72encCipher.init(Cipher.ENCRYPT_MODE, key);73encCipher.updateAAD(aad);74Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", p);75decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());76decCipher.updateAAD(aad);7778byte[] recovered = test(encCipher, decCipher, plainText);79if (!Arrays.equals(plainText, recovered)) {80throw new Exception("sameAAD: diff check failed!");81} else System.out.println("sameAAD: passed");8283encCipher.init(Cipher.ENCRYPT_MODE, key);84encCipher.updateAAD(aad2);85recovered = test(encCipher, decCipher, plainText);86if (recovered != null && recovered.length != 0) {87throw new Exception("diffAAD: no data should be returned!");88} else System.out.println("diffAAD: passed");89}9091private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText)92throws Exception {93//init cipher streams94ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);95CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);96ByteArrayOutputStream baOutput = new ByteArrayOutputStream();97CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);9899//do test100byte[] buffer = new byte[200];101int len = ciInput.read(buffer);102System.out.println("read " + len + " bytes from input buffer");103104while (len != -1) {105ciOutput.write(buffer, 0, len);106System.out.println("wite " + len + " bytes to output buffer");107len = ciInput.read(buffer);108if (len != -1) {109System.out.println("read " + len + " bytes from input buffer");110} else {111System.out.println("finished reading");112}113}114115ciOutput.flush();116ciInput.close();117ciOutput.close();118119return baOutput.toByteArray();120}121}122123124