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