a32fdff4170d045caf0503ff0ba82740851ceb8d
[bertos.git] / algo / randpool.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief API function for to manage entropy pool.
34  *
35  * \version $Id$
36  * \author Daniele Basile <asterix@develer.com>
37  */
38
39 /*#*
40  *#* $Log$
41  *#* Revision 1.20  2007/06/07 16:06:39  batt
42  *#* Fix some doxygen errors.
43  *#*
44  *#* Revision 1.19  2007/02/15 13:54:26  asterix
45  *#* Rename randpool_getN in randpool_get. Fix bug in randpool_get.
46  *#*
47  *#* Revision 1.17  2007/02/15 13:40:42  asterix
48  *#* Fix bug in randpool_add and randpool_strir.
49  *#*
50  *#* Revision 1.16  2007/02/13 15:11:37  asterix
51  *#* Typo.
52  *#*
53  *#* Revision 1.14  2007/02/13 09:57:12  asterix
54  *#* Add directive #if in struct EntropyPool, and remove #else in randpool_add.
55  *#*
56  *#* Revision 1.13  2007/02/12 18:25:34  asterix
57  *#* Fix bug in randpool_getN.
58  *#*
59  *#* Revision 1.12  2007/02/12 09:47:39  asterix
60  *#* Remove randpool_save. Add randpool_pool.
61  *#*
62  *#* Revision 1.10  2007/02/12 09:03:32  asterix
63  *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
64  *#*
65  *#* Revision 1.9  2007/02/09 17:58:09  asterix
66  *#* Add macro CONFIG_RANDPOOL_TIMER.
67  *#*
68  *#* Revision 1.6  2007/02/09 09:24:38  asterix
69  *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
70  *#*
71  *#* Revision 1.3  2007/02/08 14:25:29  asterix
72  *#* Write static funcion push_byte.
73  *#*
74  *#*/
75
76 #include "randpool.h"
77 #include "md2.h"
78
79 #include <stdio.h>           //sprintf();
80 #include <string.h>          //memset(), memcpy();
81
82 #include <cfg/compiler.h>
83 #include <cfg/debug.h>       //ASSERT()
84 #include <cfg/macros.h>      //MIN(), ROUND_UP();
85
86 #if CONFIG_RANDPOOL_TIMER
87         #include <drv/timer.h>       //timer_clock();
88 #endif
89
90
91
92 /*
93  * Insert bytes in entropy pool, making a XOR of bytes present
94  * in entropy pool.
95  */
96 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
97 {
98         size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
99         uint8_t *byte;
100
101         byte = (uint8_t *)_byte;
102
103         /*
104          * Insert a bytes in entropy pool.
105          */
106         for(int j = 0; j < n_byte; j++)
107         {
108                 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
109                 i = (++i) % CONFIG_SIZE_ENTROPY_POOL;
110         }
111
112         pool->pos_add  =  i; // Update a insert bytes.
113 }
114
115
116 /*
117  * This function stir entropy pool with MD2 function hash.
118  *
119  */
120 static void randpool_stir(EntropyPool *pool)
121 {
122         size_t entropy = pool->entropy; //Save current calue of entropy.
123         Md2Context context;
124         uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer.
125
126         md2_init(&context); //Init MD2 algorithm.
127
128         randpool_add(pool, NULL, 0);
129
130         for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
131         {
132                 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
133
134                 /*
135                  * Hash with MD2 algorithm the entropy pool.
136                  */
137                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
138
139                 md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
140
141                 /*Insert a message digest in entropy pool.*/
142                 randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
143
144                 pool->counter = pool->counter + 1; 
145
146         }
147
148         /*Insert in pool the difference between a two call of this function (see above).*/
149         randpool_add(pool, NULL, 0);
150
151         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
152 }
153
154 /**
155  * Add \param entropy bits from \param data buffer to the entropy \param pool
156  */
157 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
158 {
159         uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
160         size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input.
161
162         randpool_push(pool, data, data_len); //Insert data to entropy pool.
163
164 #if CONFIG_RANDPOOL_TIMER
165
166         ticks_t event = timer_clock();
167         ticks_t delta;
168
169         /*Difference of time between a two accese to entropy pool.*/
170         delta = event - pool->last_counter;
171
172         randpool_push(pool, &event, sizeof(ticks_t));
173         randpool_push(pool, sep, sizeof(sep) - 1); // ??
174         randpool_push(pool, &delta, sizeof(delta));
175
176         /*
177          * Count of number entropy bit add with delta.
178          */
179         delta = delta & 0xff;
180         while(delta)
181         {
182                 delta >>= 1;
183                 entropy++;
184         }
185
186         pool->last_counter = event;
187
188 #endif
189
190         pool->entropy += entropy;      //Update a entropy of the pool.
191 }
192
193 /**
194  * Randpool function initialization.
195  * The entropy pool can be initialize also with 
196  * a previous entropy pool. 
197  */
198 void randpool_init(EntropyPool *pool, void *_data, size_t len)
199 {
200         uint8_t *data;
201
202         data = (uint8_t *)_data;
203
204         memset(pool, 0, sizeof(EntropyPool));
205         pool->pos_get = MD2_DIGEST_LEN;
206
207 #if CONFIG_RANDPOOL_TIMER
208         pool->last_counter = timer_clock();
209 #endif
210
211         if(data)
212         {
213                 /*
214                  * Initialize a entropy pool with a 
215                  * previous pool, and assume all pool as
216                  * entropy.
217                  */
218                 len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
219                 memcpy(pool->pool_entropy, data, len);
220                 pool->entropy = len;
221         }
222
223 }
224
225 /**
226  * Get the actual value of entropy.
227  */
228 size_t randpool_size(EntropyPool *pool)
229 {
230         return pool->entropy;
231 }
232
233 /**
234  * Get \param n_byte from entropy pool. If n_byte is larger than number
235  * byte of entropy in entropy pool, randpool_get continue
236  * to generate pseudocasual value from previous state of
237  * pool.
238  * \param n_byte number fo bytes to read.
239  * \param pool is the pool entropy context.
240  * \param _data is the pointer to write the random data to.
241  */
242 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
243 {
244         Md2Context context;
245         size_t i = pool->pos_get;
246         size_t n = n_byte;
247         size_t pos_write = 0;  //Number of block has been written in data.
248         size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
249         uint8_t *data;
250
251         data = (uint8_t *)_data;
252
253         /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
254         ASSERT((MD2_DIGEST_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
255
256         md2_init(&context);
257
258         while(n > 0)
259         {
260
261                 /*Hash previous state of pool*/
262                 md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
263
264                 memcpy(&data[pos_write], md2_end(&context), len);
265
266                 pos_write += len;   //Update number of block has been written in data.
267                 n -= len;           //Number of byte copied in data.
268
269                 len = MIN(n,(size_t)MD2_DIGEST_LEN);
270
271                 i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
272
273                 /* If we haven't more entropy pool to hash, we stir it.*/
274                 if(i < MD2_DIGEST_LEN)
275                 {
276                         randpool_stir(pool);
277                         i = pool->pos_get;
278                 }
279
280         }
281
282         pool->pos_get = i; //Current number of byte we get from pool.
283         pool->entropy -= n_byte; //Update a entropy.
284
285         /*If we get all entropy entropy is 0*/
286         if(pool->entropy < 0) 
287                 pool->entropy = 0;
288
289 }
290
291 /**
292  * Return a pointer to entropy pool.
293  */
294 uint8_t *randpool_pool(EntropyPool *pool)
295 {
296         return pool->pool_entropy;
297 }
298