RandomNumberGenerator

From Crypto++ Wiki

(Redirected from LCG)
Jump to: navigation, search

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

Contents

[edit] Usage

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

[edit] Pseudo Random Number Generation

[edit] 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.

[edit] 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 );

[edit] 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 );

[edit] 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 );

[edit] Sample Programs

[edit] LCG

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

[edit] RandomPool

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

[edit] AutoSeededX917RNG

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

[edit] AutoSeedeRandomPool

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

Personal tools