|
||||
File indexing completed on 2025-01-18 09:55:08
0001 // rng.h - originally written and placed in the public domain by Wei Dai 0002 0003 /// \file rng.h 0004 /// \brief Miscellaneous classes for RNGs 0005 /// \details This file contains miscellaneous classes for RNGs, including LC_RNG(), 0006 /// X917RNG() and MaurerRandomnessTest() 0007 /// \sa osrng.h, randpool.h 0008 0009 #ifndef CRYPTOPP_RNG_H 0010 #define CRYPTOPP_RNG_H 0011 0012 #include "cryptlib.h" 0013 #include "filters.h" 0014 #include "smartptr.h" 0015 0016 NAMESPACE_BEGIN(CryptoPP) 0017 0018 /// \brief Linear Congruential Generator (LCG) 0019 /// \details Originally propsed by William S. England. 0020 /// \warning LC_RNG is suitable for simulations, where uniformaly distributed numbers are 0021 /// required quickly. It should not be used for cryptographic purposes. 0022 class LC_RNG : public RandomNumberGenerator 0023 { 0024 public: 0025 /// \brief Construct a Linear Congruential Generator (LCG) 0026 /// \param init_seed the initial value for the generator 0027 LC_RNG(word32 init_seed) 0028 : seed(init_seed) {} 0029 0030 void GenerateBlock(byte *output, size_t size); 0031 0032 word32 GetSeed() {return seed;} 0033 0034 private: 0035 word32 seed; 0036 0037 static const word32 m; 0038 static const word32 q; 0039 static const word16 a; 0040 static const word16 r; 0041 }; 0042 0043 /// \brief ANSI X9.17 RNG 0044 /// \details X917RNG is from ANSI X9.17 Appendix C, and it uses a 64-bit block cipher, like TripleDES. 0045 /// If you use a 128-bit block cipher, like AES, then you are effectively using an ANSI X9.31 generator. 0046 /// \details You should reseed the generator after a fork() to avoid multiple generators 0047 /// with the same internal state. 0048 /// \sa AutoSeededX917RNG, DefaultAutoSeededRNG 0049 class CRYPTOPP_DLL X917RNG : public RandomNumberGenerator, public NotCopyable 0050 { 0051 public: 0052 /// \brief Construct a X917RNG 0053 /// \param cipher the block cipher to use for the generator 0054 /// \param seed a byte buffer to use as a seed 0055 /// \param deterministicTimeVector additional entropy 0056 /// \details <tt>cipher</tt> will be deleted by the destructor. <tt>seed</tt> must be at least 0057 /// BlockSize() in length. <tt>deterministicTimeVector = 0</tt> means obtain time vector 0058 /// from the system. 0059 /// \details When constructing a X917RNG, the generator must be keyed or an access 0060 /// violation will occur because the time vector is encrypted using the block cipher. 0061 /// To key the generator during constructions, perform the following: 0062 /// <pre> 0063 /// SecByteBlock key(AES::DEFAULT_KEYLENGTH), seed(AES::BLOCKSIZE); 0064 /// OS_GenerateRandomBlock(false, key, key.size()); 0065 /// OS_GenerateRandomBlock(false, seed, seed.size()); 0066 /// X917RNG prng(new AES::Encryption(key, AES::DEFAULT_KEYLENGTH), seed, NULLPTR);</pre> 0067 /// \sa AutoSeededX917RNG 0068 X917RNG(BlockTransformation *cipher, const byte *seed, const byte *deterministicTimeVector = NULLPTR); 0069 0070 void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size); 0071 0072 private: 0073 member_ptr<BlockTransformation> m_cipher; 0074 const unsigned int m_size; // S, blocksize of cipher 0075 SecByteBlock m_datetime; // DT, buffer for enciphered timestamp 0076 SecByteBlock m_randseed, m_lastBlock, m_deterministicTimeVector; 0077 }; 0078 0079 /// \brief Maurer's Universal Statistical Test for Random Bit Generators 0080 /// \details This class implements Maurer's Universal Statistical Test for 0081 /// Random Bit Generators. It is intended for measuring the randomness of 0082 /// *PHYSICAL* RNGs. 0083 /// \details For more details see Maurer's paper in Journal of Cryptology, 1992. 0084 class MaurerRandomnessTest : public Bufferless<Sink> 0085 { 0086 public: 0087 /// \brief Construct a MaurerRandomnessTest 0088 MaurerRandomnessTest(); 0089 0090 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking); 0091 0092 /// \brief Provides the number of bytes of input is needed by the test 0093 /// \return how many more bytes of input is needed by the test 0094 // BytesNeeded() returns how many more bytes of input is needed by the test 0095 // GetTestValue() should not be called before BytesNeeded()==0 0096 unsigned int BytesNeeded() const {return n >= (Q+K) ? 0 : Q+K-n;} 0097 0098 // returns a number between 0.0 and 1.0, describing the quality of the 0099 // random numbers entered 0100 double GetTestValue() const; 0101 0102 private: 0103 enum {L=8, V=256, Q=2000, K=2000}; 0104 double sum; 0105 unsigned int n; 0106 unsigned int tab[V]; 0107 }; 0108 0109 NAMESPACE_END 0110 0111 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |