src/java.base/share/classes/java/security/SecureRandom.java
author jlaskey
Thu, 14 Nov 2019 12:39:49 -0400
branchJDK-8193209-branch
changeset 59086 214afc7a1e02
parent 58519 6e017b301287
permissions -rw-r--r--
[mq]: refresh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
    28
import java.math.BigInteger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class provides a cryptographically strong random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * generator (RNG).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * cryptographically strong, as described in
25972
dca4e4c83da4 8054366: Broken link in SecureRandom.html
ascarpino
parents: 20505
diff changeset
    51
 * <a href="http://tools.ietf.org/html/rfc4086">
dca4e4c83da4 8054366: Broken link in SecureRandom.html
ascarpino
parents: 20505
diff changeset
    52
 * <i>RFC 4086: Randomness Requirements for Security</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Other implementations may produce true random numbers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * and yet others may use a combination of both techniques.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
b2566397a3c0 8174909: Doc error in SecureRandom
weijun
parents: 42780
diff changeset
    68
 * SecureRandom r3 = SecureRandom.getInstance("DRBG",
b2566397a3c0 8174909: Doc error in SecureRandom
weijun
parents: 42780
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * to retrieve random bytes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * to generate a given number of seed bytes (to seed other random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * @see java.security.SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @see java.util.Random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
public class SecureRandom extends java.util.Random {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private Provider provider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * The provider implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private SecureRandomSpi secureRandomSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * The algorithm name of null if unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * default random number algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public SecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * immediately when it is passed zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   255
    public SecureRandom(byte[] seed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        getDefaultPRNG(true, seed);
42161
5b0b84715c06 7004967: SecureRandom should be more explicit about threading
weijun
parents: 41826
diff changeset
   258
        this.threadSafe = getThreadSafe();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private void getDefaultPRNG(boolean setSeed, byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        String prng = getPrngAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (prng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // bummer, get the SUN implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            prng = "SHA1PRNG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            this.secureRandomSpi = new sun.security.provider.SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            this.provider = Providers.getSunProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                SecureRandom random = SecureRandom.getInstance(prng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                this.secureRandomSpi = random.getSecureRandomSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                this.provider = random.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // never happens, because we made sure the algorithm exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new RuntimeException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // JDK 1.1 based implementations subclass SecureRandom instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // SecureRandomSpi. They will also go through this code path because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // they must call a SecureRandom constructor as it is their superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // If we are dealing with such an implementation, do not set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // algorithm value as it would be inaccurate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (getClass() == SecureRandom.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            this.algorithm = prng;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 37348
diff changeset
   295
     * Creates a {@code SecureRandom} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 37348
diff changeset
   297
     * @param secureRandomSpi the {@code SecureRandom} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    protected SecureRandom(SecureRandomSpi secureRandomSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                           Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        this(secureRandomSpi, provider, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        this.secureRandomSpi = secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37796
diff changeset
   350
     * @return the new {@code SecureRandom} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public static SecureRandom getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public static SecureRandom getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public static SecureRandom getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    SecureRandomSpi getSecureRandomSpi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        return secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *          if the algorithm name cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * rather than replaces, the existing seed. Thus, repeated calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * are guaranteed never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   721
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public void setSeed(long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * Ignore call from super constructor (as well as any other calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * unfortunate enough to be passing 0).  It's critical that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * ignore call from superclass constructor, as digest has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * yet been initialized at that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (seed != 0) {
42161
5b0b84715c06 7004967: SecureRandom should be more explicit about threading
weijun
parents: 41826
diff changeset
   730
            setSeed(longToByteArray(seed));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @param bytes the array to be filled in with random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Generates an integer containing the user-specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param numBits number of pseudo-random bits to be generated, where
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16915
diff changeset
   788
     * {@code 0 <= numBits <= 32}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * of pseudo-random bits (right justified, with leading zeros).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        int next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        return next >>> (numBytes*8 - numBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <p>This method is only included for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * from that object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @see #setSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Helper function to convert a long into a byte array (least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * byte first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private static byte[] longToByteArray(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        byte[] retVal = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            retVal[i] = (byte) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            l >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Gets a default PRNG algorithm by looking through all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    private static String getPrngAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            for (Service s : p.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    return s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1034
    /**
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1035
     * Returns the period of this random number generator.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1036
     *
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1037
     * @return the period of this random number generator.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1038
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1039
    @Override
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1040
    public BigInteger period() {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1041
        return RandomGenerator.HUGE_PERIOD;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1042
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 58519
diff changeset
  1043
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    static final long serialVersionUID = 4940670005562187L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    // Retain unused values serialized from JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    private byte[] state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    private MessageDigest digest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * We know that the MessageDigest class does not implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * java.io.Serializable.  However, since this field is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    private byte[] randomBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private int randomBytesUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private long counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
}