blob: 3a0c6f93bfb353303e7a607019fab93a9e5fdf3c [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 4894159
27 * @summary unit test to test basic functionality of RC2 AlgorithmParameters
28 * implementation
29 * @author Sean Mullan
30 */
31import java.io.ByteArrayOutputStream;
32import java.io.FileOutputStream;
33import java.io.IOException;
34import java.io.StringReader;
35import java.security.AlgorithmParameters;
36import java.util.Arrays;
37import javax.crypto.Cipher;
38import javax.crypto.SecretKey;
39import javax.crypto.spec.RC2ParameterSpec;
40import javax.crypto.spec.SecretKeySpec;
41
42public class RC2AlgorithmParameters {
43
44// much of code copied from test/cipher/rc2arcfour/CipherKAT.java
45
46 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
47
48 public static void main(String[] args) throws Exception {
49
50 byte[] iv_1 = {
51 (byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,
52 (byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,
53 (byte)0x33,(byte)0x33
54 };
55
56 // check that RC2 is supported by our provider
57 AlgorithmParameters rc2Params =
58 AlgorithmParameters.getInstance("RC2", "SunJCE");
59
60 // check that getAlgorithm returns "RC2"
61 if (!rc2Params.getAlgorithm().equals("RC2")) {
62 throw new Exception("getAlgorithm() returned "
63 + rc2Params.getAlgorithm() + " instead of RC2");
64 }
65
66 // test parameters with effective key size and iv
67 byte[] encoded = testParams(rc2Params, new RC2ParameterSpec(2, iv_1));
68
69 // test parameters with just iv
70 encoded = testParams(AlgorithmParameters.getInstance("RC2"),
71 new RC2ParameterSpec(0, iv_1));
72
73 // test vectors in RFC 2268
74 runTests(tests);
75 }
76
77 static void runTests(CipherTest[] tests) throws Exception {
78 for (int i = 0; i < tests.length; i++) {
79 CipherTest test = tests[i];
80 test.run();
81 }
82 System.out.println("All tests passed");
83 }
84
85 private static byte[] testParams(AlgorithmParameters rc2Params,
86 RC2ParameterSpec rc2Spec) throws Exception {
87
88 // test getParameterSpec returns object equal to input
89 rc2Params.init(rc2Spec);
90 RC2ParameterSpec rc2OtherSpec = (RC2ParameterSpec)
91 rc2Params.getParameterSpec(RC2ParameterSpec.class);
92 if (!rc2Spec.equals(rc2OtherSpec)) {
93 throw new Exception("AlgorithmParameterSpecs should be equal");
94 }
95
96 // test RC2ParameterSpec with RC2 Cipher
97 Cipher rc2Cipher = Cipher.getInstance("RC2/CBC/PKCS5PADDING", "SunJCE");
98 rc2Cipher.init(Cipher.ENCRYPT_MODE,
99 new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), rc2Spec);
100
101 // get IV
102 byte[] iv = rc2Cipher.getIV();
103 if (!Arrays.equals(iv, rc2Spec.getIV())) {
104 throw new Exception("ivs should be equal");
105 }
106
107 // test encoding and decoding
108 byte[] encoded = rc2Params.getEncoded();
109 AlgorithmParameters params = AlgorithmParameters.getInstance("RC2");
110 params.init(encoded);
111
112 // test RC2 AlgorithmParameters with RC2 Cipher
113 rc2Cipher.init(Cipher.ENCRYPT_MODE,
114 new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), params);
115
116 // get IV
117 iv = rc2Cipher.getIV();
118 if (!Arrays.equals(iv, rc2Spec.getIV())) {
119 throw new Exception("ivs should be equal");
120 }
121 return encoded;
122 }
123
124 private static void dumpBytes(byte[] encoded, String file)
125 throws Exception {
126
127 FileOutputStream fos = new FileOutputStream(file);
128 fos.write(encoded);
129 fos.close();
130 }
131
132 public static String toString(byte[] b) {
133 if (b == null) {
134 return "(null)";
135 }
136 StringBuffer sb = new StringBuffer(b.length * 3);
137 for (int i = 0; i < b.length; i++) {
138 int k = b[i] & 0xff;
139 if (i != 0) {
140 sb.append(':');
141 }
142 sb.append(hexDigits[k >>> 4]);
143 sb.append(hexDigits[k & 0xf]);
144 }
145 return sb.toString();
146 }
147
148 public static byte[] parse(String s) {
149 try {
150 int n = s.length();
151 ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
152 StringReader r = new StringReader(s);
153 while (true) {
154 int b1 = nextNibble(r);
155 if (b1 < 0) {
156 break;
157 }
158 int b2 = nextNibble(r);
159 if (b2 < 0) {
160 throw new RuntimeException("Invalid string " + s);
161 }
162 int b = (b1 << 4) | b2;
163 out.write(b);
164 }
165 return out.toByteArray();
166 } catch (IOException e) {
167 throw new RuntimeException(e);
168 }
169 }
170
171 public static byte[] b(String s) {
172 return parse(s);
173 }
174
175 private static int nextNibble(StringReader r) throws IOException {
176 while (true) {
177 int ch = r.read();
178 if (ch == -1) {
179 return -1;
180 } else if ((ch >= '0') && (ch <= '9')) {
181 return ch - '0';
182 } else if ((ch >= 'a') && (ch <= 'f')) {
183 return ch - 'a' + 10;
184 } else if ((ch >= 'A') && (ch <= 'F')) {
185 return ch - 'A' + 10;
186 }
187 }
188 }
189
190 static class CipherTest {
191 private final byte[] plaintext;
192 private final byte[] ciphertext;
193 private final byte[] key;
194 private final int effectiveKeySize;
195 CipherTest(String plaintext, String ciphertext,
196 String key, int effectiveKeySize) {
197 this.plaintext = b(plaintext);
198 this.ciphertext = b(ciphertext);
199 this.key = b(key);
200 this.effectiveKeySize = effectiveKeySize;
201 }
202 void run() throws Exception {
203 Cipher cipher = Cipher.getInstance("RC2/ECB/NOPADDING", "SunJCE");
204 SecretKey keySpec = new SecretKeySpec(key, "RC2");
205 RC2ParameterSpec rc2Spec = new RC2ParameterSpec(effectiveKeySize);
206 cipher.init(Cipher.ENCRYPT_MODE, keySpec, rc2Spec);
207 byte[] enc = cipher.doFinal(plaintext);
208 if (Arrays.equals(ciphertext, enc) == false) {
209 System.out.println("RC2AlgorithmParameters Cipher test " +
210 "encryption failed:");
211 System.out.println("plaintext: "
212 + RC2AlgorithmParameters.toString(plaintext));
213 System.out.println("ciphertext: "
214 + RC2AlgorithmParameters.toString(ciphertext));
215 System.out.println("encrypted: "
216 + RC2AlgorithmParameters.toString(enc));
217 System.out.println("key: "
218 + RC2AlgorithmParameters.toString(key));
219 System.out.println("effective key length: "
220 + effectiveKeySize);
221 throw new Exception("RC2AlgorithmParameters Cipher test "
222 + "encryption failed");
223 }
224 enc = cipher.doFinal(plaintext);
225 if (Arrays.equals(ciphertext, enc) == false) {
226 throw new Exception("Re-encryption test failed");
227 }
228 cipher.init(Cipher.DECRYPT_MODE, keySpec, rc2Spec);
229 byte[] dec = cipher.doFinal(ciphertext);
230 if (Arrays.equals(plaintext, dec) == false) {
231 System.out.println("RC2AlgorithmParameters Cipher test "
232 + "decryption failed:");
233 System.out.println("plaintext: "
234 + RC2AlgorithmParameters.toString(plaintext));
235 System.out.println("ciphertext: "
236 + RC2AlgorithmParameters.toString(ciphertext));
237 System.out.println("decrypted: "
238 + RC2AlgorithmParameters.toString(dec));
239 System.out.println("key: "
240 + RC2AlgorithmParameters.toString(key));
241 System.out.println("effective key length: "
242 + effectiveKeySize);
243 throw new Exception("RC2AlgorithmParameters Cipher test "
244 + "decryption failed");
245 }
246 System.out.println("passed");
247 }
248 }
249
250
251 // test vectors listed in RFC 2268
252 private final static CipherTest[] tests = {
253 new CipherTest("00:00:00:00:00:00:00:00", "EB:B7:73:F9:93:27:8E:FF",
254 "00:00:00:00:00:00:00:00", 63),
255 new CipherTest("FF:FF:FF:FF:FF:FF:FF:FF", "27:8B:27:E4:2E:2F:0D:49",
256 "FF:FF:FF:FF:FF:FF:FF:FF", 64),
257 new CipherTest("10:00:00:00:00:00:00:01", "30:64:9E:DF:9B:E7:D2:C2",
258 "30:00:00:00:00:00:00:00", 64),
259// This vector is not tested because it will throw an exception because the
260// key size is too small (less than 40 bits)
261// new CipherTest("00:00:00:00:00:00:00:00", "61:A8:A2:44:AD:AC:CC:F0",
262// "88", 64),
263 new CipherTest("00:00:00:00:00:00:00:00", "6C:CF:43:08:97:4C:26:7F",
264 "88:BC:A9:0E:90:87:5A", 64),
265 new CipherTest("00:00:00:00:00:00:00:00", "1A:80:7D:27:2B:BE:5D:B1",
266 "88:BC:A9:0E:90:87:5A:7F:0F:79:C3:84:62:7B:AF:B2", 64),
267 new CipherTest("00:00:00:00:00:00:00:00", "22:69:55:2A:B0:F8:5C:A6",
268 "88:BC:A9:0E:90:87:5A:7F:0F:79:C3:84:62:7B:AF:B2", 128),
269 };
270}