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.4 2007/02/08 17:18:00 asterix
17 *#* Write add_data and stir function. Typos
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 push_byte(EntrPool *pool, void *_byte)
42 size_t i = pool->pool_pos_add; // Current number of byte insert in entropy pool.
47 byte = (uint8_t *)_byte;
48 len_byte = strlen(byte);
51 * Insert a bytes in entropy pool.
53 for(int j = 0; j < len_byte; j++)
55 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
56 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
59 pool->pool_pos_add = i; // Update a insert bytes.
63 * This function stir entropy pool with MD2 function hash.
66 static void stir(EntrPool *pool)
68 size_t entropy = pool->entropy; //Save current calue of entropy.
70 uint8_t tmp_buf[(sizeof(size_t) * 2) + sizeof(int)]; //Temporary buffer.
74 add_data(pool, "", 0);
76 for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
78 sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pool_pos_add);
81 * Hash with MD2 algorithm the entropy pool.
83 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
85 md2_update(&context, tmp_buf, CONFIG_SIZE_ENTROPY_POOL);
87 push_byte(pool, md2_end(&context)); //Insert a message digest in entropy pool.
89 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Update a counter modulo 4.
93 /*Insert in pool the difference between a two call of this function (see above).*/
94 add_data(pool, "", 0);
96 pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
100 void init_pool(EntrPool *pool)
103 memset(pool, 0, sizeof(EntrPool));
105 //TODO: inizializzazione del timer di sistema.
110 * Add n_bit of entropy in entropy pool.
112 void add_data(EntrPool *pool, void *data, size_t n_bit)
114 uint32_t event = timer_clock();
117 push_byte(pool, data); //Insert data to entropy pool.
119 push_byte(pool, "\xaa\xaa\xaa\xaa"); // ??
121 /*Difference of time between a two accese to entropy pool.*/
122 delta = event - pool->last_counter;
124 push_byte(pool, &delta);
126 delta = delta & 0xff;
128 push_byte(pool, &delta);
131 * Count of number entropy bit add with delta.
139 pool->entropy = n_bit; //Update a entropy of the pool.
140 pool->last_counter = event;
143 size_t pool_size(EntrPool *pool)
147 void get_bit(EntrPool *pool, void *data, size_t n_bit)
151 void get_bit_n(EntrPool *pool, void *data, size_t n_bit)
155 bool save_pool(void *data)
159 uint8_t *load_pool(void)