jdk/src/share/classes/sun/security/provider/SecureRandom.java
author robm
Thu, 24 Jul 2014 22:22:43 +0100
changeset 25749 a556ba0ff94d
parent 16915 675d1569af3e
permissions -rw-r--r--
8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux Reviewed-by: mullan Contributed-by: Bradford Wetmore <bradford.wetmore@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
     2
 * Copyright (c) 1998, 2014, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final long serialVersionUID = 3581829991155417889L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int DIGEST_SIZE = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private transient MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private byte[] state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private byte[] remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int remCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * This empty constructor automatically seeds the generator.  We attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * to provide sufficient seed bytes to completely randomize the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * state of the generator (20 bytes).  Note, however, that our seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * generation algorithm has not been thoroughly studied or widely deployed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>The first time this constructor is called in a given Virtual Machine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * it may take several seconds of CPU time to seed the generator, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * on the underlying hardware.  Successive calls run quickly because they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * rely on the same (internal) pseudo-random number generator for their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * seed bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public SecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
    83
     * This constructor is used to instantiate the private seeder object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * with a given seed from the SeedGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private SecureRandom(byte seed[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        init(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * This call, used by the constructors, instantiates the SHA digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * and sets the seed, if given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private void init(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
    98
            /*
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
    99
             * 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
   100
             * performance overhead.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   101
             */
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   102
            digest = MessageDigest.getInstance("SHA", "SUN");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   103
        } catch (NoSuchProviderException | NoSuchAlgorithmException e) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   104
            // Fallback to any available.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   105
            try {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   106
                digest = MessageDigest.getInstance("SHA");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   107
            } catch (NoSuchAlgorithmException exc) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   108
                throw new InternalError(
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   109
                    "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
   110
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (seed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
           engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
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
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * call may be used to seed other random number generators.  While
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * we attempt to return a "truly random" sequence of bytes, we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * know exactly how random the bytes returned by this call are.  (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the empty constructor <a href = "#SecureRandom">SecureRandom</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * for a brief description of the underlying algorithm.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The prudent user will err on the side of caution and get extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * seed bytes, although it should be noted that seed generation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * somewhat costly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   134
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public byte[] engineGenerateSeed(int numBytes) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   136
        // Neither of the SeedGenerator implementations require
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   137
        // locking, so no sync needed here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        byte[] b = new byte[numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        SeedGenerator.generateSeed(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Reseeds this random object. The given seed supplements, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * replaces, the existing seed. Thus, repeated calls are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   150
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    synchronized public void engineSetSeed(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            digest.update(state);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   154
            for (int i = 0; i < state.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                state[i] = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   156
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        state = digest.digest(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static void updateState(byte[] state, byte[] output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int last = 1;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   163
        int v;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   164
        byte t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        boolean zf = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // state(n + 1) = (state(n) + output(n) + 1) % 2^160;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < state.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // Add two bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            v = (int)state[i] + (int)output[i] + last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // Result is lower 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            t = (byte)v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Store result. Check for state collision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            zf = zf | (state[i] != t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            state[i] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // High 8 bits are carry. Store for next iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            last = v >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Make sure at least one bit changes!
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   181
        if (!zf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
           state[0]++;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   183
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
14205
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   187
     * This static object will be seeded by SeedGenerator, and used
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   188
     * to seed future instances of SHA1PRNG SecureRandoms.
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   189
     *
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   190
     * Bloch, Effective Java Second Edition: Item 71
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
    private static class SeederHolder {
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 final SecureRandom seeder;
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
        static {
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
             * Call to SeedGenerator.generateSeed() to add additional
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   199
             * seed material (likely from the Native implementation).
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   200
             */
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   201
            seeder = new SecureRandom(SeedGenerator.getSystemEntropy());
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   202
            byte [] b = new byte[DIGEST_SIZE];
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   203
            SeedGenerator.generateSeed(b);
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   204
            seeder.engineSetSeed(b);
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   205
        }
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   206
    }
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
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param bytes the array to be filled in with random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 14205
diff changeset
   213
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public synchronized void engineNextBytes(byte[] result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        byte[] output = remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (state == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            byte[] seed = new byte[DIGEST_SIZE];
14205
2985a896a899 7167656: Multiple Seeders are being created
wetmore
parents: 12682
diff changeset
   221
            SeederHolder.seeder.engineNextBytes(seed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            state = digest.digest(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Use remainder from last time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int r = remCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // How many bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            todo = (result.length - index) < (DIGEST_SIZE - r) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        (result.length - index) : (DIGEST_SIZE - r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // Copy the bytes, zero the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            for (int i = 0; i < todo; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                result[i] = output[r];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                output[r++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            remCount += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            index += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // If we need more bytes, make them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        while (index < result.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // Step the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            digest.update(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            output = digest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            updateState(state, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // How many bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            todo = (result.length - index) > DIGEST_SIZE ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                DIGEST_SIZE : result.length - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Copy the bytes, zero the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            for (int i = 0; i < todo; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                result[index++] = output[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                output[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            remCount += todo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Store remainder for next time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        remainder = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        remCount %= DIGEST_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * readObject is called to restore the state of the random object from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * a stream.  We have to create a new instance of MessageDigest, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * it is not included in the stream (it is marked "transient").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Note that the engineNextBytes() method invoked on the restored random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * object will yield the exact same (random) bytes as the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * If you do not want this behaviour, you should re-seed the restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * random object, using engineSetSeed().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        s.defaultReadObject ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
25749
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   279
            /*
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   280
             * 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
   281
             * performance overhead.
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
            digest = MessageDigest.getInstance("SHA", "SUN");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   284
        } catch (NoSuchProviderException | NoSuchAlgorithmException e) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   285
            // Fallback to any available.
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   286
            try {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   287
                digest = MessageDigest.getInstance("SHA");
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   288
            } catch (NoSuchAlgorithmException exc) {
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   289
                throw new InternalError(
a556ba0ff94d 8044659: Java SecureRandom on SPARC T4 much slower than on x86/Linux
robm
parents: 16915
diff changeset
   290
                    "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
   291
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}