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.11 2007/02/12 09:40:43 asterix
17 *#* Remove randpool_load function. Add *data in randpool_init prototype.
19 *#* Revision 1.10 2007/02/12 09:03:32 asterix
20 *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
22 *#* Revision 1.9 2007/02/09 17:58:09 asterix
23 *#* Add macro CONFIG_RANDPOOL_TIMER.
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.
28 *#* Revision 1.3 2007/02/08 14:25:29 asterix
29 *#* Write static funcion push_byte.
36 #include <stdio.h> //sprintf();
37 #include <string.h> //memset(), memcpy();
39 #include <cfg/compiler.h>
40 #include <cfg/debug.h> //ASSERT()
41 #include <cfg/macros.h> //MIN()
43 #if CONFIG_RANDPOOL_TIMER
44 #include <drv/timer.h> //timer_clock();
50 * Insert bytes in entropy pool, making a XOR of bytes present
53 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
55 size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
58 byte = (uint8_t *)_byte;
61 * Insert a bytes in entropy pool.
63 for(int j = 0; j < n_byte; j++)
65 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
66 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
69 pool->pos_add = i; // Update a insert bytes.
74 * This function stir entropy pool with MD2 function hash.
77 static void randpool_stir(EntropyPool *pool)
79 size_t entropy = pool->entropy; //Save current calue of entropy.
81 uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
83 md2_init(&context); //Init MD2 algorithm.
85 randpool_add(pool, "", 0, 0);
87 for (int i = 0; i < NUM_STIR_LOOP; i++)
89 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
92 * Hash with MD2 algorithm the entropy pool.
94 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
96 md2_update(&context, tmp_buf, strlen(tmp_buf));
98 /*Insert a message digest in entropy pool.*/
99 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
101 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
105 /*Insert in pool the difference between a two call of this function (see above).*/
106 randpool_add(pool, "", 0, 0);
108 pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
112 * Add n_bit of entropy in entropy pool.
114 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
116 uint8_t sep[] = "\xaa\xaa\xaa\xaa"; // ??
118 randpool_push(pool, data, data_len); //Insert data to entropy pool.
120 randpool_push(pool, sep, strlen(sep)); // ??
122 #if CONFIG_RANDPOOL_TIMER
124 ticks_t event = timer_clock();
127 /*Difference of time between a two accese to entropy pool.*/
128 delta = event - pool->last_counter;
131 randpool_push(pool, &delta, sizeof(delta));
133 delta = delta & 0xff;
135 randpool_push(pool, &delta, sizeof(delta));
138 * Count of number entropy bit add with delta.
149 /*Difference of time between a two accese to entropy pool.*/
150 event = pool->last_counter++;
154 pool->entropy += entropy; //Update a entropy of the pool.
155 pool->last_counter = event;
159 * Randpool function initialization.
160 * The entropy pool can be initialize also with
161 * a previous entropy pool.
163 void randpool_init(EntropyPool *pool, void *_data, size_t len)
167 data = (uint8_t *)_data;
169 memset(pool, 0, sizeof(EntropyPool));
170 pool->pos_get = CONFIG_MD2_BLOCK_LEN;
172 #if CONFIG_RANDPOOL_TIMER
173 pool->last_counter = timer_clock();
176 ASSERT(len < CONFIG_SIZE_ENTROPY_POOL);
181 * Initialize a entropy pool with a
182 * previous pool, and assume all pool as
185 memcpy(pool->pool_entropy, data, len);
192 * Get the actual value of entropy.
194 size_t randpool_size(EntropyPool *pool)
196 return pool->entropy;
199 void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
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
210 void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
213 size_t i = pool->pos_get;
215 size_t len = MIN((size_t)CONFIG_MD2_BLOCK_LEN, n_byte);
217 /* Test if i + CONFIG_MD2_BLOCK_LEN is inside of entropy pool.*/
218 ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
224 /*Hash previous state of pool*/
225 md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
227 memcpy(data, md2_end(&context), len);
229 n -= len; //Number of byte copied in data.
231 len = MIN(n, CONFIG_MD2_BLOCK_LEN);
233 i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL;
235 /* If we haven't more entropy pool to hash, we stir it.*/
236 if(i < CONFIG_MD2_BLOCK_LEN)
243 pool->pos_get = i; //Current number of byte we get from pool.
245 pool->entropy -= n_byte; //Update a entropy.
247 /*If we get all entropy entropy is 0*/
248 if(pool->entropy < 0)
252 bool randpool_save(void *data)