Remove randpool_load function. Add *data in randpool_init prototype.
[bertos.git] / algos / randpool.c
index 029f5ba10406832051e13ccaf3fec5a9050fe53c..63a2716a6fa8a9b89145e1aef5b34eed1f1c0984 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.11  2007/02/12 09:40:43  asterix
+ *#* Remove randpool_load function. Add *data in randpool_init prototype.
+ *#*
+ *#* Revision 1.10  2007/02/12 09:03:32  asterix
+ *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
+ *#*
  *#* Revision 1.9  2007/02/09 17:58:09  asterix
  *#* Add macro CONFIG_RANDPOOL_TIMER.
  *#*
 #include "randpool.h"
 #include "md2.h"
 
+#include <stdio.h>           //sprintf();
 #include <string.h>          //memset(), memcpy();
+
 #include <cfg/compiler.h>
 #include <cfg/debug.h>       //ASSERT()
 #include <cfg/macros.h>      //MIN()
-#include <drv/timer.h>       //timer_clock();
 
-#include <stdio.h>           //sprintf();
-
-//TODO:
-#if CONFIG_RANDPOOL_TIMER 
-       #define TIMER() timer_clock()
-#else
-       #define TIMER() 0  //TODO:
+#if CONFIG_RANDPOOL_TIMER
+       #include <drv/timer.h>       //timer_clock();
 #endif
 
+
+
 /*
  * Insert bytes in entropy pool, making a XOR of bytes present
  * in entropy pool.
@@ -109,17 +113,21 @@ static void randpool_stir(EntropyPool *pool)
  */
 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
 {
-       ticks_t event = TIMER();
-       uint32_t delta;
        uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
 
        randpool_push(pool, data, data_len); //Insert data to entropy pool.
 
        randpool_push(pool, sep, strlen(sep)); // ??
 
+#if CONFIG_RANDPOOL_TIMER
+
+       ticks_t event = timer_clock();
+       uint32_t delta;
+
        /*Difference of time between a two accese to entropy pool.*/
        delta = event - pool->last_counter;
 
+
        randpool_push(pool, &delta, sizeof(delta));
 
        delta = delta & 0xff;
@@ -135,19 +143,48 @@ void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy
                entropy++;
        }
 
+#else
+       size_t event = 0;
+
+       /*Difference of time between a two accese to entropy pool.*/
+       event = pool->last_counter++;
+
+#endif
+
        pool->entropy += entropy;      //Update a entropy of the pool.
        pool->last_counter = event;
 }
 
-
-void randpool_init(EntropyPool *pool)
+/**
+ * Randpool function initialization.
+ * The entropy pool can be initialize also with 
+ * a previous entropy pool. 
+ */
+void randpool_init(EntropyPool *pool, void *_data, size_t len)
 {
+       uint8_t *data;
+
+       data = (uint8_t *)_data;
 
        memset(pool, 0, sizeof(EntropyPool));
        pool->pos_get = CONFIG_MD2_BLOCK_LEN;
-       pool->last_counter = TIMER();
 
-       //TODO: inizializzazione del timer di sistema.
+#if CONFIG_RANDPOOL_TIMER
+       pool->last_counter = timer_clock();
+#endif
+
+       ASSERT(len < CONFIG_SIZE_ENTROPY_POOL);
+
+       if(len > 0)
+       {
+               /*
+                * Initialize a entropy pool with a 
+                * previous pool, and assume all pool as
+                * entropy.
+                */
+               memcpy(pool->pool_entropy, data, len);
+               pool->entropy = len;
+       }
 
 }
 
@@ -216,7 +253,3 @@ bool randpool_save(void *data)
 {
 }
 
-uint8_t *randpool_load(void)
-{
-}
-