6 * Description: This file contains an implementaition of a pseudo-random-number generator.
8 * rndCore is expanded to 512 bits for more security.
14 * ####################################################################################
16 * # +---------------------------+ #
20 * +---------------+ # o---------o (xor)+---------+ o---------o o---------o # +--------------+
21 * | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
22 * +---------------+ # o---------o (xor)+---------+ o---------o | o---------o # +--------------+
26 * # (offset)---------------------+ #
28 * ####################################################################################
39 uint32_t rndCore[16]; /* secret */
42 * idea is: hash the message and add it via xor to rndCore
46 * we simply first "hash" rndCore, then entropy.
48 void addEntropy(unsigned length, void* data){
50 static uint8_t offset=0; /* selects if higher or lower half gets updated */
52 sha256_nextBlock(&s, rndCore);
54 sha256_nextBlock(&s, data);
58 sha256_lastBlock(&s, data, length);
61 rndCore[i+offset] ^= s.h[i];
63 offset ^= 8; /* hehe */
66 void getRandomBlock(uint32_t *b){
71 sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
74 rndCore[i+offset] ^= s.h[i];
76 offset ^= 8; /* hehe */
77 memcpy(b, s.h, 32); /* back up first hash in b */
79 sha256_lastBlock(&s, b, 256);
83 /* this does some simple buffering */
84 uint8_t getRandomByte(void){
85 static uint8_t block[32];
89 getRandomBlock((void*)block);