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 # +--------------+
20 * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
21 * +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+
25 * # (offset)---------------------+ #
27 * ####################################################################################
32 * ####################################################################################
34 * # +---------------------------+ #
38 * +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+
39 * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
40 * +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+
44 * # (offset)---------------------+ #
46 * ####################################################################################
55 * \brief secret entropy pool.
56 * This is the core of the random which is generated
60 /*************************************************************************/
63 * \brief This function adds entropy to the central entropy pool
65 * @param length This ist the length of the random data in BITS.
66 * @param data This is the random data which should be added to the entropy pool
68 /* idea is: hash the message and add it via xor to rndCore
72 * we simply first "hash" rndCore, then entropy.
74 void addEntropy(unsigned length, void* data){
76 static uint8_t offset=0; /* selects if higher or lower half gets updated */
78 sha256_nextBlock(&s, rndCore);
80 sha256_nextBlock(&s, data);
84 sha256_lastBlock(&s, data, length);
87 rndCore[i+offset] ^= s.h[i];
89 offset ^= 8; /* hehe */
92 /*************************************************************************/
94 * \brief This function fills a given buffer with 32 random bytes
95 * @param b Pointer to buffer wich is to fill
97 void getRandomBlock(uint32_t *b){
102 sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
105 rndCore[i+offset] ^= s.h[i];
107 offset ^= 8; /* hehe */
108 memcpy(b, s.h, 32); /* back up first hash in b */
110 sha256_lastBlock(&s, b, 256);
114 /*************************************************************************/
117 * \brief This function simply returns a random byte
118 * @return a random byte
120 uint8_t getRandomByte(void){
121 static uint8_t block[32];
125 getRandomBlock((void*)block);