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