Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/EncryptionPadding.java
41152 views
1
/*
2
* Copyright (c) 2021, Red Hat, Inc.
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 8261355
27
* @library /test/lib ..
28
* @run main/othervm EncryptionPadding
29
*/
30
31
import java.nio.ByteBuffer;
32
import java.security.Key;
33
import java.security.Provider;
34
import java.util.Arrays;
35
import javax.crypto.Cipher;
36
import javax.crypto.spec.SecretKeySpec;
37
38
public class EncryptionPadding extends PKCS11Test {
39
40
private static String transformation = "AES/ECB/PKCS5Padding";
41
private static Key key = new SecretKeySpec(new byte[16], "AES");
42
43
public static void main(String[] args) throws Exception {
44
main(new EncryptionPadding(), args);
45
}
46
47
@Override
48
public void main(Provider p) throws Exception {
49
testWithInputSize(p, 1);
50
testWithInputSize(p, 15);
51
testWithInputSize(p, 16);
52
testWithInputSize(p, 17);
53
System.out.println("TEST PASS - OK");
54
}
55
56
private static void testWithInputSize(Provider p, int inputSize)
57
throws Exception {
58
testWithInputSize(p, inputSize, false);
59
testWithInputSize(p, inputSize, true);
60
}
61
62
private static void testWithInputSize(Provider p, int inputSize,
63
boolean isByteBuffer) throws Exception {
64
byte[] plainText = new byte[inputSize];
65
Arrays.fill(plainText, (byte)(inputSize & 0xFF));
66
ByteBuffer cipherText =
67
ByteBuffer.allocate(((inputSize / 16 ) + 1) * 16);
68
byte[] tmp;
69
70
Cipher sunPKCS11cipher = Cipher.getInstance(transformation, p);
71
sunPKCS11cipher.init(Cipher.ENCRYPT_MODE, key);
72
for (int i = 0; i < ((inputSize - 1) / 16) + 1; i++) {
73
int updateLength = Math.min(inputSize - (16 * i), 16);
74
if (!isByteBuffer) {
75
tmp = sunPKCS11cipher.update(plainText, i * 16,
76
updateLength);
77
if (tmp != null) {
78
cipherText.put(tmp);
79
}
80
} else {
81
ByteBuffer bb = ByteBuffer.allocate(updateLength);
82
bb.put(plainText, i * 16, updateLength);
83
bb.flip();
84
sunPKCS11cipher.update(bb, cipherText);
85
}
86
}
87
if (!isByteBuffer) {
88
tmp = sunPKCS11cipher.doFinal();
89
if (tmp != null) {
90
cipherText.put(tmp);
91
}
92
} else {
93
sunPKCS11cipher.doFinal(ByteBuffer.allocate(0), cipherText);
94
}
95
96
Cipher sunJCECipher = Cipher.getInstance(transformation, "SunJCE");
97
sunJCECipher.init(Cipher.DECRYPT_MODE, key);
98
byte[] sunJCEPlain = sunJCECipher.doFinal(cipherText.array());
99
100
if (!Arrays.equals(plainText, sunJCEPlain)) {
101
throw new Exception("Cross-provider cipher test failed.");
102
}
103
}
104
}
105
106