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.6 2007/02/09 09:24:38 asterix
17 *#* Typos. Add data_len in randpool_add and n_byte in randpool_push pototypes.
19 *#* Revision 1.3 2007/02/08 14:25:29 asterix
20 *#* Write static funcion push_byte.
27 #include <string.h> //memset(), memcpy();
28 #include <cfg/compiler.h>
29 #include <cfg/debug.h> //ASSERT()
30 #include <drv/timer.h> //timer_clock();
32 #include <stdio.h> //sprintf();
37 * Insert bytes in entropy pool, making a XOR of bytes present
40 static void randpool_push(EntrPool *pool, void *_byte, size_t n_byte)
42 size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
45 byte = (uint8_t *)_byte;
48 * Insert a bytes in entropy pool.
50 for(int j = 0; j < n_byte; j++)
52 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
53 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
56 pool->pos_add = i; // Update a insert bytes.
61 * Add n_bit of entropy in entropy pool.
63 void randpool_add(EntrPool *pool, void *data, size_t data_len, size_t entropy)
65 uint32_t event = timer_clock();
67 uint8_t sep[] = "\xaa\xaa\xaa\xaa"; // ??
69 randpool_push(pool, data, data_len); //Insert data to entropy pool.
71 randpool_push(pool, sep, strlen(sep)); // ??
73 /*Difference of time between a two accese to entropy pool.*/
74 delta = event - pool->last_counter;
76 randpool_push(pool, &delta, sizeof(delta));
80 randpool_push(pool, &delta, sizeof(delta));
83 * Count of number entropy bit add with delta.
91 pool->entropy = entropy; //Update a entropy of the pool.
92 pool->last_counter = event;
96 * This function stir entropy pool with MD2 function hash.
99 static void randpool_stir(EntrPool *pool)
101 size_t entropy = pool->entropy; //Save current calue of entropy.
103 uint8_t tmp_buf[(sizeof(size_t) * 2) + sizeof(int)]; //Temporary buffer.
107 randpool_add(pool, "", 0, 0);
109 for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
111 sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pos_add);
114 * Hash with MD2 algorithm the entropy pool.
116 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
118 md2_update(&context, tmp_buf, CONFIG_SIZE_ENTROPY_POOL);
120 /*Insert a message digest in entropy pool.*/
121 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
123 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Update a counter modulo 4.
127 /*Insert in pool the difference between a two call of this function (see above).*/
128 randpool_add(pool, "", 0, 0);
130 pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
134 void randpool_init(EntrPool *pool)
137 memset(pool, 0, sizeof(EntrPool));
138 pool->pos_get = CONFIG_MD2_BLOCK_LEN;
139 pool->last_counter = timer_clock();
141 //TODO: inizializzazione del timer di sistema.
146 * Get the actual value of entropy.
148 size_t randpool_size(EntrPool *pool)
150 return pool->entropy;
153 void randpool_get(EntrPool *pool, void *data, size_t n_byte)
158 void randpool_getN(EntrPool *pool, void *data, size_t n_byte)
162 bool randpool_save(void *data)
166 uint8_t *randpool_load(void)