/*#*
*#* $Log$
+ *#* Revision 1.4 2007/02/08 17:18:00 asterix
+ *#* Write add_data and stir function. Typos
+ *#*
*#* Revision 1.3 2007/02/08 14:25:29 asterix
*#* Write static funcion push_byte.
*#*
#include <string.h> //memset(), memcpy();
#include <cfg/compiler.h>
#include <cfg/debug.h> //ASSERT()
+#include <drv/timer.h> //timer_clock();
+
+#include <stdio.h> //sprintf();
+
-static void stir(EntrPool *pool)
-{
-
-}
/*
* Insert bytes in entropy pool, making a XOR of bytes present
pool->pool_pos_add = i; // Update a insert bytes.
}
+/*
+ * This function stir entropy pool with MD2 function hash.
+ *
+ */
+static void stir(EntrPool *pool)
+{
+ size_t entropy = pool->entropy; //Save current calue of entropy.
+ Md2Context context;
+ uint8_t tmp_buf[(sizeof(size_t) * 2) + sizeof(int)]; //Temporary buffer.
+
+ md2_init(&context);
+
+ add_data(pool, "", 0);
+
+ for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
+ {
+ sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pool_pos_add);
+
+ /*
+ * Hash with MD2 algorithm the entropy pool.
+ */
+ md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
+
+ md2_update(&context, tmp_buf, CONFIG_SIZE_ENTROPY_POOL);
+
+ push_byte(pool, md2_end(&context)); //Insert a message digest in entropy pool.
+
+ pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Update a counter modulo 4.
+
+ }
+
+ /*Insert in pool the difference between a two call of this function (see above).*/
+ add_data(pool, "", 0);
+
+ pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
+}
+
+
void init_pool(EntrPool *pool)
{
memset(pool, 0, sizeof(EntrPool));
+ //TODO: inizializzazione del timer di sistema.
+
}
+/**
+ * Add n_bit of entropy in entropy pool.
+ */
void add_data(EntrPool *pool, void *data, size_t n_bit)
{
+ uint32_t event = timer_clock();
+ uint32_t delta;
+
+ push_byte(pool, data); //Insert data to entropy pool.
+
+ push_byte(pool, "\xaa\xaa\xaa\xaa"); // ??
+
+ /*Difference of time between a two accese to entropy pool.*/
+ delta = event - pool->last_counter;
+
+ push_byte(pool, &delta);
+
+ delta = delta & 0xff;
+
+ push_byte(pool, &delta);
+ /*
+ * Count of number entropy bit add with delta.
+ */
+ while(delta)
+ {
+ delta >>= 1;
+ n_bit++;
+ }
+
+ pool->entropy = n_bit; //Update a entropy of the pool.
+ pool->last_counter = event;
}
size_t pool_size(EntrPool *pool)