Fix bug in randpool_stir and randpool_add. Typos.
[bertos.git] / algos / randpool.c
index e8c74229ae85c8af0117c66d75b308057d5ffd26..69be610d3aa2c2c2363dc3e387ba647726d96f30 100755 (executable)
 
 /*#*
  *#* $Log$
- *#* Revision 1.5  2007/02/08 17:21:51  asterix
- *#* Write pool_size function.
+ *#* Revision 1.7  2007/02/09 15:49:54  asterix
+ *#* Fix bug in randpool_stir and randpool_add. Typos.
+ *#*
+ *#* Revision 1.6  2007/02/09 09:24:38  asterix
+ *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
  *#*
  *#* Revision 1.3  2007/02/08 14:25:29  asterix
  *#* Write static funcion push_byte.
@@ -29,7 +32,7 @@
 #include <cfg/debug.h>        //ASSERT()
 #include <drv/timer.h>        //timer_clock();
 
-#include <stdio.h>   //sprintf();
+#include <stdio.h>            //sprintf();
 
 
 
  * Insert bytes in entropy pool, making a XOR of bytes present
  * in entropy pool.
  */
-static void push_byte(EntrPool *pool, void *_byte)
+static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
 {
-       size_t i = pool->pool_pos_add; // Current number of byte insert in entropy pool.
-       size_t len_byte;
+       size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
        uint8_t *byte;
 
-               
        byte = (uint8_t *)_byte;
-       len_byte = strlen(byte);
 
        /*
         * Insert a bytes in entropy pool.
         */
-       for(int j = 0; j < len_byte; j++)
+       for(int j = 0; j < n_byte; j++)
        {
                pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
                i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
        }
 
-       pool->pool_pos_add  =  i; // Update a insert bytes.
+       pool->pos_add  =  i; // Update a insert bytes.
+}
+
+
+/**
+ * Add n_bit of  entropy in entropy pool.
+ */
+void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
+{
+       ticks_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)); // ??
+
+       /*Difference of time between a two accese to entropy pool.*/
+       delta = event - pool->last_counter;
+
+       randpool_push(pool, &delta, sizeof(delta));
+
+       delta = delta & 0xff;
+
+       randpool_push(pool, &delta, sizeof(delta));
+
+       /*
+        * Count of number entropy bit add with delta.
+        */
+       while(delta)
+       {
+               delta >>= 1;
+               entropy++;
+       }
+
+       pool->entropy += entropy;      //Update a entropy of the pool.
+       pool->last_counter = event;
 }
 
-/* 
+/* \
  * This function stir entropy pool with MD2 function hash.
  *
  */
-static void stir(EntrPool *pool)
+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)];  //Temporary buffer.
+       uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
+
+       md2_init(&context); //Init MD2 algorithm.
 
-       md2_init(&context);
-       
-       add_data(pool, "", 0);
-               
-       for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
+       randpool_add(pool, "", 0, 0);
+
+       for (int i = 0; i < NUM_STIR_LOOP; i++)
        {
-               sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pool_pos_add); 
-               
+               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, CONFIG_SIZE_ENTROPY_POOL);
 
-               push_byte(pool, md2_end(&context)); //Insert a message digest in entropy pool.
+               md2_update(&context, tmp_buf, strlen(tmp_buf));
 
-               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.
-}
+               /*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.
 
-void init_pool(EntrPool *pool)
-{
-       
-       memset(pool, 0, sizeof(EntrPool));
+       }
 
-       //TODO: inizializzazione del timer di sistema.
+       /*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 add_data(EntrPool *pool, void *data, size_t n_bit)
+
+void randpool_init(EntropyPool *pool)
 {
-       uint32_t event = timer_clock();
-       uint32_t delta;
 
-       push_byte(pool, data); //Insert data to entropy pool. 
-       
-       push_byte(pool, "\xaa\xaa\xaa\xaa"); // ??
+       memset(pool, 0, sizeof(EntropyPool));
+       pool->pos_get = CONFIG_MD2_BLOCK_LEN;
+       pool->last_counter = timer_clock();
 
-       /*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);
+       //TODO: inizializzazione del timer di sistema.
 
-       /*
-        * 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;
 }
 
 /**
  * Get the actual value of entropy.
  */
-size_t pool_size(EntrPool *pool)
+size_t randpool_size(EntropyPool *pool)
 {
        return pool->entropy;
 }
 
-void get_bit(EntrPool *pool, void *data, size_t n_bit)
+void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
 {
+
 }
 
-void get_bit_n(EntrPool *pool, void *data, size_t n_bit)
+void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
 {
 }
 
-bool save_pool(void *data)
+bool randpool_save(void *data)
 {
 }
 
-uint8_t *load_pool(void)
+uint8_t *randpool_load(void)
 {
 }