42 |
import java.util.Enumeration; |
import java.util.Enumeration; |
43 |
|
|
44 |
/** |
/** |
45 |
SecureRandom is the class interface for using SecureRandom |
* An interface to a cryptographically secure pseudo-random number |
46 |
providers. It provides an interface to the SecureRandomSpi |
* generator (PRNG). Random (or at least unguessable) numbers are used |
47 |
engine so that programmers can generate pseudo-random numbers. |
* in all areas of security and cryptography, from the generation of |
48 |
|
* keys and initialization vectors to the generation of random padding |
49 |
@author Mark Benvenuto <ivymccough@worldnet.att.net> |
* bytes. |
50 |
|
* |
51 |
|
* @author Mark Benvenuto <ivymccough@worldnet.att.net> |
52 |
|
* @author Casey Marshall |
53 |
*/ |
*/ |
54 |
public class SecureRandom extends Random |
public class SecureRandom extends Random |
55 |
{ |
{ |
56 |
|
|
57 |
|
// Constants and fields. |
58 |
|
// ------------------------------------------------------------------------ |
59 |
|
|
60 |
|
/** Service name for PRNGs. */ |
61 |
|
private static final String SECURE_RANDOM = "SecureRandom"; |
62 |
|
|
63 |
static final long serialVersionUID = 4940670005562187L; |
static final long serialVersionUID = 4940670005562187L; |
64 |
|
|
65 |
//Serialized Field |
//Serialized Field |
70 |
SecureRandomSpi secureRandomSpi = null; |
SecureRandomSpi secureRandomSpi = null; |
71 |
byte[] state = null; |
byte[] state = null; |
72 |
|
|
73 |
|
// Constructors. |
74 |
|
// ------------------------------------------------------------------------ |
75 |
|
|
76 |
/** |
/** |
77 |
Default constructor for SecureRandom. It constructs a |
Default constructor for SecureRandom. It constructs a |
78 |
new SecureRandom by instantating the first SecureRandom |
new SecureRandom by instantating the first SecureRandom |
82 |
on the first call to getnextBytes it will force a seed. |
on the first call to getnextBytes it will force a seed. |
83 |
|
|
84 |
It is maintained for backwards compatibility and programs |
It is maintained for backwards compatibility and programs |
85 |
should use getInstance. |
should use {@link #getInstance(java.lang.String)}. |
86 |
*/ |
*/ |
87 |
public SecureRandom() |
public SecureRandom() |
88 |
{ |
{ |
101 |
{ |
{ |
102 |
key = (String) e.nextElement(); |
key = (String) e.nextElement(); |
103 |
if (key.startsWith("SECURERANDOM.")) |
if (key.startsWith("SECURERANDOM.")) |
104 |
{ |
{ |
105 |
if ((classname = p[i].getProperty(key)) != null) |
if ((classname = p[i].getProperty(key)) != null) |
106 |
{ |
{ |
107 |
try |
try |
108 |
{ |
{ |
109 |
secureRandomSpi = (SecureRandomSpi) Class. |
secureRandomSpi = (SecureRandomSpi) Class. |
110 |
forName(classname).newInstance(); |
forName(classname).newInstance(); |
111 |
provider = p[i]; |
provider = p[i]; |
112 |
return; |
return; |
113 |
} |
} |
114 |
catch (Throwable ignore) { } |
catch (Throwable ignore) { } |
115 |
} |
} |
116 |
} |
} |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
120 |
// Nothing found. Fall back to SHA1PRNG |
// Nothing found. Fall back to SHA1PRNG |
154 |
this.provider = provider; |
this.provider = provider; |
155 |
} |
} |
156 |
|
|
157 |
/** |
// Class methods. |
158 |
Returns an instance of a SecureRandom. It creates the class |
// ------------------------------------------------------------------------ |
|
for the specified algorithm if it exists from a provider. |
|
|
|
|
|
@param algorithm A SecureRandom algorithm to use |
|
159 |
|
|
160 |
@return Returns a new SecureRandom implmenting the chosen algorithm |
/** |
161 |
|
* Returns an instance of a SecureRandom. It creates the class from |
162 |
@throws NoSuchAlgorithmException if the algorithm cannot be found |
* the first provider that implements it. |
163 |
|
* |
164 |
|
* @param algorithm The algorithm name. |
165 |
|
* @return A new SecureRandom implmenting the given algorithm. |
166 |
|
* @throws NoSuchAlgorithmException If no installed provider implements |
167 |
|
* the given algorithm. |
168 |
*/ |
*/ |
169 |
public static SecureRandom getInstance(String algorithm) throws |
public static SecureRandom getInstance(String algorithm) throws |
170 |
NoSuchAlgorithmException |
NoSuchAlgorithmException |
172 |
Provider p[] = Security.getProviders(); |
Provider p[] = Security.getProviders(); |
173 |
for (int i = 0; i < p.length; i++) |
for (int i = 0; i < p.length; i++) |
174 |
{ |
{ |
175 |
try |
try |
176 |
{ |
{ |
177 |
return getInstance(algorithm, p[i]); |
return getInstance(algorithm, p[i]); |
178 |
} |
} |
179 |
catch (NoSuchAlgorithmException ignored) { } |
catch (NoSuchAlgorithmException ignored) |
180 |
|
{ |
181 |
|
} |
182 |
} |
} |
183 |
|
|
184 |
// None found. |
// None found. |
186 |
} |
} |
187 |
|
|
188 |
/** |
/** |
189 |
Returns an instance of a SecureRandom. It creates the class |
* Returns an instance of a SecureRandom. It creates the class |
190 |
for the specified algorithm from the specified provider. |
* for the specified algorithm from the named provider. |
191 |
|
* |
192 |
@param algorithm A SecureRandom algorithm to use |
* @param algorithm The algorithm name. |
193 |
@param provider A security provider to use |
* @param provider The provider name. |
194 |
|
* @return A new SecureRandom implmenting the chosen algorithm. |
195 |
@return Returns a new SecureRandom implmenting the chosen algorithm |
* @throws NoSuchAlgorithmException If the named provider does not implement |
196 |
|
* the algorithm, or if the implementation cannot be |
197 |
@throws NoSuchAlgorithmException if the algorithm cannot be found |
* instantiated. |
198 |
@throws NoSuchProviderException if the provider cannot be found |
* @throws NoSuchProviderException If no provider named |
199 |
|
* <code>provider</code> is currently installed. |
200 |
|
* @throws IllegalArgumentException If <code>provider</code> is null |
201 |
|
* or is empty. |
202 |
*/ |
*/ |
203 |
public static SecureRandom getInstance(String algorithm, |
public static SecureRandom getInstance(String algorithm, String provider) |
204 |
String provider) throws |
throws NoSuchAlgorithmException, NoSuchProviderException |
|
NoSuchAlgorithmException, NoSuchProviderException |
|
205 |
{ |
{ |
206 |
|
if (provider == null || provider.length() == 0) |
207 |
|
throw new IllegalArgumentException("Illegal provider"); |
208 |
|
|
209 |
Provider p = Security.getProvider(provider); |
Provider p = Security.getProvider(provider); |
210 |
if (p == null) |
if (p == null) |
211 |
throw new NoSuchProviderException(); |
throw new NoSuchProviderException(); |
214 |
} |
} |
215 |
|
|
216 |
/** |
/** |
217 |
Returns an instance of a SecureRandom. It creates the class for |
* Returns an instance of a SecureRandom. It creates the class for |
218 |
the specified algorithm from the given provider. |
* the specified algorithm from the given provider. |
219 |
|
* |
220 |
@param algorithm The SecureRandom algorithm to create. |
* @param algorithm The SecureRandom algorithm to create. |
221 |
@param provider The provider to get the instance from. |
* @param provider The provider to get the instance from. |
222 |
|
* @throws NoSuchAlgorithmException If the algorithm cannot be found, or |
223 |
@throws NoSuchAlgorithmException If the algorithm cannot be found, or |
* if the class cannot be instantiated. |
224 |
if the class cannot be instantiated. |
* @throws IllegalArgumentException If <code>provider</code> is null. |
225 |
*/ |
*/ |
226 |
public static SecureRandom getInstance(String algorithm, |
public static SecureRandom getInstance(String algorithm, Provider provider) |
227 |
Provider provider) throws |
throws NoSuchAlgorithmException |
228 |
NoSuchAlgorithmException |
{ |
229 |
{ |
if (provider == null) |
230 |
return getInstance(algorithm, provider, true); |
throw new IllegalArgumentException("Illegal provider"); |
231 |
} |
try |
|
|
|
|
/** |
|
|
Creates the instance of SecureRandom, recursing to resolve aliases. |
|
|
|
|
|
@param algorithm The SecureRandom algorithm to create. |
|
|
@param provider The provider to get the implementation from. |
|
|
@param recurse Whether or not to recurse to resolve aliases. |
|
|
|
|
|
@throws NoSuchAlgorithmException If the algorithm cannot be found, |
|
|
if there are too many aliases, or if the class cannot be |
|
|
instantiated. |
|
|
*/ |
|
|
private static SecureRandom getInstance(String algorithm, |
|
|
Provider provider, |
|
|
boolean recurse) |
|
|
throws NoSuchAlgorithmException |
|
|
{ |
|
|
String msg = algorithm; |
|
|
for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); ) |
|
232 |
{ |
{ |
233 |
// We could replace the boolean with an integer, incrementing it |
return new SecureRandom((SecureRandomSpi) |
234 |
// every |
Engine.getInstance(SECURE_RANDOM, algorithm, provider), |
235 |
String key = (String) e.nextElement(); |
provider); |
236 |
if (key.startsWith("SECURERANDOM.") |
} |
237 |
&& key.substring(13).equalsIgnoreCase(algorithm)) |
catch (ClassCastException cce) |
238 |
{ |
{ |
239 |
try |
throw new NoSuchAlgorithmException(algorithm); |
|
{ |
|
|
Class c = Class.forName(provider.getProperty(key)); |
|
|
return new SecureRandom((SecureRandomSpi) c.newInstance(), |
|
|
provider); |
|
|
} |
|
|
catch (Throwable ignored) { } |
|
|
} |
|
|
else if (key.startsWith("ALG.ALIAS.SECURERANDOM.") |
|
|
&& key.substring(23).equalsIgnoreCase(algorithm) && recurse) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
// First see if this alias refers to a class in this |
|
|
// provider. |
|
|
return getInstance(provider.getProperty(key), provider, false); |
|
|
} |
|
|
catch (NoSuchAlgorithmException nsae) |
|
|
{ |
|
|
Provider[] provs = Security.getProviders(); |
|
|
for (int i = 0; i < provs.length; i++) |
|
|
{ |
|
|
if (provs[i] == provider) |
|
|
continue; |
|
|
// Now try other providers for the implementation |
|
|
try |
|
|
{ |
|
|
return getInstance(provider.getProperty(key), |
|
|
provs[i], false); |
|
|
} |
|
|
catch (NoSuchAlgorithmException nsae2) |
|
|
{ |
|
|
msg = nsae2.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
240 |
} |
} |
|
throw new NoSuchAlgorithmException(algorithm); |
|
241 |
} |
} |
242 |
|
|
243 |
|
// Instance methods. |
244 |
|
// ------------------------------------------------------------------------ |
245 |
|
|
246 |
/** |
/** |
247 |
Returns the provider being used by the current SecureRandom class. |
Returns the provider being used by the current SecureRandom class. |
248 |
|
|
287 |
(byte) (0xff & (seed >> 16)), |
(byte) (0xff & (seed >> 16)), |
288 |
(byte) (0xff & (seed >> 8)), |
(byte) (0xff & (seed >> 8)), |
289 |
(byte) (0xff & seed) |
(byte) (0xff & seed) |
290 |
}; |
}; |
291 |
secureRandomSpi.engineSetSeed(tmp); |
secureRandomSpi.engineSetSeed(tmp); |
292 |
} |
} |
293 |
} |
} |
294 |
|
|