randpool.c
Go to the documentation of this file.00001
00039 #include "randpool.h"
00040 #include "md2.h"
00041
00042 #include <cfg/compiler.h>
00043 #include <cfg/debug.h>
00044 #include <cfg/macros.h>
00045
00046 #include <stdio.h>
00047 #include <string.h>
00048
00049 #if CONFIG_RANDPOOL_TIMER
00050 #include <drv/timer.h>
00051 #endif
00052
00053
00054
00055
00056
00057
00058
00059 static void randpool_push(EntropyPool *pool, void *_byte, size_t n_byte)
00060 {
00061 size_t i = pool->pos_add;
00062 uint8_t *byte;
00063
00064 byte = (uint8_t *)_byte;
00065
00066
00067
00068
00069 for(size_t j = 0; j < n_byte; j++)
00070 {
00071 pool->pool_entropy[i] = pool->pool_entropy[i] ^ byte[j];
00072 i++;
00073 i = i % CONFIG_SIZE_ENTROPY_POOL;
00074 }
00075
00076 pool->pos_add = i;
00077 }
00078
00079
00080
00081
00082
00083
00084 static void randpool_stir(EntropyPool *pool)
00085 {
00086 size_t entropy = pool->entropy;
00087 Md2Context context;
00088 uint8_t tmp_buf[((sizeof(size_t) * 2) + sizeof(int)) * 2 + 1];
00089
00090 md2_init(&context);
00091
00092 randpool_add(pool, NULL, 0);
00093
00094 for (int i = 0; i < (CONFIG_SIZE_ENTROPY_POOL / MD2_DIGEST_LEN); i++)
00095 {
00096 sprintf((char *)tmp_buf, "%0x%0x%0x", pool->counter, i, pool->pos_add);
00097
00098
00099
00100
00101 md2_update(&context, pool->pool_entropy, CONFIG_SIZE_ENTROPY_POOL);
00102
00103 md2_update(&context, tmp_buf, sizeof(tmp_buf) - 1);
00104
00105
00106 randpool_push(pool, md2_end(&context), MD2_DIGEST_LEN);
00107
00108 pool->counter = pool->counter + 1;
00109
00110 }
00111
00112
00113 randpool_add(pool, NULL, 0);
00114
00115 pool->entropy = entropy;
00116 }
00117
00121 void randpool_add(EntropyPool *pool, void *data, size_t entropy)
00122 {
00123 uint8_t sep[] = "\xaa\xaa\xaa\xaa";
00124 size_t data_len = ROUND_UP(entropy, 8) / 8;
00125
00126 randpool_push(pool, data, data_len);
00127
00128 #if CONFIG_RANDPOOL_TIMER
00129
00130 ticks_t event = timer_clock();
00131 ticks_t delta;
00132
00133
00134 delta = event - pool->last_counter;
00135
00136 randpool_push(pool, &event, sizeof(ticks_t));
00137 randpool_push(pool, sep, sizeof(sep) - 1);
00138 randpool_push(pool, &delta, sizeof(delta));
00139
00140
00141
00142
00143 delta = delta & 0xff;
00144 while(delta)
00145 {
00146 delta >>= 1;
00147 entropy++;
00148 }
00149
00150 pool->last_counter = event;
00151
00152 #endif
00153
00154 pool->entropy += entropy;
00155 }
00156
00162 void randpool_init(EntropyPool *pool, void *_data, size_t len)
00163 {
00164 uint8_t *data;
00165
00166 data = (uint8_t *)_data;
00167
00168 memset(pool, 0, sizeof(EntropyPool));
00169 pool->pos_get = MD2_DIGEST_LEN;
00170
00171 #if CONFIG_RANDPOOL_TIMER
00172 pool->last_counter = timer_clock();
00173 #endif
00174
00175 if(data)
00176 {
00177
00178
00179
00180
00181
00182 len = MIN(len,(size_t)CONFIG_SIZE_ENTROPY_POOL);
00183 memcpy(pool->pool_entropy, data, len);
00184 pool->entropy = len;
00185 }
00186
00187 }
00188
00192 size_t randpool_size(EntropyPool *pool)
00193 {
00194 return pool->entropy;
00195 }
00196
00206 void randpool_get(EntropyPool *pool, void *_data, size_t n_byte)
00207 {
00208 Md2Context context;
00209 size_t i = pool->pos_get;
00210 size_t n = n_byte;
00211 size_t pos_write = 0;
00212 size_t len = MIN((size_t)MD2_DIGEST_LEN, n_byte);
00213 uint8_t *data;
00214
00215 data = (uint8_t *)_data;
00216
00217
00218 ASSERT((MD2_DIGEST_LEN + i) <= CONFIG_SIZE_ENTROPY_POOL);
00219
00220 md2_init(&context);
00221
00222 while(n > 0)
00223 {
00224
00225
00226 md2_update(&context, &pool->pool_entropy[i], MD2_DIGEST_LEN);
00227
00228 memcpy(&data[pos_write], md2_end(&context), len);
00229
00230 pos_write += len;
00231 n -= len;
00232
00233 len = MIN(n,(size_t)MD2_DIGEST_LEN);
00234
00235 i = (i + MD2_DIGEST_LEN) % CONFIG_SIZE_ENTROPY_POOL;
00236
00237
00238 if(i < MD2_DIGEST_LEN)
00239 {
00240 randpool_stir(pool);
00241 i = pool->pos_get;
00242 }
00243
00244 }
00245
00246 pool->pos_get = i;
00247 pool->entropy -= n_byte;
00248
00249 }
00250
00254 uint8_t *randpool_pool(EntropyPool *pool)
00255 {
00256 return pool->pool_entropy;
00257 }
00258