jdk/src/java.base/share/classes/sun/security/provider/SeedGenerator.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30033 b9c86c17164a
child 34716 7477a052aecc
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29923
e3ee0996bedb 8042332: Enhance thread contexts in security libraries
valeriep
parents: 28542
diff changeset
     2
 * Copyright (c) 1996, 2015, 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;
29923
e3ee0996bedb 8042332: Enhance thread contexts in security libraries
valeriep
parents: 28542
diff changeset
    78
import sun.misc.ManagedLocalsThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
abstract class SeedGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Static instance is created at link time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static SeedGenerator instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final Debug debug = Debug.getInstance("provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Static initializer to hook in selected or best performing generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String egdSource = SunEntries.getSeedSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    92
        /*
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    93
         * 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
    94
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    95
         * 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
    96
         * 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
    97
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
    98
         * 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
    99
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   100
         * 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
   101
         * URLSeedGenerator to read from /dev/[u]random
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   102
         */
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   103
        if (egdSource.equals(SunEntries.URL_DEV_RANDOM) ||
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   104
                egdSource.equals(SunEntries.URL_DEV_URANDOM)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   106
                instance = new NativeSeedGenerator(egdSource);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (debug != null) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   108
                    debug.println(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   109
                        "Using operating system seed generator" + egdSource);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    debug.println("Failed to use operating system seed "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                  + "generator: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else if (egdSource.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                instance = new URLSeedGenerator(egdSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    debug.println("Using URL seed generator reading from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                  + egdSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (IOException e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   125
                if (debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    debug.println("Failed to create seed generator with "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                  + egdSource + ": " + e.toString());
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   128
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Fall back to ThreadedSeedGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (instance == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                debug.println("Using default threaded seed generator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            instance = new ThreadedSeedGenerator();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Fill result with bytes from the queue. Wait for it if it isn't ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
   144
    public static void generateSeed(byte[] result) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        instance.getSeedBytes(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   148
    abstract void getSeedBytes(byte[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Retrieve some system information, hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static byte[] getSystemEntropy() {
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) {
24864
a7bf63ebda64 8046368: Code cleanup in SeedGenerator.java
ascarpino
parents: 23923
diff changeset
   159
            throw new InternalError("internal error: SHA-1 not available.",
a7bf63ebda64 8046368: Code cleanup in SeedGenerator.java
ascarpino
parents: 23923
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
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29923
diff changeset
   168
            (new java.security.PrivilegedAction<>() {
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
                        Properties p = System.getProperties();
24864
a7bf63ebda64 8046368: Code cleanup in SeedGenerator.java
ascarpino
parents: 23923
diff changeset
   174
                        for (String s: p.stringPropertyNames()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            md.update(s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            md.update(p.getProperty(s).getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
23923
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   179
                        // Include network adapter names (and a Mac address)
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   180
                        addNetworkAdapterInfo(md);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        // The temporary dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        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
   184
                        int count = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   185
                        try (
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   186
                            DirectoryStream<Path> stream =
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   187
                                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
   188
                            // 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
   189
                            // 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
   190
                            // 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
   191
                            // 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
   192
                            // 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
   193
                            Random r = new Random();
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   194
                            for (Path entry: stream) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   195
                                if (count < 512 || r.nextBoolean()) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   196
                                    md.update(entry.getFileName()
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   197
                                        .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
   198
                                }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   199
                                if (count++ > 1024) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   200
                                    break;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7546
diff changeset
   201
                                }
4176
bf303f38f727 6894534: SeedGenerator shouldn't require java.nio.file to be present
weijun
parents: 2175
diff changeset
   202
                            }
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
   203
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        md.update((byte)ex.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    // get Runtime memory stats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    Runtime rt = Runtime.getRuntime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    byte[] memBytes = longToByteArray(rt.totalMemory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    md.update(memBytes, 0, memBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    memBytes = longToByteArray(rt.freeMemory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    md.update(memBytes, 0, memBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
23923
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   221
    /*
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   222
     * Include network adapter names and, if available, a Mac address
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   223
     *
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   224
     * See also java.util.concurrent.ThreadLocalRandom.initialSeed()
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   225
     */
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   226
    private static void addNetworkAdapterInfo(MessageDigest md) {
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   227
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   228
        try {
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   229
            Enumeration<NetworkInterface> ifcs =
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   230
                NetworkInterface.getNetworkInterfaces();
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   231
            while (ifcs.hasMoreElements()) {
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   232
                NetworkInterface ifc = ifcs.nextElement();
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   233
                md.update(ifc.toString().getBytes());
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   234
                if (!ifc.isVirtual()) { // skip fake addresses
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   235
                    byte[] bs = ifc.getHardwareAddress();
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   236
                    if (bs != null) {
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   237
                        md.update(bs);
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   238
                        break;
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   239
                    }
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   240
                }
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   241
            }
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   242
        } catch (Exception ignore) {
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   243
        }
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   244
    }
baaff2487309 8035834: InetAddress.getLocalHost() can hang after JDK-8030731
vinnie
parents: 16915
diff changeset
   245
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Helper function to convert a long into a byte array (least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * byte first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static byte[] longToByteArray(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte[] retVal = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        for (int i=0; i<8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            retVal[i] = (byte) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            l >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // This method helps the test utility receive unprocessed seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public static int genTestSeed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return myself.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   269
    private static class ThreadedSeedGenerator extends SeedGenerator
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   270
            implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Queue is used to collect seed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        private byte[] pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        private int start, end, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Thread group for our threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        ThreadGroup seedGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        /**
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   279
         * 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
   280
         * 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
   281
         * and starts the thread going.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   282
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        ThreadedSeedGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            pool = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            start = end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                digest = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } catch (NoSuchAlgorithmException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   292
                throw new InternalError("internal error: SHA-1 not available."
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   293
                        , e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            final ThreadGroup[] finalsg = new ThreadGroup[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            Thread t = java.security.AccessController.doPrivileged
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29923
diff changeset
   298
                (new java.security.PrivilegedAction<>() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   299
                        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        public Thread run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            ThreadGroup parent, group =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                Thread.currentThread().getThreadGroup();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   303
                            while ((parent = group.getParent()) != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                group = parent;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   305
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                            finalsg[0] = new ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                (group, "SeedGenerator ThreadGroup");
29923
e3ee0996bedb 8042332: Enhance thread contexts in security libraries
valeriep
parents: 28542
diff changeset
   308
                            Thread newT = new ManagedLocalsThread(finalsg[0],
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   309
                                ThreadedSeedGenerator.this,
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   310
                                "SeedGenerator Thread");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            newT.setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            newT.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            return newT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            seedGroup = finalsg[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * This method does the actual work. It collects random bytes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * pushes them into the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   324
        @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
   325
        public final void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    // Queue full? Wait till there's room.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    synchronized(this) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   330
                        while (count >= pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            wait();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   332
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    int counter, quanta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    byte v = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    // Spin count must not be under 64000
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   339
                    for (counter = quanta = 0;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   340
                            (counter < 64000) && (quanta < 6); quanta++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        // Start some noisy threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            BogusThread bt = new BogusThread();
29923
e3ee0996bedb 8042332: Enhance thread contexts in security libraries
valeriep
parents: 28542
diff changeset
   345
                            Thread t = new ManagedLocalsThread
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                (seedGroup, bt, "SeedGenerator Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   350
                                "SeedGenerator thread creation error.", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        // We wait 250milli quanta, so the minimum wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        // cannot be under 250milli.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        int latch = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        long l = System.currentTimeMillis() + 250;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        while (System.currentTimeMillis() < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            synchronized(this){};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            latch++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        // Translate the value using the permutation, and xor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        // it with previous values gathered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        v ^= rndTab[latch % 255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        counter += latch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    // Push it into the queue and notify anybody who might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    // be waiting for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        pool[end] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        count++;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   374
                        if (end >= pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                            end = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   376
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   383
                    "SeedGenerator thread generated an exception.", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   387
        @Override
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   388
        void getSeedBytes(byte[] result) {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   389
            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
   390
                result[i] = getSeedByte();
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   391
            }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   392
        }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   393
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        byte getSeedByte() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   395
            byte b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                // Wait for it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                synchronized(this) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   400
                    while (count <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        wait();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   402
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            } catch (Exception e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   405
                if (count <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    throw new InternalError("internal error: " +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   407
                        "SeedGenerator thread generated an exception.", e);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   408
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // Get it from the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                b = pool[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                pool[start] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                count--;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   417
                if (start == pool.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    start = 0;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   419
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // Notify the daemon thread, just in case it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // waiting for us to make room in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // The permutation was calculated by generating 64k of random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // data and using it to mix the trivial permutation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // It should be evenly distributed. The specific values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // are not crucial to the security of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        private static byte[] rndTab = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            56, 30, -107, -6, -86, 25, -83, 75, -12, -64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            5, -128, 78, 21, 16, 32, 70, -81, 37, -51,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            -43, -46, -108, 87, 29, 17, -55, 22, -11, -111,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            -115, 84, -100, 108, -45, -15, -98, 72, -33, -28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            31, -52, -37, -117, -97, -27, 93, -123, 47, 126,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            -80, -62, -93, -79, 61, -96, -65, -5, -47, -119,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            14, 89, 81, -118, -88, 20, 67, -126, -113, 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            -102, 55, 110, 28, 85, 121, 122, -58, 2, 45,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            43, 24, -9, 103, -13, 102, -68, -54, -101, -104,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            19, 13, -39, -26, -103, 62, 77, 51, 44, 111,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            73, 18, -127, -82, 4, -30, 11, -99, -74, 40,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            -89, 42, -76, -77, -94, -35, -69, 35, 120, 76,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            33, -73, -7, 82, -25, -10, 88, 125, -112, 58,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            83, 95, 6, 10, 98, -34, 80, 15, -91, 86,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            -19, 52, -17, 117, 49, -63, 118, -90, 36, -116,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            -40, -71, 97, -53, -109, -85, 109, -16, -3, 104,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            -95, 68, 54, 34, 26, 114, -1, 106, -121, 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            66, 0, 100, -84, 57, 107, 119, -42, 112, -61,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            1, 48, 38, 12, -56, -57, 39, -106, -72, 41,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            7, 71, -29, -59, -8, -38, 79, -31, 124, -124,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            8, 91, 116, 99, -4, 9, -36, -78, 63, -49,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            -67, -87, 59, 101, -32, 92, 94, 53, -41, 115,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            -66, -70, -122, 50, -50, -22, -20, -18, -21, 23,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            -2, -48, 96, 65, -105, 123, -14, -110, 69, -24,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            -120, -75, 74, 127, -60, 113, 90, -114, 105, 46,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            27, -125, -23, -44, 64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * This inner thread causes the thread scheduler to become 'noisy',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * thus adding entropy to the system load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * At least one instance of this class is generated for every seed byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        private static class BogusThread implements Runnable {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   468
            @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
   469
            public final void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   471
                    for (int i = 0; i < 5; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        Thread.sleep(50);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   473
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    // System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    static class URLSeedGenerator extends SeedGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        private String deviceName;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   484
        private InputStream seedStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * The constructor is only called once to construct the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * instance we actually use. It opens the entropy gathering device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * which will supply the randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        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
   493
        if (egdurl == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                throw new IOException("No random source specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            deviceName = egdurl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        private void init() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            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
   502
            try {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   503
                seedStream = java.security.AccessController.doPrivileged
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29923
diff changeset
   504
                    (new java.security.PrivilegedExceptionAction<>() {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   505
                        @Override
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   506
                        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
   507
                            /*
28542
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   508
                             * return a shared InputStream for file URLs and
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   509
                             * avoid buffering.
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   510
                             * The URL.openStream() call wraps InputStream in a
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   511
                             * BufferedInputStream which
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   512
                             * 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
   513
                             * 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
   514
                             * 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
   515
                             */
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   516
                            if (device.getProtocol().equalsIgnoreCase("file")) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   517
                                File deviceFile =
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   518
                                    SunEntries.getDeviceFile(device);
28542
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   519
                                return FileInputStreamPool
d50a7783fe02 8047769: SecureRandom should be more frugal with file descriptors
plevart
parents: 25859
diff changeset
   520
                                    .getInputStream(deviceFile);
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   521
                            } else {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   522
                                return device.openStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    });
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   526
            } catch (Exception e) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   527
                throw new IOException(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   528
                    "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
   529
            }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   530
        }
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   531
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   532
        @Override
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   533
        void getSeedBytes(byte[] result) {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   534
            int len = result.length;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   535
            int read = 0;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   536
            try {
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   537
                while (read < len) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   538
                    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
   539
                    // /dev/random blocks - should never have EOF
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   540
                    if (count < 0) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   541
                        throw new InternalError(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   542
                            "URLSeedGenerator " + deviceName +
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   543
                            " reached end of file");
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   544
                    }
7546
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   545
                    read += count;
c1915029b924 6998583: NativeSeedGenerator is making 8192 byte read requests from entropy pool on each init.
coffeys
parents: 5506
diff changeset
   546
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                throw new InternalError("URLSeedGenerator " + deviceName +
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 10419
diff changeset
   549
                    " generated exception: " + ioe.getMessage(), ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
}