[kaffe] SHA1PRNG fix

Timothy Stack stack@cs.utah.edu
Mon, 3 Jun 2002 14:45:28 -0600 (MDT)


--%--multipart-mixed-boundary-1.22676.1023137128--%
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


hi,

I attached a patch for SHA1PRNG.java, it still wasn't quite doing what it
was supposed to.  Also, i think i forgot to mention that this was based on
Classpath's (broken) code, so the copyright should probably be gpl'ed.

thanks,

tim stack

--%--multipart-mixed-boundary-1.22676.1023137128--%
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Description: ascii text
Content-Disposition: attachment; filename="sha.diff"

Index: SHA1PRNG.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/kaffe/security/provider/SHA1PRNG.java,v
retrieving revision 1.3
diff -u -r1.3 SHA1PRNG.java
--- SHA1PRNG.java	12 May 2002 15:08:46 -0000	1.3
+++ SHA1PRNG.java	3 Jun 2002 20:36:08 -0000
@@ -6,10 +6,6 @@
  *
  * See the file "license.terms" for information on usage and redistribution
  * of this file.
- *
- * NB!!!! THIS DOES NOT ACTUALLY IMPLEMENT SHA1PRNG - it uses random and
- *        is a place holder.
- *
  */
 
 package kaffe.security.provider;
@@ -25,14 +21,15 @@
 public class SHA1PRNG
 	extends SecureRandomSpi
 {
-	private static final int SEED_SIZE = 20;
-	private static final int DATA_SIZE = 40;
+	private static final int SEED_SIZE = 8;
+	private static final int DATA_SIZE = 16;
 	
 	private MessageDigest md;
 	private byte seed[] = new byte[SEED_SIZE];
 	private int seedPos = 0;
 	private byte data[] = new byte[DATA_SIZE];
 	private int dataPos = 0;
+	private long counter = 0;
 	
 	public SHA1PRNG()
 	{
@@ -43,7 +40,7 @@
 			this.md = MessageDigest.getInstance("SHA-1");
 
 			new Random().nextBytes(this.seed);
-			digest = this.md.digest(this.data);
+			digest = this.md.digest(this.seed);
 			System.arraycopy(digest, 0, this.data, 0, SEED_SIZE);
 		}
 		catch(NoSuchAlgorithmException e)
@@ -77,7 +74,8 @@
 	
 	protected void engineNextBytes(byte[] bytes)
 	{
-		if( bytes.length < (20 - this.dataPos) )
+		this.counter += 1;
+		if( bytes.length < (SEED_SIZE - this.dataPos) )
 		{
 			System.arraycopy(this.data, this.dataPos,
 					 bytes, 0,
@@ -112,6 +110,22 @@
 							 this.data,
 							 SEED_SIZE,
 							 SEED_SIZE);
+					this.data[SEED_SIZE    ] =
+						(byte)(this.counter);
+					this.data[SEED_SIZE + 1] =
+						(byte)(this.counter >>  8);
+					this.data[SEED_SIZE + 2] =
+						(byte)(this.counter >> 16);
+					this.data[SEED_SIZE + 3] =
+						(byte)(this.counter >> 24);
+					this.data[SEED_SIZE + 4] =
+						(byte)(this.counter >> 32);
+					this.data[SEED_SIZE + 5] =
+						(byte)(this.counter >> 40);
+					this.data[SEED_SIZE + 6] =
+						(byte)(this.counter >> 48);
+					this.data[SEED_SIZE + 7] =
+						(byte)(this.counter >> 56);
 					digest = this.md.digest(this.data);
 					System.arraycopy(digest,
 							 0,

--%--multipart-mixed-boundary-1.22676.1023137128--%--