diff --git a/randpool.cpp b/randpool.cpp index 1813da54..974760b7 100644 --- a/randpool.cpp +++ b/randpool.cpp @@ -88,7 +88,21 @@ OldRandomPool::OldRandomPool(unsigned int poolSize) void OldRandomPool::IncorporateEntropy(const byte *input, size_t length) { - OldRandomPool::Put(input, length); + size_t t; + while (length > (t = pool.size() - addPos)) + { + xorbuf(pool+addPos, input, t); + input += t; + length -= t; + Stir(); + } + + if (length) + { + xorbuf(pool+addPos, input, length); + addPos += length; + getPos = pool.size(); // Force stir on get + } } void OldRandomPool::Stir() @@ -106,36 +120,8 @@ void OldRandomPool::Stir() getPos = key.size(); } -size_t OldRandomPool::Put2(const byte *inString, size_t length, int messageEnd, bool blocking) +void OldRandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size) { - CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); - - size_t t; - while (length > (t = pool.size() - addPos)) - { - xorbuf(pool+addPos, inString, t); - inString += t; - length -= t; - Stir(); - } - - if (length) - { - xorbuf(pool+addPos, inString, length); - addPos += length; - getPos = pool.size(); // Force stir on get - } - - return 0; -} - -size_t OldRandomPool::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking) -{ - if (!blocking) - throw NotImplemented("OldRandomPool: nonblocking transfer is not implemented by this object"); - - lword size = transferBytes; - while (size > 0) { if (getPos == pool.size()) @@ -144,10 +130,7 @@ size_t OldRandomPool::TransferTo2(BufferedTransformation &target, lword &transfe target.ChannelPut(channel, pool+getPos, t); size -= t; getPos += t; - } - - return 0; -} + }} byte OldRandomPool::GenerateByte() { @@ -160,7 +143,7 @@ byte OldRandomPool::GenerateByte() void OldRandomPool::GenerateBlock(byte *outString, size_t size) { ArraySink sink(outString, size); - TransferTo(sink, size); + GenerateIntoBufferedTransformation(sink, DEFAULT_CHANNEL, size); } NAMESPACE_END diff --git a/randpool.h b/randpool.h index 11029f58..20f356c7 100644 --- a/randpool.h +++ b/randpool.h @@ -72,8 +72,7 @@ private: //! HKDF. //! \sa RandomPool, AutoSeededRandomPool, HKDF, P1363_KDF2, PKCS12_PBKDF, PKCS5_PBKDF2_HMAC //! \since Crypto++ 6.0 (PGP 2.6.x style) -class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator, - public Bufferless +class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator { public: //! \brief Construct an OldRandomPool @@ -84,26 +83,11 @@ public: // RandomNumberGenerator interface (Crypto++ 5.5 and above) bool CanIncorporateEntropy() const {return true;} void IncorporateEntropy(const byte *input, size_t length); - - // BufferedTransformation interface (Crypto++ 5.4 and below) - size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); - - bool AnyRetrievable() const {return true;} - lword MaxRetrievable() const {return ULONG_MAX;} - - size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true); - size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const - { - CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); - CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); - throw NotImplemented("OldRandomPool: CopyRangeTo2() is not supported by this store"); - } + void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size); byte GenerateByte(); void GenerateBlock(byte *output, size_t size); - void IsolatedInitialize(const NameValuePairs ¶meters) {CRYPTOPP_UNUSED(parameters);} - protected: void Stir(); diff --git a/validat1.cpp b/validat1.cpp index cc72530a..3943aace 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -722,7 +722,7 @@ bool TestRandomPool() // with it in 2017. The missing functionality was a barrier to upgrades. std::cout << "\nTesting OldRandomPool generator...\n\n"; { - OldRandomPool old1; + OldRandomPool old; static const unsigned int ENTROPY_SIZE = 32; // https://github.com/weidai11/cryptopp/issues/452 @@ -734,9 +734,9 @@ bool TestRandomPool() }; SecByteBlock seed(0x00, 384); - old1.Put(seed, seed.size()); + old.IncorporateEntropy(seed, seed.size()); - old1.GenerateBlock(result, sizeof(result)); + old.GenerateBlock(result, sizeof(result)); fail = (0 != ::memcmp(result, expected, sizeof(expected))); pass &= !fail; @@ -746,20 +746,6 @@ bool TestRandomPool() std::cout << "passed:"; std::cout << " Expected sequence from PGP-style RandomPool (circa 2007)\n"; - OldRandomPool old2; - old2.IncorporateEntropy(seed, seed.size()); - - ArraySink sink(result, sizeof(result)); - old2.GenerateIntoBufferedTransformation(sink, DEFAULT_CHANNEL, sizeof(result)); - fail = (0 != ::memcmp(result, expected, sizeof(expected))); - - pass &= !fail; - if (fail) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " Expected sequence from PGP-style RandomPool new interface (circa 2007)\n"; - OldRandomPool prng; MeterFilter meter(new Redirector(TheBitBucket())); RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));