2
|
1 |
/*
|
|
2 |
* Copyright 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 4898484
|
|
27 |
* @summary basic test for symmetric ciphers with no padding
|
|
28 |
* @author Valerie Peng
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.nio.*;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import java.security.*;
|
|
37 |
import java.security.spec.AlgorithmParameterSpec;
|
|
38 |
|
|
39 |
import javax.crypto.*;
|
|
40 |
import javax.crypto.spec.IvParameterSpec;
|
|
41 |
|
|
42 |
public class TestSymmCiphersNoPad extends PKCS11Test {
|
|
43 |
|
|
44 |
private static class CI { // class for holding Cipher Information
|
|
45 |
String transformation;
|
|
46 |
String keyAlgo;
|
|
47 |
int dataSize;
|
|
48 |
|
|
49 |
CI(String transformation, String keyAlgo, int dataSize) {
|
|
50 |
this.transformation = transformation;
|
|
51 |
this.keyAlgo = keyAlgo;
|
|
52 |
this.dataSize = dataSize;
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
private static final CI TEST_LIST[] = {
|
|
57 |
new CI("ARCFOUR", "ARCFOUR", 400),
|
|
58 |
new CI("RC4", "RC4", 401),
|
|
59 |
new CI("DES/CBC/NoPadding", "DES", 400),
|
|
60 |
new CI("DESede/CBC/NoPadding", "DESede", 160),
|
|
61 |
new CI("AES/CBC/NoPadding", "AES", 4800),
|
|
62 |
new CI("Blowfish/CBC/NoPadding", "Blowfish", 24)
|
|
63 |
};
|
|
64 |
|
|
65 |
private static StringBuffer debugBuf;
|
|
66 |
|
|
67 |
public void main(Provider p) throws Exception {
|
|
68 |
boolean status = true;
|
|
69 |
Random random = new Random();
|
|
70 |
try {
|
|
71 |
for (int i = 0; i < TEST_LIST.length; i++) {
|
|
72 |
CI currTest = TEST_LIST[i];
|
|
73 |
System.out.println("===" + currTest.transformation + "===");
|
|
74 |
try {
|
|
75 |
KeyGenerator kg =
|
|
76 |
KeyGenerator.getInstance(currTest.keyAlgo, p);
|
|
77 |
SecretKey key = kg.generateKey();
|
|
78 |
Cipher c1 = Cipher.getInstance(currTest.transformation, p);
|
|
79 |
Cipher c2 = Cipher.getInstance(currTest.transformation,
|
|
80 |
"SunJCE");
|
|
81 |
|
|
82 |
byte[] plainTxt = new byte[currTest.dataSize];
|
|
83 |
random.nextBytes(plainTxt);
|
|
84 |
System.out.println("Testing inLen = " + plainTxt.length);
|
|
85 |
|
|
86 |
c2.init(Cipher.ENCRYPT_MODE, key);
|
|
87 |
AlgorithmParameters params = c2.getParameters();
|
|
88 |
byte[] answer = c2.doFinal(plainTxt);
|
|
89 |
test(c1, Cipher.ENCRYPT_MODE, key, params,
|
|
90 |
plainTxt, answer);
|
|
91 |
System.out.println("Encryption tests: DONE");
|
|
92 |
c2.init(Cipher.DECRYPT_MODE, key, params);
|
|
93 |
byte[] answer2 = c2.doFinal(answer);
|
|
94 |
test(c1, Cipher.DECRYPT_MODE, key, params,
|
|
95 |
answer, answer2);
|
|
96 |
System.out.println("Decryption tests: DONE");
|
|
97 |
} catch (NoSuchAlgorithmException nsae) {
|
|
98 |
System.out.println("Skipping unsupported algorithm: " +
|
|
99 |
nsae);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
} catch (Exception ex) {
|
|
103 |
// print out debug info when exception is encountered
|
|
104 |
if (debugBuf != null) {
|
|
105 |
System.out.println(debugBuf.toString());
|
|
106 |
}
|
|
107 |
throw ex;
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
private static void test(Cipher cipher, int mode, SecretKey key,
|
|
112 |
AlgorithmParameters params,
|
|
113 |
byte[] in, byte[] answer) throws Exception {
|
|
114 |
// test setup
|
|
115 |
debugBuf = new StringBuffer();
|
|
116 |
cipher.init(mode, key, params);
|
|
117 |
int outLen = cipher.getOutputSize(in.length);
|
|
118 |
debugBuf.append("Estimated output size = " + outLen + "\n");
|
|
119 |
|
|
120 |
// test data preparation
|
|
121 |
ByteBuffer inBuf = ByteBuffer.allocate(in.length);
|
|
122 |
inBuf.put(in);
|
|
123 |
inBuf.position(0);
|
|
124 |
ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);
|
|
125 |
inDirectBuf.put(in);
|
|
126 |
inDirectBuf.position(0);
|
|
127 |
ByteBuffer outBuf = ByteBuffer.allocate(outLen);
|
|
128 |
ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);
|
|
129 |
|
|
130 |
// test#1: byte[] in + byte[] out
|
|
131 |
debugBuf.append("Test#1:\n");
|
|
132 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
133 |
byte[] testOut1 = cipher.update(in, 0, 16);
|
|
134 |
if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);
|
|
135 |
testOut1 = cipher.doFinal(in, 16, in.length-16);
|
|
136 |
if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);
|
|
137 |
testOut1 = baos.toByteArray();
|
|
138 |
match(testOut1, answer);
|
|
139 |
|
|
140 |
// test#2: Non-direct Buffer in + non-direct Buffer out
|
|
141 |
debugBuf.append("Test#2:\n");
|
|
142 |
debugBuf.append("inputBuf: " + inBuf + "\n");
|
|
143 |
debugBuf.append("outputBuf: " + outBuf + "\n");
|
|
144 |
cipher.update(inBuf, outBuf);
|
|
145 |
cipher.doFinal(inBuf, outBuf);
|
|
146 |
match(outBuf, answer);
|
|
147 |
|
|
148 |
// test#3: Direct Buffer in + direc Buffer out
|
|
149 |
debugBuf.append("Test#3:\n");
|
|
150 |
debugBuf.append("(pre) inputBuf: " + inDirectBuf + "\n");
|
|
151 |
debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");
|
|
152 |
cipher.update(inDirectBuf, outDirectBuf);
|
|
153 |
cipher.doFinal(inDirectBuf, outDirectBuf);
|
|
154 |
|
|
155 |
debugBuf.append("(post) inputBuf: " + inDirectBuf + "\n");
|
|
156 |
debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");
|
|
157 |
match(outDirectBuf, answer);
|
|
158 |
|
|
159 |
// test#4: Direct Buffer in + non-direct Buffer out
|
|
160 |
debugBuf.append("Test#4:\n");
|
|
161 |
inDirectBuf.position(0);
|
|
162 |
outBuf.position(0);
|
|
163 |
debugBuf.append("inputBuf: " + inDirectBuf + "\n");
|
|
164 |
debugBuf.append("outputBuf: " + outBuf + "\n");
|
|
165 |
cipher.update(inDirectBuf, outBuf);
|
|
166 |
cipher.doFinal(inDirectBuf, outBuf);
|
|
167 |
match(outBuf, answer);
|
|
168 |
|
|
169 |
// test#5: Non-direct Buffer in + direct Buffer out
|
|
170 |
debugBuf.append("Test#5:\n");
|
|
171 |
inBuf.position(0);
|
|
172 |
outDirectBuf.position(0);
|
|
173 |
|
|
174 |
debugBuf.append("(pre) inputBuf: " + inBuf + "\n");
|
|
175 |
debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");
|
|
176 |
|
|
177 |
cipher.update(inBuf, outDirectBuf);
|
|
178 |
cipher.doFinal(inBuf, outDirectBuf);
|
|
179 |
|
|
180 |
debugBuf.append("(post) inputBuf: " + inBuf + "\n");
|
|
181 |
debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");
|
|
182 |
match(outDirectBuf, answer);
|
|
183 |
|
|
184 |
debugBuf = null;
|
|
185 |
}
|
|
186 |
|
|
187 |
private static void match(byte[] b1, byte[] b2) throws Exception {
|
|
188 |
if (b1.length != b2.length) {
|
|
189 |
debugBuf.append("got len : " + b1.length + "\n");
|
|
190 |
debugBuf.append("expect len: " + b2.length + "\n");
|
|
191 |
throw new Exception("mismatch - different length!\n");
|
|
192 |
} else {
|
|
193 |
for (int i = 0; i < b1.length; i++) {
|
|
194 |
if (b1[i] != b2[i]) {
|
|
195 |
debugBuf.append("got : " + toString(b1) + "\n");
|
|
196 |
debugBuf.append("expect: " + toString(b2) + "\n");
|
|
197 |
throw new Exception("mismatch");
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
private static void match(ByteBuffer bb, byte[] answer) throws Exception {
|
|
204 |
byte[] bbTemp = new byte[bb.position()];
|
|
205 |
bb.position(0);
|
|
206 |
bb.get(bbTemp, 0, bbTemp.length);
|
|
207 |
match(bbTemp, answer);
|
|
208 |
}
|
|
209 |
|
|
210 |
public static void main(String[] args) throws Exception {
|
|
211 |
main(new TestSymmCiphersNoPad());
|
|
212 |
}
|
|
213 |
}
|