prng improvement

This commit is contained in:
bg 2007-06-26 04:10:46 +00:00
parent 79c9a6582b
commit acf2f69a21
2 changed files with 56 additions and 32 deletions

83
prng.c
View File

@ -10,46 +10,47 @@
* rndCore is expanded to 512 bits for more security. * rndCore is expanded to 512 bits for more security.
* *
* \verbatim * \verbatim
* #################################################################################### * ################################################################################################
* # # * # #
* # +---------------------------+ # * # +---------------------------+ #
* # | | # * # | | +---+ #
* # V | # * # V | | | #
* # (concat) | # * # (concat) | | V #
* +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+ * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
* | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block | * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
* +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+ * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
* # (xor) (xor) | # * # (xor) (xor) | #
* # ^ ^ | # * # ^ ^ | #
* # \ / | # * # \ / | #
* # (offset)---------------------+ # * # (offset)---------------------+ #
* # # * # #
* #################################################################################### * ################################################################################################
* \endverbatim * \endverbatim
*/ */
/* \verbatim /* \verbatim
* #################################################################################### * ################################################################################################
* # # * # #
* # +---------------------------+ # * # +---------------------------+ #
* # | | # * # | | +---+ #
* # V | # * # V | | | #
* # (concat) | # * # (concat) | | V #
* +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+ * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
* | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block | * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
* +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+ * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
* # (xor) (xor) | # * # (xor) (xor) | #
* # ^ ^ | # * # ^ ^ | #
* # \ / | # * # \ / | #
* # (offset)---------------------+ # * # (offset)---------------------+ #
* # # * # #
* #################################################################################### * ################################################################################################
* \endverbatim * \endverbatim
*/ */
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "sha256.h" #include "sha256.h"
#include "prng.h"
/** /**
* \brief secret entropy pool. * \brief secret entropy pool.
@ -78,7 +79,7 @@ void addEntropy(unsigned length, void* data){
sha256_nextBlock(&s, rndCore); sha256_nextBlock(&s, rndCore);
while (length>=512){ while (length>=512){
sha256_nextBlock(&s, data); sha256_nextBlock(&s, data);
data += 512/8; data = (uint8_t*)data+ 512/8;
length -= 512; length -= 512;
} }
sha256_lastBlock(&s, data, length); sha256_lastBlock(&s, data, length);
@ -106,6 +107,7 @@ void getRandomBlock(uint32_t *b){
} }
offset ^= 8; /* hehe */ offset ^= 8; /* hehe */
memcpy(b, s.h, 32); /* back up first hash in b */ memcpy(b, s.h, 32); /* back up first hash in b */
((uint8_t*)b)[*b&31]++; /* the important increment step */
sha256_init(&s); sha256_init(&s);
sha256_lastBlock(&s, b, 256); sha256_lastBlock(&s, b, 256);
memcpy(b, s.h, 32); memcpy(b, s.h, 32);
@ -127,5 +129,24 @@ uint8_t getRandomByte(void){
} }
return block[i++]; 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;
}
}

5
prng.h
View File

@ -13,10 +13,13 @@
/* /*
* length in bits * length in bits
*/ */
#define RANDOMBLOCK_SIZE 32 /* bytes */
void addEntropy(unsigned length, void* data); void addEntropy(unsigned length, void* data);
void getRandomBlock(uint32_t* b); void getRandomBlock(uint32_t* b);
/* this does some simple buffering */ /* this does some simple buffering */
uint8_t getRandomByte(void); uint8_t getRandomByte(void);
void fillBlockRandom(void* block, unsigned length);
#endif /*PRNG_H_*/ #endif /*PRNG_H_*/