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 by William S. England, do not use for cryptographic purposes
21 {
22 public:
23  LC_RNG(word32 init_seed)
24  : seed(init_seed) {}
25 
26  void GenerateBlock(byte *output, size_t size);
27 
28  word32 GetSeed() {return seed;}
29 
30 private:
31  word32 seed;
32 
33  static const word32 m;
34  static const word32 q;
35  static const word16 a;
36  static const word16 r;
37 };
38 
39 //! \class X917RNG
40 //! \brief ANSI X9.17 RNG
41 //! \details X917RNG is from ANSI X9.17 Appendix C.
42 //! \sa AutoSeededX917RNG, DefaultAutoSeededRNG
43 class CRYPTOPP_DLL X917RNG : public RandomNumberGenerator, public NotCopyable
44 {
45 public:
46  //! \brief Construct a X917RNG
47  //! \param cipher the block cipher to use for the generator
48  //! \param seed a byte buffer to use as a seed
49  //! \param deterministicTimeVector additional entropy
50  //! \details <tt>cipher</tt> will be deleted by the destructor. <tt>seed</tt> must be at least
51  //! BlockSize() in length. <tt>deterministicTimeVector = 0</tt> means obtain time vector
52  //! from the system.
53  //! \details When constructing an AutoSeededX917RNG, the generator must be keyed or an
54  //! access violation will occur because the time vector is encrypted using the block cipher.
55  //! To key the generator during constructions, perform the following:
56  //! <pre>
57  //! SecByteBlock key(AES::DEFAULT_KEYLENGTH), seed(AES::BLOCKSIZE);
58  //! OS_GenerateRandomBlock(false, key, key.size());
59  //! OS_GenerateRandomBlock(false, seed, seed.size());
60  //! X917RNG prng(new AES::Encryption(key, AES::DEFAULT_KEYLENGTH), seed, NULL);
61  //! </pre>
62  //! \sa AutoSeededX917RNG
63  X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = 0);
64 
65  void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
66 
67 private:
69  const unsigned int S; // blocksize of cipher
70  SecByteBlock dtbuf; // buffer for enciphered timestamp
71  SecByteBlock randseed, m_lastBlock, m_deterministicTimeVector;
72 };
73 
74 //! \class MaurerRandomnessTest
75 //! \brief Maurer's Universal Statistical Test for Random Bit Generators
76 //! \details This class implements Maurer's Universal Statistical Test for
77 //! Random Bit Generators. It is intended for measuring the randomness of
78 //! *PHYSICAL* RNGs.
79 //! \details For more details see Maurer's paper in Journal of Cryptology, 1992.
80 class MaurerRandomnessTest : public Bufferless<Sink>
81 {
82 public:
84 
85  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
86 
87  //! \brief Provides the number of bytes of input is needed by the test
88  //! \returns how many more bytes of input is needed by the test
89  // BytesNeeded() returns how many more bytes of input is needed by the test
90  // GetTestValue() should not be called before BytesNeeded()==0
91  unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;}
92 
93  // returns a number between 0.0 and 1.0, describing the quality of the
94  // random numbers entered
95  double GetTestValue() const;
96 
97 private:
98  enum {L=8, V=256, Q=2000, K=2000};
99  double sum;
100  unsigned int n;
101  unsigned int tab[V];
102 };
103 
104 NAMESPACE_END
105 
106 #endif
unsigned int BytesNeeded() const
Provides the number of bytes of input is needed by the test.
Definition: rng.h:91
ANSI X9.17 RNG.
Definition: rng.h:43
Linear Congruential Generator (LCG)
Definition: rng.h:20
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:719
Interface for buffered transformations.
Definition: cryptlib.h:1247
Maurer&#39;s Universal Statistical Test for Random Bit Generators.
Definition: rng.h:80
Implementation of BufferedTransformation&#39;s attachment interface in cryptlib.h.
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:184
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