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