X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=algos%2Frandpool.c;h=735bd06e9447007a110a795eaf5a386713705b6e;hb=5f3952176a4e9a00ca8dd5ec4a6b994958f89e0a;hp=63a2716a6fa8a9b89145e1aef5b34eed1f1c0984;hpb=bd5a6de1603564daa55869e9dc524bb20be79556;p=bertos.git diff --git a/algos/randpool.c b/algos/randpool.c old mode 100755 new mode 100644 index 63a2716a..735bd06e --- a/algos/randpool.c +++ b/algos/randpool.c @@ -13,8 +13,26 @@ /*#* *#* $Log$ - *#* Revision 1.11 2007/02/12 09:40:43 asterix - *#* Remove randpool_load function. Add *data in randpool_init prototype. + *#* Revision 1.20 2007/06/07 16:06:39 batt + *#* Fix some doxygen errors. + *#* + *#* Revision 1.19 2007/02/15 13:54:26 asterix + *#* Rename randpool_getN in randpool_get. Fix bug in randpool_get. + *#* + *#* Revision 1.17 2007/02/15 13:40:42 asterix + *#* Fix bug in randpool_add and randpool_strir. + *#* + *#* Revision 1.16 2007/02/13 15:11:37 asterix + *#* Typo. + *#* + *#* Revision 1.14 2007/02/13 09:57:12 asterix + *#* Add directive #if in struct EntropyPool, and remove #else in randpool_add. + *#* + *#* Revision 1.13 2007/02/12 18:25:34 asterix + *#* Fix bug in randpool_getN. + *#* + *#* Revision 1.12 2007/02/12 09:47:39 asterix + *#* Remove randpool_save. Add randpool_pool. *#* *#* Revision 1.10 2007/02/12 09:03:32 asterix *#* Add CONFIG_RANDPOOL_TIMER macro to swich on or off timer support @@ -38,7 +56,7 @@ #include #include //ASSERT() -#include //MIN() +#include //MIN(), ROUND_UP(); #if CONFIG_RANDPOOL_TIMER #include //timer_clock(); @@ -63,7 +81,7 @@ static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte) for(int j = 0; j < n_byte; j++) { pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j]; - i = (i++) % CONFIG_SIZE_ENTROPY_POOL; + i = (++i) % CONFIG_SIZE_ENTROPY_POOL; } pool->pos_add = i; // Update a insert bytes. @@ -78,13 +96,13 @@ 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. + uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer. md2_init(&context); //Init MD2 algorithm. - randpool_add(pool, "", 0, 0); + randpool_add(pool, NULL, 0); - for (int i = 0; i < NUM_STIR_LOOP; i++) + for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++) { sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add); @@ -93,66 +111,58 @@ static void randpool_stir(EntropyPool *pool) */ md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL); - md2_update(&context, tmp_buf, strlen(tmp_buf)); + md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1); /*Insert a message digest in entropy pool.*/ - randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN); + randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN); - pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte. + pool->counter = pool->counter + 1; } /*Insert in pool the difference between a two call of this function (see above).*/ - randpool_add(pool, "", 0, 0); + randpool_add(pool, NULL, 0); pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy. } /** - * Add n_bit of entropy in entropy pool. + * Add \param entropy bits from \param data buffer to the entropy \param pool */ -void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy) +void randpool_add(EntropyPool *pool, void *data, size_t entropy) { uint8_t sep[] = "\xaa\xaa\xaa\xaa"; // ?? + size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input. 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; + ticks_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; - + randpool_push(pool, &event, sizeof(ticks_t)); + randpool_push(pool, sep, sizeof(sep) - 1); // ?? randpool_push(pool, &delta, sizeof(delta)); /* * Count of number entropy bit add with delta. */ + delta = delta & 0xff; while(delta) { delta >>= 1; entropy++; } -#else - size_t event = 0; - - /*Difference of time between a two accese to entropy pool.*/ - event = pool->last_counter++; + pool->last_counter = event; #endif pool->entropy += entropy; //Update a entropy of the pool. - pool->last_counter = event; } /** @@ -167,21 +177,20 @@ void randpool_init(EntropyPool *pool, void *_data, size_t len) data = (uint8_t *)_data; memset(pool, 0, sizeof(EntropyPool)); - pool->pos_get = CONFIG_MD2_BLOCK_LEN; + pool->pos_get = MD2_DIGEST_LEN; #if CONFIG_RANDPOOL_TIMER pool->last_counter = timer_clock(); #endif - ASSERT(len < CONFIG_SIZE_ENTROPY_POOL); - - if(len > 0) + if(data) { /* * Initialize a entropy pool with a * previous pool, and assume all pool as * entropy. */ + len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL); memcpy(pool->pool_entropy, data, len); pool->entropy = len; } @@ -196,60 +205,69 @@ size_t randpool_size(EntropyPool *pool) return pool->entropy; } -void randpool_get(EntropyPool *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 + * Get \param n_byte from entropy pool. If n_byte is larger than number + * byte of entropy in entropy pool, randpool_get continue * to generate pseudocasual value from previous state of * pool. + * \param n_byte number fo bytes to read. + * \param pool is the pool entropy context. + * \param _data is the pointer to write the random data to. */ -void randpool_getN(EntropyPool *pool, void *data, size_t n_byte) +void randpool_get(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); + size_t n = n_byte; + size_t pos_write = 0; //Number of block has been written in data. + size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte); + uint8_t *data; + + data = (uint8_t *)_data; /* Test if i + CONFIG_MD2_BLOCK_LEN is inside of entropy pool.*/ - ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL); + ASSERT((MD2_DIGEST_LEN + i) < CONFIG_SIZE_ENTROPY_POOL); - md2_init(&context); + md2_init(&context); - while(n < 0) + while(n > 0) { + /*Hash previous state of pool*/ - md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN); + md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN); - memcpy(data, md2_end(&context), len); + memcpy(&data[pos_write], md2_end(&context), len); - n -= len; //Number of byte copied in data. + pos_write += len; //Update number of block has been written in data. + n -= len; //Number of byte copied in data. - len = MIN(n, CONFIG_MD2_BLOCK_LEN); + len = MIN(n,(size_t)MD2_DIGEST_LEN); - i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL; + i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL; /* If we haven't more entropy pool to hash, we stir it.*/ - if(i < CONFIG_MD2_BLOCK_LEN) + if(i < MD2_DIGEST_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; + } -bool randpool_save(void *data) +/** + * Return a pointer to entropy pool. + */ +uint8_t *randpool_pool(EntropyPool *pool) { + return pool->pool_entropy; }