Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / algo / randpool.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief API function for to manage entropy pool.
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include "randpool.h"
39 #include "md2.h"
40
41 #include <cfg/compiler.h>
42 #include <cfg/debug.h>       //ASSERT()
43 #include <cfg/macros.h>      //MIN(), ROUND_UP();
44
45 #include <stdio.h>           //sprintf();
46 #include <string.h>          //memset(), memcpy();
47
48 #if CONFIG_RANDPOOL_TIMER
49         #include <drv/timer.h>       //timer_clock();
50 #endif
51
52
53
54 /*
55  * Insert bytes in entropy pool, making a XOR of bytes present
56  * in entropy pool.
57  */
58 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
59 {
60         size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
61         uint8_t *byte;
62
63         byte = (uint8_t *)_byte;
64
65         /*
66          * Insert a bytes in entropy pool.
67          */
68         for(size_t j = 0; j < n_byte; j++)
69         {
70                 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
71                 i++;
72                 i = i % CONFIG_SIZE_ENTROPY_POOL;
73         }
74
75         pool->pos_add  =  i; // Update a insert bytes.
76 }
77
78
79 /*
80  * This function stir entropy pool with MD2 function hash.
81  *
82  */
83 static void randpool_stir(EntropyPool *pool)
84 {
85         size_t entropy = pool->entropy; //Save current calue of entropy.
86         Md2Context context;
87         uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1]; //Temporary buffer.
88
89         md2_init(&context); //Init MD2 algorithm.
90
91         randpool_add(pool, NULL, 0);
92
93         for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
94         {
95                 sprintf((char *)tmp_buf, "%0x%0x%0x", pool->counter, i, pool->pos_add);
96
97                 /*
98                  * Hash with MD2 algorithm the entropy pool.
99                  */
100                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
101
102                 md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
103
104                 /*Insert a message digest in entropy pool.*/
105                 randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
106
107                 pool->counter = pool->counter + 1;
108
109         }
110
111         /*Insert in pool the difference between a two call of this function (see above).*/
112         randpool_add(pool, NULL, 0);
113
114         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
115 }
116
117 /**
118  * Add \param entropy bits from \param data buffer to the entropy \param pool
119  */
120 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
121 {
122         uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
123         size_t data_len = ROUND_UP(entropy, 8) / 8; //Number of entropy byte in input.
124
125         randpool_push(pool, data, data_len); //Insert data to entropy pool.
126
127 #if CONFIG_RANDPOOL_TIMER
128
129         ticks_t event = timer_clock();
130         ticks_t delta;
131
132         /*Difference of time between a two accese to entropy pool.*/
133         delta = event - pool->last_counter;
134
135         randpool_push(pool, &event, sizeof(ticks_t));
136         randpool_push(pool, sep, sizeof(sep) - 1); // ??
137         randpool_push(pool, &delta, sizeof(delta));
138
139         /*
140          * Count of number entropy bit add with delta.
141          */
142         delta = delta & 0xff;
143         while(delta)
144         {
145                 delta >>= 1;
146                 entropy++;
147         }
148
149         pool->last_counter = event;
150
151 #endif
152
153         pool->entropy += entropy;      //Update a entropy of the pool.
154 }
155
156 /**
157  * Randpool function initialization.
158  * The entropy pool can be initialize also with
159  * a previous entropy pool.
160  */
161 void randpool_init(EntropyPool *pool, void *_data, size_t len)
162 {
163         uint8_t *data;
164
165         data = (uint8_t *)_data;
166
167         memset(pool, 0, sizeof(EntropyPool));
168         pool->pos_get = MD2_DIGEST_LEN;
169
170 #if CONFIG_RANDPOOL_TIMER
171         pool->last_counter = timer_clock();
172 #endif
173
174         if(data)
175         {
176                 /*
177                  * Initialize a entropy pool with a
178                  * previous pool, and assume all pool as
179                  * entropy.
180                  */
181                 len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
182                 memcpy(pool->pool_entropy, data, len);
183                 pool->entropy = len;
184         }
185
186 }
187
188 /**
189  * Get the actual value of entropy.
190  */
191 size_t randpool_size(EntropyPool *pool)
192 {
193         return pool->entropy;
194 }
195
196 /**
197  * Get \param n_byte from entropy pool. If n_byte is larger than number
198  * byte of entropy in entropy pool, randpool_get continue
199  * to generate pseudocasual value from previous state of
200  * pool.
201  * \param n_byte number fo bytes to read.
202  * \param pool is the pool entropy context.
203  * \param _data is the pointer to write the random data to.
204  */
205 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
206 {
207         Md2Context context;
208         size_t i = pool->pos_get;
209         size_t n = n_byte;
210         size_t pos_write = 0;  //Number of block has been written in data.
211         size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
212         uint8_t *data;
213
214         data = (uint8_t *)_data;
215
216         /* Test if i + CONFIG_MD2_BLOCK_LEN  is inside of entropy pool.*/
217         ASSERT((MD2_DIGEST_LEN + i) <= CONFIG_SIZE_ENTROPY_POOL);
218
219         md2_init(&context);
220
221         while(n > 0)
222         {
223
224                 /*Hash previous state of pool*/
225                 md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
226
227                 memcpy(&data[pos_write], md2_end(&context), len);
228
229                 pos_write += len;   //Update number of block has been written in data.
230                 n -= len;           //Number of byte copied in data.
231
232                 len = MIN(n,(size_t)MD2_DIGEST_LEN);
233
234                 i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
235
236                 /* If we haven't more entropy pool to hash, we stir it.*/
237                 if(i < MD2_DIGEST_LEN)
238                 {
239                         randpool_stir(pool);
240                         i = pool->pos_get;
241                 }
242
243         }
244
245         pool->pos_get = i; //Current number of byte we get from pool.
246         pool->entropy -= n_byte; //Update a entropy.
247
248 }
249
250 /**
251  * Return a pointer to entropy pool.
252  */
253 uint8_t *randpool_pool(EntropyPool *pool)
254 {
255         return pool->pool_entropy;
256 }
257