Crypto++  8.0
Free C++ class library of cryptographic schemes
rng.h
Go to the documentation of this file.
1 // rng.h - originally 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 /// \brief ANSI X9.17 RNG
44 /// \details X917RNG is from ANSI X9.17 Appendix C, and it uses a 64-bit block cipher, like TripleDES.
45 /// If you use a 128-bit block cipher, like AES, then you are effectively using an ANSI X9.31 generator.
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 a X917RNG, the generator must be keyed or an access
58  /// 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, NULLPTR);</pre>
65  /// \sa AutoSeededX917RNG
66  X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = NULLPTR);
67 
68  void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
69 
70 private:
72  const unsigned int m_size; // S, blocksize of cipher
73  SecByteBlock m_datetime; // DT, buffer for enciphered timestamp
74  SecByteBlock m_randseed, m_lastBlock, m_deterministicTimeVector;
75 };
76 
77 /// \brief Maurer's Universal Statistical Test for Random Bit Generators
78 /// \details This class implements Maurer's Universal Statistical Test for
79 /// Random Bit Generators. It is intended for measuring the randomness of
80 /// *PHYSICAL* RNGs.
81 /// \details For more details see Maurer's paper in Journal of Cryptology, 1992.
82 class MaurerRandomnessTest : public Bufferless<Sink>
83 {
84 public:
85  /// \brief Construct a MaurerRandomnessTest
87 
88  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
89 
90  /// \brief Provides the number of bytes of input is needed by the test
91  /// \returns how many more bytes of input is needed by the test
92  // BytesNeeded() returns how many more bytes of input is needed by the test
93  // GetTestValue() should not be called before BytesNeeded()==0
94  unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;}
95 
96  // returns a number between 0.0 and 1.0, describing the quality of the
97  // random numbers entered
98  double GetTestValue() const;
99 
100 private:
101  enum {L=8, V=256, Q=2000, K=2000};
102  double sum;
103  unsigned int n;
104  unsigned int tab[V];
105 };
106 
107 NAMESPACE_END
108 
109 #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:1383
SecBlock<byte> typedef.
Definition: secblock.h:1058
Interface for buffered transformations.
Definition: cryptlib.h:1598
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: rng.cpp:143
Maurer&#39;s Universal Statistical Test for Random Bit Generators.
Definition: rng.h:82
LC_RNG(word32 init_seed)
Construct a Linear Congruential Generator (LCG)
Definition: rng.h:27
Implementation of BufferedTransformation&#39;s attachment interface.
unsigned int BytesNeeded() const
Provides the number of bytes of input is needed by the test.
Definition: rng.h:94
Crypto++ library namespace.
Interface for the data processing part of block ciphers.
Definition: cryptlib.h:827
Ensures an object is not copyable.
Definition: misc.h:200
MaurerRandomnessTest()
Construct a MaurerRandomnessTest.
Definition: rng.cpp:136
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
Definition: cryptlib.cpp:324
Base class for bufferless filters.
Definition: simple.h:98