Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/CICODESFuncTest.java
41155 views
1
/*
2
* Copyright (c) 2007, 2015, 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
import static java.lang.System.out;
24
25
import java.io.ByteArrayInputStream;
26
import java.io.ByteArrayOutputStream;
27
import java.io.IOException;
28
import java.security.GeneralSecurityException;
29
import java.security.NoSuchAlgorithmException;
30
import java.security.Provider;
31
import java.security.Security;
32
import java.security.spec.AlgorithmParameterSpec;
33
import java.util.Arrays;
34
import javax.crypto.CipherInputStream;
35
import javax.crypto.CipherOutputStream;
36
import javax.crypto.SecretKey;
37
import javax.crypto.Cipher;
38
import javax.crypto.KeyGenerator;
39
import javax.crypto.spec.IvParameterSpec;
40
41
/*
42
* @test
43
* @bug 8048604
44
* @summary to verify cipherInputStream and cipherInputStream cipher function
45
* @run main CICODESFuncTest
46
*/
47
public class CICODESFuncTest {
48
/**
49
* Algorithms name.
50
*/
51
private static final String[] ALGORITHMS = { "DES", "DESede", "Blowfish" };
52
private static final String[] MODES = { "ECB", "CBC", "CFB", "CFB24",
53
"CFB32", "CFB40", "CFB72", "OFB", "OFB20", "OFB48", "OFB56",
54
"OFB64", "PCBC" };
55
/**
56
* Padding mode.
57
*/
58
private static final String[] PADDINGS = { "noPadding", "pkcs5padding" };
59
/**
60
* Plain text length.
61
*/
62
private static final int TEXT_LENGTH = 80;
63
/**
64
* Initialization vector length.
65
*/
66
private static final int IV_LENGTH = 8;
67
68
public static void main(String[] args) throws Exception {
69
Provider provider = Security.getProvider("SunJCE");
70
if (provider == null) {
71
throw new RuntimeException("SunJCE provider does not exist.");
72
}
73
for (String algorithm : ALGORITHMS) {
74
for (String mode : MODES) {
75
// We only test noPadding and pkcs5padding for CFB72, OFB20, ECB
76
// PCBC and CBC. Otherwise test noPadding only.
77
int padKinds = 1;
78
if (mode.equalsIgnoreCase("CFB72")
79
|| mode.equalsIgnoreCase("OFB20")
80
|| mode.equalsIgnoreCase("ECB")
81
|| mode.equalsIgnoreCase("PCBC")
82
|| mode.equalsIgnoreCase("CBC")) {
83
padKinds = PADDINGS.length;
84
}
85
// PKCS5padding is meaningful only for ECB, CBC, PCBC
86
for (int k = 0; k < padKinds; k++) {
87
for (ReadModel readMode : ReadModel.values()) {
88
runTest(provider, algorithm, mode, PADDINGS[k], readMode);
89
}
90
}
91
}
92
}
93
}
94
95
private static void runTest(Provider p, String algo, String mo, String pad,
96
ReadModel whichRead) throws GeneralSecurityException, IOException {
97
// Do initialization
98
byte[] plainText = TestUtilities.generateBytes(TEXT_LENGTH);
99
byte[] iv = TestUtilities.generateBytes(IV_LENGTH);
100
AlgorithmParameterSpec aps = new IvParameterSpec(iv);
101
try {
102
KeyGenerator kg = KeyGenerator.getInstance(algo, p);
103
out.println(algo + "/" + mo + "/" + pad + "/" + whichRead);
104
SecretKey key = kg.generateKey();
105
Cipher ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
106
if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
107
throw new RuntimeException(
108
"NoSuchAlgorithmException not throw when mode"
109
+ " is CFB72 or OFB20");
110
}
111
Cipher ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
112
if ("ECB".equalsIgnoreCase(mo)) {
113
ci1.init(Cipher.ENCRYPT_MODE, key);
114
ci2.init(Cipher.DECRYPT_MODE, key);
115
} else {
116
ci1.init(Cipher.ENCRYPT_MODE, key, aps);
117
ci2.init(Cipher.DECRYPT_MODE, key, aps);
118
}
119
ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
120
try (CipherInputStream cInput
121
= new CipherInputStream(
122
new ByteArrayInputStream(plainText), ci1);
123
CipherOutputStream ciOutput
124
= new CipherOutputStream(baOutput, ci2);) {
125
// Read from the input and write to the output using 2 types
126
// of buffering : byte[] and int
127
whichRead.read(cInput, ciOutput, ci1, plainText.length);
128
}
129
// Verify input and output are same.
130
if (!Arrays.equals(plainText, baOutput.toByteArray())) {
131
throw new RuntimeException("Test failed due to compare fail ");
132
}
133
} catch (NoSuchAlgorithmException nsaEx) {
134
if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
135
out.println("NoSuchAlgorithmException is expected for CFB72 and OFB20");
136
} else {
137
throw new RuntimeException("Unexpected exception testing: "
138
+ algo + "/" + mo + "/" + pad + "/" + whichRead, nsaEx);
139
}
140
}
141
}
142
}
143
144