4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief API function for to manage entropy pool.
11 * \author Daniele Basile <asterix@develer.com>
16 *#* Revision 1.19 2007/02/15 13:54:26 asterix
17 *#* Rename randpool_getN in randpool_get. Fix bug in randpool_get.
19 *#* Revision 1.17 2007/02/15 13:40:42 asterix
20 *#* Fix bug in randpool_add and randpool_strir.
22 *#* Revision 1.16 2007/02/13 15:11:37 asterix
25 *#* Revision 1.14 2007/02/13 09:57:12 asterix
26 *#* Add directive #if in struct EntropyPool, and remove #else in randpool_add.
28 *#* Revision 1.13 2007/02/12 18:25:34 asterix
29 *#* Fix bug in randpool_getN.
31 *#* Revision 1.12 2007/02/12 09:47:39 asterix
32 *#* Remove randpool_save. Add randpool_pool.
34 *#* Revision 1.10 2007/02/12 09:03:32 asterix
35 *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
37 *#* Revision 1.9 2007/02/09 17:58:09 asterix
38 *#* Add macro CONFIG_RANDPOOL_TIMER.
40 *#* Revision 1.6 2007/02/09 09:24:38 asterix
41 *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
43 *#* Revision 1.3 2007/02/08 14:25:29 asterix
44 *#* Write static funcion push_byte.
51 #include <stdio.h> //sprintf();
52 #include <string.h> //memset(), memcpy();
54 #include <cfg/compiler.h>
55 #include <cfg/debug.h> //ASSERT()
56 #include <cfg/macros.h> //MIN(), ROUND_UP();
58 #if CONFIG_RANDPOOL_TIMER
59 #include <drv/timer.h> //timer_clock();
65 * Insert bytes in entropy pool, making a XOR of bytes present
68 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
70 size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
73 byte = (uint8_t *)_byte;
76 * Insert a bytes in entropy pool.
78 for(int j = 0; j < n_byte; j++)
80 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
81 i = (++i) % CONFIG_SIZE_ENTROPY_POOL;
84 pool->pos_add = i; // Update a insert bytes.
89 * This function stir entropy pool with MD2 function hash.
92 static void randpool_stir(EntropyPool *pool)
94 size_t entropy = pool->entropy; //Save current calue of entropy.
96 uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer.
98 md2_init(&context); //Init MD2 algorithm.
100 randpool_add(pool, NULL, 0);
102 for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
104 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
107 * Hash with MD2 algorithm the entropy pool.
109 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
111 md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
113 /*Insert a message digest in entropy pool.*/
114 randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
116 pool->counter = pool->counter + 1;
120 /*Insert in pool the difference between a two call of this function (see above).*/
121 randpool_add(pool, NULL, 0);
123 pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
127 * Add \param entropy bits from \param data buffer to the entropy \param pool.
129 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
131 uint8_t sep[] = "\xaa\xaa\xaa\xaa"; // ??
132 size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input.
134 randpool_push(pool, data, data_len); //Insert data to entropy pool.
136 #if CONFIG_RANDPOOL_TIMER
138 ticks_t event = timer_clock();
141 /*Difference of time between a two accese to entropy pool.*/
142 delta = event - pool->last_counter;
144 randpool_push(pool, &event, sizeof(ticks_t));
145 randpool_push(pool, sep, sizeof(sep) - 1); // ??
146 randpool_push(pool, &delta, sizeof(delta));
149 * Count of number entropy bit add with delta.
151 delta = delta & 0xff;
158 pool->last_counter = event;
162 pool->entropy += entropy; //Update a entropy of the pool.
166 * Randpool function initialization.
167 * The entropy pool can be initialize also with
168 * a previous entropy pool.
170 void randpool_init(EntropyPool *pool, void *_data, size_t len)
174 data = (uint8_t *)_data;
176 memset(pool, 0, sizeof(EntropyPool));
177 pool->pos_get = MD2_DIGEST_LEN;
179 #if CONFIG_RANDPOOL_TIMER
180 pool->last_counter = timer_clock();
186 * Initialize a entropy pool with a
187 * previous pool, and assume all pool as
190 len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
191 memcpy(pool->pool_entropy, data, len);
198 * Get the actual value of entropy.
200 size_t randpool_size(EntropyPool *pool)
202 return pool->entropy;
206 * Get \param n_byte from entropy pool. If n_byte is larger than number
207 * byte of entropy in entropy pool, randpool_get continue
208 * to generate pseudocasual value from previous state of
211 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
214 size_t i = pool->pos_get;
216 size_t pos_write = 0; //Number of block has been written in data.
217 size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
220 data = (uint8_t *)_data;
222 /* Test if i + CONFIG_MD2_BLOCK_LEN is inside of entropy pool.*/
223 ASSERT((MD2_DIGEST_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
230 /*Hash previous state of pool*/
231 md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
233 memcpy(&data[pos_write], md2_end(&context), len);
235 pos_write += len; //Update number of block has been written in data.
236 n -= len; //Number of byte copied in data.
238 len = MIN(n,(size_t)MD2_DIGEST_LEN);
240 i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
242 /* If we haven't more entropy pool to hash, we stir it.*/
243 if(i < MD2_DIGEST_LEN)
252 pool->pos_get = i; //Current number of byte we get from pool.
254 pool->entropy -= n_byte; //Update a entropy.
256 /*If we get all entropy entropy is 0*/
257 if(pool->entropy < 0)
263 * Return a pointer to entropy pool.
265 uint8_t *randpool_pool(EntropyPool *pool)
267 return pool->pool_entropy;