public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: rsdio@metastatic.org To: gcc-gnats@gcc.gnu.org Subject: libgcj/9271: Severe bias in java.security.SecureRandom Date: Sat, 11 Jan 2003 03:56:00 -0000 [thread overview] Message-ID: <20030111035154.24761.qmail@sources.redhat.com> (raw) >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:
next reply other threads:[~2003-01-11 3:56 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2003-01-11 3:56 rsdio [this message] 2003-02-13 16:54 tromey
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20030111035154.24761.qmail@sources.redhat.com \ --to=rsdio@metastatic.org \ --cc=gcc-gnats@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).