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