2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2016-02-08 13:09:16 -08:00
|
|
|
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2007-12-01 00:00:00 +00:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package java.security;
|
|
|
|
|
|
|
|
import java.util.*;
|
2013-04-11 21:03:24 -07:00
|
|
|
import java.util.regex.*;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
import java.security.Provider.Service;
|
|
|
|
|
|
|
|
import sun.security.jca.*;
|
|
|
|
import sun.security.jca.GetInstance.Instance;
|
2014-09-25 12:24:19 +01:00
|
|
|
import sun.security.util.Debug;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides a cryptographically strong random number
|
|
|
|
* generator (RNG).
|
|
|
|
*
|
|
|
|
* <p>A cryptographically strong random number
|
|
|
|
* minimally complies with the statistical random number generator tests
|
2014-08-08 14:17:14 -07:00
|
|
|
* specified in
|
|
|
|
* <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf">
|
2007-12-01 00:00:00 +00:00
|
|
|
* <i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>,
|
|
|
|
* section 4.9.1.
|
|
|
|
* Additionally, SecureRandom must produce non-deterministic output.
|
|
|
|
* Therefore any seed material passed to a SecureRandom object must be
|
|
|
|
* unpredictable, and all SecureRandom output sequences must be
|
|
|
|
* cryptographically strong, as described in
|
2014-08-08 14:17:14 -07:00
|
|
|
* <a href="http://tools.ietf.org/html/rfc4086">
|
|
|
|
* <i>RFC 4086: Randomness Requirements for Security</i></a>.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* <p>A caller obtains a SecureRandom instance via the
|
2013-06-28 10:48:02 -07:00
|
|
|
* no-argument constructor or one of the {@code getInstance} methods:
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* SecureRandom random = new SecureRandom();
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p> Many SecureRandom implementations are in the form of a pseudo-random
|
|
|
|
* number generator (PRNG), which means they use a deterministic algorithm
|
|
|
|
* to produce a pseudo-random sequence from a true random seed.
|
|
|
|
* Other implementations may produce true random numbers,
|
|
|
|
* and yet others may use a combination of both techniques.
|
|
|
|
*
|
|
|
|
* <p> Typical callers of SecureRandom invoke the following methods
|
|
|
|
* to retrieve random bytes:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* SecureRandom random = new SecureRandom();
|
2015-07-09 10:37:07 +03:00
|
|
|
* byte[] bytes = new byte[20];
|
2007-12-01 00:00:00 +00:00
|
|
|
* random.nextBytes(bytes);
|
|
|
|
* </pre>
|
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* <p> Callers may also invoke the {@code generateSeed} method
|
2007-12-01 00:00:00 +00:00
|
|
|
* to generate a given number of seed bytes (to seed other random number
|
|
|
|
* generators, for example):
|
|
|
|
* <pre>
|
2015-07-09 10:37:07 +03:00
|
|
|
* byte[] seed = random.generateSeed(20);
|
2007-12-01 00:00:00 +00:00
|
|
|
* </pre>
|
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* Note: Depending on the implementation, the {@code generateSeed} and
|
|
|
|
* {@code nextBytes} methods may block as entropy is being gathered,
|
2013-04-11 21:03:24 -07:00
|
|
|
* for example, if they need to read from /dev/random on various Unix-like
|
2007-12-01 00:00:00 +00:00
|
|
|
* operating systems.
|
|
|
|
*
|
|
|
|
* @see java.security.SecureRandomSpi
|
|
|
|
* @see java.util.Random
|
|
|
|
*
|
|
|
|
* @author Benjamin Renaud
|
|
|
|
* @author Josh Bloch
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class SecureRandom extends java.util.Random {
|
|
|
|
|
2014-09-25 12:24:19 +01:00
|
|
|
private static final Debug pdebug =
|
|
|
|
Debug.getInstance("provider", "Provider");
|
|
|
|
private static final boolean skipDebug =
|
|
|
|
Debug.isOn("engine=") && !Debug.isOn("securerandom");
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* The provider.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
private Provider provider = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The provider implementation.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
private SecureRandomSpi secureRandomSpi = null;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The algorithm name of null if unknown.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.5
|
|
|
|
*/
|
|
|
|
private String algorithm;
|
|
|
|
|
|
|
|
// Seed Generator
|
2015-12-21 20:54:00 +01:00
|
|
|
private static volatile SecureRandom seedGenerator;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a secure random number generator (RNG) implementing the
|
|
|
|
* default random number algorithm.
|
|
|
|
*
|
|
|
|
* <p> This constructor traverses the list of registered security Providers,
|
|
|
|
* starting with the most preferred Provider.
|
|
|
|
* A new SecureRandom object encapsulating the
|
|
|
|
* SecureRandomSpi implementation from the first
|
|
|
|
* Provider that supports a SecureRandom (RNG) algorithm is returned.
|
|
|
|
* If none of the Providers support a RNG algorithm,
|
|
|
|
* then an implementation-specific default is returned.
|
|
|
|
*
|
|
|
|
* <p> Note that the list of registered providers may be retrieved via
|
|
|
|
* the {@link Security#getProviders() Security.getProviders()} method.
|
|
|
|
*
|
2011-01-24 14:56:14 -05:00
|
|
|
* <p> See the SecureRandom section in the <a href=
|
|
|
|
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
|
|
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
|
2007-12-01 00:00:00 +00:00
|
|
|
* for information about standard RNG algorithm names.
|
|
|
|
*
|
|
|
|
* <p> The returned SecureRandom object has not been seeded. To seed the
|
2013-06-28 10:48:02 -07:00
|
|
|
* returned object, call the {@code setSeed} method.
|
|
|
|
* If {@code setSeed} is not called, the first call to
|
|
|
|
* {@code nextBytes} will force the SecureRandom object to seed itself.
|
|
|
|
* This self-seeding will not occur if {@code setSeed} was
|
2007-12-01 00:00:00 +00:00
|
|
|
* previously called.
|
|
|
|
*/
|
|
|
|
public SecureRandom() {
|
|
|
|
/*
|
|
|
|
* This call to our superclass constructor will result in a call
|
2013-06-28 10:48:02 -07:00
|
|
|
* to our own {@code setSeed} method, which will return
|
2007-12-01 00:00:00 +00:00
|
|
|
* immediately when it is passed zero.
|
|
|
|
*/
|
|
|
|
super(0);
|
|
|
|
getDefaultPRNG(false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a secure random number generator (RNG) implementing the
|
|
|
|
* default random number algorithm.
|
|
|
|
* The SecureRandom instance is seeded with the specified seed bytes.
|
|
|
|
*
|
|
|
|
* <p> This constructor traverses the list of registered security Providers,
|
|
|
|
* starting with the most preferred Provider.
|
|
|
|
* A new SecureRandom object encapsulating the
|
|
|
|
* SecureRandomSpi implementation from the first
|
|
|
|
* Provider that supports a SecureRandom (RNG) algorithm is returned.
|
|
|
|
* If none of the Providers support a RNG algorithm,
|
|
|
|
* then an implementation-specific default is returned.
|
|
|
|
*
|
|
|
|
* <p> Note that the list of registered providers may be retrieved via
|
|
|
|
* the {@link Security#getProviders() Security.getProviders()} method.
|
|
|
|
*
|
2011-01-24 14:56:14 -05:00
|
|
|
* <p> See the SecureRandom section in the <a href=
|
|
|
|
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
|
|
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
|
2007-12-01 00:00:00 +00:00
|
|
|
* for information about standard RNG algorithm names.
|
|
|
|
*
|
|
|
|
* @param seed the seed.
|
|
|
|
*/
|
2015-07-09 10:37:07 +03:00
|
|
|
public SecureRandom(byte[] seed) {
|
2007-12-01 00:00:00 +00:00
|
|
|
super(0);
|
|
|
|
getDefaultPRNG(true, seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getDefaultPRNG(boolean setSeed, byte[] seed) {
|
|
|
|
String prng = getPrngAlgorithm();
|
|
|
|
if (prng == null) {
|
|
|
|
// bummer, get the SUN implementation
|
|
|
|
prng = "SHA1PRNG";
|
|
|
|
this.secureRandomSpi = new sun.security.provider.SecureRandom();
|
|
|
|
this.provider = Providers.getSunProvider();
|
|
|
|
if (setSeed) {
|
|
|
|
this.secureRandomSpi.engineSetSeed(seed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
SecureRandom random = SecureRandom.getInstance(prng);
|
|
|
|
this.secureRandomSpi = random.getSecureRandomSpi();
|
|
|
|
this.provider = random.getProvider();
|
|
|
|
if (setSeed) {
|
|
|
|
this.secureRandomSpi.engineSetSeed(seed);
|
|
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException nsae) {
|
|
|
|
// never happens, because we made sure the algorithm exists
|
|
|
|
throw new RuntimeException(nsae);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// JDK 1.1 based implementations subclass SecureRandom instead of
|
|
|
|
// SecureRandomSpi. They will also go through this code path because
|
|
|
|
// they must call a SecureRandom constructor as it is their superclass.
|
|
|
|
// If we are dealing with such an implementation, do not set the
|
|
|
|
// algorithm value as it would be inaccurate.
|
|
|
|
if (getClass() == SecureRandom.class) {
|
|
|
|
this.algorithm = prng;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a SecureRandom object.
|
|
|
|
*
|
|
|
|
* @param secureRandomSpi the SecureRandom implementation.
|
|
|
|
* @param provider the provider.
|
|
|
|
*/
|
|
|
|
protected SecureRandom(SecureRandomSpi secureRandomSpi,
|
|
|
|
Provider provider) {
|
|
|
|
this(secureRandomSpi, provider, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider,
|
|
|
|
String algorithm) {
|
|
|
|
super(0);
|
|
|
|
this.secureRandomSpi = secureRandomSpi;
|
|
|
|
this.provider = provider;
|
|
|
|
this.algorithm = algorithm;
|
2014-09-25 12:24:19 +01:00
|
|
|
|
|
|
|
if (!skipDebug && pdebug != null) {
|
|
|
|
pdebug.println("SecureRandom." + algorithm +
|
|
|
|
" algorithm from: " + this.provider.getName());
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a SecureRandom object that implements the specified
|
|
|
|
* Random Number Generator (RNG) algorithm.
|
|
|
|
*
|
|
|
|
* <p> This method traverses the list of registered security Providers,
|
|
|
|
* starting with the most preferred Provider.
|
|
|
|
* A new SecureRandom object encapsulating the
|
|
|
|
* SecureRandomSpi implementation from the first
|
|
|
|
* Provider that supports the specified algorithm is returned.
|
|
|
|
*
|
|
|
|
* <p> Note that the list of registered providers may be retrieved via
|
|
|
|
* the {@link Security#getProviders() Security.getProviders()} method.
|
|
|
|
*
|
|
|
|
* <p> The returned SecureRandom object has not been seeded. To seed the
|
2013-06-28 10:48:02 -07:00
|
|
|
* returned object, call the {@code setSeed} method.
|
|
|
|
* If {@code setSeed} is not called, the first call to
|
|
|
|
* {@code nextBytes} will force the SecureRandom object to seed itself.
|
|
|
|
* This self-seeding will not occur if {@code setSeed} was
|
2007-12-01 00:00:00 +00:00
|
|
|
* previously called.
|
|
|
|
*
|
2015-10-19 17:35:18 -07:00
|
|
|
* @implNote
|
|
|
|
* The JDK Reference Implementation additionally uses the
|
|
|
|
* {@code jdk.security.provider.preferred} property to determine
|
|
|
|
* the preferred provider order for the specified algorithm. This
|
|
|
|
* may be different than the order of providers returned by
|
|
|
|
* {@link Security#getProviders() Security.getProviders()}.
|
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* @param algorithm the name of the RNG algorithm.
|
2011-01-24 14:56:14 -05:00
|
|
|
* See the SecureRandom section in the <a href=
|
|
|
|
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
|
|
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
|
2007-12-01 00:00:00 +00:00
|
|
|
* for information about standard RNG algorithm names.
|
|
|
|
*
|
|
|
|
* @return the new SecureRandom object.
|
|
|
|
*
|
|
|
|
* @exception NoSuchAlgorithmException if no Provider supports a
|
|
|
|
* SecureRandomSpi implementation for the
|
|
|
|
* specified algorithm.
|
|
|
|
*
|
|
|
|
* @see Provider
|
|
|
|
*
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
public static SecureRandom getInstance(String algorithm)
|
|
|
|
throws NoSuchAlgorithmException {
|
|
|
|
Instance instance = GetInstance.getInstance("SecureRandom",
|
|
|
|
SecureRandomSpi.class, algorithm);
|
|
|
|
return new SecureRandom((SecureRandomSpi)instance.impl,
|
|
|
|
instance.provider, algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a SecureRandom object that implements the specified
|
|
|
|
* Random Number Generator (RNG) algorithm.
|
|
|
|
*
|
|
|
|
* <p> A new SecureRandom object encapsulating the
|
|
|
|
* SecureRandomSpi implementation from the specified provider
|
|
|
|
* is returned. The specified provider must be registered
|
|
|
|
* in the security provider list.
|
|
|
|
*
|
|
|
|
* <p> Note that the list of registered providers may be retrieved via
|
|
|
|
* the {@link Security#getProviders() Security.getProviders()} method.
|
|
|
|
*
|
|
|
|
* <p> The returned SecureRandom object has not been seeded. To seed the
|
2013-06-28 10:48:02 -07:00
|
|
|
* returned object, call the {@code setSeed} method.
|
|
|
|
* If {@code setSeed} is not called, the first call to
|
|
|
|
* {@code nextBytes} will force the SecureRandom object to seed itself.
|
|
|
|
* This self-seeding will not occur if {@code setSeed} was
|
2007-12-01 00:00:00 +00:00
|
|
|
* previously called.
|
|
|
|
*
|
|
|
|
* @param algorithm the name of the RNG algorithm.
|
2011-01-24 14:56:14 -05:00
|
|
|
* See the SecureRandom section in the <a href=
|
|
|
|
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
|
|
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
|
2007-12-01 00:00:00 +00:00
|
|
|
* for information about standard RNG algorithm names.
|
|
|
|
*
|
|
|
|
* @param provider the name of the provider.
|
|
|
|
*
|
|
|
|
* @return the new SecureRandom object.
|
|
|
|
*
|
|
|
|
* @exception NoSuchAlgorithmException if a SecureRandomSpi
|
|
|
|
* implementation for the specified algorithm is not
|
|
|
|
* available from the specified provider.
|
|
|
|
*
|
|
|
|
* @exception NoSuchProviderException if the specified provider is not
|
|
|
|
* registered in the security provider list.
|
|
|
|
*
|
|
|
|
* @exception IllegalArgumentException if the provider name is null
|
|
|
|
* or empty.
|
|
|
|
*
|
|
|
|
* @see Provider
|
|
|
|
*
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
public static SecureRandom getInstance(String algorithm, String provider)
|
|
|
|
throws NoSuchAlgorithmException, NoSuchProviderException {
|
|
|
|
Instance instance = GetInstance.getInstance("SecureRandom",
|
|
|
|
SecureRandomSpi.class, algorithm, provider);
|
|
|
|
return new SecureRandom((SecureRandomSpi)instance.impl,
|
|
|
|
instance.provider, algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a SecureRandom object that implements the specified
|
|
|
|
* Random Number Generator (RNG) algorithm.
|
|
|
|
*
|
|
|
|
* <p> A new SecureRandom object encapsulating the
|
|
|
|
* SecureRandomSpi implementation from the specified Provider
|
|
|
|
* object is returned. Note that the specified Provider object
|
|
|
|
* does not have to be registered in the provider list.
|
|
|
|
*
|
|
|
|
* <p> The returned SecureRandom object has not been seeded. To seed the
|
2013-06-28 10:48:02 -07:00
|
|
|
* returned object, call the {@code setSeed} method.
|
|
|
|
* If {@code setSeed} is not called, the first call to
|
|
|
|
* {@code nextBytes} will force the SecureRandom object to seed itself.
|
|
|
|
* This self-seeding will not occur if {@code setSeed} was
|
2007-12-01 00:00:00 +00:00
|
|
|
* previously called.
|
|
|
|
*
|
|
|
|
* @param algorithm the name of the RNG algorithm.
|
2011-01-24 14:56:14 -05:00
|
|
|
* See the SecureRandom section in the <a href=
|
|
|
|
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
|
|
|
|
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
|
2007-12-01 00:00:00 +00:00
|
|
|
* for information about standard RNG algorithm names.
|
|
|
|
*
|
|
|
|
* @param provider the provider.
|
|
|
|
*
|
|
|
|
* @return the new SecureRandom object.
|
|
|
|
*
|
|
|
|
* @exception NoSuchAlgorithmException if a SecureRandomSpi
|
|
|
|
* implementation for the specified algorithm is not available
|
|
|
|
* from the specified Provider object.
|
|
|
|
*
|
|
|
|
* @exception IllegalArgumentException if the specified provider is null.
|
|
|
|
*
|
|
|
|
* @see Provider
|
|
|
|
*
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public static SecureRandom getInstance(String algorithm,
|
|
|
|
Provider provider) throws NoSuchAlgorithmException {
|
|
|
|
Instance instance = GetInstance.getInstance("SecureRandom",
|
|
|
|
SecureRandomSpi.class, algorithm, provider);
|
|
|
|
return new SecureRandom((SecureRandomSpi)instance.impl,
|
|
|
|
instance.provider, algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the SecureRandomSpi of this SecureRandom object.
|
|
|
|
*/
|
|
|
|
SecureRandomSpi getSecureRandomSpi() {
|
|
|
|
return secureRandomSpi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the provider of this SecureRandom object.
|
|
|
|
*
|
|
|
|
* @return the provider of this SecureRandom object.
|
|
|
|
*/
|
|
|
|
public final Provider getProvider() {
|
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the algorithm implemented by this SecureRandom
|
|
|
|
* object.
|
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* @return the name of the algorithm or {@code unknown}
|
2007-12-01 00:00:00 +00:00
|
|
|
* if the algorithm name cannot be determined.
|
|
|
|
* @since 1.5
|
|
|
|
*/
|
|
|
|
public String getAlgorithm() {
|
2015-11-11 22:38:15 -05:00
|
|
|
return Objects.toString(algorithm, "unknown");
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reseeds this random object. The given seed supplements, rather than
|
|
|
|
* replaces, the existing seed. Thus, repeated calls are guaranteed
|
|
|
|
* never to reduce randomness.
|
|
|
|
*
|
|
|
|
* @param seed the seed.
|
|
|
|
*
|
|
|
|
* @see #getSeed
|
|
|
|
*/
|
2015-09-15 21:56:04 -07:00
|
|
|
public synchronized void setSeed(byte[] seed) {
|
2007-12-01 00:00:00 +00:00
|
|
|
secureRandomSpi.engineSetSeed(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reseeds this random object, using the eight bytes contained
|
2013-06-28 10:48:02 -07:00
|
|
|
* in the given {@code long seed}. The given seed supplements,
|
2007-12-01 00:00:00 +00:00
|
|
|
* rather than replaces, the existing seed. Thus, repeated calls
|
|
|
|
* are guaranteed never to reduce randomness.
|
|
|
|
*
|
|
|
|
* <p>This method is defined for compatibility with
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code java.util.Random}.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @param seed the seed.
|
|
|
|
*
|
|
|
|
* @see #getSeed
|
|
|
|
*/
|
2013-04-11 21:03:24 -07:00
|
|
|
@Override
|
2007-12-01 00:00:00 +00:00
|
|
|
public void setSeed(long seed) {
|
|
|
|
/*
|
|
|
|
* Ignore call from super constructor (as well as any other calls
|
|
|
|
* unfortunate enough to be passing 0). It's critical that we
|
|
|
|
* ignore call from superclass constructor, as digest has not
|
|
|
|
* yet been initialized at that point.
|
|
|
|
*/
|
|
|
|
if (seed != 0) {
|
|
|
|
secureRandomSpi.engineSetSeed(longToByteArray(seed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a user-specified number of random bytes.
|
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* <p> If a call to {@code setSeed} had not occurred previously,
|
2007-12-01 00:00:00 +00:00
|
|
|
* the first call to this method forces this SecureRandom object
|
|
|
|
* to seed itself. This self-seeding will not occur if
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code setSeed} was previously called.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @param bytes the array to be filled in with random bytes.
|
|
|
|
*/
|
2013-04-11 21:03:24 -07:00
|
|
|
@Override
|
2016-02-08 13:09:16 -08:00
|
|
|
public void nextBytes(byte[] bytes) {
|
2007-12-01 00:00:00 +00:00
|
|
|
secureRandomSpi.engineNextBytes(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an integer containing the user-specified number of
|
|
|
|
* pseudo-random bits (right justified, with leading zeros). This
|
2013-06-28 10:48:02 -07:00
|
|
|
* method overrides a {@code java.util.Random} method, and serves
|
2007-12-01 00:00:00 +00:00
|
|
|
* to provide a source of random bits to all of the methods inherited
|
2013-06-28 10:48:02 -07:00
|
|
|
* from that class (for example, {@code nextInt},
|
|
|
|
* {@code nextLong}, and {@code nextFloat}).
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @param numBits number of pseudo-random bits to be generated, where
|
2013-06-10 12:58:32 +01:00
|
|
|
* {@code 0 <= numBits <= 32}.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* @return an {@code int} containing the user-specified number
|
2007-12-01 00:00:00 +00:00
|
|
|
* of pseudo-random bits (right justified, with leading zeros).
|
|
|
|
*/
|
2013-04-11 21:03:24 -07:00
|
|
|
@Override
|
2015-09-15 21:56:04 -07:00
|
|
|
protected final int next(int numBits) {
|
2007-12-01 00:00:00 +00:00
|
|
|
int numBytes = (numBits+7)/8;
|
2015-07-09 10:37:07 +03:00
|
|
|
byte[] b = new byte[numBytes];
|
2007-12-01 00:00:00 +00:00
|
|
|
int next = 0;
|
|
|
|
|
|
|
|
nextBytes(b);
|
2013-04-11 21:03:24 -07:00
|
|
|
for (int i = 0; i < numBytes; i++) {
|
2007-12-01 00:00:00 +00:00
|
|
|
next = (next << 8) + (b[i] & 0xFF);
|
2013-04-11 21:03:24 -07:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
return next >>> (numBytes*8 - numBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given number of seed bytes, computed using the seed
|
|
|
|
* generation algorithm that this class uses to seed itself. This
|
|
|
|
* call may be used to seed other random number generators.
|
|
|
|
*
|
|
|
|
* <p>This method is only included for backwards compatibility.
|
|
|
|
* The caller is encouraged to use one of the alternative
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code getInstance} methods to obtain a SecureRandom object, and
|
|
|
|
* then call the {@code generateSeed} method to obtain seed bytes
|
2007-12-01 00:00:00 +00:00
|
|
|
* from that object.
|
|
|
|
*
|
|
|
|
* @param numBytes the number of seed bytes to generate.
|
|
|
|
*
|
|
|
|
* @return the seed bytes.
|
|
|
|
*
|
|
|
|
* @see #setSeed
|
|
|
|
*/
|
|
|
|
public static byte[] getSeed(int numBytes) {
|
2015-12-21 20:54:00 +01:00
|
|
|
SecureRandom seedGen = seedGenerator;
|
|
|
|
if (seedGen == null) {
|
|
|
|
seedGen = new SecureRandom();
|
|
|
|
seedGenerator = seedGen;
|
2013-04-11 21:03:24 -07:00
|
|
|
}
|
2015-12-21 20:54:00 +01:00
|
|
|
return seedGen.generateSeed(numBytes);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given number of seed bytes, computed using the seed
|
|
|
|
* generation algorithm that this class uses to seed itself. This
|
|
|
|
* call may be used to seed other random number generators.
|
|
|
|
*
|
|
|
|
* @param numBytes the number of seed bytes to generate.
|
|
|
|
*
|
|
|
|
* @return the seed bytes.
|
|
|
|
*/
|
|
|
|
public byte[] generateSeed(int numBytes) {
|
|
|
|
return secureRandomSpi.engineGenerateSeed(numBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to convert a long into a byte array (least significant
|
|
|
|
* byte first).
|
|
|
|
*/
|
|
|
|
private static byte[] longToByteArray(long l) {
|
|
|
|
byte[] retVal = new byte[8];
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
retVal[i] = (byte) l;
|
|
|
|
l >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a default PRNG algorithm by looking through all registered
|
|
|
|
* providers. Returns the first PRNG algorithm of the first provider that
|
|
|
|
* has registered a SecureRandom implementation, or null if none of the
|
|
|
|
* registered providers supplies a SecureRandom implementation.
|
|
|
|
*/
|
|
|
|
private static String getPrngAlgorithm() {
|
|
|
|
for (Provider p : Providers.getProviderList().providers()) {
|
|
|
|
for (Service s : p.getServices()) {
|
|
|
|
if (s.getType().equals("SecureRandom")) {
|
|
|
|
return s.getAlgorithm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:03:24 -07:00
|
|
|
/*
|
|
|
|
* Lazily initialize since Pattern.compile() is heavy.
|
|
|
|
* Effective Java (2nd Edition), Item 71.
|
|
|
|
*/
|
|
|
|
private static final class StrongPatternHolder {
|
|
|
|
/*
|
|
|
|
* Entries are alg:prov separated by ,
|
|
|
|
* Allow for prepended/appended whitespace between entries.
|
|
|
|
*
|
|
|
|
* Capture groups:
|
|
|
|
* 1 - alg
|
|
|
|
* 2 - :prov (optional)
|
|
|
|
* 3 - prov (optional)
|
|
|
|
* 4 - ,nextEntry (optional)
|
|
|
|
* 5 - nextEntry (optional)
|
|
|
|
*/
|
|
|
|
private static Pattern pattern =
|
|
|
|
Pattern.compile(
|
|
|
|
"\\s*([\\S&&[^:,]]*)(\\:([\\S&&[^,]]*))?\\s*(\\,(.*))?");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a {@code SecureRandom} object that was selected by using
|
|
|
|
* the algorithms/providers specified in the {@code
|
2013-10-02 09:38:57 -07:00
|
|
|
* securerandom.strongAlgorithms} {@link Security} property.
|
2013-04-11 21:03:24 -07:00
|
|
|
* <p>
|
|
|
|
* Some situations require strong random values, such as when
|
|
|
|
* creating high-value/long-lived secrets like RSA public/private
|
|
|
|
* keys. To help guide applications in selecting a suitable strong
|
2013-10-02 09:38:57 -07:00
|
|
|
* {@code SecureRandom} implementation, Java distributions
|
2013-04-11 21:03:24 -07:00
|
|
|
* include a list of known strong {@code SecureRandom}
|
|
|
|
* implementations in the {@code securerandom.strongAlgorithms}
|
|
|
|
* Security property.
|
2013-10-02 09:38:57 -07:00
|
|
|
* <p>
|
|
|
|
* Every implementation of the Java platform is required to
|
|
|
|
* support at least one strong {@code SecureRandom} implementation.
|
2013-04-11 21:03:24 -07:00
|
|
|
*
|
|
|
|
* @return a strong {@code SecureRandom} implementation as indicated
|
2013-10-02 09:38:57 -07:00
|
|
|
* by the {@code securerandom.strongAlgorithms} Security property
|
|
|
|
*
|
|
|
|
* @throws NoSuchAlgorithmException if no algorithm is available
|
2013-04-11 21:03:24 -07:00
|
|
|
*
|
|
|
|
* @see Security#getProperty(String)
|
|
|
|
*
|
|
|
|
* @since 1.8
|
|
|
|
*/
|
2013-10-02 09:38:57 -07:00
|
|
|
public static SecureRandom getInstanceStrong()
|
|
|
|
throws NoSuchAlgorithmException {
|
2013-04-11 21:03:24 -07:00
|
|
|
|
|
|
|
String property = AccessController.doPrivileged(
|
2015-04-23 18:51:18 -07:00
|
|
|
new PrivilegedAction<>() {
|
2013-04-11 21:03:24 -07:00
|
|
|
@Override
|
|
|
|
public String run() {
|
|
|
|
return Security.getProperty(
|
|
|
|
"securerandom.strongAlgorithms");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ((property == null) || (property.length() == 0)) {
|
2013-10-02 09:38:57 -07:00
|
|
|
throw new NoSuchAlgorithmException(
|
|
|
|
"Null/empty securerandom.strongAlgorithms Security Property");
|
2013-04-11 21:03:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
String remainder = property;
|
|
|
|
while (remainder != null) {
|
|
|
|
Matcher m;
|
|
|
|
if ((m = StrongPatternHolder.pattern.matcher(
|
|
|
|
remainder)).matches()) {
|
|
|
|
|
|
|
|
String alg = m.group(1);
|
|
|
|
String prov = m.group(3);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (prov == null) {
|
|
|
|
return SecureRandom.getInstance(alg);
|
|
|
|
} else {
|
|
|
|
return SecureRandom.getInstance(alg, prov);
|
|
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException |
|
|
|
|
NoSuchProviderException e) {
|
|
|
|
}
|
|
|
|
remainder = m.group(5);
|
|
|
|
} else {
|
|
|
|
remainder = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:38:57 -07:00
|
|
|
throw new NoSuchAlgorithmException(
|
|
|
|
"No strong SecureRandom impls available: " + property);
|
2013-04-11 21:03:24 -07:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Declare serialVersionUID to be compatible with JDK1.1
|
|
|
|
static final long serialVersionUID = 4940670005562187L;
|
|
|
|
|
|
|
|
// Retain unused values serialized from JDK1.1
|
|
|
|
/**
|
|
|
|
* @serial
|
|
|
|
*/
|
|
|
|
private byte[] state;
|
|
|
|
/**
|
|
|
|
* @serial
|
|
|
|
*/
|
|
|
|
private MessageDigest digest = null;
|
|
|
|
/**
|
|
|
|
* @serial
|
|
|
|
*
|
|
|
|
* We know that the MessageDigest class does not implement
|
|
|
|
* java.io.Serializable. However, since this field is no longer
|
|
|
|
* used, it will always be NULL and won't affect the serialization
|
|
|
|
* of the SecureRandom class itself.
|
|
|
|
*/
|
|
|
|
private byte[] randomBytes;
|
|
|
|
/**
|
|
|
|
* @serial
|
|
|
|
*/
|
|
|
|
private int randomBytesUsed;
|
|
|
|
/**
|
|
|
|
* @serial
|
|
|
|
*/
|
|
|
|
private long counter;
|
|
|
|
}
|