Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/crypto/Cipher/CipherStreamClose.java
38839 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 7160837
27
* @summary Make sure Cipher IO streams doesn't call extra doFinal if close()
28
* is called multiple times. Additionally, verify the input and output streams
29
* match with encryption and decryption with non-stream crypto.
30
*/
31
32
import java.io.*;
33
import java.security.DigestOutputStream;
34
import java.security.DigestInputStream;
35
import java.security.MessageDigest;
36
import java.util.Arrays;
37
38
import javax.crypto.Cipher;
39
import javax.crypto.CipherOutputStream;
40
import javax.crypto.CipherInputStream;
41
import javax.crypto.SecretKey;
42
import javax.crypto.spec.SecretKeySpec;
43
import javax.xml.bind.DatatypeConverter;
44
45
public class CipherStreamClose {
46
private static final String message = "This is the sample message";
47
static boolean debug = false;
48
49
/*
50
* This method does encryption by cipher.doFinal(), and not with
51
* CipherOutputStream
52
*/
53
public static byte[] blockEncrypt(String message, SecretKey key)
54
throws Exception {
55
56
byte[] data;
57
Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
58
encCipher.init(Cipher.ENCRYPT_MODE, key);
59
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
60
try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
61
oos.writeObject(message);
62
}
63
data = bos.toByteArray();
64
}
65
66
if (debug) {
67
System.out.println(DatatypeConverter.printHexBinary(data));
68
}
69
return encCipher.doFinal(data);
70
71
}
72
73
/*
74
* This method does decryption by cipher.doFinal(), and not with
75
* CipherIntputStream
76
*/
77
public static Object blockDecrypt(byte[] data, SecretKey key)
78
throws Exception {
79
80
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
81
c.init(Cipher.DECRYPT_MODE, key);
82
data = c.doFinal(data);
83
try (ByteArrayInputStream bis = new ByteArrayInputStream(data)) {
84
try (ObjectInputStream ois = new ObjectInputStream(bis)) {
85
return ois.readObject();
86
}
87
}
88
}
89
90
public static byte[] streamEncrypt(String message, SecretKey key,
91
MessageDigest digest)
92
throws Exception {
93
94
byte[] data;
95
Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
96
encCipher.init(Cipher.ENCRYPT_MODE, key);
97
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
98
DigestOutputStream dos = new DigestOutputStream(bos, digest);
99
CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
100
try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
101
oos.writeObject(message);
102
}
103
data = bos.toByteArray();
104
}
105
106
if (debug) {
107
System.out.println(DatatypeConverter.printHexBinary(data));
108
}
109
return data;
110
}
111
112
public static Object streamDecrypt(byte[] data, SecretKey key,
113
MessageDigest digest) throws Exception {
114
115
Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
116
decCipher.init(Cipher.DECRYPT_MODE, key);
117
digest.reset();
118
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
119
DigestInputStream dis = new DigestInputStream(bis, digest);
120
CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
121
122
try (ObjectInputStream ois = new ObjectInputStream(cis)) {
123
return ois.readObject();
124
}
125
}
126
}
127
128
public static void main(String[] args) throws Exception {
129
MessageDigest digest = MessageDigest.getInstance("SHA1");
130
SecretKeySpec key = new SecretKeySpec(
131
DatatypeConverter.parseHexBinary(
132
"12345678123456781234567812345678"), "AES");
133
134
// Run 'message' through streamEncrypt
135
byte[] se = streamEncrypt(message, key, digest);
136
// 'digest' already has the value from the stream, just finish the op
137
byte[] sd = digest.digest();
138
digest.reset();
139
// Run 'message' through blockEncrypt
140
byte[] be = blockEncrypt(message, key);
141
// Take digest of encrypted blockEncrypt result
142
byte[] bd = digest.digest(be);
143
// Verify both returned the same value
144
if (!Arrays.equals(sd, bd)) {
145
System.err.println("Stream: "+DatatypeConverter.printHexBinary(se)+
146
"\t Digest: "+DatatypeConverter.printHexBinary(sd));
147
System.err.println("Block : "+DatatypeConverter.printHexBinary(be)+
148
"\t Digest: "+DatatypeConverter.printHexBinary(bd));
149
throw new Exception("stream & block encryption does not match");
150
}
151
152
digest.reset();
153
// Sanity check: Decrypt separately from stream to verify operations
154
String bm = (String) blockDecrypt(be, key);
155
if (message.compareTo(bm) != 0) {
156
System.err.println("Expected: "+message+"\nBlock: "+bm);
157
throw new Exception("Block decryption does not match expected");
158
}
159
160
// Have decryption and digest included in the object stream
161
String sm = (String) streamDecrypt(se, key, digest);
162
if (message.compareTo(sm) != 0) {
163
System.err.println("Expected: "+message+"\nStream: "+sm);
164
throw new Exception("Stream decryption does not match expected.");
165
}
166
}
167
}
168
169