randpool.c

Go to the documentation of this file.
00001 
00039 /*#*
00040  *#* $Log$
00041  *#* Revision 1.20  2007/06/07 16:06:39  batt
00042  *#* Fix some doxygen errors.
00043  *#*
00044  *#* Revision 1.19  2007/02/15 13:54:26  asterix
00045  *#* Rename randpool_getN in randpool_get. Fix bug in randpool_get.
00046  *#*
00047  *#* Revision 1.17  2007/02/15 13:40:42  asterix
00048  *#* Fix bug in randpool_add and randpool_strir.
00049  *#*
00050  *#* Revision 1.16  2007/02/13 15:11:37  asterix
00051  *#* Typo.
00052  *#*
00053  *#* Revision 1.14  2007/02/13 09:57:12  asterix
00054  *#* Add directive #if in struct EntropyPool, and remove #else in randpool_add.
00055  *#*
00056  *#* Revision 1.13  2007/02/12 18:25:34  asterix
00057  *#* Fix bug in randpool_getN.
00058  *#*
00059  *#* Revision 1.12  2007/02/12 09:47:39  asterix
00060  *#* Remove randpool_save. Add randpool_pool.
00061  *#*
00062  *#* Revision 1.10  2007/02/12 09:03:32  asterix
00063  *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support
00064  *#*
00065  *#* Revision 1.9  2007/02/09 17:58:09  asterix
00066  *#* Add macro CONFIG_RANDPOOL_TIMER.
00067  *#*
00068  *#* Revision 1.6  2007/02/09 09:24:38  asterix
00069  *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
00070  *#*
00071  *#* Revision 1.3  2007/02/08 14:25:29  asterix
00072  *#* Write static funcion push_byte.
00073  *#*
00074  *#*/
00075 
00076 #include "randpool.h"
00077 #include "md2.h"
00078 
00079 #include <stdio.h>           //sprintf();
00080 #include <string.h>          //memset(), memcpy();
00081 
00082 #include <cfg/compiler.h>
00083 #include <cfg/debug.h>       //ASSERT()
00084 #include <cfg/macros.h>      //MIN(), ROUND_UP();
00085 
00086 #if CONFIG_RANDPOOL_TIMER
00087     #include <drv/timer.h>       //timer_clock();
00088 #endif
00089 
00090 
00091 
00092 /*
00093  * Insert bytes in entropy pool, making a XOR of bytes present
00094  * in entropy pool.
00095  */
00096 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
00097 {
00098     size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
00099     uint8_t *byte;
00100 
00101     byte = (uint8_t *)_byte;
00102 
00103     /*
00104      * Insert a bytes in entropy pool.
00105      */
00106     for(int j = 0; j < n_byte; j++)
00107     {
00108         pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
00109         i = (++i) % CONFIG_SIZE_ENTROPY_POOL;
00110     }
00111 
00112     pool->pos_add  =  i; // Update a insert bytes.
00113 }
00114 
00115 
00116 /*
00117  * This function stir entropy pool with MD2 function hash.
00118  *
00119  */
00120 static void randpool_stir(EntropyPool *pool)
00121 {
00122     size_t entropy = pool->entropy; //Save current calue of entropy.
00123     Md2Context context;
00124     uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer.
00125 
00126     md2_init(&context); //Init MD2 algorithm.
00127 
00128     randpool_add(pool, NULL, 0);
00129 
00130     for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
00131     {
00132         sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
00133 
00134         /*
00135          * Hash with MD2 algorithm the entropy pool.
00136          */
00137         md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
00138 
00139         md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
00140 
00141         /*Insert a message digest in entropy pool.*/
00142         randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
00143 
00144         pool->counter = pool->counter + 1; 
00145 
00146     }
00147 
00148     /*Insert in pool the difference between a two call of this function (see above).*/
00149     randpool_add(pool, NULL, 0);
00150 
00151     pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
00152 }
00153 
00157 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
00158 {
00159     uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
00160     size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input.
00161 
00162     randpool_push(pool, data, data_len); //Insert data to entropy pool.
00163 
00164 #if CONFIG_RANDPOOL_TIMER
00165 
00166     ticks_t event = timer_clock();
00167     ticks_t delta;
00168 
00169     /*Difference of time between a two accese to entropy pool.*/
00170     delta = event - pool->last_counter;
00171 
00172     randpool_push(pool, &event, sizeof(ticks_t));
00173     randpool_push(pool, sep, sizeof(sep) - 1); // ??
00174     randpool_push(pool, &delta, sizeof(delta));
00175 
00176     /*
00177      * Count of number entropy bit add with delta.
00178      */
00179     delta = delta & 0xff;
00180     while(delta)
00181     {
00182         delta >>= 1;
00183         entropy++;
00184     }
00185 
00186     pool->last_counter = event;
00187 
00188 #endif
00189 
00190     pool->entropy += entropy;      //Update a entropy of the pool.
00191 }
00192 
00198 void randpool_init(EntropyPool *pool, void *_data, size_t len)
00199 {
00200     uint8_t *data;
00201 
00202     data = (uint8_t *)_data;
00203 
00204     memset(pool, 0, sizeof(EntropyPool));
00205     pool->pos_get = MD2_DIGEST_LEN;
00206 
00207 #if CONFIG_RANDPOOL_TIMER
00208     pool->last_counter = timer_clock();
00209 #endif
00210 
00211     if(data)
00212     {
00213         /*
00214          * Initialize a entropy pool with a 
00215          * previous pool, and assume all pool as
00216          * entropy.
00217          */
00218         len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
00219         memcpy(pool->pool_entropy, data, len);
00220         pool->entropy = len;
00221     }
00222 
00223 }
00224 
00228 size_t randpool_size(EntropyPool *pool)
00229 {
00230     return pool->entropy;
00231 }
00232 
00242 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
00243 {
00244     Md2Context context;
00245     size_t i = pool->pos_get;
00246     size_t n = n_byte;
00247     size_t pos_write = 0;  //Number of block has been written in data.
00248     size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
00249     uint8_t *data;
00250 
00251     data = (uint8_t *)_data;
00252 
00253     /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
00254     ASSERT((MD2_DIGEST_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
00255 
00256     md2_init(&context);
00257 
00258     while(n > 0)
00259     {
00260 
00261         /*Hash previous state of pool*/
00262         md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
00263 
00264         memcpy(&data[pos_write], md2_end(&context), len);
00265 
00266         pos_write += len;   //Update number of block has been written in data.
00267         n -= len;           //Number of byte copied in data.
00268 
00269         len = MIN(n,(size_t)MD2_DIGEST_LEN);
00270 
00271         i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
00272 
00273         /* If we haven't more entropy pool to hash, we stir it.*/
00274         if(i < MD2_DIGEST_LEN)
00275         {
00276             randpool_stir(pool);
00277             i = pool->pos_get;
00278         }
00279 
00280     }
00281 
00282     pool->pos_get = i; //Current number of byte we get from pool.
00283     pool->entropy -= n_byte; //Update a entropy.
00284 
00285     /*If we get all entropy entropy is 0*/
00286     if(pool->entropy < 0) 
00287         pool->entropy = 0;
00288 
00289 }
00290 
00294 uint8_t *randpool_pool(EntropyPool *pool)
00295 {
00296     return pool->pool_entropy;
00297 }
00298