Fix bug in randpool_getN.
[bertos.git] / algos / randpool.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief API function for to manage entropy pool.
9  *
10  * \version $Id$
11  * \author Daniele Basile <asterix@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.15  2007/02/13 15:09:19  asterix
17  *#* Fix bug in randpool_getN.
18  *#*
19  *#* Revision 1.14  2007/02/13 09:57:12  asterix
20  *#* Add directive #if in struct EntropyPool, and remove #else in randpool_add.
21  *#*
22  *#* Revision 1.13  2007/02/12 18:25:34  asterix
23  *#* Fix bug in randpool_getN.
24  *#*
25  *#* Revision 1.12  2007/02/12 09:47:39  asterix
26  *#* Remove randpool_save. Add randpool_pool.
27  *#*
28  *#* Revision 1.10  2007/02/12 09:03:32  asterix
29  *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
30  *#*
31  *#* Revision 1.9  2007/02/09 17:58:09  asterix
32  *#* Add macro CONFIG_RANDPOOL_TIMER.
33  *#*
34  *#* Revision 1.6  2007/02/09 09:24:38  asterix
35  *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
36  *#*
37  *#* Revision 1.3  2007/02/08 14:25:29  asterix
38  *#* Write static funcion push_byte.
39  *#*
40  *#*/
41
42 #include "randpool.h"
43 #include "md2.h"
44
45 #include <stdio.h>           //sprintf();
46 #include <string.h>          //memset(), memcpy();
47
48 #include <cfg/compiler.h>
49 #include <cfg/debug.h>       //ASSERT()
50 #include <cfg/macros.h>      //MIN()
51
52 #if CONFIG_RANDPOOL_TIMER
53         #include <drv/timer.h>       //timer_clock();
54 #endif
55
56
57
58 /*
59  * Insert bytes in entropy pool, making a XOR of bytes present
60  * in entropy pool.
61  */
62 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
63 {
64         size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
65         uint8_t *byte;
66
67         byte = (uint8_t *)_byte;
68
69         /*
70          * Insert a bytes in entropy pool.
71          */
72         for(int j = 0; j < n_byte; j++)
73         {
74                 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
75                 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
76         }
77
78         pool->pos_add  =  i; // Update a insert bytes.
79 }
80
81
82 /*
83  * This function stir entropy pool with MD2 function hash.
84  *
85  */
86 static void randpool_stir(EntropyPool *pool)
87 {
88         size_t entropy = pool->entropy; //Save current calue of entropy.
89         Md2Context context;
90         uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
91
92         md2_init(&context); //Init MD2 algorithm.
93
94         randpool_add(pool, "", 0, 0);
95
96         for (int i = 0; i < NUM_STIR_LOOP; i++)
97         {
98                 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
99
100                 /*
101                  * Hash with MD2 algorithm the entropy pool.
102                  */
103                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
104
105                 md2_update(&context, tmp_buf, strlen(tmp_buf));
106
107                 /*Insert a message digest in entropy pool.*/
108                 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
109
110                 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
111
112         }
113
114         /*Insert in pool the difference between a two call of this function (see above).*/
115         randpool_add(pool, "", 0, 0);
116
117         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
118 }
119
120 /**
121  * Add n_bit of  entropy in entropy pool.
122  */
123 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
124 {
125         uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
126
127         randpool_push(pool, data, data_len); //Insert data to entropy pool.
128
129         randpool_push(pool, sep, strlen(sep)); // ??
130
131 #if CONFIG_RANDPOOL_TIMER
132
133         ticks_t event = timer_clock();
134         uint32_t delta;
135
136         /*Difference of time between a two accese to entropy pool.*/
137         delta = event - pool->last_counter;
138
139
140         randpool_push(pool, &delta, sizeof(delta));
141
142         delta = delta & 0xff;
143
144         randpool_push(pool, &delta, sizeof(delta));
145
146         /*
147          * Count of number entropy bit add with delta.
148          */
149         while(delta)
150         {
151                 delta >>= 1;
152                 entropy++;
153         }
154
155         pool->last_counter = event;
156
157 #endif
158
159         pool->entropy += entropy;      //Update a entropy of the pool.
160 }
161
162 /**
163  * Randpool function initialization.
164  * The entropy pool can be initialize also with 
165  * a previous entropy pool. 
166  */
167 void randpool_init(EntropyPool *pool, void *_data, size_t len)
168 {
169         uint8_t *data;
170
171         data = (uint8_t *)_data;
172
173         memset(pool, 0, sizeof(EntropyPool));
174         pool->pos_get = CONFIG_MD2_BLOCK_LEN;
175
176 #if CONFIG_RANDPOOL_TIMER
177         pool->last_counter = timer_clock();
178 #endif
179
180         ASSERT(len < CONFIG_SIZE_ENTROPY_POOL);
181
182         if(len > 0)
183         {
184                 /*
185                  * Initialize a entropy pool with a 
186                  * previous pool, and assume all pool as
187                  * entropy.
188                  */
189                 memcpy(pool->pool_entropy, data, len);
190                 pool->entropy = len;
191         }
192
193 }
194
195 /**
196  * Get the actual value of entropy.
197  */
198 size_t randpool_size(EntropyPool *pool)
199 {
200         return pool->entropy;
201 }
202
203 void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
204 {
205
206 }
207
208 /**
209  * Get n_byte from entropy pool. If n_byte is larger than number
210  * byte of entropy in entropy pool, rand_pool_getN continue
211  * to generate pseudocasual value from previous state of
212  * pool.
213  */
214 void randpool_getN(EntropyPool *pool, void *_data, size_t n_byte)
215 {
216         Md2Context context;
217         size_t i = pool->pos_get;
218         size_t n = n_byte;
219         size_t pos_write = 0;  //Number of block has been written in data.
220         size_t len = MIN((size_t)CONFIG_MD2_BLOCK_LEN, n_byte);
221         uint8_t *data;
222
223         data = (uint8_t *)_data;
224
225         /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
226         ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
227
228         md2_init(&context); 
229
230         while(n > 0)
231         {
232
233                 /*Hash previous state of pool*/
234                 md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
235
236                 memcpy(&data[prev], md2_end(&context), len);
237
238                 pos_write += len;   //Update number of block has been written in data.
239                 n -= len;           //Number of byte copied in data.
240
241                 len = MIN(n,(size_t)CONFIG_MD2_BLOCK_LEN);
242
243                 i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL;
244
245                 /* If we haven't more entropy pool to hash, we stir it.*/
246                 if(i < CONFIG_MD2_BLOCK_LEN)
247                 {
248                         randpool_stir(pool);
249                         i = pool->pos_get;
250                 }
251
252         }
253         
254
255         pool->pos_get = i; //Current number of byte we get from pool.
256         
257         pool->entropy -= n_byte; //Update a entropy.
258
259         /*If we get all entropy entropy is 0*/
260         if(pool->entropy < 0) 
261                 pool->entropy = 0;
262
263 }
264
265 /**
266  * Return a pointer to entropy pool.
267  */
268 uint8_t *randpool_pool(EntropyPool *pool)
269 {
270         return pool->pool_entropy;
271 }
272