author | weijun |
Thu, 12 May 2016 13:06:03 +0800 | |
changeset 37896 | cd841af7dcd0 |
parent 37796 | 256c45c4af5d |
child 38461 | 103f7a26d3f7 |
permissions | -rw-r--r-- |
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 |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37796
diff
changeset
|
199 |
* DRBG algorithm SHA-224, SHA-512/224, SHA-256, SHA-512/256, SHA-384 and |
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37796
diff
changeset
|
200 |
* SHA-512, and CTR_DRBG (both using derivation function and not using |
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37796
diff
changeset
|
201 |
* derivation function) with DRBG algorithm AES-128, AES-192 and AES-256. |
37796 | 202 |
* <p> |
203 |
* The mechanism name and DRBG algorithm name are determined by the |
|
204 |
* {@linkplain Security#getProperty(String) security property} |
|
205 |
* {@code securerandom.drbg.config}. The default choice is Hash_DRBG |
|
206 |
* with SHA-256. |
|
207 |
* <p> |
|
208 |
* For each combination, the security strength can be requested from 112 |
|
209 |
* up to the highest strength it supports. Both reseeding and prediction |
|
210 |
* resistance are supported. |
|
211 |
* <p> |
|
212 |
* Personalization string is supported through the |
|
213 |
* {@link DrbgParameters.Instantiation} class and additional input is supported |
|
214 |
* through the {@link DrbgParameters.NextBytes} and |
|
215 |
* {@link DrbgParameters.Reseed} classes. |
|
216 |
* <p> |
|
217 |
* If a DRBG is not instantiated with a {@link DrbgParameters.Instantiation} |
|
218 |
* object explicitly, this implementation instantiates it with a default |
|
219 |
* requested strength of 128 bits (112 bits for CTR_DRBG with 3KeyTDEA), |
|
220 |
* no prediction resistance request, and no personalization string. |
|
221 |
* These default instantiation parameters can also be customized with |
|
222 |
* the {@code securerandom.drbg.config} security property. |
|
223 |
* <p> |
|
224 |
* This implementation reads fresh entropy from the system default entropy |
|
225 |
* source determined by the security property {@code securerandom.source}. |
|
226 |
* <p> |
|
227 |
* Calling {@link SecureRandom#generateSeed(int)} will directly read |
|
228 |
* from this system default entropy source. |
|
229 |
* <p> |
|
230 |
* This implementation has passed all tests included in the 20151104 version of |
|
231 |
* <a href="http://csrc.nist.gov/groups/STM/cavp/documents/drbg/drbgtestvectors.zip"> |
|
232 |
* The DRBG Test Vectors</a>. |
|
233 |
* |
|
234 |
* @since 9 |
|
235 |
*/ |
|
236 |
public class DrbgParameters { |
|
237 |
||
238 |
private DrbgParameters() { |
|
239 |
// This class should not be instantiated |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* The reseedable and prediction resistance capabilities of a DRBG. |
|
244 |
* <p> |
|
245 |
* When this object is passed to a {@code SecureRandom.getInstance()} call, |
|
246 |
* it is the requested minimum capability. When it's returned from |
|
247 |
* {@code SecureRandom.getParameters()}, it is the effective capability. |
|
248 |
* <p> |
|
249 |
* Please note that while the {@code Instantiate_function} defined in |
|
250 |
* NIST SP 800-90Ar1 only includes a {@code prediction_resistance_flag} |
|
251 |
* parameter, the {@code Capability} type includes an extra value |
|
252 |
* {@link #RESEED_ONLY} because reseeding is an optional function. |
|
253 |
* If {@code NONE} is used in an {@code Instantiation} object in calling the |
|
254 |
* {@code SecureRandom.getInstance} method, the returned DRBG instance |
|
255 |
* is not guaranteed to support reseeding. If {@code RESEED_ONLY} or |
|
256 |
* {@code PR_AND_RESEED} is used, the instance must support reseeding. |
|
257 |
* <p> |
|
258 |
* The table below lists possible effective values if a certain |
|
259 |
* capability is requested, i.e. |
|
260 |
* <blockquote><pre> |
|
261 |
* Capability requested = ...; |
|
262 |
* SecureRandom s = SecureRandom.getInstance("DRBG", |
|
263 |
* DrbgParameters(-1, requested, null)); |
|
264 |
* Capability effective = ((DrbgParametes.Initiate) s.getParameters()) |
|
265 |
* .getCapability();</pre> |
|
266 |
* </blockquote> |
|
267 |
* <table border=1 summary="requested and effective capabilities"> |
|
268 |
* <tr> |
|
269 |
* <th>Requested Value</th> |
|
270 |
* <th>Possible Effective Values</th> |
|
271 |
* </tr> |
|
272 |
* <tr><td>NONE</td><td>NONE, RESEED_ONLY, PR_AND_RESEED</td></tr> |
|
273 |
* <tr><td>RESEED_ONLY</td><td>RESEED_ONLY, PR_AND_RESEED</td></tr> |
|
274 |
* <tr><td>PR_AND_RESEED</td><td>PR_AND_RESEED</td></tr> |
|
275 |
* </table> |
|
276 |
* <p> |
|
277 |
* A DRBG implementation supporting prediction resistance must also |
|
278 |
* support reseeding. |
|
279 |
* |
|
280 |
* @since 9 |
|
281 |
*/ |
|
282 |
public enum Capability { |
|
283 |
||
284 |
/** |
|
285 |
* Both prediction resistance and reseed. |
|
286 |
*/ |
|
287 |
PR_AND_RESEED, |
|
288 |
||
289 |
/** |
|
290 |
* Reseed but no prediction resistance. |
|
291 |
*/ |
|
292 |
RESEED_ONLY, |
|
293 |
||
294 |
/** |
|
295 |
* Neither prediction resistance nor reseed. |
|
296 |
*/ |
|
297 |
NONE; |
|
298 |
||
299 |
@Override |
|
300 |
public String toString() { |
|
301 |
return name().toLowerCase(Locale.ROOT); |
|
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Returns whether this capability supports reseeding. |
|
306 |
* |
|
307 |
* @return {@code true} for {@link #PR_AND_RESEED} and |
|
308 |
* {@link #RESEED_ONLY}, and {@code false} for {@link #NONE} |
|
309 |
*/ |
|
310 |
public boolean supportsReseeding() { |
|
311 |
return this != NONE; |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Returns whether this capability supports prediction resistance. |
|
316 |
* |
|
317 |
* @return {@code true} for {@link #PR_AND_RESEED}, and {@code false} |
|
318 |
* for {@link #RESEED_ONLY} and {@link #NONE} |
|
319 |
*/ |
|
320 |
public boolean supportsPredictionResistance() { |
|
321 |
return this == PR_AND_RESEED; |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* DRBG parameters for instantiation. |
|
327 |
* <p> |
|
328 |
* When used in |
|
329 |
* {@link SecureRandom#getInstance(String, SecureRandomParameters)} |
|
330 |
* or one of the other similar {@code getInstance} calls that take a |
|
331 |
* {@code SecureRandomParameters} parameter, it means the |
|
332 |
* requested instantiate parameters the newly created {@code SecureRandom} |
|
333 |
* object must minimally support. When used as the return value of the |
|
334 |
* {@link SecureRandom#getParameters()} method, it means the effective |
|
335 |
* instantiate parameters of the {@code SecureRandom} object. |
|
336 |
* |
|
337 |
* @since 9 |
|
338 |
*/ |
|
339 |
public static final class Instantiation |
|
340 |
implements SecureRandomParameters { |
|
341 |
||
342 |
private final int strength; |
|
343 |
private final Capability capability; |
|
344 |
private final byte[] personalizationString; |
|
345 |
||
346 |
/** |
|
347 |
* Returns the security strength in bits. |
|
348 |
* |
|
349 |
* @return If used in {@code getInstance}, returns the minimum strength |
|
350 |
* requested, or -1 if there is no specific request on the strength. |
|
351 |
* If used in {@code getParameters}, returns the effective strength. |
|
352 |
* The effective strength must be greater than or equal to the minimum |
|
353 |
* strength requested. |
|
354 |
*/ |
|
355 |
public int getStrength() { |
|
356 |
return strength; |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Returns the capability. |
|
361 |
* |
|
362 |
* @return If used in {@code getInstance}, returns the minimum |
|
363 |
* capability requested. If used in {@code getParameters}, returns |
|
364 |
* information on the effective prediction resistance flag and |
|
365 |
* whether it supports reseeding. |
|
366 |
*/ |
|
367 |
public Capability getCapability() { |
|
368 |
return capability; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Returns the personalization string as a byte array. |
|
373 |
* |
|
374 |
* @return If used in {@code getInstance}, returns the requested |
|
375 |
* personalization string as a newly allocated array, or {@code null} |
|
376 |
* if no personalization string is requested. The same string should |
|
377 |
* be returned in {@code getParameters} as a new copy, or {@code null} |
|
378 |
* if no personalization string is requested in {@code getInstance}. |
|
379 |
*/ |
|
380 |
public byte[] getPersonalizationString() { |
|
381 |
return (personalizationString == null) ? |
|
382 |
null : personalizationString.clone(); |
|
383 |
} |
|
384 |
||
385 |
private Instantiation(int strength, Capability capability, |
|
386 |
byte[] personalizationString) { |
|
387 |
this.strength = strength; |
|
388 |
this.capability = capability; |
|
389 |
this.personalizationString = (personalizationString == null) ? |
|
390 |
null : personalizationString.clone(); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Returns a Human-readable string representation of this |
|
395 |
* {@code Instantiation}. |
|
396 |
* |
|
397 |
* @return the string representation |
|
398 |
*/ |
|
399 |
@Override |
|
400 |
public String toString() { |
|
401 |
// I don't care what personalizationString looks like |
|
402 |
return strength + "," + capability + "," + personalizationString; |
|
403 |
} |
|
404 |
} |
|
405 |
||
406 |
/** |
|
407 |
* DRBG parameters for random bits generation. It is used in |
|
408 |
* {@link SecureRandom#nextBytes(byte[], SecureRandomParameters)}. |
|
409 |
* |
|
410 |
* @since 9 |
|
411 |
*/ |
|
412 |
public static final class NextBytes |
|
413 |
implements SecureRandomParameters { |
|
414 |
private final int strength; |
|
415 |
private final boolean predictionResistance; |
|
416 |
private final byte[] additionalInput; |
|
417 |
||
418 |
/** |
|
419 |
* Returns the security strength requested in bits. |
|
420 |
* |
|
421 |
* @return the strength requested, or -1 if the effective strength |
|
422 |
* should be used. |
|
423 |
*/ |
|
424 |
public int getStrength() { |
|
425 |
return strength; |
|
426 |
} |
|
427 |
||
428 |
/** |
|
429 |
* Returns whether prediction resistance is requested. |
|
430 |
* |
|
431 |
* @return whether prediction resistance is requested |
|
432 |
*/ |
|
433 |
public boolean getPredictionResistance() { |
|
434 |
return predictionResistance; |
|
435 |
} |
|
436 |
||
437 |
/** |
|
438 |
* Returns the requested additional input. |
|
439 |
* |
|
440 |
* @return the requested additional input, {@code null} if not |
|
441 |
* requested. A new byte array is returned each time this method |
|
442 |
* is called. |
|
443 |
*/ |
|
444 |
public byte[] getAdditionalInput() { |
|
445 |
return additionalInput == null? null: additionalInput.clone(); |
|
446 |
} |
|
447 |
||
448 |
private NextBytes(int strength, boolean predictionResistance, |
|
449 |
byte[] additionalInput) { |
|
450 |
this.strength = strength; |
|
451 |
this.predictionResistance = predictionResistance; |
|
452 |
this.additionalInput = (additionalInput == null) ? |
|
453 |
null : additionalInput.clone(); |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* DRBG parameters for reseed. It is used in |
|
459 |
* {@link SecureRandom#reseed(SecureRandomParameters)}. |
|
460 |
* |
|
461 |
* @since 9 |
|
462 |
*/ |
|
463 |
public static final class Reseed implements SecureRandomParameters { |
|
464 |
||
465 |
private final byte[] additionalInput; |
|
466 |
private final boolean predictionResistance; |
|
467 |
||
468 |
/** |
|
469 |
* Returns whether prediction resistance is requested. |
|
470 |
* |
|
471 |
* @return whether prediction resistance is requested |
|
472 |
*/ |
|
473 |
public boolean getPredictionResistance() { |
|
474 |
return predictionResistance; |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Returns the requested additional input. |
|
479 |
* |
|
480 |
* @return the requested additional input, or {@code null} if |
|
481 |
* not requested. A new byte array is returned each time this method |
|
482 |
* is called. |
|
483 |
*/ |
|
484 |
public byte[] getAdditionalInput() { |
|
485 |
return additionalInput == null ? null : additionalInput.clone(); |
|
486 |
} |
|
487 |
||
488 |
private Reseed(boolean predictionResistance, byte[] additionalInput) { |
|
489 |
this.predictionResistance = predictionResistance; |
|
490 |
this.additionalInput = (additionalInput == null) ? |
|
491 |
null : additionalInput.clone(); |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
/** |
|
496 |
* Generates a {@link DrbgParameters.Instantiation} object. |
|
497 |
* |
|
498 |
* @param strength security strength in bits, -1 for default strength |
|
499 |
* if used in {@code getInstance}. |
|
500 |
* @param capability capability |
|
501 |
* @param personalizationString personalization string as a byte array, |
|
502 |
* can be {@code null}. The content of this |
|
503 |
* byte array will be copied. |
|
504 |
* @return a new {@code Instantiation} object |
|
505 |
* @throws NullPointerException if {@code capability} is {@code null} |
|
506 |
*/ |
|
507 |
public static Instantiation instantiation(int strength, |
|
508 |
Capability capability, |
|
509 |
byte[] personalizationString) { |
|
510 |
return new Instantiation(strength, Objects.requireNonNull(capability), |
|
511 |
personalizationString); |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* Generates a {@link NextBytes} object. |
|
516 |
* |
|
517 |
* @param strength requested security strength in bits. If set to -1, the |
|
518 |
* effective strength will be used. |
|
519 |
* @param predictionResistance prediction resistance requested |
|
520 |
* @param additionalInput additional input, can be {@code null}. |
|
521 |
* The content of this byte array will be copied. |
|
522 |
* @return a new {@code NextBytes} object |
|
523 |
*/ |
|
524 |
public static NextBytes nextBytes(int strength, |
|
525 |
boolean predictionResistance, |
|
526 |
byte[] additionalInput) { |
|
527 |
return new NextBytes(strength, predictionResistance, additionalInput); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Generates a {@link Reseed} object. |
|
532 |
* |
|
533 |
* @param predictionResistance prediction resistance requested |
|
534 |
* @param additionalInput additional input, can be {@code null}. |
|
535 |
* The content of this byte array will be copied. |
|
536 |
* @return a new {@code Reseed} object |
|
537 |
*/ |
|
538 |
public static Reseed reseed( |
|
539 |
boolean predictionResistance, byte[] additionalInput) { |
|
540 |
return new Reseed(predictionResistance, additionalInput); |
|
541 |
} |
|
542 |
} |