<div dir="ltr"><div>We know that loading the networking machinery is problematic.  On Linux we would be content to hard-code a read from /dev/urandom, which is safer and strictly more random than the existing network hardware determination, but y'all will reject that as too system-dependent (insufficient machinery!). Hmmmm .... maybe not .... as long as we code up a good fallback ...</div>
<div><br></div>I learned that SecureRandom by default on Unix uses /dev/random for "seed bytes" and /dev/urandom for nextBytes.<div><br></div><div>Here's my proposal, that in the default case on Unix doesn't load any machinery, and as a fallback loads the SecureRandom machinery instead of the network machinery, while maintaining the ultra-secure behavior of the <span style="font-family:'courier new',monospace">java.util.secureRandomSeed </span>system property:</div>
<div><br></div><div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">    private static long initialSeed() {</font></div><div><font face="courier new, monospace">        byte[] seedBytes = initialSeedBytes();</font></div>
<div><font face="courier new, monospace">        long s = (long)(seedBytes[0]) & 0xffL;</font></div><div><font face="courier new, monospace">        for (int i = 1; i < seedBytes.length; ++i)</font></div><div><font face="courier new, monospace">            s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);</font></div>
<div><font face="courier new, monospace">        return s ^ mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());</font></div><div><font face="courier new, monospace">    }</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">    private static byte[] initialSeedBytes() {</font></div><div><font face="courier new, monospace">        String pp = java.security.AccessController.doPrivileged(</font></div>
<div><font face="courier new, monospace">                new sun.security.action.GetPropertyAction(</font></div><div><font face="courier new, monospace">                        "java.util.secureRandomSeed"));</font></div>
<div><font face="courier new, monospace">        boolean secureRandomSeed = (pp != null && pp.equalsIgnoreCase("true"));</font></div><div><font face="courier new, monospace">        if (secureRandomSeed)</font></div>
<div><font face="courier new, monospace">            return java.security.SecureRandom.getSeed(8);</font></div><div><font face="courier new, monospace">        final byte[] seedBytes = new byte[8];</font></div><div><font face="courier new, monospace">        File seedSource = new File("/dev/urandom");</font></div>
<div><font face="courier new, monospace">        if (seedSource.exists()) {</font></div><div><font face="courier new, monospace">            try (FileInputStream stream = new FileInputStream(seedSource)) {</font></div><div>
<font face="courier new, monospace">                if (stream.read(seedBytes) == 8)</font></div><div><font face="courier new, monospace">                    return seedBytes;</font></div><div><font face="courier new, monospace">            } catch (IOException ignore) { }</font></div>
<div><font face="courier new, monospace">        }</font></div><div><font face="courier new, monospace">        new java.security.SecureRandom().nextBytes(seedBytes);</font></div><div><font face="courier new, monospace">        return seedBytes;</font></div>
<div><font face="courier new, monospace">    }</font></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jun 21, 2014 at 9:05 PM, Martin Buchholz <span dir="ltr"><<a href="mailto:martinrb@google.com" target="_blank">martinrb@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">While looking at NativePRNG, I filed<div><br></div><div><a href="https://bugs.openjdk.java.net/browse/JDK-8047769" target="_blank">https://bugs.openjdk.java.net/browse/JDK-8047769</a><br>
<br>SecureRandom should be more frugal with file descriptors<br>
<br>If I run this java program on Linux <br><br>public class SecureRandoms { </div><div><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">    public static void main(String[] args) throws Throwable { </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">        new java.security.SecureRandom(); </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">    } </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">} </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">it creates 6 file descriptors for /dev/random and /dev/urandom, as shown by: </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">strace -q -ff -e open java SecureRandoms |& grep /dev/ </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 5 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 6 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 7 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 8 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 9 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 10 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">Looking at jdk/src/solaris/classes/sun/security/provider/NativePRNG.java </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">it looks like 2 file descriptors are created for every variant of NativePRNG, whether or not they are ever used. Which is wasteful. In fact, you only ever need at most two file descriptors, one for /dev/random and one for /dev/urandom. </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">

<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">Further, it would be nice if the file descriptors were closed when idle and lazily re-created. Especially /dev/random should typically be used at startup and never thereafter.</span><br>

</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 20, 2014 at 7:59 AM, Alan Bateman <span dir="ltr"><<a href="mailto:Alan.Bateman@oracle.com" target="_blank">Alan.Bateman@oracle.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On 20/06/2014 15:02, Peter Levart wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
And, as Martin pointed out, it seems to be used for tests that exercise particular responses from NameService API to test the behaviour of JDK classes. It would be a shame for those tests to go away.<br>
</blockquote></div>
We've been talking about removing it for many years because it has been so troublesome. If we really need to having something for testing then I don't think it needs to be general purpose, we can get right of the lookup at least.<span><font color="#888888"><br>


<br>
-Alan.<br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>