Write randpool_getN.
[bertos.git] / algos / randpool.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief API function for to manage entropy pool.
9  *
10  * \version $Id$
11  * \author Daniele Basile <asterix@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.8  2007/02/09 17:27:09  asterix
17  *#* Write randpool_getN.
18  *#*
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.
21  *#*
22  *#* Revision 1.3  2007/02/08 14:25:29  asterix
23  *#* Write static funcion push_byte.
24  *#*
25  *#*/
26
27 #include "randpool.h"
28 #include "md2.h"
29
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();
35
36 #include <stdio.h>           //sprintf();
37
38
39
40 /*
41  * Insert bytes in entropy pool, making a XOR of bytes present
42  * in entropy pool.
43  */
44 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
45 {
46         size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
47         uint8_t *byte;
48
49         byte = (uint8_t *)_byte;
50
51         /*
52          * Insert a bytes in entropy pool.
53          */
54         for(int j = 0; j < n_byte; j++)
55         {
56                 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
57                 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
58         }
59
60         pool->pos_add  =  i; // Update a insert bytes.
61 }
62
63
64 /*
65  * This function stir entropy pool with MD2 function hash.
66  *
67  */
68 static void randpool_stir(EntropyPool *pool)
69 {
70         size_t entropy = pool->entropy; //Save current calue of entropy.
71         Md2Context context;
72         uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
73
74         md2_init(&context); //Init MD2 algorithm.
75
76         randpool_add(pool, "", 0, 0);
77
78         for (int i = 0; i < NUM_STIR_LOOP; i++)
79         {
80                 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
81
82                 /*
83                  * Hash with MD2 algorithm the entropy pool.
84                  */
85                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
86
87                 md2_update(&context, tmp_buf, strlen(tmp_buf));
88
89                 /*Insert a message digest in entropy pool.*/
90                 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
91
92                 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
93
94         }
95
96         /*Insert in pool the difference between a two call of this function (see above).*/
97         randpool_add(pool, "", 0, 0);
98
99         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
100 }
101
102 /**
103  * Add n_bit of  entropy in entropy pool.
104  */
105 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
106 {
107         ticks_t event = timer_clock();
108         uint32_t delta;
109         uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
110
111         randpool_push(pool, data, data_len); //Insert data to entropy pool.
112
113         randpool_push(pool, sep, strlen(sep)); // ??
114
115         /*Difference of time between a two accese to entropy pool.*/
116         delta = event - pool->last_counter;
117
118         randpool_push(pool, &delta, sizeof(delta));
119
120         delta = delta & 0xff;
121
122         randpool_push(pool, &delta, sizeof(delta));
123
124         /*
125          * Count of number entropy bit add with delta.
126          */
127         while(delta)
128         {
129                 delta >>= 1;
130                 entropy++;
131         }
132
133         pool->entropy += entropy;      //Update a entropy of the pool.
134         pool->last_counter = event;
135 }
136
137
138 void randpool_init(EntropyPool *pool)
139 {
140
141         memset(pool, 0, sizeof(EntropyPool));
142         pool->pos_get = CONFIG_MD2_BLOCK_LEN;
143         pool->last_counter = timer_clock();
144
145         //TODO: inizializzazione del timer di sistema.
146
147 }
148
149 /**
150  * Get the actual value of entropy.
151  */
152 size_t randpool_size(EntropyPool *pool)
153 {
154         return pool->entropy;
155 }
156
157 void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
158 {
159
160 }
161
162 /**
163  * Get n_byte from entropy pool. If n_byte is larger than number
164  * byte of entropy in entropy pool, rand_pool_getN continue
165  * to generate pseudocasual value from previous state of
166  * pool.
167  */
168 void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
169 {
170         Md2Context context;
171         size_t i = pool->pos_get;
172         int n = n_byte;
173         size_t len = MIN((size_t)CONFIG_MD2_BLOCK_LEN, n_byte);
174
175         /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
176         ASSERT((CONFIG_MD2_BLOCK_LEN + i) < CONFIG_SIZE_ENTROPY_POOL);
177
178         md2_init(&context); 
179
180         while(n < 0)
181         {
182                 /*Hash previous state of pool*/
183                 md2_update(&context, &pool->pool_entropy[i], CONFIG_MD2_BLOCK_LEN);
184
185                 memcpy(data, md2_end(&context), len);
186
187                 n -= len; //Number of byte copied in data.
188
189                 len = MIN(n, CONFIG_MD2_BLOCK_LEN);
190
191                 i = (i + CONFIG_MD2_BLOCK_LEN) % CONFIG_SIZE_ENTROPY_POOL;
192
193                 /* If we haven't more entropy pool to hash, we stir it.*/
194                 if(i < CONFIG_MD2_BLOCK_LEN)
195                 {
196                         randpool_stir(pool);
197                         i = pool->pos_get;
198                 }
199         }
200         
201         pool->pos_get = i; //Current number of byte we get from pool.
202         
203         pool->entropy -= n_byte; //Update a entropy.
204
205         /*If we get all entropy entropy is 0*/
206         if(pool->entropy < 0) 
207                 pool->entropy = 0;
208 }
209
210 bool randpool_save(void *data)
211 {
212 }
213
214 uint8_t *randpool_load(void)
215 {
216 }
217