Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RC2ArcFour/CipherKAT.java
41161 views
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 4890078
27
* @summary Basic known-answer-test for RC2 and ARCFOUR
28
* @author Andreas Sterbenz
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
34
import java.security.*;
35
36
import javax.crypto.*;
37
import javax.crypto.spec.*;
38
39
public class CipherKAT {
40
41
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
42
43
public static String toString(byte[] b) {
44
if (b == null) {
45
return "(null)";
46
}
47
StringBuffer sb = new StringBuffer(b.length * 3);
48
for (int i = 0; i < b.length; i++) {
49
int k = b[i] & 0xff;
50
if (i != 0) {
51
sb.append(':');
52
}
53
sb.append(hexDigits[k >>> 4]);
54
sb.append(hexDigits[k & 0xf]);
55
}
56
return sb.toString();
57
}
58
59
public static byte[] parse(String s) {
60
try {
61
int n = s.length();
62
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
63
StringReader r = new StringReader(s);
64
while (true) {
65
int b1 = nextNibble(r);
66
if (b1 < 0) {
67
break;
68
}
69
int b2 = nextNibble(r);
70
if (b2 < 0) {
71
throw new RuntimeException("Invalid string " + s);
72
}
73
int b = (b1 << 4) | b2;
74
out.write(b);
75
}
76
return out.toByteArray();
77
} catch (IOException e) {
78
throw new RuntimeException(e);
79
}
80
}
81
82
public static byte[] b(String s) {
83
return parse(s);
84
}
85
86
private static int nextNibble(StringReader r) throws IOException {
87
while (true) {
88
int ch = r.read();
89
if (ch == -1) {
90
return -1;
91
} else if ((ch >= '0') && (ch <= '9')) {
92
return ch - '0';
93
} else if ((ch >= 'a') && (ch <= 'f')) {
94
return ch - 'a' + 10;
95
} else if ((ch >= 'A') && (ch <= 'F')) {
96
return ch - 'A' + 10;
97
}
98
}
99
}
100
101
static abstract class Test {
102
abstract void run(Provider p) throws Exception;
103
}
104
105
static class CipherTest extends Test {
106
private final String alg;
107
private final byte[] plaintext;
108
private final byte[] ciphertext;
109
private final byte[] key;
110
private final byte[] iv;
111
CipherTest(String alg, byte[] plaintext, byte[] ciphertext, byte[] key, byte[] iv) {
112
this.alg = alg;
113
this.plaintext = plaintext;
114
this.ciphertext = ciphertext;
115
this.key = key;
116
this.iv = iv;
117
}
118
void run(Provider p) throws Exception {
119
Cipher cipher = Cipher.getInstance(alg, p);
120
SecretKey keySpec = new SecretKeySpec(key, alg.split("/")[0]);
121
IvParameterSpec ivSpec;
122
if (iv == null) {
123
ivSpec = null;
124
} else {
125
ivSpec = new IvParameterSpec(iv);
126
}
127
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
128
byte[] enc = cipher.doFinal(plaintext);
129
if (Arrays.equals(ciphertext, enc) == false) {
130
System.out.println("Cipher test encryption for " + alg + " failed:");
131
if (plaintext.length < 256) {
132
System.out.println("plaintext: " + CipherKAT.toString(plaintext));
133
System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));
134
System.out.println("encrypted: " + CipherKAT.toString(enc));
135
}
136
System.out.println("key: " + CipherKAT.toString(key));
137
System.out.println("iv: " + CipherKAT.toString(iv));
138
throw new Exception("Cipher test encryption for " + alg + " failed");
139
}
140
enc = cipher.doFinal(plaintext);
141
if (Arrays.equals(ciphertext, enc) == false) {
142
throw new Exception("Re-encryption test failed");
143
}
144
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
145
byte[] dec = cipher.doFinal(ciphertext);
146
if (Arrays.equals(plaintext, dec) == false) {
147
System.out.println("Cipher test decryption for " + alg + " failed:");
148
if (plaintext.length < 256) {
149
System.out.println("plaintext: " + CipherKAT.toString(plaintext));
150
System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));
151
System.out.println("decrypted: " + CipherKAT.toString(dec));
152
}
153
System.out.println("key: " + CipherKAT.toString(key));
154
System.out.println("iv: " + CipherKAT.toString(iv));
155
throw new Exception("Cipher test decryption for " + alg + " failed");
156
}
157
System.out.println("passed: " + alg);
158
}
159
}
160
161
private static byte[] s(String s) {
162
try {
163
return s.getBytes("UTF8");
164
} catch (Exception e) {
165
throw new RuntimeException(e);
166
}
167
}
168
169
private static Test t(String alg, String plaintext, String ciphertext, String key) {
170
return new CipherTest(alg, b(plaintext), b(ciphertext), b(key), null);
171
}
172
173
private final static byte[] ALONG;
174
175
static {
176
ALONG = new byte[1024 * 128];
177
Arrays.fill(ALONG, (byte)'a');
178
}
179
180
private final static Test[] tests = {
181
t("RC4", "00", "74", "0123456789abcdef"),
182
t("RC4", "000102030405060708090a0b0c0d0e0f", "74:95:c0:e4:14:4e:0e:7e:05:42:df:58:3e:82:10:f3", "0123456789abcdef"),
183
t("RC4", "000102030405060708090a0b0c0d0e0f", "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a", "0123456789"), // 40 bit
184
t("RC4", "000102030405060708090a0b0c0d0e0f", "ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24", "0123456789abcdeffedcba9876543210"), // 128 bit
185
t("RC4", "74:95:c0:e4:14:4e:0e:7e:05:42:df:58:3e:82:10:f3 1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24 ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24",
186
"e1:15:95:fc:79:da:5e:a4:e4:2a:a8:ce:cd:7d:1e:f3:f7:ef:19:a5:05:70:9f:f7:59:49:44:d6:fb:5a:b6:54:04:4d:f8:e6:60:a3:96:7d:40:33:09:78:f2:2e:de:25:fe:ad:54:dd:b9:65:97:d6:d4:4c:a8:a6:f2:2c:13:61",
187
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
188
t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "81:07:71:4F:0D:81:88:A7",
189
"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),
190
t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "39:88:F7:B8:6C:14:D3:F8",
191
"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01"),
192
t("RC2/ECB/NoPadding", "FF:FF:FF:FF:FF:FF:FF:FF", "BA:B8:12:73:3E:2E:B7:EE",
193
"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),
194
t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "9C:4B:FE:6D:FE:73:9C:2B",
195
"00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F"),
196
t("RC2/ECB/NoPadding", "9C:4B:FE:6D:FE:73:9C:2B", "1d:b2:cb:09:7f:e8:ab:43",
197
"39:88:F7:B8:6C:14:D3:F8:BA:B8:12:73:3E:2E:B7:EE"),
198
t("RC2/ECB/NoPadding", "BA:B8:12:73:3E:2E:B7:EE", "14:e5:5b:62:ca:f5:16:24",
199
"1d:b2:cb:09:7f:e8:ab:43"),
200
};
201
202
static void runTests(Test[] tests) throws Exception {
203
long start = System.currentTimeMillis();
204
Provider p = Security.getProvider("SunJCE");
205
System.out.println("Testing provider " + p.getName() + "...");
206
Cipher.getInstance("RC2", p);
207
Cipher.getInstance("RC4", p);
208
Cipher.getInstance("ARCFOUR", p);
209
KeyGenerator.getInstance("RC2", p);
210
KeyGenerator.getInstance("RC4", p);
211
KeyGenerator.getInstance("ARCFOUR", p);
212
for (int i = 0; i < tests.length; i++) {
213
Test test = tests[i];
214
test.run(p);
215
}
216
System.out.println("All tests passed");
217
long stop = System.currentTimeMillis();
218
System.out.println("Done (" + (stop - start) + " ms).");
219
}
220
221
public static void main(String[] args) throws Exception {
222
runTests(tests);
223
}
224
225
}
226
227