RandomNumberGenerator

From Crypto++ Wiki
Jump to: navigation, search

Crypto++ provides the following RandomNumberGenerators when using the library. Some are Cryptographically secure, others are not.

Usage

Wei Dai recommends [1] using a Generator on a per thread basis. Additionally, see WORKAROUND_MS_BUG_Q258000.

Pseudo Random Number Generation

LC_RNG

LC_RNG is a Linear Congruential RNG. Though this generator has no cryptographic value, it does allow one to reproduce results when debugging a program. Additionally, it is generally faster at generating a byte block (or stream). If one seeds the LCG with 0x00, a steady stream of 0x80 is the result. Other seeds perform as expected.

RandomPool

The RandomPool behaves similar to an LCG in that the same seed produces the same results. However, unlike LC_RNG, the cipher behind the RandomPool is currently MDC<SHA>. From randpoool.cpp:

typedef MDC<SHA> RandomPoolCipher;

Then RandomPool would be initialized and used as follows:

// Must be at least 16 for RandomPool
const unsigned int SEEDSIZE = 16;
byte pcbSeed[ SEEDSIZE ];

// Scratch Area
const unsigned int BLOCKSIZE = 16 * 8;
byte pcbScratch[ BLOCKSIZE ];

...

// Random Pool Initalization
CryptoPP::RandomPool rng( SEEDSIZE );
rng.Put( pcbSeed, SEEDSIZE );
    
rng.GenerateBlock( pcbScratch, BLOCKSIZE );

AutoSeededX917RNG< BlockCipher >

Unlike LG_RNG and RandomPool, and does not require seed. However, one must specify an approved Block Cipher as a template parameter.

// Scratch Area
const unsigned int BLOCKSIZE = 16 * 8;
byte pcbScratch[ BLOCKSIZE ];

// Construction
CryptoPP::AutoSeededX917RNG<CryptoPP::DES_EDE3> rng;

// Random Block
rng.GenerateBlock( pcbScratch, BLOCKSIZE );

AutoSeededRandomPool

An auto seeded random pool was suggested by [?], which Wei later incorporated into Crypto++ with version [?]. The library uses either method below to seed the generator, depending on the Operating System.

  • CryptGenRandom() by way of a Cryptographic Service Provider
  • /dev/random
  • /dev/urandom

For the Windows NT family, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\RNG\Seed contains the latest seed value that the Windows Crypto API utilizes when generating a pseudo random block.

// Scratch Area
const unsigned int BLOCKSIZE = 16 * 8;
byte pcbScratch[ BLOCKSIZE ];

// Construction
CryptoPP::AutoSeededRandomPool rng;

// Random Block
rng.GenerateBlock( pcbScratch, BLOCKSIZE );

Sample Programs

LCG

LCG.zip - Demonstrates using the Linear Congruential PRNG to generate pseudo random bytes

RandomPool

RandomPool.zip - Demonstrates using a RandomPool to generate pseudo random bytes

AutoSeededX917RNG

AutoSeededX917.zip - Demonstrates using a AutoSeededX917RNG to generate pseudo random bytes

AutoSeedeRandomPool

ASRP.zip - Demonstrates using an AutoSeededRandomPool to generate pseudo random bytes