Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/SameBufferOverwrite.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.KeyGenerator;
26
import javax.crypto.SecretKey;
27
import java.security.AlgorithmParameters;
28
import java.util.Arrays;
29
30
31
/*
32
* @test
33
* @summary Verify when decrypting over an existing buffer than padding does not
34
* overwrite past what the plaintext length is.
35
*
36
*/
37
38
public class SameBufferOverwrite {
39
40
private SecretKey skey;
41
private Cipher c;
42
private int start = 17, end = 17; // default
43
44
SameBufferOverwrite(String algo, String transformation)
45
throws Exception {
46
47
KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");
48
skey = kg.generateKey();
49
c = Cipher.getInstance(transformation, "SunJCE");
50
}
51
52
/*
53
* Run the test
54
*/
55
void test() throws Exception {
56
byte[] in = new byte[end + (c.getBlockSize() - (end % c.getBlockSize()))];
57
Arrays.fill(in, (byte)8);
58
int len = start;
59
AlgorithmParameters params = null;
60
61
System.out.println("Testing transformation: " + c.getAlgorithm() +
62
", byte length from " + start + " to " + end);
63
while (end >= len) {
64
// encrypt
65
c.init(Cipher.ENCRYPT_MODE, skey, params);
66
byte[] out = c.doFinal(in, 0, len);
67
System.out.println(" enc = " + byteToHex(out));
68
System.out.println(" => enc " + len + " bytes, ret " +
69
(out == null ? "null" : (out.length + " byte")));
70
71
// decrypt
72
params = c.getParameters();
73
c.init(Cipher.DECRYPT_MODE, skey, params);
74
int rLen = c.doFinal(out, 0, out.length, in);
75
System.out.println(" dec = " + byteToHex(in));
76
System.out.println(" => dec " + out.length + " bytes, ret " +
77
rLen + " byte");
78
// check if more than rLen bytes are written into 'in'
79
for (int j = rLen; j < in.length; j++) {
80
if (in[j] != (byte) 8) {
81
throw new Exception("Value check failed at index " + j);
82
}
83
}
84
System.out.println(" Test Passed: len = " + len);
85
len++;
86
87
// Because GCM doesn't allow params reuse
88
if (c.getAlgorithm().contains("GCM")) {
89
params = null;
90
}
91
}
92
}
93
94
/**
95
* Builder method for the test to run data lengths from the start value to
96
* the end value. To do one length, have start and end equal that number.
97
* @param start starting data length
98
* @param end ending data length
99
*/
100
SameBufferOverwrite iterate(int start, int end) {
101
this.start = start;
102
this.end = end;
103
return this;
104
}
105
106
public static void main(String args[]) throws Exception {
107
new SameBufferOverwrite("AES", "AES/GCM/NoPadding").iterate(1, 25).
108
test();
109
new SameBufferOverwrite("AES", "AES/CTR/NoPadding").iterate(1, 25).
110
test();
111
new SameBufferOverwrite("AES", "AES/CBC/PKCS5Padding").iterate(1, 25).
112
test();
113
new SameBufferOverwrite("AES", "AES/ECB/PKCS5Padding").iterate(1, 25).
114
test();
115
new SameBufferOverwrite("DES", "DES/CBC/PKCS5Padding").iterate(1, 17).
116
test();
117
new SameBufferOverwrite("DESede", "DESede/CBC/PKCS5Padding").iterate(1, 17).
118
test();
119
}
120
121
private static String byteToHex(byte[] barray) {
122
StringBuilder s = new StringBuilder();
123
for (byte b : barray) {
124
s.append(String.format("%02x", b));
125
}
126
return s.toString();
127
}
128
}
129
130