From c59bf12270a66718595d4aeef78ca1ba4636b9ca Mon Sep 17 00:00:00 2001 From: asterix Date: Thu, 8 Feb 2007 17:18:01 +0000 Subject: [PATCH] Write add_data and stir function. Typos git-svn-id: https://src.develer.com/svnoss/bertos/trunk@770 38d2e660-2303-0410-9eaa-f027e97ec537 --- algos/randpool.c | 80 +++++++++++++++++++++++++++++++++++++++++++++--- algos/randpool.h | 5 ++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/algos/randpool.c b/algos/randpool.c index b7f090b9..92751dd7 100755 --- a/algos/randpool.c +++ b/algos/randpool.c @@ -13,6 +13,9 @@ /*#* *#* $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. *#* @@ -24,11 +27,11 @@ #include //memset(), memcpy(); #include #include //ASSERT() +#include //timer_clock(); + +#include //sprintf(); + -static void stir(EntrPool *pool) -{ - -} /* * Insert bytes in entropy pool, making a XOR of bytes present @@ -56,16 +59,85 @@ static void push_byte(EntrPool *pool, void *_byte) 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) diff --git a/algos/randpool.h b/algos/randpool.h index 02daafa2..e996fa3c 100755 --- a/algos/randpool.h +++ b/algos/randpool.h @@ -14,6 +14,9 @@ /*#* *#* $Log$ + *#* Revision 1.4 2007/02/08 17:18:01 asterix + *#* Write add_data and stir function. Typos + *#* *#* Revision 1.3 2007/02/08 14:25:56 asterix *#* Typos. *#* @@ -36,7 +39,7 @@ */ typedef struct EntrPool { - size_t entropy; ///< Actual value of entropy. + size_t entropy; ///< Actual value of entropy (In bit). size_t pool_pos_add; ///< Size of byte insert in entropy pool. size_t pool_pos_get; ///< Size of byte take in entropy pool. size_t counter; ///< Counter. -- 2.25.1