blob: ff2faa357f80e24cb67f789aec31c12fe1fd0fd3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 */
31import java.io.PrintStream;
32import java.security.*;
33import java.security.spec.*;
34import java.util.Random;
35
36import javax.crypto.*;
37import javax.crypto.spec.*;
38import java.security.Provider;
39import com.sun.crypto.provider.*;
40
41public class Test4513830 {
42
43 private static final String ALGO = "AES";
44 private static final String MODE = "ECB";
45 private static final String PADDING = "PKCS5Padding";
46 private static final int KEYSIZE = 16; // in bytes
47 private static final int TEXTLENGTHS[] = {
48 16, 17, 18, 19, 20, 21, 22, 23 };
49
50 public boolean execute() throws Exception {
51 Random rdm = new Random();
52 byte[] plainText=new byte[125];
53 rdm.nextBytes(plainText);
54
55 Cipher ci = Cipher.getInstance(ALGO+"/"+MODE+"/"+PADDING, "SunJCE");
56
57 // TEST FIX 4513830
58 KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
59 kg.init(KEYSIZE*8);
60 SecretKey key = kg.generateKey();
61
62 ci.init(Cipher.DECRYPT_MODE, key);
63 int recoveredTextLength = ci.getOutputSize(16);
64
65 if (recoveredTextLength != 16) {
66 throw new Exception("output size should not increase when decrypting!");
67 }
68
69 // BONUS TESTS
70 // 1. call getOutputSize with various lengths and see if
71 // the returned size is correct.
72 for (int i=0; i<TEXTLENGTHS.length; i++) {
73 ci.init(Cipher.ENCRYPT_MODE, key);
74 int cipherTextLength = ci.getOutputSize(TEXTLENGTHS[i]);
75 if (cipherTextLength != 32) {
76 throw new Exception("output size " + cipherTextLength
77 + " for input size " + TEXTLENGTHS[i]
78 + " when encrypting is wrong!");
79 }
80 }
81
82 // passed all tests...hooray!
83 return true;
84 }
85
86 public static void main (String[] args) throws Exception {
87 Security.addProvider(new com.sun.crypto.provider.SunJCE());
88
89 Test4513830 test = new Test4513830();
90 String testName = test.getClass().getName() + "[" + ALGO +
91 "/" + MODE + "/" + PADDING + "]";
92 if (test.execute()) {
93 System.out.println(testName + ": Passed!");
94 }
95 }
96}