aboutsummaryrefslogtreecommitdiffstats
path: root/modules/es_ftw
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-05-18 18:33:19 +0000
committerlloyd <[email protected]>2006-05-18 18:33:19 +0000
commita2c99d3270eb73ef2db5704fc54356c6b75096f8 (patch)
treead3d6c4fcc8dd0f403f8105598943616246fe172 /modules/es_ftw
Initial checkin1.5.6
Diffstat (limited to 'modules/es_ftw')
-rw-r--r--modules/es_ftw/es_ftw.cpp110
-rw-r--r--modules/es_ftw/es_ftw.h31
-rw-r--r--modules/es_ftw/modinfo.txt24
3 files changed, 165 insertions, 0 deletions
diff --git a/modules/es_ftw/es_ftw.cpp b/modules/es_ftw/es_ftw.cpp
new file mode 100644
index 000000000..08a03c0e4
--- /dev/null
+++ b/modules/es_ftw/es_ftw.cpp
@@ -0,0 +1,110 @@
+/*************************************************
+* FTW EntropySource Source File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/es_ftw.h>
+#include <botan/util.h>
+#include <fstream>
+#include <cstring>
+#include <vector>
+
+#ifndef _POSIX_C_SOURCE
+ #define _POSIX_C_SOURCE 199309
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <fcntl.h>
+
+namespace Botan {
+
+/*************************************************
+* FTW_EntropySource Constructor *
+*************************************************/
+FTW_EntropySource::FTW_EntropySource(const std::string& p) : path(p)
+ {
+ }
+
+/*************************************************
+* FTW Fast Poll *
+*************************************************/
+void FTW_EntropySource::do_fast_poll()
+ {
+ files_read = 0;
+ max_read = 32;
+ gather_from_dir(path);
+ }
+
+/*************************************************
+* FTW Slow Poll *
+*************************************************/
+void FTW_EntropySource::do_slow_poll()
+ {
+ files_read = 0;
+ max_read = 256;
+ gather_from_dir(path);
+ }
+
+/*************************************************
+* Gather Entropy From Directory Tree *
+*************************************************/
+void FTW_EntropySource::gather_from_dir(const std::string& dirname)
+ {
+ if(dirname == "" || files_read >= max_read)
+ return;
+
+ DIR* dir = opendir(dirname.c_str());
+ if(dir == 0)
+ return;
+
+ std::vector<std::string> subdirs;
+
+ dirent* entry = readdir(dir);
+ while(entry && (files_read < max_read))
+ {
+ if((std::strcmp(entry->d_name, ".") == 0) ||
+ (std::strcmp(entry->d_name, "..") == 0))
+ { entry = readdir(dir); continue; }
+
+ const std::string filename = dirname + '/' + entry->d_name;
+
+ struct stat stat_buf;
+ if(lstat(filename.c_str(), &stat_buf) == -1)
+ { entry = readdir(dir); continue; }
+
+ if(S_ISREG(stat_buf.st_mode))
+ gather_from_file(filename);
+ else if(S_ISDIR(stat_buf.st_mode))
+ subdirs.push_back(filename);
+ entry = readdir(dir);
+ }
+ closedir(dir);
+
+ for(u32bit j = 0; j != subdirs.size(); j++)
+ gather_from_dir(subdirs[j]);
+ }
+
+/*************************************************
+* Gather Entropy From A File *
+*************************************************/
+void FTW_EntropySource::gather_from_file(const std::string& filename)
+ {
+ int fd = ::open(filename.c_str(), O_RDONLY | O_NOCTTY);
+ if(fd == -1)
+ return;
+
+ SecureVector<byte> read_buf(1024);
+ ssize_t got = ::read(fd, (byte*)read_buf.begin(), read_buf.size());
+ close(fd);
+
+ if(got > 0)
+ {
+ add_bytes(read_buf, got);
+ files_read++;
+ }
+ }
+
+}
diff --git a/modules/es_ftw/es_ftw.h b/modules/es_ftw/es_ftw.h
new file mode 100644
index 000000000..7b450798b
--- /dev/null
+++ b/modules/es_ftw/es_ftw.h
@@ -0,0 +1,31 @@
+/*************************************************
+* File Tree Walking EntropySource Header File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#ifndef BOTAN_EXT_ENTROPY_SRC_FTW_H__
+#define BOTAN_EXT_ENTROPY_SRC_FTW_H__
+
+#include <botan/buf_es.h>
+
+namespace Botan {
+
+/*************************************************
+* File Tree Walking Entropy Source *
+*************************************************/
+class FTW_EntropySource : public Buffered_EntropySource
+ {
+ public:
+ FTW_EntropySource(const std::string& = "/proc");
+ private:
+ void do_fast_poll();
+ void do_slow_poll();
+ void gather_from_dir(const std::string&);
+ void gather_from_file(const std::string&);
+ const std::string path;
+ u32bit files_read, max_read;
+ };
+
+}
+
+#endif
diff --git a/modules/es_ftw/modinfo.txt b/modules/es_ftw/modinfo.txt
new file mode 100644
index 000000000..d68ac7ac9
--- /dev/null
+++ b/modules/es_ftw/modinfo.txt
@@ -0,0 +1,24 @@
+realname "File Tree Walking Entropy Source"
+
+define ENTROPY_SRC_FTW
+
+add_file es_ftw.h
+add_file es_ftw.cpp
+
+<os>
+aix
+cygwin
+darwin
+freebsd
+hpux
+irix
+linux
+openbsd
+qnx
+solaris
+tru64
+
+# Doesn't build on 2.0.2/x86 due to libc/libstdc++ header issues; no
+# big deal since it has /dev/*random
+#netbsd
+</os>