[kaffe] CVS kaffe (stack): Fixes for SHA1PRNG and a bug in the test suite Makefile.

Kaffe CVS Kaffe Mailing List <kaffe@kaffe.org>
Tue Jan 20 15:39:02 2004


PatchSet 4356 
Date: 2004/01/20 23:23:59
Author: stack
Branch: HEAD
Tag: (none) 
Log:
Fixes for SHA1PRNG and a bug in the test suite Makefile.

Members: 
	ChangeLog:1.1941->1.1942 
	libraries/javalib/Makefile.am:1.162->1.163 
	libraries/javalib/Makefile.in:1.218->1.219 
	libraries/javalib/kaffe/security/LameRandomness.java:INITIAL->1.1 
	libraries/javalib/kaffe/security/Randomness.java:INITIAL->1.1 
	libraries/javalib/kaffe/security/UnixRandomness.java:INITIAL->1.1 
	libraries/javalib/kaffe/security/provider/SHA1PRNG.java:1.6->1.7 
	libraries/javalib/profiles/allatonce/all.files:1.35->1.36 
	libraries/javalib/profiles/default/core.files:1.23->1.24 
	test/regression/Makefile.am:1.85->1.86 
	test/regression/Makefile.in:1.155->1.156 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1941 kaffe/ChangeLog:1.1942
--- kaffe/ChangeLog:1.1941	Mon Jan 19 17:47:59 2004
+++ kaffe/ChangeLog	Tue Jan 20 23:23:59 2004
@@ -1,3 +1,26 @@
+2004-01-20  Timothy S. Stack <stack@cs.utah.edu>
+
+	* test/regression/Makefile.am:
+	Fix JASMIN_TESTS/BCEL_TESTS variables since they were being set to
+	FOO_TESTS_DIST and not $(FOO_TESTS_DIST).
+
+	* libraries/javalib/kaffe/security/LameRandomness.java,
+	libraries/javalib/kaffe/security/Randomness.java,
+	libraries/javalib/kaffe/security/UnixRandomness.java:
+	New files.  Used to break out the initial source of randomness for
+	SHA1PRNG.
+
+	* libraries/javalib/kaffe/security/provider/SHA1PRNG.java:
+	Make setSeed and nextBytes methods synchronized (Reported by:
+	"jrandom").  Allow for the initial source of randomness to be
+	determined at run-time so we can take advantage of "/dev/random",
+	if it is available.
+
+	* libraries/javalib/Makefile.am,
+	libraries/javalib/profiles/allatonce/all.files,
+	libraries/javalib/profiles/default/core.files:
+	Add kaffe/security/*Randomness.java.
+
 2004-01-19  Dalibor Topic <robilad@kaffe.org>
 
 	* config/arm/jit.h
Index: kaffe/libraries/javalib/Makefile.am
diff -u kaffe/libraries/javalib/Makefile.am:1.162 kaffe/libraries/javalib/Makefile.am:1.163
--- kaffe/libraries/javalib/Makefile.am:1.162	Sat Jan 17 18:00:40 2004
+++ kaffe/libraries/javalib/Makefile.am	Tue Jan 20 23:24:01 2004
@@ -160,6 +160,7 @@
 	$(kaffe_jar_SRCS) \
 	$(kaffe_lang_SRCS) \
 	$(kaffe_management_SRCS) \
+	$(kaffe_security_SRCS) \
 	$(kaffe_security_provider_SRCS) \
 	$(kaffe_tools_jar_SRCS) \
 	$(kaffe_tools_native2ascii_SRCS) \
@@ -2083,6 +2084,10 @@
 	kaffe/management/Debug.java \
 	kaffe/management/JIT.java \
 	kaffe/management/XProfiler.java
+kaffe_security_SRCS = \
+	kaffe/security/LameRandomness.java \
+	kaffe/security/Randomness.java \
+	kaffe/security/UnixRandomness.java
 kaffe_security_provider_SRCS = \
 	kaffe/security/provider/Kaffe.java \
 	kaffe/security/provider/MD2.java \
Index: kaffe/libraries/javalib/Makefile.in
diff -u kaffe/libraries/javalib/Makefile.in:1.218 kaffe/libraries/javalib/Makefile.in:1.219
--- kaffe/libraries/javalib/Makefile.in:1.218	Sat Jan 17 18:00:40 2004
+++ kaffe/libraries/javalib/Makefile.in	Tue Jan 20 23:24:01 2004
@@ -441,6 +441,7 @@
 	$(kaffe_jar_SRCS) \
 	$(kaffe_lang_SRCS) \
 	$(kaffe_management_SRCS) \
+	$(kaffe_security_SRCS) \
 	$(kaffe_security_provider_SRCS) \
 	$(kaffe_tools_jar_SRCS) \
 	$(kaffe_tools_native2ascii_SRCS) \
@@ -2478,6 +2479,11 @@
 	kaffe/management/Debug.java \
 	kaffe/management/JIT.java \
 	kaffe/management/XProfiler.java
+
+kaffe_security_SRCS = \
+	kaffe/security/LameRandomness.java \
+	kaffe/security/Randomness.java \
+	kaffe/security/UnixRandomness.java
 
 kaffe_security_provider_SRCS = \
 	kaffe/security/provider/Kaffe.java \
===================================================================
Checking out kaffe/libraries/javalib/kaffe/security/LameRandomness.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/kaffe/security/LameRandomness.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/kaffe/security/LameRandomness.java	Tue Jan 20 23:26:50 2004
@@ -0,0 +1,52 @@
+/*
+ * LameRandomness.java
+ *
+ * Copyright (c) 2004 The University of Utah and the Flux Group.
+ * All rights reserved.
+ *
+ * This file is licensed under the terms of the GNU Public License.
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ *
+ * Contributed by the Flux Research Group, Department of Computer Science,
+ * University of Utah, http://www.cs.utah.edu/flux/
+ */
+
+package kaffe.security;
+
+import java.util.Random;
+
+/**
+ * Lame implementation of kaffe.security.Randomness, just uses a value from
+ * java.util.Random.
+ */
+public class LameRandomness
+    extends Randomness
+{
+	/**
+	 * Construct an empty LameRandomness object.
+	 */
+	public LameRandomness()
+	{
+	}
+	
+	/**
+	 * Fill the bits array using java.util.Random#nextBytes().
+	 *
+	 * @see kaffe.security.Randomness#fill()
+	 */
+	public void fill(byte bits[])
+	{
+		new Random().nextBytes(bits);
+	}
+	
+	/**
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString()
+	{
+		return "LameRandomness["
+			+ super.toString()
+			+ "]";
+	}
+}
===================================================================
Checking out kaffe/libraries/javalib/kaffe/security/Randomness.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/kaffe/security/Randomness.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/kaffe/security/Randomness.java	Tue Jan 20 23:26:51 2004
@@ -0,0 +1,44 @@
+/*
+ * Randomness.java
+ *
+ * Copyright (c) 2004 The University of Utah and the Flux Group.
+ * All rights reserved.
+ *
+ * This file is licensed under the terms of the GNU Public License.
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ *
+ * Contributed by the Flux Research Group, Department of Computer Science,
+ * University of Utah, http://www.cs.utah.edu/flux/
+ */
+
+package kaffe.security;
+
+/**
+ * Base class for implementations of initial sources of randomness.
+ */
+public abstract class Randomness
+{
+	/**
+	 * Construct an empty Randomness object.
+	 */
+	public Randomness()
+	{
+	}
+	
+	/**
+	 * Fill the given array with some random bits.
+	 *
+	 * @param bits The array to fill with random bits.
+	 */
+	public abstract void fill(byte bits[]);
+
+	/**
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString()
+	{
+		return "Randomness["
+			+ "]";
+	}
+}
===================================================================
Checking out kaffe/libraries/javalib/kaffe/security/UnixRandomness.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/kaffe/security/UnixRandomness.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/kaffe/security/UnixRandomness.java	Tue Jan 20 23:26:51 2004
@@ -0,0 +1,79 @@
+/*
+ * UnixRandomness.java
+ *
+ * Copyright (c) 2004 The University of Utah and the Flux Group.
+ * All rights reserved.
+ *
+ * This file is licensed under the terms of the GNU Public License.
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ *
+ * Contributed by the Flux Research Group, Department of Computer Science,
+ * University of Utah, http://www.cs.utah.edu/flux/
+ */
+
+package kaffe.security;
+
+import java.io.IOException;
+import java.io.DataInputStream;
+import java.io.FileInputStream;
+
+import java.util.Random;
+
+/**
+ * Unix implementation of kaffe.security.Randomness, reads from "/dev/urandom".
+ */
+public class UnixRandomness
+    extends Randomness
+{
+	/**
+	 * The file name of the random device.
+	 *
+	 * XXX Use a property.
+	 */
+	private static final String FILE_NAME = "/dev/urandom";
+
+	/**
+	 * The input stream bound to the random device.
+	 */
+	private final DataInputStream dis;
+
+	/**
+	 * Construct a UnixRandomness object.
+	 *
+	 * @exception IOException if the random device cannot be opened.
+	 */
+	public UnixRandomness()
+		throws IOException
+	{
+		this.dis = new DataInputStream(new FileInputStream(FILE_NAME));
+	}
+
+	/**
+	 * Fill the bits array with data from the random device.
+	 *
+	 * @see kaffe.security.Randomness#fill()
+	 */
+	public synchronized void fill(byte bits[])
+	{
+		try
+		{
+			this.dis.readFully(bits);
+		}
+		catch(IOException e)
+		{
+			/* XXX Better way to fall back? */
+			new Random().nextBytes(bits);
+		}
+	}
+
+	/**
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString()
+	{
+		return "UnixRandomness["
+			+ super.toString()
+			+ "]";
+	}
+}
Index: kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java
diff -u kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java:1.6 kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java:1.7
--- kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java:1.6	Thu Feb 20 13:52:10 2003
+++ kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java	Tue Jan 20 23:24:03 2004
@@ -4,7 +4,7 @@
  * SHA1PRNG.java
  * SHA-1 based pseudo-random number generator.
  *
- * Copyright (c) 2002 The University of Utah and the Flux Group.
+ * Copyright (c) 2002, 2004 The University of Utah and the Flux Group.
  * All rights reserved.
  *
  * This file is licensed under the terms of the GNU Public License.
@@ -17,7 +17,7 @@
 
 package kaffe.security.provider;
 
-import java.util.Random;
+import kaffe.security.Randomness;
 
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -30,6 +30,72 @@
 	extends SecureRandomSpi
 {
 	/**
+	 * The list of randomness implmentation classes, ordered from most to
+	 * least desirable.
+	 */
+	private static final String RANDOMNESS_IMPLS[] = {
+		"kaffe.security.UnixRandomness",
+		"kaffe.security.LameRandomness"
+	};
+
+	/**
+	 * The root source of randomness.
+	 */
+	private static final Randomness impl;
+	
+	static {
+		Randomness rand = null;
+		int lpc;
+
+		/* Walk the list of implementations. */
+		for( lpc = 0;
+		     (lpc < RANDOMNESS_IMPLS.length) && (rand == null);
+		     lpc++ )
+		{
+			try
+			{
+				ClassLoader cl;
+				Class rclass;
+
+				cl = ClassLoader.getSystemClassLoader();
+				/* Try to load and */
+				rclass = cl.loadClass(RANDOMNESS_IMPLS[lpc]);
+				/* ... instantiate an object. */
+				rand = (Randomness)rclass.newInstance();
+				/*
+				 * Success!
+				 * Set a property to inform the user.
+				 */
+				System.setProperty("org.kaffe.randomness",
+						   rclass.getName());
+			}
+			catch(IllegalAccessException e)
+			{
+				/* Really should not happen. */
+				throw new InternalError(e.toString());
+			}
+			catch(ExceptionInInitializerError e)
+			{
+			}
+			catch(InstantiationException e)
+			{
+			}
+			catch(NoClassDefFoundError e)
+			{
+			}
+			catch(ClassNotFoundException e)
+			{
+			}
+		}
+		if( rand == null )
+		{
+			throw new UnsatisfiedLinkError(
+				"Cannot find working Randomness");
+		}
+		impl = rand;
+	}
+	
+	/**
 	 * The "true" random seed size.
 	 */
 	private static final int SEED_SIZE = 8;
@@ -74,14 +140,17 @@
 	 */
 	public SHA1PRNG()
 	{
+		/*
+		 * Fill the seed using the implementation specific source of
+		 * randomness.
+		 */
+		impl.fill(this.seed);
+		
 		try
 		{
 			byte digest[];
 			
 			this.md = MessageDigest.getInstance("SHA-1");
-
-			/* XXX This is a lame source of randomness. */
-			new Random().nextBytes(this.seed);
 			digest = this.md.digest(this.seed);
 			System.arraycopy(digest, 0, this.data, 0, SEED_SIZE);
 		}
@@ -95,7 +164,7 @@
 		}
 	}
 	
-	protected void engineSetSeed(byte[] otherSeed)
+	protected synchronized void engineSetSeed(byte[] otherSeed)
 	{
 		try
 		{
@@ -114,7 +183,7 @@
 		}
 	}
 	
-	protected void engineNextBytes(byte[] bytes)
+	protected synchronized void engineNextBytes(byte[] bytes)
 	{
 		if( bytes.length < (SEED_SIZE - this.dataPos) )
 		{
Index: kaffe/libraries/javalib/profiles/allatonce/all.files
diff -u kaffe/libraries/javalib/profiles/allatonce/all.files:1.35 kaffe/libraries/javalib/profiles/allatonce/all.files:1.36
--- kaffe/libraries/javalib/profiles/allatonce/all.files:1.35	Mon Jan 12 06:28:40 2004
+++ kaffe/libraries/javalib/profiles/allatonce/all.files	Tue Jan 20 23:24:03 2004
@@ -1337,6 +1337,9 @@
 kaffe/management/Debug.java
 kaffe/management/JIT.java
 kaffe/management/XProfiler.java
+kaffe/security/LameRandomness.java
+kaffe/security/Randomness.java
+kaffe/security/UnixRandomness.java
 kaffe/security/provider/Kaffe.java
 kaffe/security/provider/MD2.java
 kaffe/security/provider/MD4.java
Index: kaffe/libraries/javalib/profiles/default/core.files
diff -u kaffe/libraries/javalib/profiles/default/core.files:1.23 kaffe/libraries/javalib/profiles/default/core.files:1.24
--- kaffe/libraries/javalib/profiles/default/core.files:1.23	Tue Jan  6 16:27:55 2004
+++ kaffe/libraries/javalib/profiles/default/core.files	Tue Jan 20 23:24:03 2004
@@ -270,6 +270,9 @@
 kaffe/management/Debug.java
 kaffe/management/JIT.java
 kaffe/management/XProfiler.java
+kaffe/security/LameRandomness.java
+kaffe/security/Randomness.java
+kaffe/security/UnixRandomness.java
 kaffe/security/provider/Kaffe.java
 kaffe/security/provider/MD2.java
 kaffe/security/provider/MD4.java
Index: kaffe/test/regression/Makefile.am
diff -u kaffe/test/regression/Makefile.am:1.85 kaffe/test/regression/Makefile.am:1.86
--- kaffe/test/regression/Makefile.am:1.85	Mon Dec  8 02:46:43 2003
+++ kaffe/test/regression/Makefile.am	Tue Jan 20 23:24:04 2004
@@ -258,8 +258,9 @@
 	MethodSignature7.j \
 	MethodSignature8.j \
 	MethodSignature9.j
+
 if HAVE_JASMIN
-JASMIN_TESTS = JASMIN_TESTS_DIST
+JASMIN_TESTS = $(JASMIN_TESTS_DIST)
 else
 JASMIN_TESTS =
 endif
@@ -267,8 +268,9 @@
 BCEL_TESTS_DIST = \
 	BadClassFileCode.java \
 	BadClassFileConstants.java
+
 if HAVE_BCEL
-BCEL_TESTS = BCEL_TESTS_DIST
+BCEL_TESTS = $(BCEL_TESTS_DIST)
 else
 BCEL_TESTS =
 endif
Index: kaffe/test/regression/Makefile.in
diff -u kaffe/test/regression/Makefile.in:1.155 kaffe/test/regression/Makefile.in:1.156
--- kaffe/test/regression/Makefile.in:1.155	Sat Jan 17 18:00:45 2004
+++ kaffe/test/regression/Makefile.in	Tue Jan 20 23:24:04 2004
@@ -539,13 +539,13 @@
 	MethodSignature9.j
 
 @HAVE_JASMIN_FALSE@JASMIN_TESTS = 
-@HAVE_JASMIN_TRUE@JASMIN_TESTS = JASMIN_TESTS_DIST
+@HAVE_JASMIN_TRUE@JASMIN_TESTS = $(JASMIN_TESTS_DIST)
 BCEL_TESTS_DIST = \
 	BadClassFileCode.java \
 	BadClassFileConstants.java
 
 @HAVE_BCEL_FALSE@BCEL_TESTS = 
-@HAVE_BCEL_TRUE@BCEL_TESTS = BCEL_TESTS_DIST
+@HAVE_BCEL_TRUE@BCEL_TESTS = $(BCEL_TESTS_DIST)
 @USER_WANT_GNU_CRYPTO_FALSE@TEST_GNU_CRYPTO = 
 @USER_WANT_GNU_CRYPTO_TRUE@TEST_GNU_CRYPTO = \
 @USER_WANT_GNU_CRYPTO_TRUE@	TestGnuCrypto.java