aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2007-10-19 13:18:40 +0000
committerlloyd <[email protected]>2007-10-19 13:18:40 +0000
commitc9761e6b9773d7eeea3f12c888a782d3bbe33446 (patch)
tree5eb4329fb8ada18c60770c27a24f791beb389bdf
parent403369f7d8c41fad22a694a6e7a9a42f8292d9c7 (diff)
New implementation of fast polling in es_unix. Instead of executing
programs, the fast poll will just call a handful of simple Unix/POSIX functions like getpid, getuid, getrusage, etc. Identifying further useful sources would probably be helpful.
-rw-r--r--modules/es_unix/es_unix.cpp80
-rw-r--r--modules/es_unix/es_unix.h5
2 files changed, 44 insertions, 41 deletions
diff --git a/modules/es_unix/es_unix.cpp b/modules/es_unix/es_unix.cpp
index 243ba9164..8c4b8984b 100644
--- a/modules/es_unix/es_unix.cpp
+++ b/modules/es_unix/es_unix.cpp
@@ -8,6 +8,9 @@
#include <botan/parsing.h>
#include <botan/config.h>
#include <algorithm>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <unistd.h>
namespace Botan {
@@ -43,42 +46,35 @@ void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count)
*************************************************/
void Unix_EntropySource::do_fast_poll()
{
- gather(2*1024);
- }
+ add_bytes(getpid());
+ add_bytes(getppid());
-/*************************************************
-* Unix Slow Poll *
-*************************************************/
-void Unix_EntropySource::do_slow_poll()
- {
- gather(16*1024);
- }
+ add_bytes(getuid());
+ add_bytes(getgid());
+ add_bytes(geteuid());
+ add_bytes(getegid());
-/*************************************************
-* Gather Entropy From Several Unix_Programs *
-*************************************************/
-void Unix_EntropySource::gather(u32bit target_amount)
- {
- const u32bit MINIMAL_WORKING = 32;
+ add_bytes(getpgrp());
+ add_bytes(getsid(0));
- u32bit got = 0;
- for(u32bit j = 0; j != sources.size(); j++)
- {
- add_timestamp();
+ struct rusage usage;
- got += gather_from(sources[j]);
- sources[j].working = (got >= MINIMAL_WORKING) ? true : false;
+ clear_mem(&usage, 1);
+ getrusage(RUSAGE_SELF, &usage);
+ add_bytes(&usage, sizeof(usage));
- if(got >= target_amount)
- break;
- }
+ getrusage(RUSAGE_CHILDREN, &usage);
+ add_bytes(&usage, sizeof(usage));
}
/*************************************************
-* Gather entropy from a Unix program *
+* Unix Slow Poll *
*************************************************/
-u32bit Unix_EntropySource::gather_from(const Unix_Program& prog)
+void Unix_EntropySource::do_slow_poll()
{
+ const u32bit TRY_TO_GET = 16 * 1024;
+ const u32bit MINIMAL_WORKING = 32;
+
const std::string BASE_PATH = "/bin:/sbin:/usr/bin:/usr/sbin";
const std::string EXTRA_PATH = global_config().option("rng/unix_path");
@@ -86,21 +82,29 @@ u32bit Unix_EntropySource::gather_from(const Unix_Program& prog)
if(EXTRA_PATH != "")
PATH += ':' + EXTRA_PATH;
- DataSource_Command pipe(prog.name_and_args, PATH);
- if(pipe.end_of_data())
- return 0;
-
u32bit got = 0;
- SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
-
- while(!pipe.end_of_data())
+ for(u32bit j = 0; j != sources.size(); j++)
{
- u32bit this_loop = pipe.read(buffer, buffer.size());
- add_bytes(buffer, this_loop);
- got += this_loop;
- }
+ add_timestamp();
+
+ DataSource_Command pipe(sources[j].name_and_args, PATH);
+ SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
+
+ uint32_t got_from_src = 0;
- return got;
+ while(!pipe.end_of_data())
+ {
+ u32bit this_loop = pipe.read(buffer, buffer.size());
+ add_bytes(buffer, this_loop);
+ got_from_src += this_loop;
+ }
+
+ sources[j].working = (got_from_src >= MINIMAL_WORKING) ? true : false;
+ got += got_from_src;
+
+ if(got >= TRY_TO_GET)
+ break;
+ }
}
}
diff --git a/modules/es_unix/es_unix.h b/modules/es_unix/es_unix.h
index 167bafc89..149e6f395 100644
--- a/modules/es_unix/es_unix.h
+++ b/modules/es_unix/es_unix.h
@@ -21,11 +21,10 @@ class Unix_EntropySource : public Buffered_EntropySource
void add_sources(const Unix_Program[], u32bit);
Unix_EntropySource();
private:
+ static void add_default_sources(std::vector<Unix_Program>&);
+
void do_fast_poll();
void do_slow_poll();
- void gather(u32bit);
- u32bit gather_from(const Unix_Program&);
- static void add_default_sources(std::vector<Unix_Program>&);
std::vector<Unix_Program> sources;
};