Write add_data and stir function. 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.4  2007/02/08 17:18:00  asterix
17  *#* Write add_data and stir function. Typos
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 push_byte(EntrPool *pool, void *_byte)
41 {
42         size_t i = pool->pool_pos_add; // Current number of byte insert in entropy pool.
43         size_t len_byte;
44         uint8_t *byte;
45
46                 
47         byte = (uint8_t *)_byte;
48         len_byte = strlen(byte);
49
50         /*
51          * Insert a bytes in entropy pool.
52          */
53         for(int j = 0; j < len_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->pool_pos_add  =  i; // Update a insert bytes.
60 }
61
62 /* 
63  * This function stir entropy pool with MD2 function hash.
64  *
65  */
66 static void stir(EntrPool *pool)
67 {
68         size_t entropy = pool->entropy; //Save current calue of entropy.
69         Md2Context context;
70         uint8_t tmp_buf[(sizeof(size_t) * 2) + sizeof(int)];  //Temporary buffer.
71
72         md2_init(&context);
73         
74         add_data(pool, "", 0);
75                 
76         for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / CONFIG_MD2_BLOCK_LEN); i++)
77         {
78                 sprintf(tmp_buf, "%x%x%x",pool->counter, i, pool->pool_pos_add); 
79                 
80                 /*
81                  * Hash with MD2 algorithm the entropy pool.
82                  */
83                 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
84                 
85                 md2_update(&context, tmp_buf, CONFIG_SIZE_ENTROPY_POOL);
86
87                 push_byte(pool, md2_end(&context)); //Insert a message digest in entropy pool.
88
89                 pool->counter = (pool->counter + 1) & 0xFFFFFFFF; //Update a counter modulo 4.
90         
91         }
92         
93         /*Insert in pool the difference between a two call of this function (see above).*/
94         add_data(pool, "", 0); 
95         
96         pool->entropy = entropy; //Restore old value of entropy. We haven't add entropy.
97 }
98
99
100 void init_pool(EntrPool *pool)
101 {
102         
103         memset(pool, 0, sizeof(EntrPool));
104
105         //TODO: inizializzazione del timer di sistema.
106
107 }
108
109 /**
110  * Add n_bit of  entropy in entropy pool.
111  */
112 void add_data(EntrPool *pool, void *data, size_t n_bit)
113 {
114         uint32_t event = timer_clock();
115         uint32_t delta;
116
117         push_byte(pool, data); //Insert data to entropy pool. 
118         
119         push_byte(pool, "\xaa\xaa\xaa\xaa"); // ??
120
121         /*Difference of time between a two accese to entropy pool.*/
122         delta = event - pool->last_counter;
123         
124         push_byte(pool, &delta);
125         
126         delta = delta & 0xff;
127         
128         push_byte(pool, &delta);
129
130         /*
131          * Count of number entropy bit add with delta.
132          */
133         while(delta) 
134         {
135                 delta >>= 1;
136                 n_bit++;
137         }
138         
139         pool->entropy = n_bit;      //Update a entropy of the pool.
140         pool->last_counter = event;
141 }
142
143 size_t pool_size(EntrPool *pool)
144 {
145 }
146
147 void get_bit(EntrPool *pool, void *data, size_t n_bit)
148 {
149 }
150
151 void get_bit_n(EntrPool *pool, void *data, size_t n_bit)
152 {
153 }
154
155 bool save_pool(void *data)
156 {
157 }
158
159 uint8_t *load_pool(void)
160 {
161 }
162