diff options
author | lloyd <[email protected]> | 2008-11-06 19:22:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-06 19:22:24 +0000 |
commit | bf7f64fae0d14cc175732d7dc69f71f47edc7c41 (patch) | |
tree | 8842acf8197f733782fbbf3ebbbf3fb858151769 /src/utils/entropy.cpp | |
parent | d50e41609c95945c714c445f6b400e02fe659b90 (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/entropy.cpp')
-rw-r--r-- | src/utils/entropy.cpp | 80 |
1 files changed, 80 insertions, 0 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); + } + +} |