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