From 5e3bad2f86665b35155095b91a2d672fc7335870 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 30 Nov 2013 12:02:56 -0500 Subject: Refactor random data generation Add new RandomDataProvider object and implement existing random number generation in terms of that. This enables end users to supply their own random data providers. --- include/qpdf/QUtil.hh | 20 ++++++++++++++++++++ include/qpdf/RandomDataProvider.hh | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 include/qpdf/RandomDataProvider.hh (limited to 'include') diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index cbdc065c..f61fa844 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -15,6 +15,8 @@ #include #include +class RandomDataProvider; + namespace QUtil { // This is a collection of useful utility functions that don't @@ -123,8 +125,26 @@ namespace QUtil QPDF_DLL void srandom(unsigned int seed); + // Initialize a buffer with random bytes. By default, qpdf tries + // to use a secure random number source. It can be configured at + // compile time to use an insecure random number source (from + // stdlib). You can also call setRandomDataProvider with a + // RandomDataProvider, in which case this method will get its + // random bytes from that. + QPDF_DLL void initializeWithRandomBytes(unsigned char* data, size_t len); + + // Supply a random data provider. If not supplied, depending on + // compile time options, qpdf will either use the operating + // system's secure random number source or an insecure random + // source from stdlib. The caller is responsible for managing the + // memory for the RandomDataProvider. This method modifies a + // static variable. If you are providing your own random data + // provider, you should call this at the beginning of your program + // before creating any QPDF objects. + QPDF_DLL + void setRandomDataProvider(RandomDataProvider*); }; #endif // __QUTIL_HH__ diff --git a/include/qpdf/RandomDataProvider.hh b/include/qpdf/RandomDataProvider.hh new file mode 100644 index 00000000..68c9cb6e --- /dev/null +++ b/include/qpdf/RandomDataProvider.hh @@ -0,0 +1,32 @@ +/* Copyright (c) 2005-2013 Jay Berkenbilt + * + * This file is part of qpdf. This software may be distributed under + * the terms of version 2 of the Artistic License which may be found + * in the source distribution. It is provided "as is" without express + * or implied warranty. + */ + +#ifndef __RANDOMDATAPROVIDER_HH__ +#define __RANDOMDATAPROVIDER_HH__ + +#include // for size_t + +class RandomDataProvider +{ + public: + virtual ~RandomDataProvider() + { + } + virtual void provideRandomData(unsigned char* data, size_t len) = 0; + + protected: + RandomDataProvider() + { + } + + private: + RandomDataProvider(RandomDataProvider const&); + RandomDataProvider& operator=(RandomDataProvider const&); +}; + +#endif // __RANDOMDATAPROVIDER_HH__ -- cgit v1.2.3-54-g00ecf