37796
|
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. 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 java.security;
|
|
27 |
|
|
28 |
import java.util.Locale;
|
|
29 |
import java.util.Objects;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* This class specifies the parameters used by a DRBG (Deterministic
|
|
33 |
* Random Bit Generator).
|
|
34 |
* <p>
|
|
35 |
* According to
|
|
36 |
* <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf">
|
|
37 |
* NIST Special Publication 800-90A Revision 1, Recommendation for Random
|
|
38 |
* Number Generation Using Deterministic Random Bit Generators</a> (800-90Ar1),
|
|
39 |
* <blockquote>
|
|
40 |
* A DRBG is based on a DRBG mechanism as specified in this Recommendation
|
|
41 |
* and includes a source of randomness. A DRBG mechanism uses an algorithm
|
|
42 |
* (i.e., a DRBG algorithm) that produces a sequence of bits from an initial
|
|
43 |
* value that is determined by a seed that is determined from the output of
|
|
44 |
* the randomness source."
|
|
45 |
* </blockquote>
|
|
46 |
* <p>
|
|
47 |
* The 800-90Ar1 specification allows for a variety of DRBG implementation
|
|
48 |
* choices, such as:
|
|
49 |
* <ul>
|
|
50 |
* <li> an entropy source,
|
|
51 |
* <li> a DRBG mechanism (for example, Hash_DRBG),
|
|
52 |
* <li> a DRBG algorithm (for example, SHA-256 for Hash_DRBG and AES-256
|
|
53 |
* for CTR_DRBG. Please note that it is not the algorithm used in
|
|
54 |
* {@link SecureRandom#getInstance}, which we will call a
|
|
55 |
* <em>SecureRandom algorithm</em> below),
|
|
56 |
* <li> optionally features, including prediction resistance
|
|
57 |
* and reseeding supports.
|
|
58 |
* <li> highest security strength.
|
|
59 |
* </ul>
|
|
60 |
* <p>
|
|
61 |
* These choices are set in each implementation and are not directly
|
|
62 |
* managed by the {@code SecureRandom} API. Check your DRBG provider's
|
|
63 |
* documentation to find an appropriate implementation for the situation.
|
|
64 |
* <p>
|
|
65 |
* On the other hand, the 800-90Ar1 specification does have some configurable
|
|
66 |
* options, such as:
|
|
67 |
* <ul>
|
|
68 |
* <li> required security strength,
|
|
69 |
* <li> if prediction resistance is required,
|
|
70 |
* <li> personalization string and additional input.
|
|
71 |
* </ul>
|
|
72 |
* <p>
|
|
73 |
* A DRBG instance can be instantiated with parameters from an
|
|
74 |
* {@link DrbgParameters.Instantiation} object and other information
|
|
75 |
* (for example, the nonce, which is not managed by this API). This maps
|
|
76 |
* to the {@code Instantiate_function} defined in NIST SP 800-90Ar1.
|
|
77 |
* <p>
|
|
78 |
* A DRBG instance can be reseeded with parameters from a
|
|
79 |
* {@link DrbgParameters.Reseed} object. This maps to the
|
|
80 |
* {@code Reseed_function} defined in NIST SP 800-90Ar1. Calling
|
|
81 |
* {@link SecureRandom#reseed()} is equivalent to calling
|
|
82 |
* {@link SecureRandom#reseed(SecureRandomParameters)} with the effective
|
|
83 |
* instantiated prediction resistance flag (as returned by
|
|
84 |
* {@link SecureRandom#getParameters()}) with no additional input.
|
|
85 |
* <p>
|
|
86 |
* A DRBG instance generates data with additional parameters from a
|
|
87 |
* {@link DrbgParameters.NextBytes} object. This maps to the
|
|
88 |
* {@code Generate_function} defined in NIST SP 800-90Ar1. Calling
|
|
89 |
* {@link SecureRandom#nextBytes(byte[])} is equivalent to calling
|
|
90 |
* {@link SecureRandom#nextBytes(byte[], SecureRandomParameters)}
|
|
91 |
* with the effective instantiated strength and prediction resistance flag
|
|
92 |
* (as returned by {@link SecureRandom#getParameters()}) with no
|
|
93 |
* additional input.
|
|
94 |
* <p>
|
|
95 |
* A DRBG should be implemented as a subclass of {@link SecureRandomSpi}.
|
|
96 |
* It is recommended that the implementation contain the 1-arg
|
|
97 |
* {@linkplain SecureRandomSpi#SecureRandomSpi(SecureRandomParameters) constructor}
|
|
98 |
* that takes a {@code DrbgParameters.Instantiation} argument. If implemented
|
|
99 |
* this way, this implementation can be chosen by any
|
|
100 |
* {@code SecureRandom.getInstance()} method. If it is chosen by a
|
|
101 |
* {@code SecureRandom.getInstance()} with a {@link SecureRandomParameters}
|
|
102 |
* parameter, the parameter is passed into this constructor. If it is chosen
|
|
103 |
* by a {@code SecureRandom.getInstance()} without a
|
|
104 |
* {@code SecureRandomParameters} parameter, the constructor is called with
|
|
105 |
* a {@code null} argument and the implementation should choose its own
|
|
106 |
* parameters. Its {@link SecureRandom#getParameters()} must always return a
|
|
107 |
* non-null effective {@code DrbgParameters.Instantiation} object that reflects
|
|
108 |
* how the DRBG is actually instantiated. A caller can use this information
|
|
109 |
* to determine whether a {@code SecureRandom} object is a DRBG and what
|
|
110 |
* features it supports. Please note that the returned value does not
|
|
111 |
* necessarily equal to the {@code DrbgParameters.Instantiation} object passed
|
|
112 |
* into the {@code SecureRandom.getInstance()} call. For example,
|
|
113 |
* the requested capability can be {@link DrbgParameters.Capability#NONE}
|
|
114 |
* but the effective value can be {@link DrbgParameters.Capability#RESEED_ONLY}
|
|
115 |
* if the implementation supports reseeding. The implementation must implement
|
|
116 |
* the {@link SecureRandomSpi#engineNextBytes(byte[], SecureRandomParameters)}
|
|
117 |
* method which takes a {@code DrbgParameters.NextBytes} parameter. Unless
|
|
118 |
* the result of {@link SecureRandom#getParameters()} has its
|
|
119 |
* {@linkplain DrbgParameters.Instantiation#getCapability() capability} being
|
|
120 |
* {@link Capability#NONE NONE}, it must implement
|
|
121 |
* {@link SecureRandomSpi#engineReseed(SecureRandomParameters)} which takes
|
|
122 |
* a {@code DrbgParameters.Reseed} parameter.
|
|
123 |
* <p>
|
|
124 |
* On the other hand, if a DRBG implementation does not contain a constructor
|
|
125 |
* that has an {@code DrbgParameters.Instantiation} argument (not recommended),
|
|
126 |
* it can only be chosen by a {@code SecureRandom.getInstance()} without
|
|
127 |
* a {@code SecureRandomParameters} parameter, but will not be chosen if
|
|
128 |
* a {@code getInstance} method with a {@code SecureRandomParameters} parameter
|
|
129 |
* is called. If implemented this way, its {@link SecureRandom#getParameters()}
|
|
130 |
* must return {@code null}, and it does not need to implement either
|
|
131 |
* {@link SecureRandomSpi#engineNextBytes(byte[], SecureRandomParameters)}
|
|
132 |
* or {@link SecureRandomSpi#engineReseed(SecureRandomParameters)}.
|
|
133 |
* <p>
|
|
134 |
* A DRBG might reseed itself automatically if the seed period is bigger
|
|
135 |
* than the maximum seed life defined by the DRBG mechanism.
|
|
136 |
* <p>
|
|
137 |
* A DRBG implementation should support serialization and deserialization
|
|
138 |
* by retaining the configuration and effective parameters, but the internal
|
|
139 |
* state must not be serialized and the deserialized object must be
|
|
140 |
* reinstantiated.
|
|
141 |
* <p>
|
|
142 |
* Examples:
|
|
143 |
* <blockquote><pre>
|
|
144 |
* SecureRandom drbg;
|
|
145 |
* byte[] buffer = new byte[32];
|
|
146 |
*
|
|
147 |
* // Any DRBG is OK
|
|
148 |
* drbg = SecureRandom.getInstance("DRBG");
|
|
149 |
* drbg.nextBytes(buffer);
|
|
150 |
*
|
|
151 |
* SecureRandomParameters params = drbg.getParameters();
|
|
152 |
* if (params instanceof DrbgParameters.Instantiation) {
|
|
153 |
* DrbgParameters.Instantiation ins = (DrbgParameters.Instantiation) params;
|
|
154 |
* if (ins.getCapability().supportsReseeding()) {
|
|
155 |
* drbg.reseed();
|
|
156 |
* }
|
|
157 |
* }
|
|
158 |
*
|
|
159 |
* // The following call requests a weak DRBG instance. It is only
|
|
160 |
* // guaranteed to support 112 bits of security strength.
|
|
161 |
* drbg = SecureRandom.getInstance("DRBG",
|
|
162 |
* DrbgParameters.instantiation(112, NONE, null));
|
|
163 |
*
|
|
164 |
* // Both the next two calls will likely fail, because drbg could be
|
|
165 |
* // instantiated with a smaller strength with no prediction resistance
|
|
166 |
* // support.
|
|
167 |
* drbg.nextBytes(buffer,
|
|
168 |
* DrbgParameters.nextBytes(256, false, "more".getBytes()));
|
|
169 |
* drbg.nextBytes(buffer,
|
|
170 |
* DrbgParameters.nextBytes(112, true, "more".getBytes()));
|
|
171 |
*
|
|
172 |
* // The following call requests a strong DRBG instance, with a
|
|
173 |
* // personalization string. If it successfully returns an instance,
|
|
174 |
* // that instance is guaranteed to support 256 bits of security strength
|
|
175 |
* // with prediction resistance available.
|
|
176 |
* drbg = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(
|
|
177 |
* 256, PR_AND_RESEED, "hello".getBytes()));
|
|
178 |
*
|
|
179 |
* // Prediction resistance is not requested in this single call,
|
|
180 |
* // but an additional input is used.
|
|
181 |
* drbg.nextBytes(buffer,
|
|
182 |
* DrbgParameters.nextBytes(-1, false, "more".getBytes()));
|
|
183 |
*
|
|
184 |
* // Same for this call.
|
|
185 |
* drbg.reseed(DrbgParameters.reseed(false, "extra".getBytes()));</pre>
|
|
186 |
* </blockquote>
|
|
187 |
*
|
|
188 |
* @implSpec
|
|
189 |
* By convention, a provider should name its primary DRBG implementation
|
|
190 |
* with the <a href=
|
|
191 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
192 |
* standard {@code SecureRandom} algorithm name</a> "DRBG".
|
|
193 |
*
|
|
194 |
* @implNote
|
|
195 |
* The following notes apply to the "DRBG" implementation in the SUN provider
|
|
196 |
* of the JDK reference implementation.
|
|
197 |
* <p>
|
|
198 |
* This implementation supports the Hash_DRBG and HMAC_DRBG mechanisms with
|
|
199 |
* DRBG algorithm SHA-1, SHA-224, SHA-512/224, SHA-256, SHA-512/256,
|
|
200 |
* SHA-384 and SHA-512, and CTR_DRBG (both using derivation function and
|
|
201 |
* not using derivation function) with DRBG algorithm 3KeyTDEA
|
|
202 |
* (also known as DESede in JCE), AES-128, AES-192 and AES-256.
|
|
203 |
* <p>
|
|
204 |
* The mechanism name and DRBG algorithm name are determined by the
|
|
205 |
* {@linkplain Security#getProperty(String) security property}
|
|
206 |
* {@code securerandom.drbg.config}. The default choice is Hash_DRBG
|
|
207 |
* with SHA-256.
|
|
208 |
* <p>
|
|
209 |
* For each combination, the security strength can be requested from 112
|
|
210 |
* up to the highest strength it supports. Both reseeding and prediction
|
|
211 |
* resistance are supported.
|
|
212 |
* <p>
|
|
213 |
* Personalization string is supported through the
|
|
214 |
* {@link DrbgParameters.Instantiation} class and additional input is supported
|
|
215 |
* through the {@link DrbgParameters.NextBytes} and
|
|
216 |
* {@link DrbgParameters.Reseed} classes.
|
|
217 |
* <p>
|
|
218 |
* If a DRBG is not instantiated with a {@link DrbgParameters.Instantiation}
|
|
219 |
* object explicitly, this implementation instantiates it with a default
|
|
220 |
* requested strength of 128 bits (112 bits for CTR_DRBG with 3KeyTDEA),
|
|
221 |
* no prediction resistance request, and no personalization string.
|
|
222 |
* These default instantiation parameters can also be customized with
|
|
223 |
* the {@code securerandom.drbg.config} security property.
|
|
224 |
* <p>
|
|
225 |
* This implementation reads fresh entropy from the system default entropy
|
|
226 |
* source determined by the security property {@code securerandom.source}.
|
|
227 |
* <p>
|
|
228 |
* Calling {@link SecureRandom#generateSeed(int)} will directly read
|
|
229 |
* from this system default entropy source.
|
|
230 |
* <p>
|
|
231 |
* This implementation has passed all tests included in the 20151104 version of
|
|
232 |
* <a href="http://csrc.nist.gov/groups/STM/cavp/documents/drbg/drbgtestvectors.zip">
|
|
233 |
* The DRBG Test Vectors</a>.
|
|
234 |
*
|
|
235 |
* @since 9
|
|
236 |
*/
|
|
237 |
public class DrbgParameters {
|
|
238 |
|
|
239 |
private DrbgParameters() {
|
|
240 |
// This class should not be instantiated
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* The reseedable and prediction resistance capabilities of a DRBG.
|
|
245 |
* <p>
|
|
246 |
* When this object is passed to a {@code SecureRandom.getInstance()} call,
|
|
247 |
* it is the requested minimum capability. When it's returned from
|
|
248 |
* {@code SecureRandom.getParameters()}, it is the effective capability.
|
|
249 |
* <p>
|
|
250 |
* Please note that while the {@code Instantiate_function} defined in
|
|
251 |
* NIST SP 800-90Ar1 only includes a {@code prediction_resistance_flag}
|
|
252 |
* parameter, the {@code Capability} type includes an extra value
|
|
253 |
* {@link #RESEED_ONLY} because reseeding is an optional function.
|
|
254 |
* If {@code NONE} is used in an {@code Instantiation} object in calling the
|
|
255 |
* {@code SecureRandom.getInstance} method, the returned DRBG instance
|
|
256 |
* is not guaranteed to support reseeding. If {@code RESEED_ONLY} or
|
|
257 |
* {@code PR_AND_RESEED} is used, the instance must support reseeding.
|
|
258 |
* <p>
|
|
259 |
* The table below lists possible effective values if a certain
|
|
260 |
* capability is requested, i.e.
|
|
261 |
* <blockquote><pre>
|
|
262 |
* Capability requested = ...;
|
|
263 |
* SecureRandom s = SecureRandom.getInstance("DRBG",
|
|
264 |
* DrbgParameters(-1, requested, null));
|
|
265 |
* Capability effective = ((DrbgParametes.Initiate) s.getParameters())
|
|
266 |
* .getCapability();</pre>
|
|
267 |
* </blockquote>
|
|
268 |
* <table border=1 summary="requested and effective capabilities">
|
|
269 |
* <tr>
|
|
270 |
* <th>Requested Value</th>
|
|
271 |
* <th>Possible Effective Values</th>
|
|
272 |
* </tr>
|
|
273 |
* <tr><td>NONE</td><td>NONE, RESEED_ONLY, PR_AND_RESEED</td></tr>
|
|
274 |
* <tr><td>RESEED_ONLY</td><td>RESEED_ONLY, PR_AND_RESEED</td></tr>
|
|
275 |
* <tr><td>PR_AND_RESEED</td><td>PR_AND_RESEED</td></tr>
|
|
276 |
* </table>
|
|
277 |
* <p>
|
|
278 |
* A DRBG implementation supporting prediction resistance must also
|
|
279 |
* support reseeding.
|
|
280 |
*
|
|
281 |
* @since 9
|
|
282 |
*/
|
|
283 |
public enum Capability {
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Both prediction resistance and reseed.
|
|
287 |
*/
|
|
288 |
PR_AND_RESEED,
|
|
289 |
|
|
290 |
/**
|
|
291 |
* Reseed but no prediction resistance.
|
|
292 |
*/
|
|
293 |
RESEED_ONLY,
|
|
294 |
|
|
295 |
/**
|
|
296 |
* Neither prediction resistance nor reseed.
|
|
297 |
*/
|
|
298 |
NONE;
|
|
299 |
|
|
300 |
@Override
|
|
301 |
public String toString() {
|
|
302 |
return name().toLowerCase(Locale.ROOT);
|
|
303 |
}
|
|
304 |
|
|
305 |
/**
|
|
306 |
* Returns whether this capability supports reseeding.
|
|
307 |
*
|
|
308 |
* @return {@code true} for {@link #PR_AND_RESEED} and
|
|
309 |
* {@link #RESEED_ONLY}, and {@code false} for {@link #NONE}
|
|
310 |
*/
|
|
311 |
public boolean supportsReseeding() {
|
|
312 |
return this != NONE;
|
|
313 |
}
|
|
314 |
|
|
315 |
/**
|
|
316 |
* Returns whether this capability supports prediction resistance.
|
|
317 |
*
|
|
318 |
* @return {@code true} for {@link #PR_AND_RESEED}, and {@code false}
|
|
319 |
* for {@link #RESEED_ONLY} and {@link #NONE}
|
|
320 |
*/
|
|
321 |
public boolean supportsPredictionResistance() {
|
|
322 |
return this == PR_AND_RESEED;
|
|
323 |
}
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* DRBG parameters for instantiation.
|
|
328 |
* <p>
|
|
329 |
* When used in
|
|
330 |
* {@link SecureRandom#getInstance(String, SecureRandomParameters)}
|
|
331 |
* or one of the other similar {@code getInstance} calls that take a
|
|
332 |
* {@code SecureRandomParameters} parameter, it means the
|
|
333 |
* requested instantiate parameters the newly created {@code SecureRandom}
|
|
334 |
* object must minimally support. When used as the return value of the
|
|
335 |
* {@link SecureRandom#getParameters()} method, it means the effective
|
|
336 |
* instantiate parameters of the {@code SecureRandom} object.
|
|
337 |
*
|
|
338 |
* @since 9
|
|
339 |
*/
|
|
340 |
public static final class Instantiation
|
|
341 |
implements SecureRandomParameters {
|
|
342 |
|
|
343 |
private final int strength;
|
|
344 |
private final Capability capability;
|
|
345 |
private final byte[] personalizationString;
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Returns the security strength in bits.
|
|
349 |
*
|
|
350 |
* @return If used in {@code getInstance}, returns the minimum strength
|
|
351 |
* requested, or -1 if there is no specific request on the strength.
|
|
352 |
* If used in {@code getParameters}, returns the effective strength.
|
|
353 |
* The effective strength must be greater than or equal to the minimum
|
|
354 |
* strength requested.
|
|
355 |
*/
|
|
356 |
public int getStrength() {
|
|
357 |
return strength;
|
|
358 |
}
|
|
359 |
|
|
360 |
/**
|
|
361 |
* Returns the capability.
|
|
362 |
*
|
|
363 |
* @return If used in {@code getInstance}, returns the minimum
|
|
364 |
* capability requested. If used in {@code getParameters}, returns
|
|
365 |
* information on the effective prediction resistance flag and
|
|
366 |
* whether it supports reseeding.
|
|
367 |
*/
|
|
368 |
public Capability getCapability() {
|
|
369 |
return capability;
|
|
370 |
}
|
|
371 |
|
|
372 |
/**
|
|
373 |
* Returns the personalization string as a byte array.
|
|
374 |
*
|
|
375 |
* @return If used in {@code getInstance}, returns the requested
|
|
376 |
* personalization string as a newly allocated array, or {@code null}
|
|
377 |
* if no personalization string is requested. The same string should
|
|
378 |
* be returned in {@code getParameters} as a new copy, or {@code null}
|
|
379 |
* if no personalization string is requested in {@code getInstance}.
|
|
380 |
*/
|
|
381 |
public byte[] getPersonalizationString() {
|
|
382 |
return (personalizationString == null) ?
|
|
383 |
null : personalizationString.clone();
|
|
384 |
}
|
|
385 |
|
|
386 |
private Instantiation(int strength, Capability capability,
|
|
387 |
byte[] personalizationString) {
|
|
388 |
this.strength = strength;
|
|
389 |
this.capability = capability;
|
|
390 |
this.personalizationString = (personalizationString == null) ?
|
|
391 |
null : personalizationString.clone();
|
|
392 |
}
|
|
393 |
|
|
394 |
/**
|
|
395 |
* Returns a Human-readable string representation of this
|
|
396 |
* {@code Instantiation}.
|
|
397 |
*
|
|
398 |
* @return the string representation
|
|
399 |
*/
|
|
400 |
@Override
|
|
401 |
public String toString() {
|
|
402 |
// I don't care what personalizationString looks like
|
|
403 |
return strength + "," + capability + "," + personalizationString;
|
|
404 |
}
|
|
405 |
}
|
|
406 |
|
|
407 |
/**
|
|
408 |
* DRBG parameters for random bits generation. It is used in
|
|
409 |
* {@link SecureRandom#nextBytes(byte[], SecureRandomParameters)}.
|
|
410 |
*
|
|
411 |
* @since 9
|
|
412 |
*/
|
|
413 |
public static final class NextBytes
|
|
414 |
implements SecureRandomParameters {
|
|
415 |
private final int strength;
|
|
416 |
private final boolean predictionResistance;
|
|
417 |
private final byte[] additionalInput;
|
|
418 |
|
|
419 |
/**
|
|
420 |
* Returns the security strength requested in bits.
|
|
421 |
*
|
|
422 |
* @return the strength requested, or -1 if the effective strength
|
|
423 |
* should be used.
|
|
424 |
*/
|
|
425 |
public int getStrength() {
|
|
426 |
return strength;
|
|
427 |
}
|
|
428 |
|
|
429 |
/**
|
|
430 |
* Returns whether prediction resistance is requested.
|
|
431 |
*
|
|
432 |
* @return whether prediction resistance is requested
|
|
433 |
*/
|
|
434 |
public boolean getPredictionResistance() {
|
|
435 |
return predictionResistance;
|
|
436 |
}
|
|
437 |
|
|
438 |
/**
|
|
439 |
* Returns the requested additional input.
|
|
440 |
*
|
|
441 |
* @return the requested additional input, {@code null} if not
|
|
442 |
* requested. A new byte array is returned each time this method
|
|
443 |
* is called.
|
|
444 |
*/
|
|
445 |
public byte[] getAdditionalInput() {
|
|
446 |
return additionalInput == null? null: additionalInput.clone();
|
|
447 |
}
|
|
448 |
|
|
449 |
private NextBytes(int strength, boolean predictionResistance,
|
|
450 |
byte[] additionalInput) {
|
|
451 |
this.strength = strength;
|
|
452 |
this.predictionResistance = predictionResistance;
|
|
453 |
this.additionalInput = (additionalInput == null) ?
|
|
454 |
null : additionalInput.clone();
|
|
455 |
}
|
|
456 |
}
|
|
457 |
|
|
458 |
/**
|
|
459 |
* DRBG parameters for reseed. It is used in
|
|
460 |
* {@link SecureRandom#reseed(SecureRandomParameters)}.
|
|
461 |
*
|
|
462 |
* @since 9
|
|
463 |
*/
|
|
464 |
public static final class Reseed implements SecureRandomParameters {
|
|
465 |
|
|
466 |
private final byte[] additionalInput;
|
|
467 |
private final boolean predictionResistance;
|
|
468 |
|
|
469 |
/**
|
|
470 |
* Returns whether prediction resistance is requested.
|
|
471 |
*
|
|
472 |
* @return whether prediction resistance is requested
|
|
473 |
*/
|
|
474 |
public boolean getPredictionResistance() {
|
|
475 |
return predictionResistance;
|
|
476 |
}
|
|
477 |
|
|
478 |
/**
|
|
479 |
* Returns the requested additional input.
|
|
480 |
*
|
|
481 |
* @return the requested additional input, or {@code null} if
|
|
482 |
* not requested. A new byte array is returned each time this method
|
|
483 |
* is called.
|
|
484 |
*/
|
|
485 |
public byte[] getAdditionalInput() {
|
|
486 |
return additionalInput == null ? null : additionalInput.clone();
|
|
487 |
}
|
|
488 |
|
|
489 |
private Reseed(boolean predictionResistance, byte[] additionalInput) {
|
|
490 |
this.predictionResistance = predictionResistance;
|
|
491 |
this.additionalInput = (additionalInput == null) ?
|
|
492 |
null : additionalInput.clone();
|
|
493 |
}
|
|
494 |
}
|
|
495 |
|
|
496 |
/**
|
|
497 |
* Generates a {@link DrbgParameters.Instantiation} object.
|
|
498 |
*
|
|
499 |
* @param strength security strength in bits, -1 for default strength
|
|
500 |
* if used in {@code getInstance}.
|
|
501 |
* @param capability capability
|
|
502 |
* @param personalizationString personalization string as a byte array,
|
|
503 |
* can be {@code null}. The content of this
|
|
504 |
* byte array will be copied.
|
|
505 |
* @return a new {@code Instantiation} object
|
|
506 |
* @throws NullPointerException if {@code capability} is {@code null}
|
|
507 |
*/
|
|
508 |
public static Instantiation instantiation(int strength,
|
|
509 |
Capability capability,
|
|
510 |
byte[] personalizationString) {
|
|
511 |
return new Instantiation(strength, Objects.requireNonNull(capability),
|
|
512 |
personalizationString);
|
|
513 |
}
|
|
514 |
|
|
515 |
/**
|
|
516 |
* Generates a {@link NextBytes} object.
|
|
517 |
*
|
|
518 |
* @param strength requested security strength in bits. If set to -1, the
|
|
519 |
* effective strength will be used.
|
|
520 |
* @param predictionResistance prediction resistance requested
|
|
521 |
* @param additionalInput additional input, can be {@code null}.
|
|
522 |
* The content of this byte array will be copied.
|
|
523 |
* @return a new {@code NextBytes} object
|
|
524 |
*/
|
|
525 |
public static NextBytes nextBytes(int strength,
|
|
526 |
boolean predictionResistance,
|
|
527 |
byte[] additionalInput) {
|
|
528 |
return new NextBytes(strength, predictionResistance, additionalInput);
|
|
529 |
}
|
|
530 |
|
|
531 |
/**
|
|
532 |
* Generates a {@link Reseed} object.
|
|
533 |
*
|
|
534 |
* @param predictionResistance prediction resistance requested
|
|
535 |
* @param additionalInput additional input, can be {@code null}.
|
|
536 |
* The content of this byte array will be copied.
|
|
537 |
* @return a new {@code Reseed} object
|
|
538 |
*/
|
|
539 |
public static Reseed reseed(
|
|
540 |
boolean predictionResistance, byte[] additionalInput) {
|
|
541 |
return new Reseed(predictionResistance, additionalInput);
|
|
542 |
}
|
|
543 |
}
|