X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fsec%2Frandom.c;h=67c24859529d9a2daee538418b0e8f3a7b062083;hb=4d8a6a97c6c0e15296b51d5f02674079bede1aa0;hp=2e3877b0596476e827e2ed4f5a232f8ad9a8b329;hpb=7450404e32fd6143a16c14adf3bcdc5ca724734c;p=bertos.git diff --git a/bertos/sec/random.c b/bertos/sec/random.c index 2e3877b0..67c24859 100644 --- a/bertos/sec/random.c +++ b/bertos/sec/random.c @@ -46,18 +46,21 @@ #include #include #include +#include +#include +#include /********************************************************************************/ /* Configuration of the random module */ /********************************************************************************/ -#define POOL_CONTEXT PP_CAT(PP_CAT(PRNG_NAME, CONFIG_RANDOM_POOL), _Context) -#define POOL_INIT PP_CAT(PP_CAT(PRNG_NAME, CONFIG_RANDOM_POOL), _init) +#define POOL_CONTEXT PP_CAT(PP_CAT(POOL_NAMEU, CONFIG_RANDOM_POOL), Context) +#define POOL_INIT PP_CAT(PP_CAT(POOL_NAMEL, CONFIG_RANDOM_POOL), _init) #define EXTRACTOR_STACKINIT PP_CAT(PP_CAT(EXTRACTOR_NAME, CONFIG_RANDOM_EXTRACTOR), _stackinit) -#define PRNG_CONTEXT PP_CAT(PP_CAT(PRNG_NAME, CONFIG_RANDOM_PRNG), _Context) -#define PRNG_INIT PP_CAT(PP_CAT(PRNG_NAME, CONFIG_RANDOM_PRNG), _init) +#define PRNG_CONTEXT PP_CAT(PP_CAT(PRNG_NAMEU, CONFIG_RANDOM_PRNG), Context) +#define PRNG_INIT PP_CAT(PP_CAT(PRNG_NAMEL, CONFIG_RANDOM_PRNG), _init) /********************************************************************************/ @@ -66,7 +69,7 @@ #if CONFIG_RANDOM_POOL != POOL_NONE static POOL_CONTEXT epool_ctx; -static EntropyPool_Context * const epool = (EntropyPool_Context *)&epool_ctx; +static EntropyPool * const epool = (EntropyPool *)&epool_ctx; #endif static PRNG_CONTEXT prng_ctx; @@ -81,11 +84,11 @@ static bool initialized = 0; /* * Reseed the PRNG if there is enough entropy available at this time. - * - * Some designs (eg: fortuna) suggest to artificially limit the frequency of + * + * Some designs (eg: fortuna) suggest to artificially limit the frequency of * this operation to something like 0.1s, to avoid attacks that try to exhaust * the entropy pool. - * + * * We don't believe such attacks are available in an embedded system (as an attacker * does not have a way to ask random numbers from the pool) but we will play safe * here in case eg. the user does something wrong. @@ -93,21 +96,21 @@ static bool initialized = 0; static void optional_reseeding(void) { #if CONFIG_RANDOM_POOL != POOL_NONE - static ticks_t last_reseed = 0; + static ticks_t last_reseed = -1000; - // We don't allow more than 10 reseedings per second + // We don't allow more than 10 reseedings per second // (as suggested by Fortuna) ticks_t current = timer_clock(); if (ticks_to_ms(current - last_reseed) < 100) return; - + if (entropy_seeding_ready(epool)) { uint8_t seed[prng_seed_len(prng)]; - + entropy_make_seed(epool, seed, sizeof(seed)); prng_reseed(prng, seed); - + last_reseed = current; PURGE(seed); } @@ -117,7 +120,7 @@ static void optional_reseeding(void) /* * Perform the initial seeding of the PRNG. - * + * * At startup, we want to immediately seed the PRNG to a point where it can * generate safe-enough random numbers. To do this, we rely on a hw-dependent * function to pull entropy from available hw sources, and then feed it @@ -134,7 +137,7 @@ static void initial_seeding(void) random_pull_entropy(buf, sizeof(buf)); entropy_add(epool, 0, buf, sizeof(buf), sizeof(buf)*8); } while (!entropy_seeding_ready(epool)); - + optional_reseeding(); #elif CONFIG_RANDOM_EXTRACTOR != EXTRACTOR_NONE