osdir.com
mailing list archive

Subject: Re: SHA1PRNG synchronization w/ reseed - msg#00266

List: java.vm.kaffe.general

Date: Prev Next Index Thread: Prev Next Index
Hi Tim,

> So, I'm not quite sure if it is supposed to be thread-safe... Can you
> point me to something? Anyways, I ended up just making setSeed/nextByte
> synchronized, hopefully that works for you.

Sounds great. After looking over what I sent, the volatile doesn't seem
strictly necessary since all dangerous uses of those variables are in
the synchronized methods.

I couldn't find anything specifically requiring thread safety in the
SecureRandom docs, but the Random [1] docs do specify that their impl
is synchronized (plus running the same app I'm using under sun JVMs has
never shown the ArrayIndexOutOfBounds (or other runtime exception)).

Digging into the SPI specs didn't seem to include any mention of thread
safety (or lack thereof) either, so it seems that either the SHA1PRNG
could be threadsafe /or/ the SecureRandom.next(int) could synchronize
on the engine - both would be fine by me. Not making either of them
threadsafe is of course an option, but if kaffe goes that route, it might
be best to toss a warning in the docs :)

> Also, I added some new stuff so that it initially pulls from
> "/dev/urandom" if it is available. So, if you experience anything
> strange, holler.

kickass! thanks

=jr

[1] http://java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html#next(int)



Concerned about your privacy? Follow this link to get
FREE encrypted email: https://www.hushmail.com/?l=2

Free, ultra-private instant messaging with Hush Messenger
https://www.hushmail.com/services.php?subloc=messenger&l=434

Promote security and make money with the Hushmail Affiliate Program:
https://www.hushmail.com/about.php?subloc=affiliate&l=427


Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

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

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@xxxxxxxxxxx> + + * 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@xxxxxxxxx> * 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

Next Message by Date: click to view message preview

kaffe on xscale - how to do it

Hi! After spending hours of recompiling and testing I figured out how to build kaffe for PXA based iPaqs: JThreads -------- I needed Christophe's patch to use jthreads. http://www.mail-archive.com/kaffe@xxxxxxxxx/msg04197.html XScale has no fpu therefore you have to change SP_OFFSET: config/arm/linux/md.h: #define SP_OFFSET 20 //#define FP_OFFSET 7 glibc-2.3.2/sysdeps/arm/fpu/bits/setjmp.h -> SP_OFFSET = 20 glibc-2.3.2/sysdeps/arm/bits/setjmp.h -> SP_OFFSET = 8 It's vice versa? Why? Interpreter ----------- The Interpreter runs with the changes mentioned above. JIT --- I'm using the gcc 3.3.2 toolchain from handhelds.org This gcc breaks the code if you compile it with "-O2" I've built gcc twice: once with --with-cpu=strongarm1100 and once with --with-arch=armv4t --with-cpu=xscale. Same result. Therefore use: CFLAGS="-=O0" Furthermore the caches aren't properly flushed. XScale's cache is 32kb. Strongarm SA 1100 only has 16kb. It's recommended that you go through an array that's twice as big as your cache ( http://www.v3space.com/a/a3/75/arm/arm_linux_boot-1.html ), if you clean it that way: config/arm/linux/md.c: #define CACHE_SIZE (32 * 1024) #define LINE_SIZE sizeof(int) #define CACHE_LINES (CACHE_SIZE / LINE_SIZE) static volatile int flusher[CACHE_LINES]; void flush_dcache(void) { int i; for (i = 0; i < CACHE_LINES; i++) { flusher[i]++; } sched_yield(); } So change set: #define CACHE_SIZE (2 * 32 * 1024) The following code-fragment from glibc didn't work. It looks like the 2.4.19-rmk6-pxa1-hh36 kernel I'm currently using doesn't implement cache flushing well. Maybe it needs alignment? The kernel code already seems to perform that step. http://lists.arm.linux.org.uk/pipermail/linux-arm/2000-November/000251.html #define CLEAR_CACHE(BEG,END) \ { \ register unsigned long _beg __asm ("a1") = (unsigned long)(BEG); \ register unsigned long _end __asm ("a2") = (unsigned long)(END); \ register unsigned long _flg __asm ("a3") = 0; \ __asm __volatile ("swi 0x9f0002 @ sys_cacheflush" \ : /* no outputs */ \ : /* no inputs */ \ : "a1"); \ } Starting -------- I get stackoverflows if I run bigger programs. Starting with "-ss 256k" or so should solve the problem. Next Steps ---------- - gcc 3.4 - kernel 2.6 (kernel bug in 2.4?) - get a real cpu ;-) Alright. I could start a massive beast of a java program with that steps. I'd be very interested if someone figures out how to flush the cache the way it sould be done... ~Gérard

Previous Message by Thread: click to view message preview

Re: SHA1PRNG synchronization w/ reseed

> 'lo all, hi, > an app I'm working on is gobbling up entropy like there's no tomorrow, > and > I've run into the following intermittently: > > java.lang.ArrayIndexOutOfBoundsException > at java.lang.System.arraycopy (System.java) > at kaffe.security.provider.SHA1PRNG.engineNextBytes (SHA1PRNG.java:153) > at java.security.SecureRandom.nextBytes (SecureRandom.java:76) > at java.security.SecureRandom.next (SecureRandom.java:83) > at java.util.Random.nextInt (Random.java:108) > > After doing a little digging it seems that the SHA1PRNG impl isn't > threadsafe (this.data, this.dataPos, and counter are updated during > engineNextBytes, and this.seed as well as this.seedPos are updated during > engineSetSeed) So, I'm not quite sure if it is supposed to be thread-safe... Can you point me to something? Anyways, I ended up just making setSeed/nextByte synchronized, hopefully that works for you. Also, I added some new stuff so that it initially pulls from "/dev/urandom" if it is available. So, if you experience anything strange, holler. > hth, > =jr thanks! tim

Next Message by Thread: click to view message preview

Please Reply back Imm

Hai,   I am getting the error frequently.I don't know the reason.How to solve it .Please Reply back.My Application is crashing frequently.Please resolve this this .This very Improtant for me .Please  reply back  imm.Today i have to give .I have no time.Otherwise give some idea give resolve this issue. java.lang.NullPointerException        at java.awt.Toolkit.graDrawChars(Toolkit.java:native)        at java.awt.NativeGraphics.drawChars(NativeGraphics.java:192)        at java.awt.TextBuffer.paint(TextBuffer.java:196)        at java.awt.TextBuffer.paint(TextBuffer.java:165)        at java.awt.TextArea$TextPane.repaintLine(TextArea.java:725)        at java.awt.TextArea$TextPane.repaintRow(TextArea.java:764)        at java.awt.RowCanvas.repaintRows(RowCanvas.java:285)        at java.awt.TextArea$TextPane.paint(TextArea.java:678)        at j ava.awt.RowCanvas.update(RowCanvas.java:356)        at java.awt.Component.processPaintEvent(Component.java:1282)        at java.awt.PaintEvt.dispatch(PaintEvt.java:31)        at java.awt.EventDispatchThread.run(EventDispatchThread.java:35)Dumping live threads:`AWT-EventQueue-0' tid 0x2460010, status SUSPENDED flags DONTSTOP blocked@0x20468d0 (0x2460010->|)`gc' tid 0x2189010, status SUSPENDED flags DONTSTOP blocked@0x21741b0 (0x2189010->|)`finaliser' tid 0x2180010, status SUSPENDED flags DONTSTOP blocked@0x20e7d80 (0x2180010->|)Deadlock: all threads blocked on internal eventsrun.bat: line 4:   966 Aborted                  /usr/local/kaffe/bin/java -ss 100k -mx 16500k -ms 24k -vmdebug GCDIAG yopydemo.Launcher $1 Regards raja N Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by