RandomNumberGenerator

From Crypto++ Wiki
(Redirected from AutoSeededRandomPool)
Jump to: navigation, search
RandomNumberGenerator
Documentation
#include <cryptopp/cryptlib.h>

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

In general, use an AutoSeeded generator, and add entropy to the generator before extracting bits. If you are using a generator in a multithreaded program, then use a single generator per thread or provide an external lock for a single generator.

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
Documentation
#include <cryptopp/rng.h>

LC_RNG is a Linear Congruential Generator. 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

RandomPool
Documentation
#include <cryptopp/randpool.h>

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;
SecByteBlock seed( SEEDSIZE );

// Scratch Area
const unsigned int BLOCKSIZE = 16 * 8;
SecByteBlock scratch( BLOCKSIZE );

...

// Random Pool Initalization
CryptoPP::RandomPool rng( SEEDSIZE );
rng.Put( seed, seed.size() );
    
rng.GenerateBlock( scratch, scratch.size() );

AutoSeededRandomPool

AutoSeededRandomPool
Documentation
#include <cryptopp/osrng.h>

Unlike LG_RNG and RandomPool, AutoSeeded generators do not require a seed. 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;
SecByteBlock scratch( BLOCKSIZE );

// Construction
CryptoPP::AutoSeededRandomPool rng;

// Random Block
rng.GenerateBlock( scratch, scratch.size() );

AutoSeededX917RNG

AutoSeededX917RNG
Documentation
#include <cryptopp/osrng.h>

When using an X917 generator, one must specify an approved Block Cipher as a template parameter.

// Scratch Area
const unsigned int BLOCKSIZE = 16 * 8;
SecByteBlock scratch( BLOCKSIZE );

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

// Random Block
rng.GenerateBlock( scratch, scratch.size() );

RandomNumberSource

RandomNumberSource
Documentation
#include <cryptopp/filters.h>

A RandomNumberSource allows you to use a generator in a pipeline.

const unsigned int BLOCKSIZE = 16 * 8;
SecByteBlock scratch( BLOCKSIZE );

AutoSeededRandomPool prng;

// Extract BLOCKSIZE bytes of random bits
RandomNumberSource(prng, scratch.size(), true, new ArraySink( scratch, scratch.size() ));

RandomNumberSink

RandomNumberSink
Documentation
#include <cryptopp/filters.h>

A RandomNumberSink allows you to add entropy to a generator. Note: this is one of the times pumpAll = false is used for a Source.

const unsigned int BLOCKSIZE = 16 * 8;
SecByteBlock scratch( BLOCKSIZE );

RandomPool prng;
FileSource entropy("/dev/urandom", false, new RandomNumberSink(prng));

// Add 16 bytes of entropy before generating a block of random bits
entropy.Pump(16);
prng.GenerateBlock( scratch, scratch.size() );

// Add 16 bytes of entropy before generating a block of random bits
entropy.Pump(16);
prng.GenerateBlock( scratch, scratch.size() );
...

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

AutoSeededRandomPool

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