News for hackers. Stuff that matters.
Re:How to safely generate a random number by eliteraspberrie on Tuesday February 25, 2014 @08:59PM (#7302266)
Use urandom, but that's not the end of it. It may be that urandom supports non-blocking reads. Most implementations only check if read() returns -1 then abort, without checking for EAGAIN, not to mention EINTR, or some other subtleties like making sure not to request more than SSIZE_MAX bytes. The randombytes function in NaCl does it right, unfortunately libsodium did their own thing.

If you use Python, read from os.urandom, which is a wrapper to the system's CSPRNG. Don't use random.randint or random.getrandbits...

Re:How to safely generate a random number by pbsd on Tuesday February 25, 2014 @09:13PM (#7302323)
> The randombytes function in NaCl does it right, unfortunately libsodium did their own thing.

I don't know what you're talking about. Sodium does the same thing as NaCl, while also being more portable.

Re:How to safely generate a random number by eliteraspberrie on Wednesday February 26, 2014 @12:58PM (#7306408)
Re:How to safely generate a random number by pbsd on Wednesday February 26, 2014 @01:17PM (#7306537)
I'm aware that the implementations are not strictly equal, but how is libsodium doing it 'wrong'? Is it because it does not partition reads into smaller chunks (avoiding the unspecified >= SSIZE_MAX read)? I consider any reasonable answer a valid bug report.

I should also point out that randombytes is technically not part of NaCl. The authors maintain that random byte generation is out of scope for NaCl, and what actually happens is that randombytes.o is bundled separately from libnacl.a [1]. In TweetNacl the randombytes implementation is completely omitted.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?msg=20;bug=694...

Re:How to safely generate a random number by eliteraspberrie on Wednesday February 26, 2014 @03:26PM (#7307438)
Don't take my word for it, check libsodium with Frama-C (as NaCl was) and file the bug report yourself.
Re:How to safely generate a random number by vertex-four on Tuesday February 25, 2014 @10:01PM (#7302580)
You can also use random.SystemRandom, which gives you the same interface as random.Random (and the random module) but as a wrapper around os.urandom.