Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java
41152 views
1
/*
2
* Copyright (c) 2018, 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 8080462
27
* @library /test/lib ..
28
* @modules jdk.crypto.cryptoki
29
* @run main TestCICOWithGCM
30
* @summary Test CipherInputStream/OutputStream with AES GCM mode.
31
* @key randomness
32
*/
33
34
import java.security.*;
35
import javax.crypto.*;
36
import javax.crypto.spec.*;
37
import java.math.*;
38
import java.io.*;
39
40
import java.util.*;
41
42
public class TestCICOWithGCM extends PKCS11Test {
43
public static void main(String[] args) throws Exception {
44
main(new TestCICOWithGCM(), args);
45
}
46
47
@Override
48
public void main(Provider p) throws Exception {
49
test("GCM", p);
50
}
51
52
public void test(String mode, Provider p) throws Exception {
53
Cipher c;
54
try {
55
String transformation = "AES/" + mode + "/NoPadding";
56
c = Cipher.getInstance(transformation, p);
57
} catch (GeneralSecurityException e) {
58
System.out.println("Skip testing " + p.getName() +
59
", no support for " + mode);
60
return;
61
}
62
63
SecretKey key = new SecretKeySpec(new byte[16], "AES");
64
65
//do initialization of the plainText
66
byte[] plainText = new byte[800];
67
Random rdm = new Random();
68
rdm.nextBytes(plainText);
69
70
//init ciphers
71
Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", p);
72
encCipher.init(Cipher.ENCRYPT_MODE, key);
73
Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", p);
74
decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());
75
76
//init cipher streams
77
ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
78
CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);
79
ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
80
CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);
81
82
//do test
83
byte[] buffer = new byte[800];
84
int len = ciInput.read(buffer);
85
System.out.println("read " + len + " bytes from input buffer");
86
87
while (len != -1) {
88
ciOutput.write(buffer, 0, len);
89
System.out.println("wite " + len + " bytes to output buffer");
90
len = ciInput.read(buffer);
91
if (len != -1) {
92
System.out.println("read " + len + " bytes from input buffer");
93
} else {
94
System.out.println("finished reading");
95
}
96
}
97
98
ciOutput.flush();
99
ciInput.close();
100
ciOutput.close();
101
byte[] recovered = baOutput.toByteArray();
102
System.out.println("recovered " + recovered.length + " bytes");
103
if (!Arrays.equals(plainText, recovered)) {
104
throw new RuntimeException("diff check failed!");
105
} else {
106
System.out.println("diff check passed");
107
}
108
}
109
}
110
111