Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/PBECipherWrapper.java
41161 views
1
/*
2
* Copyright (c) 2015, 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
import java.security.AlgorithmParameters;
25
import java.security.InvalidAlgorithmParameterException;
26
import java.security.InvalidKeyException;
27
import java.security.NoSuchAlgorithmException;
28
import java.security.spec.AlgorithmParameterSpec;
29
import java.security.spec.InvalidKeySpecException;
30
import java.security.spec.InvalidParameterSpecException;
31
import java.util.Arrays;
32
import java.util.List;
33
import java.util.ArrayList;
34
import javax.crypto.BadPaddingException;
35
import javax.crypto.Cipher;
36
import javax.crypto.IllegalBlockSizeException;
37
import javax.crypto.NoSuchPaddingException;
38
import javax.crypto.SecretKey;
39
import javax.crypto.SecretKeyFactory;
40
import javax.crypto.ShortBufferException;
41
import javax.crypto.spec.IvParameterSpec;
42
import javax.crypto.spec.PBEKeySpec;
43
import javax.crypto.spec.PBEParameterSpec;
44
import javax.crypto.spec.SecretKeySpec;
45
46
/**
47
* PBECipherWrapper is the abstract class for all concrete PBE Cipher wrappers.
48
*/
49
public abstract class PBECipherWrapper {
50
51
public static final int ITERATION_COUNT = 1000;
52
private final String algorithm;
53
private final byte[] salt;
54
protected SecretKey key;
55
protected Cipher ci;
56
protected String baseAlgo;
57
protected byte[] resultText = null;
58
protected AlgorithmParameterSpec aps = null;
59
60
public PBECipherWrapper(String algorithm, int saltSize) {
61
this.algorithm = algorithm;
62
baseAlgo = algorithm.split("/")[0].toUpperCase();
63
salt = generateSalt(saltSize);
64
}
65
66
protected abstract void initCipher(int mode) throws InvalidKeyException,
67
InvalidAlgorithmParameterException, InvalidParameterSpecException;
68
69
public void execute(int edMode, byte[] inputText)
70
throws InvalidAlgorithmParameterException,
71
InvalidParameterSpecException, IllegalBlockSizeException,
72
BadPaddingException, ShortBufferException, InvalidKeyException {
73
// Initialize
74
initCipher(edMode);
75
76
// Generate a resultText using a single-part enc/dec
77
resultText = ci.doFinal(inputText);
78
79
// Generate outputText for each multi-part en/de-cryption
80
/* Combination #1:
81
update(byte[], int, int)
82
doFinal(byte[], int, int)
83
*/
84
byte[] part11 = ci.update(inputText, 0, inputText.length);
85
byte[] part12 = ci.doFinal();
86
byte[] outputText1 = new byte[part11.length + part12.length];
87
System.arraycopy(part11, 0, outputText1, 0, part11.length);
88
System.arraycopy(part12, 0, outputText1, part11.length, part12.length);
89
90
List<byte[]> outputTexts = new ArrayList<>(4);
91
outputTexts.add(outputText1);
92
93
/* Combination #2:
94
update(byte[], int, int)
95
doFinal(byte[], int, int, byte[], int)
96
*/
97
byte[] part21 = ci.update(inputText, 0, inputText.length - 5);
98
byte[] part22 = new byte[ci.getOutputSize(inputText.length)];
99
int len2 = ci.doFinal(inputText, inputText.length - 5, 5, part22, 0);
100
byte[] outputText2 = new byte[part21.length + len2];
101
System.arraycopy(part21, 0, outputText2, 0, part21.length);
102
System.arraycopy(part22, 0, outputText2, part21.length, len2);
103
104
outputTexts.add(outputText2);
105
106
/* Combination #3:
107
update(byte[], int, int, byte[], int)
108
doFinal(byte[], int, int)
109
*/
110
byte[] part31 = new byte[ci.getOutputSize(inputText.length)];
111
int len3 = ci.update(inputText, 0, inputText.length - 8, part31, 0);
112
byte[] part32 = ci.doFinal(inputText, inputText.length - 8, 8);
113
byte[] outputText3 = new byte[len3 + part32.length];
114
System.arraycopy(part31, 0, outputText3, 0, len3);
115
System.arraycopy(part32, 0, outputText3, len3, part32.length);
116
117
outputTexts.add(outputText3);
118
119
/* Combination #4:
120
update(byte[], int, int, byte[], int)
121
doFinal(byte[], int, int, byte[], int)
122
*/
123
byte[] part41 = new byte[ci.getOutputSize(inputText.length)];
124
int len4 = ci.update(inputText, 0, inputText.length - 8, part41, 0);
125
int rest4 = ci
126
.doFinal(inputText, inputText.length - 8, 8, part41, len4);
127
byte[] outputText4 = new byte[len4 + rest4];
128
System.arraycopy(part41, 0, outputText4, 0, outputText4.length);
129
130
outputTexts.add(outputText4);
131
132
// Compare results
133
for (int k = 0; k < outputTexts.size(); k++) {
134
if (!Arrays.equals(resultText, outputTexts.get(k))) {
135
throw new RuntimeException(
136
"Compare value of resultText and combination " + k
137
+ " are not same. Test failed.");
138
}
139
}
140
141
}
142
143
public final byte[] generateSalt(int numberOfBytes) {
144
byte[] aSalt = new byte[numberOfBytes];
145
for (int i = 0; i < numberOfBytes; i++) {
146
aSalt[i] = (byte) (i & 0xff);
147
}
148
return aSalt;
149
}
150
151
public byte[] getResult() {
152
return resultText;
153
}
154
155
public String getAlgorithm() {
156
return algorithm;
157
}
158
159
public byte[] getSalt() {
160
return salt;
161
}
162
163
/**
164
* Wrapper class to test a given SecretKeyFactory.PBKDF2 algorithm.
165
*/
166
public static class PBKDF2 extends PBECipherWrapper {
167
168
private static final int PBKDF2_SALT_SIZE = 64;
169
private static final int CIPHER_KEY_SIZE = 128;
170
private static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
171
private static final String KEY_ALGORITHM = "AES";
172
private byte[] iv = null;
173
174
public PBKDF2(String algo, String passwd)
175
throws InvalidKeySpecException, NoSuchAlgorithmException,
176
NoSuchPaddingException {
177
super(algo, PBKDF2_SALT_SIZE);
178
179
ci = Cipher.getInstance(CIPHER_TRANSFORMATION);
180
181
PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd.toCharArray(), getSalt(),
182
ITERATION_COUNT, CIPHER_KEY_SIZE);
183
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algo);
184
key = keyFactory.generateSecret(pbeKeySpec);
185
}
186
187
@Override
188
protected void initCipher(int mode) throws InvalidKeyException,
189
InvalidAlgorithmParameterException, InvalidParameterSpecException {
190
if (Cipher.ENCRYPT_MODE == mode) {
191
ci.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getEncoded(),
192
KEY_ALGORITHM));
193
iv = ci.getParameters().getParameterSpec(IvParameterSpec.class)
194
.getIV();
195
} else {
196
ci.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getEncoded(),
197
KEY_ALGORITHM), new IvParameterSpec(iv));
198
}
199
}
200
}
201
202
/**
203
* Wrapper class to test a given AES-based PBE algorithm.
204
*/
205
public static class AES extends PBECipherWrapper {
206
207
private AlgorithmParameters pbeParams;
208
209
public AES(String algo, String passwd)
210
throws NoSuchAlgorithmException, NoSuchPaddingException,
211
InvalidKeySpecException {
212
super(algo, 0);
213
214
ci = Cipher.getInstance(algo);
215
216
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
217
key = skf.generateSecret(new PBEKeySpec(passwd.toCharArray()));
218
}
219
220
@Override
221
protected void initCipher(int mode) throws InvalidKeyException,
222
InvalidAlgorithmParameterException, InvalidParameterSpecException {
223
if (Cipher.ENCRYPT_MODE == mode) {
224
ci.init(Cipher.ENCRYPT_MODE, key);
225
pbeParams = ci.getParameters();
226
} else {
227
ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
228
}
229
}
230
}
231
232
/**
233
* Wrapper class to test a given PBE algorithm.
234
*/
235
public static class Legacy extends PBECipherWrapper {
236
237
private static final int PBE_SALT_SIZE = 8;
238
239
public Legacy(String algo, String passwd)
240
throws NoSuchAlgorithmException, NoSuchPaddingException,
241
InvalidKeySpecException {
242
super(algo, PBE_SALT_SIZE);
243
244
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.split("/")[0]);
245
key = skf.generateSecret(new PBEKeySpec(passwd.toCharArray()));
246
247
aps = new PBEParameterSpec(getSalt(), ITERATION_COUNT);
248
249
ci = Cipher.getInstance(algo);
250
}
251
252
@Override
253
protected void initCipher(int mode) throws InvalidKeyException,
254
InvalidAlgorithmParameterException, InvalidParameterSpecException {
255
ci.init(mode, key, aps);
256
}
257
}
258
}
259
260