randpool.c

Go to the documentation of this file.
00001 
00039 #include "randpool.h"
00040 #include "md2.h"
00041 
00042 #include <cfg/compiler.h>
00043 #include <cfg/debug.h>       //ASSERT()
00044 #include <cfg/macros.h>      //MIN(), ROUND_UP();
00045 
00046 #include <stdio.h>           //sprintf();
00047 #include <string.h>          //memset(), memcpy();
00048 
00049 #if CONFIG_RANDPOOL_TIMER
00050     #include <drv/timer.h>       //timer_clock();
00051 #endif
00052 
00053 
00054 
00055 /*
00056  * Insert bytes in entropy pool, making a XOR of bytes present
00057  * in entropy pool.
00058  */
00059 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
00060 {
00061     size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
00062     uint8_t *byte;
00063 
00064     byte = (uint8_t *)_byte;
00065 
00066     /*
00067      * Insert a bytes in entropy pool.
00068      */
00069     for(size_t j = 0; j < n_byte; j++)
00070     {
00071         pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
00072         i++;
00073         i = i % CONFIG_SIZE_ENTROPY_POOL;
00074     }
00075 
00076     pool->pos_add  =  i; // Update a insert bytes.
00077 }
00078 
00079 
00080 /*
00081  * This function stir entropy pool with MD2 function hash.
00082  *
00083  */
00084 static void randpool_stir(EntropyPool *pool)
00085 {
00086     size_t entropy = pool->entropy; //Save current calue of entropy.
00087     Md2Context context;
00088     uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer.
00089 
00090     md2_init(&context); //Init MD2 algorithm.
00091 
00092     randpool_add(pool, NULL, 0);
00093 
00094     for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
00095     {
00096         sprintf((char *)tmp_buf, "%0x%0x%0x", pool->counter, i, pool->pos_add);
00097 
00098         /*
00099          * Hash with MD2 algorithm the entropy pool.
00100          */
00101         md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
00102 
00103         md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
00104 
00105         /*Insert a message digest in entropy pool.*/
00106         randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
00107 
00108         pool->counter = pool->counter + 1;
00109 
00110     }
00111 
00112     /*Insert in pool the difference between a two call of this function (see above).*/
00113     randpool_add(pool, NULL, 0);
00114 
00115     pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
00116 }
00117 
00121 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
00122 {
00123     uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
00124     size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input.
00125 
00126     randpool_push(pool, data, data_len); //Insert data to entropy pool.
00127 
00128 #if CONFIG_RANDPOOL_TIMER
00129 
00130     ticks_t event = timer_clock();
00131     ticks_t delta;
00132 
00133     /*Difference of time between a two accese to entropy pool.*/
00134     delta = event - pool->last_counter;
00135 
00136     randpool_push(pool, &event, sizeof(ticks_t));
00137     randpool_push(pool, sep, sizeof(sep) - 1); // ??
00138     randpool_push(pool, &delta, sizeof(delta));
00139 
00140     /*
00141      * Count of number entropy bit add with delta.
00142      */
00143     delta = delta & 0xff;
00144     while(delta)
00145     {
00146         delta >>= 1;
00147         entropy++;
00148     }
00149 
00150     pool->last_counter = event;
00151 
00152 #endif
00153 
00154     pool->entropy += entropy;      //Update a entropy of the pool.
00155 }
00156 
00162 void randpool_init(EntropyPool *pool, void *_data, size_t len)
00163 {
00164     uint8_t *data;
00165 
00166     data = (uint8_t *)_data;
00167 
00168     memset(pool, 0, sizeof(EntropyPool));
00169     pool->pos_get = MD2_DIGEST_LEN;
00170 
00171 #if CONFIG_RANDPOOL_TIMER
00172     pool->last_counter = timer_clock();
00173 #endif
00174 
00175     if(data)
00176     {
00177         /*
00178          * Initialize a entropy pool with a
00179          * previous pool, and assume all pool as
00180          * entropy.
00181          */
00182         len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
00183         memcpy(pool->pool_entropy, data, len);
00184         pool->entropy = len;
00185     }
00186 
00187 }
00188 
00192 size_t randpool_size(EntropyPool *pool)
00193 {
00194     return pool->entropy;
00195 }
00196 
00206 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
00207 {
00208     Md2Context context;
00209     size_t i = pool->pos_get;
00210     size_t n = n_byte;
00211     size_t pos_write = 0;  //Number of block has been written in data.
00212     size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
00213     uint8_t *data;
00214 
00215     data = (uint8_t *)_data;
00216 
00217     /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
00218     ASSERT((MD2_DIGEST_LEN + i) <= CONFIG_SIZE_ENTROPY_POOL);
00219 
00220     md2_init(&context);
00221 
00222     while(n > 0)
00223     {
00224 
00225         /*Hash previous state of pool*/
00226         md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
00227 
00228         memcpy(&data[pos_write], md2_end(&context), len);
00229 
00230         pos_write += len;   //Update number of block has been written in data.
00231         n -= len;           //Number of byte copied in data.
00232 
00233         len = MIN(n,(size_t)MD2_DIGEST_LEN);
00234 
00235         i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
00236 
00237         /* If we haven't more entropy pool to hash, we stir it.*/
00238         if(i < MD2_DIGEST_LEN)
00239         {
00240             randpool_stir(pool);
00241             i = pool->pos_get;
00242         }
00243 
00244     }
00245 
00246     pool->pos_get = i; //Current number of byte we get from pool.
00247     pool->entropy -= n_byte; //Update a entropy.
00248 
00249 }
00250 
00254 uint8_t *randpool_pool(EntropyPool *pool)
00255 {
00256     return pool->pool_entropy;
00257 }
00258