diff options
author | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
commit | 9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch) | |
tree | fe5e8ae9813b853549558b59833022e87e83981b /src/entropy/dev_random | |
parent | 9822a701516396b7de4e41339faecd48ff8dc8ff (diff) |
Move all modules into src/ directory
Diffstat (limited to 'src/entropy/dev_random')
-rw-r--r-- | src/entropy/dev_random/es_dev.cpp | 112 | ||||
-rw-r--r-- | src/entropy/dev_random/es_dev.h | 28 | ||||
-rw-r--r-- | src/entropy/dev_random/modinfo.txt | 27 |
3 files changed, 167 insertions, 0 deletions
diff --git a/src/entropy/dev_random/es_dev.cpp b/src/entropy/dev_random/es_dev.cpp new file mode 100644 index 000000000..310620716 --- /dev/null +++ b/src/entropy/dev_random/es_dev.cpp @@ -0,0 +1,112 @@ +/************************************************* +* Device EntropySource Source File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#include <botan/es_dev.h> +#include <sys/select.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/fcntl.h> +#include <unistd.h> + +namespace Botan { + +namespace { + +/************************************************* +* A class handling reading from a device * +*************************************************/ +class 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; + }; + +/************************************************* +* Read from a device file * +*************************************************/ +u32bit Device_Reader::get(byte out[], u32bit length) + { + if(fd < 0) + return 0; + + if(fd >= FD_SETSIZE) + return 0; + + const u32bit READ_WAIT_MS = 10; + + fd_set read_set; + FD_ZERO(&read_set); + FD_SET(fd, &read_set); + + struct ::timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = READ_WAIT_MS * 1000; + + if(::select(fd + 1, &read_set, 0, 0, &timeout) < 0) + return 0; + + if(!(FD_ISSET(fd, &read_set))) + return 0; + + const ssize_t got = ::read(fd, out, length); + if(got <= 0) + return 0; + + const u32bit ret = static_cast<u32bit>(got); + + if(ret > length) + return 0; + + return ret; + } + +/************************************************* +* Attempt to open a device * +*************************************************/ +int Device_Reader::open(const std::string& pathname) + { +#ifndef O_NONBLOCK + #define O_NONBLOCK 0 +#endif + +#ifndef O_NOCTTY + #define O_NOCTTY 0 +#endif + + const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; + return ::open(pathname.c_str(), flags); + } + +} + +/************************************************* +* Gather entropy from a RNG device * +*************************************************/ +u32bit Device_EntropySource::slow_poll(byte output[], u32bit length) + { + u32bit read = 0; + + for(size_t j = 0; j != fsnames.size(); ++j) + { + Device_Reader reader(Device_Reader::open(fsnames[j])); + + read += reader.get(output + read, length - read); + + if(read == length) + break; + } + + return read; + } + +} diff --git a/src/entropy/dev_random/es_dev.h b/src/entropy/dev_random/es_dev.h new file mode 100644 index 000000000..21d28b9bb --- /dev/null +++ b/src/entropy/dev_random/es_dev.h @@ -0,0 +1,28 @@ +/************************************************* +* Device EntropySource Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_ENTROPY_SRC_DEVICE_H__ +#define BOTAN_ENTROPY_SRC_DEVICE_H__ + +#include <botan/rng.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Device Based Entropy Source * +*************************************************/ +class Device_EntropySource : public EntropySource + { + public: + Device_EntropySource(const std::vector<std::string>& fs) : fsnames(fs) {} + u32bit slow_poll(byte[], u32bit); + private: + std::vector<std::string> fsnames; + }; + +} + +#endif diff --git a/src/entropy/dev_random/modinfo.txt b/src/entropy/dev_random/modinfo.txt new file mode 100644 index 000000000..6622886af --- /dev/null +++ b/src/entropy/dev_random/modinfo.txt @@ -0,0 +1,27 @@ +realname "RNG Device Reader" + +define ENTROPY_SRC_DEVICE + +load_on auto +modset unix + +<add> +es_dev.h +es_dev.cpp +</add> + +<os> +aix +beos +cygwin +darwin +freebsd +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> |