3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
25 * \brief This file contains an implementaition of a pseudo-random-number generator.
28 * rndCore is expanded to 512 bits for more security.
31 ################################################################################################
33 # +---------------------------+ #
37 +---------------+ # o---------o (xor)+---------+ o---------o o----o o---------o # +--------------+
38 | entropy Block | -----> | sha-256 | --(offset)-< | rndCore | ---> | sha-256 | --+----| +1 |---> | sha-256 | -----> | random Block |
39 +---------------+ # o---------o (xor)+---------+ o---------o | o----o o---------o # +--------------+
43 # (offset)---------------------+ #
45 ################################################################################################
52 #include "entropium.h"
55 * \brief secret entropy pool.
56 * This is the core of the random which is generated
60 /*************************************************************************/
62 /* idea is: hash the message and add it via xor to rndCore
66 * we simply first "hash" rndCore, then entropy.
68 void entropium_addEntropy(unsigned length_b, const void* data){
70 static uint8_t offset=0; /* selects if higher or lower half gets updated */
72 sha256_nextBlock(&s, rndCore);
73 while (length_b>=512){
74 sha256_nextBlock(&s, data);
75 data = (uint8_t*)data+ 512/8;
78 sha256_lastBlock(&s, data, length_b);
81 rndCore[i+offset] ^= s.h[i];
83 offset ^= 8; /* hehe */
86 /*************************************************************************/
88 void entropium_getRandomBlock(void *b){
90 static uint8_t offset=8;
93 sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
96 rndCore[i+offset] ^= s.h[i];
98 offset ^= 8; /* hehe */
99 memcpy(b, s.h, 32); /* back up first hash in b */
100 ((uint8_t*)b)[*((uint8_t*)b)&31]++; /* the important increment step */
102 sha256_lastBlock(&s, b, 256);
106 /*************************************************************************/
108 uint8_t entropium_getRandomByte(void){
109 static uint8_t block[32];
113 entropium_getRandomBlock((void*)block);
119 void entropium_fillBlockRandom(void* block, unsigned length_B){
120 while(length_B>ENTROPIUM_RANDOMBLOCK_SIZE){
121 entropium_getRandomBlock(block);
122 block = (uint8_t*)block + ENTROPIUM_RANDOMBLOCK_SIZE;
123 length_B -= ENTROPIUM_RANDOMBLOCK_SIZE;
126 *((uint8_t*)block) = entropium_getRandomByte();
127 block= (uint8_t*)block +1; --length_B;