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/TestNonexpanding.java
41161 views
1
/*
2
* Copyright (c) 2014, 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 java.security.InvalidKeyException;
25
import java.security.NoSuchAlgorithmException;
26
import java.security.NoSuchProviderException;
27
import java.security.spec.InvalidParameterSpecException;
28
import java.util.Random;
29
import javax.crypto.BadPaddingException;
30
import javax.crypto.Cipher;
31
import javax.crypto.IllegalBlockSizeException;
32
import javax.crypto.KeyGenerator;
33
import javax.crypto.NoSuchPaddingException;
34
import javax.crypto.SecretKey;
35
import javax.crypto.ShortBufferException;
36
import javax.crypto.spec.GCMParameterSpec;
37
38
/**
39
* @test
40
* @bug 8043836
41
* @summary Test AES encryption with no padding. Expect the original data length
42
* is the same as the encrypted data.
43
* @key randomness
44
*/
45
public class TestNonexpanding {
46
47
private static final String ALGORITHM = "AES";
48
private static final String PROVIDER = "SunJCE";
49
private static final String[] MODES = { "ECb", "CbC", "OFB", "OFB150",
50
"cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32", "Cfb40", "cfB48",
51
"cfB56", "cfB64", "cfB72", "cfB80", "cfB88", "cfB96", "cfb104",
52
"cfB112", "cfB120", "GCM" };
53
private static final String PADDING = "NoPadding";
54
private static final int KEY_LENGTH = 128;
55
56
public static void main(String argv[]) throws Exception {
57
TestNonexpanding test = new TestNonexpanding();
58
for (String mode : MODES) {
59
test.runTest(ALGORITHM, mode, PADDING);
60
}
61
}
62
63
public void runTest(String algo, String mo, String pad) throws Exception {
64
Cipher ci = null;
65
SecretKey key = null;
66
try {
67
// Initialization
68
Random rdm = new Random();
69
byte[] plainText = new byte[128];
70
rdm.nextBytes(plainText);
71
72
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
73
74
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
75
kg.init(KEY_LENGTH);
76
key = kg.generateKey();
77
78
// encrypt
79
ci.init(Cipher.ENCRYPT_MODE, key);
80
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
81
int offset = ci.update(plainText, 0, plainText.length, cipherText,
82
0);
83
ci.doFinal(cipherText, offset);
84
85
// Comparison
86
if (!(plainText.length == cipherText.length)) {
87
// The result of encryption in GCM is a combination of an
88
// authentication tag and cipher text.
89
if (mo.equalsIgnoreCase("GCM")) {
90
GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
91
int cipherTextLength = cipherText.length - spec.getTLen()
92
/ 8;
93
if (plainText.length == cipherTextLength) {
94
return;
95
}
96
}
97
System.out.println("Original length: " + plainText.length);
98
System.out.println("Cipher text length: " + cipherText.length);
99
throw new RuntimeException("Test failed!");
100
}
101
} catch (NoSuchAlgorithmException e) {
102
//CFB7 and OFB150 are for negative testing
103
if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
104
System.out.println("Unexpected NoSuchAlgorithmException with mode: "
105
+ mo);
106
throw new RuntimeException("Test failed!");
107
}
108
} catch ( NoSuchProviderException | NoSuchPaddingException
109
| InvalidKeyException | InvalidParameterSpecException
110
| ShortBufferException | IllegalBlockSizeException
111
| BadPaddingException e) {
112
System.out.println("Test failed!");
113
throw e;
114
}
115
}
116
}
117
118