* 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 <stdint.h>
#include <string.h>
#include "sha256.h"
+#include "prng.h"
/**
* \brief secret entropy pool.
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);
}
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);
}
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;
+ }
+}