aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-11-06 19:22:24 +0000
committerlloyd <[email protected]>2008-11-06 19:22:24 +0000
commitbf7f64fae0d14cc175732d7dc69f71f47edc7c41 (patch)
tree8842acf8197f733782fbbf3ebbbf3fb858151769 /src/utils
parentd50e41609c95945c714c445f6b400e02fe659b90 (diff)
Move Entropy_Estimator to utils/entropy.h (from anon namespace in HMAC_RNG
implementation), remove freestanding estimate_entropy function, change Randpool to use entropy estimator.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/entropy.cpp80
-rw-r--r--src/utils/entropy.h48
-rw-r--r--src/utils/info.txt2
-rw-r--r--src/utils/util.cpp32
-rw-r--r--src/utils/util.h1
5 files changed, 130 insertions, 33 deletions
diff --git a/src/utils/entropy.cpp b/src/utils/entropy.cpp
new file mode 100644
index 000000000..1562eb0d2
--- /dev/null
+++ b/src/utils/entropy.cpp
@@ -0,0 +1,80 @@
+/*************************************************
+* Entropy_Estimator Source File *
+* (C) 2008 Jack Lloyd *
+*************************************************/
+
+#include <botan/entropy.h>
+#include <botan/bit_ops.h>
+
+namespace Botan {
+
+/**
+Update the estimate
+*/
+void Entropy_Estimator::update(const byte buffer[], u32bit length,
+ u32bit upper_limit)
+ {
+ u32bit this_buf_estimate = 0;
+
+ /*
+ This is pretty naive
+ */
+ for(u32bit j = 0; j != length; ++j)
+ {
+ byte delta = last ^ buffer[j];
+ last = buffer[j];
+
+ byte delta2 = delta ^ last_delta;
+ last_delta = delta;
+
+ byte delta3 = delta2 ^ last_delta2;
+ last_delta2 = delta2;
+
+ byte min_delta = delta;
+ if(min_delta > delta2) min_delta = delta2;
+ if(min_delta > delta3) min_delta = delta3;
+
+ this_buf_estimate += hamming_weight(min_delta);
+ }
+
+ this_buf_estimate /= 2;
+
+ if(upper_limit)
+ estimate += std::min(upper_limit, this_buf_estimate);
+ else
+ estimate += this_buf_estimate;
+ }
+
+/*************************************************
+* Estimate the entropy of the buffer *
+*************************************************/
+u32bit entropy_estimate(const byte buffer[], u32bit length)
+ {
+ if(length <= 4)
+ return 0;
+
+ u32bit estimate = 0;
+ byte last = 0, last_delta = 0, last_delta2 = 0;
+
+ for(u32bit j = 0; j != length; ++j)
+ {
+ byte delta = last ^ buffer[j];
+ last = buffer[j];
+
+ byte delta2 = delta ^ last_delta;
+ last_delta = delta;
+
+ byte delta3 = delta2 ^ last_delta2;
+ last_delta2 = delta2;
+
+ byte min_delta = delta;
+ if(min_delta > delta2) min_delta = delta2;
+ if(min_delta > delta3) min_delta = delta3;
+
+ estimate += hamming_weight(min_delta);
+ }
+
+ return (estimate / 2);
+ }
+
+}
diff --git a/src/utils/entropy.h b/src/utils/entropy.h
new file mode 100644
index 000000000..24d2fbdbf
--- /dev/null
+++ b/src/utils/entropy.h
@@ -0,0 +1,48 @@
+/*************************************************
+* Entropy_Estimator Header File *
+* (C) 2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_ENTROPY_ESTIMATOR_H__
+#define BOTAN_ENTROPY_ESTIMATOR_H__
+
+#include <botan/types.h>
+#include <algorithm>
+
+namespace Botan {
+
+/**
+Naive Entropy Estimation using first, second, and third order deltas
+
+@todo It would be nice to extend this to test using zlib or bzip2 if
+those modules are compiled in to the library
+*/
+class BOTAN_DLL Entropy_Estimator
+ {
+ public:
+ Entropy_Estimator()
+ { last = last_delta = last_delta2 = 0; estimate = 0; }
+
+ /**
+ Return the current estimate
+ */
+ u32bit value() const { return estimate; }
+
+ /**
+ Set an upper bound on the estimate so far
+ */
+ void set_upper_bound(u32bit upper_limit)
+ { estimate = std::min(estimate, upper_limit); }
+
+ /**
+ Add more entropy data to the current estimation
+ */
+ void update(const byte buffer[], u32bit length, u32bit upper_limit = 0);
+ private:
+ u32bit estimate;
+ byte last, last_delta, last_delta2;
+ };
+
+}
+
+#endif
diff --git a/src/utils/info.txt b/src/utils/info.txt
index b20a16a56..0f0a18a35 100644
--- a/src/utils/info.txt
+++ b/src/utils/info.txt
@@ -19,6 +19,8 @@ charset.cpp
charset.h
datastor.cpp
datastor.h
+entropy.h
+entropy.cpp
loadstor.h
mem_ops.h
mlock.cpp
diff --git a/src/utils/util.cpp b/src/utils/util.cpp
index af1c62ebd..f8b98fcb8 100644
--- a/src/utils/util.cpp
+++ b/src/utils/util.cpp
@@ -64,36 +64,4 @@ u32bit dl_work_factor(u32bit bits)
#endif
}
-/*************************************************
-* Estimate the entropy of the buffer *
-*************************************************/
-u32bit entropy_estimate(const byte buffer[], u32bit length)
- {
- if(length <= 4)
- return 0;
-
- u32bit estimate = 0;
- byte last = 0, last_delta = 0, last_delta2 = 0;
-
- for(u32bit j = 0; j != length; ++j)
- {
- byte delta = last ^ buffer[j];
- last = buffer[j];
-
- byte delta2 = delta ^ last_delta;
- last_delta = delta;
-
- byte delta3 = delta2 ^ last_delta2;
- last_delta2 = delta2;
-
- byte min_delta = delta;
- if(min_delta > delta2) min_delta = delta2;
- if(min_delta > delta3) min_delta = delta3;
-
- estimate += hamming_weight(min_delta);
- }
-
- return (estimate / 2);
- }
-
}
diff --git a/src/utils/util.h b/src/utils/util.h
index 62ecb948d..d0e2e110a 100644
--- a/src/utils/util.h
+++ b/src/utils/util.h
@@ -30,7 +30,6 @@ BOTAN_DLL u32bit round_down(u32bit, u32bit);
/*************************************************
* Work Factor Estimates *
*************************************************/
-BOTAN_DLL u32bit entropy_estimate(const byte[], u32bit);
BOTAN_DLL u32bit dl_work_factor(u32bit);
}