rng.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // rng.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rng.h
  3. /// \brief Miscellaneous classes for RNGs
  4. /// \details This file contains miscellaneous classes for RNGs, including LC_RNG(),
  5. /// X917RNG() and MaurerRandomnessTest()
  6. /// \sa osrng.h, randpool.h
  7. #ifndef CRYPTOPP_RNG_H
  8. #define CRYPTOPP_RNG_H
  9. #include "cryptlib.h"
  10. #include "filters.h"
  11. #include "smartptr.h"
  12. NAMESPACE_BEGIN(CryptoPP)
  13. /// \brief Linear Congruential Generator (LCG)
  14. /// \details Originally propsed by William S. England.
  15. /// \warning LC_RNG is suitable for simulations, where uniformaly distributed numbers are
  16. /// required quickly. It should not be used for cryptographic purposes.
  17. class LC_RNG : public RandomNumberGenerator
  18. {
  19. public:
  20. /// \brief Construct a Linear Congruential Generator (LCG)
  21. /// \param init_seed the initial value for the generator
  22. LC_RNG(word32 init_seed)
  23. : seed(init_seed) {}
  24. void GenerateBlock(byte *output, size_t size);
  25. word32 GetSeed() {return seed;}
  26. private:
  27. word32 seed;
  28. static const word32 m;
  29. static const word32 q;
  30. static const word16 a;
  31. static const word16 r;
  32. };
  33. /// \brief ANSI X9.17 RNG
  34. /// \details X917RNG is from ANSI X9.17 Appendix C, and it uses a 64-bit block cipher, like TripleDES.
  35. /// If you use a 128-bit block cipher, like AES, then you are effectively using an ANSI X9.31 generator.
  36. /// \details You should reseed the generator after a fork() to avoid multiple generators
  37. /// with the same internal state.
  38. /// \sa AutoSeededX917RNG, DefaultAutoSeededRNG
  39. class CRYPTOPP_DLL X917RNG : public RandomNumberGenerator, public NotCopyable
  40. {
  41. public:
  42. /// \brief Construct a X917RNG
  43. /// \param cipher the block cipher to use for the generator
  44. /// \param seed a byte buffer to use as a seed
  45. /// \param deterministicTimeVector additional entropy
  46. /// \details <tt>cipher</tt> will be deleted by the destructor. <tt>seed</tt> must be at least
  47. /// BlockSize() in length. <tt>deterministicTimeVector = 0</tt> means obtain time vector
  48. /// from the system.
  49. /// \details When constructing a X917RNG, the generator must be keyed or an access
  50. /// violation will occur because the time vector is encrypted using the block cipher.
  51. /// To key the generator during constructions, perform the following:
  52. /// <pre>
  53. /// SecByteBlock key(AES::DEFAULT_KEYLENGTH), seed(AES::BLOCKSIZE);
  54. /// OS_GenerateRandomBlock(false, key, key.size());
  55. /// OS_GenerateRandomBlock(false, seed, seed.size());
  56. /// X917RNG prng(new AES::Encryption(key, AES::DEFAULT_KEYLENGTH), seed, NULLPTR);</pre>
  57. /// \sa AutoSeededX917RNG
  58. X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = NULLPTR);
  59. void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
  60. private:
  61. member_ptr<BlockTransformation> m_cipher;
  62. const unsigned int m_size; // S, blocksize of cipher
  63. SecByteBlock m_datetime; // DT, buffer for enciphered timestamp
  64. SecByteBlock m_randseed, m_lastBlock, m_deterministicTimeVector;
  65. };
  66. /// \brief Maurer's Universal Statistical Test for Random Bit Generators
  67. /// \details This class implements Maurer's Universal Statistical Test for
  68. /// Random Bit Generators. It is intended for measuring the randomness of
  69. /// *PHYSICAL* RNGs.
  70. /// \details For more details see Maurer's paper in Journal of Cryptology, 1992.
  71. class MaurerRandomnessTest : public Bufferless<Sink>
  72. {
  73. public:
  74. /// \brief Construct a MaurerRandomnessTest
  75. MaurerRandomnessTest();
  76. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  77. /// \brief Provides the number of bytes of input is needed by the test
  78. /// \return how many more bytes of input is needed by the test
  79. // BytesNeeded() returns how many more bytes of input is needed by the test
  80. // GetTestValue() should not be called before BytesNeeded()==0
  81. unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;}
  82. // returns a number between 0.0 and 1.0, describing the quality of the
  83. // random numbers entered
  84. double GetTestValue() const;
  85. private:
  86. enum {L=8, V=256, Q=2000, K=2000};
  87. double sum;
  88. unsigned int n;
  89. unsigned int tab[V];
  90. };
  91. NAMESPACE_END
  92. #endif