1 /*
   2  * Copyright (c) 2002, 2007, 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 4513830
  27  * @summary Verify the output size returned by AES cipher.getOutputSize
  28  *      method in DECRYPT mode does not add extra bytes for padding
  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.Random;
  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 Test4513830 {
  43 
  44     private static final String ALGO = "AES";
  45     private static final String MODE = "ECB";
  46     private static final String PADDING = "PKCS5Padding";
  47     private static final int KEYSIZE = 16; // in bytes
  48     private static final int TEXTLENGTHS[] = {
  49         16, 17, 18, 19, 20, 21, 22, 23 };
  50 
  51     public boolean execute() throws Exception {
  52         Random rdm = new Random();
  53         byte[] plainText=new byte[125];
  54         rdm.nextBytes(plainText);
  55 
  56         Cipher ci = Cipher.getInstance(ALGO+"/"+MODE+"/"+PADDING, "SunJCE");
  57 
  58         // TEST FIX 4513830
  59         KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
  60         kg.init(KEYSIZE*8);
  61         SecretKey key = kg.generateKey();
  62 
  63         ci.init(Cipher.DECRYPT_MODE, key);
  64         int recoveredTextLength = ci.getOutputSize(16);
  65 
  66         if (recoveredTextLength != 16) {
  67             throw new Exception("output size should not increase when decrypting!");
  68         }
  69 
  70         // BONUS TESTS
  71         // 1. call getOutputSize with various lengths and see if
  72         // the returned size is correct.
  73         for (int i=0; i<TEXTLENGTHS.length; i++) {
  74             ci.init(Cipher.ENCRYPT_MODE, key);
  75             int cipherTextLength = ci.getOutputSize(TEXTLENGTHS[i]);
  76             if (cipherTextLength != 32) {
  77                 throw new Exception("output size " + cipherTextLength
  78                                     + " for input size " + TEXTLENGTHS[i]
  79                                     + " when encrypting is wrong!");
  80             }
  81         }
  82 
  83         // passed all tests...hooray!
  84         return true;
  85     }
  86 
  87     public static void main (String[] args) throws Exception {
  88         Security.addProvider(new com.sun.crypto.provider.SunJCE());
  89 
  90         Test4513830 test = new Test4513830();
  91         String testName = test.getClass().getName() + "[" + ALGO +
  92             "/" + MODE + "/" + PADDING + "]";
  93         if (test.execute()) {
  94             System.out.println(testName + ": Passed!");
  95         }
  96     }
  97 }
--- EOF ---