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