Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java
38855 views
1
/*
2
* Copyright (c) 2007, 2016, 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 4898484 6604496 8001284
27
* @summary basic test for symmetric ciphers with no padding
28
* @author Valerie Peng
29
* @library ..
30
* @key randomness
31
* @run main/othervm TestSymmCiphersNoPad
32
* @run main/othervm TestSymmCiphersNoPad sm
33
*/
34
35
import java.io.ByteArrayInputStream;
36
import java.io.ByteArrayOutputStream;
37
import java.io.InputStream;
38
import java.nio.ByteBuffer;
39
import java.security.AlgorithmParameters;
40
import java.security.NoSuchAlgorithmException;
41
import java.security.Provider;
42
import java.util.Random;
43
import javax.crypto.Cipher;
44
import javax.crypto.CipherInputStream;
45
import javax.crypto.KeyGenerator;
46
import javax.crypto.SecretKey;
47
48
public class TestSymmCiphersNoPad extends PKCS11Test {
49
50
private static class CI { // class for holding Cipher Information
51
String transformation;
52
String keyAlgo;
53
int dataSize;
54
55
CI(String transformation, String keyAlgo, int dataSize) {
56
this.transformation = transformation;
57
this.keyAlgo = keyAlgo;
58
this.dataSize = dataSize;
59
}
60
}
61
62
private static final CI TEST_LIST[] = {
63
new CI("ARCFOUR", "ARCFOUR", 400),
64
new CI("RC4", "RC4", 401),
65
new CI("DES/CBC/NoPadding", "DES", 400),
66
new CI("DESede/CBC/NoPadding", "DESede", 160),
67
new CI("AES/CBC/NoPadding", "AES", 4800),
68
new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
69
new CI("AES/CTR/NoPadding", "AES", 1600),
70
new CI("AES/CTR/NoPadding", "AES", 65)
71
};
72
73
private static StringBuffer debugBuf;
74
75
@Override
76
public void main(Provider p) throws Exception {
77
boolean status = true;
78
Random random = new Random();
79
try {
80
for (int i = 0; i < TEST_LIST.length; i++) {
81
CI currTest = TEST_LIST[i];
82
System.out.println("===" + currTest.transformation + "===");
83
try {
84
KeyGenerator kg =
85
KeyGenerator.getInstance(currTest.keyAlgo, p);
86
SecretKey key = kg.generateKey();
87
Cipher c1 = Cipher.getInstance(currTest.transformation, p);
88
Cipher c2 = Cipher.getInstance(currTest.transformation,
89
"SunJCE");
90
91
byte[] plainTxt = new byte[currTest.dataSize];
92
random.nextBytes(plainTxt);
93
System.out.println("Testing inLen = " + plainTxt.length);
94
95
c2.init(Cipher.ENCRYPT_MODE, key);
96
AlgorithmParameters params = c2.getParameters();
97
byte[] answer = c2.doFinal(plainTxt);
98
test(c1, Cipher.ENCRYPT_MODE, key, params,
99
plainTxt, answer);
100
System.out.println("Encryption tests: DONE");
101
c2.init(Cipher.DECRYPT_MODE, key, params);
102
byte[] answer2 = c2.doFinal(answer);
103
test(c1, Cipher.DECRYPT_MODE, key, params,
104
answer, answer2);
105
System.out.println("Decryption tests: DONE");
106
} catch (NoSuchAlgorithmException nsae) {
107
System.out.println("Skipping unsupported algorithm: " +
108
nsae);
109
}
110
}
111
} catch (Exception ex) {
112
// print out debug info when exception is encountered
113
if (debugBuf != null) {
114
System.out.println(debugBuf.toString());
115
}
116
throw ex;
117
}
118
}
119
120
private static void test(Cipher cipher, int mode, SecretKey key,
121
AlgorithmParameters params,
122
byte[] in, byte[] answer) throws Exception {
123
// test setup
124
debugBuf = new StringBuffer();
125
cipher.init(mode, key, params);
126
int outLen = cipher.getOutputSize(in.length);
127
debugBuf.append("Estimated output size = " + outLen + "\n");
128
129
// test data preparation
130
ByteBuffer inBuf = ByteBuffer.allocate(in.length);
131
inBuf.put(in);
132
inBuf.position(0);
133
ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);
134
inDirectBuf.put(in);
135
inDirectBuf.position(0);
136
ByteBuffer outBuf = ByteBuffer.allocate(outLen);
137
ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);
138
139
// test#1: byte[] in + byte[] out
140
debugBuf.append("Test#1:\n");
141
ByteArrayOutputStream baos = new ByteArrayOutputStream();
142
byte[] testOut1 = cipher.update(in, 0, 16);
143
if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);
144
testOut1 = cipher.doFinal(in, 16, in.length-16);
145
if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);
146
testOut1 = baos.toByteArray();
147
match(testOut1, answer);
148
149
// test#2: Non-direct Buffer in + non-direct Buffer out
150
debugBuf.append("Test#2:\n");
151
debugBuf.append("inputBuf: " + inBuf + "\n");
152
debugBuf.append("outputBuf: " + outBuf + "\n");
153
cipher.update(inBuf, outBuf);
154
cipher.doFinal(inBuf, outBuf);
155
match(outBuf, answer);
156
157
// test#3: Direct Buffer in + direc Buffer out
158
debugBuf.append("Test#3:\n");
159
debugBuf.append("(pre) inputBuf: " + inDirectBuf + "\n");
160
debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");
161
cipher.update(inDirectBuf, outDirectBuf);
162
cipher.doFinal(inDirectBuf, outDirectBuf);
163
164
debugBuf.append("(post) inputBuf: " + inDirectBuf + "\n");
165
debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");
166
match(outDirectBuf, answer);
167
168
// test#4: Direct Buffer in + non-direct Buffer out
169
debugBuf.append("Test#4:\n");
170
inDirectBuf.position(0);
171
outBuf.position(0);
172
debugBuf.append("inputBuf: " + inDirectBuf + "\n");
173
debugBuf.append("outputBuf: " + outBuf + "\n");
174
cipher.update(inDirectBuf, outBuf);
175
cipher.doFinal(inDirectBuf, outBuf);
176
match(outBuf, answer);
177
178
// test#5: Non-direct Buffer in + direct Buffer out
179
debugBuf.append("Test#5:\n");
180
inBuf.position(0);
181
outDirectBuf.position(0);
182
183
debugBuf.append("(pre) inputBuf: " + inBuf + "\n");
184
debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");
185
186
cipher.update(inBuf, outDirectBuf);
187
cipher.doFinal(inBuf, outDirectBuf);
188
189
debugBuf.append("(post) inputBuf: " + inBuf + "\n");
190
debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");
191
match(outDirectBuf, answer);
192
193
// test#6: Streams
194
debugBuf.append("Test#6: Streaming\n");
195
outBuf.position(0);
196
InputStream stream =
197
new CipherInputStream(new ByteArrayInputStream(in), cipher);
198
byte[] data = new byte[1024];
199
int bytesRead = 0;
200
try {
201
while (bytesRead >= 0) {
202
bytesRead = stream.read(data);
203
if (bytesRead == -1)
204
break;
205
debugBuf.append("bytesRead: " + bytesRead);
206
debugBuf.append("\toutBuf.position(): " + outBuf.position() +
207
"\n");
208
outBuf.put(data, 0 , bytesRead);
209
}
210
} catch (Exception ex) {
211
debugBuf.append("Caught Exception during stream reading\n");
212
throw ex;
213
}
214
match(outBuf, answer);
215
216
debugBuf = null;
217
}
218
219
private static void match(byte[] b1, byte[] b2) throws Exception {
220
if (b1.length != b2.length) {
221
debugBuf.append("got len : " + b1.length + "\n");
222
debugBuf.append("expect len: " + b2.length + "\n");
223
throw new Exception("mismatch - different length!\n");
224
} else {
225
for (int i = 0; i < b1.length; i++) {
226
if (b1[i] != b2[i]) {
227
debugBuf.append("got : " + toString(b1) + "\n");
228
debugBuf.append("expect: " + toString(b2) + "\n");
229
throw new Exception("mismatch");
230
}
231
}
232
}
233
}
234
235
private static void match(ByteBuffer bb, byte[] answer) throws Exception {
236
byte[] bbTemp = new byte[bb.position()];
237
bb.position(0);
238
bb.get(bbTemp, 0, bbTemp.length);
239
match(bbTemp, answer);
240
}
241
242
public static void main(String[] args) throws Exception {
243
main(new TestSymmCiphersNoPad(), args);
244
}
245
}
246
247