Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/SecureRandom/DRBGAlg.java
41154 views
1
/*
2
* Copyright (c) 2016, 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 sun.security.provider.MoreDrbgParameters;
24
25
import java.security.DrbgParameters;
26
import java.security.NoSuchAlgorithmException;
27
import java.security.SecureRandom;
28
import java.security.SecureRandomParameters;
29
import java.security.Security;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Collections;
33
import java.util.List;
34
import java.util.stream.Collectors;
35
36
import static java.security.DrbgParameters.Capability.*;
37
38
/**
39
* @test
40
* @bug 8051408
41
* @modules java.base/sun.security.provider
42
* @summary make sure DRBG alg can be defined and instantiated freely
43
*/
44
public class DRBGAlg {
45
46
public static void main(String[] args) throws Exception {
47
48
check(null, "Hash_DRBG", "SHA-256", "reseed_only", ",128");
49
check("", "Hash_DRBG", "SHA-256", "reseed_only", ",128");
50
check("sha-256", "Hash_DRBG", "SHA-256", "reseed_only", ",128");
51
check("SHA-3");
52
check("hash_drbg", "Hash_DRBG", "SHA-256", "reseed_only", ",128");
53
check("hmac_drbg", "HMAC_DRBG", "SHA-256", "reseed_only", ",128");
54
check("ctr_drbg", "CTR_DRBG", "AES-", "reseed_only", ",128", "use_df");
55
56
// trying all permutations
57
checkPermutations(
58
Collections.emptyList(),
59
Arrays.asList("hash_drbg","sha-512","Pr_and_Reseed","192"),
60
"Hash_DRBG", "SHA-512", "pr_and_reseed", ",192");
61
62
check("Hash_DRBG,Hmac_DRBG");
63
check("SHA-224,SHA-256");
64
check("128,256");
65
check("none,reseed_only");
66
check("use_df,no_df");
67
check("Hash_DRBG,,SHA-256");
68
69
check(null, DrbgParameters.instantiation(112, PR_AND_RESEED, null),
70
"Hash_DRBG", "SHA-256", "pr_and_reseed", ",112");
71
check(null, DrbgParameters.instantiation(256, PR_AND_RESEED, null),
72
"Hash_DRBG", "SHA-256", "pr_and_reseed", ",256");
73
check(null, DrbgParameters.instantiation(384, PR_AND_RESEED, null));
74
check("sha-224", DrbgParameters.instantiation(112, PR_AND_RESEED, null),
75
"Hash_DRBG", "SHA-224", "pr_and_reseed", ",112");
76
check("sha-224", DrbgParameters.instantiation(256, PR_AND_RESEED, null));
77
check("hash_drbg,sha-512,Pr_and_Reseed,192",
78
DrbgParameters.instantiation(112, NONE, null),
79
"Hash_DRBG", "SHA-512", "reseed_only", ",112");
80
check("hash_drbg,sha-512,Pr_and_Reseed,192",
81
DrbgParameters.instantiation(-1, NONE, null),
82
"Hash_DRBG", "SHA-512", "reseed_only", ",192");
83
// getInstance params can be stronger than definition
84
check("hash_drbg,sha-256,None,112",
85
DrbgParameters.instantiation(192, PR_AND_RESEED, null),
86
"Hash_DRBG", "SHA-256", "pr_and_reseed", ",192");
87
88
check("hash_drbg,sha-224", new MoreDrbgParameters(
89
null, null, "sha-512", null, false,
90
DrbgParameters.instantiation(-1, NONE, null)),
91
"Hash_DRBG", "SHA-512");
92
check("hash_drbg,sha-224", new MoreDrbgParameters(
93
null, null, null, null, false,
94
DrbgParameters.instantiation(-1, NONE, null)),
95
"Hash_DRBG", "SHA-224");
96
check("hash_drbg", new MoreDrbgParameters(
97
null, "hmac_drbg", null, null, false,
98
DrbgParameters.instantiation(-1, NONE, null)),
99
"HMAC_DRBG", "SHA-256");
100
101
check("hash_drbg,sha-224", new MoreDrbgParameters(
102
null, null, "sha-3", null, false,
103
DrbgParameters.instantiation(-1, NONE, null)));
104
check("hash_drbg,sha-224", new MoreDrbgParameters(
105
null, "Unknown_DRBG", null, null, false,
106
DrbgParameters.instantiation(-1, NONE, null)));
107
}
108
109
/**
110
* Checks all permutatins of a config. This is a recursive method and
111
* should be called with checkPermutations(empty,config,expected).
112
*
113
* @param current the current chosen aspects
114
* @param remains the remaining
115
* @param expected the expected effective config
116
* @throws Exception when check fails
117
*/
118
private static void checkPermutations(List<String> current,
119
List<String> remains, String... expected) throws Exception {
120
if (remains.isEmpty()) {
121
check(current.stream().collect(Collectors.joining(",")), expected);
122
} else {
123
for (String r : remains) {
124
List<String> newCurrent = new ArrayList<>(current);
125
newCurrent.add(r);
126
List<String> newRemains = new ArrayList<>(remains);
127
newRemains.remove(r);
128
checkPermutations(newCurrent, newRemains, expected);
129
}
130
}
131
}
132
133
/**
134
* Checks DRBG definition for getInstance(alg, params).
135
*
136
* @param define DRBG
137
* @param params getInstance request (null if none)
138
* @param expected expected actual instantiate params, empty if should fail
139
*/
140
static void check(String define, SecureRandomParameters params,
141
String... expected) throws Exception {
142
System.out.println("Testing " + define + " with " + params + "...");
143
String old = Security.getProperty("securerandom.drbg.config");
144
if (define != null) {
145
Security.setProperty("securerandom.drbg.config", define);
146
}
147
try {
148
String result = params != null ?
149
SecureRandom.getInstance("DRBG", params).toString() :
150
SecureRandom.getInstance("DRBG").toString();
151
System.out.println("Result " + result);
152
if (expected.length == 0) {
153
throw new Exception("should fail");
154
}
155
for (String s : expected) {
156
if (!result.contains(s)) {
157
throw new Exception(result);
158
}
159
}
160
} catch (NoSuchAlgorithmException e) {
161
System.out.println("Result NSAE");
162
if (expected.length > 0) {
163
throw e;
164
}
165
} finally {
166
Security.setProperty("securerandom.drbg.config", old);
167
}
168
}
169
170
/**
171
* Checks DRBG definition for getInstance(alg).
172
*
173
* @param define DRBG
174
* @param expected expected actual instantiate params, empty if should fail
175
*/
176
static void check(String define, String... expected) throws Exception {
177
check(define, null, expected);
178
}
179
}
180
181