Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/AES/Test4517355.java
38889 views
/*1* Copyright (c) 2002, 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 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;39import com.sun.crypto.provider.*;4041public class Test4517355 {4243private static final String ALGO = "AES";44private static final int KEYSIZE = 16; // in bytes4546private static byte[] plainText = new byte[125];4748public void execute(String mode, String padding) throws Exception {49String transformation = ALGO + "/" + mode + "/" + padding;5051Cipher ci = Cipher.getInstance(transformation, "SunJCE");52KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");53kg.init(KEYSIZE*8);54SecretKey key = kg.generateKey();5556// TEST FIX 451735557ci.init(Cipher.ENCRYPT_MODE, key);58byte[] cipherText = ci.doFinal(plainText);5960if (mode.equalsIgnoreCase("GCM")) {61AlgorithmParameters params = ci.getParameters();62ci.init(Cipher.DECRYPT_MODE, key, params);63} else {64byte[] iv = ci.getIV();65AlgorithmParameterSpec aps = new IvParameterSpec(iv);66ci.init(Cipher.DECRYPT_MODE, key, aps);67}68byte[] recoveredText = new byte[plainText.length];69try {70int len = ci.doFinal(cipherText, 0, cipherText.length,71recoveredText);72} catch (ShortBufferException ex) {73throw new Exception("output buffer is the right size!");74}7576// BONUS TESTS77// 1. make sure the recoveredText is the same as the plainText78if (!Arrays.equals(plainText, recoveredText)) {79throw new Exception("encryption/decryption does not work!");80}81// 2. make sure encryption does happen82if (Arrays.equals(plainText, cipherText)) {83throw new Exception("encryption does not work!");84}85// 3. make sure padding is working86if (padding.equalsIgnoreCase("PKCS5Padding")) {87if ((cipherText.length/16)*16 != cipherText.length) {88throw new Exception("padding does not work!");89}90}91System.out.println(transformation + ": Passed");92}9394public static void main (String[] args) throws Exception {95Security.addProvider(new com.sun.crypto.provider.SunJCE());9697Test4517355 test = new Test4517355();98Random rdm = new Random();99rdm.nextBytes(test.plainText);100101test.execute("CBC", "PKCS5Padding");102test.execute("GCM", "NoPadding");103}104}105106107