Here's an updated patch. cryptlib.cpp already includes <osrng.h>, so 
there's no need for the hack.

$ git diff cryptlib.cpp 
diff --git a/cryptlib.cpp b/cryptlib.cpp
index a9ed290..228f18a 100644
--- a/cryptlib.cpp
+++ b/cryptlib.cpp
@@ -252,11 +252,9 @@ byte RandomNumberGenerator::GenerateByte()
 
 word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max)
 {
-       word32 range = max-min;
+       word32 value, range = max-min;
        const int maxBits = BitPrecision(range);
 
-       word32 value;
-
        do
        {
                GenerateBlock((byte *)&value, sizeof(value));
@@ -282,10 +280,10 @@ void 
RandomNumberGenerator::GenerateIntoBufferedTransforma
        FixedSizeSecBlock<byte, 256> buffer;
        while (length)
        {
-               size_t len = UnsignedMin(buffer.size(), length);
-               GenerateBlock(buffer, len);
-               target.ChannelPut(channel, buffer, len);
-               length -= len;
+               const size_t segmentLen = UnsignedMin(buffer.size(), 
length);
+               OS_GenerateRandomBlock(false, buffer, segmentLen);
+               target.ChannelPut(channel, buffer, segmentLen);
+               length -= segmentLen;
        }
 }


On Wednesday, July 8, 2015 at 11:26:53 AM UTC-4, Jeffrey Walton wrote:
>
> Testing of RandomNumberGenerator::GenerateWord32 revealed a bug in 
> GenerateBlock.
>
> GenerateBlock calls GenerateIntoBufferedTransformation. 
> GenerateIntoBufferedTransformation, in turn, calls, GenerateBlock. Ad 
> infinitum.
>
> This patch fixes the circularity by calling OS_GenerateRandomBlock using 
> the OS's default entropy pool for userspace in a non-blocking mode.
>
> Another way to address t is to have GenerateIntoBufferedTransformation 
> throw an Exception with type set to NOT_IMPLEMENTED. But I think that 
> course will be less useful.
>
> Any comments or suggestions?
>
> **********
>
> $ cat cryptlib.diff 
> diff --git a/cryptlib.cpp b/cryptlib.cpp
> index a9ed290..ad173e2 100644
> --- a/cryptlib.cpp
> +++ b/cryptlib.cpp
> @@ -277,13 +277,15 @@ void RandomNumberGenerator::DiscardBytes(size_t n)
>      GenerateIntoBufferedTransformation(TheBitBucket(), DEFAULT_CHANNEL, 
> n);
>  }
>  
> +// Avoid bringing in <osrng.h>, which depends on <cryptlib.h>
> +extern void OS_GenerateRandomBlock(bool, byte*, size_t);
>  void 
> RandomNumberGenerator::GenerateIntoBufferedTransformation(BufferedTransformation
>  
> &target, const std::string &channel, lword length)
>  {
>      FixedSizeSecBlock<byte, 256> buffer;
>      while (length)
>      {
>          size_t len = UnsignedMin(buffer.size(), length);
> -        GenerateBlock(buffer, len);
> +        OS_GenerateRandomBlock(false, buffer, len);
>          target.ChannelPut(channel, buffer, len);
>          length -= len;
>      }
>
 

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to