Cryptopp crash during random number generation

94 views
Skip to first unread message

nick aschberger

unread,
Nov 19, 2009, 10:10:43 PM11/19/09
to cryptop...@googlegroups.com
Hi Folks,

I have a C++ class that is generating a random number using cryptopp, using either almost exactly the same code as per the FAQ.

I also have a test harness that creates and destroys the class, as it applies different testcases to it.

The second time the class is created and the random number generation fn is entered, cryptopp crashes.

The code snippets I have are shown below:

   /*
   // Autoseeded random pool object creates a pool of random numbers to
   // read from, already seeded with a seed created from one of:
   // - CryptGenRandom() by way of a Cryptographic Service Provider
   // - /dev/random
   // - /dev/urandom
   */
   CryptoPP::AutoSeededRandomPool rand_num_gen;
   rand_num_gen.GenerateBlock(plainstring, size);


  Alternately:


   // Alternate method:
   // Declare a random number generator object.
   OFB_Mode<AES>::Encryption s_globalRNG;

   // Init the random generator with a seed based on current time.
   std::string seed = IntToString(time(NULL));
   seed.resize(16);
   s_globalRNG.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data());
   // Use the random generator to generate a random string.
   s_globalRNG.GenerateBlock((byte *)plainstring, size);


Either way, I get a crash.

Has anyone seen this before, or have a suggestion?

cheers

Nick

Jeffrey Walton

unread,
Nov 19, 2009, 10:16:54 PM11/19/09
to nick aschberger, cryptop...@googlegroups.com
Hi Nick,

> rand_num_gen.GenerateBlock(plainstring, size);
> ...
> s_globalRNG.GenerateBlock((byte *)plainstring, size);
What is plainstring? if its a std::string, resize it first then get a
pointer to byte[0]:
plainstring.resize(size);
s_globalRNG.GenerateBlock((byte *)plainstring.data(), plainstring.size());

Jeff
> --
> You received this message because you are subscribed to the "Crypto++ Users"
> Google Group.
> To unsubscribe, send an email to
> cryptopp-user...@googlegroups.com.
> More information about Crypto++ and this group is available at
> http://www.cryptopp.com.

nick aschberger

unread,
Nov 19, 2009, 10:20:29 PM11/19/09
to nolo...@gmail.com, cryptop...@googlegroups.com
2009/11/20 Jeffrey Walton <nolo...@gmail.com>

Hi Nick,

>    rand_num_gen.GenerateBlock(plainstring, size);
>    ...
>    s_globalRNG.GenerateBlock((byte *)plainstring, size);
What is plainstring? if its a std::string, resize it first then get a
pointer to byte[0]:
plainstring.resize(size);
s_globalRNG.GenerateBlock((byte *)plainstring.data(), plainstring.size());

Jeff

Hi Jeff,

plainstring is a char* buffer of size "size".

And the first call has no problems...

cheers

Nick
Reply all
Reply to author
Forward
0 new messages