1 /* 2 * Copyright (c) 2003, 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 4921443 27 * @summary Ensure ISO10126Padding works correctly. 28 * @author Valerie Peng 29 * @key randomness 30 */ 31 import java.util.Arrays; 32 import java.security.*; 33 import java.security.spec.*; 34 35 import javax.crypto.*; 36 import javax.crypto.spec.*; 37 import java.security.Provider; 38 import com.sun.crypto.provider.*; 39 40 public class TestISO10126Padding { 41 private static final String ALGO = "AES"; 42 private static final String TRANS = "AES/ECB"; 43 private static final int KEYSIZE = 16; // in bytes 44 45 private SecretKey key; 46 47 private TestISO10126Padding() throws Exception { 48 // setup 49 KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE"); 50 kg.init(KEYSIZE*8); 51 key = kg.generateKey(); 52 } 53 54 private void runTest(int dataLength) throws Exception { 55 // setup 56 byte[] data = new byte[dataLength]; 57 new SecureRandom().nextBytes(data); 58 System.out.println("Testing data length: " + dataLength); 59 60 // TEST#1 -- 61 // generate the cipher text using manually-supplied 62 // XML Encryption padding 63 Cipher ci = Cipher.getInstance(TRANS + "/NoPadding", "SunJCE"); 64 ci.init(Cipher.ENCRYPT_MODE, key); 65 byte[] paddedData = new byte[ci.getBlockSize()]; 66 System.arraycopy(data, 0, paddedData, 0, data.length); 67 int padValue = paddedData.length - data.length; 68 paddedData[paddedData.length-1] = (byte) padValue; 69 byte[] cipherText = ci.doFinal(paddedData); 70 71 // decrypt using ISO10126Padding 72 ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE"); 73 ci.init(Cipher.DECRYPT_MODE, key); 74 byte[] recovered = ci.doFinal(cipherText); 75 if (!Arrays.equals(data, recovered)) { 76 throw new Exception("TEST#1: decryption failed"); 77 } 78 // TEST#2 -- 79 // generate the cipher text using ISO10126Padding 80 ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE"); 81 ci.init(Cipher.ENCRYPT_MODE, key); 82 cipherText = ci.doFinal(data); 83 84 // decrypt using ISO10126Padding 85 ci.init(Cipher.DECRYPT_MODE, key); 86 recovered = ci.doFinal(cipherText); 87 if (!Arrays.equals(data, recovered)) { 88 throw new Exception("TEST#2: decryption failed"); 89 } 90 } 91 92 public static void main(String[] argv) throws Exception { 93 TestISO10126Padding test = new TestISO10126Padding(); 94 for (int i = 0; i<16; i++) { 95 test.runTest(i); 96 } 97 System.out.println("Test Passed"); 98 } 99 }