2013-11-30 12:02:56 -05:00
|
|
|
#include <qpdf/InsecureRandomDataProvider.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 17:14:10 -04:00
|
|
|
#include <qpdf/qpdf-config.h>
|
2023-05-20 12:22:32 +01:00
|
|
|
#include <cstdlib>
|
2013-11-30 12:02:56 -05:00
|
|
|
|
|
|
|
InsecureRandomDataProvider::InsecureRandomDataProvider() :
|
|
|
|
seeded_random(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
|
|
|
|
{
|
2022-04-02 17:14:10 -04:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2013-11-30 12:02:56 -05:00
|
|
|
data[i] = static_cast<unsigned char>((this->random() & 0xff0) >> 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
InsecureRandomDataProvider::random()
|
|
|
|
{
|
2022-04-02 17:14:10 -04:00
|
|
|
if (!this->seeded_random) {
|
2023-05-27 18:19:52 +01:00
|
|
|
// Seed the random number generator with something simple, but just to be interesting, don't
|
|
|
|
// use the unmodified current time. It would be better if this were a more secure seed.
|
2023-05-21 13:35:09 -04:00
|
|
|
auto seed = static_cast<unsigned int>(QUtil::get_current_time() ^ 0xcccc);
|
2020-04-06 09:49:02 -04:00
|
|
|
#ifdef HAVE_RANDOM
|
|
|
|
::srandom(seed);
|
|
|
|
#else
|
|
|
|
srand(seed);
|
|
|
|
#endif
|
2022-02-08 09:18:08 -05:00
|
|
|
this->seeded_random = true;
|
2013-11-30 12:02:56 -05:00
|
|
|
}
|
|
|
|
|
2022-04-02 17:14:10 -04:00
|
|
|
#ifdef HAVE_RANDOM
|
2013-11-30 12:02:56 -05:00
|
|
|
return ::random();
|
2022-04-02 17:14:10 -04:00
|
|
|
#else
|
2013-11-30 12:02:56 -05:00
|
|
|
return rand();
|
2022-04-02 17:14:10 -04:00
|
|
|
#endif
|
2013-11-30 12:02:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
RandomDataProvider*
|
|
|
|
InsecureRandomDataProvider::getInstance()
|
|
|
|
{
|
|
|
|
static InsecureRandomDataProvider instance;
|
|
|
|
return &instance;
|
|
|
|
}
|