author | jlaskey |
Thu, 14 Nov 2019 12:39:49 -0400 | |
branch | JDK-8193209-branch |
changeset 59086 | 214afc7a1e02 |
parent 58519 | 6e017b301287 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57736
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.security; |
|
27 |
||
59086 | 28 |
import java.math.BigInteger; |
2 | 29 |
import java.util.*; |
59086 | 30 |
import java.util.random.RandomGenerator; |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
31 |
import java.util.regex.*; |
2 | 32 |
import java.security.Provider.Service; |
33 |
import sun.security.jca.*; |
|
34 |
import sun.security.jca.GetInstance.Instance; |
|
57736
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
35 |
import sun.security.provider.SunEntries; |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
36 |
import sun.security.util.Debug; |
2 | 37 |
|
38 |
/** |
|
39 |
* This class provides a cryptographically strong random number |
|
40 |
* generator (RNG). |
|
41 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
42 |
* <p>A cryptographically strong random number minimally complies with the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
43 |
* statistical random number generator tests specified in |
44258
2017f44ec3e5
8176793: SecureRandom FIPS 140-2, Security Requirements for Cryptographic Modules link 404
wetmore
parents:
43801
diff
changeset
|
44 |
* <a href="http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.140-2.pdf"> |
2 | 45 |
* <i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>, |
46 |
* section 4.9.1. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
47 |
* Additionally, {@code SecureRandom} must produce non-deterministic output. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
48 |
* Therefore any seed material passed to a {@code SecureRandom} object must be |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
49 |
* unpredictable, and all {@code SecureRandom} output sequences must be |
2 | 50 |
* cryptographically strong, as described in |
25972 | 51 |
* <a href="http://tools.ietf.org/html/rfc4086"> |
52 |
* <i>RFC 4086: Randomness Requirements for Security</i></a>. |
|
2 | 53 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
54 |
* <p> Many {@code SecureRandom} implementations are in the form of a |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
55 |
* pseudo-random number generator (PRNG, also known as deterministic random |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
56 |
* bits generator or DRBG), which means they use a deterministic algorithm |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
57 |
* to produce a pseudo-random sequence from a random seed. |
2 | 58 |
* Other implementations may produce true random numbers, |
59 |
* and yet others may use a combination of both techniques. |
|
60 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
61 |
* <p>A caller obtains a {@code SecureRandom} instance via the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
62 |
* no-argument constructor or one of the {@code getInstance} methods. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
63 |
* For example: |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
64 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
65 |
* <blockquote><pre> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
66 |
* SecureRandom r1 = new SecureRandom(); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
67 |
* SecureRandom r2 = SecureRandom.getInstance("NativePRNG"); |
43801 | 68 |
* SecureRandom r3 = SecureRandom.getInstance("DRBG", |
69 |
* DrbgParameters.instantiation(128, RESEED_ONLY, null));</pre> |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
70 |
* </blockquote> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
71 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
72 |
* <p> The third statement above returns a {@code SecureRandom} object of the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
73 |
* specific algorithm supporting the specific instantiate parameters. The |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
74 |
* implementation's effective instantiated parameters must match this minimum |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
75 |
* request but is not necessarily the same. For example, even if the request |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
76 |
* does not require a certain feature, the actual instantiation can provide |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
77 |
* the feature. An implementation may lazily instantiate a {@code SecureRandom} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
78 |
* until it's actually used, but the effective instantiate parameters must be |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
79 |
* determined right after it's created and {@link #getParameters()} should |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
80 |
* always return the same result unchanged. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
81 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
82 |
* <p> Typical callers of {@code SecureRandom} invoke the following methods |
2 | 83 |
* to retrieve random bytes: |
84 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
85 |
* <blockquote><pre> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
86 |
* SecureRandom random = new SecureRandom(); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
87 |
* byte[] bytes = new byte[20]; |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
88 |
* random.nextBytes(bytes);</pre> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
89 |
* </blockquote> |
2 | 90 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
91 |
* <p> Callers may also invoke the {@link #generateSeed} method |
2 | 92 |
* to generate a given number of seed bytes (to seed other random number |
93 |
* generators, for example): |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
94 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
95 |
* <blockquote><pre> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
96 |
* byte[] seed = random.generateSeed(20);</pre> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
97 |
* </blockquote> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
98 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
99 |
* <p> A newly created PRNG {@code SecureRandom} object is not seeded (except |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
100 |
* if it is created by {@link #SecureRandom(byte[])}). The first call to |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
101 |
* {@code nextBytes} will force it to seed itself from an implementation- |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
102 |
* specific entropy source. This self-seeding will not occur if {@code setSeed} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
103 |
* was previously called. |
2 | 104 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
105 |
* <p> A {@code SecureRandom} can be reseeded at any time by calling the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
106 |
* {@code reseed} or {@code setSeed} method. The {@code reseed} method |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
107 |
* reads entropy input from its entropy source to reseed itself. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
108 |
* The {@code setSeed} method requires the caller to provide the seed. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
109 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
110 |
* <p> Please note that {@code reseed} may not be supported by all |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
111 |
* {@code SecureRandom} implementations. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
112 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
113 |
* <p> Some {@code SecureRandom} implementations may accept a |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
114 |
* {@link SecureRandomParameters} parameter in its |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
115 |
* {@link #nextBytes(byte[], SecureRandomParameters)} and |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
116 |
* {@link #reseed(SecureRandomParameters)} methods to further |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
117 |
* control the behavior of the methods. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
118 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
119 |
* <p> Note: Depending on the implementation, the {@code generateSeed}, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
120 |
* {@code reseed} and {@code nextBytes} methods may block as entropy is being |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
121 |
* gathered, for example, if the entropy source is /dev/random on various |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
122 |
* Unix-like operating systems. |
2 | 123 |
* |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
124 |
* <h2> Thread safety </h2> |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
125 |
* {@code SecureRandom} objects are safe for use by multiple concurrent threads. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
126 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
127 |
* @implSpec |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
128 |
* A {@code SecureRandom} service provider can advertise that it is thread-safe |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
129 |
* by setting the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
130 |
* "{@docRoot}/../specs/security/standard-names.html#service-attributes">service |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
131 |
* provider attribute</a> "ThreadSafe" to "true" when registering the provider. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
132 |
* Otherwise, this class will instead synchronize access to the following |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
133 |
* methods of the {@code SecureRandomSpi} implementation: |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
134 |
* <ul> |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
135 |
* <li>{@link SecureRandomSpi#engineSetSeed(byte[])} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
136 |
* <li>{@link SecureRandomSpi#engineNextBytes(byte[])} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
137 |
* <li>{@link SecureRandomSpi#engineNextBytes(byte[], SecureRandomParameters)} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
138 |
* <li>{@link SecureRandomSpi#engineGenerateSeed(int)} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
139 |
* <li>{@link SecureRandomSpi#engineReseed(SecureRandomParameters)} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
140 |
* </ul> |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
141 |
* |
2 | 142 |
* @see java.security.SecureRandomSpi |
143 |
* @see java.util.Random |
|
144 |
* |
|
145 |
* @author Benjamin Renaud |
|
146 |
* @author Josh Bloch |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
45118
diff
changeset
|
147 |
* @since 1.1 |
2 | 148 |
*/ |
149 |
||
150 |
public class SecureRandom extends java.util.Random { |
|
151 |
||
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
152 |
private static final Debug pdebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
153 |
Debug.getInstance("provider", "Provider"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
154 |
private static final boolean skipDebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
155 |
Debug.isOn("engine=") && !Debug.isOn("securerandom"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
156 |
|
2 | 157 |
/** |
158 |
* The provider. |
|
159 |
* |
|
160 |
* @serial |
|
161 |
* @since 1.2 |
|
162 |
*/ |
|
163 |
private Provider provider = null; |
|
164 |
||
165 |
/** |
|
166 |
* The provider implementation. |
|
167 |
* |
|
168 |
* @serial |
|
169 |
* @since 1.2 |
|
170 |
*/ |
|
171 |
private SecureRandomSpi secureRandomSpi = null; |
|
172 |
||
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
173 |
/** |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
174 |
* Thread safety. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
175 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
176 |
* @serial |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
177 |
* @since 9 |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
178 |
*/ |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
179 |
private final boolean threadSafe; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
180 |
|
2 | 181 |
/* |
182 |
* The algorithm name of null if unknown. |
|
183 |
* |
|
184 |
* @serial |
|
185 |
* @since 1.5 |
|
186 |
*/ |
|
187 |
private String algorithm; |
|
188 |
||
189 |
// Seed Generator |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
190 |
private static volatile SecureRandom seedGenerator; |
2 | 191 |
|
192 |
/** |
|
193 |
* Constructs a secure random number generator (RNG) implementing the |
|
194 |
* default random number algorithm. |
|
195 |
* |
|
196 |
* <p> This constructor traverses the list of registered security Providers, |
|
197 |
* starting with the most preferred Provider. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
198 |
* A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
199 |
* {@code SecureRandomSpi} implementation from the first |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
200 |
* Provider that supports a {@code SecureRandom} (RNG) algorithm is returned. |
2 | 201 |
* If none of the Providers support a RNG algorithm, |
202 |
* then an implementation-specific default is returned. |
|
203 |
* |
|
204 |
* <p> Note that the list of registered providers may be retrieved via |
|
205 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
206 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
207 |
* <p> See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
208 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
209 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 210 |
* for information about standard RNG algorithm names. |
211 |
*/ |
|
212 |
public SecureRandom() { |
|
213 |
/* |
|
214 |
* This call to our superclass constructor will result in a call |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
215 |
* to our own {@code setSeed} method, which will return |
2 | 216 |
* immediately when it is passed zero. |
217 |
*/ |
|
218 |
super(0); |
|
219 |
getDefaultPRNG(false, null); |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
220 |
this.threadSafe = getThreadSafe(); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
221 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
222 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
223 |
private boolean getThreadSafe() { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
224 |
if (provider == null || algorithm == null) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
225 |
return false; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
226 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
227 |
return Boolean.parseBoolean(provider.getProperty( |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
228 |
"SecureRandom." + algorithm + " ThreadSafe", "false")); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
229 |
} |
2 | 230 |
} |
231 |
||
232 |
/** |
|
233 |
* Constructs a secure random number generator (RNG) implementing the |
|
234 |
* default random number algorithm. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
235 |
* The {@code SecureRandom} instance is seeded with the specified seed bytes. |
2 | 236 |
* |
237 |
* <p> This constructor traverses the list of registered security Providers, |
|
238 |
* starting with the most preferred Provider. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
239 |
* A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
240 |
* {@code SecureRandomSpi} implementation from the first |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
241 |
* Provider that supports a {@code SecureRandom} (RNG) algorithm is returned. |
2 | 242 |
* If none of the Providers support a RNG algorithm, |
243 |
* then an implementation-specific default is returned. |
|
244 |
* |
|
245 |
* <p> Note that the list of registered providers may be retrieved via |
|
246 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
247 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
248 |
* <p> See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
249 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
250 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 251 |
* for information about standard RNG algorithm names. |
252 |
* |
|
253 |
* @param seed the seed. |
|
254 |
*/ |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30033
diff
changeset
|
255 |
public SecureRandom(byte[] seed) { |
2 | 256 |
super(0); |
257 |
getDefaultPRNG(true, seed); |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
258 |
this.threadSafe = getThreadSafe(); |
2 | 259 |
} |
260 |
||
261 |
private void getDefaultPRNG(boolean setSeed, byte[] seed) { |
|
262 |
String prng = getPrngAlgorithm(); |
|
263 |
if (prng == null) { |
|
264 |
// bummer, get the SUN implementation |
|
265 |
prng = "SHA1PRNG"; |
|
266 |
this.secureRandomSpi = new sun.security.provider.SecureRandom(); |
|
267 |
this.provider = Providers.getSunProvider(); |
|
268 |
if (setSeed) { |
|
269 |
this.secureRandomSpi.engineSetSeed(seed); |
|
270 |
} |
|
271 |
} else { |
|
272 |
try { |
|
273 |
SecureRandom random = SecureRandom.getInstance(prng); |
|
274 |
this.secureRandomSpi = random.getSecureRandomSpi(); |
|
275 |
this.provider = random.getProvider(); |
|
276 |
if (setSeed) { |
|
277 |
this.secureRandomSpi.engineSetSeed(seed); |
|
278 |
} |
|
279 |
} catch (NoSuchAlgorithmException nsae) { |
|
280 |
// never happens, because we made sure the algorithm exists |
|
281 |
throw new RuntimeException(nsae); |
|
282 |
} |
|
283 |
} |
|
284 |
// JDK 1.1 based implementations subclass SecureRandom instead of |
|
285 |
// SecureRandomSpi. They will also go through this code path because |
|
286 |
// they must call a SecureRandom constructor as it is their superclass. |
|
287 |
// If we are dealing with such an implementation, do not set the |
|
288 |
// algorithm value as it would be inaccurate. |
|
289 |
if (getClass() == SecureRandom.class) { |
|
290 |
this.algorithm = prng; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
295 |
* Creates a {@code SecureRandom} object. |
2 | 296 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
297 |
* @param secureRandomSpi the {@code SecureRandom} implementation. |
2 | 298 |
* @param provider the provider. |
299 |
*/ |
|
300 |
protected SecureRandom(SecureRandomSpi secureRandomSpi, |
|
301 |
Provider provider) { |
|
302 |
this(secureRandomSpi, provider, null); |
|
303 |
} |
|
304 |
||
305 |
private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider, |
|
306 |
String algorithm) { |
|
307 |
super(0); |
|
308 |
this.secureRandomSpi = secureRandomSpi; |
|
309 |
this.provider = provider; |
|
310 |
this.algorithm = algorithm; |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
311 |
this.threadSafe = getThreadSafe(); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
312 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
313 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
314 |
pdebug.println("SecureRandom." + algorithm + |
42780
7781326fff20
8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents:
42161
diff
changeset
|
315 |
" algorithm from: " + getProviderName()); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25991
diff
changeset
|
316 |
} |
2 | 317 |
} |
318 |
||
42780
7781326fff20
8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents:
42161
diff
changeset
|
319 |
private String getProviderName() { |
7781326fff20
8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents:
42161
diff
changeset
|
320 |
return (provider == null) ? "(no provider)" : provider.getName(); |
7781326fff20
8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents:
42161
diff
changeset
|
321 |
} |
7781326fff20
8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents:
42161
diff
changeset
|
322 |
|
2 | 323 |
/** |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
324 |
* Returns a {@code SecureRandom} object that implements the specified |
2 | 325 |
* Random Number Generator (RNG) algorithm. |
326 |
* |
|
327 |
* <p> This method traverses the list of registered security Providers, |
|
328 |
* starting with the most preferred Provider. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
329 |
* A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
330 |
* {@code SecureRandomSpi} implementation from the first |
2 | 331 |
* Provider that supports the specified algorithm is returned. |
332 |
* |
|
333 |
* <p> Note that the list of registered providers may be retrieved via |
|
334 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
335 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
336 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
337 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
35718
diff
changeset
|
338 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
35718
diff
changeset
|
339 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
340 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
341 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
342 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
343 |
* |
2 | 344 |
* @param algorithm the name of the RNG algorithm. |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
345 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
346 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
347 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 348 |
* for information about standard RNG algorithm names. |
349 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
350 |
* @return the new {@code SecureRandom} object |
2 | 351 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
352 |
* @throws NoSuchAlgorithmException if no {@code Provider} supports a |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
353 |
* {@code SecureRandomSpi} implementation for the |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
354 |
* specified algorithm |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
355 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
356 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 357 |
* |
358 |
* @see Provider |
|
359 |
* |
|
360 |
* @since 1.2 |
|
361 |
*/ |
|
362 |
public static SecureRandom getInstance(String algorithm) |
|
363 |
throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
364 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 365 |
Instance instance = GetInstance.getInstance("SecureRandom", |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
366 |
SecureRandomSpi.class, algorithm); |
2 | 367 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
368 |
instance.provider, algorithm); |
2 | 369 |
} |
370 |
||
371 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
372 |
* Returns a {@code SecureRandom} object that implements the specified |
2 | 373 |
* Random Number Generator (RNG) algorithm. |
374 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
375 |
* <p> A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
376 |
* {@code SecureRandomSpi} implementation from the specified provider |
2 | 377 |
* is returned. The specified provider must be registered |
378 |
* in the security provider list. |
|
379 |
* |
|
380 |
* <p> Note that the list of registered providers may be retrieved via |
|
381 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
382 |
* |
|
383 |
* @param algorithm the name of the RNG algorithm. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
384 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
385 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
386 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 387 |
* for information about standard RNG algorithm names. |
388 |
* |
|
389 |
* @param provider the name of the provider. |
|
390 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
391 |
* @return the new {@code SecureRandom} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
392 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
393 |
* @throws IllegalArgumentException if the provider name is {@code null} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
394 |
* or empty |
2 | 395 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
396 |
* @throws NoSuchAlgorithmException if a {@code SecureRandomSpi} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
397 |
* implementation for the specified algorithm is not |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
398 |
* available from the specified provider |
2 | 399 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
400 |
* @throws NoSuchProviderException if the specified provider is not |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
401 |
* registered in the security provider list |
2 | 402 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
403 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 404 |
* |
405 |
* @see Provider |
|
406 |
* |
|
407 |
* @since 1.2 |
|
408 |
*/ |
|
409 |
public static SecureRandom getInstance(String algorithm, String provider) |
|
410 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
411 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 412 |
Instance instance = GetInstance.getInstance("SecureRandom", |
413 |
SecureRandomSpi.class, algorithm, provider); |
|
414 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
|
415 |
instance.provider, algorithm); |
|
416 |
} |
|
417 |
||
418 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
419 |
* Returns a {@code SecureRandom} object that implements the specified |
2 | 420 |
* Random Number Generator (RNG) algorithm. |
421 |
* |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
422 |
* <p> A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
423 |
* {@code SecureRandomSpi} implementation from the specified {@code Provider} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
424 |
* object is returned. Note that the specified {@code Provider} object |
2 | 425 |
* does not have to be registered in the provider list. |
426 |
* |
|
427 |
* @param algorithm the name of the RNG algorithm. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
428 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
429 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
430 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 431 |
* for information about standard RNG algorithm names. |
432 |
* |
|
433 |
* @param provider the provider. |
|
434 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
435 |
* @return the new {@code SecureRandom} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
436 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
437 |
* @throws IllegalArgumentException if the specified provider is |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
438 |
* {@code null} |
2 | 439 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
440 |
* @throws NoSuchAlgorithmException if a {@code SecureRandomSpi} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
441 |
* implementation for the specified algorithm is not available |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
442 |
* from the specified {@code Provider} object |
2 | 443 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
444 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 445 |
* |
446 |
* @see Provider |
|
447 |
* |
|
448 |
* @since 1.4 |
|
449 |
*/ |
|
450 |
public static SecureRandom getInstance(String algorithm, |
|
451 |
Provider provider) throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
452 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 453 |
Instance instance = GetInstance.getInstance("SecureRandom", |
454 |
SecureRandomSpi.class, algorithm, provider); |
|
455 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
|
456 |
instance.provider, algorithm); |
|
457 |
} |
|
458 |
||
459 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
460 |
* Returns a {@code SecureRandom} object that implements the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
461 |
* Random Number Generator (RNG) algorithm and supports the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
462 |
* {@code SecureRandomParameters} request. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
463 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
464 |
* <p> This method traverses the list of registered security Providers, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
465 |
* starting with the most preferred Provider. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
466 |
* A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
467 |
* {@code SecureRandomSpi} implementation from the first |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
468 |
* Provider that supports the specified algorithm and the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
469 |
* {@code SecureRandomParameters} is returned. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
470 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
471 |
* <p> Note that the list of registered providers may be retrieved via |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
472 |
* the {@link Security#getProviders() Security.getProviders()} method. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
473 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
474 |
* @implNote |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
475 |
* The JDK Reference Implementation additionally uses the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
476 |
* {@code jdk.security.provider.preferred} property to determine |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
477 |
* the preferred provider order for the specified algorithm. This |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
478 |
* may be different than the order of providers returned by |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
479 |
* {@link Security#getProviders() Security.getProviders()}. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
480 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
481 |
* @param algorithm the name of the RNG algorithm. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
482 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
483 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
484 |
* Java Security Standard Algorithm Names Specification</a> |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
485 |
* for information about standard RNG algorithm names. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
486 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
487 |
* @param params the {@code SecureRandomParameters} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
488 |
* the newly created {@code SecureRandom} object must support. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
489 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
490 |
* @return the new {@code SecureRandom} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
491 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
492 |
* @throws IllegalArgumentException if the specified params is |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
493 |
* {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
494 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
495 |
* @throws NoSuchAlgorithmException if no Provider supports a |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
496 |
* {@code SecureRandomSpi} implementation for the specified |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
497 |
* algorithm and parameters |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
498 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
499 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
500 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
501 |
* @see Provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
502 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
503 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
504 |
*/ |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
505 |
public static SecureRandom getInstance( |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
506 |
String algorithm, SecureRandomParameters params) |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
507 |
throws NoSuchAlgorithmException { |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
508 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
509 |
if (params == null) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
510 |
throw new IllegalArgumentException("params cannot be null"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
511 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
512 |
Instance instance = GetInstance.getInstance("SecureRandom", |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
513 |
SecureRandomSpi.class, algorithm, params); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
514 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
515 |
instance.provider, algorithm); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
516 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
517 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
518 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
519 |
* Returns a {@code SecureRandom} object that implements the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
520 |
* Random Number Generator (RNG) algorithm and supports the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
521 |
* {@code SecureRandomParameters} request. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
522 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
523 |
* <p> A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
524 |
* {@code SecureRandomSpi} implementation from the specified provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
525 |
* is returned. The specified provider must be registered |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
526 |
* in the security provider list. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
527 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
528 |
* <p> Note that the list of registered providers may be retrieved via |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
529 |
* the {@link Security#getProviders() Security.getProviders()} method. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
530 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
531 |
* @param algorithm the name of the RNG algorithm. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
532 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
533 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
534 |
* Java Security Standard Algorithm Names Specification</a> |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
535 |
* for information about standard RNG algorithm names. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
536 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
537 |
* @param params the {@code SecureRandomParameters} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
538 |
* the newly created {@code SecureRandom} object must support. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
539 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
540 |
* @param provider the name of the provider. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
541 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
542 |
* @return the new {@code SecureRandom} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
543 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
544 |
* @throws IllegalArgumentException if the provider name is {@code null} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
545 |
* or empty, or params is {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
546 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
547 |
* @throws NoSuchAlgorithmException if the specified provider does not |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
548 |
* support a {@code SecureRandomSpi} implementation for the |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
549 |
* specified algorithm and parameters |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
550 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
551 |
* @throws NoSuchProviderException if the specified provider is not |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
552 |
* registered in the security provider list |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
553 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
554 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
555 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
556 |
* @see Provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
557 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
558 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
559 |
*/ |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
560 |
public static SecureRandom getInstance(String algorithm, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
561 |
SecureRandomParameters params, String provider) |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
562 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
563 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
564 |
if (params == null) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
565 |
throw new IllegalArgumentException("params cannot be null"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
566 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
567 |
Instance instance = GetInstance.getInstance("SecureRandom", |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
568 |
SecureRandomSpi.class, algorithm, params, provider); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
569 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
570 |
instance.provider, algorithm); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
571 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
572 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
573 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
574 |
* Returns a {@code SecureRandom} object that implements the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
575 |
* Random Number Generator (RNG) algorithm and supports the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
576 |
* {@code SecureRandomParameters} request. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
577 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
578 |
* <p> A new {@code SecureRandom} object encapsulating the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
579 |
* {@code SecureRandomSpi} implementation from the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
580 |
* {@code Provider} object is returned. Note that the specified |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
581 |
* {@code Provider} object does not have to be registered in the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
582 |
* provider list. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
583 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
584 |
* @param algorithm the name of the RNG algorithm. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
585 |
* See the {@code SecureRandom} section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
586 |
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
44369
diff
changeset
|
587 |
* Java Security Standard Algorithm Names Specification</a> |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
588 |
* for information about standard RNG algorithm names. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
589 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
590 |
* @param params the {@code SecureRandomParameters} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
591 |
* the newly created {@code SecureRandom} object must support. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
592 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
593 |
* @param provider the provider. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
594 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
595 |
* @return the new {@code SecureRandom} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
596 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
597 |
* @throws IllegalArgumentException if the specified provider or params |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
598 |
* is {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
599 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
600 |
* @throws NoSuchAlgorithmException if the specified provider does not |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
601 |
* support a {@code SecureRandomSpi} implementation for the |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
602 |
* specified algorithm and parameters |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
603 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
604 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
605 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
606 |
* @see Provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
607 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
608 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
609 |
*/ |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
610 |
public static SecureRandom getInstance(String algorithm, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
611 |
SecureRandomParameters params, Provider provider) |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
612 |
throws NoSuchAlgorithmException { |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37796
diff
changeset
|
613 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
614 |
if (params == null) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
615 |
throw new IllegalArgumentException("params cannot be null"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
616 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
617 |
Instance instance = GetInstance.getInstance("SecureRandom", |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
618 |
SecureRandomSpi.class, algorithm, params, provider); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
619 |
return new SecureRandom((SecureRandomSpi)instance.impl, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
620 |
instance.provider, algorithm); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
621 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
622 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
623 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
624 |
* Returns the {@code SecureRandomSpi} of this {@code SecureRandom} object. |
2 | 625 |
*/ |
626 |
SecureRandomSpi getSecureRandomSpi() { |
|
627 |
return secureRandomSpi; |
|
628 |
} |
|
629 |
||
630 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
631 |
* Returns the provider of this {@code SecureRandom} object. |
2 | 632 |
* |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
633 |
* @return the provider of this {@code SecureRandom} object. |
2 | 634 |
*/ |
635 |
public final Provider getProvider() { |
|
636 |
return provider; |
|
637 |
} |
|
638 |
||
639 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
640 |
* Returns the name of the algorithm implemented by this |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
641 |
* {@code SecureRandom} object. |
2 | 642 |
* |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
643 |
* @return the name of the algorithm or {@code unknown} |
2 | 644 |
* if the algorithm name cannot be determined. |
645 |
* @since 1.5 |
|
646 |
*/ |
|
647 |
public String getAlgorithm() { |
|
33675
7d9d372a41df
8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents:
33241
diff
changeset
|
648 |
return Objects.toString(algorithm, "unknown"); |
2 | 649 |
} |
650 |
||
651 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
652 |
* Returns a Human-readable string representation of this |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
653 |
* {@code SecureRandom}. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
654 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
655 |
* @return the string representation |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
656 |
*/ |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
657 |
@Override |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
658 |
public String toString() { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
659 |
return secureRandomSpi.toString(); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
660 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
661 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
662 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
663 |
* Returns the effective {@link SecureRandomParameters} for this |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
664 |
* {@code SecureRandom} instance. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
665 |
* <p> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
666 |
* The returned value can be different from the |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
667 |
* {@code SecureRandomParameters} object passed into a {@code getInstance} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
668 |
* method, but it cannot change during the lifetime of this |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
669 |
* {@code SecureRandom} object. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
670 |
* <p> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
671 |
* A caller can use the returned value to find out what features this |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
672 |
* {@code SecureRandom} supports. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
673 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
674 |
* @return the effective {@link SecureRandomParameters} parameters, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
675 |
* or {@code null} if no parameters were used. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
676 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
677 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
678 |
* @see SecureRandomSpi |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
679 |
*/ |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
680 |
public SecureRandomParameters getParameters() { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
681 |
return secureRandomSpi.engineGetParameters(); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
682 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
683 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
684 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
685 |
* Reseeds this random object with the given seed. The seed supplements, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
686 |
* rather than replaces, the existing seed. Thus, repeated calls are |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
687 |
* guaranteed never to reduce randomness. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
688 |
* <p> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
689 |
* A PRNG {@code SecureRandom} will not seed itself automatically if |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
690 |
* {@code setSeed} is called before any {@code nextBytes} or {@code reseed} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
691 |
* calls. The caller should make sure that the {@code seed} argument |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
692 |
* contains enough entropy for the security of this {@code SecureRandom}. |
2 | 693 |
* |
694 |
* @param seed the seed. |
|
695 |
* |
|
696 |
* @see #getSeed |
|
697 |
*/ |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
698 |
public void setSeed(byte[] seed) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
699 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
700 |
secureRandomSpi.engineSetSeed(seed); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
701 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
702 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
703 |
secureRandomSpi.engineSetSeed(seed); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
704 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
705 |
} |
2 | 706 |
} |
707 |
||
708 |
/** |
|
709 |
* Reseeds this random object, using the eight bytes contained |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
710 |
* in the given {@code long seed}. The given seed supplements, |
2 | 711 |
* rather than replaces, the existing seed. Thus, repeated calls |
712 |
* are guaranteed never to reduce randomness. |
|
713 |
* |
|
714 |
* <p>This method is defined for compatibility with |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
715 |
* {@code java.util.Random}. |
2 | 716 |
* |
717 |
* @param seed the seed. |
|
718 |
* |
|
719 |
* @see #getSeed |
|
720 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
721 |
@Override |
2 | 722 |
public void setSeed(long seed) { |
723 |
/* |
|
724 |
* Ignore call from super constructor (as well as any other calls |
|
725 |
* unfortunate enough to be passing 0). It's critical that we |
|
726 |
* ignore call from superclass constructor, as digest has not |
|
727 |
* yet been initialized at that point. |
|
728 |
*/ |
|
729 |
if (seed != 0) { |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
730 |
setSeed(longToByteArray(seed)); |
2 | 731 |
} |
732 |
} |
|
733 |
||
734 |
/** |
|
735 |
* Generates a user-specified number of random bytes. |
|
736 |
* |
|
737 |
* @param bytes the array to be filled in with random bytes. |
|
738 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
739 |
@Override |
35718
e6b4f7af3fca
8098581: SecureRandom.nextBytes() hurts performance with small size requests
ascarpino
parents:
34774
diff
changeset
|
740 |
public void nextBytes(byte[] bytes) { |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
741 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
742 |
secureRandomSpi.engineNextBytes(bytes); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
743 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
744 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
745 |
secureRandomSpi.engineNextBytes(bytes); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
746 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
747 |
} |
2 | 748 |
} |
749 |
||
750 |
/** |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
751 |
* Generates a user-specified number of random bytes with |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
752 |
* additional parameters. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
753 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
754 |
* @param bytes the array to be filled in with random bytes |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
755 |
* @param params additional parameters |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
756 |
* @throws NullPointerException if {@code bytes} is null |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
757 |
* @throws UnsupportedOperationException if the underlying provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
758 |
* implementation has not overridden this method |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
759 |
* @throws IllegalArgumentException if {@code params} is {@code null}, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
760 |
* illegal or unsupported by this {@code SecureRandom} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
761 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
762 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
763 |
*/ |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
764 |
public void nextBytes(byte[] bytes, SecureRandomParameters params) { |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
765 |
if (params == null) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
766 |
throw new IllegalArgumentException("params cannot be null"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
767 |
} |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
768 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
769 |
secureRandomSpi.engineNextBytes( |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
770 |
Objects.requireNonNull(bytes), params); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
771 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
772 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
773 |
secureRandomSpi.engineNextBytes( |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
774 |
Objects.requireNonNull(bytes), params); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
775 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
776 |
} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
777 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
778 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
779 |
/** |
2 | 780 |
* Generates an integer containing the user-specified number of |
781 |
* pseudo-random bits (right justified, with leading zeros). This |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
782 |
* method overrides a {@code java.util.Random} method, and serves |
2 | 783 |
* to provide a source of random bits to all of the methods inherited |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
784 |
* from that class (for example, {@code nextInt}, |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
785 |
* {@code nextLong}, and {@code nextFloat}). |
2 | 786 |
* |
787 |
* @param numBits number of pseudo-random bits to be generated, where |
|
18156 | 788 |
* {@code 0 <= numBits <= 32}. |
2 | 789 |
* |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
790 |
* @return an {@code int} containing the user-specified number |
2 | 791 |
* of pseudo-random bits (right justified, with leading zeros). |
792 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
793 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31538
diff
changeset
|
794 |
protected final int next(int numBits) { |
2 | 795 |
int numBytes = (numBits+7)/8; |
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30033
diff
changeset
|
796 |
byte[] b = new byte[numBytes]; |
2 | 797 |
int next = 0; |
798 |
||
799 |
nextBytes(b); |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
800 |
for (int i = 0; i < numBytes; i++) { |
2 | 801 |
next = (next << 8) + (b[i] & 0xFF); |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
802 |
} |
2 | 803 |
|
804 |
return next >>> (numBytes*8 - numBits); |
|
805 |
} |
|
806 |
||
807 |
/** |
|
808 |
* Returns the given number of seed bytes, computed using the seed |
|
809 |
* generation algorithm that this class uses to seed itself. This |
|
810 |
* call may be used to seed other random number generators. |
|
811 |
* |
|
812 |
* <p>This method is only included for backwards compatibility. |
|
813 |
* The caller is encouraged to use one of the alternative |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
814 |
* {@code getInstance} methods to obtain a {@code SecureRandom} object, and |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18156
diff
changeset
|
815 |
* then call the {@code generateSeed} method to obtain seed bytes |
2 | 816 |
* from that object. |
817 |
* |
|
818 |
* @param numBytes the number of seed bytes to generate. |
|
819 |
* |
|
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
820 |
* @throws IllegalArgumentException if {@code numBytes} is negative |
2 | 821 |
* @return the seed bytes. |
822 |
* |
|
823 |
* @see #setSeed |
|
824 |
*/ |
|
825 |
public static byte[] getSeed(int numBytes) { |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
826 |
SecureRandom seedGen = seedGenerator; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
827 |
if (seedGen == null) { |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
828 |
seedGen = new SecureRandom(); |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
829 |
seedGenerator = seedGen; |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
830 |
} |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33675
diff
changeset
|
831 |
return seedGen.generateSeed(numBytes); |
2 | 832 |
} |
833 |
||
834 |
/** |
|
835 |
* Returns the given number of seed bytes, computed using the seed |
|
836 |
* generation algorithm that this class uses to seed itself. This |
|
837 |
* call may be used to seed other random number generators. |
|
838 |
* |
|
839 |
* @param numBytes the number of seed bytes to generate. |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
840 |
* @throws IllegalArgumentException if {@code numBytes} is negative |
2 | 841 |
* @return the seed bytes. |
842 |
*/ |
|
843 |
public byte[] generateSeed(int numBytes) { |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
844 |
if (numBytes < 0) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
845 |
throw new IllegalArgumentException("numBytes cannot be negative"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
846 |
} |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
847 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
848 |
return secureRandomSpi.engineGenerateSeed(numBytes); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
849 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
850 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
851 |
return secureRandomSpi.engineGenerateSeed(numBytes); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
852 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
853 |
} |
2 | 854 |
} |
855 |
||
856 |
/** |
|
857 |
* Helper function to convert a long into a byte array (least significant |
|
858 |
* byte first). |
|
859 |
*/ |
|
860 |
private static byte[] longToByteArray(long l) { |
|
861 |
byte[] retVal = new byte[8]; |
|
862 |
||
863 |
for (int i = 0; i < 8; i++) { |
|
864 |
retVal[i] = (byte) l; |
|
865 |
l >>= 8; |
|
866 |
} |
|
867 |
||
868 |
return retVal; |
|
869 |
} |
|
870 |
||
871 |
/** |
|
872 |
* Gets a default PRNG algorithm by looking through all registered |
|
873 |
* providers. Returns the first PRNG algorithm of the first provider that |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
874 |
* has registered a {@code SecureRandom} implementation, or null if none of |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
875 |
* the registered providers supplies a {@code SecureRandom} implementation. |
2 | 876 |
*/ |
877 |
private static String getPrngAlgorithm() { |
|
878 |
for (Provider p : Providers.getProviderList().providers()) { |
|
57736
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
879 |
// For SUN provider, we use SunEntries.DEFF_SECURE_RANDOM_ALGO |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
880 |
// as the default SecureRandom algorithm; for other providers, |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
881 |
// we continue to iterate through to the 1st SecureRandom |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
882 |
// service |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
883 |
if (p.getName().equals("SUN")) { |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
884 |
return SunEntries.DEF_SECURE_RANDOM_ALGO; |
7f75db20c209
8228613: java.security.Provider#getServices order is no longer deterministic
valeriep
parents:
53018
diff
changeset
|
885 |
} |
2 | 886 |
for (Service s : p.getServices()) { |
887 |
if (s.getType().equals("SecureRandom")) { |
|
888 |
return s.getAlgorithm(); |
|
889 |
} |
|
890 |
} |
|
891 |
} |
|
892 |
return null; |
|
893 |
} |
|
894 |
||
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
895 |
/* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
896 |
* Lazily initialize since Pattern.compile() is heavy. |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
897 |
* Effective Java (2nd Edition), Item 71. |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
898 |
*/ |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
899 |
private static final class StrongPatternHolder { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
900 |
/* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
901 |
* Entries are alg:prov separated by , |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
902 |
* Allow for prepended/appended whitespace between entries. |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
903 |
* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
904 |
* Capture groups: |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
905 |
* 1 - alg |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
906 |
* 2 - :prov (optional) |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
907 |
* 3 - prov (optional) |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
908 |
* 4 - ,nextEntry (optional) |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
909 |
* 5 - nextEntry (optional) |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
910 |
*/ |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
911 |
private static Pattern pattern = |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
912 |
Pattern.compile( |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
913 |
"\\s*([\\S&&[^:,]]*)(\\:([\\S&&[^,]]*))?\\s*(\\,(.*))?"); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
914 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
915 |
|
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
916 |
/** |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
917 |
* Returns a {@code SecureRandom} object that was selected by using |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
918 |
* the algorithms/providers specified in the {@code |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
919 |
* securerandom.strongAlgorithms} {@link Security} property. |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
920 |
* <p> |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
921 |
* Some situations require strong random values, such as when |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
922 |
* creating high-value/long-lived secrets like RSA public/private |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
923 |
* keys. To help guide applications in selecting a suitable strong |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
924 |
* {@code SecureRandom} implementation, Java distributions |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
925 |
* include a list of known strong {@code SecureRandom} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
926 |
* implementations in the {@code securerandom.strongAlgorithms} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
927 |
* Security property. |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
928 |
* <p> |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
929 |
* Every implementation of the Java platform is required to |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
930 |
* support at least one strong {@code SecureRandom} implementation. |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
931 |
* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
932 |
* @return a strong {@code SecureRandom} implementation as indicated |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
933 |
* by the {@code securerandom.strongAlgorithms} Security property |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
934 |
* |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
935 |
* @throws NoSuchAlgorithmException if no algorithm is available |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
936 |
* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
937 |
* @see Security#getProperty(String) |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
938 |
* |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
939 |
* @since 1.8 |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
940 |
*/ |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
941 |
public static SecureRandom getInstanceStrong() |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
942 |
throws NoSuchAlgorithmException { |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
943 |
|
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
944 |
String property = AccessController.doPrivileged( |
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
26736
diff
changeset
|
945 |
new PrivilegedAction<>() { |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
946 |
@Override |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
947 |
public String run() { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
948 |
return Security.getProperty( |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
949 |
"securerandom.strongAlgorithms"); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
950 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
951 |
}); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
952 |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
953 |
if (property == null || property.isEmpty()) { |
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
954 |
throw new NoSuchAlgorithmException( |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
955 |
"Null/empty securerandom.strongAlgorithms Security Property"); |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
956 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
957 |
|
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
958 |
String remainder = property; |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
959 |
while (remainder != null) { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
960 |
Matcher m; |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
961 |
if ((m = StrongPatternHolder.pattern.matcher( |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
962 |
remainder)).matches()) { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
963 |
|
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
964 |
String alg = m.group(1); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
965 |
String prov = m.group(3); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
966 |
|
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
967 |
try { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
968 |
if (prov == null) { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
969 |
return SecureRandom.getInstance(alg); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
970 |
} else { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
971 |
return SecureRandom.getInstance(alg, prov); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
972 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
973 |
} catch (NoSuchAlgorithmException | |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
974 |
NoSuchProviderException e) { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
975 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
976 |
remainder = m.group(5); |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
977 |
} else { |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
978 |
remainder = null; |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
979 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
980 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
981 |
|
20505
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
982 |
throw new NoSuchAlgorithmException( |
b94e6ca64006
8025694: Rename getStrongSecureRandom based on feedback
wetmore
parents:
18579
diff
changeset
|
983 |
"No strong SecureRandom impls available: " + property); |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
984 |
} |
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
9035
diff
changeset
|
985 |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
986 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
987 |
* Reseeds this {@code SecureRandom} with entropy input read from its |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
988 |
* entropy source. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
989 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
990 |
* @throws UnsupportedOperationException if the underlying provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
991 |
* implementation has not overridden this method. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
992 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
993 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
994 |
*/ |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
995 |
public void reseed() { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
996 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
997 |
secureRandomSpi.engineReseed(null); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
998 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
999 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1000 |
secureRandomSpi.engineReseed(null); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1001 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1002 |
} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1003 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1004 |
|
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1005 |
/** |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1006 |
* Reseeds this {@code SecureRandom} with entropy input read from its |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1007 |
* entropy source with additional parameters. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1008 |
* <p> |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1009 |
* Note that entropy is obtained from an entropy source. While |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1010 |
* some data in {@code params} may contain entropy, its main usage is to |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1011 |
* provide diversity. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1012 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1013 |
* @param params extra parameters |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1014 |
* @throws UnsupportedOperationException if the underlying provider |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1015 |
* implementation has not overridden this method. |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1016 |
* @throws IllegalArgumentException if {@code params} is {@code null}, |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1017 |
* illegal or unsupported by this {@code SecureRandom} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1018 |
* |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1019 |
* @since 9 |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1020 |
*/ |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1021 |
public void reseed(SecureRandomParameters params) { |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1022 |
if (params == null) { |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1023 |
throw new IllegalArgumentException("params cannot be null"); |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1024 |
} |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1025 |
if (threadSafe) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1026 |
secureRandomSpi.engineReseed(params); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1027 |
} else { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1028 |
synchronized (this) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1029 |
secureRandomSpi.engineReseed(params); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1030 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
41826
diff
changeset
|
1031 |
} |
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1032 |
} |
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1033 |
|
59086 | 1034 |
/** |
1035 |
* Returns the period of this random number generator. |
|
1036 |
* |
|
1037 |
* @return the period of this random number generator. |
|
1038 |
*/ |
|
1039 |
@Override |
|
1040 |
public BigInteger period() { |
|
1041 |
return RandomGenerator.HUGE_PERIOD; |
|
1042 |
} |
|
1043 |
||
2 | 1044 |
// Declare serialVersionUID to be compatible with JDK1.1 |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
57736
diff
changeset
|
1045 |
@java.io.Serial |
2 | 1046 |
static final long serialVersionUID = 4940670005562187L; |
1047 |
||
1048 |
// Retain unused values serialized from JDK1.1 |
|
1049 |
/** |
|
1050 |
* @serial |
|
1051 |
*/ |
|
1052 |
private byte[] state; |
|
1053 |
/** |
|
1054 |
* @serial |
|
1055 |
*/ |
|
58519
6e017b301287
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents:
57950
diff
changeset
|
1056 |
@SuppressWarnings("serial") // Not statically typed as Serializable |
2 | 1057 |
private MessageDigest digest = null; |
1058 |
/** |
|
1059 |
* @serial |
|
1060 |
* |
|
1061 |
* We know that the MessageDigest class does not implement |
|
1062 |
* java.io.Serializable. However, since this field is no longer |
|
1063 |
* used, it will always be NULL and won't affect the serialization |
|
37796
256c45c4af5d
8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
37348
diff
changeset
|
1064 |
* of the {@code SecureRandom} class itself. |
2 | 1065 |
*/ |
1066 |
private byte[] randomBytes; |
|
1067 |
/** |
|
1068 |
* @serial |
|
1069 |
*/ |
|
1070 |
private int randomBytesUsed; |
|
1071 |
/** |
|
1072 |
* @serial |
|
1073 |
*/ |
|
1074 |
private long counter; |
|
1075 |
} |