Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/KeyGenerator/TestKGParity.java
41149 views
1
/*
2
* Copyright (c) 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
24
import java.io.PrintStream;
25
import java.lang.String;
26
import java.lang.System;
27
import java.security.Provider;
28
import java.security.SecureRandom;
29
import java.security.Security;
30
import javax.crypto.KeyGenerator;
31
import static java.lang.System.out;
32
33
/*
34
* @test
35
* @bug 8048607
36
* @compile ../../../com/sun/crypto/provider/Cipher/DES/TestUtility.java
37
* @run main TestKGParity
38
* @summary Test key generation of DES and DESEDE
39
* @key randomness
40
*/
41
public class TestKGParity {
42
43
private static final String[] ALGORITHM_ARR = {
44
"deS", "DesEDE"
45
};
46
47
public static void main(String argv[]) throws Exception {
48
49
TestKGParity test = new TestKGParity();
50
test.run();
51
}
52
53
private void run() throws Exception {
54
Provider[] providers = Security.getProviders();
55
for (Provider p : providers) {
56
String prvName = p.getName();
57
if (prvName.startsWith("SunJCE")
58
|| prvName.startsWith("SunPKCS11-")) {
59
for (String algorithm : ALGORITHM_ARR) {
60
if (!runTest(p, algorithm)) {
61
throw new RuntimeException(
62
"Test failed with provider/algorithm:"
63
+ p.getName() + "/" + algorithm);
64
} else {
65
out.println("Test passed with provider/algorithm:"
66
+ p.getName() + "/" + algorithm);
67
}
68
}
69
}
70
}
71
}
72
73
public boolean runTest(Provider p, String algo) throws Exception {
74
byte[] keyValue = null;
75
try {
76
// Initialization
77
SecureRandom sRdm = new SecureRandom();
78
KeyGenerator kg = KeyGenerator.getInstance(algo, p);
79
kg.init(sRdm);
80
81
// Generate a SecretKey and retrieve its value
82
keyValue = kg.generateKey().getEncoded();
83
84
// Verify its parity in the unit of byte
85
for (int i = 0; i < keyValue.length; i++) {
86
if (!checkParity(keyValue[i])) {
87
out.println("Testing: "
88
+ p.getName()
89
+ "/"
90
+ algo
91
+ " failed when verify its parity in the unit of byte:"
92
+ TestUtility.hexDump(keyValue, i));
93
return false;
94
}
95
}
96
return true;
97
} catch (Exception ex) {
98
out.println("Testing: " + p.getName() + "/" + algo
99
+ " failed with unexpected exception");
100
ex.printStackTrace();
101
throw ex;
102
}
103
}
104
105
private boolean checkParity(byte keyByte) {
106
boolean even = false;
107
byte[] PARITY_BIT_MASK = {
108
(byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,
109
(byte) 0x04, (byte) 0x02, (byte) 0x01
110
};
111
112
for (int i = 0; i < 7; i++) {
113
if ((keyByte & PARITY_BIT_MASK[i]) > 0) {
114
even = !even;
115
}
116
}
117
if (keyByte < 0) {
118
even = !even;
119
}
120
121
return even;
122
}
123
}
124
125