aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/entropy.cpp
blob: 1562eb0d2400f47fb0526ac7db905355110325e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
   }

}