This fixes some build issues associated with Mario's patch.
It was necessary to move java.security.VMSecureRandom to
gnu.java.security.jce.prng.VMSecureRandom so it could be
accessed.
ChangeLog:
2009-02-03 Andrew John Hughes <[email protected]>
PR classpath/38417:
* gnu/java/security/jce/prng/SecureRandomAdapter.java:
Remove unneeded import.
* gnu/javax/crypto/jce/prng/FortunaImpl.java:
Fix typo.
* java/security/SecureRandom.java:
Remove duplicate use of VMSecureRandom,
call SecureRandomAdapter instead.
* vm/reference/gnu/java/security/jce/prng/VMSecureRandom.java:
Moved from java/security so SecureRandomAdapter can
access it.
--
Andrew :)
Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net
PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint = F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8
Index: gnu/java/security/jce/prng/SecureRandomAdapter.java
===================================================================
RCS file:
/sources/classpath/classpath/gnu/java/security/jce/prng/SecureRandomAdapter.java,v
retrieving revision 1.4
diff -u -u -r1.4 SecureRandomAdapter.java
--- gnu/java/security/jce/prng/SecureRandomAdapter.java 3 Feb 2009 21:02:58
-0000 1.4
+++ gnu/java/security/jce/prng/SecureRandomAdapter.java 3 Feb 2009 23:05:15
-0000
@@ -45,7 +45,6 @@
import java.security.AccessController;
import java.security.SecureRandom;
-import java.security.VMSecureRandom;
import java.security.SecureRandomSpi;
import java.util.Collections;
@@ -101,7 +100,7 @@
}
public static final byte[] getSeed(int numBytes)
- {
+ {
URL sourceUrl = null;
String urlStr = null;
Index: gnu/javax/crypto/jce/prng/FortunaImpl.java
===================================================================
RCS file:
/sources/classpath/classpath/gnu/javax/crypto/jce/prng/FortunaImpl.java,v
retrieving revision 1.3
diff -u -u -r1.3 FortunaImpl.java
--- gnu/javax/crypto/jce/prng/FortunaImpl.java 3 Feb 2009 21:02:59 -0000
1.3
+++ gnu/javax/crypto/jce/prng/FortunaImpl.java 3 Feb 2009 23:05:17 -0000
@@ -93,7 +93,7 @@
}
}
- protected byte[] engineGenerateSeed(int numbytes)
+ protected byte[] engineGenerateSeed(int numBytes)
{
return SecureRandomAdapter.getSeed(numBytes);
}
Index: java/security/SecureRandom.java
===================================================================
RCS file: /sources/classpath/classpath/java/security/SecureRandom.java,v
retrieving revision 1.25
diff -u -u -r1.25 SecureRandom.java
--- java/security/SecureRandom.java 16 Mar 2008 22:44:40 -0000 1.25
+++ java/security/SecureRandom.java 3 Feb 2009 23:05:21 -0000
@@ -42,6 +42,7 @@
import gnu.java.lang.CPStringBuilder;
import gnu.java.security.Engine;
import gnu.java.security.action.GetSecurityPropertyAction;
+import gnu.java.security.jce.prng.SecureRandomAdapter;
import gnu.java.security.jce.prng.Sha160RandomSpi;
import java.io.IOException;
@@ -401,9 +402,7 @@
*/
public static byte[] getSeed(int numBytes)
{
- byte[] tmp = new byte[numBytes];
- generateSeed(tmp);
- return tmp;
+ return SecureRandomAdapter.getSeed(numBytes);
}
/**
@@ -418,64 +417,4 @@
return secureRandomSpi.engineGenerateSeed(numBytes);
}
- // Seed methods.
-
- private static final String SECURERANDOM_SOURCE = "securerandom.source";
- private static final String JAVA_SECURITY_EGD = "java.security.egd";
- private static final Logger logger =
Logger.getLogger(SecureRandom.class.getName());
-
- private static int generateSeed(byte[] buffer)
- {
- return generateSeed(buffer, 0, buffer.length);
- }
-
- private static int generateSeed(byte[] buffer, int offset, int length)
- {
- URL sourceUrl = null;
- String urlStr = null;
-
- GetSecurityPropertyAction action = new
GetSecurityPropertyAction(SECURERANDOM_SOURCE);
- try
- {
- urlStr = (String) AccessController.doPrivileged(action);
- if (urlStr != null)
- sourceUrl = new URL(urlStr);
- }
- catch (MalformedURLException ignored)
- {
- logger.log(Level.WARNING, SECURERANDOM_SOURCE + " property is
malformed: {0}",
- urlStr);
- }
-
- if (sourceUrl == null)
- {
- try
- {
- urlStr = SystemProperties.getProperty(JAVA_SECURITY_EGD);
- if (urlStr != null)
- sourceUrl = new URL(urlStr);
- }
- catch (MalformedURLException mue)
- {
- logger.log(Level.WARNING, JAVA_SECURITY_EGD + " property is
malformed: {0}",
- urlStr);
- }
- }
-
- if (sourceUrl != null)
- {
- try
- {
- InputStream in = sourceUrl.openStream();
- return in.read(buffer, offset, length);
- }
- catch (IOException ioe)
- {
- logger.log(Level.FINE, "error reading random bytes", ioe);
- }
- }
-
- // If we get here, we did not get any seed from a property URL.
- return VMSecureRandom.generateSeed(buffer, offset, length);
- }
}
Index: vm/reference/gnu/java/security/jce/prng/VMSecureRandom.java
===================================================================
RCS file: vm/reference/gnu/java/security/jce/prng/VMSecureRandom.java
diff -N vm/reference/gnu/java/security/jce/prng/VMSecureRandom.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ vm/reference/gnu/java/security/jce/prng/VMSecureRandom.java 3 Feb 2009
23:05:27 -0000
@@ -0,0 +1,129 @@
+/* VMSecureRandom.java -- random seed generator.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is a part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+USA
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.security.jce.prng;
+
+/**
+ * VM-specific methods for generating real (or almost real) random
+ * seeds. VM implementors should write a version of this class that
+ * reads random bytes from some system source.
+ *
+ * <p>The default implementation of this class runs eight threads that
+ * increment counters in a tight loop, and XORs each counter to
+ * produce one byte of seed data. This is not very efficient, and is
+ * not guaranteed to be random (the thread scheduler is probably
+ * deterministic, after all). If possible, VM implementors should
+ * reimplement this class so it obtains a random seed from a system
+ * facility, such as a system entropy gathering device or hardware
+ * random number generator.
+ */
+final class VMSecureRandom
+{
+
+ /**
+ * Generate a random seed. Implementations are free to generate
+ * fewer random bytes than are requested, and leave the remaining
+ * bytes of the destination buffer as zeros. Implementations SHOULD,
+ * however, make a best-effort attempt to satisfy the request.
+ *
+ * @param buffer The destination buffer.
+ * @param offset The offset in the buffer to start putting bytes.
+ * @param length The number of random bytes to generate.
+ */
+ static int generateSeed(byte[] buffer, int offset, int length)
+ {
+ if (length < 0)
+ throw new IllegalArgumentException("length must be nonnegative");
+ if (offset < 0 || offset + length > buffer.length)
+ throw new IndexOutOfBoundsException();
+
+ Spinner[] spinners = new Spinner[8];
+ int n = 0x1;
+ for (int i = 0; i < spinners.length; i++)
+ {
+ spinners[i] = new Spinner((byte) n);
+ Thread t = new Thread(spinners[i]);
+ t.start();
+ n <<= 1;
+ }
+
+ // Wait until at least one spinner has started.
+ while (!(spinners[0].running || spinners[1].running || spinners[2].running
+ || spinners[3].running || spinners[4].running ||
spinners[5].running
+ || spinners[6].running || spinners[7].running))
+ {
+ Thread.yield();
+ }
+
+ for (int i = offset; i < length; i++)
+ {
+ buffer[i] = (byte) (spinners[0].value ^ spinners[1].value ^
spinners[2].value
+ ^ spinners[3].value ^ spinners[4].value ^
spinners[5].value
+ ^ spinners[6].value ^ spinners[7].value);
+ Thread.yield();
+ }
+
+ for (int i = 0; i < spinners.length; i++)
+ spinners[i].stop();
+
+ return length;
+ }
+
+ static class Spinner implements Runnable
+ {
+ volatile byte value;
+ volatile boolean running;
+
+ Spinner(final byte initial)
+ {
+ value = initial;
+ }
+
+ public void run()
+ {
+ running = true;
+ while (running)
+ value++;
+ }
+
+ private void stop()
+ {
+ running = false;
+ }
+ }
+}
\ No newline at end of file