Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/DRBG.java
41159 views
1
/*
2
* Copyright (c) 2016, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.provider;
27
28
import java.io.IOException;
29
import java.security.AccessController;
30
import java.security.DrbgParameters;
31
import java.security.PrivilegedAction;
32
import java.security.SecureRandomParameters;
33
import java.security.SecureRandomSpi;
34
import java.security.Security;
35
import java.util.Locale;
36
import static java.security.DrbgParameters.Capability.*;
37
38
/**
39
* Implement the "SecureRandom.DRBG" algorithm.
40
*
41
* About the default "securerandom.drbg.config" value:
42
*
43
* The default value in java.security is set to "". This is because
44
* the default values of different aspects are dependent (For example,
45
* strength depends on algorithm) and if we write a full string there
46
* it will be difficult to modify one and keep all others legal.
47
*
48
* When changing default values, touch all places including:
49
*
50
* 1. comments of the security property in java.security
51
* 2. Default mech, cap, usedf set in this class
52
* 3. Default algorithm set in final implementation of each mech
53
* 4. Default strength set in AbstractDrbg, but the effective
54
* value can be smaller if an algorithm does not support it.
55
*
56
* The default value is also mentioned in the @implNote part of
57
* {@link DrbgParameters} class.
58
*/
59
public final class DRBG extends SecureRandomSpi {
60
61
private static final String PROP_NAME = "securerandom.drbg.config";
62
63
@java.io.Serial
64
private static final long serialVersionUID = 9L;
65
66
private transient AbstractDrbg impl;
67
68
/**
69
* @serial
70
*/
71
private final MoreDrbgParameters mdp;
72
73
public DRBG(SecureRandomParameters params) {
74
75
// All parameters at unset status (null or -1).
76
77
// Configurable with the "securerandom.drbg.config" security property
78
String mech = null;
79
Boolean usedf = null;
80
String algorithm = null;
81
82
// Default instantiate parameters also configurable with
83
// "securerandom.drbg.config", and can be changed with params
84
// in getInstance("drbg", params)
85
int strength = -1;
86
DrbgParameters.Capability cap = null;
87
byte[] ps = null;
88
89
// Not configurable with public interfaces, but is a part of
90
// MoreDrbgParameters
91
EntropySource es = null;
92
byte[] nonce = null;
93
94
// Can be configured with a security property
95
96
@SuppressWarnings("removal")
97
String config = AccessController.doPrivileged((PrivilegedAction<String>)
98
() -> Security.getProperty(PROP_NAME));
99
100
if (config != null && !config.isEmpty()) {
101
for (String part : config.split(",")) {
102
part = part.trim();
103
switch (part.toLowerCase(Locale.ROOT)) {
104
case "":
105
throw new IllegalArgumentException(
106
"aspect in " + PROP_NAME + " cannot be empty");
107
case "pr_and_reseed":
108
checkTwice(cap != null, "capability");
109
cap = PR_AND_RESEED;
110
break;
111
case "reseed_only":
112
checkTwice(cap != null, "capability");
113
cap = RESEED_ONLY;
114
break;
115
case "none":
116
checkTwice(cap != null, "capability");
117
cap = NONE;
118
break;
119
case "hash_drbg":
120
case "hmac_drbg":
121
case "ctr_drbg":
122
checkTwice(mech != null, "mechanism name");
123
mech = part;
124
break;
125
case "no_df":
126
checkTwice(usedf != null, "usedf flag");
127
usedf = false;
128
break;
129
case "use_df":
130
checkTwice(usedf != null, "usedf flag");
131
usedf = true;
132
break;
133
default:
134
// For all other parts of the property, it is
135
// either an algorithm name or a strength
136
try {
137
int tmp = Integer.parseInt(part);
138
if (tmp < 0) {
139
throw new IllegalArgumentException(
140
"strength in " + PROP_NAME +
141
" cannot be negative: " + part);
142
}
143
checkTwice(strength >= 0, "strength");
144
strength = tmp;
145
} catch (NumberFormatException e) {
146
checkTwice(algorithm != null, "algorithm name");
147
algorithm = part;
148
}
149
}
150
}
151
}
152
153
// Can be updated by params
154
155
if (params != null) {
156
// MoreDrbgParameters is used for testing.
157
if (params instanceof MoreDrbgParameters) {
158
MoreDrbgParameters m = (MoreDrbgParameters) params;
159
params = DrbgParameters.instantiation(m.strength,
160
m.capability, m.personalizationString);
161
162
// No need to check null for es and nonce, they are still null
163
es = m.es;
164
nonce = m.nonce;
165
166
if (m.mech != null) {
167
mech = m.mech;
168
}
169
if (m.algorithm != null) {
170
algorithm = m.algorithm;
171
}
172
usedf = m.usedf;
173
}
174
if (params instanceof DrbgParameters.Instantiation) {
175
DrbgParameters.Instantiation dp =
176
(DrbgParameters.Instantiation) params;
177
178
// ps is still null by now
179
ps = dp.getPersonalizationString();
180
181
int tmp = dp.getStrength();
182
if (tmp != -1) {
183
strength = tmp;
184
}
185
cap = dp.getCapability();
186
} else {
187
throw new IllegalArgumentException("Unsupported params: "
188
+ params.getClass());
189
}
190
}
191
192
// Hardcoded defaults.
193
// Remember to sync with "securerandom.drbg.config" in java.security.
194
195
if (cap == null) {
196
cap = NONE;
197
}
198
if (mech == null) {
199
mech = "Hash_DRBG";
200
}
201
if (usedf == null) {
202
usedf = true;
203
}
204
205
mdp = new MoreDrbgParameters(
206
es, mech, algorithm, nonce, usedf,
207
DrbgParameters.instantiation(strength, cap, ps));
208
209
createImpl();
210
}
211
212
private void createImpl() {
213
switch (mdp.mech.toLowerCase(Locale.ROOT)) {
214
case "hash_drbg":
215
impl = new HashDrbg(mdp);
216
break;
217
case "hmac_drbg":
218
impl = new HmacDrbg(mdp);
219
break;
220
case "ctr_drbg":
221
impl = new CtrDrbg(mdp);
222
break;
223
default:
224
throw new IllegalArgumentException("Unsupported mech: " + mdp.mech);
225
}
226
}
227
228
@Override
229
protected void engineSetSeed(byte[] seed) {
230
impl.engineSetSeed(seed);
231
}
232
233
@Override
234
protected void engineNextBytes(byte[] bytes) {
235
impl.engineNextBytes(bytes);
236
}
237
238
@Override
239
protected byte[] engineGenerateSeed(int numBytes) {
240
return impl.engineGenerateSeed(numBytes);
241
}
242
243
@Override
244
protected void engineNextBytes(
245
byte[] bytes, SecureRandomParameters params) {
246
impl.engineNextBytes(bytes, params);
247
}
248
249
@Override
250
protected void engineReseed(SecureRandomParameters params) {
251
impl.engineReseed(params);
252
}
253
254
@Override
255
protected SecureRandomParameters engineGetParameters() {
256
return impl.engineGetParameters();
257
}
258
259
@Override
260
public String toString() {
261
return impl.toString();
262
}
263
264
/**
265
* Ensures an aspect is not set more than once.
266
*
267
* @param flag true if set more than once
268
* @param name the name of aspect shown in IAE
269
* @throws IllegalArgumentException if it happens
270
*/
271
private static void checkTwice(boolean flag, String name) {
272
if (flag) {
273
throw new IllegalArgumentException(name
274
+ " cannot be provided more than once in " + PROP_NAME);
275
}
276
}
277
278
@java.io.Serial
279
private void readObject(java.io.ObjectInputStream s)
280
throws IOException, ClassNotFoundException {
281
s.defaultReadObject();
282
if (mdp.mech == null) {
283
throw new IllegalArgumentException("Input data is corrupted");
284
}
285
createImpl();
286
}
287
}
288
289