From acf2f69a2106ed43e3737ad6a2f84bf69ade290f Mon Sep 17 00:00:00 2001 From: bg Date: Tue, 26 Jun 2007 04:10:46 +0000 Subject: [PATCH] prng improvement --- prng.c | 83 ++++++++++++++++++++++++++++++++++++---------------------- prng.h | 5 +++- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/prng.c b/prng.c index 078ed0d..7a2cdda 100644 --- a/prng.c +++ b/prng.c @@ -10,46 +10,47 @@ * rndCore is expanded to 512 bits for more security. * * \verbatim - * #################################################################################### - * # # - * # +---------------------------+ # - * # | | # - * # V | # - * # (concat) | # - * +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+ - * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block | - * +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+ - * # (xor) (xor) | # - * # ^ ^ | # - * # \ / | # - * # (offset)---------------------+ # - * # # - * #################################################################################### + * ################################################################################################ + * # # + * # +---------------------------+ # + * # | | +---+ # + * # V | | | # + * # (concat) | | V # + * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+ + * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block | + * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+ + * # (xor) (xor) | # + * # ^ ^ | # + * # \ / | # + * # (offset)---------------------+ # + * # # + * ################################################################################################ * \endverbatim */ /* \verbatim - * #################################################################################### - * # # - * # +---------------------------+ # - * # | | # - * # V | # - * # (concat) | # - * +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+ - * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block | - * +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+ - * # (xor) (xor) | # - * # ^ ^ | # - * # \ / | # - * # (offset)---------------------+ # - * # # - * #################################################################################### + * ################################################################################################ + * # # + * # +---------------------------+ # + * # | | +---+ # + * # V | | | # + * # (concat) | | V # + * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+ + * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block | + * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+ + * # (xor) (xor) | # + * # ^ ^ | # + * # \ / | # + * # (offset)---------------------+ # + * # # + * ################################################################################################ * \endverbatim */ #include #include #include "sha256.h" +#include "prng.h" /** * \brief secret entropy pool. @@ -78,7 +79,7 @@ void addEntropy(unsigned length, void* data){ sha256_nextBlock(&s, rndCore); while (length>=512){ sha256_nextBlock(&s, data); - data += 512/8; + data = (uint8_t*)data+ 512/8; length -= 512; } sha256_lastBlock(&s, data, length); @@ -106,6 +107,7 @@ void getRandomBlock(uint32_t *b){ } offset ^= 8; /* hehe */ memcpy(b, s.h, 32); /* back up first hash in b */ + ((uint8_t*)b)[*b&31]++; /* the important increment step */ sha256_init(&s); sha256_lastBlock(&s, b, 256); memcpy(b, s.h, 32); @@ -127,5 +129,24 @@ uint8_t getRandomByte(void){ } return block[i++]; } + +/*************************************************************************/ + +/** + * \brief This function fills the given bock with length random bytes + * @return a random byte + */ + +void fillBlockRandom(void* block, unsigned length){ + while(length>RANDOMBLOCK_SIZE){ + getRandomBlock(block); + block += RANDOMBLOCK_SIZE; + length -= RANDOMBLOCK_SIZE; + } + while(length){ + *((uint8_t*)block) = getRandomByte(); + ++block; --length; + } +} diff --git a/prng.h b/prng.h index 5266368..b2d3135 100644 --- a/prng.h +++ b/prng.h @@ -13,10 +13,13 @@ /* * length in bits */ +#define RANDOMBLOCK_SIZE 32 /* bytes */ + void addEntropy(unsigned length, void* data); void getRandomBlock(uint32_t* b); /* this does some simple buffering */ uint8_t getRandomByte(void); - + +void fillBlockRandom(void* block, unsigned length); #endif /*PRNG_H_*/