From f2108fe515132a85c7e84e588789f482f8feb750 Mon Sep 17 00:00:00 2001 From: rasky Date: Wed, 29 Sep 2010 15:37:11 +0000 Subject: [PATCH] SEC: make sure PRNGs are seeded before generating data. This allows init() function not to initialize seed-related memory without risking to incur into unwanted behaviour. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4355 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/sec/prng.h | 11 +++++++---- bertos/sec/prng/isaac.c | 1 + bertos/sec/prng/x917.c | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/bertos/sec/prng.h b/bertos/sec/prng.h index 7b39f5fa..d0d21b45 100644 --- a/bertos/sec/prng.h +++ b/bertos/sec/prng.h @@ -32,7 +32,7 @@ * * \brief Generic interface for cryptographically-secure pseudo-RNG * \author Giovanni Bajo - * + * */ #ifndef SEC_PRNG_H @@ -42,15 +42,16 @@ #include typedef struct PRNG -{ +{ void (*reseed)(struct PRNG *ctx, const uint8_t *seed); void (*generate)(struct PRNG *ctx, uint8_t *data, size_t len); - size_t seed_len; + uint8_t seed_len; + uint8_t seeded; } PRNG; /** * Feed a new seed into the PRNG. - * + * * \note: Being a cryptographically-secure PRNG, the seed will be * mixed to the current state of the generator, so it is NOT possible * to generate the same sequence simply by using the same seed. If you @@ -60,6 +61,7 @@ INLINE void prng_reseed(PRNG *ctx, const uint8_t *seed) { ASSERT(ctx->reseed); ctx->reseed(ctx, seed); + ctx->seeded = 1; } /** @@ -77,6 +79,7 @@ INLINE size_t prng_seed_len(PRNG *ctx) INLINE void prng_generate(PRNG *ctx, uint8_t *data, size_t len) { ASSERT(ctx->generate); + ASSERT(ctx->seeded); ctx->generate(ctx, data, len); } diff --git a/bertos/sec/prng/isaac.c b/bertos/sec/prng/isaac.c index 72d4a320..4e031c0b 100644 --- a/bertos/sec/prng/isaac.c +++ b/bertos/sec/prng/isaac.c @@ -173,6 +173,7 @@ void isaac_init(IsaacContext *ctx) ctx->prng.reseed = isaac_reseed; ctx->prng.generate = isaac_generate; ctx->prng.seed_len = sizeof(ctx->randrsl) / 2; + ctx->prng.seeded = 0; ctx->randcnt = CONFIG_ISAAC_RANDSIZ*4; memset(ctx->randrsl, 0, sizeof(ctx->randrsl)); diff --git a/bertos/sec/prng/x917.c b/bertos/sec/prng/x917.c index 2ffba3aa..63888c8f 100644 --- a/bertos/sec/prng/x917.c +++ b/bertos/sec/prng/x917.c @@ -123,4 +123,5 @@ void x917_init(X917Context *ctx) ctx->rng.reseed = x917_reseed; ctx->rng.generate = x917_generate; ctx->rng.seed_len = sizeof(ctx->key) + sizeof(ctx->state); + ctx->rng.seeded = 0; } -- 2.25.1