Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PKCS12CipherKAT.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 4893959
27
* @summary basic test for PBEWithSHA1AndDESede and
28
* PBEWithSHA1AndRC2_40
29
* @author Valerie Peng
30
*/
31
32
import java.io.*;
33
import java.util.*;
34
35
import java.security.*;
36
37
import javax.crypto.*;
38
import javax.crypto.spec.*;
39
40
public class PKCS12CipherKAT {
41
42
private final static String INPUT = "12:34:56:78:90:ab:cd:ef:ab:cd:ef:12:34:56:78:90:fe:db:ca:09:87:65";
43
private final static String SALT = "7d:60:43:5f:02:e9:e0:ae";
44
private final static int ITER_COUNT = 2048;
45
46
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
47
48
public static byte[] parse(String s) {
49
try {
50
int n = s.length();
51
ByteArrayOutputStream out = new ByteArrayOutputStream(n/3);
52
StringReader r = new StringReader(s);
53
while (true) {
54
int b1 = nextNibble(r);
55
if (b1 < 0) {
56
break;
57
}
58
int b2 = nextNibble(r);
59
if (b2 < 0) {
60
throw new RuntimeException("Invalid string " + s);
61
}
62
int b = (b1 << 4) | b2;
63
out.write(b);
64
}
65
return out.toByteArray();
66
} catch (IOException e) {
67
throw new RuntimeException(e);
68
}
69
}
70
71
public static byte[] b(String s) {
72
return parse(s);
73
}
74
75
private static int nextNibble(StringReader r) throws IOException {
76
while (true) {
77
int ch = r.read();
78
if (ch == -1) {
79
return -1;
80
} else if ((ch >= '0') && (ch <= '9')) {
81
return ch - '0';
82
} else if ((ch >= 'a') && (ch <= 'f')) {
83
return ch - 'a' + 10;
84
} else if ((ch >= 'A') && (ch <= 'F')) {
85
return ch - 'A' + 10;
86
}
87
}
88
}
89
90
static abstract class Test {
91
abstract void run(Provider p) throws Exception;
92
}
93
94
static class CipherTest extends Test {
95
private final String alg;
96
private final byte[] plaintext;
97
private final byte[] ciphertext;
98
private final char[] password;
99
private final byte[] salt;
100
private final int ic;
101
102
CipherTest(String alg, byte[] plaintext, byte[] ciphertext,
103
char[] password, byte[] salt, int ic) {
104
this.alg = alg;
105
this.plaintext = plaintext;
106
this.ciphertext = ciphertext;
107
this.password = password;
108
this.salt = salt;
109
this.ic = ic;
110
}
111
112
String hexDump(byte[] b) {
113
if (b == null) {
114
return "(null)";
115
}
116
StringBuffer sb = new StringBuffer(b.length * 3);
117
for (int i = 0; i < b.length; i++) {
118
int k = b[i] & 0xff;
119
if (i != 0) {
120
sb.append(':');
121
}
122
sb.append(hexDigits[k >>> 4]);
123
sb.append(hexDigits[k & 0xf]);
124
}
125
return sb.toString();
126
}
127
128
void run(Provider p) throws Exception {
129
Cipher cipher = Cipher.getInstance(alg, p);
130
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
131
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
132
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ic);
133
SecretKey key = keyFac.generateSecret(pbeKeySpec);
134
cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
135
byte[] enc = cipher.doFinal(plaintext);
136
if (Arrays.equals(ciphertext, enc) == false) {
137
System.out.println(
138
"Cipher test encryption for " + alg + " failed:");
139
System.out.println("plaintext: " + hexDump(plaintext));
140
System.out.println("ciphertext: " + hexDump(ciphertext));
141
System.out.println("encrypted: " + hexDump(enc));
142
System.out.println("password: " + password);
143
System.out.println("salt: " + hexDump(salt));
144
System.out.println("iterationCount: " + ic);
145
throw new Exception("encryption test for " + alg + " failed");
146
}
147
enc = cipher.doFinal(plaintext);
148
if (Arrays.equals(ciphertext, enc) == false) {
149
throw new Exception("Re-encryption test failed");
150
}
151
cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
152
byte[] dec = cipher.doFinal(ciphertext);
153
if (Arrays.equals(plaintext, dec) == false) {
154
System.out.println("plaintext: " + hexDump(plaintext));
155
System.out.println("ciphertext: " + hexDump(ciphertext));
156
System.out.println("decrypted: " + hexDump(dec));
157
System.out.println("password: " + password);
158
System.out.println("salt: " + hexDump(salt));
159
System.out.println("iterationCount: " + ic);
160
throw new Exception("decryption test for " + alg + " failed");
161
}
162
System.out.println("passed: " + alg);
163
}
164
}
165
166
private static Test t(String alg, String plaintext, char[] password,
167
String salt, int iterationCount, String ciphertext) {
168
return new CipherTest(alg, b(plaintext), b(ciphertext), password,
169
b(salt), iterationCount);
170
}
171
172
private final static char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
173
private final static Test[] tests = {
174
t("PBEWithSHA1AndDESede", INPUT, PASSWD, SALT, ITER_COUNT,
175
"95:94:49:5a:a2:cf:c9:a5:bb:21:08:23:45:41:46:a3:9c:c5:84:da:b5:04:ae:1a"),
176
t("PBEWithSHA1AndRC2_40", INPUT, PASSWD, SALT, ITER_COUNT,
177
"ec:32:f4:68:29:29:8b:c8:55:75:cb:ac:a4:01:d9:9c:b3:27:d6:b6:9f:26:98:f1")
178
};
179
180
static void runTests(Test[] tests) throws Exception {
181
long start = System.currentTimeMillis();
182
Provider p = Security.getProvider("SunJCE");
183
System.out.println("Testing provider " + p.getName() + "...");
184
Cipher.getInstance("PBEWithSHA1AndRC2_40", p);
185
Cipher.getInstance("PBEWithSHA1AndDESede", p);
186
for (int i = 0; i < tests.length; i++) {
187
Test test = tests[i];
188
test.run(p);
189
}
190
System.out.println("All tests passed");
191
long stop = System.currentTimeMillis();
192
System.out.println("Done (" + (stop - start) + " ms).");
193
}
194
195
public static void main(String[] args) throws Exception {
196
runTests(tests);
197
}
198
}
199
200