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