Fix bug in randpool_getN.
[bertos.git] / algos / randpool.c
index 029f5ba10406832051e13ccaf3fec5a9050fe53c..6881bc4896fb5943f59604ab416e138655e90330 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.13  2007/02/12 18:25:34  asterix
+ *#* Fix bug in randpool_getN.
+ *#*
+ *#* Revision 1.12  2007/02/12 09:47:39  asterix
+ *#* Remove randpool_save. Add randpool_pool.
+ *#*
+ *#* 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 +116,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 +146,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;
+       }
 
 }
 
@@ -170,20 +210,24 @@ void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
  * to generate pseudocasual value from previous state of
  * pool.
  */
-void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
+void randpool_getN(EntropyPool *pool, void *_data, size_t n_byte)
 {
        Md2Context context;
        size_t i = pool->pos_get;
        int n = n_byte;
        size_t len = MIN((size_t)CONFIG_MD2_BLOCK_LEN, n_byte);
+       uint8_t *data;
+
+       data = (uint8_t *)_data;
 
        /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
        ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
 
        md2_init(&context); 
 
-       while(n < 0)
+       while(n > 0)
        {
+
                /*Hash previous state of pool*/
                md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
 
@@ -210,13 +254,14 @@ void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
        /*If we get all entropy entropy is 0*/
        if(pool->entropy < 0) 
                pool->entropy = 0;
-}
 
-bool randpool_save(void *data)
-{
 }
 
-uint8_t *randpool_load(void)
+/**
+ * Return a pointer to entropy pool.
+ */
+uint8_t *randpool_pool(EntropyPool *pool)
 {
+       return pool->pool_entropy;
 }