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