7 * \brief This file contains an implementaition of a pseudo-random-number generator.
10 * rndCore is expanded to 512 bits for more security.
13 * ################################################################################################
15 * # +---------------------------+ #
19 * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
20 * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
21 * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
25 * # (offset)---------------------+ #
27 * ################################################################################################
32 * ################################################################################################
34 * # +---------------------------+ #
38 * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
39 * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
40 * +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
44 * # (offset)---------------------+ #
46 * ################################################################################################
56 * \brief secret entropy pool.
57 * This is the core of the random which is generated
61 /*************************************************************************/
64 * \brief This function adds entropy to the central entropy pool
66 * @param length This ist the length of the random data in BITS.
67 * @param data This is the random data which should be added to the entropy pool
69 /* idea is: hash the message and add it via xor to rndCore
73 * we simply first "hash" rndCore, then entropy.
75 void addEntropy(unsigned length, void* data){
77 static uint8_t offset=0; /* selects if higher or lower half gets updated */
79 sha256_nextBlock(&s, rndCore);
81 sha256_nextBlock(&s, data);
82 data = (uint8_t*)data+ 512/8;
85 sha256_lastBlock(&s, data, length);
88 rndCore[i+offset] ^= s.h[i];
90 offset ^= 8; /* hehe */
93 /*************************************************************************/
95 * \brief This function fills a given buffer with 32 random bytes
96 * @param b Pointer to buffer wich is to fill
98 void getRandomBlock(uint32_t *b){
103 sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
106 rndCore[i+offset] ^= s.h[i];
108 offset ^= 8; /* hehe */
109 memcpy(b, s.h, 32); /* back up first hash in b */
110 ((uint8_t*)b)[*b&31]++; /* the important increment step */
112 sha256_lastBlock(&s, b, 256);
116 /*************************************************************************/
119 * \brief This function simply returns a random byte
120 * @return a random byte
122 uint8_t getRandomByte(void){
123 static uint8_t block[32];
127 getRandomBlock((void*)block);
133 /*************************************************************************/
136 * \brief This function fills the given bock with length random bytes
137 * @return a random byte
140 void fillBlockRandom(void* block, unsigned length){
141 while(length>RANDOMBLOCK_SIZE){
142 getRandomBlock(block);
143 block += RANDOMBLOCK_SIZE;
144 length -= RANDOMBLOCK_SIZE;
147 *((uint8_t*)block) = getRandomByte();