4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief API function for to manage entropy pool.
11 * \author Daniele Basile <asterix@develer.com>
16 *#* Revision 1.9 2007/02/09 17:58:09 asterix
17 *#* Add macro CONFIG_RANDPOOL_TIMER.
19 *#* Revision 1.6 2007/02/09 09:24:38 asterix
20 *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
22 *#* Revision 1.3 2007/02/08 14:25:29 asterix
23 *#* Write static funcion push_byte.
30 #include <string.h> //memset(), memcpy();
31 #include <cfg/compiler.h>
32 #include <cfg/debug.h> //ASSERT()
33 #include <cfg/macros.h> //MIN()
34 #include <drv/timer.h> //timer_clock();
36 #include <stdio.h> //sprintf();
39 #if CONFIG_RANDPOOL_TIMER
40 #define TIMER() timer_clock()
42 #define TIMER() 0 //TODO:
46 * Insert bytes in entropy pool, making a XOR of bytes present
49 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
51 size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
54 byte = (uint8_t *)_byte;
57 * Insert a bytes in entropy pool.
59 for(int j = 0; j < n_byte; j++)
61 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
62 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
65 pool->pos_add = i; // Update a insert bytes.
70 * This function stir entropy pool with MD2 function hash.
73 static void randpool_stir(EntropyPool *pool)
75 size_t entropy = pool->entropy; //Save current calue of entropy.
77 uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
79 md2_init(&context); //Init MD2 algorithm.
81 randpool_add(pool, "", 0, 0);
83 for (int i = 0; i < NUM_STIR_LOOP; i++)
85 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
88 * Hash with MD2 algorithm the entropy pool.
90 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
92 md2_update(&context, tmp_buf, strlen(tmp_buf));
94 /*Insert a message digest in entropy pool.*/
95 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
97 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
101 /*Insert in pool the difference between a two call of this function (see above).*/
102 randpool_add(pool, "", 0, 0);
104 pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
108 * Add n_bit of entropy in entropy pool.
110 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
112 ticks_t event = TIMER();
114 uint8_t sep[] = "\xaa\xaa\xaa\xaa"; // ??
116 randpool_push(pool, data, data_len); //Insert data to entropy pool.
118 randpool_push(pool, sep, strlen(sep)); // ??
120 /*Difference of time between a two accese to entropy pool.*/
121 delta = event - pool->last_counter;
123 randpool_push(pool, &delta, sizeof(delta));
125 delta = delta & 0xff;
127 randpool_push(pool, &delta, sizeof(delta));
130 * Count of number entropy bit add with delta.
138 pool->entropy += entropy; //Update a entropy of the pool.
139 pool->last_counter = event;
143 void randpool_init(EntropyPool *pool)
146 memset(pool, 0, sizeof(EntropyPool));
147 pool->pos_get = CONFIG_MD2_BLOCK_LEN;
148 pool->last_counter = TIMER();
150 //TODO: inizializzazione del timer di sistema.
155 * Get the actual value of entropy.
157 size_t randpool_size(EntropyPool *pool)
159 return pool->entropy;
162 void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
168 * Get n_byte from entropy pool. If n_byte is larger than number
169 * byte of entropy in entropy pool, rand_pool_getN continue
170 * to generate pseudocasual value from previous state of
173 void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
176 size_t i = pool->pos_get;
178 size_t len = MIN((size_t)CONFIG_MD2_BLOCK_LEN, n_byte);
180 /* Test if i + CONFIG_MD2_BLOCK_LEN is inside of entropy pool.*/
181 ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
187 /*Hash previous state of pool*/
188 md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
190 memcpy(data, md2_end(&context), len);
192 n -= len; //Number of byte copied in data.
194 len = MIN(n, CONFIG_MD2_BLOCK_LEN);
196 i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL;
198 /* If we haven't more entropy pool to hash, we stir it.*/
199 if(i < CONFIG_MD2_BLOCK_LEN)
206 pool->pos_get = i; //Current number of byte we get from pool.
208 pool->entropy -= n_byte; //Update a entropy.
210 /*If we get all entropy entropy is 0*/
211 if(pool->entropy < 0)
215 bool randpool_save(void *data)
219 uint8_t *randpool_load(void)