summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2013-01-26 17:31:23 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2013-01-26 17:31:23 +0000
commitef8946645ef2915149dc65ddc774aa91dd97cce9 (patch)
tree42842a4e758253766b4fecb820f4414fbd043888
parente5e239ddc77fa87c0c6fafeb94a83511005c329b (diff)
downloadcryptopp-ef8946645ef2915149dc65ddc774aa91dd97cce9.tar.gz
handle EAGAIN from /dev/urandom and /dev/random (Folkert van Heusden)
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@538 57ff6487-cd31-0410-9ec3-f628ee90f5f0
-rw-r--r--osrng.cpp29
1 files changed, 25 insertions, 4 deletions
diff --git a/osrng.cpp b/osrng.cpp
index fa6dd36..76e486b 100644
--- a/osrng.cpp
+++ b/osrng.cpp
@@ -83,8 +83,22 @@ void NonblockingRng::GenerateBlock(byte *output, size_t size)
if (!CryptGenRandom(m_Provider.GetProviderHandle(), (DWORD)size, output))
throw OS_RNG_Err("CryptGenRandom");
#else
- if (read(m_fd, output, size) != size)
- throw OS_RNG_Err("read /dev/urandom");
+ while (size)
+ {
+ ssize_t len = read(m_fd, output, size);
+
+ if (len < 0)
+ {
+ // /dev/urandom reads CAN give EAGAIN errors! (maybe EINTR as well)
+ if (errno != EINTR && errno != EAGAIN)
+ throw OS_RNG_Err("read /dev/urandom");
+
+ continue;
+ }
+
+ output += len;
+ size -= len;
+ }
#endif
}
@@ -119,10 +133,17 @@ void BlockingRng::GenerateBlock(byte *output, size_t size)
while (size)
{
// on some systems /dev/random will block until all bytes
- // are available, on others it will returns immediately
+ // are available, on others it returns immediately
ssize_t len = read(m_fd, output, size);
if (len < 0)
- throw OS_RNG_Err("read " CRYPTOPP_BLOCKING_RNG_FILENAME);
+ {
+ // /dev/random reads CAN give EAGAIN errors! (maybe EINTR as well)
+ if (errno != EINTR && errno != EAGAIN)
+ throw OS_RNG_Err("read " CRYPTOPP_BLOCKING_RNG_FILENAME);
+
+ continue;
+ }
+
size -= len;
output += len;
if (size)