Remove randpool_load function. Add *data in randpool_init prototype.
[bertos.git] / algos / randpool.c
index b2d91727c6bb7162fe008edf0966a04af24e52c4..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.
+ *#*
  *#* Revision 1.6  2007/02/09 09:24:38  asterix
  *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
  *#*
 #include "randpool.h"
 #include "md2.h"
 
-#include <string.h>            //memset(), memcpy();
+#include <stdio.h>           //sprintf();
+#include <string.h>          //memset(), memcpy();
+
 #include <cfg/compiler.h>
-#include <cfg/debug.h>        //ASSERT()
-#include <drv/timer.h>        //timer_clock();
+#include <cfg/debug.h>       //ASSERT()
+#include <cfg/macros.h>      //MIN()
 
-#include <stdio.h>            //sprintf();
+#if CONFIG_RANDPOOL_TIMER
+       #include <drv/timer.h>       //timer_clock();
+#endif
 
 
 
@@ -37,7 +50,7 @@
  * Insert bytes in entropy pool, making a XOR of bytes present
  * in entropy pool.
  */
-static void randpool_push(EntrPool *pool, void *_byte, size_t n_byte)
+static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
 {
        size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
        uint8_t *byte;
@@ -57,22 +70,64 @@ static void randpool_push(EntrPool *pool, void *_byte, size_t n_byte)
 }
 
 
+/*
+ * This function stir entropy pool with MD2 function hash.
+ *
+ */
+static void randpool_stir(EntropyPool *pool)
+{
+       size_t entropy = pool->entropy; //Save current calue of entropy.
+       Md2Context context;
+       uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
+
+       md2_init(&context); //Init MD2 algorithm.
+
+       randpool_add(pool, "", 0, 0);
+
+       for (int i = 0; i < NUM_STIR_LOOP; i++)
+       {
+               sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, 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, strlen(tmp_buf));
+
+               /*Insert a message digest in entropy pool.*/
+               randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
+
+               pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
+
+       }
+
+       /*Insert in pool the difference between a two call of this function (see above).*/
+       randpool_add(pool, "", 0, 0);
+
+       pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
+}
+
 /**
  * Add n_bit of  entropy in entropy pool.
  */
-void randpool_add(EntrPool *pool, void *data, size_t data_len, size_t entropy)
+void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
 {
-       uint32_t event = timer_clock();
-       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;
@@ -88,82 +143,113 @@ void randpool_add(EntrPool *pool, void *data, size_t data_len, size_t entropy)
                entropy++;
        }
 
-       pool->entropy = entropy;      //Update a entropy of the pool.
+#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;
 }
 
-/* \
- * This function stir entropy pool with MD2 function hash.
- *
+/**
+ * Randpool function initialization.
+ * The entropy pool can be initialize also with 
+ * a previous entropy pool. 
  */
-static void randpool_stir(EntrPool *pool)
+void randpool_init(EntropyPool *pool, void *_data, size_t len)
 {
-       size_t entropy = pool->entropy; //Save current calue of entropy.
-       Md2Context context;
-       uint8_t tmp_buf[(sizeof(size_t) * 2) + sizeof(int)];  //Temporary buffer.
+       uint8_t *data;
 
-       md2_init(&context);
+       data = (uint8_t *)_data;
 
-       randpool_add(pool, "", 0, 0);
+       memset(pool, 0, sizeof(EntropyPool));
+       pool->pos_get = CONFIG_MD2_BLOCK_LEN;
 
-       for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
-       {
-               sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pos_add);
+#if CONFIG_RANDPOOL_TIMER
+       pool->last_counter = timer_clock();
+#endif
+
+       ASSERT(len < CONFIG_SIZE_ENTROPY_POOL);
 
+       if(len > 0)
+       {
                /*
-                * Hash with MD2 algorithm the entropy pool.
+                * Initialize a entropy pool with a 
+                * previous pool, and assume all pool as
+                * entropy.
                 */
-               md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
-
-               md2_update(&context, tmp_buf, CONFIG_SIZE_ENTROPY_POOL);
-
-               /*Insert a message digest in entropy pool.*/
-               randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
-
-               pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Update a counter modulo 4.
-
+               memcpy(pool->pool_entropy, data, len);
+               pool->entropy = len;
        }
 
-       /*Insert in pool the difference between a two call of this function (see above).*/
-       randpool_add(pool, "", 0, 0);
-
-       pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
-}
-
-
-void randpool_init(EntrPool *pool)
-{
-
-       memset(pool, 0, sizeof(EntrPool));
-       pool->pos_get = CONFIG_MD2_BLOCK_LEN;
-       pool->last_counter = timer_clock();
-
-       //TODO: inizializzazione del timer di sistema.
-
 }
 
 /**
  * Get the actual value of entropy.
  */
-size_t randpool_size(EntrPool *pool)
+size_t randpool_size(EntropyPool *pool)
 {
        return pool->entropy;
 }
 
-void randpool_get(EntrPool *pool, void *data, size_t n_byte)
+void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
 {
 
 }
 
-void randpool_getN(EntrPool *pool, void *data, size_t n_byte)
+/**
+ * Get n_byte from entropy pool. If n_byte is larger than number
+ * byte of entropy in entropy pool, rand_pool_getN continue
+ * to generate pseudocasual value from previous state of
+ * pool.
+ */
+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);
 
-bool randpool_save(void *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)
+       {
+               /*Hash previous state of pool*/
+               md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
+
+               memcpy(data, md2_end(&context), len);
+
+               n -= len; //Number of byte copied in data.
+
+               len = MIN(n, CONFIG_MD2_BLOCK_LEN);
+
+               i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL;
+
+               /* If we haven't more entropy pool to hash, we stir it.*/
+               if(i < CONFIG_MD2_BLOCK_LEN)
+               {
+                       randpool_stir(pool);
+                       i = pool->pos_get;
+               }
+       }
+       
+       pool->pos_get = i; //Current number of byte we get from pool.
+       
+       pool->entropy -= n_byte; //Update a entropy.
+
+       /*If we get all entropy entropy is 0*/
+       if(pool->entropy < 0) 
+               pool->entropy = 0;
 }
 
-uint8_t *randpool_load(void)
+bool randpool_save(void *data)
 {
 }