public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: libgcj/9271: Severe bias in java.security.SecureRandom
@ 2003-02-13 16:54 tromey
  0 siblings, 0 replies; 2+ messages in thread
From: tromey @ 2003-02-13 16:54 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, java-prs, nobody, rsdio, tromey

Synopsis: Severe bias in java.security.SecureRandom

Responsible-Changed-From-To: unassigned->tromey
Responsible-Changed-By: tromey
Responsible-Changed-When: Thu Feb 13 16:54:39 2003
Responsible-Changed-Why:
    I'm handling the administration
State-Changed-From-To: open->closed
State-Changed-By: tromey
State-Changed-When: Thu Feb 13 16:54:39 2003
State-Changed-Why:
    I'm checking in the fix.
    It will appear in gcj 3.3.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9271


^ permalink raw reply	[flat|nested] 2+ messages in thread

* libgcj/9271: Severe bias in java.security.SecureRandom
@ 2003-01-11  3:56 rsdio
  0 siblings, 0 replies; 2+ messages in thread
From: rsdio @ 2003-01-11  3:56 UTC (permalink / raw)
  To: gcc-gnats


>Number:         9271
>Category:       libgcj
>Synopsis:       Severe bias in java.security.SecureRandom
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jan 10 19:56:00 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Casey Marshall
>Release:        gcj (GCC) 3.4 20030105 (experimental) (probably in most versions)
>Organization:
>Environment:
Linux 2.4.19-gentoo-r9 i686
configure: --enable-threads=posix --enable-shared --enable-languages=c++,java
>Description:
java.security.SecureRandom.nextBoolean() returns `true' an inordinate number of times.

java.security.SecureRandom.nextInt() returns negative integers too often.

The problem seems to be in the next(int numBits) method of java.security.SecureRandom. When packing the bits into the integer, line 360:

   for (int i = 0; i < tmp.length; i++)
      ret |= tmp[i] << (8 * i);

promotes the byte tmp[i] to an integer, therefore if tmp[i] is *negative* `ret' will be OR'd with 0xffffffXX, shifted left. This behavior is incorrect.

Furthermore, this method returns 8 bits of random data for next(1), meaning that nextBoolean() (in java.util.Random), which returns:

   return next(1) != 0;

will return `false' with only a 1 in 256 probability!
>How-To-Repeat:
// Shows that SecureRandom.nextBoolean() returns
// true far more often than false.

class randtest {
public static void main(String[] argv) {
   int ntrue = 0;
   int nfalse = 0;

   SecureRandom rand = new SecureRandom();
   for (int i = 0; i < 10000; i++)
      if (rand.nextBoolean())
         ntrue++;
      else
         nfalse++;

   System.out.println("true=" + ntrue + " false=" + nfalse);
   System.out.println("proportion t:f=" +
      ((double) ntrue / (double) nfalse) + ":1");
}
}
>Fix:
Patch as follows:

diff -u -r1.7 SecureRandom.java
--- libjava/java/security/SecureRandom.java     13 Dec 2002 14:21:07 -0000     1.7
+++ libjava/java/security/SecureRandom.java     11 Jan 2003 03:39:30 -0000
@@ -358,9 +358,13 @@
     int ret = 0;
 
     for (int i = 0; i < tmp.length; i++)
-      ret |= tmp[i] << (8 * i);
+      ret |= tmp[i] & 0xFF << (8 * i);
 
-    return ret;
+    int mask = 0;
+    for (int i = 0; i < numBits; i++)
+      mask |= 1 << i;
+
+    return ret & mask;
   }
 
   /**
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-02-13 16:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-13 16:54 libgcj/9271: Severe bias in java.security.SecureRandom tromey
  -- strict thread matches above, loose matches on Subject: below --
2003-01-11  3:56 rsdio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).