Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AEAD/OverlapByteBuffer.java
41161 views
1
/*
2
* Copyright (c) 2020, 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
import javax.crypto.Cipher;
25
import javax.crypto.spec.GCMParameterSpec;
26
import javax.crypto.spec.SecretKeySpec;
27
import java.nio.ByteBuffer;
28
29
/*
30
* @test
31
* @summary This tests overlapping buffers using ByteBuffer.slice() with
32
* array-backed ByteBuffer, read only array-backed, ByteBuffer, and direct
33
* ByteBuffer.
34
*/
35
36
/*
37
* This tests overlapping buffers created with ByteBuffer.slice(). That is
38
* when the input and output ByteBuffers have shared memory (use the same
39
* underlying buffer space, commonly used for in-place crypto). The
40
* complication is the Cipher object specifies that it must be copy-safe. That
41
* means the output buffer will not overwrite any input data that has not been
42
* processed. If the output buffer's position or offset is greater than the
43
* input's overwriting will occur.
44
*/
45
46
public class OverlapByteBuffer {
47
48
public static void main(String[] args) throws Exception {
49
byte[] baseBuf = new byte[8192];
50
ByteBuffer output, input, in;
51
// Output offset from the baseBuf
52
int outOfs;
53
54
for (int i = 0; i < 3; i++) {
55
for (outOfs = -1; outOfs <= 1; outOfs++) {
56
57
SecretKeySpec key = new SecretKeySpec(new byte[16], "AES");
58
GCMParameterSpec params =
59
new GCMParameterSpec(128, new byte[12]);
60
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
61
cipher.init(Cipher.ENCRYPT_MODE, key, params);
62
63
// Offset on the particular ByteBuffer (aka position())
64
int inOfsInBuf = 1;
65
int outOfsInBuf = inOfsInBuf + outOfs;
66
int sliceLen = cipher.getOutputSize(baseBuf.length);
67
int bufferSize = sliceLen + Math.max(inOfsInBuf, outOfsInBuf);
68
byte[] buffer;
69
// Create overlapping input and output buffers
70
switch (i) {
71
case 0 -> {
72
buffer = new byte[bufferSize];
73
output = ByteBuffer.wrap(buffer, outOfsInBuf, sliceLen).
74
slice();
75
input = ByteBuffer.wrap(buffer, inOfsInBuf, sliceLen).
76
slice();
77
System.out.println("Using array-backed ByteBuffer");
78
in = input.duplicate();
79
}
80
case 1 -> {
81
buffer = new byte[bufferSize];
82
output = ByteBuffer.wrap(buffer, outOfsInBuf, sliceLen).
83
slice();
84
input = ByteBuffer.wrap(buffer, inOfsInBuf, sliceLen).
85
slice();
86
87
System.out.println("Using read-only array-backed " + "ByteBuffer");
88
in = input.asReadOnlyBuffer();
89
}
90
case 2 -> {
91
System.out.println("Using direct ByteBuffer");
92
ByteBuffer buf = ByteBuffer.allocateDirect(bufferSize);
93
output = buf.duplicate();
94
output.position(outOfsInBuf);
95
output.limit(sliceLen + outOfsInBuf);
96
output = output.slice();
97
98
input = buf.duplicate();
99
input.position(inOfsInBuf);
100
input.limit(sliceLen + inOfsInBuf);
101
input = input.slice();
102
103
in = input.duplicate();
104
}
105
default -> {
106
throw new Exception("Unknown index " + i);
107
}
108
}
109
110
// Copy data into shared buffer
111
input.put(baseBuf);
112
input.flip();
113
in.limit(input.limit());
114
115
try {
116
int ctSize = cipher.doFinal(in, output);
117
118
// Get ready to decrypt
119
byte[] tmp = new byte[ctSize];
120
output.flip();
121
output.get(tmp);
122
output.clear();
123
124
input.clear();
125
input.put(tmp);
126
input.flip();
127
128
in.clear();
129
in.limit(input.limit());
130
131
cipher.init(Cipher.DECRYPT_MODE, key, params);
132
cipher.doFinal(in, output);
133
134
output.flip();
135
System.out.println("inOfsInBuf = " + inOfsInBuf);
136
System.out.println("outOfsInBuf = " + outOfsInBuf);
137
ByteBuffer b = ByteBuffer.wrap(baseBuf);
138
if (b.compareTo(output) != 0) {
139
System.err.println(
140
"\nresult (" + output + "):\n" +
141
byteToHex(output) +
142
"\nexpected (" + b + "):\n" +
143
byteToHex(b));
144
throw new Exception("Mismatch");
145
}
146
} catch (Exception e) {
147
throw new Exception("Error with base offset " + outOfs, e);
148
}
149
}
150
}
151
}
152
private static String byteToHex(ByteBuffer bb) {
153
StringBuilder s = new StringBuilder();
154
while (bb.remaining() > 0) {
155
s.append(String.format("%02x", bb.get()));
156
}
157
return s.toString();
158
}
159
}
160
161