1 /* 2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4517355 27 * @summary Verify that AES cipher.doFinal method does NOT need more 28 * than necessary bytes in decrypt mode 29 * @author Valerie Peng 30 * @key randomness 31 */ 32 import java.io.PrintStream; 33 import java.security.*; 34 import java.security.spec.*; 35 import java.util.*; 36 37 import javax.crypto.*; 38 import javax.crypto.spec.*; 39 import java.security.Provider; 40 import com.sun.crypto.provider.*; 41 42 public class Test4517355 { 43 44 private static final String ALGO = "AES"; 45 private static final int KEYSIZE = 16; // in bytes 46 47 private static byte[] plainText = new byte[125]; 48 49 public void execute(String mode, String padding) throws Exception { 50 String transformation = ALGO + "/" + mode + "/" + padding; 51 52 Cipher ci = Cipher.getInstance(transformation, "SunJCE"); 53 KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE"); 54 kg.init(KEYSIZE*8); 55 SecretKey key = kg.generateKey(); 56 57 // TEST FIX 4517355 58 ci.init(Cipher.ENCRYPT_MODE, key); 59 byte[] cipherText = ci.doFinal(plainText); 60 61 if (mode.equalsIgnoreCase("GCM")) { 62 AlgorithmParameters params = ci.getParameters(); 63 ci.init(Cipher.DECRYPT_MODE, key, params); 64 } else { 65 byte[] iv = ci.getIV(); 66 AlgorithmParameterSpec aps = new IvParameterSpec(iv); 67 ci.init(Cipher.DECRYPT_MODE, key, aps); 68 } 69 byte[] recoveredText = new byte[plainText.length]; 70 try { 71 int len = ci.doFinal(cipherText, 0, cipherText.length, 72 recoveredText); 73 } catch (ShortBufferException ex) { 74 throw new Exception("output buffer is the right size!"); 75 } 76 77 // BONUS TESTS 78 // 1. make sure the recoveredText is the same as the plainText 79 if (!Arrays.equals(plainText, recoveredText)) { 80 throw new Exception("encryption/decryption does not work!"); 81 } 82 // 2. make sure encryption does happen 83 if (Arrays.equals(plainText, cipherText)) { 84 throw new Exception("encryption does not work!"); 85 } 86 // 3. make sure padding is working 87 if (padding.equalsIgnoreCase("PKCS5Padding")) { 88 if ((cipherText.length/16)*16 != cipherText.length) { 89 throw new Exception("padding does not work!"); 90 } 91 } 92 System.out.println(transformation + ": Passed"); 93 } 94 95 public static void main (String[] args) throws Exception { 96 Security.addProvider(new com.sun.crypto.provider.SunJCE()); 97 98 Test4517355 test = new Test4517355(); 99 Random rdm = new Random(); 100 rdm.nextBytes(test.plainText); 101 102 test.execute("CBC", "PKCS5Padding"); 103 test.execute("GCM", "NoPadding"); 104 } 105 }