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