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/PBMacBuffer.java
41161 views
1
/*
2
* Copyright (c) 2012, 2014, 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.nio.ByteBuffer;
25
import java.security.InvalidKeyException;
26
import java.security.NoSuchAlgorithmException;
27
import java.security.SecureRandom;
28
import java.security.spec.InvalidKeySpecException;
29
import java.util.Random;
30
import javax.crypto.Mac;
31
import javax.crypto.SecretKey;
32
import javax.crypto.SecretKeyFactory;
33
import javax.crypto.spec.PBEKeySpec;
34
35
/**
36
* @test
37
* @bug 8041787
38
* @summary verify that Mac.update works with different size ByteBuffer
39
* @author Alexander Fomin
40
* @run main PBMacBuffer
41
*/
42
public class PBMacBuffer {
43
44
private final int LARGE_SIZE = 500000;
45
46
public static void main(String[] args) {
47
String[] PBMAC1Algorithms = {
48
"HmacPBESHA1",
49
"PBEWithHmacSHA1",
50
"PBEWithHmacSHA224",
51
"PBEWithHmacSHA256",
52
"PBEWithHmacSHA384",
53
"PBEWithHmacSHA512"
54
};
55
56
String[] PBKDF2Algorithms = {
57
"PBKDF2WithHmacSHA1",
58
"PBKDF2WithHmacSHA224",
59
"PBKDF2WithHmacSHA256",
60
"PBKDF2WithHmacSHA384",
61
"PBKDF2WithHmacSHA512"
62
};
63
64
PBMacBuffer testRunner = new PBMacBuffer();
65
boolean failed = false;
66
67
for (String thePBMacAlgo : PBMAC1Algorithms) {
68
69
for (String thePBKDF2Algo : PBKDF2Algorithms) {
70
71
System.out.println("Running test with " + thePBMacAlgo
72
+ " and " + thePBKDF2Algo + ":");
73
try {
74
if (!testRunner.doTest(thePBMacAlgo, thePBKDF2Algo)) {
75
failed = true;
76
}
77
} catch (NoSuchAlgorithmException | InvalidKeyException |
78
InvalidKeySpecException e) {
79
failed = true;
80
e.printStackTrace(System.out);
81
System.out.println("Test FAILED.");
82
}
83
}
84
}
85
86
if (failed) {
87
throw new RuntimeException("One or more tests failed....");
88
}
89
}
90
91
/**
92
* Tests Mac.update(ByteBuffer input) method. Three test cases are
93
* performed: - large ByteBuffer test case to test if the update() method
94
* process a large ByteBuffer correctly; - empty ByteBuffer test case to
95
* test if the update() method process an empty ByteBuffer correctly; - NULL
96
* ByteBuffer test case to test if the update() method throws expected
97
* IllegalArgumentException exception.
98
*
99
* @param theMacAlgo PBMAC algorithm to test
100
* @param thePBKDF2Algo PBKDF2 algorithm to test
101
* @return true - test passed; false - otherwise.
102
* @throws NoSuchAlgorithmException
103
* @throws InvalidKeyException
104
* @throws InvalidKeySpecException
105
* @see javax.crypto.Mac
106
*/
107
protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)
108
throws NoSuchAlgorithmException, InvalidKeyException,
109
InvalidKeySpecException {
110
// obtain a SecretKey using PBKDF2
111
SecretKey key = getSecretKey(thePBKDF2Algo);
112
113
// Instantiate Mac object and init it with a SecretKey
114
Mac theMac = Mac.getInstance(theMacAlgo);
115
theMac.init(key);
116
117
// Do large ByteBuffer test case
118
if (!largeByteBufferTest(theMac)) {
119
System.out.println("Large ByteBuffer test case failed.");
120
return false;
121
}
122
123
// Do empty ByteBuffer test case
124
if (!emptyByteBufferTest(theMac)) {
125
System.out.println("Empty ByteBuffer test case failed.");
126
return false;
127
}
128
129
// Do null ByteBuffer test case
130
if (!nullByteBufferTest(theMac)) {
131
System.out.println("NULL ByteBuffer test case failed.");
132
return false;
133
}
134
135
return true;
136
}
137
138
/**
139
* Large ByteBuffer test case. Generate random ByteBuffer of LARGE_SIZE
140
* size. Performs MAC operation with the given Mac object (theMac
141
* parameter).Verifies the assertion "Upon return, the buffer's position
142
* will be equal to its limit; its limit will not have changed".
143
*
144
* @param theMac MAC object to test.
145
* @return true - test case passed; false - otherwise;
146
*/
147
protected boolean largeByteBufferTest(Mac theMac) {
148
ByteBuffer buf = generateRandomByteBuffer(LARGE_SIZE);
149
int limitBefore = buf.limit();
150
151
theMac.update(buf);
152
theMac.doFinal();
153
154
int limitAfter = buf.limit();
155
int positonAfter = buf.position();
156
157
if (limitAfter != limitBefore) {
158
System.out.println("FAIL: Buffer's limit has been chenged.");
159
return false;
160
}
161
162
if (positonAfter != limitAfter) {
163
System.out.println("FAIL: "
164
+ "Buffer's position isn't equal to its limit");
165
return false;
166
}
167
168
return true;
169
}
170
171
/**
172
* Empty ByteBuffer test case. Generates an empty ByteBuffer. Perform MAC
173
* operation. No exceptions are expected.
174
*
175
* @param theMac
176
* @return true - test case pass; exception otherwise
177
*/
178
protected boolean emptyByteBufferTest(Mac theMac) {
179
ByteBuffer buf = generateRandomByteBuffer(0);
180
theMac.update(buf);
181
theMac.doFinal();
182
return true;
183
}
184
185
/**
186
* NULL ByteBuffer test case. Pass NULL ByteBuffer to Mac.update(ByteBuffer
187
* buffer) method. An IllegalArgumentException expected.
188
*
189
* @param theMac Mac object to test.
190
* @return true - test case pass; false - otherwise.
191
*/
192
protected boolean nullByteBufferTest(Mac theMac) {
193
try {
194
ByteBuffer buf = null;
195
theMac.update(buf);
196
theMac.doFinal();
197
} catch (IllegalArgumentException e) {
198
// expected exception has been thrown
199
return true;
200
}
201
202
System.out.println("FAIL: "
203
+ "IllegalArgumentException hasn't been thrown as expected");
204
205
return false;
206
}
207
208
/**
209
* Get SecretKey for the given PBKDF2 algorithm.
210
*
211
* @param thePBKDF2Algorithm - PBKDF2 algorithm
212
* @return SecretKey according to thePBKDF2Algorithm
213
* @throws NoSuchAlgorithmException
214
* @throws InvalidKeySpecException
215
*/
216
protected SecretKey getSecretKey(String thePBKDF2Algorithm)
217
throws NoSuchAlgorithmException, InvalidKeySpecException {
218
// Prepare salt
219
byte[] salt = new byte[64]; // PKCS #5 v2.1 recommendation
220
new SecureRandom().nextBytes(salt);
221
222
// Generate secret key
223
PBEKeySpec pbeKeySpec = new PBEKeySpec(
224
"A #pwd# implied to be hidden!".toCharArray(),
225
salt, 1000, 128);
226
SecretKeyFactory keyFactory
227
= SecretKeyFactory.getInstance(thePBKDF2Algorithm);
228
return keyFactory.generateSecret(pbeKeySpec);
229
}
230
231
/**
232
* An utility method to generate a random ByteBuffer of the requested size.
233
*
234
* @param size size of the ByteBuffer.
235
* @return ByteBuffer populated random data;
236
*/
237
private ByteBuffer generateRandomByteBuffer(int size) {
238
// generate randome byte array
239
byte[] data = new byte[size];
240
new Random().nextBytes(data);
241
242
// create ByteBuffer
243
ByteBuffer bb = ByteBuffer.wrap(data);
244
245
return bb;
246
}
247
248
}
249
250