From add74b1ad71f92ce018532c6d0614881ca47517e Mon Sep 17 00:00:00 2001 From: rasky Date: Tue, 28 Sep 2010 17:52:11 +0000 Subject: [PATCH] SEC: add generic interface for PRNGs. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4332 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/sec/prng.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 bertos/sec/prng.h diff --git a/bertos/sec/prng.h b/bertos/sec/prng.h new file mode 100644 index 00000000..7b39f5fa --- /dev/null +++ b/bertos/sec/prng.h @@ -0,0 +1,84 @@ +/** + * \file + * + * + * \brief Generic interface for cryptographically-secure pseudo-RNG + * \author Giovanni Bajo + * + */ + +#ifndef SEC_PRNG_H +#define SEC_PRNG_H + +#include +#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; +} 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 + * need such a property, use a normale PRGN like \a rand(). + */ +INLINE void prng_reseed(PRNG *ctx, const uint8_t *seed) +{ + ASSERT(ctx->reseed); + ctx->reseed(ctx, seed); +} + +/** + * Get the length of the seed in bytes required by this generator. + */ +INLINE size_t prng_seed_len(PRNG *ctx) +{ + return ctx->seed_len; +} + +/** + * Generate some cryptographically-secure random bytes into the specified + * buffer. + */ +INLINE void prng_generate(PRNG *ctx, uint8_t *data, size_t len) +{ + ASSERT(ctx->generate); + ctx->generate(ctx, data, len); +} + + +#endif /* SEC_PRNG_H */ -- 2.25.1