Crypto++  5.6.3
Free C++ class library of cryptographic schemes
rng.h
Go to the documentation of this file.
1 // rng.h - written and placed in the public domain by Wei Dai
2 
3 //! \file rng.h
4 //! \brief Miscellaneous classes for RNGs
5 //! \details This file contains miscellaneous classes for RNGs, including LC_RNG(),
6 //! X917RNG() and MaurerRandomnessTest()
7 //! \sa osrng.h, randpool.h
8 
9 #ifndef CRYPTOPP_RNG_H
10 #define CRYPTOPP_RNG_H
11 
12 #include "cryptlib.h"
13 #include "filters.h"
14 #include "smartptr.h"
15 
16 NAMESPACE_BEGIN(CryptoPP)
17 
18 //! \brief Linear Congruential Generator (LCG)
19 //! \details Originally propsed by William S. England.
20 //! \warning LC_RNG is suitable for simulations, where uniformaly distrubuted numbers are
21 //! required quickly. It should not be used for cryptographic purposes.
23 {
24 public:
25  //! \brief Construct a Linear Congruential Generator (LCG)
26  //! \param init_seed the initial value for the generator
27  LC_RNG(word32 init_seed)
28  : seed(init_seed) {}
29 
30  void GenerateBlock(byte *output, size_t size);
31 
32  word32 GetSeed() {return seed;}
33 
34 private:
35  word32 seed;
36 
37  static const word32 m;
38  static const word32 q;
39  static const word16 a;
40  static const word16 r;
41 };
42 
43 //! \class X917RNG
44 //! \brief ANSI X9.17 RNG
45 //! \details X917RNG is from ANSI X9.17 Appendix C.
46 //! \sa AutoSeededX917RNG, DefaultAutoSeededRNG
47 class CRYPTOPP_DLL X917RNG : public RandomNumberGenerator, public NotCopyable
48 {
49 public:
50  //! \brief Construct a X917RNG
51  //! \param cipher the block cipher to use for the generator
52  //! \param seed a byte buffer to use as a seed
53  //! \param deterministicTimeVector additional entropy
54  //! \details <tt>cipher</tt> will be deleted by the destructor. <tt>seed</tt> must be at least
55  //! BlockSize() in length. <tt>deterministicTimeVector = 0</tt> means obtain time vector
56  //! from the system.
57  //! \details When constructing an AutoSeededX917RNG, the generator must be keyed or an
58  //! access violation will occur because the time vector is encrypted using the block cipher.
59  //! To key the generator during constructions, perform the following:
60  //! <pre>
61  //! SecByteBlock key(AES::DEFAULT_KEYLENGTH), seed(AES::BLOCKSIZE);
62  //! OS_GenerateRandomBlock(false, key, key.size());
63  //! OS_GenerateRandomBlock(false, seed, seed.size());
64  //! X917RNG prng(new AES::Encryption(key, AES::DEFAULT_KEYLENGTH), seed, NULL);
65  //! </pre>
66  //! \sa AutoSeededX917RNG
67  X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = 0);
68 
69  void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
70 
71 private:
73  const unsigned int S; // blocksize of cipher
74  SecByteBlock dtbuf; // buffer for enciphered timestamp
75  SecByteBlock randseed, m_lastBlock, m_deterministicTimeVector;
76 };
77 
78 //! \class MaurerRandomnessTest
79 //! \brief Maurer's Universal Statistical Test for Random Bit Generators
80 //! \details This class implements Maurer's Universal Statistical Test for
81 //! Random Bit Generators. It is intended for measuring the randomness of
82 //! *PHYSICAL* RNGs.
83 //! \details For more details see Maurer's paper in Journal of Cryptology, 1992.
84 class MaurerRandomnessTest : public Bufferless<Sink>
85 {
86 public:
88 
89  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
90 
91  //! \brief Provides the number of bytes of input is needed by the test
92  //! \returns how many more bytes of input is needed by the test
93  // BytesNeeded() returns how many more bytes of input is needed by the test
94  // GetTestValue() should not be called before BytesNeeded()==0
95  unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;}
96 
97  // returns a number between 0.0 and 1.0, describing the quality of the
98  // random numbers entered
99  double GetTestValue() const;
100 
101 private:
102  enum {L=8, V=256, Q=2000, K=2000};
103  double sum;
104  unsigned int n;
105  unsigned int tab[V];
106 };
107 
108 NAMESPACE_END
109 
110 #endif
ANSI X9.17 RNG.
Definition: rng.h:47
Linear Congruential Generator (LCG)
Definition: rng.h:22
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Interface for random number generators.
Definition: cryptlib.h:1085
SecByteBlock is a SecBlock<byte> typedef.
Definition: secblock.h:722
Interface for buffered transformations.
Definition: cryptlib.h:1248
Maurer&#39;s Universal Statistical Test for Random Bit Generators.
Definition: rng.h:84
LC_RNG(word32 init_seed)
Construct a Linear Congruential Generator (LCG)
Definition: rng.h:27
Implementation of BufferedTransformation&#39;s attachment interface in cryptlib.h.
unsigned int BytesNeeded() const
Provides the number of bytes of input is needed by the test.
Definition: rng.h:95
Crypto++ library namespace.
Interface for the data processing part of block ciphers.
Definition: cryptlib.h:663
Ensures an object is not copyable.
Definition: misc.h:187
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
Definition: cryptlib.cpp:347
Base class for bufferless filters.
Definition: simple.h:67