Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/SecureRandom/CommonSeeder.java
41154 views
1
/*
2
* Copyright (c) 2016, 2018, 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.DrbgParameters;
25
import java.security.SecureRandom;
26
import java.security.Security;
27
28
import sun.security.provider.SeedGenerator;
29
/**
30
* @test
31
* @bug 8051408 8202608
32
* @modules java.base/sun.security.provider
33
* @build java.base/sun.security.provider.SeedGenerator
34
* @run main/othervm CommonSeeder
35
* @summary check entropy reading of DRBGs
36
*/
37
public class CommonSeeder {
38
39
public static void main(String[] args) throws Exception {
40
41
byte[] result = new byte[10];
42
43
// Use patched SeedGenerator in java.base/sun/security/provider/.
44
45
// Nothing happened yet
46
SeedGenerator.checkUsage(0);
47
48
SecureRandom sr;
49
sr = SecureRandom.getInstance("DRBG");
50
51
// No entropy reading if only getInstance
52
SeedGenerator.checkUsage(0);
53
54
// Entropy is read at 1st nextBytes of the 1st DRBG
55
sr.nextInt();
56
SeedGenerator.checkUsage(1);
57
58
for (String mech : new String[]{"Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
59
System.out.println("Testing " + mech + "...");
60
61
// DRBG with pr_false will never read entropy again no matter
62
// if nextBytes or reseed is called.
63
64
Security.setProperty("securerandom.drbg.config", mech);
65
sr = SecureRandom.getInstance("DRBG");
66
sr.nextInt();
67
sr.reseed();
68
SeedGenerator.checkUsage(0);
69
70
// DRBG with pr_true always read from default entropy, and
71
// its nextBytes always reseed itself
72
73
Security.setProperty("securerandom.drbg.config",
74
mech + ",pr_and_reseed");
75
sr = SecureRandom.getInstance("DRBG");
76
77
sr.nextInt();
78
SeedGenerator.checkUsage(2); // one instantiate, one reseed
79
sr.nextInt();
80
SeedGenerator.checkUsage(1); // one reseed in nextBytes
81
sr.reseed();
82
SeedGenerator.checkUsage(1); // one reseed
83
sr.nextBytes(result, DrbgParameters.nextBytes(-1, false, null));
84
SeedGenerator.checkUsage(0); // pr_false for this call
85
sr.nextBytes(result, DrbgParameters.nextBytes(-1, true, null));
86
SeedGenerator.checkUsage(1); // pr_true for this call
87
sr.reseed(DrbgParameters.reseed(true, null));
88
SeedGenerator.checkUsage(1); // reseed from es
89
sr.reseed(DrbgParameters.reseed(false, null));
90
SeedGenerator.checkUsage(0); // reseed from AbstractDrbg.SeederHolder.seeder
91
}
92
}
93
}
94
95