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