Fix bug in randpool_stir and randpool_add. Typos.
[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.7  2007/02/09 15:49:54  asterix
17  *#* Fix bug in randpool_stir and randpool_add. Typos.
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 <drv/timer.h>        //timer_clock();
34
35 #include <stdio.h>            //sprintf();
36
37
38
39 /*
40  * Insert bytes in entropy pool, making a XOR of bytes present
41  * in entropy pool.
42  */
43 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
44 {
45         size_t i = pool->pos_add; // Current number of byte insert in entropy pool.
46         uint8_t *byte;
47
48         byte = (uint8_t *)_byte;
49
50         /*
51          * Insert a bytes in entropy pool.
52          */
53         for(int j = 0; j < n_byte; j++)
54         {
55                 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
56                 i = (i++) % CONFIG_SIZE_ENTROPY_POOL;
57         }
58
59         pool->pos_add  =  i; // Update a insert bytes.
60 }
61
62
63 /**
64  * Add n_bit of  entropy in entropy pool.
65  */
66 void randpool_add(EntropyPool *pool, void *data, size_t data_len, size_t entropy)
67 {
68         ticks_t event = timer_clock();
69         uint32_t delta;
70         uint8_t sep[] = "\xaa\xaa\xaa\xaa";  // ??
71
72         randpool_push(pool, data, data_len); //Insert data to entropy pool.
73
74         randpool_push(pool, sep, strlen(sep)); // ??
75
76         /*Difference of time between a two accese to entropy pool.*/
77         delta = event - pool->last_counter;
78
79         randpool_push(pool, &delta, sizeof(delta));
80
81         delta = delta & 0xff;
82
83         randpool_push(pool, &delta, sizeof(delta));
84
85         /*
86          * Count of number entropy bit add with delta.
87          */
88         while(delta)
89         {
90                 delta >>= 1;
91                 entropy++;
92         }
93
94         pool->entropy += entropy;      //Update a entropy of the pool.
95         pool->last_counter = event;
96 }
97
98 /* \
99  * This function stir entropy pool with MD2 function hash.
100  *
101  */
102 static void randpool_stir(EntropyPool *pool)
103 {
104         size_t entropy = pool->entropy; //Save current calue of entropy.
105         Md2Context context;
106         uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2]; //Temporary buffer.
107
108         md2_init(&context); //Init MD2 algorithm.
109
110         randpool_add(pool, "", 0, 0);
111
112         for (int i = 0; i < NUM_STIR_LOOP; i++)
113         {
114                 sprintf(tmp_buf, "%0x%0x%0x",pool->counter, i, pool->pos_add);
115
116                 /*
117                  * Hash with MD2 algorithm the entropy pool.
118                  */
119                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
120
121                 md2_update(&context, tmp_buf, strlen(tmp_buf));
122
123                 /*Insert a message digest in entropy pool.*/
124                 randpool_push(pool, md2_end(&context), CONFIG_MD2_BLOCK_LEN);
125
126                 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Clamp a counter to 4 byte.
127
128         }
129
130         /*Insert in pool the difference between a two call of this function (see above).*/
131         randpool_add(pool, "", 0, 0);
132
133         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
134 }
135
136
137 void randpool_init(EntropyPool *pool)
138 {
139
140         memset(pool, 0, sizeof(EntropyPool));
141         pool->pos_get = CONFIG_MD2_BLOCK_LEN;
142         pool->last_counter = timer_clock();
143
144         //TODO: inizializzazione del timer di sistema.
145
146 }
147
148 /**
149  * Get the actual value of entropy.
150  */
151 size_t randpool_size(EntropyPool *pool)
152 {
153         return pool->entropy;
154 }
155
156 void randpool_get(EntropyPool *pool, void *data, size_t n_byte)
157 {
158
159 }
160
161 void randpool_getN(EntropyPool *pool, void *data, size_t n_byte)
162 {
163 }
164
165 bool randpool_save(void *data)
166 {
167 }
168
169 uint8_t *randpool_load(void)
170 {
171 }
172