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