Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.8 KiB

  1. // rng.h - 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 distrubuted 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. //! \class X917RNG
  34. //! \brief ANSI X9.17 RNG
  35. //! \details X917RNG is from ANSI X9.17 Appendix C.
  36. //! \sa AutoSeededX917RNG, DefaultAutoSeededRNG
  37. class CRYPTOPP_DLL X917RNG : public RandomNumberGenerator, public NotCopyable
  38. {
  39. public:
  40. //! \brief Construct a X917RNG
  41. //! \param cipher the block cipher to use for the generator
  42. //! \param seed a byte buffer to use as a seed
  43. //! \param deterministicTimeVector additional entropy
  44. //! \details <tt>cipher</tt> will be deleted by the destructor. <tt>seed</tt> must be at least
  45. //! BlockSize() in length. <tt>deterministicTimeVector = 0</tt> means obtain time vector
  46. //! from the system.
  47. //! \details When constructing an AutoSeededX917RNG, the generator must be keyed or an
  48. //! access violation will occur because the time vector is encrypted using the block cipher.
  49. //! To key the generator during constructions, perform the following:
  50. //! <pre>
  51. //! SecByteBlock key(AES::DEFAULT_KEYLENGTH), seed(AES::BLOCKSIZE);
  52. //! OS_GenerateRandomBlock(false, key, key.size());
  53. //! OS_GenerateRandomBlock(false, seed, seed.size());
  54. //! X917RNG prng(new AES::Encryption(key, AES::DEFAULT_KEYLENGTH), seed, NULL);
  55. //! </pre>
  56. //! \sa AutoSeededX917RNG
  57. X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = 0);
  58. void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
  59. private:
  60. member_ptr<BlockTransformation> cipher;
  61. const unsigned int S; // blocksize of cipher
  62. SecByteBlock dtbuf; // buffer for enciphered timestamp
  63. SecByteBlock randseed, m_lastBlock, m_deterministicTimeVector;
  64. };
  65. //! \class MaurerRandomnessTest
  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. MaurerRandomnessTest();
  75. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  76. //! \brief Provides the number of bytes of input is needed by the test
  77. //! \returns how many more bytes of input is needed by the test
  78. // BytesNeeded() returns how many more bytes of input is needed by the test
  79. // GetTestValue() should not be called before BytesNeeded()==0
  80. unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;}
  81. // returns a number between 0.0 and 1.0, describing the quality of the
  82. // random numbers entered
  83. double GetTestValue() const;
  84. private:
  85. enum {L=8, V=256, Q=2000, K=2000};
  86. double sum;
  87. unsigned int n;
  88. unsigned int tab[V];
  89. };
  90. NAMESPACE_END
  91. #endif