News for hackers. Stuff that matters.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by gwillen on Sunday August 11, 2013 @02:27PM (#6195671)
As far as I know that is NOT correct. The wallets in question were all -- to my knowledge -- using the Android platform-provided Java SecureRandom generator.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by patio11 on Sunday August 11, 2013 @02:38PM (#6195726)
As tptacek has mentioned many, many times, SecureRandom is one of those things which is very secure if you understand exactly what it is doing and do not shoot yourself in the foot.

One easy way to shoot yourself in the foot with SecureRandom is to use seed values from a source with low entropy.

http://developer.android.com/reference/java/security/SecureR...

If one were to copy/paste the sort of code samples which show up when one Googles for [Android SecureRandom], one would more than likely call setSeed, possibly in such a fashion as to duplicate seeds and, predictably, make the output of the random number generator deterministic.

I will leave it to one's individual judgement as to whether "It is unlikely a serious developer would copy/paste in crypto code into a project that has real money on the line" is descriptive of the prevalent standard of care in engineering in the Bitcoin community.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by tptacek on Sunday August 11, 2013 @02:40PM (#6195737)
They were explicitly seeding SecureRandom?
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by glurgh on Sunday August 11, 2013 @03:01PM (#6195821)
Not in the bitcoinj library they all appear to be using, no. That, in turn, uses bouncycastle. Bitcoinj is not explicitly seeding SecureRandom, I'm reasonably sure.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by tptacek on Sunday August 11, 2013 @03:20PM (#6195899)
I couldn't find a line of code in bitcoinj that explicitly seeded SecureRandom, or used the explicit-seed constructor.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by glurgh on Sunday August 11, 2013 @03:51PM (#6196066)
Neither could I. Maybe it is SecureRandom itself but then that would be a bigger deal than some broken BitCoin wallets, one would think.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by patio11 on Sunday August 11, 2013 @02:54PM (#6195794)
Don't know. This just seemed like the high-percentage guess.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by Dylan16807 on Sunday August 11, 2013 @02:41PM (#6195738)
If that's the problem, it does lead to a beautiful solution of fixing bugs by removing code entirely.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by tptacek on Sunday August 11, 2013 @02:35PM (#6195707)
If the story here is that Java SecureRandom on Android is bad enough to break DSA, the headline on this story is wrong; it should be something more like "Android Doomed".

Can you provide a link to something corroborating this?

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by gwillen on Sunday August 11, 2013 @03:31PM (#6195961)
Ok, more source diving. Bitcoin Wallet calls into bitcoinj java/com/google/bitcoin/core/Wallet.java sendCoinsOffline(...), which calls completeTx(...), which calls core/Transaction.java signInputs(...), which calls calculateSignature(...), which calls core/ECKey.java sign(...), which performs:

        ECDSASigner signer = new ECDSASigner();
        ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, ecParams);
        signer.init(true, privKey);
        BigInteger[] sigs = signer.generateSignature(input.getBytes());
        return new ECDSASignature(sigs[0], sigs[1]);
This _appears_ to be a correct invocation of spongycastle-formerly-bouncycastle, initializing the signer with an instance of ECPrivateKeyParametes, and NOT an instance of ParametersWithRandom, so that, on org/spongycastle/crypto/signers/ECDSASigner.java line 41, we call SecureRandom() with no arguments.

I don't see spongycastle ever calling setSeed, and I don't see it ever leaking its SecureRandom instance, so unless calling setSeed on ANY SecureRandom instance is a problem, this _looks_ like a correct usage.

Also, I now remember how much I hate reading Java.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by gwillen on Sunday August 11, 2013 @02:53PM (#6195787)
I have done a bit of source diving in response to your question, and the answer is that all I can provide is the bitcoiners' announcement:

https://bitcointalk.org/index.php?topic=271831.0

They claim the problem lies with 'a component of Android'. One of them told me that the solution was to switch from using SecureRandom to reading /dev/urandom directly. The actual source changes appear not to be public, and he wouldn't tell me details about the issue.

I tried to find the usages of SecureRandom in a couple of the apps that are supposed to be affected, but a cursory search didn't turn up much. I suspect it's inside some library that I don't know to look inside. The question, I guess, is whether all the affected apps share the same library or not -- I don't _think_ so, but if they do it would dramatically reduce my confidence that the issue lies with the Android platform, as claimed.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by glurgh on Sunday August 11, 2013 @03:04PM (#6195827)
That's the lib, if you're bored enough to keep diving

https://code.google.com/p/bitcoinj/

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by gwillen on Sunday August 11, 2013 @03:15PM (#6195873)
OK, I dove a little more. The actual signing happens in org.spongycastle.crypto.signers.ECDSASigner . I haven't dug into _that_ yet.

But one thing I'm wondering is: would any setSeed on any SecureRandom instance cause a potential problem? Or just on the same instance being used for signing? In other words, do I only need to check that spongycastle -- which I presume is a fork of bouncycastle -- handles things reasonably? Or could any other messing about with SecureRandom mess it up?

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by benmmurphy on Sunday August 11, 2013 @08:47PM (#6197069)
the only other thing i see is the openssl securerandom code doesn't properly check the return value. it is possible for RAND_bytes to fail and for the securerandom code to not throw an exception. i think this is quite unlikely to happen. so basically:

1) reading from /dev/urandom takes more than 10ms causing an entropy error

2) a malloc fails causing the error to be saved to the fallback ERR_STATE instead of the thread local ERR_STATE

3) java sees failing code and tries to read the last error but now malloc succeeds so it reads it from its local thread ERR_STATE which succeeds or some other thread has clobbered the fallback ERR_STATE

4) java doesn't throw an exception because it sees no error

the other possibility is the openssl random state is shared between processes due to forking. i hear there is a process called the zygote that warms up the vm and forks to create apps. if it initialises openssl then it is possible child processes could get the same random state.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by robertotyley on Monday August 12, 2013 @11:06AM (#6199578)
SpongyCastle is a repackage of BouncyCastle specifically for the Android platform, an unfortunate necessity due to Android shipping with a cut-down version of Bouncy Castle (see https://code.google.com/p/android/issues/detail?id=3280 & http://rtyley.github.io/spongycastle/ ).

...I'm the packager of SpongyCastle (a task a well-trained monkey could do, I don't alter the code other than package-renaming it), and I confess some relief that the issue is with Android's in-built SecureRandom class, rather than anything in (Bouncy|Spongy)Castle.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by benmmurphy on Sunday August 11, 2013 @06:09PM (#6196535)
if they are using SecureRandom backed by openssl then i think any instance is a problem. However, the 'seed' is used to augment the state and not override it. I'm not sure if it is possible or how much data would be required to get predictable output from SecureRandom by calling setSeed.

http://www.openssl.org/docs/crypto/RAND_add.html

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by tptacek on Sunday August 11, 2013 @03:25PM (#6195926)
Not seeing any relevant code that sets seeds in SpongyCastle, either.
Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by gwillen on Sunday August 11, 2013 @02:59PM (#6195808)
Actually, someone elsewhere in the thread has found, and linked, the bugfix commit, in another Android bitcoin wallet:

https://code.google.com/p/bitcoin-wallet/source/detail?name=...

So literally they're just ripping out SecureRandom and replacing it with their own stub that reads /dev/urandom.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by VMG on Sunday August 11, 2013 @02:43PM (#6195750)
Well there is this paper: http://www.scribd.com/doc/131955288/Randomly-Failed-The-Stat...

Quote:

> Apache Harmony revealed multiple weaknesses caused by implementation bugs. As a part of Android a plethora of cryptographic functions [17] rely on this PRNG. One of the bugs addresses directly the Android platform, where as the second one only targets Apache Harmony.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by tptacek on Sunday August 11, 2013 @02:54PM (#6195791)
The bug described here isn't sufficient to generate repeated DSA k-values, even if it is still in the code†, which is unlikely. It seems more likely that the application code used SecureRandom insecurely, as described upthread.

† It's virtually certain not to be in the code, since Android's CSPRNG is based on OpenSSL now, not Harmony's built-in CSPRNG.

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by makomk on Sunday August 11, 2013 @04:07PM (#6196139)
According to Mike Hearn's comment over at the Bitcoin forums[1]:

> You should not assume that using OpenSSL directly is safe. On Jellybean+ the SecureRandom provider is just a shim over the OpenSSL RAND_* functions and Jellybean+ is also affected. However TLS connections are OK.

> I realise there's going to be a lot of questions about what exactly is going on here, but I'm not on the Android team and can't talk on their behalf. It's up to them to document exactly how the RNG is broken. Suffice it to say, if you aren't sure if you're affected, you probably are but you are welcome to send me a private message detailing what you're doing and I'll let you know.

[1] https://bitcointalk.org/index.php?topic=271486.msg2911376#ms...

Re:Android RNG Weakness Renders Bitcoin Wallets Insecure by benmmurphy on Sunday August 11, 2013 @05:53PM (#6196477)
that is kind of scary. i had a look over the code and the problem seems not obvious. the only issue i saw was multiple instances of SecureRandom share the same state which could cause some problems if you do [1]:

x = new SecureRandom();

y = new SecureRandom();

y.setSeed(predictable);

x.generateBytes(zz);

and also i think the seed is thread local so you can have:

x = new SecureRandom();

x.setSeed(xxx);

then on another thread x.generateBytes()

won't do what you think it will do. i don't think this would cause dupes because i assume apps are isolated from each other and these apps aren't calling setSeed...

[1] this is assuming android people haven't patched openssl to have different behaviour and also setSeed might be safe in openssl because it uses the value to augment and not replace the state.