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