noloader@gmail.com wrote:
> I installed Debian 7.3 x64 on a Core i5 laptop for some testing (real
> hardware, not a VM). When testing a program I wrote, I noticed it was
> not getting the full number of bytes requested from /dev/random:
>
> unsigned char buffer[32];
> fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
>
> rc = (int)read(fd, buffer, sizeof(buffer));
> assert(rc == sizeof(buffer));
>
> RAND_add(buffer, sizeof(buffer), 1.0f);
> OPENSSL_cleanse(buffer, sizeof(buffer));
>
> The assert fired on nearly every run, including the first run when no
> previous calls were made by the test program. The number of bytes
> returned varied, but they included 7, 12, 16, and 19. That's really
> bad for a request of 32 bytes, and it would be catastrophic for a
> request of 128-bytes or 256-bytes for a long term private key.
[...]
This is catastrophic only if people don't RTFM about read(). Yes, it
can return a short length. You must call it again if you want more.
(This doesn't normally happen with regular files, although it will if
you specify a length >= (1U << 31).)
Ben.
--
Ben Hutchings
One of the nice things about standards is that there are so many of them.
Attachment:
signature.asc
Description: This is a digitally signed message part