Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestCICOWithGCMAndAAD.java
41161 views
1
/*
2
* Copyright (c) 2013, 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 8012900
27
* @library ../UTIL
28
* @build TestUtil
29
* @run main TestCICOWithGCMAndAAD
30
* @summary Test CipherInputStream/OutputStream with AES GCM mode with AAD.
31
* @author Valerie Peng
32
* @key randomness
33
*/
34
import java.io.*;
35
import java.security.*;
36
import java.util.*;
37
import javax.crypto.*;
38
39
public class TestCICOWithGCMAndAAD {
40
public static void main(String[] args) throws Exception {
41
//init Secret Key
42
KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");
43
kg.init(128);
44
SecretKey key = kg.generateKey();
45
46
//Do initialization of the plainText
47
byte[] plainText = new byte[700];
48
Random rdm = new Random();
49
rdm.nextBytes(plainText);
50
51
byte[] aad = new byte[128];
52
rdm.nextBytes(aad);
53
byte[] aad2 = aad.clone();
54
aad2[50]++;
55
56
Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
57
encCipher.init(Cipher.ENCRYPT_MODE, key);
58
encCipher.updateAAD(aad);
59
Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
60
decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());
61
decCipher.updateAAD(aad);
62
63
byte[] recovered = test(encCipher, decCipher, plainText);
64
if (!Arrays.equals(plainText, recovered)) {
65
throw new Exception("sameAAD: diff check failed!");
66
} else System.out.println("sameAAD: passed");
67
68
encCipher.init(Cipher.ENCRYPT_MODE, key);
69
encCipher.updateAAD(aad2);
70
recovered = test(encCipher, decCipher, plainText);
71
if (recovered != null && recovered.length != 0) {
72
throw new Exception("diffAAD: no data should be returned!");
73
} else System.out.println("diffAAD: passed");
74
}
75
76
private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText)
77
throws Exception {
78
//init cipher streams
79
ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
80
CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);
81
ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
82
CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);
83
84
//do test
85
byte[] buffer = new byte[200];
86
int len = ciInput.read(buffer);
87
System.out.println("read " + len + " bytes from input buffer");
88
89
while (len != -1) {
90
ciOutput.write(buffer, 0, len);
91
System.out.println("wite " + len + " bytes to output buffer");
92
len = ciInput.read(buffer);
93
if (len != -1) {
94
System.out.println("read " + len + " bytes from input buffer");
95
} else {
96
System.out.println("finished reading");
97
}
98
}
99
100
ciOutput.flush();
101
ciInput.close();
102
ciOutput.close();
103
104
return baOutput.toByteArray();
105
}
106
}
107
108