aboutsummaryrefslogtreecommitdiffstats
path: root/src/sha1prng.cpp
blob: 562b001bdc7a67936eca89e66a551e45d70817aa (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*************************************************
* SHA1PRNG Source File                           *
* (C) 2007 FlexSecure GmbH / Manuel Hartl        *
* (C) 2008 Jack Lloyd                            *
*************************************************/

#include <botan/sha1prng.h>
#include <botan/lookup.h>
#include <botan/bit_ops.h>
#include <algorithm>

namespace Botan {

/*************************************************
* Generate a buffer of random bytes              *
*************************************************/
void SHA1PRNG::randomize(byte result[], u32bit length) throw(PRNG_Unseeded)
   {
   if(!is_seeded())
      throw PRNG_Unseeded(name());

   const u32bit SHA1_BYTES = hash->OUTPUT_LENGTH;

   u32bit resultIndex=0;
   /**
    * use ramining bytes from buffer for result
    */
   if(buf_pos>0)
   {
       u32bit remainderCountIndex=buf_pos;
       unsigned int j = length >= (unsigned)(SHA1_BYTES - buf_pos) ? SHA1_BYTES - buf_pos : length;

       for(;resultIndex < j;resultIndex++)
       {
           result[resultIndex] = buffer[remainderCountIndex];
           buffer[remainderCountIndex++] = 0;
       }

       buf_pos += j;
   }

   /**
    * fill result with fresh random bytes
    */
   while(resultIndex < length)
       {
       hash->update(state.begin(),SHA1_BYTES);
       hash->final(buffer.begin());
       update_state(buffer.begin());
       int k=length-1 <= SHA1_BYTES ? length : SHA1_BYTES;
       for(int j = 0;j < k; j++)
       {
           result[resultIndex++] = buffer[j];
           buffer[j] = 0;
       }
       buf_pos+=k;
       }
   buf_pos %=SHA1_BYTES;
   }

/*************************************************
* Refill the internal state                      *
*************************************************/
void SHA1PRNG::update_state(byte update[])
   {
   signed int i = 1;
   bool flag2 = false;

   for(u32bit k = 0; k < state.size(); k++)
      {
      int b1 = state[k]%256;
      if(b1>128)
         {
         b1-=256;
         }

      int b2 = update[k]%256;
      if(b2>128)
         {
         b2-=256;
         }
      int j = b1+b2+i;
      if(j>256)
         {
         j-=256;
         }
      flag2 |= state.begin()[k] != (byte)j;
      state.begin()[k] = (byte)j;
      i = j >> 8;
      }

   if(!flag2)
      {
      state[0]++;
      }
   }

/*************************************************
* Add entropy to internal state                  *
*************************************************/
void SHA1PRNG::add_randomness(const byte data[], u32bit length)
   {
   prng->add_entropy(data, length);
   MemoryVector<byte> for_rand;
   for_rand.set(data, length);

   if(prng->is_seeded())
      {
      prng->randomize(for_rand, length);
      hash->clear();
      hash->update(for_rand,length);
      hash->final(state.begin());
      }
   }

/*************************************************
* Check if the RNG is seeded                     *
*************************************************/
bool SHA1PRNG::is_seeded() const
   {
   return prng->is_seeded();
   }

/*************************************************
* Clear memory of sensitive data                 *
*************************************************/
void SHA1PRNG::clear() throw()
   {
   hash->clear();
   prng->clear();
   }

/*************************************************
* Return the name of this type                   *
*************************************************/
std::string SHA1PRNG::name() const
   {
   return "SHA1PRNG";
   }

/*************************************************
* SHA1PRNG Constructor                           *
*************************************************/
SHA1PRNG::SHA1PRNG(RandomNumberGenerator* prng_ptr)
   {
   if(!prng_ptr)
      throw Invalid_Argument("SHA1PRNG constructor: NULL prng");

   hash = get_hash("SHA-1");
   prng = prng_ptr;

   buf_pos = 0;

   state.grow_to(hash->OUTPUT_LENGTH);
   buffer.grow_to(hash->OUTPUT_LENGTH);
   }

/*************************************************
* SHA1PRNG Destructor                            *
*************************************************/
SHA1PRNG::~SHA1PRNG()
   {
   delete hash;
   delete prng;
   }

}