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