jdk/src/share/classes/sun/security/provider/SeedGenerator.java
author wetmore
Thu, 11 Apr 2013 21:03:24 -0700
changeset 16915 675d1569af3e
parent 10419 12c063b39232
child 23923 baaff2487309
permissions -rw-r--r--
6425477: Better support for generation of high entropy random numbers Reviewed-by: xuelei, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
     2
 * Copyright (c) 1996, 2013, 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: 4176
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: 4176
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: 4176
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4176
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4176
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
/**
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    29
 * This class generates seeds for the SHA1PRNG cryptographically strong
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    30
 * random number generator.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    31
 * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    32
 * The seed is produced using one of two techniques, via a computation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of current system activity or from an entropy gathering device.
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    34
 * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    35
 * In the default technique the seed is produced by counting the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * number of times the VM manages to loop in a given period. This number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * roughly reflects the machine load at that point in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The samples are translated using a permutation (s-box)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and then XORed together. This process is non linear and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * should prevent the samples from "averaging out". The s-box
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * was designed to have even statistical distribution; it's specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * values are not crucial for the security of the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * We also create a number of sleeper threads which add entropy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to the system by keeping the scheduler busy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Twenty such samples should give us roughly 160 bits of randomness.
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    46
 * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    47
 * These values are gathered in the background by a daemon thread
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * thus allowing the system to continue performing it's different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * activites, which in turn add entropy to the random seed.
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    50
 * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    51
 * The class also gathers miscellaneous system information, some
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * machine dependent, some not. This information is then hashed together
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * with the 20 seed bytes.
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    54
 * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    55
 * The alternative to the above approach is to acquire seed material
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * from an entropy gathering device, such as /dev/random. This can be
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    57
 * accomplished by setting the value of the {@code securerandom.source}
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    58
 * Security property to a URL specifying the location of the entropy
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    59
 * gathering device, or by setting the {@code java.security.egd} System
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    60
 * property.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    61
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * In the event the specified URL cannot be accessed the default
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    63
 * threading mechanism is used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author Joshua Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Gadi Guy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.net.*;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
    74
import java.nio.file.DirectoryStream;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
    75
import java.nio.file.Files;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
    76
import java.nio.file.Path;
2175
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
    77
import java.util.Random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
abstract class SeedGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Static instance is created at link time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static SeedGenerator instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final Debug debug = Debug.getInstance("provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Static initializer to hook in selected or best performing generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String egdSource = SunEntries.getSeedSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    91
        /*
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    92
         * Try the URL specifying the source (e.g. file:/dev/random)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    93
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    94
         * The URLs "file:/dev/random" or "file:/dev/urandom" are used to
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    95
         * indicate the SeedGenerator should use OS support, if available.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    96
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    97
         * On Windows, this causes the MS CryptoAPI seeder to be used.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    98
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    99
         * On Solaris/Linux/MacOS, this is identical to using
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   100
         * URLSeedGenerator to read from /dev/[u]random
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   101
         */
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   102
        if (egdSource.equals(SunEntries.URL_DEV_RANDOM) ||
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   103
                egdSource.equals(SunEntries.URL_DEV_URANDOM)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   105
                instance = new NativeSeedGenerator(egdSource);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (debug != null) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   107
                    debug.println(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   108
                        "Using operating system seed generator" + egdSource);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    debug.println("Failed to use operating system seed "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                  + "generator: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else if (egdSource.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                instance = new URLSeedGenerator(egdSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    debug.println("Using URL seed generator reading from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                  + egdSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } catch (IOException e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   124
                if (debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    debug.println("Failed to create seed generator with "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                  + egdSource + ": " + e.toString());
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   127
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Fall back to ThreadedSeedGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (instance == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                debug.println("Using default threaded seed generator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            instance = new ThreadedSeedGenerator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Fill result with bytes from the queue. Wait for it if it isn't ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static public void generateSeed(byte[] result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        instance.getSeedBytes(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   147
    abstract void getSeedBytes(byte[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Retrieve some system information, hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static byte[] getSystemEntropy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        byte[] ba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (NoSuchAlgorithmException nsae) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   159
            throw new InternalError("internal error: SHA-1 not available."
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   160
                    , nsae);
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
        // The current time in millis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        byte b =(byte)System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            (new java.security.PrivilegedAction<Void>() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   169
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        // System properties can change from machine to machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        Properties p = System.getProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        Enumeration<?> e = p.propertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            s =(String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            md.update(s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            md.update(p.getProperty(s).getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        md.update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            (InetAddress.getLocalHost().toString().getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        // The temporary dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        File f = new File(p.getProperty("java.io.tmpdir"));
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   187
                        int count = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   188
                        try (
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   189
                            DirectoryStream<Path> stream =
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   190
                                Files.newDirectoryStream(f.toPath())) {
2175
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   191
                            // We use a Random object to choose what file names
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   192
                            // should be used. Otherwise on a machine with too
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   193
                            // many files, the same first 1024 files always get
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   194
                            // used. Any, We make sure the first 512 files are
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   195
                            // always used.
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   196
                            Random r = new Random();
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   197
                            for (Path entry: stream) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   198
                                if (count < 512 || r.nextBoolean()) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   199
                                    md.update(entry.getFileName()
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   200
                                        .toString().getBytes());
2175
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   201
                                }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   202
                                if (count++ > 1024) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   203
                                    break;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   204
                                }
4176
bf303f38f727 6894534: SeedGenerator shouldn't require java.nio.file to be present
weijun
parents: 2175
diff changeset
   205
                            }
2175
7d7e238cb41a 6705872: SecureRandom number init is taking too long on a java.io.tmpdir with a large number of files.
weijun
parents: 2
diff changeset
   206
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        md.update((byte)ex.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    // get Runtime memory stats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    Runtime rt = Runtime.getRuntime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    byte[] memBytes = longToByteArray(rt.totalMemory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    md.update(memBytes, 0, memBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    memBytes = longToByteArray(rt.freeMemory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    md.update(memBytes, 0, memBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Helper function to convert a long into a byte array (least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * byte first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private static byte[] longToByteArray(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        byte[] retVal = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        for (int i=0; i<8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            retVal[i] = (byte) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            l >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // This method helps the test utility receive unprocessed seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static int genTestSeed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return myself.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   247
    private static class ThreadedSeedGenerator extends SeedGenerator
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   248
            implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Queue is used to collect seed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        private byte[] pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        private int start, end, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // Thread group for our threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        ThreadGroup seedGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /**
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   257
         * The constructor is only called once to construct the one
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   258
         * instance we actually use. It instantiates the message digest
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   259
         * and starts the thread going.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   260
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        ThreadedSeedGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            pool = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            start = end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                digest = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } catch (NoSuchAlgorithmException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   270
                throw new InternalError("internal error: SHA-1 not available."
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   271
                        , e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            final ThreadGroup[] finalsg = new ThreadGroup[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            Thread t = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                (new java.security.PrivilegedAction<Thread>() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   277
                        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        public Thread run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                            ThreadGroup parent, group =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                Thread.currentThread().getThreadGroup();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   281
                            while ((parent = group.getParent()) != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                group = parent;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   283
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            finalsg[0] = new ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                (group, "SeedGenerator ThreadGroup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            Thread newT = new Thread(finalsg[0],
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   287
                                ThreadedSeedGenerator.this,
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   288
                                "SeedGenerator Thread");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            newT.setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            newT.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            return newT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            seedGroup = finalsg[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * This method does the actual work. It collects random bytes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * pushes them into the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   302
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        final public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    // Queue full? Wait till there's room.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    synchronized(this) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   308
                        while (count >= pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            wait();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   310
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    int counter, quanta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    byte v = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    // Spin count must not be under 64000
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   317
                    for (counter = quanta = 0;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   318
                            (counter < 64000) && (quanta < 6); quanta++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        // Start some noisy threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            BogusThread bt = new BogusThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            Thread t = new Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                (seedGroup, bt, "SeedGenerator Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   328
                                "SeedGenerator thread creation error.", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        // We wait 250milli quanta, so the minimum wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        // cannot be under 250milli.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        int latch = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        long l = System.currentTimeMillis() + 250;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        while (System.currentTimeMillis() < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            synchronized(this){};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            latch++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        // Translate the value using the permutation, and xor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        // it with previous values gathered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        v ^= rndTab[latch % 255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        counter += latch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    // Push it into the queue and notify anybody who might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    // be waiting for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        pool[end] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        count++;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   352
                        if (end >= pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            end = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   354
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   361
                    "SeedGenerator thread generated an exception.", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   365
        @Override
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   366
        void getSeedBytes(byte[] result) {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   367
            for (int i = 0; i < result.length; i++) {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   368
                result[i] = getSeedByte();
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   369
            }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   370
        }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   371
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        byte getSeedByte() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   373
            byte b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // Wait for it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                synchronized(this) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   378
                    while (count <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        wait();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   380
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            } catch (Exception e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   383
                if (count <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   385
                        "SeedGenerator thread generated an exception.", e);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   386
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // Get it from the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                b = pool[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                pool[start] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                count--;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   395
                if (start == pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    start = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   397
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // Notify the daemon thread, just in case it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                // waiting for us to make room in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // The permutation was calculated by generating 64k of random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // data and using it to mix the trivial permutation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // It should be evenly distributed. The specific values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // are not crucial to the security of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        private static byte[] rndTab = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            56, 30, -107, -6, -86, 25, -83, 75, -12, -64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            5, -128, 78, 21, 16, 32, 70, -81, 37, -51,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            -43, -46, -108, 87, 29, 17, -55, 22, -11, -111,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            -115, 84, -100, 108, -45, -15, -98, 72, -33, -28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            31, -52, -37, -117, -97, -27, 93, -123, 47, 126,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            -80, -62, -93, -79, 61, -96, -65, -5, -47, -119,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            14, 89, 81, -118, -88, 20, 67, -126, -113, 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            -102, 55, 110, 28, 85, 121, 122, -58, 2, 45,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            43, 24, -9, 103, -13, 102, -68, -54, -101, -104,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            19, 13, -39, -26, -103, 62, 77, 51, 44, 111,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            73, 18, -127, -82, 4, -30, 11, -99, -74, 40,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            -89, 42, -76, -77, -94, -35, -69, 35, 120, 76,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            33, -73, -7, 82, -25, -10, 88, 125, -112, 58,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            83, 95, 6, 10, 98, -34, 80, 15, -91, 86,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            -19, 52, -17, 117, 49, -63, 118, -90, 36, -116,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            -40, -71, 97, -53, -109, -85, 109, -16, -3, 104,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            -95, 68, 54, 34, 26, 114, -1, 106, -121, 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            66, 0, 100, -84, 57, 107, 119, -42, 112, -61,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            1, 48, 38, 12, -56, -57, 39, -106, -72, 41,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            7, 71, -29, -59, -8, -38, 79, -31, 124, -124,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            8, 91, 116, 99, -4, 9, -36, -78, 63, -49,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            -67, -87, 59, 101, -32, 92, 94, 53, -41, 115,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            -66, -70, -122, 50, -50, -22, -20, -18, -21, 23,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            -2, -48, 96, 65, -105, 123, -14, -110, 69, -24,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            -120, -75, 74, 127, -60, 113, 90, -114, 105, 46,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            27, -125, -23, -44, 64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * This inner thread causes the thread scheduler to become 'noisy',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * thus adding entropy to the system load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * At least one instance of this class is generated for every seed byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        private static class BogusThread implements Runnable {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   446
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            final public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   449
                    for (int i = 0; i < 5; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        Thread.sleep(50);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   451
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    // System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    static class URLSeedGenerator extends SeedGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        private String deviceName;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   462
        private InputStream seedStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * The constructor is only called once to construct the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * instance we actually use. It opens the entropy gathering device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * which will supply the randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        URLSeedGenerator(String egdurl) throws IOException {
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   471
        if (egdurl == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                throw new IOException("No random source specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            deviceName = egdurl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        private void init() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            final URL device = new URL(deviceName);
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   480
            try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   481
                seedStream = java.security.AccessController.doPrivileged
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   482
                    (new java.security.PrivilegedExceptionAction<InputStream>() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   483
                        @Override
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   484
                        public InputStream run() throws IOException {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   485
                            /*
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   486
                             * return a FileInputStream for file URLs and
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   487
                             * avoid buffering. The openStream() call wraps
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   488
                             * InputStream in a BufferedInputStream which
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   489
                             * can buffer up to 8K bytes. This read is a
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   490
                             * performance issue for entropy sources which
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   491
                             * can be slow to replenish.
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   492
                             */
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   493
                            if (device.getProtocol().equalsIgnoreCase("file")) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   494
                                File deviceFile =
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   495
                                    SunEntries.getDeviceFile(device);
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   496
                                return new FileInputStream(deviceFile);
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   497
                            } else {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   498
                                return device.openStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    });
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   502
            } catch (Exception e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   503
                throw new IOException(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   504
                    "Failed to open " + deviceName, e.getCause());
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   505
            }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   506
        }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   507
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   508
        @Override
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   509
        void getSeedBytes(byte[] result) {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   510
            int len = result.length;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   511
            int read = 0;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   512
            try {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   513
                while (read < len) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   514
                    int count = seedStream.read(result, read, len - read);
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   515
                    // /dev/random blocks - should never have EOF
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   516
                    if (count < 0) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   517
                        throw new InternalError(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   518
                            "URLSeedGenerator " + deviceName +
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   519
                            " reached end of file");
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   520
                    }
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   521
                    read += count;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   522
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                throw new InternalError("URLSeedGenerator " + deviceName +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   525
                    " generated exception: " + ioe.getMessage(), ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
}