Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/Test4517355.java
41161 views
/*1* Copyright (c) 2002, 2015, 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 451735526* @summary Verify that AES cipher.doFinal method does NOT need more27* than necessary bytes in decrypt mode28* @author Valerie Peng29* @key randomness30*/31import java.io.PrintStream;32import java.security.*;33import java.security.spec.*;34import java.util.*;3536import javax.crypto.*;37import javax.crypto.spec.*;38import java.security.Provider;3940public class Test4517355 {4142private static final String ALGO = "AES";43private static final int KEYSIZE = 16; // in bytes4445private static byte[] plainText = new byte[125];4647public void execute(String mode, String padding) throws Exception {48String transformation = ALGO + "/" + mode + "/" + padding;4950Cipher ci = Cipher.getInstance(transformation, "SunJCE");51KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");52kg.init(KEYSIZE*8);53SecretKey key = kg.generateKey();5455// TEST FIX 451735556ci.init(Cipher.ENCRYPT_MODE, key);57byte[] cipherText = ci.doFinal(plainText);5859if (mode.equalsIgnoreCase("GCM")) {60AlgorithmParameters params = ci.getParameters();61ci.init(Cipher.DECRYPT_MODE, key, params);62} else {63byte[] iv = ci.getIV();64AlgorithmParameterSpec aps = new IvParameterSpec(iv);65ci.init(Cipher.DECRYPT_MODE, key, aps);66}67byte[] recoveredText = new byte[plainText.length];68try {69int len = ci.doFinal(cipherText, 0, cipherText.length,70recoveredText);71} catch (ShortBufferException ex) {72throw new Exception("output buffer is the right size!");73}7475// BONUS TESTS76// 1. make sure the recoveredText is the same as the plainText77if (!Arrays.equals(plainText, recoveredText)) {78throw new Exception("encryption/decryption does not work!");79}80// 2. make sure encryption does happen81if (Arrays.equals(plainText, cipherText)) {82throw new Exception("encryption does not work!");83}84// 3. make sure padding is working85if (padding.equalsIgnoreCase("PKCS5Padding")) {86if ((cipherText.length/16)*16 != cipherText.length) {87throw new Exception("padding does not work!");88}89}90System.out.println(transformation + ": Passed");91}9293public static void main (String[] args) throws Exception {94Test4517355 test = new Test4517355();95Random rdm = new Random();96rdm.nextBytes(test.plainText);9798test.execute("CBC", "PKCS5Padding");99test.execute("GCM", "NoPadding");100}101}102103104