X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=prng.c;h=ad73bdda488dd690c4e26035ce26a0550e184ff8;hb=10f93dc9b969b20ed9efb480b7b2664a92bf43d2;hp=078ed0df24bce281ea6abd72fa228e2564841786;hpb=9b1bf59ca8d49db1a7495d95119c68d62b036c59;p=avr-crypto-lib.git diff --git a/prng.c b/prng.c index 078ed0d..ad73bdd 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 = (uint8_t*)block + RANDOMBLOCK_SIZE; + length -= RANDOMBLOCK_SIZE; + } + while(length){ + *((uint8_t*)block) = getRandomByte(); + block= (uint8_t*)block +1; --length; + } +}