diff options
author | lloyd <[email protected]> | 2008-11-07 15:52:35 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-07 15:52:35 +0000 |
commit | edd4a752d1127c8a72be70565dd03ba0d5b7a6a4 (patch) | |
tree | 817663c1718a311b434bbc82fa36901f077df7e7 /src/entropy | |
parent | 6a6cd361d4bf28cfcb6a32c2c55b226fef3f9b23 (diff) |
Cache device descriptors in Device_EntropySource
Diffstat (limited to 'src/entropy')
-rw-r--r-- | src/entropy/dev_random/es_dev.cpp | 56 | ||||
-rw-r--r-- | src/entropy/dev_random/es_dev.h | 23 |
2 files changed, 45 insertions, 34 deletions
diff --git a/src/entropy/dev_random/es_dev.cpp b/src/entropy/dev_random/es_dev.cpp index 04b3a90ee..b185eae75 100644 --- a/src/entropy/dev_random/es_dev.cpp +++ b/src/entropy/dev_random/es_dev.cpp @@ -12,29 +12,18 @@ namespace Botan { -namespace { - -/************************************************* -* A class handling reading from a device * -*************************************************/ -class Device_Reader +/** +Close the device, if open +*/ +Device_EntropySource::Device_Reader::~Device_Reader() { - public: - typedef int fd_type; - - Device_Reader(fd_type device_fd) : fd(device_fd) {} - ~Device_Reader() { if(fd > 0) { ::close(fd); } } - u32bit get(byte out[], u32bit length); - - static fd_type open(const std::string& pathname); - private: - fd_type fd; - }; + if(fd > 0) { ::close(fd); } + } -/************************************************* -* Read from a device file * -*************************************************/ -u32bit Device_Reader::get(byte out[], u32bit length) +/** +Read bytes from a device file +*/ +u32bit Device_EntropySource::Device_Reader::get(byte out[], u32bit length) { if(fd < 0) return 0; @@ -42,7 +31,7 @@ u32bit Device_Reader::get(byte out[], u32bit length) if(fd >= FD_SETSIZE) return 0; - const u32bit READ_WAIT_MS = 10; + const u32bit READ_WAIT_MS = 3; fd_set read_set; FD_ZERO(&read_set); @@ -70,10 +59,10 @@ u32bit Device_Reader::get(byte out[], u32bit length) return ret; } -/************************************************* -* Attempt to open a device * -*************************************************/ -int Device_Reader::open(const std::string& pathname) +/** +Attempt to open a device +*/ +int Device_EntropySource::Device_Reader::open(const std::string& pathname) { #ifndef O_NONBLOCK #define O_NONBLOCK 0 @@ -87,7 +76,14 @@ int Device_Reader::open(const std::string& pathname) return ::open(pathname.c_str(), flags); } -} +/** +Device_EntropySource constructor +*/ +Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnames) + { + for(u32bit i = 0; i != fsnames.size(); ++i) + devices.push_back(Device_Reader(Device_Reader::open(fsnames[i]))); + } /** * Gather entropy from a RNG device @@ -96,11 +92,9 @@ u32bit Device_EntropySource::slow_poll(byte output[], u32bit length) { u32bit read = 0; - for(size_t j = 0; j != fsnames.size(); ++j) + for(size_t i = 0; i != devices.size(); ++i) { - Device_Reader reader(Device_Reader::open(fsnames[j])); - - read += reader.get(output + read, length - read); + read += devices[i].get(output + read, length - read); if(read == length) break; diff --git a/src/entropy/dev_random/es_dev.h b/src/entropy/dev_random/es_dev.h index 2c9d93369..ff6a14f2c 100644 --- a/src/entropy/dev_random/es_dev.h +++ b/src/entropy/dev_random/es_dev.h @@ -20,13 +20,30 @@ class BOTAN_DLL Device_EntropySource : public EntropySource public: std::string name() const { return "RNG Device Reader"; } - Device_EntropySource(const std::vector<std::string>& fs) : - fsnames(fs) {} + Device_EntropySource(const std::vector<std::string>& fsnames); u32bit slow_poll(byte[], u32bit); u32bit fast_poll(byte[], u32bit); private: - std::vector<std::string> fsnames; + + /** + A class handling reading from a Unix character device + */ + class Device_Reader + { + public: + typedef int fd_type; + + Device_Reader(fd_type device_fd) : fd(device_fd) {} + ~Device_Reader(); + u32bit get(byte out[], u32bit length); + + static fd_type open(const std::string& pathname); + private: + fd_type fd; + }; + + std::vector<Device_Reader> devices; }; } |