Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/CipherSpi/ResetByteBuffer.java
41149 views
1
/*
2
* Copyright (c) 2021, 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 8261462
27
* @summary Verify that after the first doFinal() decryption op, the ByteBuffer
28
* is properly set for the second operation.
29
*/
30
31
import javax.crypto.Cipher;
32
import javax.crypto.KeyGenerator;
33
import javax.crypto.SecretKey;
34
import java.nio.ByteBuffer;
35
36
public class ResetByteBuffer {
37
38
Cipher c;
39
SecretKey key;
40
ByteBuffer in, out;
41
byte[] data = new byte[1500];
42
byte encrypted[];
43
44
public static final void main(String args[]) throws Exception {
45
// Cannot do encryption back to back with AES/GCM
46
// Tests GCM's ByteBuffer code
47
String algo = "AES/GCM/NoPadding";
48
new ResetByteBuffer(algo).decrypt(true).updateTest().updateTest();
49
new ResetByteBuffer(algo).decrypt(false).updateTest().updateTest();
50
new ResetByteBuffer(algo).decrypt(true).updateTest().doFinalTest();
51
new ResetByteBuffer(algo).decrypt(false).updateTest().doFinalTest();
52
new ResetByteBuffer(algo).decrypt(true).doFinalTest().updateTest();
53
new ResetByteBuffer(algo).decrypt(false).doFinalTest().updateTest();
54
new ResetByteBuffer(algo).decrypt(true).doFinalTest().doFinalTest();
55
new ResetByteBuffer(algo).decrypt(false).doFinalTest().doFinalTest();
56
57
// Tests CipherCore code. Testing CBC should be enough to cover the
58
// other algorithms that use CipherCore
59
algo = "AES/CBC/PKCS5Padding";
60
new ResetByteBuffer(algo).encrypt(true).updateTest().updateTest();
61
new ResetByteBuffer(algo).encrypt(false).updateTest().updateTest();
62
new ResetByteBuffer(algo).encrypt(true).updateTest().doFinalTest();
63
new ResetByteBuffer(algo).encrypt(false).updateTest().doFinalTest();
64
new ResetByteBuffer(algo).encrypt(true).doFinalTest().updateTest();
65
new ResetByteBuffer(algo).encrypt(false).doFinalTest().updateTest();
66
new ResetByteBuffer(algo).encrypt(true).doFinalTest().doFinalTest();
67
new ResetByteBuffer(algo).encrypt(false).doFinalTest().doFinalTest();
68
new ResetByteBuffer(algo).decrypt(true).updateTest().updateTest();
69
new ResetByteBuffer(algo).decrypt(false).updateTest().updateTest();
70
new ResetByteBuffer(algo).decrypt(true).updateTest().doFinalTest();
71
new ResetByteBuffer(algo).decrypt(false).updateTest().doFinalTest();
72
new ResetByteBuffer(algo).decrypt(true).doFinalTest().updateTest();
73
new ResetByteBuffer(algo).decrypt(false).doFinalTest().updateTest();
74
new ResetByteBuffer(algo).decrypt(true).doFinalTest().doFinalTest();
75
new ResetByteBuffer(algo).decrypt(false).doFinalTest().doFinalTest();
76
}
77
78
public ResetByteBuffer(String algo) throws Exception {
79
c = Cipher.getInstance(algo);
80
String a[] = algo.split("/");
81
KeyGenerator kg = KeyGenerator.getInstance(a[0]);
82
key = kg.generateKey();
83
// Setup encrypted data
84
c.init(Cipher.ENCRYPT_MODE, key, c.getParameters());
85
encrypted = new byte[c.getOutputSize(data.length)];
86
c.doFinal(data, 0, data.length, encrypted, 0);
87
}
88
89
ResetByteBuffer decrypt(boolean direct) throws Exception {
90
// allocate bytebuffers
91
if (direct) {
92
in = ByteBuffer.allocateDirect(encrypted.length);
93
out = ByteBuffer.allocateDirect(encrypted.length);
94
} else {
95
in = ByteBuffer.allocate(encrypted.length);
96
out = ByteBuffer.allocate(encrypted.length);
97
}
98
in.put(encrypted);
99
in.flip();
100
c.init(Cipher.DECRYPT_MODE, key, c.getParameters());
101
return this;
102
}
103
104
ResetByteBuffer encrypt(boolean direct) throws Exception {
105
// allocate bytebuffers
106
if (direct) {
107
in = ByteBuffer.allocateDirect(data.length);
108
out = ByteBuffer.allocateDirect(c.getOutputSize(data.length));
109
} else {
110
in = ByteBuffer.allocate(data.length);
111
out = ByteBuffer.allocate(c.getOutputSize(data.length));
112
}
113
c.init(Cipher.ENCRYPT_MODE, key, c.getParameters());
114
return this;
115
}
116
117
ResetByteBuffer updateTest() throws Exception {
118
int updateLen = data.length / 2;
119
in.limit(updateLen);
120
c.update(in, out);
121
in.limit(in.capacity());
122
c.doFinal(in, out);
123
if (in.capacity() != in.position()) {
124
System.out.println("There is data remaining in the input buffer");
125
}
126
if (out.limit() != out.position()) {
127
System.out.println("There is data remaining in the output buffer");
128
}
129
in.flip();
130
out.position(0);
131
out.limit(out.capacity());
132
return this;
133
}
134
135
ResetByteBuffer doFinalTest() throws Exception {
136
c.doFinal(in, out);
137
if (in.capacity() != in.position()) {
138
System.out.println("There is data remaining in the input buffer");
139
}
140
if (out.limit() != out.position()) {
141
System.out.println("There is data remaining in the output buffer");
142
}
143
in.flip();
144
out.position(0);
145
out.limit(out.capacity());
146
return this;
147
}
148
}
149
150