1/*
2 * Copyright (c) 2002, 2015, 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 */
32import java.io.PrintStream;
33import java.security.*;
34import java.security.spec.*;
35import java.util.*;
36
37import javax.crypto.*;
38import javax.crypto.spec.*;
39import java.security.Provider;
40
41public class Test4517355 {
42
43    private static final String ALGO = "AES";
44    private static final int KEYSIZE = 16; // in bytes
45
46    private static byte[] plainText = new byte[125];
47
48    public void execute(String mode, String padding) throws Exception {
49        String transformation = ALGO + "/" + mode + "/" + padding;
50
51        Cipher ci = Cipher.getInstance(transformation, "SunJCE");
52        KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
53        kg.init(KEYSIZE*8);
54        SecretKey key = kg.generateKey();
55
56        // TEST FIX 4517355
57        ci.init(Cipher.ENCRYPT_MODE, key);
58        byte[] cipherText = ci.doFinal(plainText);
59
60        if (mode.equalsIgnoreCase("GCM")) {
61            AlgorithmParameters params = ci.getParameters();
62            ci.init(Cipher.DECRYPT_MODE, key, params);
63        } else {
64            byte[] iv = ci.getIV();
65            AlgorithmParameterSpec aps = new IvParameterSpec(iv);
66            ci.init(Cipher.DECRYPT_MODE, key, aps);
67        }
68        byte[] recoveredText = new byte[plainText.length];
69        try {
70            int len = ci.doFinal(cipherText, 0, cipherText.length,
71                                 recoveredText);
72        } catch (ShortBufferException ex) {
73            throw new Exception("output buffer is the right size!");
74        }
75
76        // BONUS TESTS
77        // 1. make sure the recoveredText is the same as the plainText
78        if (!Arrays.equals(plainText, recoveredText)) {
79            throw new Exception("encryption/decryption does not work!");
80        }
81        // 2. make sure encryption does happen
82        if (Arrays.equals(plainText, cipherText)) {
83            throw new Exception("encryption does not work!");
84        }
85        // 3. make sure padding is working
86        if (padding.equalsIgnoreCase("PKCS5Padding")) {
87            if ((cipherText.length/16)*16 != cipherText.length) {
88                throw new Exception("padding does not work!");
89            }
90        }
91        System.out.println(transformation + ": Passed");
92    }
93
94    public static void main (String[] args) throws Exception {
95        Test4517355 test = new Test4517355();
96        Random rdm = new Random();
97        rdm.nextBytes(test.plainText);
98
99        test.execute("CBC", "PKCS5Padding");
100        test.execute("GCM", "NoPadding");
101    }
102}
103