src/java.base/share/classes/sun/security/provider/SecureRandom.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.SecureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
    32
import java.security.NoSuchProviderException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>This class provides a crytpographically strong pseudo-random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * generator based on the SHA-1 hash algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>Note that if a seed is not provided, we attempt to provide sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * seed bytes to completely randomize the internal state of the generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * (20 bytes).  However, our seed generation algorithm has not been thoroughly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * studied or widely deployed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Also note that when a random object is deserialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <a href="#engineNextBytes(byte[])">engineNextBytes</a> invoked on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * restored random object will yield the exact same (random) bytes as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * original object.  If this behaviour is not desired, the restored random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * object should be seeded, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <a href="#engineSetSeed(byte[])">engineSetSeed</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Gadi Guy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public final class SecureRandom extends SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    58
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final long serialVersionUID = 3581829991155417889L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int DIGEST_SIZE = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private transient MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private byte[] state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private byte[] remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int remCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * This empty constructor automatically seeds the generator.  We attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * to provide sufficient seed bytes to completely randomize the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * state of the generator (20 bytes).  Note, however, that our seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * generation algorithm has not been thoroughly studied or widely deployed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p>The first time this constructor is called in a given Virtual Machine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * it may take several seconds of CPU time to seed the generator, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * on the underlying hardware.  Successive calls run quickly because they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * rely on the same (internal) pseudo-random number generator for their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * seed bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public SecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
    84
     * This constructor is used to instantiate the private seeder object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * with a given seed from the SeedGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
    89
    private SecureRandom(byte[] seed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        init(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * This call, used by the constructors, instantiates the SHA digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * and sets the seed, if given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private void init(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
    99
            /*
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   100
             * Use the local SUN implementation to avoid native
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   101
             * performance overhead.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   102
             */
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   103
            digest = MessageDigest.getInstance("SHA", "SUN");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   104
        } catch (NoSuchProviderException | NoSuchAlgorithmException e) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   105
            // Fallback to any available.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   106
            try {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   107
                digest = MessageDigest.getInstance("SHA");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   108
            } catch (NoSuchAlgorithmException exc) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   109
                throw new InternalError(
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   110
                    "internal error: SHA-1 not available.", exc);
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   111
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (seed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
           engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * call may be used to seed other random number generators.  While
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * we attempt to return a "truly random" sequence of bytes, we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * know exactly how random the bytes returned by this call are.  (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the empty constructor <a href = "#SecureRandom">SecureRandom</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * for a brief description of the underlying algorithm.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The prudent user will err on the side of caution and get extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * seed bytes, although it should be noted that seed generation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * somewhat costly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public byte[] engineGenerateSeed(int numBytes) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   137
        // Neither of the SeedGenerator implementations require
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   138
        // locking, so no sync needed here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        byte[] b = new byte[numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SeedGenerator.generateSeed(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Reseeds this random object. The given seed supplements, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * replaces, the existing seed. Thus, repeated calls are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   151
    @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31538
diff changeset
   152
    public synchronized void engineSetSeed(byte[] seed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            digest.update(state);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   155
            for (int i = 0; i < state.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                state[i] = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   157
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        state = digest.digest(seed);
37883
e2acd0d8d716 8154523: SHA1PRNG output should change after setSeed
weijun
parents: 32649
diff changeset
   160
        remCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static void updateState(byte[] state, byte[] output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int last = 1;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   165
        int v;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   166
        byte t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        boolean zf = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // state(n + 1) = (state(n) + output(n) + 1) % 2^160;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int i = 0; i < state.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // Add two bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            v = (int)state[i] + (int)output[i] + last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Result is lower 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            t = (byte)v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // Store result. Check for state collision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            zf = zf | (state[i] != t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            state[i] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // High 8 bits are carry. Store for next iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            last = v >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // Make sure at least one bit changes!
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   183
        if (!zf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
           state[0]++;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   185
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
14205
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   189
     * This static object will be seeded by SeedGenerator, and used
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   190
     * to seed future instances of SHA1PRNG SecureRandoms.
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   191
     *
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   192
     * Bloch, Effective Java Second Edition: Item 71
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   193
     */
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   194
    private static class SeederHolder {
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   195
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   196
        private static final SecureRandom seeder;
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   197
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   198
        static {
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   199
            /*
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   200
             * Call to SeedGenerator.generateSeed() to add additional
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   201
             * seed material (likely from the Native implementation).
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   202
             */
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   203
            seeder = new SecureRandom(SeedGenerator.getSystemEntropy());
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   204
            byte [] b = new byte[DIGEST_SIZE];
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   205
            SeedGenerator.generateSeed(b);
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   206
            seeder.engineSetSeed(b);
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   207
        }
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   208
    }
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   209
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   210
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   213
     * @param result the array to be filled in with random bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   215
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public synchronized void engineNextBytes(byte[] result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        byte[] output = remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (state == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            byte[] seed = new byte[DIGEST_SIZE];
14205
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   223
            SeederHolder.seeder.engineNextBytes(seed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            state = digest.digest(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Use remainder from last time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int r = remCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // How many bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            todo = (result.length - index) < (DIGEST_SIZE - r) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        (result.length - index) : (DIGEST_SIZE - r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // Copy the bytes, zero the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (int i = 0; i < todo; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                result[i] = output[r];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                output[r++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            remCount += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            index += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // If we need more bytes, make them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        while (index < result.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Step the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            digest.update(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            output = digest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            updateState(state, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // How many bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            todo = (result.length - index) > DIGEST_SIZE ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                DIGEST_SIZE : result.length - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Copy the bytes, zero the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (int i = 0; i < todo; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                result[index++] = output[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                output[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            remCount += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Store remainder for next time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        remainder = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        remCount %= DIGEST_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * readObject is called to restore the state of the random object from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * a stream.  We have to create a new instance of MessageDigest, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * it is not included in the stream (it is marked "transient").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Note that the engineNextBytes() method invoked on the restored random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * object will yield the exact same (random) bytes as the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * If you do not want this behaviour, you should re-seed the restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * random object, using engineSetSeed().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   275
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        s.defaultReadObject ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   282
            /*
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   283
             * Use the local SUN implementation to avoid native
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   284
             * performance overhead.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   285
             */
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   286
            digest = MessageDigest.getInstance("SHA", "SUN");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   287
        } catch (NoSuchProviderException | NoSuchAlgorithmException e) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   288
            // Fallback to any available.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   289
            try {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   290
                digest = MessageDigest.getInstance("SHA");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   291
            } catch (NoSuchAlgorithmException exc) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   292
                throw new InternalError(
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   293
                    "internal error: SHA-1 not available.", exc);
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   294
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}