Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/SecureRandom/StrongSecureRandom.java
38853 views
1
/*
2
* Copyright (c) 2013, 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
/**
25
* @test
26
* @bug 6425477
27
* @summary Better support for generation of high entropy random numbers
28
* @run main/othervm StrongSecureRandom
29
*/
30
import java.security.*;
31
import java.util.*;
32
33
/**
34
* This test assumes that the standard Sun providers are installed.
35
*/
36
public class StrongSecureRandom {
37
38
private static String os = System.getProperty("os.name", "unknown");
39
40
private static void testDefaultEgd() throws Exception {
41
// No SecurityManager installed.
42
String s = Security.getProperty("securerandom.source");
43
44
System.out.println("Testing: default EGD: " + s);
45
if (!s.equals("file:/dev/random")) {
46
throw new Exception("Default is not 'file:/dev/random'");
47
}
48
}
49
50
private static void testSHA1PRNGImpl() throws Exception {
51
SecureRandom sr;
52
byte[] ba;
53
54
String urandom = "file:/dev/urandom";
55
56
System.out.println("Testing new SeedGenerator and EGD");
57
58
Security.setProperty("securerandom.source", urandom);
59
if (!Security.getProperty("securerandom.source").equals(urandom)) {
60
throw new Exception("Couldn't set securerandom.source");
61
}
62
63
/*
64
* Take out a large number of bytes in hopes of blocking.
65
* Don't expect this to happen, unless something is broken on Linux
66
*/
67
sr = SecureRandom.getInstance("SHA1PRNG");
68
if (!sr.getAlgorithm().equals("SHA1PRNG")) {
69
throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());
70
}
71
72
ba = sr.generateSeed(4096);
73
sr.nextBytes(ba);
74
sr.setSeed(ba);
75
}
76
77
private static void testNativePRNGImpls() throws Exception {
78
SecureRandom sr;
79
byte[] ba;
80
81
System.out.println("Testing new NativePRNGImpls");
82
83
if (os.startsWith("Windows")) {
84
System.out.println("Skip windows testing.");
85
return;
86
}
87
88
System.out.println(" Testing regular");
89
sr = SecureRandom.getInstance("NativePRNG");
90
if (!sr.getAlgorithm().equals("NativePRNG")) {
91
throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());
92
}
93
ba = sr.generateSeed(1);
94
sr.nextBytes(ba);
95
sr.setSeed(ba);
96
97
System.out.println(" Testing NonBlocking");
98
sr = SecureRandom.getInstance("NativePRNGNonBlocking");
99
if (!sr.getAlgorithm().equals("NativePRNGNonBlocking")) {
100
throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());
101
}
102
ba = sr.generateSeed(1);
103
sr.nextBytes(ba);
104
sr.setSeed(ba);
105
106
if (os.equals("Linux")) {
107
System.out.println("Skip Linux blocking test.");
108
return;
109
}
110
111
System.out.println(" Testing Blocking");
112
sr = SecureRandom.getInstance("NativePRNGBlocking");
113
if (!sr.getAlgorithm().equals("NativePRNGBlocking")) {
114
throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());
115
}
116
ba = sr.generateSeed(1);
117
sr.nextBytes(ba);
118
sr.setSeed(ba);
119
}
120
121
private static void testStrongInstance(boolean expected) throws Exception {
122
123
boolean result;
124
125
try {
126
SecureRandom.getInstanceStrong();
127
result = true;
128
} catch (NoSuchAlgorithmException e) {
129
result = false;
130
}
131
132
if (expected != result) {
133
throw new Exception("Received: " + result);
134
}
135
}
136
137
/*
138
* This test assumes that the standard providers are installed.
139
*/
140
private static void testProperty(String property, boolean expected)
141
throws Exception {
142
143
System.out.println("Testing: '" + property + "' " + expected);
144
145
Security.setProperty("securerandom.strongAlgorithms", property);
146
testStrongInstance(expected);
147
}
148
149
private static void testProperties() throws Exception {
150
// Sets securerandom.strongAlgorithms, and then tests various combos.
151
testProperty("", false);
152
153
testProperty("SHA1PRNG", true);
154
testProperty(" SHA1PRNG", true);
155
testProperty("SHA1PRNG ", true);
156
testProperty(" SHA1PRNG ", true);
157
158
// Impls are case-insenstive, providers are sensitive.
159
testProperty("SHA1PRNG:SUN", true);
160
testProperty("Sha1PRNG:SUN", true);
161
testProperty("SHA1PRNG:Sun", false);
162
163
testProperty(" SHA1PRNG:SUN", true);
164
testProperty("SHA1PRNG:SUN ", true);
165
testProperty(" SHA1PRNG:SUN ", true);
166
167
testProperty(" SHA1PRNG:SUn", false);
168
testProperty("SHA1PRNG:SUn ", false);
169
testProperty(" SHA1PRNG:SUn ", false);
170
171
testProperty(",,,SHA1PRNG", true);
172
testProperty(",,, SHA1PRNG", true);
173
testProperty(" , , ,SHA1PRNG ", true);
174
175
testProperty(",,,, SHA1PRNG ,,,", true);
176
testProperty(",,,, SHA1PRNG:SUN ,,,", true);
177
testProperty(",,,, SHA1PRNG:SUn ,,,", false);
178
179
testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUN", true);
180
testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUN", true);
181
testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUN", true);
182
183
testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUn", false);
184
testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUn", false);
185
testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUn", false);
186
187
testProperty(
188
" @#%,%$#:!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUN",
189
true);
190
testProperty(" @#%,%$#!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUn",
191
false);
192
}
193
194
/*
195
* Linux tends to block, so ignore anything that reads /dev/random.
196
*/
197
private static void handleLinuxRead(SecureRandom sr) throws Exception {
198
if (os.equals("Linux")) {
199
if (!sr.getAlgorithm().equalsIgnoreCase("NativePRNGBlocking")) {
200
sr.nextBytes(new byte[34]);
201
}
202
} else {
203
sr.nextBytes(new byte[34]);
204
sr.generateSeed(34);
205
sr.setSeed(new byte[34]);
206
}
207
}
208
209
/*
210
* This is duplicating stuff above, but just iterate over all impls
211
* just in case we missed something.
212
*/
213
private static void testAllImpls() throws Exception {
214
System.out.print("Testing: AllImpls: ");
215
216
Iterator<String> i = Security.getAlgorithms("SecureRandom").iterator();
217
218
while (i.hasNext()) {
219
String s = i.next();
220
System.out.print("/" + s);
221
SecureRandom sr = SecureRandom.getInstance(s);
222
223
handleLinuxRead(sr);
224
handleLinuxRead(sr);
225
}
226
System.out.println("/");
227
}
228
229
public static void main(String args[]) throws Exception {
230
testDefaultEgd();
231
testSHA1PRNGImpl();
232
testNativePRNGImpls();
233
testAllImpls();
234
235
// test default.
236
testStrongInstance(true);
237
testProperties();
238
}
239
}
240
241